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