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 | } |
Jamie Gennis | 5e67f87 | 2010-05-10 17:33:32 -0700 | [diff] [blame^] | 101 | |
| 102 | // Implement the methods that sp<android_native_window_t> expects so that it |
| 103 | // can be used to automatically refcount android_native_window_t's. |
| 104 | void incStrong(const void* id) const { |
| 105 | common.incRef(const_cast<android_native_base_t*>(&common)); |
| 106 | } |
| 107 | void decStrong(const void* id) const { |
| 108 | common.decRef(const_cast<android_native_base_t*>(&common)); |
| 109 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | struct android_native_base_t common; |
| 113 | |
| 114 | /* flags describing some attributes of this surface or its updater */ |
| 115 | const uint32_t flags; |
| 116 | |
| 117 | /* min swap interval supported by this updated */ |
| 118 | const int minSwapInterval; |
| 119 | |
| 120 | /* max swap interval supported by this updated */ |
| 121 | const int maxSwapInterval; |
| 122 | |
| 123 | /* horizontal and vertical resolution in DPI */ |
| 124 | const float xdpi; |
| 125 | const float ydpi; |
| 126 | |
| 127 | /* Some storage reserved for the OEM's driver. */ |
| 128 | intptr_t oem[4]; |
| 129 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Set the swap interval for this surface. |
| 133 | * |
| 134 | * Returns 0 on success or -errno on error. |
| 135 | */ |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 136 | int (*setSwapInterval)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 137 | int interval); |
| 138 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 139 | /* |
| 140 | * hook called by EGL to acquire a buffer. After this call, the buffer |
| 141 | * is not locked, so its content cannot be modified. |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 142 | * this call may block if no buffers are available. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 143 | * |
| 144 | * Returns 0 on success or -errno on error. |
| 145 | */ |
Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 146 | int (*dequeueBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 147 | struct android_native_buffer_t** buffer); |
| 148 | |
| 149 | /* |
| 150 | * hook called by EGL to lock a buffer. This MUST be called before modifying |
| 151 | * the content of a buffer. The buffer must have been acquired with |
| 152 | * dequeueBuffer first. |
| 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 (*lockBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 157 | struct android_native_buffer_t* buffer); |
| 158 | /* |
| 159 | * hook called by EGL when modifications to the render buffer are done. |
| 160 | * This unlocks and post the buffer. |
| 161 | * |
| 162 | * Buffers MUST be queued in the same order than they were dequeued. |
| 163 | * |
| 164 | * Returns 0 on success or -errno on error. |
| 165 | */ |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 166 | int (*queueBuffer)(struct android_native_window_t* window, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 167 | struct android_native_buffer_t* buffer); |
| 168 | |
Mathias Agopian | cb6b904 | 2009-07-30 18:14:56 -0700 | [diff] [blame] | 169 | /* |
| 170 | * hook used to retrieve information about the native window. |
| 171 | * |
| 172 | * Returns 0 on success or -errno on error. |
| 173 | */ |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 174 | int (*query)(struct android_native_window_t* window, |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 175 | int what, int* value); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 176 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 177 | /* |
| 178 | * hook used to perform various operations on the surface. |
| 179 | * (*perform)() is a generic mechanism to add functionality to |
| 180 | * android_native_window_t while keeping backward binary compatibility. |
| 181 | * |
| 182 | * This hook should not be called directly, instead use the helper functions |
| 183 | * defined below. |
| 184 | * |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame] | 185 | * (*perform)() returns -ENOENT if the 'what' parameter is not supported |
| 186 | * by the surface's implementation. |
| 187 | * |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 188 | * The valid operations are: |
| 189 | * NATIVE_WINDOW_SET_USAGE |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame] | 190 | * NATIVE_WINDOW_CONNECT |
| 191 | * NATIVE_WINDOW_DISCONNECT |
Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 192 | * NATIVE_WINDOW_SET_CROP |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 193 | * |
| 194 | */ |
| 195 | |
Mathias Agopian | 37b2a37 | 2009-08-17 12:33:20 -0700 | [diff] [blame] | 196 | int (*perform)(struct android_native_window_t* window, |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 197 | int operation, ... ); |
| 198 | |
| 199 | void* reserved_proc[3]; |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 200 | } android_native_window_t; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 201 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * native_window_set_usage() sets the intended usage flags for the next |
| 205 | * buffers acquired with (*lockBuffer)() and on. |
| 206 | * By default (if this function is never called), a usage of |
| 207 | * GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE |
| 208 | * is assumed. |
| 209 | * Calling this function will usually cause following buffers to be |
| 210 | * reallocated. |
| 211 | */ |
| 212 | |
Dima Zavin | fae3e73 | 2009-08-13 16:50:54 -0700 | [diff] [blame] | 213 | static inline int native_window_set_usage( |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 214 | android_native_window_t* window, int usage) |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 215 | { |
| 216 | return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage); |
| 217 | } |
| 218 | |
Mathias Agopian | e156e64 | 2010-03-11 15:05:52 -0800 | [diff] [blame] | 219 | /* |
| 220 | * native_window_connect(..., NATIVE_WINDOW_API_EGL) must be called |
| 221 | * by EGL when the window is made current. |
| 222 | * Returns -EINVAL if for some reason the window cannot be connected, which |
| 223 | * can happen if it's connected to some other API. |
| 224 | */ |
| 225 | static inline int native_window_connect( |
| 226 | android_native_window_t* window, int api) |
| 227 | { |
| 228 | return window->perform(window, NATIVE_WINDOW_CONNECT, api); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * native_window_disconnect(..., NATIVE_WINDOW_API_EGL) must be called |
| 233 | * by EGL when the window is made not current. |
| 234 | * An error is returned if for instance the window wasn't connected in the |
| 235 | * first place. |
| 236 | */ |
| 237 | static inline int native_window_disconnect( |
| 238 | android_native_window_t* window, int api) |
| 239 | { |
| 240 | return window->perform(window, NATIVE_WINDOW_DISCONNECT, api); |
| 241 | } |
| 242 | |
Mathias Agopian | cc08e68 | 2010-04-15 18:48:26 -0700 | [diff] [blame] | 243 | /* |
| 244 | * native_window_set_crop(..., crop) sets which region of the next queued |
| 245 | * buffers needs to be considered. |
| 246 | * A buffer's crop region is scaled to match the surface's size. |
| 247 | * |
| 248 | * The specified crop region applies to all buffers queued after it is called. |
| 249 | * |
| 250 | * if 'crop' is NULL, subsequently queued buffers won't be cropped. |
| 251 | * |
| 252 | * An error is returned if for instance the crop region is invalid, |
| 253 | * out of the buffer's bound or if the window is invalid. |
| 254 | */ |
| 255 | static inline int native_window_set_crop( |
| 256 | android_native_window_t* window, |
| 257 | android_native_rect_t const * crop) |
| 258 | { |
| 259 | return window->perform(window, NATIVE_WINDOW_SET_CROP, crop); |
| 260 | } |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 261 | |
Mathias Agopian | aa8c0ff | 2009-05-05 18:29:35 -0700 | [diff] [blame] | 262 | // --------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 263 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 264 | /* FIXME: this is legacy for pixmaps */ |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 265 | typedef struct egl_native_pixmap_t |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 266 | { |
| 267 | int32_t version; /* must be 32 */ |
| 268 | int32_t width; |
| 269 | int32_t height; |
| 270 | int32_t stride; |
| 271 | uint8_t* data; |
| 272 | uint8_t format; |
| 273 | uint8_t rfu[3]; |
| 274 | union { |
| 275 | uint32_t compressedFormat; |
| 276 | int32_t vstride; |
| 277 | }; |
| 278 | int32_t reserved; |
Mathias Agopian | 238a66e | 2009-08-13 17:57:53 -0700 | [diff] [blame] | 279 | } egl_native_pixmap_t; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 280 | |
| 281 | /*****************************************************************************/ |
| 282 | |
| 283 | #ifdef __cplusplus |
| 284 | } |
| 285 | #endif |
| 286 | |
| 287 | |
| 288 | /*****************************************************************************/ |
| 289 | |
| 290 | #ifdef __cplusplus |
| 291 | |
| 292 | #include <utils/RefBase.h> |
| 293 | |
| 294 | namespace android { |
| 295 | |
| 296 | /* |
| 297 | * This helper class turns an EGL android_native_xxx type into a C++ |
| 298 | * reference-counted object; with proper type conversions. |
| 299 | */ |
| 300 | template <typename NATIVE_TYPE, typename TYPE, typename REF> |
| 301 | class EGLNativeBase : public NATIVE_TYPE, public REF |
| 302 | { |
Jamie Gennis | 5e67f87 | 2010-05-10 17:33:32 -0700 | [diff] [blame^] | 303 | public: |
| 304 | // Disambiguate between the incStrong in REF and NATIVE_TYPE |
| 305 | void incStrong(const void* id) const { |
| 306 | REF::incStrong(id); |
| 307 | } |
| 308 | void decStrong(const void* id) const { |
| 309 | REF::decStrong(id); |
| 310 | } |
| 311 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 312 | protected: |
| 313 | typedef EGLNativeBase<NATIVE_TYPE, TYPE, REF> BASE; |
| 314 | EGLNativeBase() : NATIVE_TYPE(), REF() { |
| 315 | NATIVE_TYPE::common.incRef = incRef; |
| 316 | NATIVE_TYPE::common.decRef = decRef; |
| 317 | } |
| 318 | static inline TYPE* getSelf(NATIVE_TYPE* self) { |
| 319 | return static_cast<TYPE*>(self); |
| 320 | } |
| 321 | static inline TYPE const* getSelf(NATIVE_TYPE const* self) { |
| 322 | return static_cast<TYPE const *>(self); |
| 323 | } |
| 324 | static inline TYPE* getSelf(android_native_base_t* base) { |
| 325 | return getSelf(reinterpret_cast<NATIVE_TYPE*>(base)); |
| 326 | } |
| 327 | static inline TYPE const * getSelf(android_native_base_t const* base) { |
| 328 | return getSelf(reinterpret_cast<NATIVE_TYPE const*>(base)); |
| 329 | } |
| 330 | static void incRef(android_native_base_t* base) { |
| 331 | EGLNativeBase* self = getSelf(base); |
| 332 | self->incStrong(self); |
| 333 | } |
| 334 | static void decRef(android_native_base_t* base) { |
| 335 | EGLNativeBase* self = getSelf(base); |
| 336 | self->decStrong(self); |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | } // namespace android |
| 341 | #endif // __cplusplus |
| 342 | |
| 343 | /*****************************************************************************/ |
| 344 | |
| 345 | #endif /* ANDROID_ANDROID_NATIVES_H */ |