1 /*
2  * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup NativeVsync
18  * @{
19  *
20  * @brief Provides the native vsync capability.
21  *
22  * @syscap SystemCapability.Graphic.Graphic2D.NativeVsync
23  * @since 9
24  * @version 1.0
25  */
26 
27 /**
28  * @file native_vsync.h
29  *
30  * @brief Defines the functions for obtaining and using a native vsync.
31  *
32  * @library libnative_vsync.so
33  * @since 9
34  * @version 1.0
35  */
36 
37 #ifndef NDK_INCLUDE_NATIVE_VSYNC_H_
38 #define NDK_INCLUDE_NATIVE_VSYNC_H_
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 struct OH_NativeVSync;
45 typedef struct OH_NativeVSync OH_NativeVSync;
46 typedef void (*OH_NativeVSync_FrameCallback)(long long timestamp, void *data);
47 
48 /**
49  * @brief Creates a <b>NativeVsync</b> instance.\n
50  * A new <b>NativeVsync</b> instance is created each time this function is called.
51  *
52  * @syscap SystemCapability.Graphic.Graphic2D.NativeVsync
53  * @param name Indicates the vsync connection name.
54  * @param length Indicates the name's length.
55  * @return Returns the pointer to the <b>NativeVsync</b> instance created.
56  * @since 9
57  * @version 1.0
58  */
59 OH_NativeVSync* OH_NativeVSync_Create(const char* name, unsigned int length);
60 
61 /**
62  * @brief Creates a associated with Xcomponentid <b>NativeVsync</b> instance.\n
63  * A new associated with Xcomponentid<b>NativeVsync</b> instance is created each time this function is called.
64  *
65  * @syscap SystemCapability.Graphic.Graphic2D.NativeVsync
66  * @param windowID Indicates the Xcomponentid of the associated window.
67  * @param name Indicates the vsync connection name.
68  * @param length Indicates the name's length.
69  * @return Returns the pointer to the <b>NativeVsync</b> instance created.
70  * @since 14
71  * @version 1.0
72  */
73 OH_NativeVSync* OH_NativeVSync_Create_ForAssociatedWindow(uint64_t windowID, const char* name, unsigned int length);
74 
75 /**
76  * @brief Delete the NativeVsync instance.
77  *
78  * @syscap SystemCapability.Graphic.Graphic2D.NativeVsync
79  * @param nativeVsync Indicates the pointer to a <b>NativeVsync</b> instance.
80  * @since 9
81  * @version 1.0
82  */
83 void OH_NativeVSync_Destroy(OH_NativeVSync* nativeVsync);
84 
85 /**
86  * @brief Request next vsync with callback.
87  * If you call this Interface multi times in one frame, it will only call the last callback.
88  *
89  * @syscap SystemCapability.Graphic.Graphic2D.NativeVsync
90  * @param nativeVsync Indicates the pointer to a NativeVsync.
91  * @param callback Indicates the OH_NativeVSync_FrameCallback which will be called when next vsync coming.
92  * @param data Indicates data which will be used in callback.
93  * @return {@link NATIVE_ERROR_OK} 0 - Success.
94  *     {@link NATIVE_ERROR_INVALID_ARGUMENTS} 40001000 - the parameter nativeVsync is NULL or callback is NULL.
95  *     {@link NATIVE_ERROR_BINDER_ERROR} 50401000 - ipc send failed.
96  * @since 9
97  * @version 1.0
98  */
99 int OH_NativeVSync_RequestFrame(OH_NativeVSync* nativeVsync, OH_NativeVSync_FrameCallback callback, void* data);
100 
101 /**
102  * @brief Request next vsync with callback.
103  * If this function is called multiple times in one vsync period, all these callbacks and datas be callbacked.
104  *
105  * @syscap SystemCapability.Graphic.Graphic2D.NativeVsync
106  * @param nativeVsync Indicates the pointer to a NativeVsync.
107  * @param callback Indicates the OH_NativeVSync_FrameCallback which will be called when next vsync coming.
108  * @param data Indicates data which will be used in callback.
109  * @return {@link NATIVE_ERROR_OK} 0 - Success.
110  *     {@link NATIVE_ERROR_INVALID_ARGUMENTS} 40001000 - the parameter nativeVsync is NULL or callback is NULL.
111  *     {@link NATIVE_ERROR_BINDER_ERROR} 50401000 - ipc send failed.
112  * @since 12
113  * @version 1.0
114  */
115 int OH_NativeVSync_RequestFrameWithMultiCallback(
116     OH_NativeVSync* nativeVsync, OH_NativeVSync_FrameCallback callback, void* data);
117 
118 /**
119  * @brief Get vsync period.
120  *
121  * @syscap SystemCapability.Graphic.Graphic2D.NativeVsync
122  * @param nativeVsync Indicates the pointer to a NativeVsync.
123  * @param period Indicates the vsync period.
124  * @return Returns int32_t, return value == 0, success, otherwise, failed.
125  * @since 10
126  * @version 1.0
127  */
128 int OH_NativeVSync_GetPeriod(OH_NativeVSync* nativeVsync, long long* period);
129 
130 /**
131  * @brief Enables DVSync to improve the smoothness of self-drawing animations.
132  * DVSync, short for Decoupled VSync, is a frame timing management policy that is decoupled from the hardware's VSync.
133  * DVSync drives the early rendering of upcoming animation frames by sending VSync signals with future timestamps.
134  * These frames are stored in a frame buffer queue. This helps DVSync reduce potential frame drop and therefore
135  * enhances the smoothness of animations.
136  * DVSync requires free self-drawing frame buffers to store these pre-rendered animation frames.
137  * Therefore, you must ensure that at least one free frame buffer is available. Otherwise, do not enable DVSync.
138  * After DVSync is enabled, you must correctly respond to the early VSync signals and request the subsequent VSync
139  * after the animation frame associated with the previous VSync is complete. In addition, the self-drawing frames must
140  * carry timestamps that align with VSync.
141  * After the animation ends, disable DVSync.
142  * Only phones and tablets support DVSync.
143  * On a platform that does not support DVSync or if another application has enabled DVSync, the attempt to enable it
144  * will not take effect, and the application still receives normal VSync signals.
145  *
146  * @syscap SystemCapability.Graphic.Graphic2D.NativeVsync
147  * @param nativeVsync Indicates the pointer to a NativeVsync.
148  * @param enable Whether to enable DVSync.The value true means to enable DVSync, and false means the opposite.
149  * @return {@link NATIVE_ERROR_OK} 0 - Success.
150  *     {@link NATIVE_ERROR_INVALID_ARGUMENTS} 40001000 - the parameter nativeVsync is NULL.
151  *     {@link NATIVE_ERROR_BINDER_ERROR} 50401000 - ipc send failed.
152  * @since 14
153  * @version 1.0
154  */
155 int OH_NativeVSync_DVSyncSwitch(OH_NativeVSync* nativeVsync, bool enable);
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 /** @} */
161 #endif