• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 #ifndef ANDROID_ANDROID_NATIVES_H
18 #define ANDROID_ANDROID_NATIVES_H
19 
20 #include <sys/types.h>
21 #include <string.h>
22 
23 #include <hardware/gralloc.h>
24 
25 #include <android/native_window.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /*****************************************************************************/
32 
33 #define ANDROID_NATIVE_MAKE_CONSTANT(a,b,c,d) \
34     (((unsigned)(a)<<24)|((unsigned)(b)<<16)|((unsigned)(c)<<8)|(unsigned)(d))
35 
36 #define ANDROID_NATIVE_WINDOW_MAGIC \
37     ANDROID_NATIVE_MAKE_CONSTANT('_','w','n','d')
38 
39 #define ANDROID_NATIVE_BUFFER_MAGIC \
40     ANDROID_NATIVE_MAKE_CONSTANT('_','b','f','r')
41 
42 // ---------------------------------------------------------------------------
43 
44 struct android_native_buffer_t;
45 
46 typedef struct android_native_rect_t
47 {
48     int32_t left;
49     int32_t top;
50     int32_t right;
51     int32_t bottom;
52 } android_native_rect_t;
53 
54 // ---------------------------------------------------------------------------
55 
56 typedef struct android_native_base_t
57 {
58     /* a magic value defined by the actual EGL native type */
59     int magic;
60 
61     /* the sizeof() of the actual EGL native type */
62     int version;
63 
64     void* reserved[4];
65 
66     /* reference-counting interface */
67     void (*incRef)(struct android_native_base_t* base);
68     void (*decRef)(struct android_native_base_t* base);
69 } android_native_base_t;
70 
71 // ---------------------------------------------------------------------------
72 
73 /* attributes queriable with query() */
74 enum {
75     NATIVE_WINDOW_WIDTH     = 0,
76     NATIVE_WINDOW_HEIGHT,
77     NATIVE_WINDOW_FORMAT,
78 };
79 
80 /* valid operations for the (*perform)() hook */
81 enum {
82     NATIVE_WINDOW_SET_USAGE  = 0,
83     NATIVE_WINDOW_CONNECT,
84     NATIVE_WINDOW_DISCONNECT,
85     NATIVE_WINDOW_SET_CROP,
86     NATIVE_WINDOW_SET_BUFFER_COUNT,
87     NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
88     NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
89 };
90 
91 /* parameter for NATIVE_WINDOW_[DIS]CONNECT */
92 enum {
93     NATIVE_WINDOW_API_EGL = 1
94 };
95 
96 /* parameter for NATIVE_WINDOW_SET_BUFFERS_TRANSFORM */
97 enum {
98     /* flip source image horizontally */
99     NATIVE_WINDOW_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H ,
100     /* flip source image vertically */
101     NATIVE_WINDOW_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
102     /* rotate source image 90 degrees clock-wise */
103     NATIVE_WINDOW_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
104     /* rotate source image 180 degrees */
105     NATIVE_WINDOW_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
106     /* rotate source image 270 degrees clock-wise */
107     NATIVE_WINDOW_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
108 };
109 
110 struct ANativeWindow
111 {
112 #ifdef __cplusplus
ANativeWindowANativeWindow113     ANativeWindow()
114         : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
115     {
116         common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
117         common.version = sizeof(ANativeWindow);
118         memset(common.reserved, 0, sizeof(common.reserved));
119     }
120 
121     // Implement the methods that sp<ANativeWindow> expects so that it
122     // can be used to automatically refcount ANativeWindow's.
incStrongANativeWindow123     void incStrong(const void* id) const {
124         common.incRef(const_cast<android_native_base_t*>(&common));
125     }
decStrongANativeWindow126     void decStrong(const void* id) const {
127         common.decRef(const_cast<android_native_base_t*>(&common));
128     }
129 #endif
130 
131     struct android_native_base_t common;
132 
133     /* flags describing some attributes of this surface or its updater */
134     const uint32_t flags;
135 
136     /* min swap interval supported by this updated */
137     const int   minSwapInterval;
138 
139     /* max swap interval supported by this updated */
140     const int   maxSwapInterval;
141 
142     /* horizontal and vertical resolution in DPI */
143     const float xdpi;
144     const float ydpi;
145 
146     /* Some storage reserved for the OEM's driver. */
147     intptr_t    oem[4];
148 
149 
150     /*
151      * Set the swap interval for this surface.
152      *
153      * Returns 0 on success or -errno on error.
154      */
155     int     (*setSwapInterval)(struct ANativeWindow* window,
156                 int interval);
157 
158     /*
159      * hook called by EGL to acquire a buffer. After this call, the buffer
160      * is not locked, so its content cannot be modified.
161      * this call may block if no buffers are available.
162      *
163      * Returns 0 on success or -errno on error.
164      */
165     int     (*dequeueBuffer)(struct ANativeWindow* window,
166                 struct android_native_buffer_t** buffer);
167 
168     /*
169      * hook called by EGL to lock a buffer. This MUST be called before modifying
170      * the content of a buffer. The buffer must have been acquired with
171      * dequeueBuffer first.
172      *
173      * Returns 0 on success or -errno on error.
174      */
175     int     (*lockBuffer)(struct ANativeWindow* window,
176                 struct android_native_buffer_t* buffer);
177    /*
178     * hook called by EGL when modifications to the render buffer are done.
179     * This unlocks and post the buffer.
180     *
181     * Buffers MUST be queued in the same order than they were dequeued.
182     *
183     * Returns 0 on success or -errno on error.
184     */
185     int     (*queueBuffer)(struct ANativeWindow* window,
186                 struct android_native_buffer_t* buffer);
187 
188     /*
189      * hook used to retrieve information about the native window.
190      *
191      * Returns 0 on success or -errno on error.
192      */
193     int     (*query)(struct ANativeWindow* window,
194                 int what, int* value);
195 
196     /*
197      * hook used to perform various operations on the surface.
198      * (*perform)() is a generic mechanism to add functionality to
199      * ANativeWindow while keeping backward binary compatibility.
200      *
201      * This hook should not be called directly, instead use the helper functions
202      * defined below.
203      *
204      *  (*perform)() returns -ENOENT if the 'what' parameter is not supported
205      *  by the surface's implementation.
206      *
207      * The valid operations are:
208      *     NATIVE_WINDOW_SET_USAGE
209      *     NATIVE_WINDOW_CONNECT
210      *     NATIVE_WINDOW_DISCONNECT
211      *     NATIVE_WINDOW_SET_CROP
212      *     NATIVE_WINDOW_SET_BUFFER_COUNT
213      *     NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
214      *     NATIVE_WINDOW_SET_BUFFERS_TRANSFORM
215      *
216      */
217 
218     int     (*perform)(struct ANativeWindow* window,
219                 int operation, ... );
220 
221     /*
222      * hook used to cancel a buffer that has been dequeued.
223      * No synchronization is performed between dequeue() and cancel(), so
224      * either external synchronization is needed, or these functions must be
225      * called from the same thread.
226      */
227     int     (*cancelBuffer)(struct ANativeWindow* window,
228                 struct android_native_buffer_t* buffer);
229 
230 
231     void* reserved_proc[2];
232 };
233 
234 // Backwards compatibility...  please switch to ANativeWindow.
235 typedef struct ANativeWindow android_native_window_t;
236 
237 /*
238  *  native_window_set_usage(..., usage)
239  *  Sets the intended usage flags for the next buffers
240  *  acquired with (*lockBuffer)() and on.
241  *  By default (if this function is never called), a usage of
242  *      GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
243  *  is assumed.
244  *  Calling this function will usually cause following buffers to be
245  *  reallocated.
246  */
247 
native_window_set_usage(ANativeWindow * window,int usage)248 static inline int native_window_set_usage(
249         ANativeWindow* window, int usage)
250 {
251     return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
252 }
253 
254 /*
255  * native_window_connect(..., NATIVE_WINDOW_API_EGL)
256  * Must be called by EGL when the window is made current.
257  * Returns -EINVAL if for some reason the window cannot be connected, which
258  * can happen if it's connected to some other API.
259  */
native_window_connect(ANativeWindow * window,int api)260 static inline int native_window_connect(
261         ANativeWindow* window, int api)
262 {
263     return window->perform(window, NATIVE_WINDOW_CONNECT, api);
264 }
265 
266 /*
267  * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
268  * Must be called by EGL when the window is made not current.
269  * An error is returned if for instance the window wasn't connected in the
270  * first place.
271  */
native_window_disconnect(ANativeWindow * window,int api)272 static inline int native_window_disconnect(
273         ANativeWindow* window, int api)
274 {
275     return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
276 }
277 
278 /*
279  * native_window_set_crop(..., crop)
280  * Sets which region of the next queued buffers needs to be considered.
281  * A buffer's crop region is scaled to match the surface's size.
282  *
283  * The specified crop region applies to all buffers queued after it is called.
284  *
285  * if 'crop' is NULL, subsequently queued buffers won't be cropped.
286  *
287  * An error is returned if for instance the crop region is invalid,
288  * out of the buffer's bound or if the window is invalid.
289  */
native_window_set_crop(ANativeWindow * window,android_native_rect_t const * crop)290 static inline int native_window_set_crop(
291         ANativeWindow* window,
292         android_native_rect_t const * crop)
293 {
294     return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
295 }
296 
297 /*
298  * native_window_set_buffer_count(..., count)
299  * Sets the number of buffers associated with this native window.
300  */
native_window_set_buffer_count(ANativeWindow * window,size_t bufferCount)301 static inline int native_window_set_buffer_count(
302         ANativeWindow* window,
303         size_t bufferCount)
304 {
305     return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
306 }
307 
308 /*
309  * native_window_set_buffers_geometry(..., int w, int h, int format)
310  * All buffers dequeued after this call will have the geometry specified.
311  * In particular, all buffers will have a fixed-size, independent form the
312  * native-window size. They will be appropriately scaled to the window-size
313  * upon composition.
314  *
315  * If all parameters are 0, the normal behavior is restored. That is,
316  * dequeued buffers following this call will be sized to the window's size.
317  *
318  */
native_window_set_buffers_geometry(ANativeWindow * window,int w,int h,int format)319 static inline int native_window_set_buffers_geometry(
320         ANativeWindow* window,
321         int w, int h, int format)
322 {
323     return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
324             w, h, format);
325 }
326 
327 /*
328  * native_window_set_buffers_transform(..., int transform)
329  * All buffers queued after this call will be displayed transformed according
330  * to the transform parameter specified.
331  */
native_window_set_buffers_transform(ANativeWindow * window,int transform)332 static inline int native_window_set_buffers_transform(
333         ANativeWindow* window,
334         int transform)
335 {
336     return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
337             transform);
338 }
339 
340 // ---------------------------------------------------------------------------
341 
342 /* FIXME: this is legacy for pixmaps */
343 typedef struct egl_native_pixmap_t
344 {
345     int32_t     version;    /* must be 32 */
346     int32_t     width;
347     int32_t     height;
348     int32_t     stride;
349     uint8_t*    data;
350     uint8_t     format;
351     uint8_t     rfu[3];
352     union {
353         uint32_t    compressedFormat;
354         int32_t     vstride;
355     };
356     int32_t     reserved;
357 } egl_native_pixmap_t;
358 
359 /*****************************************************************************/
360 
361 #ifdef __cplusplus
362 }
363 #endif
364 
365 
366 /*****************************************************************************/
367 
368 #ifdef __cplusplus
369 
370 #include <utils/RefBase.h>
371 
372 namespace android {
373 
374 /*
375  * This helper class turns an EGL android_native_xxx type into a C++
376  * reference-counted object; with proper type conversions.
377  */
378 template <typename NATIVE_TYPE, typename TYPE, typename REF>
379 class EGLNativeBase : public NATIVE_TYPE, public REF
380 {
381 public:
382     // Disambiguate between the incStrong in REF and NATIVE_TYPE
incStrong(const void * id)383     void incStrong(const void* id) const {
384         REF::incStrong(id);
385     }
decStrong(const void * id)386     void decStrong(const void* id) const {
387         REF::decStrong(id);
388     }
389 
390 protected:
391     typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
EGLNativeBase()392     EGLNativeBase() : NATIVE_TYPE(), REF() {
393         NATIVE_TYPE::common.incRef = incRef;
394         NATIVE_TYPE::common.decRef = decRef;
395     }
getSelf(NATIVE_TYPE * self)396     static inline TYPE* getSelf(NATIVE_TYPE* self) {
397         return static_cast<TYPE*>(self);
398     }
getSelf(NATIVE_TYPE const * self)399     static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
400         return static_cast<TYPE const *>(self);
401     }
getSelf(android_native_base_t * base)402     static inline TYPE* getSelf(android_native_base_t* base) {
403         return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
404     }
getSelf(android_native_base_t const * base)405     static inline TYPE const * getSelf(android_native_base_t const* base) {
406         return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
407     }
incRef(android_native_base_t * base)408     static void incRef(android_native_base_t* base) {
409         EGLNativeBase* self = getSelf(base);
410         self->incStrong(self);
411     }
decRef(android_native_base_t * base)412     static void decRef(android_native_base_t* base) {
413         EGLNativeBase* self = getSelf(base);
414         self->decStrong(self);
415     }
416 };
417 
418 } // namespace android
419 #endif // __cplusplus
420 
421 /*****************************************************************************/
422 
423 #endif /* ANDROID_ANDROID_NATIVES_H */
424