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