blob: fd83f469ce0923005a19262831b63bd0bfbe2887 [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,
Mathias Agopiancb6b9042009-07-30 18:14:56 -070098};
99
Mathias Agopian52212712009-08-11 22:34:02 -0700100/* valid operations for the (*perform)() hook */
101enum {
Mathias Agopiane156e642010-03-11 15:05:52 -0800102 NATIVE_WINDOW_SET_USAGE = 0,
Mathias Agopiancc08e682010-04-15 18:48:26 -0700103 NATIVE_WINDOW_CONNECT,
104 NATIVE_WINDOW_DISCONNECT,
105 NATIVE_WINDOW_SET_CROP,
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700106 NATIVE_WINDOW_SET_BUFFER_COUNT,
Mathias Agopiana138f892010-05-21 17:24:35 -0700107 NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
Mathias Agopianb661d662010-08-19 17:01:19 -0700108 NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
Mathias Agopiane156e642010-03-11 15:05:52 -0800109};
110
111/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
112enum {
113 NATIVE_WINDOW_API_EGL = 1
Mathias Agopian52212712009-08-11 22:34:02 -0700114};
115
Mathias Agopianb661d662010-08-19 17:01:19 -0700116/* parameter for NATIVE_WINDOW_SET_BUFFERS_TRANSFORM */
117enum {
118 /* flip source image horizontally */
119 NATIVE_WINDOW_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H ,
120 /* flip source image vertically */
121 NATIVE_WINDOW_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
122 /* rotate source image 90 degrees clock-wise */
123 NATIVE_WINDOW_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
124 /* rotate source image 180 degrees */
125 NATIVE_WINDOW_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
126 /* rotate source image 270 degrees clock-wise */
127 NATIVE_WINDOW_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
128};
129
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700130struct ANativeWindow
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131{
132#ifdef __cplusplus
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700133 ANativeWindow()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
135 {
136 common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700137 common.version = sizeof(ANativeWindow);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 memset(common.reserved, 0, sizeof(common.reserved));
139 }
Jamie Gennis5e67f872010-05-10 17:33:32 -0700140
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700141 // Implement the methods that sp<ANativeWindow> expects so that it
142 // can be used to automatically refcount ANativeWindow's.
Jamie Gennis5e67f872010-05-10 17:33:32 -0700143 void incStrong(const void* id) const {
144 common.incRef(const_cast<android_native_base_t*>(&common));
145 }
146 void decStrong(const void* id) const {
147 common.decRef(const_cast<android_native_base_t*>(&common));
148 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149#endif
150
151 struct android_native_base_t common;
152
153 /* flags describing some attributes of this surface or its updater */
154 const uint32_t flags;
155
156 /* min swap interval supported by this updated */
157 const int minSwapInterval;
158
159 /* max swap interval supported by this updated */
160 const int maxSwapInterval;
161
162 /* horizontal and vertical resolution in DPI */
163 const float xdpi;
164 const float ydpi;
165
166 /* Some storage reserved for the OEM's driver. */
167 intptr_t oem[4];
168
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700169
170 /*
171 * Set the swap interval for this surface.
172 *
173 * Returns 0 on success or -errno on error.
174 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700175 int (*setSwapInterval)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700176 int interval);
177
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178 /*
179 * hook called by EGL to acquire a buffer. After this call, the buffer
180 * is not locked, so its content cannot be modified.
Mathias Agopian0926f502009-05-04 14:17:04 -0700181 * this call may block if no buffers are available.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700182 *
183 * Returns 0 on success or -errno on error.
184 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700185 int (*dequeueBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700186 struct android_native_buffer_t** buffer);
187
188 /*
189 * hook called by EGL to lock a buffer. This MUST be called before modifying
190 * the content of a buffer. The buffer must have been acquired with
191 * dequeueBuffer first.
192 *
193 * Returns 0 on success or -errno on error.
194 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700195 int (*lockBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700196 struct android_native_buffer_t* buffer);
197 /*
198 * hook called by EGL when modifications to the render buffer are done.
199 * This unlocks and post the buffer.
200 *
201 * Buffers MUST be queued in the same order than they were dequeued.
202 *
203 * Returns 0 on success or -errno on error.
204 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700205 int (*queueBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206 struct android_native_buffer_t* buffer);
207
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700208 /*
209 * hook used to retrieve information about the native window.
210 *
211 * Returns 0 on success or -errno on error.
212 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700213 int (*query)(struct ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700214 int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700215
Mathias Agopian52212712009-08-11 22:34:02 -0700216 /*
217 * hook used to perform various operations on the surface.
218 * (*perform)() is a generic mechanism to add functionality to
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700219 * ANativeWindow while keeping backward binary compatibility.
Mathias Agopian52212712009-08-11 22:34:02 -0700220 *
221 * This hook should not be called directly, instead use the helper functions
222 * defined below.
223 *
Mathias Agopiane156e642010-03-11 15:05:52 -0800224 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
225 * by the surface's implementation.
226 *
Mathias Agopian52212712009-08-11 22:34:02 -0700227 * The valid operations are:
228 * NATIVE_WINDOW_SET_USAGE
Mathias Agopiane156e642010-03-11 15:05:52 -0800229 * NATIVE_WINDOW_CONNECT
230 * NATIVE_WINDOW_DISCONNECT
Mathias Agopiancc08e682010-04-15 18:48:26 -0700231 * NATIVE_WINDOW_SET_CROP
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700232 * NATIVE_WINDOW_SET_BUFFER_COUNT
Mathias Agopiana138f892010-05-21 17:24:35 -0700233 * NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
Mathias Agopianb661d662010-08-19 17:01:19 -0700234 * NATIVE_WINDOW_SET_BUFFERS_TRANSFORM
Mathias Agopian52212712009-08-11 22:34:02 -0700235 *
236 */
237
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700238 int (*perform)(struct ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700239 int operation, ... );
240
Mathias Agopian19957552010-10-01 16:22:41 -0700241 /*
242 * hook used to cancel a buffer that has been dequeued.
243 * No synchronization is performed between dequeue() and cancel(), so
244 * either external synchronization is needed, or these functions must be
245 * called from the same thread.
246 */
247 int (*cancelBuffer)(struct ANativeWindow* window,
248 struct android_native_buffer_t* buffer);
249
250
251 void* reserved_proc[2];
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700252};
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700253
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700254// Backwards compatibility... please switch to ANativeWindow.
255typedef struct ANativeWindow android_native_window_t;
Mathias Agopian52212712009-08-11 22:34:02 -0700256
257/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700258 * native_window_set_usage(..., usage)
259 * Sets the intended usage flags for the next buffers
260 * acquired with (*lockBuffer)() and on.
Mathias Agopian52212712009-08-11 22:34:02 -0700261 * By default (if this function is never called), a usage of
262 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
263 * is assumed.
264 * Calling this function will usually cause following buffers to be
265 * reallocated.
266 */
267
Dima Zavinfae3e732009-08-13 16:50:54 -0700268static inline int native_window_set_usage(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700269 ANativeWindow* window, int usage)
Mathias Agopian52212712009-08-11 22:34:02 -0700270{
271 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
272}
273
Mathias Agopiane156e642010-03-11 15:05:52 -0800274/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700275 * native_window_connect(..., NATIVE_WINDOW_API_EGL)
276 * Must be called by EGL when the window is made current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800277 * Returns -EINVAL if for some reason the window cannot be connected, which
278 * can happen if it's connected to some other API.
279 */
280static inline int native_window_connect(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700281 ANativeWindow* window, int api)
Mathias Agopiane156e642010-03-11 15:05:52 -0800282{
283 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
284}
285
286/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700287 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
288 * Must be called by EGL when the window is made not current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800289 * An error is returned if for instance the window wasn't connected in the
290 * first place.
291 */
292static inline int native_window_disconnect(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700293 ANativeWindow* window, int api)
Mathias Agopiane156e642010-03-11 15:05:52 -0800294{
295 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
296}
297
Mathias Agopiancc08e682010-04-15 18:48:26 -0700298/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700299 * native_window_set_crop(..., crop)
300 * Sets which region of the next queued buffers needs to be considered.
Mathias Agopiancc08e682010-04-15 18:48:26 -0700301 * A buffer's crop region is scaled to match the surface's size.
302 *
303 * The specified crop region applies to all buffers queued after it is called.
304 *
305 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
306 *
307 * An error is returned if for instance the crop region is invalid,
308 * out of the buffer's bound or if the window is invalid.
309 */
310static inline int native_window_set_crop(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700311 ANativeWindow* window,
Mathias Agopiancc08e682010-04-15 18:48:26 -0700312 android_native_rect_t const * crop)
313{
314 return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
315}
Mathias Agopian52212712009-08-11 22:34:02 -0700316
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700317/*
318 * native_window_set_buffer_count(..., count)
319 * Sets the number of buffers associated with this native window.
320 */
321static inline int native_window_set_buffer_count(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700322 ANativeWindow* window,
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700323 size_t bufferCount)
324{
325 return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
326}
327
Mathias Agopiana138f892010-05-21 17:24:35 -0700328/*
329 * native_window_set_buffers_geometry(..., int w, int h, int format)
330 * All buffers dequeued after this call will have the geometry specified.
331 * In particular, all buffers will have a fixed-size, independent form the
332 * native-window size. They will be appropriately scaled to the window-size
333 * upon composition.
334 *
335 * If all parameters are 0, the normal behavior is restored. That is,
336 * dequeued buffers following this call will be sized to the window's size.
337 *
Jamie Gennis68f91272011-01-28 18:21:54 -0800338 * Calling this function will reset the window crop to a NULL value, which
339 * disables cropping of the buffers.
Mathias Agopiana138f892010-05-21 17:24:35 -0700340 */
341static inline int native_window_set_buffers_geometry(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700342 ANativeWindow* window,
Mathias Agopiana138f892010-05-21 17:24:35 -0700343 int w, int h, int format)
344{
345 return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
346 w, h, format);
347}
348
Mathias Agopianb661d662010-08-19 17:01:19 -0700349/*
350 * native_window_set_buffers_transform(..., int transform)
351 * All buffers queued after this call will be displayed transformed according
352 * to the transform parameter specified.
353 */
354static inline int native_window_set_buffers_transform(
355 ANativeWindow* window,
356 int transform)
357{
358 return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
359 transform);
360}
361
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700362// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700363
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700364/* FIXME: this is legacy for pixmaps */
Mathias Agopian238a66e2009-08-13 17:57:53 -0700365typedef struct egl_native_pixmap_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700366{
367 int32_t version; /* must be 32 */
368 int32_t width;
369 int32_t height;
370 int32_t stride;
371 uint8_t* data;
372 uint8_t format;
373 uint8_t rfu[3];
374 union {
375 uint32_t compressedFormat;
376 int32_t vstride;
377 };
378 int32_t reserved;
Mathias Agopian238a66e2009-08-13 17:57:53 -0700379} egl_native_pixmap_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700380
381/*****************************************************************************/
382
383#ifdef __cplusplus
384}
385#endif
386
387
388/*****************************************************************************/
389
390#ifdef __cplusplus
391
392#include <utils/RefBase.h>
393
394namespace android {
395
396/*
397 * This helper class turns an EGL android_native_xxx type into a C++
398 * reference-counted object; with proper type conversions.
399 */
400template <typename NATIVE_TYPE, typename TYPE, typename REF>
401class EGLNativeBase : public NATIVE_TYPE, public REF
402{
Jamie Gennis5e67f872010-05-10 17:33:32 -0700403public:
404 // Disambiguate between the incStrong in REF and NATIVE_TYPE
405 void incStrong(const void* id) const {
406 REF::incStrong(id);
407 }
408 void decStrong(const void* id) const {
409 REF::decStrong(id);
410 }
411
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700412protected:
413 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
414 EGLNativeBase() : NATIVE_TYPE(), REF() {
415 NATIVE_TYPE::common.incRef = incRef;
416 NATIVE_TYPE::common.decRef = decRef;
417 }
418 static inline TYPE* getSelf(NATIVE_TYPE* self) {
419 return static_cast<TYPE*>(self);
420 }
421 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
422 return static_cast<TYPE const *>(self);
423 }
424 static inline TYPE* getSelf(android_native_base_t* base) {
425 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
426 }
427 static inline TYPE const * getSelf(android_native_base_t const* base) {
428 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
429 }
430 static void incRef(android_native_base_t* base) {
431 EGLNativeBase* self = getSelf(base);
432 self->incStrong(self);
433 }
434 static void decRef(android_native_base_t* base) {
435 EGLNativeBase* self = getSelf(base);
436 self->decStrong(self);
437 }
438};
439
440} // namespace android
441#endif // __cplusplus
442
443/*****************************************************************************/
444
445#endif /* ANDROID_ANDROID_NATIVES_H */