blob: 171f3df3a17983bae4c255549470a30e6f9eef73 [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
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/*****************************************************************************/
30
31#define ANDROID_NATIVE_MAKE_CONSTANT(a,b,c,d) \
32 (((unsigned)(a)<<24)|((unsigned)(b)<<16)|((unsigned)(c)<<8)|(unsigned)(d))
33
34#define ANDROID_NATIVE_WINDOW_MAGIC \
35 ANDROID_NATIVE_MAKE_CONSTANT('_','w','n','d')
36
37#define ANDROID_NATIVE_BUFFER_MAGIC \
38 ANDROID_NATIVE_MAKE_CONSTANT('_','b','f','r')
39
40// ---------------------------------------------------------------------------
41
42struct android_native_buffer_t;
43
Mathias Agopiancc08e682010-04-15 18:48:26 -070044typedef struct android_native_rect_t
45{
46 int32_t left;
47 int32_t top;
48 int32_t right;
49 int32_t bottom;
50} android_native_rect_t;
51
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052// ---------------------------------------------------------------------------
53
Mathias Agopian238a66e2009-08-13 17:57:53 -070054typedef struct android_native_base_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055{
56 /* a magic value defined by the actual EGL native type */
57 int magic;
58
59 /* the sizeof() of the actual EGL native type */
60 int version;
61
62 void* reserved[4];
63
64 /* reference-counting interface */
Mathias Agopian37b2a372009-08-17 12:33:20 -070065 void (*incRef)(struct android_native_base_t* base);
66 void (*decRef)(struct android_native_base_t* base);
Mathias Agopian238a66e2009-08-13 17:57:53 -070067} android_native_base_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070068
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -070069// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070
Mathias Agopiancb6b9042009-07-30 18:14:56 -070071/* attributes queriable with query() */
72enum {
73 NATIVE_WINDOW_WIDTH = 0,
Mathias Agopiancc08e682010-04-15 18:48:26 -070074 NATIVE_WINDOW_HEIGHT,
75 NATIVE_WINDOW_FORMAT,
Mathias Agopiancb6b9042009-07-30 18:14:56 -070076};
77
Mathias Agopian52212712009-08-11 22:34:02 -070078/* valid operations for the (*perform)() hook */
79enum {
Mathias Agopiane156e642010-03-11 15:05:52 -080080 NATIVE_WINDOW_SET_USAGE = 0,
Mathias Agopiancc08e682010-04-15 18:48:26 -070081 NATIVE_WINDOW_CONNECT,
82 NATIVE_WINDOW_DISCONNECT,
83 NATIVE_WINDOW_SET_CROP,
Mathias Agopianf10d7fd2010-05-21 14:19:50 -070084 NATIVE_WINDOW_SET_BUFFER_COUNT,
Mathias Agopiana138f892010-05-21 17:24:35 -070085 NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
Mathias Agopiane156e642010-03-11 15:05:52 -080086};
87
88/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
89enum {
90 NATIVE_WINDOW_API_EGL = 1
Mathias Agopian52212712009-08-11 22:34:02 -070091};
92
Mathias Agopian238a66e2009-08-13 17:57:53 -070093typedef struct android_native_window_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -070094{
95#ifdef __cplusplus
96 android_native_window_t()
97 : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
98 {
99 common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
100 common.version = sizeof(android_native_window_t);
101 memset(common.reserved, 0, sizeof(common.reserved));
102 }
Jamie Gennis5e67f872010-05-10 17:33:32 -0700103
104 // Implement the methods that sp<android_native_window_t> expects so that it
105 // can be used to automatically refcount android_native_window_t's.
106 void incStrong(const void* id) const {
107 common.incRef(const_cast<android_native_base_t*>(&common));
108 }
109 void decStrong(const void* id) const {
110 common.decRef(const_cast<android_native_base_t*>(&common));
111 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112#endif
113
114 struct android_native_base_t common;
115
116 /* flags describing some attributes of this surface or its updater */
117 const uint32_t flags;
118
119 /* min swap interval supported by this updated */
120 const int minSwapInterval;
121
122 /* max swap interval supported by this updated */
123 const int maxSwapInterval;
124
125 /* horizontal and vertical resolution in DPI */
126 const float xdpi;
127 const float ydpi;
128
129 /* Some storage reserved for the OEM's driver. */
130 intptr_t oem[4];
131
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132
133 /*
134 * Set the swap interval for this surface.
135 *
136 * Returns 0 on success or -errno on error.
137 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700138 int (*setSwapInterval)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 int interval);
140
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 /*
142 * hook called by EGL to acquire a buffer. After this call, the buffer
143 * is not locked, so its content cannot be modified.
Mathias Agopian0926f502009-05-04 14:17:04 -0700144 * this call may block if no buffers are available.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700145 *
146 * Returns 0 on success or -errno on error.
147 */
Mathias Agopiancc08e682010-04-15 18:48:26 -0700148 int (*dequeueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149 struct android_native_buffer_t** buffer);
150
151 /*
152 * hook called by EGL to lock a buffer. This MUST be called before modifying
153 * the content of a buffer. The buffer must have been acquired with
154 * dequeueBuffer first.
155 *
156 * Returns 0 on success or -errno on error.
157 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700158 int (*lockBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159 struct android_native_buffer_t* buffer);
160 /*
161 * hook called by EGL when modifications to the render buffer are done.
162 * This unlocks and post the buffer.
163 *
164 * Buffers MUST be queued in the same order than they were dequeued.
165 *
166 * Returns 0 on success or -errno on error.
167 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700168 int (*queueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700169 struct android_native_buffer_t* buffer);
170
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700171 /*
172 * hook used to retrieve information about the native window.
173 *
174 * Returns 0 on success or -errno on error.
175 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700176 int (*query)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700177 int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178
Mathias Agopian52212712009-08-11 22:34:02 -0700179 /*
180 * hook used to perform various operations on the surface.
181 * (*perform)() is a generic mechanism to add functionality to
182 * android_native_window_t while keeping backward binary compatibility.
183 *
184 * This hook should not be called directly, instead use the helper functions
185 * defined below.
186 *
Mathias Agopiane156e642010-03-11 15:05:52 -0800187 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
188 * by the surface's implementation.
189 *
Mathias Agopian52212712009-08-11 22:34:02 -0700190 * The valid operations are:
191 * NATIVE_WINDOW_SET_USAGE
Mathias Agopiane156e642010-03-11 15:05:52 -0800192 * NATIVE_WINDOW_CONNECT
193 * NATIVE_WINDOW_DISCONNECT
Mathias Agopiancc08e682010-04-15 18:48:26 -0700194 * NATIVE_WINDOW_SET_CROP
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700195 * NATIVE_WINDOW_SET_BUFFER_COUNT
Mathias Agopiana138f892010-05-21 17:24:35 -0700196 * NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
Mathias Agopian52212712009-08-11 22:34:02 -0700197 *
198 */
199
Mathias Agopian37b2a372009-08-17 12:33:20 -0700200 int (*perform)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700201 int operation, ... );
202
203 void* reserved_proc[3];
Mathias Agopian238a66e2009-08-13 17:57:53 -0700204} android_native_window_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700205
Mathias Agopian52212712009-08-11 22:34:02 -0700206
207/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700208 * native_window_set_usage(..., usage)
209 * Sets the intended usage flags for the next buffers
210 * acquired with (*lockBuffer)() and on.
Mathias Agopian52212712009-08-11 22:34:02 -0700211 * By default (if this function is never called), a usage of
212 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
213 * is assumed.
214 * Calling this function will usually cause following buffers to be
215 * reallocated.
216 */
217
Dima Zavinfae3e732009-08-13 16:50:54 -0700218static inline int native_window_set_usage(
Mathias Agopian238a66e2009-08-13 17:57:53 -0700219 android_native_window_t* window, int usage)
Mathias Agopian52212712009-08-11 22:34:02 -0700220{
221 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
222}
223
Mathias Agopiane156e642010-03-11 15:05:52 -0800224/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700225 * native_window_connect(..., NATIVE_WINDOW_API_EGL)
226 * Must be called by EGL when the window is made current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800227 * Returns -EINVAL if for some reason the window cannot be connected, which
228 * can happen if it's connected to some other API.
229 */
230static inline int native_window_connect(
231 android_native_window_t* window, int api)
232{
233 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
234}
235
236/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700237 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
238 * Must be called by EGL when the window is made not current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800239 * An error is returned if for instance the window wasn't connected in the
240 * first place.
241 */
242static inline int native_window_disconnect(
243 android_native_window_t* window, int api)
244{
245 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
246}
247
Mathias Agopiancc08e682010-04-15 18:48:26 -0700248/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700249 * native_window_set_crop(..., crop)
250 * Sets which region of the next queued buffers needs to be considered.
Mathias Agopiancc08e682010-04-15 18:48:26 -0700251 * A buffer's crop region is scaled to match the surface's size.
252 *
253 * The specified crop region applies to all buffers queued after it is called.
254 *
255 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
256 *
257 * An error is returned if for instance the crop region is invalid,
258 * out of the buffer's bound or if the window is invalid.
259 */
260static inline int native_window_set_crop(
261 android_native_window_t* window,
262 android_native_rect_t const * crop)
263{
264 return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
265}
Mathias Agopian52212712009-08-11 22:34:02 -0700266
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700267/*
268 * native_window_set_buffer_count(..., count)
269 * Sets the number of buffers associated with this native window.
270 */
271static inline int native_window_set_buffer_count(
272 android_native_window_t* window,
273 size_t bufferCount)
274{
275 return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
276}
277
Mathias Agopiana138f892010-05-21 17:24:35 -0700278/*
279 * native_window_set_buffers_geometry(..., int w, int h, int format)
280 * All buffers dequeued after this call will have the geometry specified.
281 * In particular, all buffers will have a fixed-size, independent form the
282 * native-window size. They will be appropriately scaled to the window-size
283 * upon composition.
284 *
285 * If all parameters are 0, the normal behavior is restored. That is,
286 * dequeued buffers following this call will be sized to the window's size.
287 *
288 */
289static inline int native_window_set_buffers_geometry(
290 android_native_window_t* window,
291 int w, int h, int format)
292{
293 return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
294 w, h, format);
295}
296
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700297// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700298
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700299/* FIXME: this is legacy for pixmaps */
Mathias Agopian238a66e2009-08-13 17:57:53 -0700300typedef struct egl_native_pixmap_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700301{
302 int32_t version; /* must be 32 */
303 int32_t width;
304 int32_t height;
305 int32_t stride;
306 uint8_t* data;
307 uint8_t format;
308 uint8_t rfu[3];
309 union {
310 uint32_t compressedFormat;
311 int32_t vstride;
312 };
313 int32_t reserved;
Mathias Agopian238a66e2009-08-13 17:57:53 -0700314} egl_native_pixmap_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700315
316/*****************************************************************************/
317
318#ifdef __cplusplus
319}
320#endif
321
322
323/*****************************************************************************/
324
325#ifdef __cplusplus
326
327#include <utils/RefBase.h>
328
329namespace android {
330
331/*
332 * This helper class turns an EGL android_native_xxx type into a C++
333 * reference-counted object; with proper type conversions.
334 */
335template <typename NATIVE_TYPE, typename TYPE, typename REF>
336class EGLNativeBase : public NATIVE_TYPE, public REF
337{
Jamie Gennis5e67f872010-05-10 17:33:32 -0700338public:
339 // Disambiguate between the incStrong in REF and NATIVE_TYPE
340 void incStrong(const void* id) const {
341 REF::incStrong(id);
342 }
343 void decStrong(const void* id) const {
344 REF::decStrong(id);
345 }
346
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700347protected:
348 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
349 EGLNativeBase() : NATIVE_TYPE(), REF() {
350 NATIVE_TYPE::common.incRef = incRef;
351 NATIVE_TYPE::common.decRef = decRef;
352 }
353 static inline TYPE* getSelf(NATIVE_TYPE* self) {
354 return static_cast<TYPE*>(self);
355 }
356 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
357 return static_cast<TYPE const *>(self);
358 }
359 static inline TYPE* getSelf(android_native_base_t* base) {
360 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
361 }
362 static inline TYPE const * getSelf(android_native_base_t const* base) {
363 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
364 }
365 static void incRef(android_native_base_t* base) {
366 EGLNativeBase* self = getSelf(base);
367 self->incStrong(self);
368 }
369 static void decRef(android_native_base_t* base) {
370 EGLNativeBase* self = getSelf(base);
371 self->decStrong(self);
372 }
373};
374
375} // namespace android
376#endif // __cplusplus
377
378/*****************************************************************************/
379
380#endif /* ANDROID_ANDROID_NATIVES_H */