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