harmony 鸿蒙NativeVSync Development

  • 2023-10-30
  • 浏览 (555)

NativeVSync Development

When to Use

The 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.

Available APIs

API Description
OH_NativeVSync_Create (const char *name, unsigned int length) Creates an OH_NativeVSync instance. A new OH_NativeVSync instance is created each time this function is called.
OH_NativeVSync_Destroy (OH_NativeVSync *nativeVsync) Destroys an OH_NativeVSync instance.
OH_NativeVSync_FrameCallback (long long timestamp, void *data) Sets a callback function. timestamp indicates the timestamp, and data indicates the input parameter of the callback function.
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.

For details about the APIs, see native_vsync.

How to Develop

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 to CMakeLists.txt:

libnative_vsync.so

Header File

#include <native_vsync/native_vsync.h>
  1. Prepare a VSync callback function.

    #include <iostream>
    
    
    static bool flag = false;
    static void OnVSync(long long timestamp, void* data)
    {
        flag = true;
        std::cout << "OnVSync: " << timestamp << std::endl;
    }
    OH_NativeVSync_FrameCallback callback = OnVSync; // The callback function must be of the OH_NativeVSync_FrameCallback type.
    
  2. Create an OH_NativeVSync instance.

    char name[] = "hello_vsync";
    OH_NativeVSync* nativeVSync = OH_NativeVSync_Create(name, strlen(name));
    
  3. Set the VSync callback function through the OH_NativeVSync instance.

    #include <unistd.h>
    #include <iostream>
    
    
    OH_NativeVSync_RequestFrame(nativeVSync, callback, nullptr);
    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.
        std::cout << "wait for vsync!\n";
        sleep(1);
    }
    std::cout << "vsync come, end this thread\n";
    
  4. Destroy the OH_NativeVSync instance.

    OH_NativeVSync_Destroy(nativeVSync); // Destroy the OH_NativeVSync instance when the application does not need to receive VSync signals.
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Native APIs

harmony 鸿蒙CPU Features

harmony 鸿蒙Drawing Development

harmony 鸿蒙Drawing and Display Sample

harmony 鸿蒙Hardware Compatibility

harmony 鸿蒙Using MindSpore Lite for Model Inference

harmony 鸿蒙Using MindSpore Lite for Offline Model Conversion and Inference

harmony 鸿蒙Using Native APIs in Application Projects

harmony 鸿蒙NativeBuffer Development

harmony 鸿蒙NativeImage Development

0  赞