blob: 471c3c70cb2ea11a0dd09734404d80ba7dd3a0e8 [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 }
101#endif
102
103 struct android_native_base_t common;
104
105 /* flags describing some attributes of this surface or its updater */
106 const uint32_t flags;
107
108 /* min swap interval supported by this updated */
109 const int minSwapInterval;
110
111 /* max swap interval supported by this updated */
112 const int maxSwapInterval;
113
114 /* horizontal and vertical resolution in DPI */
115 const float xdpi;
116 const float ydpi;
117
118 /* Some storage reserved for the OEM's driver. */
119 intptr_t oem[4];
120
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121
122 /*
123 * Set the swap interval for this surface.
124 *
125 * Returns 0 on success or -errno on error.
126 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700127 int (*setSwapInterval)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700128 int interval);
129
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130 /*
131 * hook called by EGL to acquire a buffer. After this call, the buffer
132 * is not locked, so its content cannot be modified.
Mathias Agopian0926f502009-05-04 14:17:04 -0700133 * this call may block if no buffers are available.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 *
135 * Returns 0 on success or -errno on error.
136 */
Mathias Agopiancc08e682010-04-15 18:48:26 -0700137 int (*dequeueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 struct android_native_buffer_t** buffer);
139
140 /*
141 * hook called by EGL to lock a buffer. This MUST be called before modifying
142 * the content of a buffer. The buffer must have been acquired with
143 * dequeueBuffer first.
144 *
145 * Returns 0 on success or -errno on error.
146 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700147 int (*lockBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700148 struct android_native_buffer_t* buffer);
149 /*
150 * hook called by EGL when modifications to the render buffer are done.
151 * This unlocks and post the buffer.
152 *
153 * Buffers MUST be queued in the same order than they were dequeued.
154 *
155 * Returns 0 on success or -errno on error.
156 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700157 int (*queueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700158 struct android_native_buffer_t* buffer);
159
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700160 /*
161 * hook used to retrieve information about the native window.
162 *
163 * Returns 0 on success or -errno on error.
164 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700165 int (*query)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700166 int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700167
Mathias Agopian52212712009-08-11 22:34:02 -0700168 /*
169 * hook used to perform various operations on the surface.
170 * (*perform)() is a generic mechanism to add functionality to
171 * android_native_window_t while keeping backward binary compatibility.
172 *
173 * This hook should not be called directly, instead use the helper functions
174 * defined below.
175 *
Mathias Agopiane156e642010-03-11 15:05:52 -0800176 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
177 * by the surface's implementation.
178 *
Mathias Agopian52212712009-08-11 22:34:02 -0700179 * The valid operations are:
180 * NATIVE_WINDOW_SET_USAGE
Mathias Agopiane156e642010-03-11 15:05:52 -0800181 * NATIVE_WINDOW_CONNECT
182 * NATIVE_WINDOW_DISCONNECT
Mathias Agopiancc08e682010-04-15 18:48:26 -0700183 * NATIVE_WINDOW_SET_CROP
Mathias Agopian52212712009-08-11 22:34:02 -0700184 *
185 */
186
Mathias Agopian37b2a372009-08-17 12:33:20 -0700187 int (*perform)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700188 int operation, ... );
189
190 void* reserved_proc[3];
Mathias Agopian238a66e2009-08-13 17:57:53 -0700191} android_native_window_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192
Mathias Agopian52212712009-08-11 22:34:02 -0700193
194/*
195 * native_window_set_usage() sets the intended usage flags for the next
196 * buffers acquired with (*lockBuffer)() and on.
197 * By default (if this function is never called), a usage of
198 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
199 * is assumed.
200 * Calling this function will usually cause following buffers to be
201 * reallocated.
202 */
203
Dima Zavinfae3e732009-08-13 16:50:54 -0700204static inline int native_window_set_usage(
Mathias Agopian238a66e2009-08-13 17:57:53 -0700205 android_native_window_t* window, int usage)
Mathias Agopian52212712009-08-11 22:34:02 -0700206{
207 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
208}
209
Mathias Agopiane156e642010-03-11 15:05:52 -0800210/*
211 * native_window_connect(..., NATIVE_WINDOW_API_EGL) must be called
212 * by EGL when the window is made current.
213 * Returns -EINVAL if for some reason the window cannot be connected, which
214 * can happen if it's connected to some other API.
215 */
216static inline int native_window_connect(
217 android_native_window_t* window, int api)
218{
219 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
220}
221
222/*
223 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL) must be called
224 * by EGL when the window is made not current.
225 * An error is returned if for instance the window wasn't connected in the
226 * first place.
227 */
228static inline int native_window_disconnect(
229 android_native_window_t* window, int api)
230{
231 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
232}
233
Mathias Agopiancc08e682010-04-15 18:48:26 -0700234/*
235 * native_window_set_crop(..., crop) sets which region of the next queued
236 * buffers needs to be considered.
237 * A buffer's crop region is scaled to match the surface's size.
238 *
239 * The specified crop region applies to all buffers queued after it is called.
240 *
241 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
242 *
243 * An error is returned if for instance the crop region is invalid,
244 * out of the buffer's bound or if the window is invalid.
245 */
246static inline int native_window_set_crop(
247 android_native_window_t* window,
248 android_native_rect_t const * crop)
249{
250 return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
251}
Mathias Agopian52212712009-08-11 22:34:02 -0700252
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700253// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700255/* FIXME: this is legacy for pixmaps */
Mathias Agopian238a66e2009-08-13 17:57:53 -0700256typedef struct egl_native_pixmap_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257{
258 int32_t version; /* must be 32 */
259 int32_t width;
260 int32_t height;
261 int32_t stride;
262 uint8_t* data;
263 uint8_t format;
264 uint8_t rfu[3];
265 union {
266 uint32_t compressedFormat;
267 int32_t vstride;
268 };
269 int32_t reserved;
Mathias Agopian238a66e2009-08-13 17:57:53 -0700270} egl_native_pixmap_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700271
272/*****************************************************************************/
273
274#ifdef __cplusplus
275}
276#endif
277
278
279/*****************************************************************************/
280
281#ifdef __cplusplus
282
283#include <utils/RefBase.h>
284
285namespace android {
286
287/*
288 * This helper class turns an EGL android_native_xxx type into a C++
289 * reference-counted object; with proper type conversions.
290 */
291template <typename NATIVE_TYPE, typename TYPE, typename REF>
292class EGLNativeBase : public NATIVE_TYPE, public REF
293{
294protected:
295 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
296 EGLNativeBase() : NATIVE_TYPE(), REF() {
297 NATIVE_TYPE::common.incRef = incRef;
298 NATIVE_TYPE::common.decRef = decRef;
299 }
300 static inline TYPE* getSelf(NATIVE_TYPE* self) {
301 return static_cast<TYPE*>(self);
302 }
303 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
304 return static_cast<TYPE const *>(self);
305 }
306 static inline TYPE* getSelf(android_native_base_t* base) {
307 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
308 }
309 static inline TYPE const * getSelf(android_native_base_t const* base) {
310 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
311 }
312 static void incRef(android_native_base_t* base) {
313 EGLNativeBase* self = getSelf(base);
314 self->incStrong(self);
315 }
316 static void decRef(android_native_base_t* base) {
317 EGLNativeBase* self = getSelf(base);
318 self->decStrong(self);
319 }
320};
321
322} // namespace android
323#endif // __cplusplus
324
325/*****************************************************************************/
326
327#endif /* ANDROID_ANDROID_NATIVES_H */