blob: 3b7f45e7fcb87da3c31c325abe17671d2ac0d669 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 ** Copyright 2007, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17#define LOG_TAG "GLLogger"
18
19#include <ctype.h>
20#include <string.h>
21#include <errno.h>
22#include <dlfcn.h>
23
24#include <sys/ioctl.h>
25
26#if HAVE_ANDROID_OS
27#include <linux/android_pmem.h>
28#endif
29
30#include <GLES/egl.h>
31
32#include <cutils/log.h>
33#include <cutils/atomic.h>
34#include <cutils/properties.h>
35#include <cutils/memory.h>
36
37#include <utils/IMemory.h>
38#include <utils/KeyedVector.h>
39#include <utils/threads.h>
40#include <utils/IServiceManager.h>
41#include <utils/IPCThreadState.h>
42#include <utils/Parcel.h>
43
44#include <ui/EGLDisplaySurface.h>
45#include <ui/ISurfaceComposer.h>
46
47#include "gl_logger.h"
48
49#undef NELEM
50
51#define GL_LOGGER 0
52#define USE_SLOW_BINDING 0
53#define NELEM(x) (sizeof(x)/sizeof(*(x)))
54#define MAX_NUMBER_OF_GL_EXTENSIONS 32
55#define MAKE_CONFIG(_impl, _index) ((EGLConfig)(((_impl)<<24) | (_index)))
56#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
57
58// ----------------------------------------------------------------------------
59namespace android {
60// ----------------------------------------------------------------------------
61
62// EGLDisplay are global, not attached to a given thread
63static const unsigned int NUM_DISPLAYS = 1;
64static const unsigned int IMPL_HARDWARE = 0;
65static const unsigned int IMPL_SOFTWARE = 1;
66static const unsigned int IMPL_HARDWARE_CONTEXT_LOST = 2;
67static const unsigned int IMPL_SOFTWARE_CONTEXT_LOST = 3;
68static const unsigned int IMPL_NO_CONTEXT = 4;
69
70// ----------------------------------------------------------------------------
71
72struct gl_hooks_t;
73
74struct egl_connection_t
75{
76 void volatile * dso;
77 gl_hooks_t * hooks;
78 EGLint major;
79 EGLint minor;
80 int unavailable;
81};
82
83template <int MAGIC>
84struct egl_object_t
85{
86 egl_object_t() : magic(MAGIC) { }
87 ~egl_object_t() { magic = 0; }
88 bool isValid() const { return magic == MAGIC; }
89private:
90 uint32_t magic;
91};
92
93struct egl_display_t : public egl_object_t<'_dpy'>
94{
95 EGLDisplay dpys[2];
96 EGLConfig* configs[2];
97 EGLint numConfigs[2];
98 EGLint numTotalConfigs;
99 char const* extensionsString;
100 volatile int32_t refs;
101 struct strings_t {
102 char const * vendor;
103 char const * version;
104 char const * clientApi;
105 char const * extensions;
106 char const * extensions_config;
107 };
108 strings_t queryString[2];
109};
110
111struct egl_surface_t : public egl_object_t<'_srf'>
112{
113 egl_surface_t(EGLDisplay dpy, EGLSurface surface,
114 NativeWindowType window, int impl, egl_connection_t const* cnx)
115 : dpy(dpy), surface(surface), window(window), impl(impl), cnx(cnx)
116 {
117 // NOTE: window must be incRef'ed and connected already
118 }
119 ~egl_surface_t() {
120 if (window) {
121 if (window->disconnect)
122 window->disconnect(window);
123 window->decRef(window);
124 }
125 }
126 EGLDisplay dpy;
127 EGLSurface surface;
128 NativeWindowType window;
129 int impl;
130 egl_connection_t const* cnx;
131};
132
133struct egl_context_t : public egl_object_t<'_ctx'>
134{
135 egl_context_t(EGLDisplay dpy, EGLContext context,
136 int impl, egl_connection_t const* cnx)
137 : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx)
138 {
139 }
140 EGLDisplay dpy;
141 EGLContext context;
142 EGLSurface read;
143 EGLSurface draw;
144 int impl;
145 egl_connection_t const* cnx;
146};
147
148struct tls_t
149{
150 tls_t() : error(EGL_SUCCESS), ctx(0) { }
151 EGLint error;
152 EGLContext ctx;
153};
154
155
156// GL / EGL hooks
157
158typedef void(*proc_t)();
159
160struct gl_hooks_t {
161 struct gl_t {
162 #define GL_ENTRY(_r, _api, ...) _r (*_api)(__VA_ARGS__);
163 #include "gl_entries.cpp"
164 #undef GL_ENTRY
165 } gl;
166 struct egl_t {
167 #define EGL_ENTRY(_r, _api, ...) _r (*_api)(__VA_ARGS__);
168 #include "egl_entries.cpp"
169 #undef EGL_ENTRY
170 } egl;
171 struct gl_ext_t {
172 void (*extensions[MAX_NUMBER_OF_GL_EXTENSIONS])(void);
173 } ext;
174};
175
176static char const * const gl_names[] = {
177 #define GL_ENTRY(_r, _api, ...) #_api,
178 #include "gl_entries.cpp"
179 #undef GL_ENTRY
180 NULL
181};
182
183static char const * const egl_names[] = {
184 #define EGL_ENTRY(_r, _api, ...) #_api,
185 #include "egl_entries.cpp"
186 #undef EGL_ENTRY
187 NULL
188};
189
190static void gl_unimplemented() {
191 LOGE("called unimplemented OpenGL ES API");
192}
193
194// ----------------------------------------------------------------------------
195
196static egl_connection_t gEGLImpl[2];
197static egl_display_t gDisplay[NUM_DISPLAYS];
198static gl_hooks_t gHooks[5];
199static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
200static pthread_key_t gEGLThreadLocalStorageKey = -1;
201
202// ----------------------------------------------------------------------------
203
204#if defined(HAVE_ANDROID_OS) && !USE_SLOW_BINDING && !GL_LOGGER
205
206#include <sys/tls.h>
207// We have a dedicated TLS slot in bionic
208static inline void setGlThreadSpecific(gl_hooks_t const *value) {
209 ((uint32_t *)__get_tls())[TLS_SLOT_OPENGL_API] = (uint32_t)value;
210}
211static gl_hooks_t const* getGlThreadSpecific() {
212 gl_hooks_t const* hooks = (gl_hooks_t const *)(((unsigned const *)__get_tls())[TLS_SLOT_OPENGL_API]);
213 if (hooks) return hooks;
214 return &gHooks[IMPL_NO_CONTEXT];
215}
216
217#else
218
219static pthread_key_t gGLWrapperKey = -1;
220static inline void setGlThreadSpecific(gl_hooks_t const *value) {
221 pthread_setspecific(gGLWrapperKey, value);
222}
223static gl_hooks_t const* getGlThreadSpecific() {
224 gl_hooks_t const* hooks = static_cast<gl_hooks_t*>(pthread_getspecific(gGLWrapperKey));
225 if (hooks) return hooks;
226 return &gHooks[IMPL_NO_CONTEXT];
227}
228
229#endif
230
231static __attribute__((noinline))
232const char *egl_strerror(EGLint err)
233{
234 switch (err){
235 case EGL_SUCCESS: return "EGL_SUCCESS";
236 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
237 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
238 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
239 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
240 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
241 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
242 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
243 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
244 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
245 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
246 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
247 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
248 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
249 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
250 default: return "UNKNOWN";
251 }
252}
253
254static __attribute__((noinline))
255void clearTLS() {
256 if (gEGLThreadLocalStorageKey != -1) {
257 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
258 if (tls) {
259 delete tls;
260 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
261 }
262 }
263}
264
265static tls_t* getTLS()
266{
267 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
268 if (tls == 0) {
269 tls = new tls_t;
270 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
271 }
272 return tls;
273}
274
275template<typename T>
276static __attribute__((noinline))
277T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
278 if (gEGLThreadLocalStorageKey == -1) {
279 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
280 if (gEGLThreadLocalStorageKey == -1)
281 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
282 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
283 }
284 tls_t* tls = getTLS();
285 if (tls->error != error) {
286 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
287 tls->error = error;
288 }
289 return returnValue;
290}
291
292static __attribute__((noinline))
293GLint getError() {
294 if (gEGLThreadLocalStorageKey == -1)
295 return EGL_SUCCESS;
296 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
297 if (!tls) return EGL_SUCCESS;
298 GLint error = tls->error;
299 tls->error = EGL_SUCCESS;
300 return error;
301}
302
303static __attribute__((noinline))
304void setContext(EGLContext ctx) {
305 if (gEGLThreadLocalStorageKey == -1) {
306 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
307 if (gEGLThreadLocalStorageKey == -1)
308 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
309 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
310 }
311 tls_t* tls = getTLS();
312 tls->ctx = ctx;
313}
314
315static __attribute__((noinline))
316EGLContext getContext() {
317 if (gEGLThreadLocalStorageKey == -1)
318 return EGL_NO_CONTEXT;
319 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
320 if (!tls) return EGL_NO_CONTEXT;
321 return tls->ctx;
322}
323
324/*****************************************************************************/
325
326/*
327 * we provide our own allocators for the GPU regions, these
328 * allocators go through surfaceflinger
329 */
330
331static Mutex gRegionsLock;
332static request_gpu_t gRegions;
333static sp<ISurfaceComposer> gSurfaceManager;
334ISurfaceComposer* GLES_localSurfaceManager = 0;
335
336const sp<ISurfaceComposer>& getSurfaceFlinger()
337{
338 Mutex::Autolock _l(gRegionsLock);
339
340 /*
341 * There is a little bit of voodoo magic here. We want to access
342 * surfaceflinger for allocating GPU regions, however, when we are
343 * running as part of surfaceflinger, we want to bypass the
344 * service manager because surfaceflinger might not be registered yet.
345 * SurfaceFlinger will populate "GLES_localSurfaceManager" with its
346 * own address, so we can just use that.
347 */
348 if (gSurfaceManager == 0) {
349 if (GLES_localSurfaceManager) {
350 // we're running in SurfaceFlinger's context
351 gSurfaceManager = GLES_localSurfaceManager;
352 } else {
353 // we're a remote process or not part of surfaceflinger,
354 // go through the service manager
355 sp<IServiceManager> sm = defaultServiceManager();
356 if (sm != NULL) {
357 sp<IBinder> binder = sm->getService(String16("SurfaceFlinger"));
358 gSurfaceManager = interface_cast<ISurfaceComposer>(binder);
359 }
360 }
361 }
362 return gSurfaceManager;
363}
364
365class GPURevokeRequester : public BnGPUCallback
366{
367public:
368 virtual void gpuLost() {
369 LOGD("CONTEXT_LOST: Releasing GPU upon request from SurfaceFlinger.");
370 gEGLImpl[IMPL_HARDWARE].hooks = &gHooks[IMPL_HARDWARE_CONTEXT_LOST];
371 }
372};
373
374static sp<GPURevokeRequester> gRevokerCallback;
375
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800376
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700377static request_gpu_t* gpu_acquire(void* user)
378{
379 sp<ISurfaceComposer> server( getSurfaceFlinger() );
380
381 Mutex::Autolock _l(gRegionsLock);
382 if (server == NULL) {
383 return 0;
384 }
385
386 ISurfaceComposer::gpu_info_t info;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800387
388 if (gRevokerCallback == 0)
389 gRevokerCallback = new GPURevokeRequester();
390
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700391 status_t err = server->requestGPU(gRevokerCallback, &info);
392 if (err != NO_ERROR) {
393 LOGD("requestGPU returned %d", err);
394 return 0;
395 }
396
397 bool failed = false;
398 request_gpu_t* gpu = &gRegions;
399 memset(gpu, 0, sizeof(*gpu));
400
401 if (info.regs != 0) {
402 sp<IMemoryHeap> heap(info.regs->getMemory());
403 if (heap != 0) {
404 int fd = heap->heapID();
405 gpu->regs.fd = fd;
406 gpu->regs.base = info.regs->pointer();
407 gpu->regs.size = info.regs->size();
408 gpu->regs.user = info.regs.get();
409#if HAVE_ANDROID_OS
410 struct pmem_region region;
411 if (ioctl(fd, PMEM_GET_PHYS, &region) >= 0)
412 gpu->regs.phys = (void*)region.offset;
413#endif
414 info.regs->incStrong(gpu);
415 } else {
416 LOGE("GPU register handle %p is invalid!", info.regs.get());
417 failed = true;
418 }
419 }
420
421 for (size_t i=0 ; i<info.count && !failed ; i++) {
422 sp<IMemory>& region(info.regions[i].region);
423 if (region != 0) {
424 sp<IMemoryHeap> heap(region->getMemory());
425 if (heap != 0) {
426 const int fd = heap->heapID();
427 gpu->gpu[i].fd = fd;
428 gpu->gpu[i].base = region->pointer();
429 gpu->gpu[i].size = region->size();
430 gpu->gpu[i].user = region.get();
431 gpu->gpu[i].offset = info.regions[i].reserved;
432#if HAVE_ANDROID_OS
433 struct pmem_region reg;
434 if (ioctl(fd, PMEM_GET_PHYS, &reg) >= 0)
435 gpu->gpu[i].phys = (void*)reg.offset;
436#endif
437 region->incStrong(gpu);
438 } else {
439 LOGE("GPU region handle [%d, %p] is invalid!", i, region.get());
440 failed = true;
441 }
442 }
443 }
444
445 if (failed) {
446 // something went wrong, clean up everything!
447 if (gpu->regs.user) {
448 static_cast<IMemory*>(gpu->regs.user)->decStrong(gpu);
449 for (size_t i=0 ; i<info.count ; i++) {
450 if (gpu->gpu[i].user) {
451 static_cast<IMemory*>(gpu->gpu[i].user)->decStrong(gpu);
452 }
453 }
454 }
455 }
456
457 gpu->count = info.count;
458 return gpu;
459}
460
461static int gpu_release(void*, request_gpu_t* gpu)
462{
463 sp<IMemory> regs;
464
465 { // scope for lock
466 Mutex::Autolock _l(gRegionsLock);
467 regs = static_cast<IMemory*>(gpu->regs.user);
468 gpu->regs.user = 0;
469 if (regs != 0) regs->decStrong(gpu);
470
471 for (int i=0 ; i<gpu->count ; i++) {
472 sp<IMemory> r(static_cast<IMemory*>(gpu->gpu[i].user));
473 gpu->gpu[i].user = 0;
474 if (r != 0) r->decStrong(gpu);
475 }
476 }
477
478 // there is a special transaction to relinquish the GPU
479 // (it will happen automatically anyway if we don't do this)
480 Parcel data, reply;
481 // NOTE: this transaction does not require an interface token
482 regs->asBinder()->transact(1000, data, &reply);
483 return 1;
484}
485
486/*****************************************************************************/
487
488static __attribute__((noinline))
489void *load_driver(const char* driver, gl_hooks_t* hooks)
490{
491 void* dso = dlopen(driver, RTLD_NOW | RTLD_LOCAL);
492 LOGE_IF(!dso,
493 "couldn't load <%s> library (%s)",
494 driver, dlerror());
495
496 if (dso) {
497 void** curr;
498 char const * const * api;
499 gl_hooks_t::gl_t* gl = &hooks->gl;
500 curr = (void**)gl;
501 api = gl_names;
502 while (*api) {
503 void* f = dlsym(dso, *api);
504 //LOGD("<%s> @ 0x%p", *api, f);
505 if (f == NULL) {
506 //LOGW("<%s> not found in %s", *api, driver);
507 f = (void*)gl_unimplemented;
508 }
509 *curr++ = f;
510 api++;
511 }
512 gl_hooks_t::egl_t* egl = &hooks->egl;
513 curr = (void**)egl;
514 api = egl_names;
515 while (*api) {
516 void* f = dlsym(dso, *api);
517 if (f == NULL) {
518 //LOGW("<%s> not found in %s", *api, driver);
519 f = (void*)0;
520 }
521 *curr++ = f;
522 api++;
523 }
524
525 // hook this driver up with surfaceflinger if needed
526 register_gpu_t register_gpu =
527 (register_gpu_t)dlsym(dso, "oem_register_gpu");
528
529 if (register_gpu != NULL) {
530 if (getSurfaceFlinger() != 0) {
531 register_gpu(dso, gpu_acquire, gpu_release);
532 }
533 }
534 }
535 return dso;
536}
537
538template<typename T>
539static __attribute__((noinline))
540int binarySearch(
541 T const sortedArray[], int first, int last, T key)
542{
543 while (first <= last) {
544 int mid = (first + last) / 2;
545 if (key > sortedArray[mid]) {
546 first = mid + 1;
547 } else if (key < sortedArray[mid]) {
548 last = mid - 1;
549 } else {
550 return mid;
551 }
552 }
553 return -1;
554}
555
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800556static EGLint configToUniqueId(egl_display_t const* dp, int i, int index)
557{
558 // NOTE: this mapping works only if we have no more than two EGLimpl
559 return (i>0 ? dp->numConfigs[0] : 0) + index;
560}
561
562static void uniqueIdToConfig(egl_display_t const* dp, EGLint configId,
563 int& i, int& index)
564{
565 // NOTE: this mapping works only if we have no more than two EGLimpl
566 size_t numConfigs = dp->numConfigs[0];
567 i = configId / numConfigs;
568 index = configId % numConfigs;
569}
570
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700571static int cmp_configs(const void* a, const void *b)
572{
573 EGLConfig c0 = *(EGLConfig const *)a;
574 EGLConfig c1 = *(EGLConfig const *)b;
575 return c0<c1 ? -1 : (c0>c1 ? 1 : 0);
576}
577
578static char const * const gVendorString = "Android";
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800579static char const * const gVersionString = "1.3 Android META-EGL";
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700580static char const * const gClientApiString = "OpenGL ES";
581
582struct extention_map_t {
583 const char* name;
584 void (*address)(void);
585};
586
587static const extention_map_t gExtentionMap[] = {
588 { "eglSwapRectangleANDROID", (void(*)())&eglSwapRectangleANDROID },
589 { "eglQueryStringConfigANDROID", (void(*)())&eglQueryStringConfigANDROID },
590};
591
592static extention_map_t gGLExtentionMap[MAX_NUMBER_OF_GL_EXTENSIONS];
593
594static void(*findProcAddress(const char* name,
595 const extention_map_t* map, size_t n))()
596{
597 for (uint32_t i=0 ; i<n ; i++) {
598 if (!strcmp(name, map[i].name)) {
599 return map[i].address;
600 }
601 }
602 return NULL;
603}
604
605// ----------------------------------------------------------------------------
606}; // namespace android
607// ----------------------------------------------------------------------------
608
609using namespace android;
610
611
612// ----------------------------------------------------------------------------
613// extensions for the framework
614// ----------------------------------------------------------------------------
615
616void glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
617 const GLvoid *ptr, GLsizei count) {
618 glColorPointer(size, type, stride, ptr);
619}
620void glNormalPointerBounds(GLenum type, GLsizei stride,
621 const GLvoid *pointer, GLsizei count) {
622 glNormalPointer(type, stride, pointer);
623}
624void glTexCoordPointerBounds(GLint size, GLenum type,
625 GLsizei stride, const GLvoid *pointer, GLsizei count) {
626 glTexCoordPointer(size, type, stride, pointer);
627}
628void glVertexPointerBounds(GLint size, GLenum type,
629 GLsizei stride, const GLvoid *pointer, GLsizei count) {
630 glVertexPointer(size, type, stride, pointer);
631}
632
633
634// ----------------------------------------------------------------------------
635// Actual GL wrappers
636// ----------------------------------------------------------------------------
637
638#if __OPTIMIZE__ && defined(__arm__) && !defined(__thumb__) && !USE_SLOW_BINDING && !GL_LOGGER
639
640 #define API_ENTRY(_api) __attribute__((naked)) _api
641 #define CALL_GL_API(_api, ...) \
642 asm volatile( \
643 "mov r12, #0xFFFF0FFF \n" \
644 "ldr r12, [r12, #-15] \n" \
645 "ldr r12, [r12, %[tls]] \n" \
646 "cmp r12, #0 \n" \
647 "ldrne pc, [r12, %[api]] \n" \
648 "bx lr \n" \
649 : \
650 : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
651 [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
652 : \
653 );
654
655 #define CALL_GL_API_RETURN(_api, ...) \
656 CALL_GL_API(_api, __VA_ARGS__) \
657 return 0; // placate gcc's warnings. never reached.
658
659#else
660
661 #define API_ENTRY(_api) _api
662 #if GL_LOGGER
663
664 #define CALL_GL_API(_api, ...) \
665 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
666 log_##_api(__VA_ARGS__); \
667 _c->_api(__VA_ARGS__);
668
669 #define CALL_GL_API_RETURN(_api, ...) \
670 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
671 log_##_api(__VA_ARGS__); \
672 return _c->_api(__VA_ARGS__)
673
674 #else
675
676 #define CALL_GL_API(_api, ...) \
677 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
678 _c->_api(__VA_ARGS__);
679
680 #define CALL_GL_API_RETURN(_api, ...) \
681 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
682 return _c->_api(__VA_ARGS__)
683
684 #endif
685
686#endif
687
688#include "gl_api.cpp"
689
690#undef API_ENTRY
691#undef CALL_GL_API
692#undef CALL_GL_API_RETURN
693
694// ----------------------------------------------------------------------------
695namespace android {
696// ----------------------------------------------------------------------------
697
698static int gl_context_lost() {
699 setGlThreadSpecific(&gHooks[IMPL_HARDWARE_CONTEXT_LOST]);
700 return 0;
701}
702static int egl_context_lost() {
703 setGlThreadSpecific(&gHooks[IMPL_HARDWARE_CONTEXT_LOST]);
704 return EGL_FALSE;
705}
706static EGLBoolean egl_context_lost_swap_buffers(void*, void*) {
707 usleep(100000); // don't use all the CPU
708 setGlThreadSpecific(&gHooks[IMPL_HARDWARE_CONTEXT_LOST]);
709 return EGL_FALSE;
710}
711static GLint egl_context_lost_get_error() {
712 return EGL_CONTEXT_LOST;
713}
714static int ext_context_lost() {
715 return 0;
716}
717
718static void gl_no_context() {
719 LOGE("call to OpenGL ES API with no current context");
720}
721static void early_egl_init(void)
722{
723#if !defined(HAVE_ANDROID_OS) || USE_SLOW_BINDING || GL_LOGGER
724 pthread_key_create(&gGLWrapperKey, NULL);
725#endif
726 uint32_t addr = (uint32_t)((void*)gl_no_context);
727 android_memset32((uint32_t*)(void*)&gHooks[IMPL_NO_CONTEXT], addr, sizeof(gHooks[IMPL_NO_CONTEXT]));
728 setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
729}
730
731static pthread_once_t once_control = PTHREAD_ONCE_INIT;
732static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
733
734
735static inline
736egl_display_t* get_display(EGLDisplay dpy)
737{
738 uintptr_t index = uintptr_t(dpy)-1U;
739 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
740}
741
742static inline
743egl_surface_t* get_surface(EGLSurface surface)
744{
745 egl_surface_t* s = (egl_surface_t *)surface;
746 return s;
747}
748
749static inline
750egl_context_t* get_context(EGLContext context)
751{
752 egl_context_t* c = (egl_context_t *)context;
753 return c;
754}
755
756static egl_connection_t* validate_display_config(
757 EGLDisplay dpy, EGLConfig config,
758 egl_display_t const*& dp, int& impl, int& index)
759{
760 dp = get_display(dpy);
761 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
762
763 impl = uintptr_t(config)>>24;
764 if (uint32_t(impl) >= 2) {
765 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
766 }
767 index = uintptr_t(config) & 0xFFFFFF;
768 if (index >= dp->numConfigs[impl]) {
769 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
770 }
771 egl_connection_t* const cnx = &gEGLImpl[impl];
772 if (cnx->dso == 0) {
773 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
774 }
775 return cnx;
776}
777
778static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
779{
780 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
781 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
782 if (!get_display(dpy)->isValid())
783 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
784 if (!ctx) // TODO: make sure context is a valid object
785 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
786 if (!get_context(ctx)->isValid())
787 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
788 return EGL_TRUE;
789}
790
791static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
792{
793 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
794 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
795 if (!get_display(dpy)->isValid())
796 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
797 if (!surface) // TODO: make sure surface is a valid object
798 return setError(EGL_BAD_SURFACE, EGL_FALSE);
799 if (!get_surface(surface)->isValid())
800 return setError(EGL_BAD_SURFACE, EGL_FALSE);
801 return EGL_TRUE;
802}
803
804static void add_extension(egl_display_t* dp, char const*& p, const char* ext)
805{
806 if (!strstr(p, ext)) {
807 p = (char const*)realloc((void*)p, strlen(p) + 1 + strlen(ext) + 1);
808 strcat((char*)p, " ");
809 strcat((char*)p, ext);
810 }
811 if (!strstr(dp->extensionsString, ext)) {
812 char const*& es = dp->extensionsString;
813 es = (char const*)realloc((void*)es, strlen(es) + 1 + strlen(ext) + 1);
814 strcat((char*)es, " ");
815 strcat((char*)es, ext);
816 }
817}
818
819// ----------------------------------------------------------------------------
820}; // namespace android
821// ----------------------------------------------------------------------------
822
823EGLDisplay eglGetDisplay(NativeDisplayType display)
824{
825 if (sEarlyInitState) {
826 return EGL_NO_DISPLAY;
827 }
828
829 uint32_t index = uint32_t(display);
830 if (index >= NUM_DISPLAYS) {
831 return EGL_NO_DISPLAY;
832 }
833
834 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
835 egl_display_t* d = &gDisplay[index];
836
837 // dynamically load all our EGL implementations for that display
838 // and call into the real eglGetGisplay()
839 egl_connection_t* cnx = &gEGLImpl[IMPL_SOFTWARE];
840 if (cnx->dso == 0) {
841 cnx->hooks = &gHooks[IMPL_SOFTWARE];
842 cnx->dso = load_driver("libagl.so", cnx->hooks);
843 }
844 if (cnx->dso && d->dpys[IMPL_SOFTWARE]==EGL_NO_DISPLAY) {
845 d->dpys[IMPL_SOFTWARE] = cnx->hooks->egl.eglGetDisplay(display);
846 LOGE_IF(d->dpys[IMPL_SOFTWARE]==EGL_NO_DISPLAY,
847 "No EGLDisplay for software EGL!");
848 }
849
850 cnx = &gEGLImpl[IMPL_HARDWARE];
851 if (cnx->dso == 0 && cnx->unavailable == 0) {
852 char value[PROPERTY_VALUE_MAX];
853 property_get("debug.egl.hw", value, "1");
854 if (atoi(value) != 0) {
855 cnx->hooks = &gHooks[IMPL_HARDWARE];
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800856 property_get("debug.egl.profiler", value, "0");
857 if (atoi(value) == 0) {
858 cnx->dso = load_driver("libhgl.so", cnx->hooks);
859 } else {
860 LOGW("Using instrumented h/w OpenGL ES library");
861 cnx->dso = load_driver("libhgld.so", cnx->hooks);
862 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700863 } else {
864 LOGD("3D hardware acceleration is disabled");
865 }
866 }
867 if (cnx->dso && d->dpys[IMPL_HARDWARE]==EGL_NO_DISPLAY) {
868 android_memset32(
869 (uint32_t*)(void*)&gHooks[IMPL_HARDWARE_CONTEXT_LOST].gl,
870 (uint32_t)((void*)gl_context_lost),
871 sizeof(gHooks[IMPL_HARDWARE_CONTEXT_LOST].gl));
872 android_memset32(
873 (uint32_t*)(void*)&gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl,
874 (uint32_t)((void*)egl_context_lost),
875 sizeof(gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl));
876 android_memset32(
877 (uint32_t*)(void*)&gHooks[IMPL_HARDWARE_CONTEXT_LOST].ext,
878 (uint32_t)((void*)ext_context_lost),
879 sizeof(gHooks[IMPL_HARDWARE_CONTEXT_LOST].ext));
880
881 gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl.eglSwapBuffers =
882 egl_context_lost_swap_buffers;
883
884 gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl.eglGetError =
885 egl_context_lost_get_error;
886
887 gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl.eglTerminate =
888 gHooks[IMPL_HARDWARE].egl.eglTerminate;
889
890 d->dpys[IMPL_HARDWARE] = cnx->hooks->egl.eglGetDisplay(display);
891 if (d->dpys[IMPL_HARDWARE] == EGL_NO_DISPLAY) {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800892 LOGE("h/w accelerated eglGetDisplay() failed (%s)",
893 egl_strerror(cnx->hooks->egl.eglGetError()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700894 dlclose((void*)cnx->dso);
895 cnx->dso = 0;
896 // in case of failure, we want to make sure we don't try again
897 // as it's expensive.
898 cnx->unavailable = 1;
899 }
900 }
901
902 return dpy;
903}
904
905// ----------------------------------------------------------------------------
906// Initialization
907// ----------------------------------------------------------------------------
908
909EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
910{
911 egl_display_t * const dp = get_display(dpy);
912 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
913
914 if (android_atomic_inc(&dp->refs) > 0) {
915 if (major != NULL) *major = 1;
916 if (minor != NULL) *minor = 2;
917 return EGL_TRUE;
918 }
919
920 setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
921
922 // initialize each EGL and
923 // build our own extension string first, based on the extension we know
924 // and the extension supported by our client implementation
925 dp->extensionsString = strdup("EGL_ANDROID_query_string_config");
926 for (int i=0 ; i<2 ; i++) {
927 egl_connection_t* const cnx = &gEGLImpl[i];
928 cnx->major = -1;
929 cnx->minor = -1;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800930 if (!cnx->dso)
931 continue;
932
933 if (cnx->hooks->egl.eglInitialize(
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700934 dp->dpys[i], &cnx->major, &cnx->minor)) {
935
936 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
937 // i, dp->dpys[i], cnx->major, cnx->minor, cnx);
938
939 // get the query-strings for this display for each implementation
940 dp->queryString[i].vendor =
941 cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_VENDOR);
942 dp->queryString[i].version =
943 cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_VERSION);
944 dp->queryString[i].extensions = strdup(
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800945 cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_EXTENSIONS));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700946 dp->queryString[i].clientApi =
947 cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_CLIENT_APIS);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800948
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700949 // Dynamically insert extensions we know about
950 if (cnx->hooks->egl.eglSwapRectangleANDROID)
951 add_extension(dp, dp->queryString[i].extensions,
952 "EGL_ANDROID_swap_rectangle");
953
954 if (cnx->hooks->egl.eglQueryStringConfigANDROID)
955 add_extension(dp, dp->queryString[i].extensions,
956 "EGL_ANDROID_query_string_config");
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800957 } else {
958 LOGD("%d: eglInitialize() failed (%s)",
959 i, egl_strerror(cnx->hooks->egl.eglGetError()));
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700960 }
961 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800962
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700963 // Build the extension list that depends on the current config.
964 // It is the intersection of our extension list and the
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800965 // underlying EGL's extensions list
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700966 EGLBoolean res = EGL_FALSE;
967 for (int i=0 ; i<2 ; i++) {
968 egl_connection_t* const cnx = &gEGLImpl[i];
969 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
970 char const* const their_extensions = dp->queryString[i].extensions;
971 char* our_extensions = strdup(dp->extensionsString);
972 char* const our_extensions_org = our_extensions;
973 char* extensions_config = (char*)calloc(strlen(our_extensions)+2, 1);
974 char* p;
975 do {
976 p = strchr(our_extensions, ' ');
977 if (p) *p++ = 0;
978 else p = strchr(our_extensions, 0);
979 if (strstr(their_extensions, our_extensions)) {
980 strcat(extensions_config, our_extensions);
981 strcat(extensions_config, " ");
982 }
983 our_extensions = p;
984 } while (*p);
985 free((void*)our_extensions_org);
986
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800987 // remove the trailing white space
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700988 if (extensions_config[0] != 0) {
989 size_t l = strlen(extensions_config) - 1; // new size
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800990 extensions_config[l] = 0; // remove the trailing white space
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700991 extensions_config = (char*)realloc(extensions_config, l+1);
992 } else {
993 extensions_config = (char*)realloc(extensions_config, 1);
994 }
995 dp->queryString[i].extensions_config = extensions_config;
996
997 EGLint n;
998 if (cnx->hooks->egl.eglGetConfigs(dp->dpys[i], 0, 0, &n)) {
999 dp->configs[i] = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
1000 if (dp->configs[i]) {
1001 if (cnx->hooks->egl.eglGetConfigs(
1002 dp->dpys[i], dp->configs[i], n, &dp->numConfigs[i]))
1003 {
1004 // sort the configurations so we can do binary searches
1005 qsort( dp->configs[i],
1006 dp->numConfigs[i],
1007 sizeof(EGLConfig), cmp_configs);
1008
1009 dp->numTotalConfigs += n;
1010 res = EGL_TRUE;
1011 }
1012 }
1013 }
1014 }
1015 }
1016
1017 if (res == EGL_TRUE) {
1018 if (major != NULL) *major = 1;
1019 if (minor != NULL) *minor = 2;
1020 return EGL_TRUE;
1021 }
1022 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
1023}
1024
1025EGLBoolean eglTerminate(EGLDisplay dpy)
1026{
1027 egl_display_t* const dp = get_display(dpy);
1028 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1029 if (android_atomic_dec(&dp->refs) != 1)
1030 return EGL_TRUE;
1031
1032 EGLBoolean res = EGL_FALSE;
1033 for (int i=0 ; i<2 ; i++) {
1034 egl_connection_t* const cnx = &gEGLImpl[i];
1035 if (cnx->dso) {
1036 cnx->hooks->egl.eglTerminate(dp->dpys[i]);
1037
1038 /* REVISIT: it's unclear what to do if eglTerminate() fails,
1039 * on one end we shouldn't care, on the other end if it fails
1040 * it might not be safe to call dlclose() (there could be some
1041 * threads around). */
1042
1043 free(dp->configs[i]);
1044 free((void*)dp->queryString[i].extensions_config);
1045 free((void*)dp->queryString[i].extensions);
1046 dp->numConfigs[i] = 0;
1047 dp->dpys[i] = EGL_NO_DISPLAY;
1048 dlclose((void*)cnx->dso);
1049 cnx->dso = 0;
1050 res = EGL_TRUE;
1051 }
1052 }
1053 free((void*)dp->extensionsString);
1054 dp->extensionsString = 0;
1055 dp->numTotalConfigs = 0;
1056 clearTLS();
1057 return res;
1058}
1059
1060// ----------------------------------------------------------------------------
1061// configuration
1062// ----------------------------------------------------------------------------
1063
1064EGLBoolean eglGetConfigs( EGLDisplay dpy,
1065 EGLConfig *configs,
1066 EGLint config_size, EGLint *num_config)
1067{
1068 egl_display_t const * const dp = get_display(dpy);
1069 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1070
1071 GLint numConfigs = dp->numTotalConfigs;
1072 if (!configs) {
1073 *num_config = numConfigs;
1074 return EGL_TRUE;
1075 }
1076 GLint n = 0;
1077 for (int j=0 ; j<2 ; j++) {
1078 for (int i=0 ; i<dp->numConfigs[j] && config_size ; i++) {
1079 *configs++ = MAKE_CONFIG(j, i);
1080 config_size--;
1081 n++;
1082 }
1083 }
1084
1085 *num_config = n;
1086 return EGL_TRUE;
1087}
1088
1089EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1090 EGLConfig *configs, EGLint config_size,
1091 EGLint *num_config)
1092{
1093 egl_display_t const * const dp = get_display(dpy);
1094 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1095
1096 if (configs == 0) {
1097 *num_config = 0;
1098 return EGL_TRUE;
1099 }
1100
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08001101 EGLint n;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001102 EGLBoolean res = EGL_FALSE;
1103 *num_config = 0;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08001104
1105
1106 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
1107 // to do this, we have to go through the attrib_list array once
1108 // to figure out both its size and if it contains an EGL_CONFIG_ID
1109 // key. If so, the full array is copied and patched.
1110 // NOTE: we assume that there can be only one occurrence
1111 // of EGL_CONFIG_ID.
1112
1113 EGLint patch_index = -1;
1114 GLint attr;
1115 size_t size = 0;
1116 while ((attr=attrib_list[size])) {
1117 if (attr == EGL_CONFIG_ID)
1118 patch_index = size;
1119 size += 2;
1120 }
1121 if (patch_index >= 0) {
1122 size += 2; // we need copy the sentinel as well
1123 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
1124 if (new_list == 0)
1125 return setError(EGL_BAD_ALLOC, EGL_FALSE);
1126 memcpy(new_list, attrib_list, size*sizeof(EGLint));
1127
1128 // patch the requested EGL_CONFIG_ID
1129 int i, index;
1130 EGLint& configId(new_list[patch_index+1]);
1131 uniqueIdToConfig(dp, configId, i, index);
1132
1133 egl_connection_t* const cnx = &gEGLImpl[i];
1134 if (cnx->dso) {
1135 cnx->hooks->egl.eglGetConfigAttrib(
1136 dp->dpys[i], dp->configs[i][index],
1137 EGL_CONFIG_ID, &configId);
1138
1139 // and switch to the new list
1140 attrib_list = const_cast<const EGLint *>(new_list);
1141
1142 // At this point, the only configuration that can match is
1143 // dp->configs[i][index], however, we don't know if it would be
1144 // rejected because of the other attributes, so we do have to call
1145 // cnx->hooks->egl.eglChooseConfig() -- but we don't have to loop
1146 // through all the EGLimpl[].
1147 // We also know we can only get a single config back, and we know
1148 // which one.
1149
1150 res = cnx->hooks->egl.eglChooseConfig(
1151 dp->dpys[i], attrib_list, configs, config_size, &n);
1152 if (res && n>0) {
1153 // n has to be 0 or 1, by construction, and we already know
1154 // which config it will return (since there can be only one).
1155 configs[0] = MAKE_CONFIG(i, index);
1156 *num_config = 1;
1157 }
1158 }
1159
1160 free(const_cast<EGLint *>(attrib_list));
1161 return res;
1162 }
1163
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001164 for (int i=0 ; i<2 ; i++) {
1165 egl_connection_t* const cnx = &gEGLImpl[i];
1166 if (cnx->dso) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001167 if (cnx->hooks->egl.eglChooseConfig(
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08001168 dp->dpys[i], attrib_list, configs, config_size, &n)) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001169 // now we need to convert these client EGLConfig to our
1170 // internal EGLConfig format. This is done in O(n log n).
1171 for (int j=0 ; j<n ; j++) {
1172 int index = binarySearch<EGLConfig>(
1173 dp->configs[i], 0, dp->numConfigs[i]-1, configs[j]);
1174 if (index >= 0) {
1175 configs[j] = MAKE_CONFIG(i, index);
1176 } else {
1177 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1178 }
1179 }
1180 configs += n;
1181 config_size -= n;
1182 *num_config += n;
1183 res = EGL_TRUE;
1184 }
1185 }
1186 }
1187 return res;
1188}
1189
1190EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1191 EGLint attribute, EGLint *value)
1192{
1193 egl_display_t const* dp = 0;
1194 int i=0, index=0;
1195 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1196 if (!cnx) return EGL_FALSE;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08001197
1198 if (attribute == EGL_CONFIG_ID) {
1199 // EGL_CONFIG_IDs must be unique, just use the order of the selected
1200 // EGLConfig.
1201 *value = configToUniqueId(dp, i, index);
1202 return EGL_TRUE;
1203 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001204 return cnx->hooks->egl.eglGetConfigAttrib(
1205 dp->dpys[i], dp->configs[i][index], attribute, value);
1206}
1207
1208// ----------------------------------------------------------------------------
1209// surfaces
1210// ----------------------------------------------------------------------------
1211
1212EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1213 NativeWindowType window,
1214 const EGLint *attrib_list)
1215{
1216 egl_display_t const* dp = 0;
1217 int i=0, index=0;
1218 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1219 if (cnx) {
1220 // window must be connected upon calling underlying
1221 // eglCreateWindowSurface
1222 if (window) {
1223 window->incRef(window);
1224 if (window->connect)
1225 window->connect(window);
1226 }
1227
1228 EGLSurface surface = cnx->hooks->egl.eglCreateWindowSurface(
1229 dp->dpys[i], dp->configs[i][index], window, attrib_list);
1230 if (surface != EGL_NO_SURFACE) {
1231 egl_surface_t* s = new egl_surface_t(dpy, surface, window, i, cnx);
1232 return s;
1233 }
1234
1235 // something went wrong, disconnect and free window
1236 // (will disconnect() automatically)
1237 if (window) {
1238 window->decRef(window);
1239 }
1240 }
1241 return EGL_NO_SURFACE;
1242}
1243
1244EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1245 NativePixmapType pixmap,
1246 const EGLint *attrib_list)
1247{
1248 egl_display_t const* dp = 0;
1249 int i=0, index=0;
1250 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1251 if (cnx) {
1252 EGLSurface surface = cnx->hooks->egl.eglCreatePixmapSurface(
1253 dp->dpys[i], dp->configs[i][index], pixmap, attrib_list);
1254 if (surface != EGL_NO_SURFACE) {
1255 egl_surface_t* s = new egl_surface_t(dpy, surface, NULL, i, cnx);
1256 return s;
1257 }
1258 }
1259 return EGL_NO_SURFACE;
1260}
1261
1262EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1263 const EGLint *attrib_list)
1264{
1265 egl_display_t const* dp = 0;
1266 int i=0, index=0;
1267 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1268 if (cnx) {
1269 EGLSurface surface = cnx->hooks->egl.eglCreatePbufferSurface(
1270 dp->dpys[i], dp->configs[i][index], attrib_list);
1271 if (surface != EGL_NO_SURFACE) {
1272 egl_surface_t* s = new egl_surface_t(dpy, surface, NULL, i, cnx);
1273 return s;
1274 }
1275 }
1276 return EGL_NO_SURFACE;
1277}
1278
1279EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1280{
1281 if (!validate_display_surface(dpy, surface))
1282 return EGL_FALSE;
1283 egl_display_t const * const dp = get_display(dpy);
1284 egl_surface_t const * const s = get_surface(surface);
1285
1286 EGLBoolean result = s->cnx->hooks->egl.eglDestroySurface(
1287 dp->dpys[s->impl], s->surface);
1288
1289 delete s;
1290 return result;
1291}
1292
1293EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1294 EGLint attribute, EGLint *value)
1295{
1296 if (!validate_display_surface(dpy, surface))
1297 return EGL_FALSE;
1298 egl_display_t const * const dp = get_display(dpy);
1299 egl_surface_t const * const s = get_surface(surface);
1300
1301 return s->cnx->hooks->egl.eglQuerySurface(
1302 dp->dpys[s->impl], s->surface, attribute, value);
1303}
1304
1305// ----------------------------------------------------------------------------
1306// contextes
1307// ----------------------------------------------------------------------------
1308
1309EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1310 EGLContext share_list, const EGLint *attrib_list)
1311{
1312 egl_display_t const* dp = 0;
1313 int i=0, index=0;
1314 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1315 if (cnx) {
1316 EGLContext context = cnx->hooks->egl.eglCreateContext(
1317 dp->dpys[i], dp->configs[i][index], share_list, attrib_list);
1318 if (context != EGL_NO_CONTEXT) {
1319 egl_context_t* c = new egl_context_t(dpy, context, i, cnx);
1320 return c;
1321 }
1322 }
1323 return EGL_NO_CONTEXT;
1324}
1325
1326EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1327{
1328 if (!validate_display_context(dpy, ctx))
1329 return EGL_FALSE;
1330 egl_display_t const * const dp = get_display(dpy);
1331 egl_context_t * const c = get_context(ctx);
1332 EGLBoolean result = c->cnx->hooks->egl.eglDestroyContext(
1333 dp->dpys[c->impl], c->context);
1334 delete c;
1335 return result;
1336}
1337
1338EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1339 EGLSurface read, EGLContext ctx)
1340{
1341 egl_display_t const * const dp = get_display(dpy);
1342 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1343
1344 if (read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE &&
1345 ctx == EGL_NO_CONTEXT)
1346 {
1347 EGLBoolean result = EGL_TRUE;
1348 ctx = getContext();
1349 if (ctx) {
1350 egl_context_t * const c = get_context(ctx);
1351 result = c->cnx->hooks->egl.eglMakeCurrent(dp->dpys[c->impl], 0, 0, 0);
1352 if (result == EGL_TRUE) {
1353 setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
1354 setContext(EGL_NO_CONTEXT);
1355 }
1356 }
1357 return result;
1358 }
1359
1360 if (!validate_display_context(dpy, ctx))
1361 return EGL_FALSE;
1362
1363 egl_context_t * const c = get_context(ctx);
1364 if (draw != EGL_NO_SURFACE) {
1365 egl_surface_t const * d = get_surface(draw);
1366 if (!d) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1367 if (d->impl != c->impl)
1368 return setError(EGL_BAD_MATCH, EGL_FALSE);
1369 draw = d->surface;
1370 }
1371 if (read != EGL_NO_SURFACE) {
1372 egl_surface_t const * r = get_surface(read);
1373 if (!r) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1374 if (r->impl != c->impl)
1375 return setError(EGL_BAD_MATCH, EGL_FALSE);
1376 read = r->surface;
1377 }
1378 EGLBoolean result = c->cnx->hooks->egl.eglMakeCurrent(
1379 dp->dpys[c->impl], draw, read, c->context);
1380
1381 if (result == EGL_TRUE) {
1382 setGlThreadSpecific(c->cnx->hooks);
1383 setContext(ctx);
1384 c->read = read;
1385 c->draw = draw;
1386 }
1387 return result;
1388}
1389
1390
1391EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1392 EGLint attribute, EGLint *value)
1393{
1394 if (!validate_display_context(dpy, ctx))
1395 return EGL_FALSE;
1396
1397 egl_display_t const * const dp = get_display(dpy);
1398 egl_context_t * const c = get_context(ctx);
1399
1400 return c->cnx->hooks->egl.eglQueryContext(
1401 dp->dpys[c->impl], c->context, attribute, value);
1402}
1403
1404EGLContext eglGetCurrentContext(void)
1405{
1406 EGLContext ctx = getContext();
1407 return ctx;
1408}
1409
1410EGLSurface eglGetCurrentSurface(EGLint readdraw)
1411{
1412 EGLContext ctx = getContext();
1413 if (ctx) {
1414 egl_context_t const * const c = get_context(ctx);
1415 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1416 switch (readdraw) {
1417 case EGL_READ: return c->read;
1418 case EGL_DRAW: return c->draw;
1419 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1420 }
1421 }
1422 return EGL_NO_SURFACE;
1423}
1424
1425EGLDisplay eglGetCurrentDisplay(void)
1426{
1427 EGLContext ctx = getContext();
1428 if (ctx) {
1429 egl_context_t const * const c = get_context(ctx);
1430 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1431 return c->dpy;
1432 }
1433 return EGL_NO_DISPLAY;
1434}
1435
1436EGLBoolean eglWaitGL(void)
1437{
1438 EGLBoolean res = EGL_TRUE;
1439 EGLContext ctx = getContext();
1440 if (ctx) {
1441 egl_context_t const * const c = get_context(ctx);
1442 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1443 if (uint32_t(c->impl)>=2)
1444 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1445 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1446 if (!cnx->dso)
1447 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1448 res = cnx->hooks->egl.eglWaitGL();
1449 }
1450 return res;
1451}
1452
1453EGLBoolean eglWaitNative(EGLint engine)
1454{
1455 EGLBoolean res = EGL_TRUE;
1456 EGLContext ctx = getContext();
1457 if (ctx) {
1458 egl_context_t const * const c = get_context(ctx);
1459 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1460 if (uint32_t(c->impl)>=2)
1461 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1462 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1463 if (!cnx->dso)
1464 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1465 res = cnx->hooks->egl.eglWaitNative(engine);
1466 }
1467 return res;
1468}
1469
1470EGLint eglGetError(void)
1471{
1472 EGLint result = EGL_SUCCESS;
1473 for (int i=0 ; i<2 ; i++) {
1474 EGLint err = EGL_SUCCESS;
1475 egl_connection_t* const cnx = &gEGLImpl[i];
1476 if (cnx->dso)
1477 err = cnx->hooks->egl.eglGetError();
1478 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1479 result = err;
1480 }
1481 if (result == EGL_SUCCESS)
1482 result = getError();
1483 return result;
1484}
1485
1486void (*eglGetProcAddress(const char *procname))()
1487{
1488 void (*addr)();
1489 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1490 if (addr) return addr;
1491
1492 return NULL; // TODO: finish implementation below
1493
1494 addr = findProcAddress(procname, gGLExtentionMap, NELEM(gGLExtentionMap));
1495 if (addr) return addr;
1496
1497 addr = 0;
1498 int slot = -1;
1499 for (int i=0 ; i<2 ; i++) {
1500 egl_connection_t* const cnx = &gEGLImpl[i];
1501 if (cnx->dso) {
1502 if (cnx->hooks->egl.eglGetProcAddress) {
1503 addr = cnx->hooks->egl.eglGetProcAddress(procname);
1504 if (addr) {
1505 if (slot == -1) {
1506 slot = 0; // XXX: find free slot
1507 if (slot == -1) {
1508 addr = 0;
1509 break;
1510 }
1511 }
1512 cnx->hooks->ext.extensions[slot] = addr;
1513 }
1514 }
1515 }
1516 }
1517
1518 if (slot >= 0) {
1519 addr = 0; // XXX: address of stub 'slot'
1520 gGLExtentionMap[slot].name = strdup(procname);
1521 gGLExtentionMap[slot].address = addr;
1522 }
1523
1524 return addr;
1525
1526
1527 /*
1528 * TODO: For OpenGL ES extensions, we must generate a stub
1529 * that looks like
1530 * mov r12, #0xFFFF0FFF
1531 * ldr r12, [r12, #-15]
1532 * ldr r12, [r12, #TLS_SLOT_OPENGL_API*4]
1533 * mov r12, [r12, #api_offset]
1534 * ldrne pc, r12
1535 * mov pc, #unsupported_extension
1536 *
1537 * and write the address of the extension in *all*
1538 * gl_hooks_t::gl_ext_t at offset "api_offset" from gl_hooks_t
1539 *
1540 */
1541}
1542
1543EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1544{
1545 if (!validate_display_surface(dpy, draw))
1546 return EGL_FALSE;
1547 egl_display_t const * const dp = get_display(dpy);
1548 egl_surface_t const * const s = get_surface(draw);
1549 return s->cnx->hooks->egl.eglSwapBuffers(dp->dpys[s->impl], s->surface);
1550}
1551
1552EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1553 NativePixmapType target)
1554{
1555 if (!validate_display_surface(dpy, surface))
1556 return EGL_FALSE;
1557 egl_display_t const * const dp = get_display(dpy);
1558 egl_surface_t const * const s = get_surface(surface);
1559 return s->cnx->hooks->egl.eglCopyBuffers(
1560 dp->dpys[s->impl], s->surface, target);
1561}
1562
1563const char* eglQueryString(EGLDisplay dpy, EGLint name)
1564{
1565 egl_display_t const * const dp = get_display(dpy);
1566 switch (name) {
1567 case EGL_VENDOR:
1568 return gVendorString;
1569 case EGL_VERSION:
1570 return gVersionString;
1571 case EGL_EXTENSIONS:
1572 return dp->extensionsString;
1573 case EGL_CLIENT_APIS:
1574 return gClientApiString;
1575 }
1576 return setError(EGL_BAD_PARAMETER, (const char *)0);
1577}
1578
1579
1580// ----------------------------------------------------------------------------
1581// EGL 1.1
1582// ----------------------------------------------------------------------------
1583
1584EGLBoolean eglSurfaceAttrib(
1585 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1586{
1587 if (!validate_display_surface(dpy, surface))
1588 return EGL_FALSE;
1589 egl_display_t const * const dp = get_display(dpy);
1590 egl_surface_t const * const s = get_surface(surface);
1591 if (s->cnx->hooks->egl.eglSurfaceAttrib) {
1592 return s->cnx->hooks->egl.eglSurfaceAttrib(
1593 dp->dpys[s->impl], s->surface, attribute, value);
1594 }
1595 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1596}
1597
1598EGLBoolean eglBindTexImage(
1599 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1600{
1601 if (!validate_display_surface(dpy, surface))
1602 return EGL_FALSE;
1603 egl_display_t const * const dp = get_display(dpy);
1604 egl_surface_t const * const s = get_surface(surface);
1605 if (s->cnx->hooks->egl.eglBindTexImage) {
1606 return s->cnx->hooks->egl.eglBindTexImage(
1607 dp->dpys[s->impl], s->surface, buffer);
1608 }
1609 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1610}
1611
1612EGLBoolean eglReleaseTexImage(
1613 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1614{
1615 if (!validate_display_surface(dpy, surface))
1616 return EGL_FALSE;
1617 egl_display_t const * const dp = get_display(dpy);
1618 egl_surface_t const * const s = get_surface(surface);
1619 if (s->cnx->hooks->egl.eglReleaseTexImage) {
1620 return s->cnx->hooks->egl.eglReleaseTexImage(
1621 dp->dpys[s->impl], s->surface, buffer);
1622 }
1623 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1624}
1625
1626EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1627{
1628 egl_display_t * const dp = get_display(dpy);
1629 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1630
1631 EGLBoolean res = EGL_TRUE;
1632 for (int i=0 ; i<2 ; i++) {
1633 egl_connection_t* const cnx = &gEGLImpl[i];
1634 if (cnx->dso) {
1635 if (cnx->hooks->egl.eglSwapInterval) {
1636 if (cnx->hooks->egl.eglSwapInterval(dp->dpys[i], interval) == EGL_FALSE) {
1637 res = EGL_FALSE;
1638 }
1639 }
1640 }
1641 }
1642 return res;
1643}
1644
1645
1646// ----------------------------------------------------------------------------
1647// EGL 1.2
1648// ----------------------------------------------------------------------------
1649
1650EGLBoolean eglWaitClient(void)
1651{
1652 EGLBoolean res = EGL_TRUE;
1653 EGLContext ctx = getContext();
1654 if (ctx) {
1655 egl_context_t const * const c = get_context(ctx);
1656 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1657 if (uint32_t(c->impl)>=2)
1658 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1659 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1660 if (!cnx->dso)
1661 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1662 if (cnx->hooks->egl.eglWaitClient) {
1663 res = cnx->hooks->egl.eglWaitClient();
1664 } else {
1665 res = cnx->hooks->egl.eglWaitGL();
1666 }
1667 }
1668 return res;
1669}
1670
1671EGLBoolean eglBindAPI(EGLenum api)
1672{
1673 // bind this API on all EGLs
1674 EGLBoolean res = EGL_TRUE;
1675 for (int i=0 ; i<2 ; i++) {
1676 egl_connection_t* const cnx = &gEGLImpl[i];
1677 if (cnx->dso) {
1678 if (cnx->hooks->egl.eglBindAPI) {
1679 if (cnx->hooks->egl.eglBindAPI(api) == EGL_FALSE) {
1680 res = EGL_FALSE;
1681 }
1682 }
1683 }
1684 }
1685 return res;
1686}
1687
1688EGLenum eglQueryAPI(void)
1689{
1690 for (int i=0 ; i<2 ; i++) {
1691 egl_connection_t* const cnx = &gEGLImpl[i];
1692 if (cnx->dso) {
1693 if (cnx->hooks->egl.eglQueryAPI) {
1694 // the first one we find is okay, because they all
1695 // should be the same
1696 return cnx->hooks->egl.eglQueryAPI();
1697 }
1698 }
1699 }
1700 // or, it can only be OpenGL ES
1701 return EGL_OPENGL_ES_API;
1702}
1703
1704EGLBoolean eglReleaseThread(void)
1705{
1706 for (int i=0 ; i<2 ; i++) {
1707 egl_connection_t* const cnx = &gEGLImpl[i];
1708 if (cnx->dso) {
1709 if (cnx->hooks->egl.eglReleaseThread) {
1710 cnx->hooks->egl.eglReleaseThread();
1711 }
1712 }
1713 }
1714 clearTLS();
1715 return EGL_TRUE;
1716}
1717
1718EGLSurface eglCreatePbufferFromClientBuffer(
1719 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1720 EGLConfig config, const EGLint *attrib_list)
1721{
1722 egl_display_t const* dp = 0;
1723 int i=0, index=0;
1724 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1725 if (!cnx) return EGL_FALSE;
1726 if (cnx->hooks->egl.eglCreatePbufferFromClientBuffer) {
1727 return cnx->hooks->egl.eglCreatePbufferFromClientBuffer(
1728 dp->dpys[i], buftype, buffer, dp->configs[i][index], attrib_list);
1729 }
1730 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1731}
1732
1733// ----------------------------------------------------------------------------
1734// Android extentions
1735// ----------------------------------------------------------------------------
1736
1737EGLBoolean eglSwapRectangleANDROID(
1738 EGLDisplay dpy, EGLSurface draw,
1739 EGLint l, EGLint t, EGLint w, EGLint h)
1740{
1741 if (!validate_display_surface(dpy, draw))
1742 return EGL_FALSE;
1743 egl_display_t const * const dp = get_display(dpy);
1744 egl_surface_t const * const s = get_surface(draw);
1745 if (s->cnx->hooks->egl.eglSwapRectangleANDROID) {
1746 return s->cnx->hooks->egl.eglSwapRectangleANDROID(
1747 dp->dpys[s->impl], s->surface, l, t, w, h);
1748 }
1749 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1750}
1751
1752const char* eglQueryStringConfigANDROID(
1753 EGLDisplay dpy, EGLConfig config, EGLint name)
1754{
1755 egl_display_t const* dp = 0;
1756 int i=0, index=0;
1757 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1758 if (cnx) {
1759 return dp->queryString[i].extensions_config;
1760 }
1761 return setError(EGL_BAD_PARAMETER, (const char *)0);
1762}