1e41f4b71Sopenharmony_ci# Vulkan Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciVulkan is a set of graphics APIs for 2D and 3D rendering. To start with, you need to create a **VkSurfaceKHR** instance, which works with the **OHNativeWindow** module to implement buffer recycling. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciA **VkSurfaceKHR** instance is obtained through an **OHNativeWindow**, which is obtained from the **\<XComponent>**. Therefore, the **OHNativeWindow** module must be used together with the **XComponent** and **NativeWindow** modules. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Available APIs 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci| API | Description | 12e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ---------------------- | 13e41f4b71Sopenharmony_ci| vkCreateSurfaceOHOS (VkInstance instance, const VkSurfaceCreateInfoOHOS\* pCreateInfo, const VkAllocationCallbacks\* pAllocator, VkSurfaceKHR\* pSurface) | Creates a **VkSurfaceKHR** instance.| 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciFor details about the APIs, see [Vulkan](vulkan.md). 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## How to Develop 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciThe following steps illustrate how to create a **VkSurfaceKHR** instance. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciTo use the extended APIs, define the macro **VK_USE_PLATFORM_OHOS** in the **CMakeLists.txt** file. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci```txt 24e41f4b71Sopenharmony_ciADD_DEFINITIONS(-DVK_USE_PLATFORM_OHOS=1) 25e41f4b71Sopenharmony_ci``` 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**Adding Dynamic Link Libraries** 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciAdd the following libraries to **CMakeLists.txt**: 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci```txt 32e41f4b71Sopenharmony_cilibace_ndk.z.so 33e41f4b71Sopenharmony_cilibnative_window.so 34e41f4b71Sopenharmony_cilibvulkan.so 35e41f4b71Sopenharmony_ci``` 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Including Header Files** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci```c++ 40e41f4b71Sopenharmony_ci#include <ace/xcomponent/native_interface_xcomponent.h> 41e41f4b71Sopenharmony_ci#include <native_window/external_window.h> 42e41f4b71Sopenharmony_ci#include <vulkan/vulkan.h> 43e41f4b71Sopenharmony_ci``` 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci1. Create a Vulkan instance. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci ```c++ 48e41f4b71Sopenharmony_ci VkInstance instance = VK_NULL_HANDLE; 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci VkApplicationInfo appInfo = {}; 51e41f4b71Sopenharmony_ci appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; 52e41f4b71Sopenharmony_ci appInfo.pApplicationName = "vulkanExample"; 53e41f4b71Sopenharmony_ci appInfo.pEngineName = "vulkanExample"; 54e41f4b71Sopenharmony_ci appInfo.apiVersion = VK_API_VERSION_1_3; 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci VkInstanceCreateInfo instanceCreateInfo = {}; 57e41f4b71Sopenharmony_ci instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; 58e41f4b71Sopenharmony_ci instanceCreateInfo.pNext = NULL; 59e41f4b71Sopenharmony_ci instanceCreateInfo.pApplicationInfo = &appInfo; 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci std::vector<const char *> instanceExtensions = { 62e41f4b71Sopenharmony_ci VK_KHR_SURFACE_EXTENSION_NAME, 63e41f4b71Sopenharmony_ci VK_OHOS_SURFACE_EXTENSION_NAME // Surface extension. 64e41f4b71Sopenharmony_ci }; 65e41f4b71Sopenharmony_ci instanceCreateInfo.enabledExtensionCount = static_cast<uint32_t>(instanceExtensions.size()); 66e41f4b71Sopenharmony_ci instanceCreateInfo.ppEnabledExtensionNames = instanceExtensions.data(); 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci vkCreateInstance(&instanceCreateInfo, nullptr, &instance); 69e41f4b71Sopenharmony_ci ``` 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci2. Obtain an **OHNativeWindow** instance. 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci The **OHNativeWindow** instance is obtained from the **\<XComponent>**. For details about how to use the **\<XComponent>**, see [XComponent](../../ui/arkts-common-components-xcomponent.md) and [XComponent Development](../../ui/napi-xcomponent-guidelines.md). 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci 1. Add an **\<XComponent>** to **ets/pages/Index.ets**. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci ```ts 78e41f4b71Sopenharmony_ci XComponent({ 79e41f4b71Sopenharmony_ci id: 'xcomponentId', 80e41f4b71Sopenharmony_ci type: 'surface', 81e41f4b71Sopenharmony_ci libraryname: 'entry' 82e41f4b71Sopenharmony_ci }) 83e41f4b71Sopenharmony_ci .margin({ bottom: 20 }) 84e41f4b71Sopenharmony_ci .width(360) 85e41f4b71Sopenharmony_ci .height(360) 86e41f4b71Sopenharmony_ci ``` 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci 2. Obtain an **OHNativeWindow** instance from the **\<XComponent>**. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci ```c++ 91e41f4b71Sopenharmony_ci // Callback function of the \<XComponent> triggered when a surface is created. 92e41f4b71Sopenharmony_ci void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window) { 93e41f4b71Sopenharmony_ci // You can obtain an OHNativeWindow instance from the callback function. 94e41f4b71Sopenharmony_ci OHNativeWindow *nativeWindow = static_cast<OHNativeWindow *>(window); 95e41f4b71Sopenharmony_ci } 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci static napi_value Init(napi_env env, napi_value exports) { 98e41f4b71Sopenharmony_ci napi_property_descriptor desc[] = {{"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr}}; 99e41f4b71Sopenharmony_ci napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci napi_value exportInstance = nullptr; 102e41f4b71Sopenharmony_ci OH_NativeXComponent *nativeXComponent = nullptr; 103e41f4b71Sopenharmony_ci // Obtain a native XComponent. 104e41f4b71Sopenharmony_ci napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance); 105e41f4b71Sopenharmony_ci napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent)); 106e41f4b71Sopenharmony_ci // Obtain the XComponent ID. 107e41f4b71Sopenharmony_ci char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {}; 108e41f4b71Sopenharmony_ci uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; 109e41f4b71Sopenharmony_ci OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize); 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci // Declare an XComponent callback. 112e41f4b71Sopenharmony_ci OH_NativeXComponent_Callback callback; 113e41f4b71Sopenharmony_ci // Register the OnSurfaceCreated callback function. 114e41f4b71Sopenharmony_ci callback.OnSurfaceCreated = OnSurfaceCreatedCB; 115e41f4b71Sopenharmony_ci // Register the callback function for the native XComponent. 116e41f4b71Sopenharmony_ci OH_NativeXComponent_RegisterCallback(nativeXComponent, &callback); 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci return exports; 119e41f4b71Sopenharmony_ci } 120e41f4b71Sopenharmony_ci ``` 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci3. Create a **VkSurfaceKHR** instance. 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci ```c++ 125e41f4b71Sopenharmony_ci VkSurfaceKHR surface = VK_NULL_HANDLE; 126e41f4b71Sopenharmony_ci VkSurfaceCreateInfoOHOS surfaceCreateInfo = {}; 127e41f4b71Sopenharmony_ci surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_SURFACE_CREATE_INFO_OHOS; 128e41f4b71Sopenharmony_ci surfaceCreateInfo.window = nativeWindow; // nativeWindow is obtained from the OnSurfaceCreatedCB callback function in the previous step. 129e41f4b71Sopenharmony_ci int err = vkCreateSurfaceOHOS(instance, &surfaceCreateInfo, NULL, &surface); 130e41f4b71Sopenharmony_ci if (err != VK_SUCCESS) { 131e41f4b71Sopenharmony_ci // Creating the surface failed. 132e41f4b71Sopenharmony_ci } 133e41f4b71Sopenharmony_ci ``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciFor details about how to use Vulkan, visit the [Vulkan official website](https://www.vulkan.org/). 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci <!--no_check-->