blob: 0fc1ddf226444d7248beb6f73b81fcae88b65e67 [file] [log] [blame]
Mathias Agopian1473f462009-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 Hackborn8b49bd12010-06-30 13:56:17 -070025#include <android/native_window.h>
26
Mathias Agopian1473f462009-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 Agopian16a86ee2010-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 Agopian1473f462009-04-10 14:24:30 -070054// ---------------------------------------------------------------------------
55
Mathias Agopianf2339152009-08-13 17:57:53 -070056typedef struct android_native_base_t
Mathias Agopian1473f462009-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 Agopian2b895482009-08-17 12:33:20 -070067 void (*incRef)(struct android_native_base_t* base);
68 void (*decRef)(struct android_native_base_t* base);
Mathias Agopianf2339152009-08-13 17:57:53 -070069} android_native_base_t;
Mathias Agopian1473f462009-04-10 14:24:30 -070070
Mathias Agopian9bd5da42009-05-05 18:29:35 -070071// ---------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -070072
Mathias Agopian5b5c9142009-07-30 18:14:56 -070073/* attributes queriable with query() */
74enum {
75 NATIVE_WINDOW_WIDTH = 0,
Mathias Agopian16a86ee2010-04-15 18:48:26 -070076 NATIVE_WINDOW_HEIGHT,
77 NATIVE_WINDOW_FORMAT,
Jamie Gennis96dcc972011-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 Gennisd2acedf2011-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,
Jamie Gennisc4ca7c52011-03-14 15:00:06 -0700113
114 /* Get the concrete type of a ANativeWindow. See below for the list of
115 * possible return values.
116 *
117 * This query should not be used outside the Android framework and will
118 * likely be removed in the near future.
119 */
120 NATIVE_WINDOW_CONCRETE_TYPE,
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700121};
122
Mathias Agopian5cec4742009-08-11 22:34:02 -0700123/* valid operations for the (*perform)() hook */
124enum {
Mathias Agopianf6331a42010-03-11 15:05:52 -0800125 NATIVE_WINDOW_SET_USAGE = 0,
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700126 NATIVE_WINDOW_CONNECT,
127 NATIVE_WINDOW_DISCONNECT,
128 NATIVE_WINDOW_SET_CROP,
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700129 NATIVE_WINDOW_SET_BUFFER_COUNT,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700130 NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700131 NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
Mathias Agopianf6331a42010-03-11 15:05:52 -0800132};
133
134/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
135enum {
136 NATIVE_WINDOW_API_EGL = 1
Mathias Agopian5cec4742009-08-11 22:34:02 -0700137};
138
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700139/* parameter for NATIVE_WINDOW_SET_BUFFERS_TRANSFORM */
140enum {
141 /* flip source image horizontally */
142 NATIVE_WINDOW_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H ,
143 /* flip source image vertically */
144 NATIVE_WINDOW_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
145 /* rotate source image 90 degrees clock-wise */
146 NATIVE_WINDOW_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
147 /* rotate source image 180 degrees */
148 NATIVE_WINDOW_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
149 /* rotate source image 270 degrees clock-wise */
150 NATIVE_WINDOW_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
151};
152
Jamie Gennisc4ca7c52011-03-14 15:00:06 -0700153/* values returned by the NATIVE_WINDOW_CONCRETE_TYPE query */
154enum {
155 NATIVE_WINDOW_FRAMEBUFFER, // FramebufferNativeWindow
156 NATIVE_WINDOW_SURFACE, // Surface
157 NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT, // SurfaceTextureClient
158};
159
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700160struct ANativeWindow
Mathias Agopian1473f462009-04-10 14:24:30 -0700161{
162#ifdef __cplusplus
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700163 ANativeWindow()
Mathias Agopian1473f462009-04-10 14:24:30 -0700164 : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
165 {
166 common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700167 common.version = sizeof(ANativeWindow);
Mathias Agopian1473f462009-04-10 14:24:30 -0700168 memset(common.reserved, 0, sizeof(common.reserved));
169 }
Jamie Gennis505cef32010-05-10 17:33:32 -0700170
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700171 // Implement the methods that sp<ANativeWindow> expects so that it
172 // can be used to automatically refcount ANativeWindow's.
Jamie Gennis505cef32010-05-10 17:33:32 -0700173 void incStrong(const void* id) const {
174 common.incRef(const_cast<android_native_base_t*>(&common));
175 }
176 void decStrong(const void* id) const {
177 common.decRef(const_cast<android_native_base_t*>(&common));
178 }
Mathias Agopian1473f462009-04-10 14:24:30 -0700179#endif
180
181 struct android_native_base_t common;
182
183 /* flags describing some attributes of this surface or its updater */
184 const uint32_t flags;
185
186 /* min swap interval supported by this updated */
187 const int minSwapInterval;
188
189 /* max swap interval supported by this updated */
190 const int maxSwapInterval;
191
192 /* horizontal and vertical resolution in DPI */
193 const float xdpi;
194 const float ydpi;
195
196 /* Some storage reserved for the OEM's driver. */
197 intptr_t oem[4];
198
Mathias Agopian1473f462009-04-10 14:24:30 -0700199
200 /*
201 * Set the swap interval for this surface.
202 *
203 * Returns 0 on success or -errno on error.
204 */
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700205 int (*setSwapInterval)(struct ANativeWindow* window,
Mathias Agopian1473f462009-04-10 14:24:30 -0700206 int interval);
207
Mathias Agopian1473f462009-04-10 14:24:30 -0700208 /*
209 * hook called by EGL to acquire a buffer. After this call, the buffer
210 * is not locked, so its content cannot be modified.
Mathias Agopiandff8e582009-05-04 14:17:04 -0700211 * this call may block if no buffers are available.
Mathias Agopian1473f462009-04-10 14:24:30 -0700212 *
213 * Returns 0 on success or -errno on error.
214 */
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700215 int (*dequeueBuffer)(struct ANativeWindow* window,
Mathias Agopian1473f462009-04-10 14:24:30 -0700216 struct android_native_buffer_t** buffer);
217
218 /*
219 * hook called by EGL to lock a buffer. This MUST be called before modifying
220 * the content of a buffer. The buffer must have been acquired with
221 * dequeueBuffer first.
222 *
223 * Returns 0 on success or -errno on error.
224 */
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700225 int (*lockBuffer)(struct ANativeWindow* window,
Mathias Agopian1473f462009-04-10 14:24:30 -0700226 struct android_native_buffer_t* buffer);
227 /*
228 * hook called by EGL when modifications to the render buffer are done.
229 * This unlocks and post the buffer.
230 *
231 * Buffers MUST be queued in the same order than they were dequeued.
232 *
233 * Returns 0 on success or -errno on error.
234 */
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700235 int (*queueBuffer)(struct ANativeWindow* window,
Mathias Agopian1473f462009-04-10 14:24:30 -0700236 struct android_native_buffer_t* buffer);
237
Mathias Agopian5b5c9142009-07-30 18:14:56 -0700238 /*
239 * hook used to retrieve information about the native window.
240 *
241 * Returns 0 on success or -errno on error.
242 */
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700243 int (*query)(struct ANativeWindow* window,
Mathias Agopian5cec4742009-08-11 22:34:02 -0700244 int what, int* value);
Mathias Agopian1473f462009-04-10 14:24:30 -0700245
Mathias Agopian5cec4742009-08-11 22:34:02 -0700246 /*
247 * hook used to perform various operations on the surface.
248 * (*perform)() is a generic mechanism to add functionality to
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700249 * ANativeWindow while keeping backward binary compatibility.
Mathias Agopian5cec4742009-08-11 22:34:02 -0700250 *
251 * This hook should not be called directly, instead use the helper functions
252 * defined below.
253 *
Mathias Agopianf6331a42010-03-11 15:05:52 -0800254 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
255 * by the surface's implementation.
256 *
Mathias Agopian5cec4742009-08-11 22:34:02 -0700257 * The valid operations are:
258 * NATIVE_WINDOW_SET_USAGE
Mathias Agopianf6331a42010-03-11 15:05:52 -0800259 * NATIVE_WINDOW_CONNECT
260 * NATIVE_WINDOW_DISCONNECT
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700261 * NATIVE_WINDOW_SET_CROP
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700262 * NATIVE_WINDOW_SET_BUFFER_COUNT
Mathias Agopian2be352a2010-05-21 17:24:35 -0700263 * NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700264 * NATIVE_WINDOW_SET_BUFFERS_TRANSFORM
Mathias Agopian5cec4742009-08-11 22:34:02 -0700265 *
266 */
267
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700268 int (*perform)(struct ANativeWindow* window,
Mathias Agopian5cec4742009-08-11 22:34:02 -0700269 int operation, ... );
270
Mathias Agopian8ddd2c72010-10-01 16:22:41 -0700271 /*
272 * hook used to cancel a buffer that has been dequeued.
273 * No synchronization is performed between dequeue() and cancel(), so
274 * either external synchronization is needed, or these functions must be
275 * called from the same thread.
276 */
277 int (*cancelBuffer)(struct ANativeWindow* window,
278 struct android_native_buffer_t* buffer);
279
280
281 void* reserved_proc[2];
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700282};
Mathias Agopian1473f462009-04-10 14:24:30 -0700283
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700284// Backwards compatibility... please switch to ANativeWindow.
285typedef struct ANativeWindow android_native_window_t;
Mathias Agopian5cec4742009-08-11 22:34:02 -0700286
287/*
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700288 * native_window_set_usage(..., usage)
289 * Sets the intended usage flags for the next buffers
290 * acquired with (*lockBuffer)() and on.
Mathias Agopian5cec4742009-08-11 22:34:02 -0700291 * By default (if this function is never called), a usage of
292 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
293 * is assumed.
294 * Calling this function will usually cause following buffers to be
295 * reallocated.
296 */
297
Dima Zavin8db93382009-08-13 16:50:54 -0700298static inline int native_window_set_usage(
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700299 ANativeWindow* window, int usage)
Mathias Agopian5cec4742009-08-11 22:34:02 -0700300{
301 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
302}
303
Mathias Agopianf6331a42010-03-11 15:05:52 -0800304/*
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700305 * native_window_connect(..., NATIVE_WINDOW_API_EGL)
306 * Must be called by EGL when the window is made current.
Mathias Agopianf6331a42010-03-11 15:05:52 -0800307 * Returns -EINVAL if for some reason the window cannot be connected, which
308 * can happen if it's connected to some other API.
309 */
310static inline int native_window_connect(
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700311 ANativeWindow* window, int api)
Mathias Agopianf6331a42010-03-11 15:05:52 -0800312{
313 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
314}
315
316/*
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700317 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
318 * Must be called by EGL when the window is made not current.
Mathias Agopianf6331a42010-03-11 15:05:52 -0800319 * An error is returned if for instance the window wasn't connected in the
320 * first place.
321 */
322static inline int native_window_disconnect(
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700323 ANativeWindow* window, int api)
Mathias Agopianf6331a42010-03-11 15:05:52 -0800324{
325 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
326}
327
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700328/*
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700329 * native_window_set_crop(..., crop)
330 * Sets which region of the next queued buffers needs to be considered.
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700331 * A buffer's crop region is scaled to match the surface's size.
332 *
333 * The specified crop region applies to all buffers queued after it is called.
334 *
335 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
336 *
337 * An error is returned if for instance the crop region is invalid,
338 * out of the buffer's bound or if the window is invalid.
339 */
340static inline int native_window_set_crop(
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700341 ANativeWindow* window,
Mathias Agopian16a86ee2010-04-15 18:48:26 -0700342 android_native_rect_t const * crop)
343{
344 return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
345}
Mathias Agopian5cec4742009-08-11 22:34:02 -0700346
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700347/*
348 * native_window_set_buffer_count(..., count)
349 * Sets the number of buffers associated with this native window.
350 */
351static inline int native_window_set_buffer_count(
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700352 ANativeWindow* window,
Mathias Agopian25f0bda2010-05-21 14:19:50 -0700353 size_t bufferCount)
354{
355 return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
356}
357
Mathias Agopian2be352a2010-05-21 17:24:35 -0700358/*
359 * native_window_set_buffers_geometry(..., int w, int h, int format)
360 * All buffers dequeued after this call will have the geometry specified.
361 * In particular, all buffers will have a fixed-size, independent form the
362 * native-window size. They will be appropriately scaled to the window-size
363 * upon composition.
364 *
365 * If all parameters are 0, the normal behavior is restored. That is,
366 * dequeued buffers following this call will be sized to the window's size.
367 *
Jamie Gennis2ece4cd2011-01-28 18:21:54 -0800368 * Calling this function will reset the window crop to a NULL value, which
369 * disables cropping of the buffers.
Mathias Agopian2be352a2010-05-21 17:24:35 -0700370 */
371static inline int native_window_set_buffers_geometry(
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700372 ANativeWindow* window,
Mathias Agopian2be352a2010-05-21 17:24:35 -0700373 int w, int h, int format)
374{
375 return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
376 w, h, format);
377}
378
Mathias Agopiane96aa3e2010-08-19 17:01:19 -0700379/*
380 * native_window_set_buffers_transform(..., int transform)
381 * All buffers queued after this call will be displayed transformed according
382 * to the transform parameter specified.
383 */
384static inline int native_window_set_buffers_transform(
385 ANativeWindow* window,
386 int transform)
387{
388 return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_TRANSFORM,
389 transform);
390}
391
Mathias Agopian9bd5da42009-05-05 18:29:35 -0700392// ---------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -0700393
Mathias Agopian1473f462009-04-10 14:24:30 -0700394/* FIXME: this is legacy for pixmaps */
Mathias Agopianf2339152009-08-13 17:57:53 -0700395typedef struct egl_native_pixmap_t
Mathias Agopian1473f462009-04-10 14:24:30 -0700396{
397 int32_t version; /* must be 32 */
398 int32_t width;
399 int32_t height;
400 int32_t stride;
401 uint8_t* data;
402 uint8_t format;
403 uint8_t rfu[3];
404 union {
405 uint32_t compressedFormat;
406 int32_t vstride;
407 };
408 int32_t reserved;
Mathias Agopianf2339152009-08-13 17:57:53 -0700409} egl_native_pixmap_t;
Mathias Agopian1473f462009-04-10 14:24:30 -0700410
411/*****************************************************************************/
412
413#ifdef __cplusplus
414}
415#endif
416
417
418/*****************************************************************************/
419
420#ifdef __cplusplus
421
422#include <utils/RefBase.h>
423
424namespace android {
425
426/*
427 * This helper class turns an EGL android_native_xxx type into a C++
428 * reference-counted object; with proper type conversions.
429 */
430template <typename NATIVE_TYPE, typename TYPE, typename REF>
431class EGLNativeBase : public NATIVE_TYPE, public REF
432{
Jamie Gennis505cef32010-05-10 17:33:32 -0700433public:
434 // Disambiguate between the incStrong in REF and NATIVE_TYPE
435 void incStrong(const void* id) const {
436 REF::incStrong(id);
437 }
438 void decStrong(const void* id) const {
439 REF::decStrong(id);
440 }
441
Mathias Agopian1473f462009-04-10 14:24:30 -0700442protected:
443 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
444 EGLNativeBase() : NATIVE_TYPE(), REF() {
445 NATIVE_TYPE::common.incRef = incRef;
446 NATIVE_TYPE::common.decRef = decRef;
447 }
448 static inline TYPE* getSelf(NATIVE_TYPE* self) {
449 return static_cast<TYPE*>(self);
450 }
451 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
452 return static_cast<TYPE const *>(self);
453 }
454 static inline TYPE* getSelf(android_native_base_t* base) {
455 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
456 }
457 static inline TYPE const * getSelf(android_native_base_t const* base) {
458 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
459 }
460 static void incRef(android_native_base_t* base) {
461 EGLNativeBase* self = getSelf(base);
462 self->incStrong(self);
463 }
464 static void decRef(android_native_base_t* base) {
465 EGLNativeBase* self = getSelf(base);
466 self->decStrong(self);
467 }
468};
469
470} // namespace android
471#endif // __cplusplus
472
473/*****************************************************************************/
474
475#endif /* ANDROID_ANDROID_NATIVES_H */