1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <nativebase/nativebase.h>
20 #include <stdarg.h>
21 
22 // apex is a superset of the NDK
23 #include <android/native_window.h>
24 
25 __BEGIN_DECLS
26 
27 /*
28  * perform bits that can be used with ANativeWindow_perform()
29  *
30  * This is only to support the intercepting methods below - these should notbe
31  * used directly otherwise.
32  */
33 enum ANativeWindowPerform {
34     // clang-format off
35     ANATIVEWINDOW_PERFORM_SET_USAGE            = 0,
36     ANATIVEWINDOW_PERFORM_SET_BUFFERS_GEOMETRY = 5,
37     ANATIVEWINDOW_PERFORM_SET_BUFFERS_FORMAT   = 9,
38     ANATIVEWINDOW_PERFORM_SET_USAGE64          = 30,
39     // clang-format on
40 };
41 
42 /**
43  * Prototype of the function that an ANativeWindow implementation would call
44  * when ANativeWindow_cancelBuffer is called.
45  */
46 typedef int (*ANativeWindow_cancelBufferFn)(ANativeWindow* window, ANativeWindowBuffer* buffer,
47                                             int fenceFd);
48 
49 /**
50  * Prototype of the function that intercepts an invocation of
51  * ANativeWindow_cancelBufferFn, along with a data pointer that's passed by the
52  * caller who set the interceptor, as well as arguments that would be
53  * passed to ANativeWindow_cancelBufferFn if it were to be called.
54  */
55 typedef int (*ANativeWindow_cancelBufferInterceptor)(ANativeWindow* window,
56                                                      ANativeWindow_cancelBufferFn cancelBuffer,
57                                                      void* data, ANativeWindowBuffer* buffer,
58                                                      int fenceFd);
59 
60 /**
61  * Prototype of the function that an ANativeWindow implementation would call
62  * when ANativeWindow_dequeueBuffer is called.
63  */
64 typedef int (*ANativeWindow_dequeueBufferFn)(ANativeWindow* window, ANativeWindowBuffer** buffer,
65                                              int* fenceFd);
66 
67 /**
68  * Prototype of the function that intercepts an invocation of
69  * ANativeWindow_dequeueBufferFn, along with a data pointer that's passed by the
70  * caller who set the interceptor, as well as arguments that would be
71  * passed to ANativeWindow_dequeueBufferFn if it were to be called.
72  */
73 typedef int (*ANativeWindow_dequeueBufferInterceptor)(ANativeWindow* window,
74                                                       ANativeWindow_dequeueBufferFn dequeueBuffer,
75                                                       void* data, ANativeWindowBuffer** buffer,
76                                                       int* fenceFd);
77 
78 /**
79  * Prototype of the function that an ANativeWindow implementation would call
80  * when ANativeWindow_perform is called.
81  */
82 typedef int (*ANativeWindow_performFn)(ANativeWindow* window, int operation, va_list args);
83 
84 /**
85  * Prototype of the function that intercepts an invocation of
86  * ANativeWindow_performFn, along with a data pointer that's passed by the
87  * caller who set the interceptor, as well as arguments that would be
88  * passed to ANativeWindow_performFn if it were to be called.
89  */
90 typedef int (*ANativeWindow_performInterceptor)(ANativeWindow* window,
91                                                 ANativeWindow_performFn perform, void* data,
92                                                 int operation, va_list args);
93 
94 /**
95  * Prototype of the function that an ANativeWindow implementation would call
96  * when ANativeWindow_queueBuffer is called.
97  */
98 typedef int (*ANativeWindow_queueBufferFn)(ANativeWindow* window, ANativeWindowBuffer* buffer,
99                                            int fenceFd);
100 
101 /**
102  * Prototype of the function that intercepts an invocation of
103  * ANativeWindow_queueBufferFn, along with a data pointer that's passed by the
104  * caller who set the interceptor, as well as arguments that would be
105  * passed to ANativeWindow_queueBufferFn if it were to be called.
106  */
107 typedef int (*ANativeWindow_queueBufferInterceptor)(ANativeWindow* window,
108                                                     ANativeWindow_queueBufferFn queueBuffer,
109                                                     void* data, ANativeWindowBuffer* buffer,
110                                                     int fenceFd);
111 
112 /**
113  * Registers an interceptor for ANativeWindow_cancelBuffer. Instead of calling
114  * the underlying cancelBuffer function, instead the provided interceptor is
115  * called, which may optionally call the underlying cancelBuffer function. An
116  * optional data pointer is also provided to side-channel additional arguments.
117  *
118  * Note that usage of this should only be used for specialized use-cases by
119  * either the system partition or to Mainline modules. This should never be
120  * exposed to NDK or LL-NDK.
121  *
122  * Returns NO_ERROR on success, -errno if registration failed.
123  */
124 int ANativeWindow_setCancelBufferInterceptor(ANativeWindow* window,
125                                              ANativeWindow_cancelBufferInterceptor interceptor,
126                                              void* data);
127 
128 /**
129  * Registers an interceptor for ANativeWindow_dequeueBuffer. Instead of calling
130  * the underlying dequeueBuffer function, instead the provided interceptor is
131  * called, which may optionally call the underlying dequeueBuffer function. An
132  * optional data pointer is also provided to side-channel additional arguments.
133  *
134  * Note that usage of this should only be used for specialized use-cases by
135  * either the system partition or to Mainline modules. This should never be
136  * exposed to NDK or LL-NDK.
137  *
138  * Returns NO_ERROR on success, -errno if registration failed.
139  */
140 int ANativeWindow_setDequeueBufferInterceptor(ANativeWindow* window,
141                                               ANativeWindow_dequeueBufferInterceptor interceptor,
142                                               void* data);
143 /**
144  * Registers an interceptor for ANativeWindow_perform. Instead of calling
145  * the underlying perform function, instead the provided interceptor is
146  * called, which may optionally call the underlying perform function. An
147  * optional data pointer is also provided to side-channel additional arguments.
148  *
149  * Note that usage of this should only be used for specialized use-cases by
150  * either the system partition or to Mainline modules. This should never be
151  * exposed to NDK or LL-NDK.
152  *
153  * Returns NO_ERROR on success, -errno if registration failed.
154  */
155 int ANativeWindow_setPerformInterceptor(ANativeWindow* window,
156                                         ANativeWindow_performInterceptor interceptor, void* data);
157 /**
158  * Registers an interceptor for ANativeWindow_queueBuffer. Instead of calling
159  * the underlying queueBuffer function, instead the provided interceptor is
160  * called, which may optionally call the underlying queueBuffer function. An
161  * optional data pointer is also provided to side-channel additional arguments.
162  *
163  * Note that usage of this should only be used for specialized use-cases by
164  * either the system partition or to Mainline modules. This should never be
165  * exposed to NDK or LL-NDK.
166  *
167  * Returns NO_ERROR on success, -errno if registration failed.
168  */
169 int ANativeWindow_setQueueBufferInterceptor(ANativeWindow* window,
170                                             ANativeWindow_queueBufferInterceptor interceptor,
171                                             void* data);
172 
173 /**
174  * Retrieves how long it took for the last time a buffer was dequeued.
175  *
176  * \return the dequeue duration in nanoseconds
177  */
178 int64_t ANativeWindow_getLastDequeueDuration(ANativeWindow* window);
179 
180 /**
181  * Retrieves how long it took for the last time a buffer was queued.
182  *
183  * \return the queue duration in nanoseconds
184  */
185 int64_t ANativeWindow_getLastQueueDuration(ANativeWindow* window);
186 
187 /**
188  * Retrieves the system time in nanoseconds when the last time a buffer
189  * started to be dequeued.
190  *
191  * \return the start time in nanoseconds
192  */
193 int64_t ANativeWindow_getLastDequeueStartTime(ANativeWindow* window);
194 
195 /**
196  * Sets a timeout in nanoseconds for dequeue calls. All subsequent dequeue calls
197  * made by the window will return -ETIMEDOUT after the timeout if the dequeue
198  * takes too long.
199  *
200  * If the provided timeout is negative, hen this removes the previously configured
201  * timeout. The window then behaves as if ANativeWindow_setDequeueTimeout was
202  * never called.
203  *
204  * \return NO_ERROR on success
205  * \return BAD_VALUE if the dequeue timeout was unabled to be updated, as
206  * updating the dequeue timeout may change internals of the underlying window.
207  */
208 int ANativeWindow_setDequeueTimeout(ANativeWindow* window, int64_t timeout);
209 
210 __END_DECLS
211