blob: b44901ff5b0cb10f8b4df84ad88c6cb47ae75da1 [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 Agopiane156e642010-03-11 15:05:52 -080085};
86
87/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
88enum {
89 NATIVE_WINDOW_API_EGL = 1
Mathias Agopian52212712009-08-11 22:34:02 -070090};
91
Mathias Agopian238a66e2009-08-13 17:57:53 -070092typedef struct android_native_window_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -070093{
94#ifdef __cplusplus
95 android_native_window_t()
96 : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
97 {
98 common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
99 common.version = sizeof(android_native_window_t);
100 memset(common.reserved, 0, sizeof(common.reserved));
101 }
Jamie Gennis5e67f872010-05-10 17:33:32 -0700102
103 // Implement the methods that sp<android_native_window_t> expects so that it
104 // can be used to automatically refcount android_native_window_t's.
105 void incStrong(const void* id) const {
106 common.incRef(const_cast<android_native_base_t*>(&common));
107 }
108 void decStrong(const void* id) const {
109 common.decRef(const_cast<android_native_base_t*>(&common));
110 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700111#endif
112
113 struct android_native_base_t common;
114
115 /* flags describing some attributes of this surface or its updater */
116 const uint32_t flags;
117
118 /* min swap interval supported by this updated */
119 const int minSwapInterval;
120
121 /* max swap interval supported by this updated */
122 const int maxSwapInterval;
123
124 /* horizontal and vertical resolution in DPI */
125 const float xdpi;
126 const float ydpi;
127
128 /* Some storage reserved for the OEM's driver. */
129 intptr_t oem[4];
130
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131
132 /*
133 * Set the swap interval for this surface.
134 *
135 * Returns 0 on success or -errno on error.
136 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700137 int (*setSwapInterval)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 int interval);
139
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 /*
141 * hook called by EGL to acquire a buffer. After this call, the buffer
142 * is not locked, so its content cannot be modified.
Mathias Agopian0926f502009-05-04 14:17:04 -0700143 * this call may block if no buffers are available.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700144 *
145 * Returns 0 on success or -errno on error.
146 */
Mathias Agopiancc08e682010-04-15 18:48:26 -0700147 int (*dequeueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700148 struct android_native_buffer_t** buffer);
149
150 /*
151 * hook called by EGL to lock a buffer. This MUST be called before modifying
152 * the content of a buffer. The buffer must have been acquired with
153 * dequeueBuffer first.
154 *
155 * Returns 0 on success or -errno on error.
156 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700157 int (*lockBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700158 struct android_native_buffer_t* buffer);
159 /*
160 * hook called by EGL when modifications to the render buffer are done.
161 * This unlocks and post the buffer.
162 *
163 * Buffers MUST be queued in the same order than they were dequeued.
164 *
165 * Returns 0 on success or -errno on error.
166 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700167 int (*queueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700168 struct android_native_buffer_t* buffer);
169
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700170 /*
171 * hook used to retrieve information about the native window.
172 *
173 * Returns 0 on success or -errno on error.
174 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700175 int (*query)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700176 int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177
Mathias Agopian52212712009-08-11 22:34:02 -0700178 /*
179 * hook used to perform various operations on the surface.
180 * (*perform)() is a generic mechanism to add functionality to
181 * android_native_window_t while keeping backward binary compatibility.
182 *
183 * This hook should not be called directly, instead use the helper functions
184 * defined below.
185 *
Mathias Agopiane156e642010-03-11 15:05:52 -0800186 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
187 * by the surface's implementation.
188 *
Mathias Agopian52212712009-08-11 22:34:02 -0700189 * The valid operations are:
190 * NATIVE_WINDOW_SET_USAGE
Mathias Agopiane156e642010-03-11 15:05:52 -0800191 * NATIVE_WINDOW_CONNECT
192 * NATIVE_WINDOW_DISCONNECT
Mathias Agopiancc08e682010-04-15 18:48:26 -0700193 * NATIVE_WINDOW_SET_CROP
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700194 * NATIVE_WINDOW_SET_BUFFER_COUNT
Mathias Agopian52212712009-08-11 22:34:02 -0700195 *
196 */
197
Mathias Agopian37b2a372009-08-17 12:33:20 -0700198 int (*perform)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700199 int operation, ... );
200
201 void* reserved_proc[3];
Mathias Agopian238a66e2009-08-13 17:57:53 -0700202} android_native_window_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700203
Mathias Agopian52212712009-08-11 22:34:02 -0700204
205/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700206 * native_window_set_usage(..., usage)
207 * Sets the intended usage flags for the next buffers
208 * acquired with (*lockBuffer)() and on.
Mathias Agopian52212712009-08-11 22:34:02 -0700209 * By default (if this function is never called), a usage of
210 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
211 * is assumed.
212 * Calling this function will usually cause following buffers to be
213 * reallocated.
214 */
215
Dima Zavinfae3e732009-08-13 16:50:54 -0700216static inline int native_window_set_usage(
Mathias Agopian238a66e2009-08-13 17:57:53 -0700217 android_native_window_t* window, int usage)
Mathias Agopian52212712009-08-11 22:34:02 -0700218{
219 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
220}
221
Mathias Agopiane156e642010-03-11 15:05:52 -0800222/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700223 * native_window_connect(..., NATIVE_WINDOW_API_EGL)
224 * Must be called by EGL when the window is made current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800225 * Returns -EINVAL if for some reason the window cannot be connected, which
226 * can happen if it's connected to some other API.
227 */
228static inline int native_window_connect(
229 android_native_window_t* window, int api)
230{
231 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
232}
233
234/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700235 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
236 * Must be called by EGL when the window is made not current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800237 * An error is returned if for instance the window wasn't connected in the
238 * first place.
239 */
240static inline int native_window_disconnect(
241 android_native_window_t* window, int api)
242{
243 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
244}
245
Mathias Agopiancc08e682010-04-15 18:48:26 -0700246/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700247 * native_window_set_crop(..., crop)
248 * Sets which region of the next queued buffers needs to be considered.
Mathias Agopiancc08e682010-04-15 18:48:26 -0700249 * A buffer's crop region is scaled to match the surface's size.
250 *
251 * The specified crop region applies to all buffers queued after it is called.
252 *
253 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
254 *
255 * An error is returned if for instance the crop region is invalid,
256 * out of the buffer's bound or if the window is invalid.
257 */
258static inline int native_window_set_crop(
259 android_native_window_t* window,
260 android_native_rect_t const * crop)
261{
262 return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
263}
Mathias Agopian52212712009-08-11 22:34:02 -0700264
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700265/*
266 * native_window_set_buffer_count(..., count)
267 * Sets the number of buffers associated with this native window.
268 */
269static inline int native_window_set_buffer_count(
270 android_native_window_t* window,
271 size_t bufferCount)
272{
273 return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
274}
275
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700276// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700277
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278/* FIXME: this is legacy for pixmaps */
Mathias Agopian238a66e2009-08-13 17:57:53 -0700279typedef struct egl_native_pixmap_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280{
281 int32_t version; /* must be 32 */
282 int32_t width;
283 int32_t height;
284 int32_t stride;
285 uint8_t* data;
286 uint8_t format;
287 uint8_t rfu[3];
288 union {
289 uint32_t compressedFormat;
290 int32_t vstride;
291 };
292 int32_t reserved;
Mathias Agopian238a66e2009-08-13 17:57:53 -0700293} egl_native_pixmap_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700294
295/*****************************************************************************/
296
297#ifdef __cplusplus
298}
299#endif
300
301
302/*****************************************************************************/
303
304#ifdef __cplusplus
305
306#include <utils/RefBase.h>
307
308namespace android {
309
310/*
311 * This helper class turns an EGL android_native_xxx type into a C++
312 * reference-counted object; with proper type conversions.
313 */
314template <typename NATIVE_TYPE, typename TYPE, typename REF>
315class EGLNativeBase : public NATIVE_TYPE, public REF
316{
Jamie Gennis5e67f872010-05-10 17:33:32 -0700317public:
318 // Disambiguate between the incStrong in REF and NATIVE_TYPE
319 void incStrong(const void* id) const {
320 REF::incStrong(id);
321 }
322 void decStrong(const void* id) const {
323 REF::decStrong(id);
324 }
325
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700326protected:
327 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
328 EGLNativeBase() : NATIVE_TYPE(), REF() {
329 NATIVE_TYPE::common.incRef = incRef;
330 NATIVE_TYPE::common.decRef = decRef;
331 }
332 static inline TYPE* getSelf(NATIVE_TYPE* self) {
333 return static_cast<TYPE*>(self);
334 }
335 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
336 return static_cast<TYPE const *>(self);
337 }
338 static inline TYPE* getSelf(android_native_base_t* base) {
339 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
340 }
341 static inline TYPE const * getSelf(android_native_base_t const* base) {
342 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
343 }
344 static void incRef(android_native_base_t* base) {
345 EGLNativeBase* self = getSelf(base);
346 self->incStrong(self);
347 }
348 static void decRef(android_native_base_t* base) {
349 EGLNativeBase* self = getSelf(base);
350 self->decStrong(self);
351 }
352};
353
354} // namespace android
355#endif // __cplusplus
356
357/*****************************************************************************/
358
359#endif /* ANDROID_ANDROID_NATIVES_H */