gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/internal/vk/vulkan_android.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  //go:build !nowayland
     4  // +build !nowayland
     5  
     6  package vk
     7  
     8  /*
     9  #define VK_USE_PLATFORM_ANDROID_KHR
    10  #define VK_NO_PROTOTYPES 1
    11  #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
    12  #include <android/native_window.h>
    13  #include <vulkan/vulkan.h>
    14  
    15  static VkResult vkCreateAndroidSurfaceKHR(PFN_vkCreateAndroidSurfaceKHR f, VkInstance instance, const VkAndroidSurfaceCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
    16  	return f(instance, pCreateInfo, pAllocator, pSurface);
    17  }
    18  */
    19  import "C"
    20  import (
    21  	"fmt"
    22  	"unsafe"
    23  )
    24  
    25  var wlFuncs struct {
    26  	vkCreateAndroidSurfaceKHR C.PFN_vkCreateAndroidSurfaceKHR
    27  }
    28  
    29  func init() {
    30  	loadFuncs = append(loadFuncs, func(dlopen func(name string) *[0]byte) {
    31  		wlFuncs.vkCreateAndroidSurfaceKHR = dlopen("vkCreateAndroidSurfaceKHR")
    32  	})
    33  }
    34  
    35  func CreateAndroidSurface(inst Instance, window unsafe.Pointer) (Surface, error) {
    36  	inf := C.VkAndroidSurfaceCreateInfoKHR{
    37  		sType:  C.VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR,
    38  		window: (*C.ANativeWindow)(window),
    39  	}
    40  	var surf Surface
    41  	if err := vkErr(C.vkCreateAndroidSurfaceKHR(wlFuncs.vkCreateAndroidSurfaceKHR, inst, &inf, nil, &surf)); err != nil {
    42  		return 0, fmt.Errorf("vulkan: vkCreateAndroidSurfaceKHR: %w", err)
    43  	}
    44  	return surf, nil
    45  }