David Li | af94ceb | 2011-03-01 16:54:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2007 The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License Version 2.0(the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/mman.h> |
| 27 | |
| 28 | #include <cutils/atomic.h> |
| 29 | |
| 30 | |
| 31 | #include <private/ui/android_natives_priv.h> |
| 32 | |
| 33 | #include <hardware/copybit.h> |
| 34 | |
| 35 | #include "gles2context.h" |
| 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | namespace android |
| 39 | { |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | const unsigned int NUM_DISPLAYS = 1; |
| 43 | |
| 44 | static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
| 45 | static pthread_mutex_t gErrorKeyMutex = PTHREAD_MUTEX_INITIALIZER; |
| 46 | static pthread_key_t gEGLErrorKey = -1; |
| 47 | #ifndef HAVE_ANDROID_OS |
| 48 | namespace gl { |
| 49 | pthread_key_t gGLKey = -1; |
| 50 | }; // namespace gl |
| 51 | #endif |
| 52 | |
| 53 | template<typename T> |
| 54 | static T setError(GLint error, T returnValue) |
| 55 | { |
| 56 | if (ggl_unlikely(gEGLErrorKey == -1)) { |
| 57 | pthread_mutex_lock(&gErrorKeyMutex); |
| 58 | if (gEGLErrorKey == -1) |
| 59 | pthread_key_create(&gEGLErrorKey, NULL); |
| 60 | pthread_mutex_unlock(&gErrorKeyMutex); |
| 61 | } |
| 62 | pthread_setspecific(gEGLErrorKey, (void*)error); |
| 63 | return returnValue; |
| 64 | } |
| 65 | |
| 66 | static GLint getError() |
| 67 | { |
| 68 | if (ggl_unlikely(gEGLErrorKey == -1)) |
| 69 | return EGL_SUCCESS; |
| 70 | GLint error = (GLint)pthread_getspecific(gEGLErrorKey); |
| 71 | if (error == 0) { |
| 72 | // The TLS key has been created by another thread, but the value for |
| 73 | // this thread has not been initialized. |
| 74 | return EGL_SUCCESS; |
| 75 | } |
| 76 | pthread_setspecific(gEGLErrorKey, (void*)EGL_SUCCESS); |
| 77 | return error; |
| 78 | } |
| 79 | |
| 80 | // ---------------------------------------------------------------------------- |
| 81 | |
| 82 | struct egl_display_t { |
| 83 | egl_display_t() : type(0), initialized(0) { } |
| 84 | |
| 85 | static egl_display_t& get_display(EGLDisplay dpy); |
| 86 | |
| 87 | static EGLBoolean is_valid(EGLDisplay dpy) { |
| 88 | return ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) ? EGL_FALSE : EGL_TRUE; |
| 89 | } |
| 90 | |
| 91 | NativeDisplayType type; |
| 92 | volatile int32_t initialized; |
| 93 | }; |
| 94 | |
| 95 | static egl_display_t gDisplays[NUM_DISPLAYS]; |
| 96 | |
| 97 | egl_display_t& egl_display_t::get_display(EGLDisplay dpy) |
| 98 | { |
| 99 | return gDisplays[uintptr_t(dpy)-1U]; |
| 100 | } |
| 101 | |
| 102 | // ---------------------------------------------------------------------------- |
| 103 | |
| 104 | struct egl_surface_t { |
| 105 | enum { |
| 106 | PAGE_FLIP = 0x00000001, |
| 107 | MAGIC = 0x31415265 |
| 108 | }; |
| 109 | |
| 110 | uint32_t magic; |
| 111 | EGLDisplay dpy; |
| 112 | EGLConfig config; |
| 113 | EGLContext ctx; |
| 114 | |
| 115 | egl_surface_t(EGLDisplay dpy, EGLConfig config, int32_t depthFormat); |
| 116 | virtual ~egl_surface_t(); |
| 117 | bool isValid() const; |
| 118 | virtual bool initCheck() const = 0; |
| 119 | |
| 120 | virtual EGLBoolean bindDrawSurface(GLES2Context* gl) = 0; |
| 121 | virtual EGLBoolean bindReadSurface(GLES2Context* gl) = 0; |
| 122 | virtual EGLBoolean connect() { |
| 123 | return EGL_TRUE; |
| 124 | } |
| 125 | virtual void disconnect() {} |
| 126 | virtual EGLint getWidth() const = 0; |
| 127 | virtual EGLint getHeight() const = 0; |
| 128 | |
| 129 | virtual EGLint getHorizontalResolution() const; |
| 130 | virtual EGLint getVerticalResolution() const; |
| 131 | virtual EGLint getRefreshRate() const; |
| 132 | virtual EGLint getSwapBehavior() const; |
| 133 | virtual EGLBoolean swapBuffers(); |
| 134 | virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h); |
| 135 | protected: |
| 136 | GGLSurface depth; |
| 137 | }; |
| 138 | |
| 139 | egl_surface_t::egl_surface_t(EGLDisplay dpy, |
| 140 | EGLConfig config, |
| 141 | int32_t depthFormat) |
| 142 | : magic(MAGIC), dpy(dpy), config(config), ctx(0) |
| 143 | { |
| 144 | depth.version = sizeof(GGLSurface); |
| 145 | depth.data = 0; |
| 146 | depth.format = (GGLPixelFormat)depthFormat; |
| 147 | } |
| 148 | egl_surface_t::~egl_surface_t() |
| 149 | { |
| 150 | magic = 0; |
| 151 | free(depth.data); |
| 152 | } |
| 153 | bool egl_surface_t::isValid() const |
| 154 | { |
| 155 | LOGE_IF(magic != MAGIC, "invalid EGLSurface (%p)", this); |
| 156 | return magic == MAGIC; |
| 157 | } |
| 158 | |
| 159 | EGLBoolean egl_surface_t::swapBuffers() |
| 160 | { |
| 161 | return EGL_FALSE; |
| 162 | } |
| 163 | EGLint egl_surface_t::getHorizontalResolution() const |
| 164 | { |
| 165 | return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f); |
| 166 | } |
| 167 | EGLint egl_surface_t::getVerticalResolution() const |
| 168 | { |
| 169 | return (0 * EGL_DISPLAY_SCALING) * (1.0f / 25.4f); |
| 170 | } |
| 171 | EGLint egl_surface_t::getRefreshRate() const |
| 172 | { |
| 173 | return (60 * EGL_DISPLAY_SCALING); |
| 174 | } |
| 175 | EGLint egl_surface_t::getSwapBehavior() const |
| 176 | { |
| 177 | return EGL_BUFFER_PRESERVED; |
| 178 | } |
| 179 | EGLBoolean egl_surface_t::setSwapRectangle( |
| 180 | EGLint l, EGLint t, EGLint w, EGLint h) |
| 181 | { |
| 182 | return EGL_FALSE; |
| 183 | } |
| 184 | |
| 185 | // ---------------------------------------------------------------------------- |
| 186 | |
| 187 | struct egl_window_surface_v2_t : public egl_surface_t { |
| 188 | egl_window_surface_v2_t( |
| 189 | EGLDisplay dpy, EGLConfig config, |
| 190 | int32_t depthFormat, |
| 191 | ANativeWindow* window); |
| 192 | |
| 193 | ~egl_window_surface_v2_t(); |
| 194 | |
| 195 | virtual bool initCheck() const { |
| 196 | return true; // TODO: report failure if ctor fails |
| 197 | } |
| 198 | virtual EGLBoolean swapBuffers(); |
| 199 | virtual EGLBoolean bindDrawSurface(GLES2Context* gl); |
| 200 | virtual EGLBoolean bindReadSurface(GLES2Context* gl); |
| 201 | virtual EGLBoolean connect(); |
| 202 | virtual void disconnect(); |
| 203 | virtual EGLint getWidth() const { |
| 204 | return width; |
| 205 | } |
| 206 | virtual EGLint getHeight() const { |
| 207 | return height; |
| 208 | } |
| 209 | virtual EGLint getHorizontalResolution() const; |
| 210 | virtual EGLint getVerticalResolution() const; |
| 211 | virtual EGLint getRefreshRate() const; |
| 212 | virtual EGLint getSwapBehavior() const; |
| 213 | virtual EGLBoolean setSwapRectangle(EGLint l, EGLint t, EGLint w, EGLint h); |
| 214 | |
| 215 | private: |
| 216 | status_t lock(android_native_buffer_t* buf, int usage, void** vaddr); |
| 217 | status_t unlock(android_native_buffer_t* buf); |
| 218 | ANativeWindow* nativeWindow; |
| 219 | android_native_buffer_t* buffer; |
| 220 | android_native_buffer_t* previousBuffer; |
| 221 | gralloc_module_t const* module; |
| 222 | copybit_device_t* blitengine; |
| 223 | int width; |
| 224 | int height; |
| 225 | void* bits; |
| 226 | GGLFormat const* pixelFormatTable; |
| 227 | |
| 228 | struct Rect { |
| 229 | inline Rect() { }; |
| 230 | inline Rect(int32_t w, int32_t h) |
| 231 | : left(0), top(0), right(w), bottom(h) { } |
| 232 | inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) |
| 233 | : left(l), top(t), right(r), bottom(b) { } |
| 234 | Rect& andSelf(const Rect& r) { |
| 235 | left = max(left, r.left); |
| 236 | top = max(top, r.top); |
| 237 | right = min(right, r.right); |
| 238 | bottom = min(bottom, r.bottom); |
| 239 | return *this; |
| 240 | } |
| 241 | bool isEmpty() const { |
| 242 | return (left>=right || top>=bottom); |
| 243 | } |
| 244 | void dump(char const* what) { |
| 245 | LOGD("%s { %5d, %5d, w=%5d, h=%5d }", |
| 246 | what, left, top, right-left, bottom-top); |
| 247 | } |
| 248 | |
| 249 | int32_t left; |
| 250 | int32_t top; |
| 251 | int32_t right; |
| 252 | int32_t bottom; |
| 253 | }; |
| 254 | |
| 255 | struct Region { |
| 256 | inline Region() : count(0) { } |
| 257 | typedef Rect const* const_iterator; |
| 258 | const_iterator begin() const { |
| 259 | return storage; |
| 260 | } |
| 261 | const_iterator end() const { |
| 262 | return storage+count; |
| 263 | } |
| 264 | static Region subtract(const Rect& lhs, const Rect& rhs) { |
| 265 | Region reg; |
| 266 | Rect* storage = reg.storage; |
| 267 | if (!lhs.isEmpty()) { |
| 268 | if (lhs.top < rhs.top) { // top rect |
| 269 | storage->left = lhs.left; |
| 270 | storage->top = lhs.top; |
| 271 | storage->right = lhs.right; |
| 272 | storage->bottom = rhs.top; |
| 273 | storage++; |
| 274 | } |
| 275 | const int32_t top = max(lhs.top, rhs.top); |
| 276 | const int32_t bot = min(lhs.bottom, rhs.bottom); |
| 277 | if (top < bot) { |
| 278 | if (lhs.left < rhs.left) { // left-side rect |
| 279 | storage->left = lhs.left; |
| 280 | storage->top = top; |
| 281 | storage->right = rhs.left; |
| 282 | storage->bottom = bot; |
| 283 | storage++; |
| 284 | } |
| 285 | if (lhs.right > rhs.right) { // right-side rect |
| 286 | storage->left = rhs.right; |
| 287 | storage->top = top; |
| 288 | storage->right = lhs.right; |
| 289 | storage->bottom = bot; |
| 290 | storage++; |
| 291 | } |
| 292 | } |
| 293 | if (lhs.bottom > rhs.bottom) { // bottom rect |
| 294 | storage->left = lhs.left; |
| 295 | storage->top = rhs.bottom; |
| 296 | storage->right = lhs.right; |
| 297 | storage->bottom = lhs.bottom; |
| 298 | storage++; |
| 299 | } |
| 300 | reg.count = storage - reg.storage; |
| 301 | } |
| 302 | return reg; |
| 303 | } |
| 304 | bool isEmpty() const { |
| 305 | return count<=0; |
| 306 | } |
| 307 | private: |
| 308 | Rect storage[4]; |
| 309 | ssize_t count; |
| 310 | }; |
| 311 | |
| 312 | struct region_iterator : public copybit_region_t { |
| 313 | region_iterator(const Region& region) |
| 314 | : b(region.begin()), e(region.end()) { |
| 315 | this->next = iterate; |
| 316 | } |
| 317 | private: |
| 318 | static int iterate(copybit_region_t const * self, copybit_rect_t* rect) { |
| 319 | region_iterator const* me = static_cast<region_iterator const*>(self); |
| 320 | if (me->b != me->e) { |
| 321 | *reinterpret_cast<Rect*>(rect) = *me->b++; |
| 322 | return 1; |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | mutable Region::const_iterator b; |
| 327 | Region::const_iterator const e; |
| 328 | }; |
| 329 | |
| 330 | void copyBlt( |
| 331 | android_native_buffer_t* dst, void* dst_vaddr, |
| 332 | android_native_buffer_t* src, void const* src_vaddr, |
| 333 | const Region& clip); |
| 334 | |
| 335 | Rect dirtyRegion; |
| 336 | Rect oldDirtyRegion; |
| 337 | }; |
| 338 | |
| 339 | egl_window_surface_v2_t::egl_window_surface_v2_t(EGLDisplay dpy, |
| 340 | EGLConfig config, |
| 341 | int32_t depthFormat, |
| 342 | ANativeWindow* window) |
| 343 | : egl_surface_t(dpy, config, depthFormat), |
| 344 | nativeWindow(window), buffer(0), previousBuffer(0), module(0), |
| 345 | blitengine(0), bits(NULL) |
| 346 | { |
| 347 | hw_module_t const* pModule; |
| 348 | hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule); |
| 349 | module = reinterpret_cast<gralloc_module_t const*>(pModule); |
| 350 | |
| 351 | if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &pModule) == 0) { |
| 352 | copybit_open(pModule, &blitengine); |
| 353 | } |
| 354 | |
| 355 | pixelFormatTable = gglGetPixelFormatTable(); |
| 356 | |
| 357 | // keep a reference on the window |
| 358 | nativeWindow->common.incRef(&nativeWindow->common); |
| 359 | nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &width); |
| 360 | nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &height); |
| 361 | int format = 0; |
| 362 | nativeWindow->query(nativeWindow, NATIVE_WINDOW_FORMAT, &format); |
| 363 | LOGD("agl2: egl_window_surface_v2_t format=0x%.4X", format); |
| 364 | // assert(0); |
| 365 | } |
| 366 | |
| 367 | egl_window_surface_v2_t::~egl_window_surface_v2_t() |
| 368 | { |
| 369 | if (buffer) { |
| 370 | buffer->common.decRef(&buffer->common); |
| 371 | } |
| 372 | if (previousBuffer) { |
| 373 | previousBuffer->common.decRef(&previousBuffer->common); |
| 374 | } |
| 375 | nativeWindow->common.decRef(&nativeWindow->common); |
| 376 | if (blitengine) { |
| 377 | copybit_close(blitengine); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | EGLBoolean egl_window_surface_v2_t::connect() |
| 382 | { |
| 383 | // we're intending to do software rendering |
| 384 | native_window_set_usage(nativeWindow, |
| 385 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 386 | |
| 387 | // dequeue a buffer |
| 388 | if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) != NO_ERROR) { |
| 389 | return setError(EGL_BAD_ALLOC, EGL_FALSE); |
| 390 | } |
| 391 | |
| 392 | // allocate a corresponding depth-buffer |
| 393 | width = buffer->width; |
| 394 | height = buffer->height; |
| 395 | if (depth.format) { |
| 396 | depth.width = width; |
| 397 | depth.height = height; |
| 398 | depth.stride = depth.width; // use the width here |
| 399 | assert(GGL_PIXEL_FORMAT_Z_32 == depth.format); |
| 400 | depth.data = (GGLubyte*)malloc(depth.stride*depth.height*4); |
| 401 | if (depth.data == 0) { |
| 402 | return setError(EGL_BAD_ALLOC, EGL_FALSE); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // keep a reference on the buffer |
| 407 | buffer->common.incRef(&buffer->common); |
| 408 | |
| 409 | // Lock the buffer |
| 410 | nativeWindow->lockBuffer(nativeWindow, buffer); |
| 411 | // pin the buffer down |
| 412 | if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN | |
| 413 | GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) { |
| 414 | LOGE("connect() failed to lock buffer %p (%ux%u)", |
| 415 | buffer, buffer->width, buffer->height); |
| 416 | return setError(EGL_BAD_ACCESS, EGL_FALSE); |
| 417 | // FIXME: we should make sure we're not accessing the buffer anymore |
| 418 | } |
| 419 | return EGL_TRUE; |
| 420 | } |
| 421 | |
| 422 | void egl_window_surface_v2_t::disconnect() |
| 423 | { |
| 424 | if (buffer && bits) { |
| 425 | bits = NULL; |
| 426 | unlock(buffer); |
| 427 | } |
| 428 | // enqueue the last frame |
| 429 | if (buffer) |
| 430 | nativeWindow->queueBuffer(nativeWindow, buffer); |
| 431 | if (buffer) { |
| 432 | buffer->common.decRef(&buffer->common); |
| 433 | buffer = 0; |
| 434 | } |
| 435 | if (previousBuffer) { |
| 436 | previousBuffer->common.decRef(&previousBuffer->common); |
| 437 | previousBuffer = 0; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | status_t egl_window_surface_v2_t::lock( |
| 442 | android_native_buffer_t* buf, int usage, void** vaddr) |
| 443 | { |
| 444 | int err; |
| 445 | |
| 446 | err = module->lock(module, buf->handle, |
| 447 | usage, 0, 0, buf->width, buf->height, vaddr); |
| 448 | |
| 449 | return err; |
| 450 | } |
| 451 | |
| 452 | status_t egl_window_surface_v2_t::unlock(android_native_buffer_t* buf) |
| 453 | { |
| 454 | if (!buf) return BAD_VALUE; |
| 455 | int err = NO_ERROR; |
| 456 | |
| 457 | err = module->unlock(module, buf->handle); |
| 458 | |
| 459 | return err; |
| 460 | } |
| 461 | |
| 462 | void egl_window_surface_v2_t::copyBlt( |
| 463 | android_native_buffer_t* dst, void* dst_vaddr, |
| 464 | android_native_buffer_t* src, void const* src_vaddr, |
| 465 | const Region& clip) |
| 466 | { |
| 467 | // FIXME: use copybit if possible |
| 468 | // NOTE: dst and src must be the same format |
| 469 | |
| 470 | status_t err = NO_ERROR; |
| 471 | copybit_device_t* const copybit = blitengine; |
| 472 | if (copybit) { |
| 473 | copybit_image_t simg; |
| 474 | simg.w = src->stride; |
| 475 | simg.h = src->height; |
| 476 | simg.format = src->format; |
| 477 | simg.handle = const_cast<native_handle_t*>(src->handle); |
| 478 | |
| 479 | copybit_image_t dimg; |
| 480 | dimg.w = dst->stride; |
| 481 | dimg.h = dst->height; |
| 482 | dimg.format = dst->format; |
| 483 | dimg.handle = const_cast<native_handle_t*>(dst->handle); |
| 484 | |
| 485 | copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0); |
| 486 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, 255); |
| 487 | copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_DISABLE); |
| 488 | region_iterator it(clip); |
| 489 | err = copybit->blit(copybit, &dimg, &simg, &it); |
| 490 | if (err != NO_ERROR) { |
| 491 | LOGE("copybit failed (%s)", strerror(err)); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (!copybit || err) { |
| 496 | Region::const_iterator cur = clip.begin(); |
| 497 | Region::const_iterator end = clip.end(); |
| 498 | |
| 499 | const size_t bpp = pixelFormatTable[src->format].size; |
| 500 | const size_t dbpr = dst->stride * bpp; |
| 501 | const size_t sbpr = src->stride * bpp; |
| 502 | |
| 503 | uint8_t const * const src_bits = (uint8_t const *)src_vaddr; |
| 504 | uint8_t * const dst_bits = (uint8_t *)dst_vaddr; |
| 505 | |
| 506 | while (cur != end) { |
| 507 | const Rect& r(*cur++); |
| 508 | ssize_t w = r.right - r.left; |
| 509 | ssize_t h = r.bottom - r.top; |
| 510 | if (w <= 0 || h<=0) continue; |
| 511 | size_t size = w * bpp; |
| 512 | uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp; |
| 513 | uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp; |
| 514 | if (dbpr==sbpr && size==sbpr) { |
| 515 | size *= h; |
| 516 | h = 1; |
| 517 | } |
| 518 | do { |
| 519 | memcpy(d, s, size); |
| 520 | d += dbpr; |
| 521 | s += sbpr; |
| 522 | } while (--h > 0); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | EGLBoolean egl_window_surface_v2_t::swapBuffers() |
| 528 | { |
| 529 | if (!buffer) { |
| 530 | return setError(EGL_BAD_ACCESS, EGL_FALSE); |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Handle eglSetSwapRectangleANDROID() |
| 535 | * We copyback from the front buffer |
| 536 | */ |
| 537 | if (!dirtyRegion.isEmpty()) { |
| 538 | dirtyRegion.andSelf(Rect(buffer->width, buffer->height)); |
| 539 | if (previousBuffer) { |
| 540 | // This was const Region copyBack, but that causes an |
| 541 | // internal compile error on simulator builds |
| 542 | /*const*/ |
| 543 | Region copyBack(Region::subtract(oldDirtyRegion, dirtyRegion)); |
| 544 | if (!copyBack.isEmpty()) { |
| 545 | void* prevBits; |
| 546 | if (lock(previousBuffer, |
| 547 | GRALLOC_USAGE_SW_READ_OFTEN, &prevBits) == NO_ERROR) { |
| 548 | // copy from previousBuffer to buffer |
| 549 | copyBlt(buffer, bits, previousBuffer, prevBits, copyBack); |
| 550 | unlock(previousBuffer); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | oldDirtyRegion = dirtyRegion; |
| 555 | } |
| 556 | |
| 557 | if (previousBuffer) { |
| 558 | previousBuffer->common.decRef(&previousBuffer->common); |
| 559 | previousBuffer = 0; |
| 560 | } |
| 561 | |
| 562 | unlock(buffer); |
| 563 | previousBuffer = buffer; |
| 564 | nativeWindow->queueBuffer(nativeWindow, buffer); |
| 565 | buffer = 0; |
| 566 | |
| 567 | // dequeue a new buffer |
| 568 | if (nativeWindow->dequeueBuffer(nativeWindow, &buffer) == NO_ERROR) { |
| 569 | |
| 570 | // TODO: lockBuffer should rather be executed when the very first |
| 571 | // direct rendering occurs. |
| 572 | nativeWindow->lockBuffer(nativeWindow, buffer); |
| 573 | |
| 574 | // reallocate the depth-buffer if needed |
| 575 | if ((width != buffer->width) || (height != buffer->height)) { |
| 576 | // TODO: we probably should reset the swap rect here |
| 577 | // if the window size has changed |
| 578 | width = buffer->width; |
| 579 | height = buffer->height; |
| 580 | if (depth.data) { |
| 581 | free(depth.data); |
| 582 | depth.width = width; |
| 583 | depth.height = height; |
| 584 | depth.stride = buffer->stride; |
| 585 | depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2); |
| 586 | if (depth.data == 0) { |
| 587 | setError(EGL_BAD_ALLOC, EGL_FALSE); |
| 588 | return EGL_FALSE; |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // keep a reference on the buffer |
| 594 | buffer->common.incRef(&buffer->common); |
| 595 | |
| 596 | // finally pin the buffer down |
| 597 | if (lock(buffer, GRALLOC_USAGE_SW_READ_OFTEN | |
| 598 | GRALLOC_USAGE_SW_WRITE_OFTEN, &bits) != NO_ERROR) { |
| 599 | LOGE("eglSwapBuffers() failed to lock buffer %p (%ux%u)", |
| 600 | buffer, buffer->width, buffer->height); |
| 601 | return setError(EGL_BAD_ACCESS, EGL_FALSE); |
| 602 | // FIXME: we should make sure we're not accessing the buffer anymore |
| 603 | } |
| 604 | } else { |
| 605 | return setError(EGL_BAD_CURRENT_SURFACE, EGL_FALSE); |
| 606 | } |
| 607 | |
| 608 | return EGL_TRUE; |
| 609 | } |
| 610 | |
| 611 | EGLBoolean egl_window_surface_v2_t::setSwapRectangle( |
| 612 | EGLint l, EGLint t, EGLint w, EGLint h) |
| 613 | { |
| 614 | dirtyRegion = Rect(l, t, l+w, t+h); |
| 615 | return EGL_TRUE; |
| 616 | } |
| 617 | |
| 618 | EGLBoolean egl_window_surface_v2_t::bindDrawSurface(GLES2Context* gl) |
| 619 | { |
| 620 | GGLSurface buffer; |
| 621 | buffer.version = sizeof(GGLSurface); |
| 622 | buffer.width = this->buffer->width; |
| 623 | buffer.height = this->buffer->height; |
| 624 | buffer.stride = this->buffer->stride; |
| 625 | buffer.data = (GGLubyte*)bits; |
| 626 | buffer.format = (GGLPixelFormat)this->buffer->format; |
| 627 | gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &buffer); |
| 628 | if (depth.data != gl->rasterizer.depthSurface.data) |
| 629 | gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth); |
| 630 | |
| 631 | return EGL_TRUE; |
| 632 | } |
| 633 | EGLBoolean egl_window_surface_v2_t::bindReadSurface(GLES2Context* gl) |
| 634 | { |
| 635 | GGLSurface buffer; |
| 636 | buffer.version = sizeof(GGLSurface); |
| 637 | buffer.width = this->buffer->width; |
| 638 | buffer.height = this->buffer->height; |
| 639 | buffer.stride = this->buffer->stride; |
| 640 | buffer.data = (GGLubyte*)bits; // FIXME: hopefully is is LOCKED!!! |
| 641 | buffer.format = (GGLPixelFormat)this->buffer->format; |
| 642 | puts("agl2: readBuffer not implemented"); |
| 643 | //gl->rasterizer.interface.readBuffer(gl, &buffer); |
| 644 | return EGL_TRUE; |
| 645 | } |
| 646 | EGLint egl_window_surface_v2_t::getHorizontalResolution() const |
| 647 | { |
| 648 | return (nativeWindow->xdpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f); |
| 649 | } |
| 650 | EGLint egl_window_surface_v2_t::getVerticalResolution() const |
| 651 | { |
| 652 | return (nativeWindow->ydpi * EGL_DISPLAY_SCALING) * (1.0f / 25.4f); |
| 653 | } |
| 654 | EGLint egl_window_surface_v2_t::getRefreshRate() const |
| 655 | { |
| 656 | return (60 * EGL_DISPLAY_SCALING); // FIXME |
| 657 | } |
| 658 | EGLint egl_window_surface_v2_t::getSwapBehavior() const |
| 659 | { |
| 660 | /* |
| 661 | * EGL_BUFFER_PRESERVED means that eglSwapBuffers() completely preserves |
| 662 | * the content of the swapped buffer. |
| 663 | * |
| 664 | * EGL_BUFFER_DESTROYED means that the content of the buffer is lost. |
| 665 | * |
| 666 | * However when ANDROID_swap_retcangle is supported, EGL_BUFFER_DESTROYED |
| 667 | * only applies to the area specified by eglSetSwapRectangleANDROID(), that |
| 668 | * is, everything outside of this area is preserved. |
| 669 | * |
| 670 | * This implementation of EGL assumes the later case. |
| 671 | * |
| 672 | */ |
| 673 | |
| 674 | return EGL_BUFFER_DESTROYED; |
| 675 | } |
| 676 | |
| 677 | // ---------------------------------------------------------------------------- |
| 678 | |
| 679 | struct egl_pixmap_surface_t : public egl_surface_t { |
| 680 | egl_pixmap_surface_t( |
| 681 | EGLDisplay dpy, EGLConfig config, |
| 682 | int32_t depthFormat, |
| 683 | egl_native_pixmap_t const * pixmap); |
| 684 | |
| 685 | virtual ~egl_pixmap_surface_t() { } |
| 686 | |
| 687 | virtual bool initCheck() const { |
| 688 | return !depth.format || depth.data!=0; |
| 689 | } |
| 690 | virtual EGLBoolean bindDrawSurface(GLES2Context* gl); |
| 691 | virtual EGLBoolean bindReadSurface(GLES2Context* gl); |
| 692 | virtual EGLint getWidth() const { |
| 693 | return nativePixmap.width; |
| 694 | } |
| 695 | virtual EGLint getHeight() const { |
| 696 | return nativePixmap.height; |
| 697 | } |
| 698 | private: |
| 699 | egl_native_pixmap_t nativePixmap; |
| 700 | }; |
| 701 | |
| 702 | egl_pixmap_surface_t::egl_pixmap_surface_t(EGLDisplay dpy, |
| 703 | EGLConfig config, |
| 704 | int32_t depthFormat, |
| 705 | egl_native_pixmap_t const * pixmap) |
| 706 | : egl_surface_t(dpy, config, depthFormat), nativePixmap(*pixmap) |
| 707 | { |
| 708 | if (depthFormat) { |
| 709 | depth.width = pixmap->width; |
| 710 | depth.height = pixmap->height; |
| 711 | depth.stride = depth.width; // use the width here |
| 712 | depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2); |
| 713 | if (depth.data == 0) { |
| 714 | setError(EGL_BAD_ALLOC, EGL_NO_SURFACE); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | EGLBoolean egl_pixmap_surface_t::bindDrawSurface(GLES2Context* gl) |
| 719 | { |
| 720 | GGLSurface buffer; |
| 721 | buffer.version = sizeof(GGLSurface); |
| 722 | buffer.width = nativePixmap.width; |
| 723 | buffer.height = nativePixmap.height; |
| 724 | buffer.stride = nativePixmap.stride; |
| 725 | buffer.data = nativePixmap.data; |
| 726 | buffer.format = (GGLPixelFormat)nativePixmap.format; |
| 727 | |
| 728 | gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &buffer); |
| 729 | if (depth.data != gl->rasterizer.depthSurface.data) |
| 730 | gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth); |
| 731 | return EGL_TRUE; |
| 732 | } |
| 733 | EGLBoolean egl_pixmap_surface_t::bindReadSurface(GLES2Context* gl) |
| 734 | { |
| 735 | GGLSurface buffer; |
| 736 | buffer.version = sizeof(GGLSurface); |
| 737 | buffer.width = nativePixmap.width; |
| 738 | buffer.height = nativePixmap.height; |
| 739 | buffer.stride = nativePixmap.stride; |
| 740 | buffer.data = nativePixmap.data; |
| 741 | buffer.format = (GGLPixelFormat)nativePixmap.format; |
| 742 | puts("agl2: readBuffer not implemented"); |
| 743 | //gl->rasterizer.interface.readBuffer(gl, &buffer); |
| 744 | return EGL_TRUE; |
| 745 | } |
| 746 | |
| 747 | // ---------------------------------------------------------------------------- |
| 748 | |
| 749 | struct egl_pbuffer_surface_t : public egl_surface_t { |
| 750 | egl_pbuffer_surface_t( |
| 751 | EGLDisplay dpy, EGLConfig config, int32_t depthFormat, |
| 752 | int32_t w, int32_t h, int32_t f); |
| 753 | |
| 754 | virtual ~egl_pbuffer_surface_t(); |
| 755 | |
| 756 | virtual bool initCheck() const { |
| 757 | return pbuffer.data != 0; |
| 758 | } |
| 759 | virtual EGLBoolean bindDrawSurface(GLES2Context* gl); |
| 760 | virtual EGLBoolean bindReadSurface(GLES2Context* gl); |
| 761 | virtual EGLint getWidth() const { |
| 762 | return pbuffer.width; |
| 763 | } |
| 764 | virtual EGLint getHeight() const { |
| 765 | return pbuffer.height; |
| 766 | } |
| 767 | private: |
| 768 | GGLSurface pbuffer; |
| 769 | }; |
| 770 | |
| 771 | egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy, |
| 772 | EGLConfig config, int32_t depthFormat, |
| 773 | int32_t w, int32_t h, int32_t f) |
| 774 | : egl_surface_t(dpy, config, depthFormat) |
| 775 | { |
| 776 | size_t size = w*h; |
| 777 | switch (f) { |
| 778 | case GGL_PIXEL_FORMAT_A_8: |
| 779 | size *= 1; |
| 780 | break; |
| 781 | case GGL_PIXEL_FORMAT_RGB_565: |
| 782 | size *= 2; |
| 783 | break; |
| 784 | case GGL_PIXEL_FORMAT_RGBA_8888: |
| 785 | size *= 4; |
| 786 | break; |
| 787 | case GGL_PIXEL_FORMAT_RGBX_8888: |
| 788 | size *= 4; |
| 789 | break; |
| 790 | default: |
| 791 | LOGE("incompatible pixel format for pbuffer (format=%d)", f); |
| 792 | pbuffer.data = 0; |
| 793 | break; |
| 794 | } |
| 795 | pbuffer.version = sizeof(GGLSurface); |
| 796 | pbuffer.width = w; |
| 797 | pbuffer.height = h; |
| 798 | pbuffer.stride = w; |
| 799 | pbuffer.data = (GGLubyte*)malloc(size); |
| 800 | pbuffer.format = (GGLPixelFormat)f; |
| 801 | |
| 802 | if (depthFormat) { |
| 803 | depth.width = pbuffer.width; |
| 804 | depth.height = pbuffer.height; |
| 805 | depth.stride = depth.width; // use the width here |
| 806 | depth.data = (GGLubyte*)malloc(depth.stride*depth.height*2); |
| 807 | if (depth.data == 0) { |
| 808 | setError(EGL_BAD_ALLOC, EGL_NO_SURFACE); |
| 809 | return; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | egl_pbuffer_surface_t::~egl_pbuffer_surface_t() |
| 814 | { |
| 815 | free(pbuffer.data); |
| 816 | } |
| 817 | EGLBoolean egl_pbuffer_surface_t::bindDrawSurface(GLES2Context* gl) |
| 818 | { |
| 819 | gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_COLOR_BUFFER_BIT, &pbuffer); |
| 820 | if (depth.data != gl->rasterizer.depthSurface.data) |
| 821 | gl->rasterizer.interface.SetBuffer(&gl->rasterizer.interface, GL_DEPTH_BUFFER_BIT, &depth); |
| 822 | return EGL_TRUE; |
| 823 | } |
| 824 | EGLBoolean egl_pbuffer_surface_t::bindReadSurface(GLES2Context* gl) |
| 825 | { |
| 826 | puts("agl2: readBuffer not implemented"); |
| 827 | //gl->rasterizer.interface.readBuffer(gl, &pbuffer); |
| 828 | return EGL_TRUE; |
| 829 | } |
| 830 | |
| 831 | // ---------------------------------------------------------------------------- |
| 832 | |
| 833 | struct config_pair_t { |
| 834 | GLint key; |
| 835 | GLint value; |
| 836 | }; |
| 837 | |
| 838 | struct configs_t { |
| 839 | const config_pair_t* array; |
| 840 | int size; |
| 841 | }; |
| 842 | |
| 843 | struct config_management_t { |
| 844 | GLint key; |
| 845 | bool (*match)(GLint reqValue, GLint confValue); |
| 846 | static bool atLeast(GLint reqValue, GLint confValue) { |
| 847 | return (reqValue == EGL_DONT_CARE) || (confValue >= reqValue); |
| 848 | } |
| 849 | static bool exact(GLint reqValue, GLint confValue) { |
| 850 | return (reqValue == EGL_DONT_CARE) || (confValue == reqValue); |
| 851 | } |
| 852 | static bool mask(GLint reqValue, GLint confValue) { |
| 853 | return (confValue & reqValue) == reqValue; |
| 854 | } |
| 855 | static bool ignore(GLint reqValue, GLint confValue) { |
| 856 | return true; |
| 857 | } |
| 858 | }; |
| 859 | |
| 860 | // ---------------------------------------------------------------------------- |
| 861 | |
| 862 | #define VERSION_MAJOR 1 |
| 863 | #define VERSION_MINOR 2 |
| 864 | static char const * const gVendorString = "Google Inc."; |
| 865 | static char const * const gVersionString = "0.0 Android Driver 0.0.0"; |
| 866 | static char const * const gClientApiString = "OpenGL ES2"; |
| 867 | static char const * const gExtensionsString = |
| 868 | //"EGL_KHR_image_base " |
| 869 | // "KHR_image_pixmap " |
| 870 | //"EGL_ANDROID_image_native_buffer " |
| 871 | //"EGL_ANDROID_swap_rectangle " |
| 872 | ""; |
| 873 | |
| 874 | // ---------------------------------------------------------------------------- |
| 875 | |
| 876 | struct extention_map_t { |
| 877 | const char * const name; |
| 878 | __eglMustCastToProperFunctionPointerType address; |
| 879 | }; |
| 880 | |
| 881 | static const extention_map_t gExtentionMap[] = { |
| 882 | // { "glDrawTexsOES", |
| 883 | // (__eglMustCastToProperFunctionPointerType)&glDrawTexsOES }, |
| 884 | // { "glDrawTexiOES", |
| 885 | // (__eglMustCastToProperFunctionPointerType)&glDrawTexiOES }, |
| 886 | // { "glDrawTexfOES", |
| 887 | // (__eglMustCastToProperFunctionPointerType)&glDrawTexfOES }, |
| 888 | // { "glDrawTexxOES", |
| 889 | // (__eglMustCastToProperFunctionPointerType)&glDrawTexxOES }, |
| 890 | // { "glDrawTexsvOES", |
| 891 | // (__eglMustCastToProperFunctionPointerType)&glDrawTexsvOES }, |
| 892 | // { "glDrawTexivOES", |
| 893 | // (__eglMustCastToProperFunctionPointerType)&glDrawTexivOES }, |
| 894 | // { "glDrawTexfvOES", |
| 895 | // (__eglMustCastToProperFunctionPointerType)&glDrawTexfvOES }, |
| 896 | // { "glDrawTexxvOES", |
| 897 | // (__eglMustCastToProperFunctionPointerType)&glDrawTexxvOES }, |
| 898 | // { "glQueryMatrixxOES", |
| 899 | // (__eglMustCastToProperFunctionPointerType)&glQueryMatrixxOES }, |
| 900 | // { "glEGLImageTargetTexture2DOES", |
| 901 | // (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetTexture2DOES }, |
| 902 | // { "glEGLImageTargetRenderbufferStorageOES", |
| 903 | // (__eglMustCastToProperFunctionPointerType)&glEGLImageTargetRenderbufferStorageOES }, |
| 904 | // { "glClipPlanef", |
| 905 | // (__eglMustCastToProperFunctionPointerType)&glClipPlanef }, |
| 906 | // { "glClipPlanex", |
| 907 | // (__eglMustCastToProperFunctionPointerType)&glClipPlanex }, |
| 908 | // { "glBindBuffer", |
| 909 | // (__eglMustCastToProperFunctionPointerType)&glBindBuffer }, |
| 910 | // { "glBufferData", |
| 911 | // (__eglMustCastToProperFunctionPointerType)&glBufferData }, |
| 912 | // { "glBufferSubData", |
| 913 | // (__eglMustCastToProperFunctionPointerType)&glBufferSubData }, |
| 914 | // { "glDeleteBuffers", |
| 915 | // (__eglMustCastToProperFunctionPointerType)&glDeleteBuffers }, |
| 916 | // { "glGenBuffers", |
| 917 | // (__eglMustCastToProperFunctionPointerType)&glGenBuffers }, |
| 918 | // { "eglCreateImageKHR", |
| 919 | // (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR }, |
| 920 | // { "eglDestroyImageKHR", |
| 921 | // (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR }, |
| 922 | // { "eglSetSwapRectangleANDROID", |
| 923 | // (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID }, |
| 924 | }; |
| 925 | |
| 926 | /* |
| 927 | * In the lists below, attributes names MUST be sorted. |
| 928 | * Additionally, all configs must be sorted according to |
| 929 | * the EGL specification. |
| 930 | */ |
| 931 | |
| 932 | static config_pair_t const config_base_attribute_list[] = { |
| 933 | { EGL_STENCIL_SIZE, 0 }, |
| 934 | { EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG }, |
| 935 | { EGL_LEVEL, 0 }, |
| 936 | { EGL_MAX_PBUFFER_HEIGHT, GGL_MAX_VIEWPORT_DIMS }, |
| 937 | { EGL_MAX_PBUFFER_PIXELS, |
| 938 | GGL_MAX_VIEWPORT_DIMS*GGL_MAX_VIEWPORT_DIMS }, |
| 939 | { EGL_MAX_PBUFFER_WIDTH, GGL_MAX_VIEWPORT_DIMS }, |
| 940 | { EGL_NATIVE_RENDERABLE, EGL_TRUE }, |
| 941 | { EGL_NATIVE_VISUAL_ID, 0 }, |
| 942 | { EGL_NATIVE_VISUAL_TYPE, GGL_PIXEL_FORMAT_RGBA_8888 }, |
| 943 | { EGL_SAMPLES, 0 }, |
| 944 | { EGL_SAMPLE_BUFFERS, 0 }, |
| 945 | { EGL_TRANSPARENT_TYPE, EGL_NONE }, |
| 946 | { EGL_TRANSPARENT_BLUE_VALUE, 0 }, |
| 947 | { EGL_TRANSPARENT_GREEN_VALUE, 0 }, |
| 948 | { EGL_TRANSPARENT_RED_VALUE, 0 }, |
| 949 | { EGL_BIND_TO_TEXTURE_RGBA, EGL_FALSE }, |
| 950 | { EGL_BIND_TO_TEXTURE_RGB, EGL_FALSE }, |
| 951 | { EGL_MIN_SWAP_INTERVAL, 1 }, |
| 952 | { EGL_MAX_SWAP_INTERVAL, 1 }, |
| 953 | { EGL_LUMINANCE_SIZE, 0 }, |
| 954 | { EGL_ALPHA_MASK_SIZE, 0 }, |
| 955 | { EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER }, |
| 956 | { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT }, |
| 957 | { EGL_CONFORMANT, 0 } |
| 958 | }; |
| 959 | |
| 960 | // These configs can override the base attribute list |
| 961 | // NOTE: when adding a config here, don't forget to update eglCreate*Surface() |
| 962 | |
| 963 | // 565 configs |
| 964 | static config_pair_t const config_0_attribute_list[] = { |
| 965 | { EGL_BUFFER_SIZE, 16 }, |
| 966 | { EGL_ALPHA_SIZE, 0 }, |
| 967 | { EGL_BLUE_SIZE, 5 }, |
| 968 | { EGL_GREEN_SIZE, 6 }, |
| 969 | { EGL_RED_SIZE, 5 }, |
| 970 | { EGL_DEPTH_SIZE, 0 }, |
| 971 | { EGL_CONFIG_ID, 0 }, |
| 972 | { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 }, |
| 973 | { EGL_SURFACE_TYPE, EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT }, |
| 974 | }; |
| 975 | |
| 976 | static config_pair_t const config_1_attribute_list[] = { |
| 977 | { EGL_BUFFER_SIZE, 16 }, |
| 978 | { EGL_ALPHA_SIZE, 0 }, |
| 979 | { EGL_BLUE_SIZE, 5 }, |
| 980 | { EGL_GREEN_SIZE, 6 }, |
| 981 | { EGL_RED_SIZE, 5 }, |
| 982 | { EGL_DEPTH_SIZE, 16 }, |
| 983 | { EGL_CONFIG_ID, 1 }, |
| 984 | { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGB_565 }, |
| 985 | { EGL_SURFACE_TYPE, EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT }, |
| 986 | }; |
| 987 | |
| 988 | // RGB 888 configs |
| 989 | static config_pair_t const config_2_attribute_list[] = { |
| 990 | { EGL_BUFFER_SIZE, 32 }, |
| 991 | { EGL_ALPHA_SIZE, 0 }, |
| 992 | { EGL_BLUE_SIZE, 8 }, |
| 993 | { EGL_GREEN_SIZE, 8 }, |
| 994 | { EGL_RED_SIZE, 8 }, |
| 995 | { EGL_DEPTH_SIZE, 0 }, |
| 996 | { EGL_CONFIG_ID, 6 }, |
| 997 | { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 }, |
| 998 | { EGL_SURFACE_TYPE, EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT }, |
| 999 | }; |
| 1000 | |
| 1001 | static config_pair_t const config_3_attribute_list[] = { |
| 1002 | { EGL_BUFFER_SIZE, 32 }, |
| 1003 | { EGL_ALPHA_SIZE, 0 }, |
| 1004 | { EGL_BLUE_SIZE, 8 }, |
| 1005 | { EGL_GREEN_SIZE, 8 }, |
| 1006 | { EGL_RED_SIZE, 8 }, |
| 1007 | { EGL_DEPTH_SIZE, 16 }, |
| 1008 | { EGL_CONFIG_ID, 7 }, |
| 1009 | { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBX_8888 }, |
| 1010 | { EGL_SURFACE_TYPE, EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT }, |
| 1011 | }; |
| 1012 | |
| 1013 | // 8888 configs |
| 1014 | static config_pair_t const config_4_attribute_list[] = { |
| 1015 | { EGL_BUFFER_SIZE, 32 }, |
| 1016 | { EGL_ALPHA_SIZE, 8 }, |
| 1017 | { EGL_BLUE_SIZE, 8 }, |
| 1018 | { EGL_GREEN_SIZE, 8 }, |
| 1019 | { EGL_RED_SIZE, 8 }, |
| 1020 | { EGL_DEPTH_SIZE, 0 }, |
| 1021 | { EGL_CONFIG_ID, 2 }, |
| 1022 | { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 }, |
| 1023 | { EGL_SURFACE_TYPE, EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT }, |
| 1024 | }; |
| 1025 | |
| 1026 | static config_pair_t const config_5_attribute_list[] = { |
| 1027 | { EGL_BUFFER_SIZE, 32 }, |
| 1028 | { EGL_ALPHA_SIZE, 8 }, |
| 1029 | { EGL_BLUE_SIZE, 8 }, |
| 1030 | { EGL_GREEN_SIZE, 8 }, |
| 1031 | { EGL_RED_SIZE, 8 }, |
| 1032 | { EGL_DEPTH_SIZE, 16 }, |
| 1033 | { EGL_CONFIG_ID, 3 }, |
| 1034 | { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_RGBA_8888 }, |
| 1035 | { EGL_SURFACE_TYPE, EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT }, |
| 1036 | }; |
| 1037 | |
| 1038 | // A8 configs |
| 1039 | static config_pair_t const config_6_attribute_list[] = { |
| 1040 | { EGL_BUFFER_SIZE, 8 }, |
| 1041 | { EGL_ALPHA_SIZE, 8 }, |
| 1042 | { EGL_BLUE_SIZE, 0 }, |
| 1043 | { EGL_GREEN_SIZE, 0 }, |
| 1044 | { EGL_RED_SIZE, 0 }, |
| 1045 | { EGL_DEPTH_SIZE, 0 }, |
| 1046 | { EGL_CONFIG_ID, 4 }, |
| 1047 | { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 }, |
| 1048 | { EGL_SURFACE_TYPE, EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT }, |
| 1049 | }; |
| 1050 | |
| 1051 | static config_pair_t const config_7_attribute_list[] = { |
| 1052 | { EGL_BUFFER_SIZE, 8 }, |
| 1053 | { EGL_ALPHA_SIZE, 8 }, |
| 1054 | { EGL_BLUE_SIZE, 0 }, |
| 1055 | { EGL_GREEN_SIZE, 0 }, |
| 1056 | { EGL_RED_SIZE, 0 }, |
| 1057 | { EGL_DEPTH_SIZE, 16 }, |
| 1058 | { EGL_CONFIG_ID, 5 }, |
| 1059 | { EGL_NATIVE_VISUAL_ID, GGL_PIXEL_FORMAT_A_8 }, |
| 1060 | { EGL_SURFACE_TYPE, EGL_SWAP_BEHAVIOR_PRESERVED_BIT|EGL_WINDOW_BIT|EGL_PBUFFER_BIT|EGL_PIXMAP_BIT }, |
| 1061 | }; |
| 1062 | |
| 1063 | static configs_t const gConfigs[] = { |
| 1064 | { config_0_attribute_list, NELEM(config_0_attribute_list) }, |
| 1065 | { config_1_attribute_list, NELEM(config_1_attribute_list) }, |
| 1066 | { config_2_attribute_list, NELEM(config_2_attribute_list) }, |
| 1067 | { config_3_attribute_list, NELEM(config_3_attribute_list) }, |
| 1068 | { config_4_attribute_list, NELEM(config_4_attribute_list) }, |
| 1069 | { config_5_attribute_list, NELEM(config_5_attribute_list) }, |
| 1070 | // { config_6_attribute_list, NELEM(config_6_attribute_list) }, |
| 1071 | // { config_7_attribute_list, NELEM(config_7_attribute_list) }, |
| 1072 | }; |
| 1073 | |
| 1074 | static config_management_t const gConfigManagement[] = { |
| 1075 | { EGL_BUFFER_SIZE, config_management_t::atLeast }, |
| 1076 | { EGL_ALPHA_SIZE, config_management_t::atLeast }, |
| 1077 | { EGL_BLUE_SIZE, config_management_t::atLeast }, |
| 1078 | { EGL_GREEN_SIZE, config_management_t::atLeast }, |
| 1079 | { EGL_RED_SIZE, config_management_t::atLeast }, |
| 1080 | { EGL_DEPTH_SIZE, config_management_t::atLeast }, |
| 1081 | { EGL_STENCIL_SIZE, config_management_t::atLeast }, |
| 1082 | { EGL_CONFIG_CAVEAT, config_management_t::exact }, |
| 1083 | { EGL_CONFIG_ID, config_management_t::exact }, |
| 1084 | { EGL_LEVEL, config_management_t::exact }, |
| 1085 | { EGL_MAX_PBUFFER_HEIGHT, config_management_t::ignore }, |
| 1086 | { EGL_MAX_PBUFFER_PIXELS, config_management_t::ignore }, |
| 1087 | { EGL_MAX_PBUFFER_WIDTH, config_management_t::ignore }, |
| 1088 | { EGL_NATIVE_RENDERABLE, config_management_t::exact }, |
| 1089 | { EGL_NATIVE_VISUAL_ID, config_management_t::ignore }, |
| 1090 | { EGL_NATIVE_VISUAL_TYPE, config_management_t::exact }, |
| 1091 | { EGL_SAMPLES, config_management_t::exact }, |
| 1092 | { EGL_SAMPLE_BUFFERS, config_management_t::exact }, |
| 1093 | { EGL_SURFACE_TYPE, config_management_t::mask }, |
| 1094 | { EGL_TRANSPARENT_TYPE, config_management_t::exact }, |
| 1095 | { EGL_TRANSPARENT_BLUE_VALUE, config_management_t::exact }, |
| 1096 | { EGL_TRANSPARENT_GREEN_VALUE, config_management_t::exact }, |
| 1097 | { EGL_TRANSPARENT_RED_VALUE, config_management_t::exact }, |
| 1098 | { EGL_BIND_TO_TEXTURE_RGBA, config_management_t::exact }, |
| 1099 | { EGL_BIND_TO_TEXTURE_RGB, config_management_t::exact }, |
| 1100 | { EGL_MIN_SWAP_INTERVAL, config_management_t::exact }, |
| 1101 | { EGL_MAX_SWAP_INTERVAL, config_management_t::exact }, |
| 1102 | { EGL_LUMINANCE_SIZE, config_management_t::atLeast }, |
| 1103 | { EGL_ALPHA_MASK_SIZE, config_management_t::atLeast }, |
| 1104 | { EGL_COLOR_BUFFER_TYPE, config_management_t::exact }, |
| 1105 | { EGL_RENDERABLE_TYPE, config_management_t::mask }, |
| 1106 | { EGL_CONFORMANT, config_management_t::mask } |
| 1107 | }; |
| 1108 | |
| 1109 | |
| 1110 | static config_pair_t const config_defaults[] = { |
| 1111 | // attributes that are not specified are simply ignored, if a particular |
| 1112 | // one needs not be ignored, it must be specified here, eg: |
| 1113 | // { EGL_SURFACE_TYPE, EGL_WINDOW_BIT }, |
| 1114 | }; |
| 1115 | |
| 1116 | // ---------------------------------------------------------------------------- |
| 1117 | |
| 1118 | static status_t getConfigFormatInfo(EGLint configID, |
| 1119 | int32_t& pixelFormat, int32_t& depthFormat) |
| 1120 | { |
| 1121 | switch (configID) { |
| 1122 | case 0: |
| 1123 | pixelFormat = GGL_PIXEL_FORMAT_RGB_565; |
| 1124 | depthFormat = 0; |
| 1125 | break; |
| 1126 | case 1: |
| 1127 | pixelFormat = GGL_PIXEL_FORMAT_RGB_565; |
| 1128 | depthFormat = GGL_PIXEL_FORMAT_Z_32; |
| 1129 | break; |
| 1130 | case 2: |
| 1131 | pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888; |
| 1132 | depthFormat = 0; |
| 1133 | break; |
| 1134 | case 3: |
| 1135 | pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888; |
| 1136 | depthFormat = GGL_PIXEL_FORMAT_Z_32; |
| 1137 | break; |
| 1138 | case 4: |
| 1139 | pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888; |
| 1140 | depthFormat = 0; |
| 1141 | break; |
| 1142 | case 5: |
| 1143 | pixelFormat = GGL_PIXEL_FORMAT_RGBA_8888; |
| 1144 | depthFormat = GGL_PIXEL_FORMAT_Z_32; |
| 1145 | break; |
| 1146 | case 6: |
| 1147 | pixelFormat = GGL_PIXEL_FORMAT_A_8; |
| 1148 | depthFormat = 0; |
| 1149 | break; |
| 1150 | case 7: |
| 1151 | pixelFormat = GGL_PIXEL_FORMAT_A_8; |
| 1152 | depthFormat = GGL_PIXEL_FORMAT_Z_32; |
| 1153 | break; |
| 1154 | default: |
| 1155 | return NAME_NOT_FOUND; |
| 1156 | } |
| 1157 | return NO_ERROR; |
| 1158 | } |
| 1159 | |
| 1160 | // ---------------------------------------------------------------------------- |
| 1161 | |
| 1162 | template<typename T> |
| 1163 | static int binarySearch(T const sortedArray[], int first, int last, EGLint key) |
| 1164 | { |
| 1165 | while (first <= last) { |
| 1166 | int mid = (first + last) / 2; |
| 1167 | if (key > sortedArray[mid].key) { |
| 1168 | first = mid + 1; |
| 1169 | } else if (key < sortedArray[mid].key) { |
| 1170 | last = mid - 1; |
| 1171 | } else { |
| 1172 | return mid; |
| 1173 | } |
| 1174 | } |
| 1175 | return -1; |
| 1176 | } |
| 1177 | |
| 1178 | static int isAttributeMatching(int i, EGLint attr, EGLint val) |
| 1179 | { |
| 1180 | // look for the attribute in all of our configs |
| 1181 | config_pair_t const* configFound = gConfigs[i].array; |
| 1182 | int index = binarySearch<config_pair_t>( |
| 1183 | gConfigs[i].array, |
| 1184 | 0, gConfigs[i].size-1, |
| 1185 | attr); |
| 1186 | if (index < 0) { |
| 1187 | configFound = config_base_attribute_list; |
| 1188 | index = binarySearch<config_pair_t>( |
| 1189 | config_base_attribute_list, |
| 1190 | 0, NELEM(config_base_attribute_list)-1, |
| 1191 | attr); |
| 1192 | } |
| 1193 | if (index >= 0) { |
| 1194 | // attribute found, check if this config could match |
| 1195 | int cfgMgtIndex = binarySearch<config_management_t>( |
| 1196 | gConfigManagement, |
| 1197 | 0, NELEM(gConfigManagement)-1, |
| 1198 | attr); |
| 1199 | if (cfgMgtIndex >= 0) { |
| 1200 | bool match = gConfigManagement[cfgMgtIndex].match( |
| 1201 | val, configFound[index].value); |
| 1202 | if (match) { |
| 1203 | // this config matches |
| 1204 | return 1; |
| 1205 | } |
| 1206 | } else { |
| 1207 | // attribute not found. this should NEVER happen. |
| 1208 | } |
| 1209 | } else { |
| 1210 | // error, this attribute doesn't exist |
| 1211 | } |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static int makeCurrent(GLES2Context* gl) |
| 1216 | { |
| 1217 | GLES2Context* current = (GLES2Context*)getGlThreadSpecific(); |
| 1218 | if (gl) { |
| 1219 | egl_context_t* c = egl_context_t::context(gl); |
| 1220 | if (c->flags & egl_context_t::IS_CURRENT) { |
| 1221 | if (current != gl) { |
| 1222 | // it is an error to set a context current, if it's already |
| 1223 | // current to another thread |
| 1224 | return -1; |
| 1225 | } |
| 1226 | } else { |
| 1227 | if (current) { |
| 1228 | // mark the current context as not current, and flush |
| 1229 | glFlush(); |
| 1230 | egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT; |
| 1231 | } |
| 1232 | } |
| 1233 | if (!(c->flags & egl_context_t::IS_CURRENT)) { |
| 1234 | // The context is not current, make it current! |
| 1235 | setGlThreadSpecific(gl); |
| 1236 | c->flags |= egl_context_t::IS_CURRENT; |
| 1237 | } |
| 1238 | } else { |
| 1239 | if (current) { |
| 1240 | // mark the current context as not current, and flush |
| 1241 | glFlush(); |
| 1242 | egl_context_t::context(current)->flags &= ~egl_context_t::IS_CURRENT; |
| 1243 | } |
| 1244 | // this thread has no context attached to it |
| 1245 | setGlThreadSpecific(0); |
| 1246 | } |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | static EGLBoolean getConfigAttrib(EGLDisplay dpy, EGLConfig config, |
| 1251 | EGLint attribute, EGLint *value) |
| 1252 | { |
| 1253 | size_t numConfigs = NELEM(gConfigs); |
| 1254 | int index = (int)config; |
| 1255 | if (uint32_t(index) >= numConfigs) |
| 1256 | return setError(EGL_BAD_CONFIG, EGL_FALSE); |
| 1257 | |
| 1258 | int attrIndex; |
| 1259 | attrIndex = binarySearch<config_pair_t>( |
| 1260 | gConfigs[index].array, |
| 1261 | 0, gConfigs[index].size-1, |
| 1262 | attribute); |
| 1263 | if (attrIndex>=0) { |
| 1264 | *value = gConfigs[index].array[attrIndex].value; |
| 1265 | return EGL_TRUE; |
| 1266 | } |
| 1267 | |
| 1268 | attrIndex = binarySearch<config_pair_t>( |
| 1269 | config_base_attribute_list, |
| 1270 | 0, NELEM(config_base_attribute_list)-1, |
| 1271 | attribute); |
| 1272 | if (attrIndex>=0) { |
| 1273 | *value = config_base_attribute_list[attrIndex].value; |
| 1274 | return EGL_TRUE; |
| 1275 | } |
| 1276 | return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE); |
| 1277 | } |
| 1278 | |
| 1279 | static EGLSurface createWindowSurface(EGLDisplay dpy, EGLConfig config, |
| 1280 | NativeWindowType window, const EGLint *attrib_list) |
| 1281 | { |
| 1282 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1283 | return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE); |
| 1284 | if (window == 0) |
| 1285 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1286 | |
| 1287 | EGLint surfaceType; |
| 1288 | if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE) |
| 1289 | return EGL_FALSE; |
| 1290 | |
| 1291 | if (!(surfaceType & EGL_WINDOW_BIT)) |
| 1292 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1293 | |
| 1294 | if (reinterpret_cast<ANativeWindow*>(window)->common.magic != |
| 1295 | ANDROID_NATIVE_WINDOW_MAGIC) { |
| 1296 | return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE); |
| 1297 | } |
| 1298 | |
| 1299 | EGLint configID; |
| 1300 | if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE) |
| 1301 | return EGL_FALSE; |
| 1302 | |
| 1303 | int32_t depthFormat; |
| 1304 | int32_t pixelFormat; |
| 1305 | if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) { |
| 1306 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1307 | } |
| 1308 | |
| 1309 | // FIXME: we don't have access to the pixelFormat here just yet. |
| 1310 | // (it's possible that the surface is not fully initialized) |
| 1311 | // maybe this should be done after the page-flip |
| 1312 | //if (EGLint(info.format) != pixelFormat) |
| 1313 | // return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1314 | |
| 1315 | egl_surface_t* surface; |
| 1316 | surface = new egl_window_surface_v2_t(dpy, config, depthFormat, |
| 1317 | reinterpret_cast<ANativeWindow*>(window)); |
| 1318 | |
| 1319 | if (!surface->initCheck()) { |
| 1320 | // there was a problem in the ctor, the error |
| 1321 | // flag has been set. |
| 1322 | delete surface; |
| 1323 | surface = 0; |
| 1324 | } |
| 1325 | return surface; |
| 1326 | } |
| 1327 | |
| 1328 | static EGLSurface createPixmapSurface(EGLDisplay dpy, EGLConfig config, |
| 1329 | NativePixmapType pixmap, const EGLint *attrib_list) |
| 1330 | { |
| 1331 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1332 | return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE); |
| 1333 | if (pixmap == 0) |
| 1334 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1335 | |
| 1336 | EGLint surfaceType; |
| 1337 | if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE) |
| 1338 | return EGL_FALSE; |
| 1339 | |
| 1340 | if (!(surfaceType & EGL_PIXMAP_BIT)) |
| 1341 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1342 | |
| 1343 | if (reinterpret_cast<egl_native_pixmap_t*>(pixmap)->version != |
| 1344 | sizeof(egl_native_pixmap_t)) { |
| 1345 | return setError(EGL_BAD_NATIVE_PIXMAP, EGL_NO_SURFACE); |
| 1346 | } |
| 1347 | |
| 1348 | EGLint configID; |
| 1349 | if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE) |
| 1350 | return EGL_FALSE; |
| 1351 | |
| 1352 | int32_t depthFormat; |
| 1353 | int32_t pixelFormat; |
| 1354 | if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) { |
| 1355 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1356 | } |
| 1357 | |
| 1358 | if (reinterpret_cast<egl_native_pixmap_t *>(pixmap)->format != pixelFormat) |
| 1359 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1360 | |
| 1361 | egl_surface_t* surface = |
| 1362 | new egl_pixmap_surface_t(dpy, config, depthFormat, |
| 1363 | reinterpret_cast<egl_native_pixmap_t*>(pixmap)); |
| 1364 | |
| 1365 | if (!surface->initCheck()) { |
| 1366 | // there was a problem in the ctor, the error |
| 1367 | // flag has been set. |
| 1368 | delete surface; |
| 1369 | surface = 0; |
| 1370 | } |
| 1371 | return surface; |
| 1372 | } |
| 1373 | |
| 1374 | static EGLSurface createPbufferSurface(EGLDisplay dpy, EGLConfig config, |
| 1375 | const EGLint *attrib_list) |
| 1376 | { |
| 1377 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1378 | return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE); |
| 1379 | |
| 1380 | EGLint surfaceType; |
| 1381 | if (getConfigAttrib(dpy, config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE) |
| 1382 | return EGL_FALSE; |
| 1383 | |
| 1384 | if (!(surfaceType & EGL_PBUFFER_BIT)) |
| 1385 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1386 | |
| 1387 | EGLint configID; |
| 1388 | if (getConfigAttrib(dpy, config, EGL_CONFIG_ID, &configID) == EGL_FALSE) |
| 1389 | return EGL_FALSE; |
| 1390 | |
| 1391 | int32_t depthFormat; |
| 1392 | int32_t pixelFormat; |
| 1393 | if (getConfigFormatInfo(configID, pixelFormat, depthFormat) != NO_ERROR) { |
| 1394 | return setError(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 1395 | } |
| 1396 | |
| 1397 | int32_t w = 0; |
| 1398 | int32_t h = 0; |
| 1399 | while (attrib_list[0]) { |
| 1400 | if (attrib_list[0] == EGL_WIDTH) w = attrib_list[1]; |
| 1401 | if (attrib_list[0] == EGL_HEIGHT) h = attrib_list[1]; |
| 1402 | attrib_list+=2; |
| 1403 | } |
| 1404 | |
| 1405 | egl_surface_t* surface = |
| 1406 | new egl_pbuffer_surface_t(dpy, config, depthFormat, w, h, pixelFormat); |
| 1407 | |
| 1408 | if (!surface->initCheck()) { |
| 1409 | // there was a problem in the ctor, the error |
| 1410 | // flag has been set. |
| 1411 | delete surface; |
| 1412 | surface = 0; |
| 1413 | } |
| 1414 | return surface; |
| 1415 | } |
| 1416 | |
| 1417 | // ---------------------------------------------------------------------------- |
| 1418 | }; // namespace android |
| 1419 | // ---------------------------------------------------------------------------- |
| 1420 | |
| 1421 | using namespace android; |
| 1422 | |
| 1423 | // ---------------------------------------------------------------------------- |
| 1424 | // Initialization |
| 1425 | // ---------------------------------------------------------------------------- |
| 1426 | |
| 1427 | EGLDisplay eglGetDisplay(NativeDisplayType display) |
| 1428 | { |
| 1429 | puts("agl2:eglGetDisplay"); |
| 1430 | #ifndef HAVE_ANDROID_OS |
| 1431 | // this just needs to be done once |
| 1432 | if (gGLKey == -1) { |
| 1433 | pthread_mutex_lock(&gInitMutex); |
| 1434 | if (gGLKey == -1) |
| 1435 | pthread_key_create(&gGLKey, NULL); |
| 1436 | pthread_mutex_unlock(&gInitMutex); |
| 1437 | } |
| 1438 | #endif |
| 1439 | if (display == EGL_DEFAULT_DISPLAY) { |
| 1440 | EGLDisplay dpy = (EGLDisplay)1; |
| 1441 | egl_display_t& d = egl_display_t::get_display(dpy); |
| 1442 | d.type = display; |
| 1443 | return dpy; |
| 1444 | } |
| 1445 | return EGL_NO_DISPLAY; |
| 1446 | } |
| 1447 | |
| 1448 | EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor) |
| 1449 | { |
| 1450 | puts("agl2:eglInitialize"); |
| 1451 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1452 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1453 | |
| 1454 | EGLBoolean res = EGL_TRUE; |
| 1455 | egl_display_t& d = egl_display_t::get_display(dpy); |
| 1456 | |
| 1457 | if (android_atomic_inc(&d.initialized) == 0) { |
| 1458 | // initialize stuff here if needed |
| 1459 | //pthread_mutex_lock(&gInitMutex); |
| 1460 | //pthread_mutex_unlock(&gInitMutex); |
| 1461 | } |
| 1462 | |
| 1463 | if (res == EGL_TRUE) { |
| 1464 | if (major != NULL) *major = VERSION_MAJOR; |
| 1465 | if (minor != NULL) *minor = VERSION_MINOR; |
| 1466 | } |
| 1467 | return res; |
| 1468 | } |
| 1469 | |
| 1470 | EGLBoolean eglTerminate(EGLDisplay dpy) |
| 1471 | { |
| 1472 | puts("agl2:eglTerminate"); |
| 1473 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1474 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1475 | |
| 1476 | EGLBoolean res = EGL_TRUE; |
| 1477 | egl_display_t& d = egl_display_t::get_display(dpy); |
| 1478 | if (android_atomic_dec(&d.initialized) == 1) { |
| 1479 | // TODO: destroy all resources (surfaces, contexts, etc...) |
| 1480 | //pthread_mutex_lock(&gInitMutex); |
| 1481 | //pthread_mutex_unlock(&gInitMutex); |
| 1482 | } |
| 1483 | return res; |
| 1484 | } |
| 1485 | |
| 1486 | // ---------------------------------------------------------------------------- |
| 1487 | // configuration |
| 1488 | // ---------------------------------------------------------------------------- |
| 1489 | |
| 1490 | EGLBoolean eglGetConfigs( EGLDisplay dpy, |
| 1491 | EGLConfig *configs, |
| 1492 | EGLint config_size, EGLint *num_config) |
| 1493 | { |
| 1494 | puts("agl2:eglGetConfigs"); |
| 1495 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1496 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1497 | |
| 1498 | GLint numConfigs = NELEM(gConfigs); |
| 1499 | if (!configs) { |
| 1500 | *num_config = numConfigs; |
| 1501 | return EGL_TRUE; |
| 1502 | } |
| 1503 | GLint i; |
| 1504 | for (i=0 ; i<numConfigs && i<config_size ; i++) { |
| 1505 | *configs++ = (EGLConfig)i; |
| 1506 | } |
| 1507 | *num_config = i; |
| 1508 | return EGL_TRUE; |
| 1509 | } |
| 1510 | |
| 1511 | static const char * ATTRIBUTE_NAMES [] = { |
| 1512 | "EGL_BUFFER_SIZE", |
| 1513 | "EGL_ALPHA_SIZE", |
| 1514 | "EGL_BLUE_SIZE", |
| 1515 | "EGL_GREEN_SIZE", |
| 1516 | "EGL_RED_SIZE", |
| 1517 | "EGL_DEPTH_SIZE", |
| 1518 | "EGL_STENCIL_SIZE", |
| 1519 | "EGL_CONFIG_CAVEAT", |
| 1520 | "EGL_CONFIG_ID", |
| 1521 | "EGL_LEVEL", |
| 1522 | "EGL_MAX_PBUFFER_HEIGHT", |
| 1523 | "EGL_MAX_PBUFFER_PIXELS", |
| 1524 | "EGL_MAX_PBUFFER_WIDTH", |
| 1525 | "EGL_NATIVE_RENDERABLE", |
| 1526 | "EGL_NATIVE_VISUAL_ID", |
| 1527 | "EGL_NATIVE_VISUAL_TYPE", |
| 1528 | "EGL_PRESERVED_RESOURCES", |
| 1529 | "EGL_SAMPLES", |
| 1530 | "EGL_SAMPLE_BUFFERS", |
| 1531 | "EGL_SURFACE_TYPE", |
| 1532 | "EGL_TRANSPARENT_TYPE", |
| 1533 | "EGL_TRANSPARENT_BLUE_VALUE", |
| 1534 | "EGL_TRANSPARENT_GREEN_VALUE", |
| 1535 | "EGL_TRANSPARENT_RED_VALUE", |
| 1536 | "EGL_NONE", /* Attrib list terminator */ |
| 1537 | "EGL_BIND_TO_TEXTURE_RGB", |
| 1538 | "EGL_BIND_TO_TEXTURE_RGBA", |
| 1539 | "EGL_MIN_SWAP_INTERVAL", |
| 1540 | "EGL_MAX_SWAP_INTERVAL", |
| 1541 | "EGL_LUMINANCE_SIZE", |
| 1542 | "EGL_ALPHA_MASK_SIZE", |
| 1543 | "EGL_COLOR_BUFFER_TYPE", |
| 1544 | "EGL_RENDERABLE_TYPE", |
| 1545 | "EGL_MATCH_NATIVE_PIXMAP", /* Pseudo-attribute (not queryable) */ |
| 1546 | "EGL_CONFORMANT", |
| 1547 | }; |
| 1548 | |
| 1549 | EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list, |
| 1550 | EGLConfig *configs, EGLint config_size, |
| 1551 | EGLint *num_config) |
| 1552 | { |
| 1553 | puts("agl2:eglChooseConfig"); |
| 1554 | LOGD("\n***\n***\n agl2:LOGD eglChooseConfig \n***\n***\n"); |
| 1555 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1556 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1557 | |
| 1558 | if (ggl_unlikely(num_config==0)) { |
| 1559 | LOGD("\n***\n***\n num_config==0 \n***\n***\n"); |
| 1560 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1561 | } |
| 1562 | |
| 1563 | if (ggl_unlikely(attrib_list==0)) { |
| 1564 | /* |
| 1565 | * A NULL attrib_list should be treated as though it was an empty |
| 1566 | * one (terminated with EGL_NONE) as defined in |
| 1567 | * section 3.4.1 "Querying Configurations" in the EGL specification. |
| 1568 | */ |
| 1569 | LOGD("\n***\n***\n attrib_list==0 \n***\n***\n"); |
| 1570 | static const EGLint dummy = EGL_NONE; |
| 1571 | attrib_list = &dummy; |
| 1572 | } |
| 1573 | |
| 1574 | for (const EGLint * attrib = attrib_list; *attrib != EGL_NONE; attrib += 2) { |
| 1575 | LOGD("eglChooseConfig %s(%.4X): %d \n", ATTRIBUTE_NAMES[attrib[0] - EGL_BUFFER_SIZE], attrib[0], attrib[1]); |
| 1576 | if (EGL_BUFFER_SIZE > attrib[0] || EGL_CONFORMANT < attrib[0]) |
| 1577 | LOGD("eglChooseConfig invalid config attrib: 0x%.4X=%d \n", attrib[0], attrib[1]); |
| 1578 | } |
| 1579 | |
| 1580 | int numAttributes = 0; |
| 1581 | int numConfigs = NELEM(gConfigs); |
| 1582 | uint32_t possibleMatch = (1<<numConfigs)-1; |
| 1583 | while (possibleMatch && *attrib_list != EGL_NONE) { |
| 1584 | numAttributes++; |
| 1585 | EGLint attr = *attrib_list++; |
| 1586 | EGLint val = *attrib_list++; |
| 1587 | for (int i=0 ; possibleMatch && i<numConfigs ; i++) { |
| 1588 | if (!(possibleMatch & (1<<i))) |
| 1589 | continue; |
| 1590 | if (isAttributeMatching(i, attr, val) == 0) { |
| 1591 | LOGD("!isAttributeMatching config(%d) %s=%d \n", i, ATTRIBUTE_NAMES[attr - EGL_BUFFER_SIZE], val); |
| 1592 | possibleMatch &= ~(1<<i); |
| 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | LOGD("eglChooseConfig possibleMatch=%.4X \n", possibleMatch); |
| 1598 | |
| 1599 | // now, handle the attributes which have a useful default value |
| 1600 | for (size_t j=0 ; possibleMatch && j<NELEM(config_defaults) ; j++) { |
| 1601 | // see if this attribute was specified, if not, apply its |
| 1602 | // default value |
| 1603 | if (binarySearch<config_pair_t>( |
| 1604 | (config_pair_t const*)attrib_list, |
| 1605 | 0, numAttributes-1, |
| 1606 | config_defaults[j].key) < 0) { |
| 1607 | for (int i=0 ; possibleMatch && i<numConfigs ; i++) { |
| 1608 | if (!(possibleMatch & (1<<i))) |
| 1609 | continue; |
| 1610 | if (isAttributeMatching(i, |
| 1611 | config_defaults[j].key, |
| 1612 | config_defaults[j].value) == 0) { |
| 1613 | possibleMatch &= ~(1<<i); |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | // return the configurations found |
| 1620 | int n=0; |
| 1621 | if (possibleMatch) { |
| 1622 | if (configs) { |
| 1623 | for (int i=0 ; config_size && i<numConfigs ; i++) { |
| 1624 | if (possibleMatch & (1<<i)) { |
| 1625 | *configs++ = (EGLConfig)i; |
| 1626 | config_size--; |
| 1627 | n++; |
| 1628 | } |
| 1629 | } |
| 1630 | } else { |
| 1631 | for (int i=0 ; i<numConfigs ; i++) { |
| 1632 | if (possibleMatch & (1<<i)) { |
| 1633 | n++; |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | *num_config = n; |
| 1639 | LOGD("\n***\n***\n num_config==%d \n***\n***\n", *num_config); |
| 1640 | return EGL_TRUE; |
| 1641 | } |
| 1642 | |
| 1643 | EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, |
| 1644 | EGLint attribute, EGLint *value) |
| 1645 | { |
| 1646 | puts("agl2:eglGetConfigAttrib"); |
| 1647 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1648 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1649 | |
| 1650 | return getConfigAttrib(dpy, config, attribute, value); |
| 1651 | } |
| 1652 | |
| 1653 | // ---------------------------------------------------------------------------- |
| 1654 | // surfaces |
| 1655 | // ---------------------------------------------------------------------------- |
| 1656 | |
| 1657 | EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config, |
| 1658 | NativeWindowType window, |
| 1659 | const EGLint *attrib_list) |
| 1660 | { |
| 1661 | puts("agl2:eglCreateWindowSurface"); |
| 1662 | return createWindowSurface(dpy, config, window, attrib_list); |
| 1663 | } |
| 1664 | |
| 1665 | EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config, |
| 1666 | NativePixmapType pixmap, |
| 1667 | const EGLint *attrib_list) |
| 1668 | { |
| 1669 | puts("agl2:eglCreatePixmapSurface"); |
| 1670 | return createPixmapSurface(dpy, config, pixmap, attrib_list); |
| 1671 | } |
| 1672 | |
| 1673 | EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config, |
| 1674 | const EGLint *attrib_list) |
| 1675 | { |
| 1676 | puts("agl2:eglCreatePbufferSurface"); |
| 1677 | return createPbufferSurface(dpy, config, attrib_list); |
| 1678 | } |
| 1679 | |
| 1680 | EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface) |
| 1681 | { |
| 1682 | puts("agl2:eglDestroySurface"); |
| 1683 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1684 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1685 | if (eglSurface != EGL_NO_SURFACE) { |
| 1686 | egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) ); |
| 1687 | if (!surface->isValid()) |
| 1688 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1689 | if (surface->dpy != dpy) |
| 1690 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1691 | if (surface->ctx) { |
| 1692 | // FIXME: this surface is current check what the spec says |
| 1693 | surface->disconnect(); |
| 1694 | surface->ctx = 0; |
| 1695 | } |
| 1696 | delete surface; |
| 1697 | } |
| 1698 | return EGL_TRUE; |
| 1699 | } |
| 1700 | |
| 1701 | EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface eglSurface, |
| 1702 | EGLint attribute, EGLint *value) |
| 1703 | { |
| 1704 | puts("agl2:eglQuerySurface"); |
| 1705 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1706 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1707 | egl_surface_t* surface = static_cast<egl_surface_t*>(eglSurface); |
| 1708 | if (!surface->isValid()) |
| 1709 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1710 | if (surface->dpy != dpy) |
| 1711 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1712 | |
| 1713 | EGLBoolean ret = EGL_TRUE; |
| 1714 | switch (attribute) { |
| 1715 | case EGL_CONFIG_ID: |
| 1716 | ret = getConfigAttrib(dpy, surface->config, EGL_CONFIG_ID, value); |
| 1717 | break; |
| 1718 | case EGL_WIDTH: |
| 1719 | *value = surface->getWidth(); |
| 1720 | break; |
| 1721 | case EGL_HEIGHT: |
| 1722 | *value = surface->getHeight(); |
| 1723 | break; |
| 1724 | case EGL_LARGEST_PBUFFER: |
| 1725 | // not modified for a window or pixmap surface |
| 1726 | break; |
| 1727 | case EGL_TEXTURE_FORMAT: |
| 1728 | *value = EGL_NO_TEXTURE; |
| 1729 | break; |
| 1730 | case EGL_TEXTURE_TARGET: |
| 1731 | *value = EGL_NO_TEXTURE; |
| 1732 | break; |
| 1733 | case EGL_MIPMAP_TEXTURE: |
| 1734 | *value = EGL_FALSE; |
| 1735 | break; |
| 1736 | case EGL_MIPMAP_LEVEL: |
| 1737 | *value = 0; |
| 1738 | break; |
| 1739 | case EGL_RENDER_BUFFER: |
| 1740 | // TODO: return the real RENDER_BUFFER here |
| 1741 | *value = EGL_BACK_BUFFER; |
| 1742 | break; |
| 1743 | case EGL_HORIZONTAL_RESOLUTION: |
| 1744 | // pixel/mm * EGL_DISPLAY_SCALING |
| 1745 | *value = surface->getHorizontalResolution(); |
| 1746 | break; |
| 1747 | case EGL_VERTICAL_RESOLUTION: |
| 1748 | // pixel/mm * EGL_DISPLAY_SCALING |
| 1749 | *value = surface->getVerticalResolution(); |
| 1750 | break; |
| 1751 | case EGL_PIXEL_ASPECT_RATIO: { |
| 1752 | // w/h * EGL_DISPLAY_SCALING |
| 1753 | int wr = surface->getHorizontalResolution(); |
| 1754 | int hr = surface->getVerticalResolution(); |
| 1755 | *value = (wr * EGL_DISPLAY_SCALING) / hr; |
| 1756 | } |
| 1757 | break; |
| 1758 | case EGL_SWAP_BEHAVIOR: |
| 1759 | *value = surface->getSwapBehavior(); |
| 1760 | break; |
| 1761 | default: |
| 1762 | ret = setError(EGL_BAD_ATTRIBUTE, EGL_FALSE); |
| 1763 | } |
| 1764 | return ret; |
| 1765 | } |
| 1766 | |
| 1767 | EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, |
| 1768 | EGLContext share_list, const EGLint *attrib_list) |
| 1769 | { |
| 1770 | puts("agl2:eglCreateContext"); |
| 1771 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1772 | return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE); |
| 1773 | |
| 1774 | GLES2Context* gl = new GLES2Context();//ogles_init(sizeof(egl_context_t)); |
| 1775 | if (!gl) return setError(EGL_BAD_ALLOC, EGL_NO_CONTEXT); |
| 1776 | |
| 1777 | //egl_context_t* c = static_cast<egl_context_t*>(gl->rasterizer.base); |
| 1778 | egl_context_t * c = &gl->egl; |
| 1779 | c->flags = egl_context_t::NEVER_CURRENT; |
| 1780 | c->dpy = dpy; |
| 1781 | c->config = config; |
| 1782 | c->read = 0; |
| 1783 | c->draw = 0; |
| 1784 | |
| 1785 | c->frame = 0; |
| 1786 | c->lastSwapTime = clock(); |
| 1787 | c->accumulateSeconds = 0; |
| 1788 | return (EGLContext)gl; |
| 1789 | } |
| 1790 | |
| 1791 | EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx) |
| 1792 | { |
| 1793 | puts("agl2:eglDestroyContext"); |
| 1794 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1795 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1796 | egl_context_t* c = egl_context_t::context(ctx); |
| 1797 | if (c->flags & egl_context_t::IS_CURRENT) |
| 1798 | setGlThreadSpecific(0); |
| 1799 | //ogles_uninit((GLES2Context*)ctx); |
| 1800 | delete (GLES2Context*)ctx; |
| 1801 | return EGL_TRUE; |
| 1802 | } |
| 1803 | |
| 1804 | EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw, |
| 1805 | EGLSurface read, EGLContext ctx) |
| 1806 | { |
| 1807 | puts("agl2:eglMakeCurrent"); |
| 1808 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1809 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1810 | if (draw) { |
| 1811 | egl_surface_t* s = (egl_surface_t*)draw; |
| 1812 | if (!s->isValid()) |
| 1813 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1814 | if (s->dpy != dpy) |
| 1815 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1816 | // TODO: check that draw is compatible with the context |
| 1817 | } |
| 1818 | if (read && read!=draw) { |
| 1819 | egl_surface_t* s = (egl_surface_t*)read; |
| 1820 | if (!s->isValid()) |
| 1821 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1822 | if (s->dpy != dpy) |
| 1823 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1824 | // TODO: check that read is compatible with the context |
| 1825 | } |
| 1826 | |
| 1827 | EGLContext current_ctx = EGL_NO_CONTEXT; |
| 1828 | |
| 1829 | if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT)) |
| 1830 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
| 1831 | |
| 1832 | if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT)) |
| 1833 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
| 1834 | |
| 1835 | if (ctx == EGL_NO_CONTEXT) { |
| 1836 | // if we're detaching, we need the current context |
| 1837 | current_ctx = (EGLContext)getGlThreadSpecific(); |
| 1838 | } else { |
| 1839 | egl_context_t* c = egl_context_t::context(ctx); |
| 1840 | egl_surface_t* d = (egl_surface_t*)draw; |
| 1841 | egl_surface_t* r = (egl_surface_t*)read; |
| 1842 | if ((d && d->ctx && d->ctx != ctx) || |
| 1843 | (r && r->ctx && r->ctx != ctx)) { |
| 1844 | // one of the surface is bound to a context in another thread |
| 1845 | return setError(EGL_BAD_ACCESS, EGL_FALSE); |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | GLES2Context* gl = (GLES2Context*)ctx; |
| 1850 | if (makeCurrent(gl) == 0) { |
| 1851 | if (ctx) { |
| 1852 | egl_context_t* c = egl_context_t::context(ctx); |
| 1853 | egl_surface_t* d = (egl_surface_t*)draw; |
| 1854 | egl_surface_t* r = (egl_surface_t*)read; |
| 1855 | |
| 1856 | if (c->draw) { |
| 1857 | egl_surface_t* s = reinterpret_cast<egl_surface_t*>(c->draw); |
| 1858 | s->disconnect(); |
| 1859 | } |
| 1860 | if (c->read) { |
| 1861 | // FIXME: unlock/disconnect the read surface too |
| 1862 | } |
| 1863 | |
| 1864 | c->draw = draw; |
| 1865 | c->read = read; |
| 1866 | |
| 1867 | if (c->flags & egl_context_t::NEVER_CURRENT) { |
| 1868 | c->flags &= ~egl_context_t::NEVER_CURRENT; |
| 1869 | GLint w = 0; |
| 1870 | GLint h = 0; |
| 1871 | if (draw) { |
| 1872 | w = d->getWidth(); |
| 1873 | h = d->getHeight(); |
| 1874 | } |
| 1875 | gl->rasterizer.interface.Viewport(&gl->rasterizer.interface, 0, 0, w, h); |
| 1876 | //ogles_surfaceport(gl, 0, 0); |
| 1877 | //ogles_viewport(gl, 0, 0, w, h); |
| 1878 | //ogles_scissor(gl, 0, 0, w, h); |
| 1879 | } |
| 1880 | if (d) { |
| 1881 | if (d->connect() == EGL_FALSE) { |
| 1882 | return EGL_FALSE; |
| 1883 | } |
| 1884 | d->ctx = ctx; |
| 1885 | d->bindDrawSurface(gl); |
| 1886 | } |
| 1887 | if (r) { |
| 1888 | // FIXME: lock/connect the read surface too |
| 1889 | r->ctx = ctx; |
| 1890 | r->bindReadSurface(gl); |
| 1891 | } |
| 1892 | } else { |
| 1893 | // if surfaces were bound to the context bound to this thread |
| 1894 | // mark then as unbound. |
| 1895 | if (current_ctx) { |
| 1896 | egl_context_t* c = egl_context_t::context(current_ctx); |
| 1897 | egl_surface_t* d = (egl_surface_t*)c->draw; |
| 1898 | egl_surface_t* r = (egl_surface_t*)c->read; |
| 1899 | if (d) { |
| 1900 | c->draw = 0; |
| 1901 | d->ctx = EGL_NO_CONTEXT; |
| 1902 | d->disconnect(); |
| 1903 | } |
| 1904 | if (r) { |
| 1905 | c->read = 0; |
| 1906 | r->ctx = EGL_NO_CONTEXT; |
| 1907 | // FIXME: unlock/disconnect the read surface too |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | return EGL_TRUE; |
| 1912 | } |
| 1913 | return setError(EGL_BAD_ACCESS, EGL_FALSE); |
| 1914 | } |
| 1915 | |
| 1916 | EGLContext eglGetCurrentContext(void) |
| 1917 | { |
| 1918 | // eglGetCurrentContext returns the current EGL rendering context, |
| 1919 | // as specified by eglMakeCurrent. If no context is current, |
| 1920 | // EGL_NO_CONTEXT is returned. |
| 1921 | return (EGLContext)getGlThreadSpecific(); |
| 1922 | } |
| 1923 | |
| 1924 | EGLSurface eglGetCurrentSurface(EGLint readdraw) |
| 1925 | { |
| 1926 | // eglGetCurrentSurface returns the read or draw surface attached |
| 1927 | // to the current EGL rendering context, as specified by eglMakeCurrent. |
| 1928 | // If no context is current, EGL_NO_SURFACE is returned. |
| 1929 | EGLContext ctx = (EGLContext)getGlThreadSpecific(); |
| 1930 | if (ctx == EGL_NO_CONTEXT) return EGL_NO_SURFACE; |
| 1931 | egl_context_t* c = egl_context_t::context(ctx); |
| 1932 | if (readdraw == EGL_READ) { |
| 1933 | return c->read; |
| 1934 | } else if (readdraw == EGL_DRAW) { |
| 1935 | return c->draw; |
| 1936 | } |
| 1937 | return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 1938 | } |
| 1939 | |
| 1940 | EGLDisplay eglGetCurrentDisplay(void) |
| 1941 | { |
| 1942 | // eglGetCurrentDisplay returns the current EGL display connection |
| 1943 | // for the current EGL rendering context, as specified by eglMakeCurrent. |
| 1944 | // If no context is current, EGL_NO_DISPLAY is returned. |
| 1945 | EGLContext ctx = (EGLContext)getGlThreadSpecific(); |
| 1946 | if (ctx == EGL_NO_CONTEXT) return EGL_NO_DISPLAY; |
| 1947 | egl_context_t* c = egl_context_t::context(ctx); |
| 1948 | return c->dpy; |
| 1949 | } |
| 1950 | |
| 1951 | EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx, |
| 1952 | EGLint attribute, EGLint *value) |
| 1953 | { |
| 1954 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1955 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1956 | egl_context_t* c = egl_context_t::context(ctx); |
| 1957 | switch (attribute) { |
| 1958 | case EGL_CONFIG_ID: |
| 1959 | // Returns the ID of the EGL frame buffer configuration with |
| 1960 | // respect to which the context was created |
| 1961 | return getConfigAttrib(dpy, c->config, EGL_CONFIG_ID, value); |
| 1962 | } |
| 1963 | return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE); |
| 1964 | } |
| 1965 | |
| 1966 | EGLBoolean eglWaitGL(void) |
| 1967 | { |
| 1968 | return EGL_TRUE; |
| 1969 | } |
| 1970 | |
| 1971 | EGLBoolean eglWaitNative(EGLint engine) |
| 1972 | { |
| 1973 | return EGL_TRUE; |
| 1974 | } |
| 1975 | |
| 1976 | EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) |
| 1977 | { |
| 1978 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 1979 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1980 | |
| 1981 | egl_surface_t* d = static_cast<egl_surface_t*>(draw); |
| 1982 | if (!d->isValid()) |
| 1983 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1984 | if (d->dpy != dpy) |
| 1985 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1986 | |
| 1987 | // post the surface |
| 1988 | d->swapBuffers(); |
| 1989 | |
| 1990 | // if it's bound to a context, update the buffer |
| 1991 | if (d->ctx != EGL_NO_CONTEXT) { |
| 1992 | d->bindDrawSurface((GLES2Context*)d->ctx); |
| 1993 | // if this surface is also the read surface of the context |
| 1994 | // it is bound to, make sure to update the read buffer as well. |
| 1995 | // The EGL spec is a little unclear about this. |
| 1996 | egl_context_t* c = egl_context_t::context(d->ctx); |
| 1997 | if (c->read == draw) { |
| 1998 | d->bindReadSurface((GLES2Context*)d->ctx); |
| 1999 | } |
| 2000 | clock_t time = clock(); |
| 2001 | float elapsed = (float)(time - c->lastSwapTime) / CLOCKS_PER_SEC; |
| 2002 | c->accumulateSeconds += elapsed; |
| 2003 | c->frame++; |
| 2004 | // LOGD("agl2: eglSwapBuffers elapsed=%.2fms \n*", elapsed * 1000); |
| 2005 | if (20 == c->frame) { |
| 2006 | float avg = c->accumulateSeconds / c->frame; |
| 2007 | LOGD("\n*\n* agl2: eglSwapBuffers %u frame avg fps=%.1f elapsed=%.2fms \n*", |
| 2008 | c->frame, 1 / avg, avg * 1000); |
| 2009 | c->frame = 0; |
| 2010 | c->accumulateSeconds = 0; |
| 2011 | } |
| 2012 | c->lastSwapTime = time; |
| 2013 | } |
| 2014 | |
| 2015 | return EGL_TRUE; |
| 2016 | } |
| 2017 | |
| 2018 | EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface, |
| 2019 | NativePixmapType target) |
| 2020 | { |
| 2021 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 2022 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 2023 | // TODO: eglCopyBuffers() |
| 2024 | return EGL_FALSE; |
| 2025 | } |
| 2026 | |
| 2027 | EGLint eglGetError(void) |
| 2028 | { |
| 2029 | return getError(); |
| 2030 | } |
| 2031 | |
| 2032 | const char* eglQueryString(EGLDisplay dpy, EGLint name) |
| 2033 | { |
| 2034 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 2035 | return setError(EGL_BAD_DISPLAY, (const char*)0); |
| 2036 | |
| 2037 | switch (name) { |
| 2038 | case EGL_VENDOR: |
| 2039 | return gVendorString; |
| 2040 | case EGL_VERSION: |
| 2041 | return gVersionString; |
| 2042 | case EGL_EXTENSIONS: |
| 2043 | return gExtensionsString; |
| 2044 | case EGL_CLIENT_APIS: |
| 2045 | return gClientApiString; |
| 2046 | } |
| 2047 | return setError(EGL_BAD_PARAMETER, (const char *)0); |
| 2048 | } |
| 2049 | |
| 2050 | // ---------------------------------------------------------------------------- |
| 2051 | // EGL 1.1 |
| 2052 | // ---------------------------------------------------------------------------- |
| 2053 | |
| 2054 | EGLBoolean eglSurfaceAttrib( |
| 2055 | EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value) |
| 2056 | { |
| 2057 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 2058 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 2059 | // TODO: eglSurfaceAttrib() |
| 2060 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 2061 | } |
| 2062 | |
| 2063 | EGLBoolean eglBindTexImage( |
| 2064 | EGLDisplay dpy, EGLSurface surface, EGLint buffer) |
| 2065 | { |
| 2066 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 2067 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 2068 | // TODO: eglBindTexImage() |
| 2069 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 2070 | } |
| 2071 | |
| 2072 | EGLBoolean eglReleaseTexImage( |
| 2073 | EGLDisplay dpy, EGLSurface surface, EGLint buffer) |
| 2074 | { |
| 2075 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 2076 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 2077 | // TODO: eglReleaseTexImage() |
| 2078 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 2079 | } |
| 2080 | |
| 2081 | EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval) |
| 2082 | { |
| 2083 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 2084 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 2085 | // TODO: eglSwapInterval() |
| 2086 | return EGL_TRUE; |
| 2087 | } |
| 2088 | |
| 2089 | // ---------------------------------------------------------------------------- |
| 2090 | // EGL 1.2 |
| 2091 | // ---------------------------------------------------------------------------- |
| 2092 | |
| 2093 | EGLBoolean eglBindAPI(EGLenum api) |
| 2094 | { |
| 2095 | if (api != EGL_OPENGL_ES_API) |
| 2096 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 2097 | return EGL_TRUE; |
| 2098 | } |
| 2099 | |
| 2100 | EGLenum eglQueryAPI(void) |
| 2101 | { |
| 2102 | return EGL_OPENGL_ES_API; |
| 2103 | } |
| 2104 | |
| 2105 | EGLBoolean eglWaitClient(void) |
| 2106 | { |
| 2107 | glFinish(); |
| 2108 | return EGL_TRUE; |
| 2109 | } |
| 2110 | |
| 2111 | EGLBoolean eglReleaseThread(void) |
| 2112 | { |
| 2113 | // TODO: eglReleaseThread() |
| 2114 | return EGL_TRUE; |
| 2115 | } |
| 2116 | |
| 2117 | EGLSurface eglCreatePbufferFromClientBuffer( |
| 2118 | EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, |
| 2119 | EGLConfig config, const EGLint *attrib_list) |
| 2120 | { |
| 2121 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 2122 | return setError(EGL_BAD_DISPLAY, EGL_NO_SURFACE); |
| 2123 | // TODO: eglCreatePbufferFromClientBuffer() |
| 2124 | return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
| 2125 | } |
| 2126 | |
| 2127 | // ---------------------------------------------------------------------------- |
| 2128 | // EGL_EGLEXT_VERSION 3 |
| 2129 | // ---------------------------------------------------------------------------- |
| 2130 | |
| 2131 | void (*eglGetProcAddress (const char *procname))() |
| 2132 | { |
| 2133 | extention_map_t const * const map = gExtentionMap; |
| 2134 | for (uint32_t i=0 ; i<NELEM(gExtentionMap) ; i++) { |
| 2135 | if (!strcmp(procname, map[i].name)) { |
| 2136 | return map[i].address; |
| 2137 | } |
| 2138 | } |
| 2139 | return NULL; |
| 2140 | } |
| 2141 | |
| 2142 | EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface, |
| 2143 | const EGLint *attrib_list) |
| 2144 | { |
| 2145 | EGLBoolean result = EGL_FALSE; |
| 2146 | return result; |
| 2147 | } |
| 2148 | |
| 2149 | EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface) |
| 2150 | { |
| 2151 | EGLBoolean result = EGL_FALSE; |
| 2152 | return result; |
| 2153 | } |
| 2154 | |
| 2155 | EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, |
| 2156 | EGLClientBuffer buffer, const EGLint *attrib_list) |
| 2157 | { |
| 2158 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) { |
| 2159 | return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR); |
| 2160 | } |
| 2161 | if (ctx != EGL_NO_CONTEXT) { |
| 2162 | return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR); |
| 2163 | } |
| 2164 | if (target != EGL_NATIVE_BUFFER_ANDROID) { |
| 2165 | return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR); |
| 2166 | } |
| 2167 | |
| 2168 | android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer; |
| 2169 | |
| 2170 | if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) |
| 2171 | return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR); |
| 2172 | |
| 2173 | if (native_buffer->common.version != sizeof(android_native_buffer_t)) |
| 2174 | return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR); |
| 2175 | |
| 2176 | switch (native_buffer->format) { |
| 2177 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 2178 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 2179 | case HAL_PIXEL_FORMAT_RGB_888: |
| 2180 | case HAL_PIXEL_FORMAT_RGB_565: |
| 2181 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 2182 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 2183 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 2184 | break; |
| 2185 | default: |
| 2186 | return setError(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR); |
| 2187 | } |
| 2188 | |
| 2189 | native_buffer->common.incRef(&native_buffer->common); |
| 2190 | return (EGLImageKHR)native_buffer; |
| 2191 | } |
| 2192 | |
| 2193 | EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img) |
| 2194 | { |
| 2195 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) { |
| 2196 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 2197 | } |
| 2198 | |
| 2199 | android_native_buffer_t* native_buffer = (android_native_buffer_t*)img; |
| 2200 | |
| 2201 | if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC) |
| 2202 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 2203 | |
| 2204 | if (native_buffer->common.version != sizeof(android_native_buffer_t)) |
| 2205 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 2206 | |
| 2207 | native_buffer->common.decRef(&native_buffer->common); |
| 2208 | |
| 2209 | return EGL_TRUE; |
| 2210 | } |
| 2211 | |
| 2212 | // ---------------------------------------------------------------------------- |
| 2213 | // ANDROID extensions |
| 2214 | // ---------------------------------------------------------------------------- |
| 2215 | |
| 2216 | EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw, |
| 2217 | EGLint left, EGLint top, EGLint width, EGLint height) |
| 2218 | { |
| 2219 | if (egl_display_t::is_valid(dpy) == EGL_FALSE) |
| 2220 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 2221 | |
| 2222 | egl_surface_t* d = static_cast<egl_surface_t*>(draw); |
| 2223 | if (!d->isValid()) |
| 2224 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 2225 | if (d->dpy != dpy) |
| 2226 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 2227 | |
| 2228 | // post the surface |
| 2229 | d->setSwapRectangle(left, top, width, height); |
| 2230 | |
| 2231 | return EGL_TRUE; |
| 2232 | } |