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 | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame^] | 44 | typedef 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 52 | // --------------------------------------------------------------------------- |
| 53 | |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 54 | typedef struct android_native_base_t |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 55 | { |
| 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 Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 65 | void (*incRef)(struct android_native_base_t* base); |
| 66 | void (*decRef)(struct android_native_base_t* base); |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 67 | } android_native_base_t; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 68 | |
Mathias Agopian | aa8c0ff | 2009-05-05 18:29:35 -0700 | [diff] [blame] | 69 | // --------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 70 | |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 71 | /* attributes queriable with query() */ |
| 72 | enum { |
| 73 | NATIVE_WINDOW_WIDTH = 0, |
Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame^] | 74 | NATIVE_WINDOW_HEIGHT, |
| 75 | NATIVE_WINDOW_FORMAT, |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 78 | /* valid operations for the (*perform)() hook */ |
| 79 | enum { |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame] | 80 | NATIVE_WINDOW_SET_USAGE = 0, |
Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame^] | 81 | NATIVE_WINDOW_CONNECT, |
| 82 | NATIVE_WINDOW_DISCONNECT, |
| 83 | NATIVE_WINDOW_SET_CROP, |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* parameter for NATIVE_WINDOW_[DIS]CONNECT */ |
| 87 | enum { |
| 88 | NATIVE_WINDOW_API_EGL = 1 |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 91 | typedef struct android_native_window_t |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 92 | { |
| 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * Set the swap interval for this surface. |
| 124 | * |
| 125 | * Returns 0 on success or -errno on error. |
| 126 | */ |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 127 | int (*setSwapInterval)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 128 | int interval); |
| 129 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 130 | /* |
| 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 Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 133 | * this call may block if no buffers are available. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 134 | * |
| 135 | * Returns 0 on success or -errno on error. |
| 136 | */ |
Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame^] | 137 | int (*dequeueBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 138 | 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 Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 147 | int (*lockBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 148 | 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 Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 157 | int (*queueBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 158 | struct android_native_buffer_t* buffer); |
| 159 | |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 160 | /* |
| 161 | * hook used to retrieve information about the native window. |
| 162 | * |
| 163 | * Returns 0 on success or -errno on error. |
| 164 | */ |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 165 | int (*query)(struct android_native_window_t* window, |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 166 | int what, int* value); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 167 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 168 | /* |
| 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 Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame] | 176 | * (*perform)() returns -ENOENT if the 'what' parameter is not supported |
| 177 | * by the surface's implementation. |
| 178 | * |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 179 | * The valid operations are: |
| 180 | * NATIVE_WINDOW_SET_USAGE |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame] | 181 | * NATIVE_WINDOW_CONNECT |
| 182 | * NATIVE_WINDOW_DISCONNECT |
Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame^] | 183 | * NATIVE_WINDOW_SET_CROP |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 184 | * |
| 185 | */ |
| 186 | |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 187 | int (*perform)(struct android_native_window_t* window, |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 188 | int operation, ... ); |
| 189 | |
| 190 | void* reserved_proc[3]; |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 191 | } android_native_window_t; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 192 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 193 | |
| 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 Zavin | fae3e73 | 2009-08-13 16:50:54 -0700 | [diff] [blame] | 204 | static inline int native_window_set_usage( |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 205 | android_native_window_t* window, int usage) |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 206 | { |
| 207 | return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage); |
| 208 | } |
| 209 | |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame] | 210 | /* |
| 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 | */ |
| 216 | static 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 | */ |
| 228 | static 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 Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame^] | 234 | /* |
| 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 | */ |
| 246 | static 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 Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 252 | |
Mathias Agopian | aa8c0ff | 2009-05-05 18:29:35 -0700 | [diff] [blame] | 253 | // --------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 254 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 255 | /* FIXME: this is legacy for pixmaps */ |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 256 | typedef struct egl_native_pixmap_t |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 257 | { |
| 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 Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 270 | } egl_native_pixmap_t; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 271 | |
| 272 | /*****************************************************************************/ |
| 273 | |
| 274 | #ifdef __cplusplus |
| 275 | } |
| 276 | #endif |
| 277 | |
| 278 | |
| 279 | /*****************************************************************************/ |
| 280 | |
| 281 | #ifdef __cplusplus |
| 282 | |
| 283 | #include <utils/RefBase.h> |
| 284 | |
| 285 | namespace 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 | */ |
| 291 | template <typename NATIVE_TYPE, typename TYPE, typename REF> |
| 292 | class EGLNativeBase : public NATIVE_TYPE, public REF |
| 293 | { |
| 294 | protected: |
| 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 */ |