1# Native VSync Development (C/C++) 2 3## When to Use 4 5The NativeVSync module is used to obtain virtual synchronization (VSync) signals from the system. It provides APIs for creating and destroying an **OH_NativeVSync** instance and setting the VSync callback function, which is triggered when a VSync signal arrives. 6 7## Available APIs 8 9| API| Description| 10| -------- | -------- | 11| OH_NativeVSync_Create (const char \*name, unsigned int length) | Creates an **OH_NativeVSync** instance. A new **OH_NativeVSync** instance is created each time this API is called. This function must be used in pair with **OH_NativeVSync_Destroy**. Otherwise, memory leak occurs.| 12| OH_NativeVSync_Destroy (OH_NativeVSync \*nativeVsync) | Destroys an **OH_NativeVSync** instance.| 13| OH_NativeVSync_FrameCallback (long long timestamp, void \*data) | Sets a callback function. **timestamp** indicates the timestamp, and **data** indicates a pointer to the input parameters of the callback function.| 14| OH_NativeVSync_RequestFrame (OH_NativeVSync \*nativeVsync, OH_NativeVSync_FrameCallback callback, void \*data) | Requests the next VSync signal. When the signal arrives, a callback function is invoked.| 15 16For details about the APIs, see [native_vsync](../reference/apis-arkgraphics2d/_native_vsync.md). 17 18## How to Develop 19 20The following steps describe how to use the native APIs provided by NativeVSync to create and destroy an **OH_NativeVSync** instance and set the VSync callback function. 21 22**Adding Dynamic Link Libraries** 23 24Add the following library to **CMakeLists.txt**: 25```txt 26libnative_vsync.so 27``` 28 29**Including Header Files** 30```c++ 31#include <native_vsync/native_vsync.h> 32``` 33 341. Implement a VSync callback function. 35 ```c++ 36 #include <iostream> 37 38 static bool flag = false; 39 static void OnVSync(long long timestamp, void* data) 40 { 41 flag = true; 42 std::cout << "OnVSync: " << timestamp << std::endl; 43 } 44 OH_NativeVSync_FrameCallback callback = OnVSync; // The callback function must be of the OH_NativeVSync_FrameCallback type. 45 ``` 462. Create an **OH_NativeVSync** instance. 47 ```c++ 48 char name[] = "hello_vsync"; 49 OH_NativeVSync* nativeVSync = OH_NativeVSync_Create(name, strlen(name)); 50 ``` 51 523. Set the VSync callback function through the **OH_NativeVSync** instance. 53 ```c++ 54 #include <unistd.h> 55 #include <iostream> 56 57 OH_NativeVSync_RequestFrame(nativeVSync, callback, nullptr); 58 while (!flag) { // Check the flag value. The while loop exits only after the VSync callback function is executed, indicating that a VSync signal is received. 59 std::cout << "wait for vsync!\n"; 60 sleep(1); 61 } 62 std::cout << "vsync come, end this thread\n"; 63 ``` 64 654. Destroy the **OH_NativeVSync** instance. 66 ```c++ 67 OH_NativeVSync_Destroy(nativeVSync); // Destroy the OH_NativeVSync instance when the application does not need to receive VSync signals. 68 ``` 69