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 44 // --------------------------------------------------------------------------- 45 46 typedef struct android_native_base_t 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 */ 57 void (*incRef)(struct android_native_base_t* base); 58 void (*decRef)(struct android_native_base_t* base); 59 } android_native_base_t; 60 61 // --------------------------------------------------------------------------- 62 63 /* attributes queriable with query() */ 64 enum { 65 NATIVE_WINDOW_WIDTH = 0, 66 NATIVE_WINDOW_HEIGHT = 1, 67 NATIVE_WINDOW_FORMAT = 2, 68 }; 69 70 /* valid operations for the (*perform)() hook */ 71 enum { 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 80 }; 81 82 typedef struct android_native_window_t 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 112 113 /* 114 * Set the swap interval for this surface. 115 * 116 * Returns 0 on success or -errno on error. 117 */ 118 int (*setSwapInterval)(struct android_native_window_t* window, 119 int interval); 120 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. 124 * this call may block if no buffers are available. 125 * 126 * Returns 0 on success or -errno on error. 127 */ 128 int (*dequeueBuffer)(struct android_native_window_t* window, 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 */ 138 int (*lockBuffer)(struct android_native_window_t* window, 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 */ 148 int (*queueBuffer)(struct android_native_window_t* window, 149 struct android_native_buffer_t* buffer); 150 151 /* 152 * hook used to retrieve information about the native window. 153 * 154 * Returns 0 on success or -errno on error. 155 */ 156 int (*query)(struct android_native_window_t* window, 157 int what, int* value); 158 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 * 167 * (*perform)() returns -ENOENT if the 'what' parameter is not supported 168 * by the surface's implementation. 169 * 170 * The valid operations are: 171 * NATIVE_WINDOW_SET_USAGE 172 * NATIVE_WINDOW_CONNECT 173 * NATIVE_WINDOW_DISCONNECT 174 * 175 */ 176 177 int (*perform)(struct android_native_window_t* window, 178 int operation, ... ); 179 180 void* reserved_proc[3]; 181 } android_native_window_t; 182 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 194 static inline int native_window_set_usage( 195 android_native_window_t* window, int usage) 196 { 197 return window->perform(window, NATIVE_WINDOW_SET_USAGE, usage); 198 } 199 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 224 225 // --------------------------------------------------------------------------- 226 227 /* FIXME: this is legacy for pixmaps */ 228 typedef struct egl_native_pixmap_t 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; 242 } egl_native_pixmap_t; 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 */ 300