blob: 49bfa2b0bd33943b3440e20dc0856d0ddcfa8594 [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 Agopiane156e642010-03-11 15:05:52 -080084};
85
86/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
87enum {
88 NATIVE_WINDOW_API_EGL = 1
Mathias Agopian52212712009-08-11 22:34:02 -070089};
90
Mathias Agopian238a66e2009-08-13 17:57:53 -070091typedef struct android_native_window_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -070092{
93#ifdef __cplusplus
94 android_native_window_t()
95 : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
96 {
97 common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
98 common.version = sizeof(android_native_window_t);
99 memset(common.reserved, 0, sizeof(common.reserved));
100 }
Jamie Gennis5e67f872010-05-10 17:33:32 -0700101
102 // Implement the methods that sp<android_native_window_t> expects so that it
103 // can be used to automatically refcount android_native_window_t's.
104 void incStrong(const void* id) const {
105 common.incRef(const_cast<android_native_base_t*>(&common));
106 }
107 void decStrong(const void* id) const {
108 common.decRef(const_cast<android_native_base_t*>(&common));
109 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700110#endif
111
112 struct android_native_base_t common;
113
114 /* flags describing some attributes of this surface or its updater */
115 const uint32_t flags;
116
117 /* min swap interval supported by this updated */
118 const int minSwapInterval;
119
120 /* max swap interval supported by this updated */
121 const int maxSwapInterval;
122
123 /* horizontal and vertical resolution in DPI */
124 const float xdpi;
125 const float ydpi;
126
127 /* Some storage reserved for the OEM's driver. */
128 intptr_t oem[4];
129
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130
131 /*
132 * Set the swap interval for this surface.
133 *
134 * Returns 0 on success or -errno on error.
135 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700136 int (*setSwapInterval)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137 int interval);
138
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 /*
140 * hook called by EGL to acquire a buffer. After this call, the buffer
141 * is not locked, so its content cannot be modified.
Mathias Agopian0926f502009-05-04 14:17:04 -0700142 * this call may block if no buffers are available.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143 *
144 * Returns 0 on success or -errno on error.
145 */
Mathias Agopiancc08e682010-04-15 18:48:26 -0700146 int (*dequeueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700147 struct android_native_buffer_t** buffer);
148
149 /*
150 * hook called by EGL to lock a buffer. This MUST be called before modifying
151 * the content of a buffer. The buffer must have been acquired with
152 * dequeueBuffer first.
153 *
154 * Returns 0 on success or -errno on error.
155 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700156 int (*lockBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157 struct android_native_buffer_t* buffer);
158 /*
159 * hook called by EGL when modifications to the render buffer are done.
160 * This unlocks and post the buffer.
161 *
162 * Buffers MUST be queued in the same order than they were dequeued.
163 *
164 * Returns 0 on success or -errno on error.
165 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700166 int (*queueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700167 struct android_native_buffer_t* buffer);
168
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700169 /*
170 * hook used to retrieve information about the native window.
171 *
172 * Returns 0 on success or -errno on error.
173 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700174 int (*query)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700175 int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700176
Mathias Agopian52212712009-08-11 22:34:02 -0700177 /*
178 * hook used to perform various operations on the surface.
179 * (*perform)() is a generic mechanism to add functionality to
180 * android_native_window_t while keeping backward binary compatibility.
181 *
182 * This hook should not be called directly, instead use the helper functions
183 * defined below.
184 *
Mathias Agopiane156e642010-03-11 15:05:52 -0800185 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
186 * by the surface's implementation.
187 *
Mathias Agopian52212712009-08-11 22:34:02 -0700188 * The valid operations are:
189 * NATIVE_WINDOW_SET_USAGE
Mathias Agopiane156e642010-03-11 15:05:52 -0800190 * NATIVE_WINDOW_CONNECT
191 * NATIVE_WINDOW_DISCONNECT
Mathias Agopiancc08e682010-04-15 18:48:26 -0700192 * NATIVE_WINDOW_SET_CROP
Mathias Agopian52212712009-08-11 22:34:02 -0700193 *
194 */
195
Mathias Agopian37b2a372009-08-17 12:33:20 -0700196 int (*perform)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700197 int operation, ... );
198
199 void* reserved_proc[3];
Mathias Agopian238a66e2009-08-13 17:57:53 -0700200} android_native_window_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201
Mathias Agopian52212712009-08-11 22:34:02 -0700202
203/*
204 * native_window_set_usage() sets the intended usage flags for the next
205 * buffers acquired with (*lockBuffer)() and on.
206 * By default (if this function is never called), a usage of
207 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
208 * is assumed.
209 * Calling this function will usually cause following buffers to be
210 * reallocated.
211 */
212
Dima Zavinfae3e732009-08-13 16:50:54 -0700213static inline int native_window_set_usage(
Mathias Agopian238a66e2009-08-13 17:57:53 -0700214 android_native_window_t* window, int usage)
Mathias Agopian52212712009-08-11 22:34:02 -0700215{
216 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
217}
218
Mathias Agopiane156e642010-03-11 15:05:52 -0800219/*
220 * native_window_connect(..., NATIVE_WINDOW_API_EGL) must be called
221 * by EGL when the window is made current.
222 * Returns -EINVAL if for some reason the window cannot be connected, which
223 * can happen if it's connected to some other API.
224 */
225static inline int native_window_connect(
226 android_native_window_t* window, int api)
227{
228 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
229}
230
231/*
232 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL) must be called
233 * by EGL when the window is made not current.
234 * An error is returned if for instance the window wasn't connected in the
235 * first place.
236 */
237static inline int native_window_disconnect(
238 android_native_window_t* window, int api)
239{
240 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
241}
242
Mathias Agopiancc08e682010-04-15 18:48:26 -0700243/*
244 * native_window_set_crop(..., crop) sets which region of the next queued
245 * buffers needs to be considered.
246 * A buffer's crop region is scaled to match the surface's size.
247 *
248 * The specified crop region applies to all buffers queued after it is called.
249 *
250 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
251 *
252 * An error is returned if for instance the crop region is invalid,
253 * out of the buffer's bound or if the window is invalid.
254 */
255static inline int native_window_set_crop(
256 android_native_window_t* window,
257 android_native_rect_t const * crop)
258{
259 return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
260}
Mathias Agopian52212712009-08-11 22:34:02 -0700261
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700262// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700263
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700264/* FIXME: this is legacy for pixmaps */
Mathias Agopian238a66e2009-08-13 17:57:53 -0700265typedef struct egl_native_pixmap_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700266{
267 int32_t version; /* must be 32 */
268 int32_t width;
269 int32_t height;
270 int32_t stride;
271 uint8_t* data;
272 uint8_t format;
273 uint8_t rfu[3];
274 union {
275 uint32_t compressedFormat;
276 int32_t vstride;
277 };
278 int32_t reserved;
Mathias Agopian238a66e2009-08-13 17:57:53 -0700279} egl_native_pixmap_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700280
281/*****************************************************************************/
282
283#ifdef __cplusplus
284}
285#endif
286
287
288/*****************************************************************************/
289
290#ifdef __cplusplus
291
292#include <utils/RefBase.h>
293
294namespace android {
295
296/*
297 * This helper class turns an EGL android_native_xxx type into a C++
298 * reference-counted object; with proper type conversions.
299 */
300template <typename NATIVE_TYPE, typename TYPE, typename REF>
301class EGLNativeBase : public NATIVE_TYPE, public REF
302{
Jamie Gennis5e67f872010-05-10 17:33:32 -0700303public:
304 // Disambiguate between the incStrong in REF and NATIVE_TYPE
305 void incStrong(const void* id) const {
306 REF::incStrong(id);
307 }
308 void decStrong(const void* id) const {
309 REF::decStrong(id);
310 }
311
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700312protected:
313 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
314 EGLNativeBase() : NATIVE_TYPE(), REF() {
315 NATIVE_TYPE::common.incRef = incRef;
316 NATIVE_TYPE::common.decRef = decRef;
317 }
318 static inline TYPE* getSelf(NATIVE_TYPE* self) {
319 return static_cast<TYPE*>(self);
320 }
321 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
322 return static_cast<TYPE const *>(self);
323 }
324 static inline TYPE* getSelf(android_native_base_t* base) {
325 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
326 }
327 static inline TYPE const * getSelf(android_native_base_t const* base) {
328 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
329 }
330 static void incRef(android_native_base_t* base) {
331 EGLNativeBase* self = getSelf(base);
332 self->incStrong(self);
333 }
334 static void decRef(android_native_base_t* base) {
335 EGLNativeBase* self = getSelf(base);
336 self->decStrong(self);
337 }
338};
339
340} // namespace android
341#endif // __cplusplus
342
343/*****************************************************************************/
344
345#endif /* ANDROID_ANDROID_NATIVES_H */