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