The NativeVSync module is used to obtain the system VSync signal, create and destroy an OH_NativeVSync instance, and set the VSync callback function. When the VSync signal is triggered, the callback function is called.
| API | Description |
|---|---|
| OH_NativeVSync_Create(const char* name, unsigned int length) | Creates an OH_NativeVSync instance. Each call creates a new instance and a vsync thread to receive and process callbacks. This API must be used together with OH_NativeVSync_Destroy; otherwise, memory leaks may occur. |
| OH_NativeVSync_Destroy(OH_NativeVSync* nativeVsync) | Destroys the OH_NativeVSync instance. |
| OH_NativeVSync_FrameCallback(long long timestamp, void *data) | Defines the callback function. timestamp indicates the timestamp, and data is the input parameter of the callback function. The callback is processed in the thread created during vsync initialization. |
| OH_NativeVSync_RequestFrame(OH_NativeVSync* nativeVsync, OH_NativeVSync_FrameCallback callback, void* data) | Requests the next VSync signal. When the signal arrives, the callback function is invoked. |
| OH_NativeVSync_GetPeriod(OH_NativeVSync* nativeVsync, long long* period) | Obtains the current Vsync signal period. |
For details about the APIs, see NativeVsync.
The 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.
Adding Dynamic Link Libraries
Add the following library file to the CMakeLists.txt file.
libnative_vsync.so
Including Header Files
#include <native_vsync/native_vsync.h>
Define a VSync callback function.
void RenderEngine::OnVsync(long long timestamp, void *data)
{
OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_PRINT_DOMAIN, "RenderEngine", "OnVsync %{public}lld.", timestamp);
auto renderEngine = reinterpret_cast<RenderEngine *>(data);
if (renderEngine == nullptr) {
return;
}
renderEngine->vSyncCnt_++;
renderEngine->wakeUpCond_.notify_one();
} Create an OH_NativeVSync instance.
const char* demoName = "NativeImageSample"; nativeVsync_ = OH_NativeVSync_Create(demoName, strlen(demoName));
Set the VSync callback function through the OH_NativeVSync instance.
wakeUpCond_.wait(lock, [this]() { return wakeUp_ || vSyncCnt_ > 0; });
wakeUp_ = false;
if (vSyncCnt_ > 0) {
vSyncCnt_--;
(void)OH_NativeVSync_RequestFrame(nativeVsync_, &RenderEngine::OnVsync, this);
OH_NativeVSync_GetPeriod(nativeVsync_, &period);
} Destroy the OH_NativeVSync instance.
OH_NativeVSync_Destroy(nativeVsync_); nativeVsync_ = nullptr;