blob: 773fd9301f638bf5c51cc3af6a7b79b7ccb0d25c [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 Agopian076b1cc2009-04-10 14:24:30 -070044// ---------------------------------------------------------------------------
45
Mathias Agopian238a66e2009-08-13 17:57:53 -070046typedef struct android_native_base_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047{
48 /* a magic value defined by the actual EGL native type */
49 int magic;
50
51 /* the sizeof() of the actual EGL native type */
52 int version;
53
54 void* reserved[4];
55
56 /* reference-counting interface */
Mathias Agopian37b2a372009-08-17 12:33:20 -070057 void (*incRef)(struct android_native_base_t* base);
58 void (*decRef)(struct android_native_base_t* base);
Mathias Agopian238a66e2009-08-13 17:57:53 -070059} android_native_base_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -070061// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -070062
Mathias Agopiancb6b9042009-07-30 18:14:56 -070063/* attributes queriable with query() */
64enum {
65 NATIVE_WINDOW_WIDTH = 0,
Mathias Agopian6b1f4102009-08-06 16:04:29 -070066 NATIVE_WINDOW_HEIGHT = 1,
67 NATIVE_WINDOW_FORMAT = 2,
Mathias Agopiancb6b9042009-07-30 18:14:56 -070068};
69
Mathias Agopian52212712009-08-11 22:34:02 -070070/* valid operations for the (*perform)() hook */
71enum {
Mathias Agopiane156e642010-03-11 15:05:52 -080072 NATIVE_WINDOW_SET_USAGE = 0,
73 NATIVE_WINDOW_CONNECT = 1,
74 NATIVE_WINDOW_DISCONNECT = 2
75};
76
77/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
78enum {
79 NATIVE_WINDOW_API_EGL = 1
Mathias Agopian52212712009-08-11 22:34:02 -070080};
81
Mathias Agopian238a66e2009-08-13 17:57:53 -070082typedef struct android_native_window_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083{
84#ifdef __cplusplus
85 android_native_window_t()
86 : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
87 {
88 common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
89 common.version = sizeof(android_native_window_t);
90 memset(common.reserved, 0, sizeof(common.reserved));
91 }
92#endif
93
94 struct android_native_base_t common;
95
96 /* flags describing some attributes of this surface or its updater */
97 const uint32_t flags;
98
99 /* min swap interval supported by this updated */
100 const int minSwapInterval;
101
102 /* max swap interval supported by this updated */
103 const int maxSwapInterval;
104
105 /* horizontal and vertical resolution in DPI */
106 const float xdpi;
107 const float ydpi;
108
109 /* Some storage reserved for the OEM's driver. */
110 intptr_t oem[4];
111
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700112
113 /*
114 * Set the swap interval for this surface.
115 *
116 * Returns 0 on success or -errno on error.
117 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700118 int (*setSwapInterval)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700119 int interval);
120
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700121 /*
122 * hook called by EGL to acquire a buffer. After this call, the buffer
123 * is not locked, so its content cannot be modified.
Mathias Agopian0926f502009-05-04 14:17:04 -0700124 * this call may block if no buffers are available.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700125 *
126 * Returns 0 on success or -errno on error.
127 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700128 int (*dequeueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700129 struct android_native_buffer_t** buffer);
130
131 /*
132 * hook called by EGL to lock a buffer. This MUST be called before modifying
133 * the content of a buffer. The buffer must have been acquired with
134 * dequeueBuffer first.
135 *
136 * Returns 0 on success or -errno on error.
137 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700138 int (*lockBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 struct android_native_buffer_t* buffer);
140 /*
141 * hook called by EGL when modifications to the render buffer are done.
142 * This unlocks and post the buffer.
143 *
144 * Buffers MUST be queued in the same order than they were dequeued.
145 *
146 * Returns 0 on success or -errno on error.
147 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700148 int (*queueBuffer)(struct android_native_window_t* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149 struct android_native_buffer_t* buffer);
150
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700151 /*
152 * hook used to retrieve information about the native window.
153 *
154 * Returns 0 on success or -errno on error.
155 */
Mathias Agopian37b2a372009-08-17 12:33:20 -0700156 int (*query)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700157 int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700158
Mathias Agopian52212712009-08-11 22:34:02 -0700159 /*
160 * hook used to perform various operations on the surface.
161 * (*perform)() is a generic mechanism to add functionality to
162 * android_native_window_t while keeping backward binary compatibility.
163 *
164 * This hook should not be called directly, instead use the helper functions
165 * defined below.
166 *
Mathias Agopiane156e642010-03-11 15:05:52 -0800167 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
168 * by the surface's implementation.
169 *
Mathias Agopian52212712009-08-11 22:34:02 -0700170 * The valid operations are:
171 * NATIVE_WINDOW_SET_USAGE
Mathias Agopiane156e642010-03-11 15:05:52 -0800172 * NATIVE_WINDOW_CONNECT
173 * NATIVE_WINDOW_DISCONNECT
Mathias Agopian52212712009-08-11 22:34:02 -0700174 *
175 */
176
Mathias Agopian37b2a372009-08-17 12:33:20 -0700177 int (*perform)(struct android_native_window_t* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700178 int operation, ... );
179
180 void* reserved_proc[3];
Mathias Agopian238a66e2009-08-13 17:57:53 -0700181} android_native_window_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700182
Mathias Agopian52212712009-08-11 22:34:02 -0700183
184/*
185 * native_window_set_usage() sets the intended usage flags for the next
186 * buffers acquired with (*lockBuffer)() and on.
187 * By default (if this function is never called), a usage of
188 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
189 * is assumed.
190 * Calling this function will usually cause following buffers to be
191 * reallocated.
192 */
193
Dima Zavinfae3e732009-08-13 16:50:54 -0700194static inline int native_window_set_usage(
Mathias Agopian238a66e2009-08-13 17:57:53 -0700195 android_native_window_t* window, int usage)
Mathias Agopian52212712009-08-11 22:34:02 -0700196{
197 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
198}
199
Mathias Agopiane156e642010-03-11 15:05:52 -0800200/*
201 * native_window_connect(..., NATIVE_WINDOW_API_EGL) must be called
202 * by EGL when the window is made current.
203 * Returns -EINVAL if for some reason the window cannot be connected, which
204 * can happen if it's connected to some other API.
205 */
206static inline int native_window_connect(
207 android_native_window_t* window, int api)
208{
209 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
210}
211
212/*
213 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL) must be called
214 * by EGL when the window is made not current.
215 * An error is returned if for instance the window wasn't connected in the
216 * first place.
217 */
218static inline int native_window_disconnect(
219 android_native_window_t* window, int api)
220{
221 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
222}
223
Mathias Agopian52212712009-08-11 22:34:02 -0700224
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700225// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700226
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700227/* FIXME: this is legacy for pixmaps */
Mathias Agopian238a66e2009-08-13 17:57:53 -0700228typedef struct egl_native_pixmap_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700229{
230 int32_t version; /* must be 32 */
231 int32_t width;
232 int32_t height;
233 int32_t stride;
234 uint8_t* data;
235 uint8_t format;
236 uint8_t rfu[3];
237 union {
238 uint32_t compressedFormat;
239 int32_t vstride;
240 };
241 int32_t reserved;
Mathias Agopian238a66e2009-08-13 17:57:53 -0700242} egl_native_pixmap_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243
244/*****************************************************************************/
245
246#ifdef __cplusplus
247}
248#endif
249
250
251/*****************************************************************************/
252
253#ifdef __cplusplus
254
255#include <utils/RefBase.h>
256
257namespace android {
258
259/*
260 * This helper class turns an EGL android_native_xxx type into a C++
261 * reference-counted object; with proper type conversions.
262 */
263template <typename NATIVE_TYPE, typename TYPE, typename REF>
264class EGLNativeBase : public NATIVE_TYPE, public REF
265{
266protected:
267 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
268 EGLNativeBase() : NATIVE_TYPE(), REF() {
269 NATIVE_TYPE::common.incRef = incRef;
270 NATIVE_TYPE::common.decRef = decRef;
271 }
272 static inline TYPE* getSelf(NATIVE_TYPE* self) {
273 return static_cast<TYPE*>(self);
274 }
275 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
276 return static_cast<TYPE const *>(self);
277 }
278 static inline TYPE* getSelf(android_native_base_t* base) {
279 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
280 }
281 static inline TYPE const * getSelf(android_native_base_t const* base) {
282 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
283 }
284 static void incRef(android_native_base_t* base) {
285 EGLNativeBase* self = getSelf(base);
286 self->incStrong(self);
287 }
288 static void decRef(android_native_base_t* base) {
289 EGLNativeBase* self = getSelf(base);
290 self->decStrong(self);
291 }
292};
293
294} // namespace android
295#endif // __cplusplus
296
297/*****************************************************************************/
298
299#endif /* ANDROID_ANDROID_NATIVES_H */