blob: 0ac34d04d55a049c694c414cca36c37178123fe8 [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
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
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -070025#include <android/native_window.h>
26
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#ifdef __cplusplus
28extern "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
44struct android_native_buffer_t;
45
Mathias Agopiancc08e682010-04-15 18:48:26 -070046typedef 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
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054// ---------------------------------------------------------------------------
55
Mathias Agopian238a66e2009-08-13 17:57:53 -070056typedef struct android_native_base_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057{
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 */
Mathias Agopian37b2a372009-08-17 12:33:20 -070067 void (*incRef)(struct android_native_base_t* base);
68 void (*decRef)(struct android_native_base_t* base);
Mathias Agopian238a66e2009-08-13 17:57:53 -070069} android_native_base_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -070071// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072
Mathias Agopiancb6b9042009-07-30 18:14:56 -070073/* attributes queriable with query() */
74enum {
75 NATIVE_WINDOW_WIDTH = 0,
Mathias Agopiancc08e682010-04-15 18:48:26 -070076 NATIVE_WINDOW_HEIGHT,
77 NATIVE_WINDOW_FORMAT,
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080078
79 /* The minimum number of buffers that must remain un-dequeued after a buffer
80 * has been queued. This value applies only if set_buffer_count was used to
81 * override the number of buffers and if a buffer has since been queued.
82 * Users of the set_buffer_count ANativeWindow method should query this
83 * value before calling set_buffer_count. If it is necessary to have N
84 * buffers simultaneously dequeued as part of the steady-state operation,
85 * and this query returns M then N+M buffers should be requested via
86 * native_window_set_buffer_count.
87 *
88 * Note that this value does NOT apply until a single buffer has been
89 * queued. In particular this means that it is possible to:
90 *
91 * 1. Query M = min undequeued buffers
92 * 2. Set the buffer count to N + M
93 * 3. Dequeue all N + M buffers
94 * 4. Cancel M buffers
95 * 5. Queue, dequeue, queue, dequeue, ad infinitum
96 */
97 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
Jamie Gennis134f0422011-03-08 12:18:54 -080098
99 /* Check whether queueBuffer operations on the ANativeWindow send the buffer
100 * to the window compositor. The query sets the returned 'value' argument
101 * to 1 if the ANativeWindow DOES send queued buffers directly to the window
102 * compositor and 0 if the buffers do not go directly to the window
103 * compositor.
104 *
105 * This can be used to determine whether protected buffer content should be
106 * sent to the ANativeWindow. Note, however, that a result of 1 does NOT
107 * indicate that queued buffers will be protected from applications or users
108 * capturing their contents. If that behavior is desired then some other
109 * mechanism (e.g. the GRALLOC_USAGE_PROTECTED flag) should be used in
110 * conjunction with this query.
111 */
112 NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700113};
114
Mathias Agopian52212712009-08-11 22:34:02 -0700115/* valid operations for the (*perform)() hook */
116enum {
Mathias Agopiane156e642010-03-11 15:05:52 -0800117 NATIVE_WINDOW_SET_USAGE = 0,
Mathias Agopiancc08e682010-04-15 18:48:26 -0700118 NATIVE_WINDOW_CONNECT,
119 NATIVE_WINDOW_DISCONNECT,
120 NATIVE_WINDOW_SET_CROP,
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700121 NATIVE_WINDOW_SET_BUFFER_COUNT,
Mathias Agopiana138f892010-05-21 17:24:35 -0700122 NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
Mathias Agopianb661d662010-08-19 17:01:19 -0700123 NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
Mathias Agopiane156e642010-03-11 15:05:52 -0800124};
125
126/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
127enum {
128 NATIVE_WINDOW_API_EGL = 1
Mathias Agopian52212712009-08-11 22:34:02 -0700129};
130
Mathias Agopianb661d662010-08-19 17:01:19 -0700131/* parameter for NATIVE_WINDOW_SET_BUFFERS_TRANSFORM */
132enum {
133 /* flip source image horizontally */
134 NATIVE_WINDOW_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H ,
135 /* flip source image vertically */
136 NATIVE_WINDOW_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
137 /* rotate source image 90 degrees clock-wise */
138 NATIVE_WINDOW_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
139 /* rotate source image 180 degrees */
140 NATIVE_WINDOW_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
141 /* rotate source image 270 degrees clock-wise */
142 NATIVE_WINDOW_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
143};
144
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700145struct ANativeWindow
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700146{
147#ifdef __cplusplus
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700148 ANativeWindow()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149 : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
150 {
151 common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700152 common.version = sizeof(ANativeWindow);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700153 memset(common.reserved, 0, sizeof(common.reserved));
154 }
Jamie Gennis5e67f872010-05-10 17:33:32 -0700155
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700156 // Implement the methods that sp<ANativeWindow> expects so that it
157 // can be used to automatically refcount ANativeWindow's.
Jamie Gennis5e67f872010-05-10 17:33:32 -0700158 void incStrong(const void* id) const {
159 common.incRef(const_cast<android_native_base_t*>(&common));
160 }
161 void decStrong(const void* id) const {
162 common.decRef(const_cast<android_native_base_t*>(&common));
163 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700164#endif
165
166 struct android_native_base_t common;
167
168 /* flags describing some attributes of this surface or its updater */
169 const uint32_t flags;
170
171 /* min swap interval supported by this updated */
172 const int minSwapInterval;
173
174 /* max swap interval supported by this updated */
175 const int maxSwapInterval;
176
177 /* horizontal and vertical resolution in DPI */
178 const float xdpi;
179 const float ydpi;
180
181 /* Some storage reserved for the OEM's driver. */
182 intptr_t oem[4];
183
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184
185 /*
186 * Set the swap interval for this surface.
187 *
188 * Returns 0 on success or -errno on error.
189 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700190 int (*setSwapInterval)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700191 int interval);
192
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700193 /*
194 * hook called by EGL to acquire a buffer. After this call, the buffer
195 * is not locked, so its content cannot be modified.
Mathias Agopian0926f502009-05-04 14:17:04 -0700196 * this call may block if no buffers are available.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197 *
198 * Returns 0 on success or -errno on error.
199 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700200 int (*dequeueBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201 struct android_native_buffer_t** buffer);
202
203 /*
204 * hook called by EGL to lock a buffer. This MUST be called before modifying
205 * the content of a buffer. The buffer must have been acquired with
206 * dequeueBuffer first.
207 *
208 * Returns 0 on success or -errno on error.
209 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700210 int (*lockBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211 struct android_native_buffer_t* buffer);
212 /*
213 * hook called by EGL when modifications to the render buffer are done.
214 * This unlocks and post the buffer.
215 *
216 * Buffers MUST be queued in the same order than they were dequeued.
217 *
218 * Returns 0 on success or -errno on error.
219 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700220 int (*queueBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700221 struct android_native_buffer_t* buffer);
222
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700223 /*
224 * hook used to retrieve information about the native window.
225 *
226 * Returns 0 on success or -errno on error.
227 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700228 int (*query)(struct ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700229 int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700230
Mathias Agopian52212712009-08-11 22:34:02 -0700231 /*
232 * hook used to perform various operations on the surface.
233 * (*perform)() is a generic mechanism to add functionality to
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700234 * ANativeWindow while keeping backward binary compatibility.
Mathias Agopian52212712009-08-11 22:34:02 -0700235 *
236 * This hook should not be called directly, instead use the helper functions
237 * defined below.
238 *
Mathias Agopiane156e642010-03-11 15:05:52 -0800239 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
240 * by the surface's implementation.
241 *
Mathias Agopian52212712009-08-11 22:34:02 -0700242 * The valid operations are:
243 * NATIVE_WINDOW_SET_USAGE
Mathias Agopiane156e642010-03-11 15:05:52 -0800244 * NATIVE_WINDOW_CONNECT
245 * NATIVE_WINDOW_DISCONNECT
Mathias Agopiancc08e682010-04-15 18:48:26 -0700246 * NATIVE_WINDOW_SET_CROP
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700247 * NATIVE_WINDOW_SET_BUFFER_COUNT
Mathias Agopiana138f892010-05-21 17:24:35 -0700248 * NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
Mathias Agopianb661d662010-08-19 17:01:19 -0700249 * NATIVE_WINDOW_SET_BUFFERS_TRANSFORM
Mathias Agopian52212712009-08-11 22:34:02 -0700250 *
251 */
252
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700253 int (*perform)(struct ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700254 int operation, ... );
255
Mathias Agopian19957552010-10-01 16:22:41 -0700256 /*
257 * hook used to cancel a buffer that has been dequeued.
258 * No synchronization is performed between dequeue() and cancel(), so
259 * either external synchronization is needed, or these functions must be
260 * called from the same thread.
261 */
262 int (*cancelBuffer)(struct ANativeWindow* window,
263 struct android_native_buffer_t* buffer);
264
265
266 void* reserved_proc[2];
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700267};
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700268
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700269// Backwards compatibility... please switch to ANativeWindow.
270typedef struct ANativeWindow android_native_window_t;
Mathias Agopian52212712009-08-11 22:34:02 -0700271
272/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700273 * native_window_set_usage(..., usage)
274 * Sets the intended usage flags for the next buffers
275 * acquired with (*lockBuffer)() and on.
Mathias Agopian52212712009-08-11 22:34:02 -0700276 * By default (if this function is never called), a usage of
277 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
278 * is assumed.
279 * Calling this function will usually cause following buffers to be
280 * reallocated.
281 */
282
Dima Zavinfae3e732009-08-13 16:50:54 -0700283static inline int native_window_set_usage(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700284 ANativeWindow* window, int usage)
Mathias Agopian52212712009-08-11 22:34:02 -0700285{
286 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
287}
288
Mathias Agopiane156e642010-03-11 15:05:52 -0800289/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700290 * native_window_connect(..., NATIVE_WINDOW_API_EGL)
291 * Must be called by EGL when the window is made current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800292 * Returns -EINVAL if for some reason the window cannot be connected, which
293 * can happen if it's connected to some other API.
294 */
295static inline int native_window_connect(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700296 ANativeWindow* window, int api)
Mathias Agopiane156e642010-03-11 15:05:52 -0800297{
298 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
299}
300
301/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700302 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
303 * Must be called by EGL when the window is made not current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800304 * An error is returned if for instance the window wasn't connected in the
305 * first place.
306 */
307static inline int native_window_disconnect(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700308 ANativeWindow* window, int api)
Mathias Agopiane156e642010-03-11 15:05:52 -0800309{
310 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
311}
312
Mathias Agopiancc08e682010-04-15 18:48:26 -0700313/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700314 * native_window_set_crop(..., crop)
315 * Sets which region of the next queued buffers needs to be considered.
Mathias Agopiancc08e682010-04-15 18:48:26 -0700316 * A buffer's crop region is scaled to match the surface's size.
317 *
318 * The specified crop region applies to all buffers queued after it is called.
319 *
320 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
321 *
322 * An error is returned if for instance the crop region is invalid,
323 * out of the buffer's bound or if the window is invalid.
324 */
325static inline int native_window_set_crop(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700326 ANativeWindow* window,
Mathias Agopiancc08e682010-04-15 18:48:26 -0700327 android_native_rect_t const * crop)
328{
329 return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
330}
Mathias Agopian52212712009-08-11 22:34:02 -0700331
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700332/*
333 * native_window_set_buffer_count(..., count)
334 * Sets the number of buffers associated with this native window.
335 */
336static inline int native_window_set_buffer_count(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700337 ANativeWindow* window,
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700338 size_t bufferCount)
339{
340 return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
341}
342
Mathias Agopiana138f892010-05-21 17:24:35 -0700343/*
344 * native_window_set_buffers_geometry(..., int w, int h, int format)
345 * All buffers dequeued after this call will have the geometry specified.
346 * In particular, all buffers will have a fixed-size, independent form the
347 * native-window size. They will be appropriately scaled to the window-size
348 * upon composition.
349 *
350 * If all parameters are 0, the normal behavior is restored. That is,
351 * dequeued buffers following this call will be sized to the window's size.
352 *
Jamie Gennis68f91272011-01-28 18:21:54 -0800353 * Calling this function will reset the window crop to a NULL value, which
354 * disables cropping of the buffers.
Mathias Agopiana138f892010-05-21 17:24:35 -0700355 */
356static inline int native_window_set_buffers_geometry(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700357 ANativeWindow* window,
Mathias Agopiana138f892010-05-21 17:24:35 -0700358 int w, int h, int format)
359{
360 return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
361 w, h, format);
362}
363
Mathias Agopianb661d662010-08-19 17:01:19 -0700364/*
365 * native_window_set_buffers_transform(..., int transform)
366 * All buffers queued after this call will be displayed transformed according
367 * to the transform parameter specified.
368 */
369static inline int native_window_set_buffers_transform(
370 ANativeWindow* window,
371 int transform)
372{
373 return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
374 transform);
375}
376
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700377// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700378
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700379/* FIXME: this is legacy for pixmaps */
Mathias Agopian238a66e2009-08-13 17:57:53 -0700380typedef struct egl_native_pixmap_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700381{
382 int32_t version; /* must be 32 */
383 int32_t width;
384 int32_t height;
385 int32_t stride;
386 uint8_t* data;
387 uint8_t format;
388 uint8_t rfu[3];
389 union {
390 uint32_t compressedFormat;
391 int32_t vstride;
392 };
393 int32_t reserved;
Mathias Agopian238a66e2009-08-13 17:57:53 -0700394} egl_native_pixmap_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700395
396/*****************************************************************************/
397
398#ifdef __cplusplus
399}
400#endif
401
402
403/*****************************************************************************/
404
405#ifdef __cplusplus
406
407#include <utils/RefBase.h>
408
409namespace android {
410
411/*
412 * This helper class turns an EGL android_native_xxx type into a C++
413 * reference-counted object; with proper type conversions.
414 */
415template <typename NATIVE_TYPE, typename TYPE, typename REF>
416class EGLNativeBase : public NATIVE_TYPE, public REF
417{
Jamie Gennis5e67f872010-05-10 17:33:32 -0700418public:
419 // Disambiguate between the incStrong in REF and NATIVE_TYPE
420 void incStrong(const void* id) const {
421 REF::incStrong(id);
422 }
423 void decStrong(const void* id) const {
424 REF::decStrong(id);
425 }
426
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700427protected:
428 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
429 EGLNativeBase() : NATIVE_TYPE(), REF() {
430 NATIVE_TYPE::common.incRef = incRef;
431 NATIVE_TYPE::common.decRef = decRef;
432 }
433 static inline TYPE* getSelf(NATIVE_TYPE* self) {
434 return static_cast<TYPE*>(self);
435 }
436 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
437 return static_cast<TYPE const *>(self);
438 }
439 static inline TYPE* getSelf(android_native_base_t* base) {
440 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
441 }
442 static inline TYPE const * getSelf(android_native_base_t const* base) {
443 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
444 }
445 static void incRef(android_native_base_t* base) {
446 EGLNativeBase* self = getSelf(base);
447 self->incStrong(self);
448 }
449 static void decRef(android_native_base_t* base) {
450 EGLNativeBase* self = getSelf(base);
451 self->decStrong(self);
452 }
453};
454
455} // namespace android
456#endif // __cplusplus
457
458/*****************************************************************************/
459
460#endif /* ANDROID_ANDROID_NATIVES_H */