blob: ca89b06816cbfd8865441e5f3bbe356ec1522cb7 [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
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -070025#include <android/native_window.h>
26
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#ifdef __cplusplus
28extern "C" {
29#endif
30
31/*****************************************************************************/
32
33#define ANDROID_NATIVE_MAKE_CONSTANT(a,b,c,d) \
34 (((unsigned)(a)<<24)|((unsigned)(b)<<16)|((unsigned)(c)<<8)|(unsigned)(d))
35
36#define ANDROID_NATIVE_WINDOW_MAGIC \
37 ANDROID_NATIVE_MAKE_CONSTANT('_','w','n','d')
38
39#define ANDROID_NATIVE_BUFFER_MAGIC \
40 ANDROID_NATIVE_MAKE_CONSTANT('_','b','f','r')
41
42// ---------------------------------------------------------------------------
43
44struct android_native_buffer_t;
45
Mathias Agopiancc08e682010-04-15 18:48:26 -070046typedef struct android_native_rect_t
47{
48 int32_t left;
49 int32_t top;
50 int32_t right;
51 int32_t bottom;
52} android_native_rect_t;
53
Mathias Agopian076b1cc2009-04-10 14:24:30 -070054// ---------------------------------------------------------------------------
55
Mathias Agopian238a66e2009-08-13 17:57:53 -070056typedef struct android_native_base_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057{
58 /* a magic value defined by the actual EGL native type */
59 int magic;
60
61 /* the sizeof() of the actual EGL native type */
62 int version;
63
64 void* reserved[4];
65
66 /* reference-counting interface */
Mathias Agopian37b2a372009-08-17 12:33:20 -070067 void (*incRef)(struct android_native_base_t* base);
68 void (*decRef)(struct android_native_base_t* base);
Mathias Agopian238a66e2009-08-13 17:57:53 -070069} android_native_base_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -070071// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072
Mathias Agopiancb6b9042009-07-30 18:14:56 -070073/* attributes queriable with query() */
74enum {
75 NATIVE_WINDOW_WIDTH = 0,
Mathias Agopiancc08e682010-04-15 18:48:26 -070076 NATIVE_WINDOW_HEIGHT,
77 NATIVE_WINDOW_FORMAT,
Mathias Agopiancb6b9042009-07-30 18:14:56 -070078};
79
Mathias Agopian52212712009-08-11 22:34:02 -070080/* valid operations for the (*perform)() hook */
81enum {
Mathias Agopiane156e642010-03-11 15:05:52 -080082 NATIVE_WINDOW_SET_USAGE = 0,
Mathias Agopiancc08e682010-04-15 18:48:26 -070083 NATIVE_WINDOW_CONNECT,
84 NATIVE_WINDOW_DISCONNECT,
85 NATIVE_WINDOW_SET_CROP,
Mathias Agopianf10d7fd2010-05-21 14:19:50 -070086 NATIVE_WINDOW_SET_BUFFER_COUNT,
Mathias Agopiana138f892010-05-21 17:24:35 -070087 NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
Mathias Agopiane156e642010-03-11 15:05:52 -080088};
89
90/* parameter for NATIVE_WINDOW_[DIS]CONNECT */
91enum {
92 NATIVE_WINDOW_API_EGL = 1
Mathias Agopian52212712009-08-11 22:34:02 -070093};
94
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -070095struct ANativeWindow
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096{
97#ifdef __cplusplus
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -070098 ANativeWindow()
Mathias Agopian076b1cc2009-04-10 14:24:30 -070099 : flags(0), minSwapInterval(0), maxSwapInterval(0), xdpi(0), ydpi(0)
100 {
101 common.magic = ANDROID_NATIVE_WINDOW_MAGIC;
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700102 common.version = sizeof(ANativeWindow);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 memset(common.reserved, 0, sizeof(common.reserved));
104 }
Jamie Gennis5e67f872010-05-10 17:33:32 -0700105
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700106 // Implement the methods that sp<ANativeWindow> expects so that it
107 // can be used to automatically refcount ANativeWindow's.
Jamie Gennis5e67f872010-05-10 17:33:32 -0700108 void incStrong(const void* id) const {
109 common.incRef(const_cast<android_native_base_t*>(&common));
110 }
111 void decStrong(const void* id) const {
112 common.decRef(const_cast<android_native_base_t*>(&common));
113 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700114#endif
115
116 struct android_native_base_t common;
117
118 /* flags describing some attributes of this surface or its updater */
119 const uint32_t flags;
120
121 /* min swap interval supported by this updated */
122 const int minSwapInterval;
123
124 /* max swap interval supported by this updated */
125 const int maxSwapInterval;
126
127 /* horizontal and vertical resolution in DPI */
128 const float xdpi;
129 const float ydpi;
130
131 /* Some storage reserved for the OEM's driver. */
132 intptr_t oem[4];
133
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134
135 /*
136 * Set the swap interval for this surface.
137 *
138 * Returns 0 on success or -errno on error.
139 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700140 int (*setSwapInterval)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 int interval);
142
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143 /*
144 * hook called by EGL to acquire a buffer. After this call, the buffer
145 * is not locked, so its content cannot be modified.
Mathias Agopian0926f502009-05-04 14:17:04 -0700146 * this call may block if no buffers are available.
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700147 *
148 * Returns 0 on success or -errno on error.
149 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700150 int (*dequeueBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700151 struct android_native_buffer_t** buffer);
152
153 /*
154 * hook called by EGL to lock a buffer. This MUST be called before modifying
155 * the content of a buffer. The buffer must have been acquired with
156 * dequeueBuffer first.
157 *
158 * Returns 0 on success or -errno on error.
159 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700160 int (*lockBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700161 struct android_native_buffer_t* buffer);
162 /*
163 * hook called by EGL when modifications to the render buffer are done.
164 * This unlocks and post the buffer.
165 *
166 * Buffers MUST be queued in the same order than they were dequeued.
167 *
168 * Returns 0 on success or -errno on error.
169 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700170 int (*queueBuffer)(struct ANativeWindow* window,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171 struct android_native_buffer_t* buffer);
172
Mathias Agopiancb6b9042009-07-30 18:14:56 -0700173 /*
174 * hook used to retrieve information about the native window.
175 *
176 * Returns 0 on success or -errno on error.
177 */
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700178 int (*query)(struct ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700179 int what, int* value);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700180
Mathias Agopian52212712009-08-11 22:34:02 -0700181 /*
182 * hook used to perform various operations on the surface.
183 * (*perform)() is a generic mechanism to add functionality to
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700184 * ANativeWindow while keeping backward binary compatibility.
Mathias Agopian52212712009-08-11 22:34:02 -0700185 *
186 * This hook should not be called directly, instead use the helper functions
187 * defined below.
188 *
Mathias Agopiane156e642010-03-11 15:05:52 -0800189 * (*perform)() returns -ENOENT if the 'what' parameter is not supported
190 * by the surface's implementation.
191 *
Mathias Agopian52212712009-08-11 22:34:02 -0700192 * The valid operations are:
193 * NATIVE_WINDOW_SET_USAGE
Mathias Agopiane156e642010-03-11 15:05:52 -0800194 * NATIVE_WINDOW_CONNECT
195 * NATIVE_WINDOW_DISCONNECT
Mathias Agopiancc08e682010-04-15 18:48:26 -0700196 * NATIVE_WINDOW_SET_CROP
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700197 * NATIVE_WINDOW_SET_BUFFER_COUNT
Mathias Agopiana138f892010-05-21 17:24:35 -0700198 * NATIVE_WINDOW_SET_BUFFERS_GEOMETRY
Mathias Agopian52212712009-08-11 22:34:02 -0700199 *
200 */
201
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700202 int (*perform)(struct ANativeWindow* window,
Mathias Agopian52212712009-08-11 22:34:02 -0700203 int operation, ... );
204
205 void* reserved_proc[3];
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700206};
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700208// Backwards compatibility... please switch to ANativeWindow.
209typedef struct ANativeWindow android_native_window_t;
Mathias Agopian52212712009-08-11 22:34:02 -0700210
211/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700212 * native_window_set_usage(..., usage)
213 * Sets the intended usage flags for the next buffers
214 * acquired with (*lockBuffer)() and on.
Mathias Agopian52212712009-08-11 22:34:02 -0700215 * By default (if this function is never called), a usage of
216 * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE
217 * is assumed.
218 * Calling this function will usually cause following buffers to be
219 * reallocated.
220 */
221
Dima Zavinfae3e732009-08-13 16:50:54 -0700222static inline int native_window_set_usage(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700223 ANativeWindow* window, int usage)
Mathias Agopian52212712009-08-11 22:34:02 -0700224{
225 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage);
226}
227
Mathias Agopiane156e642010-03-11 15:05:52 -0800228/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700229 * native_window_connect(..., NATIVE_WINDOW_API_EGL)
230 * Must be called by EGL when the window is made current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800231 * Returns -EINVAL if for some reason the window cannot be connected, which
232 * can happen if it's connected to some other API.
233 */
234static inline int native_window_connect(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700235 ANativeWindow* window, int api)
Mathias Agopiane156e642010-03-11 15:05:52 -0800236{
237 return window->perform(window, NATIVE_WINDOW_CONNECT, api);
238}
239
240/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700241 * native_window_disconnect(..., NATIVE_WINDOW_API_EGL)
242 * Must be called by EGL when the window is made not current.
Mathias Agopiane156e642010-03-11 15:05:52 -0800243 * An error is returned if for instance the window wasn't connected in the
244 * first place.
245 */
246static inline int native_window_disconnect(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700247 ANativeWindow* window, int api)
Mathias Agopiane156e642010-03-11 15:05:52 -0800248{
249 return window->perform(window, NATIVE_WINDOW_DISCONNECT, api);
250}
251
Mathias Agopiancc08e682010-04-15 18:48:26 -0700252/*
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700253 * native_window_set_crop(..., crop)
254 * Sets which region of the next queued buffers needs to be considered.
Mathias Agopiancc08e682010-04-15 18:48:26 -0700255 * A buffer's crop region is scaled to match the surface's size.
256 *
257 * The specified crop region applies to all buffers queued after it is called.
258 *
259 * if 'crop' is NULL, subsequently queued buffers won't be cropped.
260 *
261 * An error is returned if for instance the crop region is invalid,
262 * out of the buffer's bound or if the window is invalid.
263 */
264static inline int native_window_set_crop(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700265 ANativeWindow* window,
Mathias Agopiancc08e682010-04-15 18:48:26 -0700266 android_native_rect_t const * crop)
267{
268 return window->perform(window, NATIVE_WINDOW_SET_CROP, crop);
269}
Mathias Agopian52212712009-08-11 22:34:02 -0700270
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700271/*
272 * native_window_set_buffer_count(..., count)
273 * Sets the number of buffers associated with this native window.
274 */
275static inline int native_window_set_buffer_count(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700276 ANativeWindow* window,
Mathias Agopianf10d7fd2010-05-21 14:19:50 -0700277 size_t bufferCount)
278{
279 return window->perform(window, NATIVE_WINDOW_SET_BUFFER_COUNT, bufferCount);
280}
281
Mathias Agopiana138f892010-05-21 17:24:35 -0700282/*
283 * native_window_set_buffers_geometry(..., int w, int h, int format)
284 * All buffers dequeued after this call will have the geometry specified.
285 * In particular, all buffers will have a fixed-size, independent form the
286 * native-window size. They will be appropriately scaled to the window-size
287 * upon composition.
288 *
289 * If all parameters are 0, the normal behavior is restored. That is,
290 * dequeued buffers following this call will be sized to the window's size.
291 *
292 */
293static inline int native_window_set_buffers_geometry(
Dianne Hackborn4b5e91e2010-06-30 13:56:17 -0700294 ANativeWindow* window,
Mathias Agopiana138f892010-05-21 17:24:35 -0700295 int w, int h, int format)
296{
297 return window->perform(window, NATIVE_WINDOW_SET_BUFFERS_GEOMETRY,
298 w, h, format);
299}
300
Mathias Agopianaa8c0ff2009-05-05 18:29:35 -0700301// ---------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700302
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700303/* FIXME: this is legacy for pixmaps */
Mathias Agopian238a66e2009-08-13 17:57:53 -0700304typedef struct egl_native_pixmap_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700305{
306 int32_t version; /* must be 32 */
307 int32_t width;
308 int32_t height;
309 int32_t stride;
310 uint8_t* data;
311 uint8_t format;
312 uint8_t rfu[3];
313 union {
314 uint32_t compressedFormat;
315 int32_t vstride;
316 };
317 int32_t reserved;
Mathias Agopian238a66e2009-08-13 17:57:53 -0700318} egl_native_pixmap_t;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700319
320/*****************************************************************************/
321
322#ifdef __cplusplus
323}
324#endif
325
326
327/*****************************************************************************/
328
329#ifdef __cplusplus
330
331#include <utils/RefBase.h>
332
333namespace android {
334
335/*
336 * This helper class turns an EGL android_native_xxx type into a C++
337 * reference-counted object; with proper type conversions.
338 */
339template <typename NATIVE_TYPE, typename TYPE, typename REF>
340class EGLNativeBase : public NATIVE_TYPE, public REF
341{
Jamie Gennis5e67f872010-05-10 17:33:32 -0700342public:
343 // Disambiguate between the incStrong in REF and NATIVE_TYPE
344 void incStrong(const void* id) const {
345 REF::incStrong(id);
346 }
347 void decStrong(const void* id) const {
348 REF::decStrong(id);
349 }
350
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700351protected:
352 typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE;
353 EGLNativeBase() : NATIVE_TYPE(), REF() {
354 NATIVE_TYPE::common.incRef = incRef;
355 NATIVE_TYPE::common.decRef = decRef;
356 }
357 static inline TYPE* getSelf(NATIVE_TYPE* self) {
358 return static_cast<TYPE*>(self);
359 }
360 static inline TYPE const* getSelf(NATIVE_TYPE const* self) {
361 return static_cast<TYPE const *>(self);
362 }
363 static inline TYPE* getSelf(android_native_base_t* base) {
364 return getSelf(reinterpret_cast<NATIVE_TYPE*>(base));
365 }
366 static inline TYPE const * getSelf(android_native_base_t const* base) {
367 return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base));
368 }
369 static void incRef(android_native_base_t* base) {
370 EGLNativeBase* self = getSelf(base);
371 self->incStrong(self);
372 }
373 static void decRef(android_native_base_t* base) {
374 EGLNativeBase* self = getSelf(base);
375 self->decStrong(self);
376 }
377};
378
379} // namespace android
380#endif // __cplusplus
381
382/*****************************************************************************/
383
384#endif /* ANDROID_ANDROID_NATIVES_H */