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