Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1 | /* |
| 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 |
| 26 | extern "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 | |
| 42 | struct android_native_buffer_t; |
| 43 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 44 | // --------------------------------------------------------------------------- |
| 45 | |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 46 | typedef struct android_native_base_t |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 47 | { |
| 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 Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 57 | void (*incRef)(struct android_native_base_t* base); |
| 58 | void (*decRef)(struct android_native_base_t* base); |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 59 | } android_native_base_t; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 60 | |
Mathias Agopian | aa8c0ff | 2009-05-05 18:29:35 -0700 | [diff] [blame] | 61 | // --------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 62 | |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 63 | /* attributes queriable with query() */ |
| 64 | enum { |
| 65 | NATIVE_WINDOW_WIDTH = 0, |
Mathias Agopian | 6b1f410 | 2009-08-06 16:04:29 -0700 | [diff] [blame] | 66 | NATIVE_WINDOW_HEIGHT = 1, |
| 67 | NATIVE_WINDOW_FORMAT = 2, |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 70 | /* valid operations for the (*perform)() hook */ |
| 71 | enum { |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame^] | 72 | NATIVE_WINDOW_SET_USAGE = 0, |
| 73 | NATIVE_WINDOW_CONNECT = 1, |
| 74 | NATIVE_WINDOW_DISCONNECT = 2 |
| 75 | }; |
| 76 | |
| 77 | /* parameter for NATIVE_WINDOW_[DIS]CONNECT */ |
| 78 | enum { |
| 79 | NATIVE_WINDOW_API_EGL = 1 |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 82 | typedef struct android_native_window_t |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 83 | { |
| 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * Set the swap interval for this surface. |
| 115 | * |
| 116 | * Returns 0 on success or -errno on error. |
| 117 | */ |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 118 | int (*setSwapInterval)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 119 | int interval); |
| 120 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 121 | /* |
| 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 Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 124 | * this call may block if no buffers are available. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 125 | * |
| 126 | * Returns 0 on success or -errno on error. |
| 127 | */ |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 128 | int (*dequeueBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 129 | 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 Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 138 | int (*lockBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 139 | 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 Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 148 | int (*queueBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 149 | struct android_native_buffer_t* buffer); |
| 150 | |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 151 | /* |
| 152 | * hook used to retrieve information about the native window. |
| 153 | * |
| 154 | * Returns 0 on success or -errno on error. |
| 155 | */ |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 156 | int (*query)(struct android_native_window_t* window, |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 157 | int what, int* value); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 158 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 159 | /* |
| 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 Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame^] | 167 | * (*perform)() returns -ENOENT if the 'what' parameter is not supported |
| 168 | * by the surface's implementation. |
| 169 | * |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 170 | * The valid operations are: |
| 171 | * NATIVE_WINDOW_SET_USAGE |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame^] | 172 | * NATIVE_WINDOW_CONNECT |
| 173 | * NATIVE_WINDOW_DISCONNECT |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 174 | * |
| 175 | */ |
| 176 | |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 177 | int (*perform)(struct android_native_window_t* window, |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 178 | int operation, ... ); |
| 179 | |
| 180 | void* reserved_proc[3]; |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 181 | } android_native_window_t; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 182 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 183 | |
| 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 Zavin | fae3e73 | 2009-08-13 16:50:54 -0700 | [diff] [blame] | 194 | static inline int native_window_set_usage( |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 195 | android_native_window_t* window, int usage) |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 196 | { |
| 197 | return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage); |
| 198 | } |
| 199 | |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame^] | 200 | /* |
| 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 | */ |
| 206 | static 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 | */ |
| 218 | static 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 Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 224 | |
Mathias Agopian | aa8c0ff | 2009-05-05 18:29:35 -0700 | [diff] [blame] | 225 | // --------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 226 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 227 | /* FIXME: this is legacy for pixmaps */ |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 228 | typedef struct egl_native_pixmap_t |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 229 | { |
| 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 Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 242 | } egl_native_pixmap_t; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 243 | |
| 244 | /*****************************************************************************/ |
| 245 | |
| 246 | #ifdef __cplusplus |
| 247 | } |
| 248 | #endif |
| 249 | |
| 250 | |
| 251 | /*****************************************************************************/ |
| 252 | |
| 253 | #ifdef __cplusplus |
| 254 | |
| 255 | #include <utils/RefBase.h> |
| 256 | |
| 257 | namespace 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 | */ |
| 263 | template <typename NATIVE_TYPE, typename TYPE, typename REF> |
| 264 | class EGLNativeBase : public NATIVE_TYPE, public REF |
| 265 | { |
| 266 | protected: |
| 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 */ |