blob: 5da4f9a222bf478bbd7dbd63bd8a842c7f2e53fc [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
376static request_gpu_t* gpu_acquire(void* user)
377{
378 sp<ISurfaceComposer> server( getSurfaceFlinger() );
379
380 Mutex::Autolock _l(gRegionsLock);
381 if (server == NULL) {
382 return 0;
383 }
384
385 ISurfaceComposer::gpu_info_t info;
386 gRevokerCallback = new GPURevokeRequester();
387 status_t err = server->requestGPU(gRevokerCallback, &info);
388 if (err != NO_ERROR) {
389 LOGD("requestGPU returned %d", err);
390 return 0;
391 }
392
393 bool failed = false;
394 request_gpu_t* gpu = &gRegions;
395 memset(gpu, 0, sizeof(*gpu));
396
397 if (info.regs != 0) {
398 sp<IMemoryHeap> heap(info.regs->getMemory());
399 if (heap != 0) {
400 int fd = heap->heapID();
401 gpu->regs.fd = fd;
402 gpu->regs.base = info.regs->pointer();
403 gpu->regs.size = info.regs->size();
404 gpu->regs.user = info.regs.get();
405#if HAVE_ANDROID_OS
406 struct pmem_region region;
407 if (ioctl(fd, PMEM_GET_PHYS, &region) >= 0)
408 gpu->regs.phys = (void*)region.offset;
409#endif
410 info.regs->incStrong(gpu);
411 } else {
412 LOGE("GPU register handle %p is invalid!", info.regs.get());
413 failed = true;
414 }
415 }
416
417 for (size_t i=0 ; i<info.count && !failed ; i++) {
418 sp<IMemory>& region(info.regions[i].region);
419 if (region != 0) {
420 sp<IMemoryHeap> heap(region->getMemory());
421 if (heap != 0) {
422 const int fd = heap->heapID();
423 gpu->gpu[i].fd = fd;
424 gpu->gpu[i].base = region->pointer();
425 gpu->gpu[i].size = region->size();
426 gpu->gpu[i].user = region.get();
427 gpu->gpu[i].offset = info.regions[i].reserved;
428#if HAVE_ANDROID_OS
429 struct pmem_region reg;
430 if (ioctl(fd, PMEM_GET_PHYS, &reg) >= 0)
431 gpu->gpu[i].phys = (void*)reg.offset;
432#endif
433 region->incStrong(gpu);
434 } else {
435 LOGE("GPU region handle [%d, %p] is invalid!", i, region.get());
436 failed = true;
437 }
438 }
439 }
440
441 if (failed) {
442 // something went wrong, clean up everything!
443 if (gpu->regs.user) {
444 static_cast<IMemory*>(gpu->regs.user)->decStrong(gpu);
445 for (size_t i=0 ; i<info.count ; i++) {
446 if (gpu->gpu[i].user) {
447 static_cast<IMemory*>(gpu->gpu[i].user)->decStrong(gpu);
448 }
449 }
450 }
451 }
452
453 gpu->count = info.count;
454 return gpu;
455}
456
457static int gpu_release(void*, request_gpu_t* gpu)
458{
459 sp<IMemory> regs;
460
461 { // scope for lock
462 Mutex::Autolock _l(gRegionsLock);
463 regs = static_cast<IMemory*>(gpu->regs.user);
464 gpu->regs.user = 0;
465 if (regs != 0) regs->decStrong(gpu);
466
467 for (int i=0 ; i<gpu->count ; i++) {
468 sp<IMemory> r(static_cast<IMemory*>(gpu->gpu[i].user));
469 gpu->gpu[i].user = 0;
470 if (r != 0) r->decStrong(gpu);
471 }
472 }
473
474 // there is a special transaction to relinquish the GPU
475 // (it will happen automatically anyway if we don't do this)
476 Parcel data, reply;
477 // NOTE: this transaction does not require an interface token
478 regs->asBinder()->transact(1000, data, &reply);
479 return 1;
480}
481
482/*****************************************************************************/
483
484static __attribute__((noinline))
485void *load_driver(const char* driver, gl_hooks_t* hooks)
486{
487 void* dso = dlopen(driver, RTLD_NOW | RTLD_LOCAL);
488 LOGE_IF(!dso,
489 "couldn't load <%s> library (%s)",
490 driver, dlerror());
491
492 if (dso) {
493 void** curr;
494 char const * const * api;
495 gl_hooks_t::gl_t* gl = &hooks->gl;
496 curr = (void**)gl;
497 api = gl_names;
498 while (*api) {
499 void* f = dlsym(dso, *api);
500 //LOGD("<%s> @ 0x%p", *api, f);
501 if (f == NULL) {
502 //LOGW("<%s> not found in %s", *api, driver);
503 f = (void*)gl_unimplemented;
504 }
505 *curr++ = f;
506 api++;
507 }
508 gl_hooks_t::egl_t* egl = &hooks->egl;
509 curr = (void**)egl;
510 api = egl_names;
511 while (*api) {
512 void* f = dlsym(dso, *api);
513 if (f == NULL) {
514 //LOGW("<%s> not found in %s", *api, driver);
515 f = (void*)0;
516 }
517 *curr++ = f;
518 api++;
519 }
520
521 // hook this driver up with surfaceflinger if needed
522 register_gpu_t register_gpu =
523 (register_gpu_t)dlsym(dso, "oem_register_gpu");
524
525 if (register_gpu != NULL) {
526 if (getSurfaceFlinger() != 0) {
527 register_gpu(dso, gpu_acquire, gpu_release);
528 }
529 }
530 }
531 return dso;
532}
533
534template<typename T>
535static __attribute__((noinline))
536int binarySearch(
537 T const sortedArray[], int first, int last, T key)
538{
539 while (first <= last) {
540 int mid = (first + last) / 2;
541 if (key > sortedArray[mid]) {
542 first = mid + 1;
543 } else if (key < sortedArray[mid]) {
544 last = mid - 1;
545 } else {
546 return mid;
547 }
548 }
549 return -1;
550}
551
552static int cmp_configs(const void* a, const void *b)
553{
554 EGLConfig c0 = *(EGLConfig const *)a;
555 EGLConfig c1 = *(EGLConfig const *)b;
556 return c0<c1 ? -1 : (c0>c1 ? 1 : 0);
557}
558
559static char const * const gVendorString = "Android";
560static char const * const gVersionString = "1.2 Android META-EGL";
561static char const * const gClientApiString = "OpenGL ES";
562
563struct extention_map_t {
564 const char* name;
565 void (*address)(void);
566};
567
568static const extention_map_t gExtentionMap[] = {
569 { "eglSwapRectangleANDROID", (void(*)())&eglSwapRectangleANDROID },
570 { "eglQueryStringConfigANDROID", (void(*)())&eglQueryStringConfigANDROID },
571};
572
573static extention_map_t gGLExtentionMap[MAX_NUMBER_OF_GL_EXTENSIONS];
574
575static void(*findProcAddress(const char* name,
576 const extention_map_t* map, size_t n))()
577{
578 for (uint32_t i=0 ; i<n ; i++) {
579 if (!strcmp(name, map[i].name)) {
580 return map[i].address;
581 }
582 }
583 return NULL;
584}
585
586// ----------------------------------------------------------------------------
587}; // namespace android
588// ----------------------------------------------------------------------------
589
590using namespace android;
591
592
593// ----------------------------------------------------------------------------
594// extensions for the framework
595// ----------------------------------------------------------------------------
596
597void glColorPointerBounds(GLint size, GLenum type, GLsizei stride,
598 const GLvoid *ptr, GLsizei count) {
599 glColorPointer(size, type, stride, ptr);
600}
601void glNormalPointerBounds(GLenum type, GLsizei stride,
602 const GLvoid *pointer, GLsizei count) {
603 glNormalPointer(type, stride, pointer);
604}
605void glTexCoordPointerBounds(GLint size, GLenum type,
606 GLsizei stride, const GLvoid *pointer, GLsizei count) {
607 glTexCoordPointer(size, type, stride, pointer);
608}
609void glVertexPointerBounds(GLint size, GLenum type,
610 GLsizei stride, const GLvoid *pointer, GLsizei count) {
611 glVertexPointer(size, type, stride, pointer);
612}
613
614
615// ----------------------------------------------------------------------------
616// Actual GL wrappers
617// ----------------------------------------------------------------------------
618
619#if __OPTIMIZE__ && defined(__arm__) && !defined(__thumb__) && !USE_SLOW_BINDING && !GL_LOGGER
620
621 #define API_ENTRY(_api) __attribute__((naked)) _api
622 #define CALL_GL_API(_api, ...) \
623 asm volatile( \
624 "mov r12, #0xFFFF0FFF \n" \
625 "ldr r12, [r12, #-15] \n" \
626 "ldr r12, [r12, %[tls]] \n" \
627 "cmp r12, #0 \n" \
628 "ldrne pc, [r12, %[api]] \n" \
629 "bx lr \n" \
630 : \
631 : [tls] "J"(TLS_SLOT_OPENGL_API*4), \
632 [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api)) \
633 : \
634 );
635
636 #define CALL_GL_API_RETURN(_api, ...) \
637 CALL_GL_API(_api, __VA_ARGS__) \
638 return 0; // placate gcc's warnings. never reached.
639
640#else
641
642 #define API_ENTRY(_api) _api
643 #if GL_LOGGER
644
645 #define CALL_GL_API(_api, ...) \
646 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
647 log_##_api(__VA_ARGS__); \
648 _c->_api(__VA_ARGS__);
649
650 #define CALL_GL_API_RETURN(_api, ...) \
651 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
652 log_##_api(__VA_ARGS__); \
653 return _c->_api(__VA_ARGS__)
654
655 #else
656
657 #define CALL_GL_API(_api, ...) \
658 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
659 _c->_api(__VA_ARGS__);
660
661 #define CALL_GL_API_RETURN(_api, ...) \
662 gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl; \
663 return _c->_api(__VA_ARGS__)
664
665 #endif
666
667#endif
668
669#include "gl_api.cpp"
670
671#undef API_ENTRY
672#undef CALL_GL_API
673#undef CALL_GL_API_RETURN
674
675// ----------------------------------------------------------------------------
676namespace android {
677// ----------------------------------------------------------------------------
678
679static int gl_context_lost() {
680 setGlThreadSpecific(&gHooks[IMPL_HARDWARE_CONTEXT_LOST]);
681 return 0;
682}
683static int egl_context_lost() {
684 setGlThreadSpecific(&gHooks[IMPL_HARDWARE_CONTEXT_LOST]);
685 return EGL_FALSE;
686}
687static EGLBoolean egl_context_lost_swap_buffers(void*, void*) {
688 usleep(100000); // don't use all the CPU
689 setGlThreadSpecific(&gHooks[IMPL_HARDWARE_CONTEXT_LOST]);
690 return EGL_FALSE;
691}
692static GLint egl_context_lost_get_error() {
693 return EGL_CONTEXT_LOST;
694}
695static int ext_context_lost() {
696 return 0;
697}
698
699static void gl_no_context() {
700 LOGE("call to OpenGL ES API with no current context");
701}
702static void early_egl_init(void)
703{
704#if !defined(HAVE_ANDROID_OS) || USE_SLOW_BINDING || GL_LOGGER
705 pthread_key_create(&gGLWrapperKey, NULL);
706#endif
707 uint32_t addr = (uint32_t)((void*)gl_no_context);
708 android_memset32((uint32_t*)(void*)&gHooks[IMPL_NO_CONTEXT], addr, sizeof(gHooks[IMPL_NO_CONTEXT]));
709 setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
710}
711
712static pthread_once_t once_control = PTHREAD_ONCE_INIT;
713static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
714
715
716static inline
717egl_display_t* get_display(EGLDisplay dpy)
718{
719 uintptr_t index = uintptr_t(dpy)-1U;
720 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
721}
722
723static inline
724egl_surface_t* get_surface(EGLSurface surface)
725{
726 egl_surface_t* s = (egl_surface_t *)surface;
727 return s;
728}
729
730static inline
731egl_context_t* get_context(EGLContext context)
732{
733 egl_context_t* c = (egl_context_t *)context;
734 return c;
735}
736
737static egl_connection_t* validate_display_config(
738 EGLDisplay dpy, EGLConfig config,
739 egl_display_t const*& dp, int& impl, int& index)
740{
741 dp = get_display(dpy);
742 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
743
744 impl = uintptr_t(config)>>24;
745 if (uint32_t(impl) >= 2) {
746 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
747 }
748 index = uintptr_t(config) & 0xFFFFFF;
749 if (index >= dp->numConfigs[impl]) {
750 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
751 }
752 egl_connection_t* const cnx = &gEGLImpl[impl];
753 if (cnx->dso == 0) {
754 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
755 }
756 return cnx;
757}
758
759static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
760{
761 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
762 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
763 if (!get_display(dpy)->isValid())
764 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
765 if (!ctx) // TODO: make sure context is a valid object
766 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
767 if (!get_context(ctx)->isValid())
768 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
769 return EGL_TRUE;
770}
771
772static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
773{
774 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
775 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
776 if (!get_display(dpy)->isValid())
777 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
778 if (!surface) // TODO: make sure surface is a valid object
779 return setError(EGL_BAD_SURFACE, EGL_FALSE);
780 if (!get_surface(surface)->isValid())
781 return setError(EGL_BAD_SURFACE, EGL_FALSE);
782 return EGL_TRUE;
783}
784
785static void add_extension(egl_display_t* dp, char const*& p, const char* ext)
786{
787 if (!strstr(p, ext)) {
788 p = (char const*)realloc((void*)p, strlen(p) + 1 + strlen(ext) + 1);
789 strcat((char*)p, " ");
790 strcat((char*)p, ext);
791 }
792 if (!strstr(dp->extensionsString, ext)) {
793 char const*& es = dp->extensionsString;
794 es = (char const*)realloc((void*)es, strlen(es) + 1 + strlen(ext) + 1);
795 strcat((char*)es, " ");
796 strcat((char*)es, ext);
797 }
798}
799
800// ----------------------------------------------------------------------------
801}; // namespace android
802// ----------------------------------------------------------------------------
803
804EGLDisplay eglGetDisplay(NativeDisplayType display)
805{
806 if (sEarlyInitState) {
807 return EGL_NO_DISPLAY;
808 }
809
810 uint32_t index = uint32_t(display);
811 if (index >= NUM_DISPLAYS) {
812 return EGL_NO_DISPLAY;
813 }
814
815 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
816 egl_display_t* d = &gDisplay[index];
817
818 // dynamically load all our EGL implementations for that display
819 // and call into the real eglGetGisplay()
820 egl_connection_t* cnx = &gEGLImpl[IMPL_SOFTWARE];
821 if (cnx->dso == 0) {
822 cnx->hooks = &gHooks[IMPL_SOFTWARE];
823 cnx->dso = load_driver("libagl.so", cnx->hooks);
824 }
825 if (cnx->dso && d->dpys[IMPL_SOFTWARE]==EGL_NO_DISPLAY) {
826 d->dpys[IMPL_SOFTWARE] = cnx->hooks->egl.eglGetDisplay(display);
827 LOGE_IF(d->dpys[IMPL_SOFTWARE]==EGL_NO_DISPLAY,
828 "No EGLDisplay for software EGL!");
829 }
830
831 cnx = &gEGLImpl[IMPL_HARDWARE];
832 if (cnx->dso == 0 && cnx->unavailable == 0) {
833 char value[PROPERTY_VALUE_MAX];
834 property_get("debug.egl.hw", value, "1");
835 if (atoi(value) != 0) {
836 cnx->hooks = &gHooks[IMPL_HARDWARE];
837 cnx->dso = load_driver("libhgl.so", cnx->hooks);
838 } else {
839 LOGD("3D hardware acceleration is disabled");
840 }
841 }
842 if (cnx->dso && d->dpys[IMPL_HARDWARE]==EGL_NO_DISPLAY) {
843 android_memset32(
844 (uint32_t*)(void*)&gHooks[IMPL_HARDWARE_CONTEXT_LOST].gl,
845 (uint32_t)((void*)gl_context_lost),
846 sizeof(gHooks[IMPL_HARDWARE_CONTEXT_LOST].gl));
847 android_memset32(
848 (uint32_t*)(void*)&gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl,
849 (uint32_t)((void*)egl_context_lost),
850 sizeof(gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl));
851 android_memset32(
852 (uint32_t*)(void*)&gHooks[IMPL_HARDWARE_CONTEXT_LOST].ext,
853 (uint32_t)((void*)ext_context_lost),
854 sizeof(gHooks[IMPL_HARDWARE_CONTEXT_LOST].ext));
855
856 gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl.eglSwapBuffers =
857 egl_context_lost_swap_buffers;
858
859 gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl.eglGetError =
860 egl_context_lost_get_error;
861
862 gHooks[IMPL_HARDWARE_CONTEXT_LOST].egl.eglTerminate =
863 gHooks[IMPL_HARDWARE].egl.eglTerminate;
864
865 d->dpys[IMPL_HARDWARE] = cnx->hooks->egl.eglGetDisplay(display);
866 if (d->dpys[IMPL_HARDWARE] == EGL_NO_DISPLAY) {
867 dlclose((void*)cnx->dso);
868 cnx->dso = 0;
869 // in case of failure, we want to make sure we don't try again
870 // as it's expensive.
871 cnx->unavailable = 1;
872 }
873 }
874
875 return dpy;
876}
877
878// ----------------------------------------------------------------------------
879// Initialization
880// ----------------------------------------------------------------------------
881
882EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
883{
884 egl_display_t * const dp = get_display(dpy);
885 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
886
887 if (android_atomic_inc(&dp->refs) > 0) {
888 if (major != NULL) *major = 1;
889 if (minor != NULL) *minor = 2;
890 return EGL_TRUE;
891 }
892
893 setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
894
895 // initialize each EGL and
896 // build our own extension string first, based on the extension we know
897 // and the extension supported by our client implementation
898 dp->extensionsString = strdup("EGL_ANDROID_query_string_config");
899 for (int i=0 ; i<2 ; i++) {
900 egl_connection_t* const cnx = &gEGLImpl[i];
901 cnx->major = -1;
902 cnx->minor = -1;
903 if (cnx->dso && cnx->hooks->egl.eglInitialize(
904 dp->dpys[i], &cnx->major, &cnx->minor)) {
905
906 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
907 // i, dp->dpys[i], cnx->major, cnx->minor, cnx);
908
909 // get the query-strings for this display for each implementation
910 dp->queryString[i].vendor =
911 cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_VENDOR);
912 dp->queryString[i].version =
913 cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_VERSION);
914 dp->queryString[i].extensions = strdup(
915 cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_EXTENSIONS));
916 dp->queryString[i].clientApi =
917 cnx->hooks->egl.eglQueryString(dp->dpys[i], EGL_CLIENT_APIS);
918
919 // Dynamically insert extensions we know about
920 if (cnx->hooks->egl.eglSwapRectangleANDROID)
921 add_extension(dp, dp->queryString[i].extensions,
922 "EGL_ANDROID_swap_rectangle");
923
924 if (cnx->hooks->egl.eglQueryStringConfigANDROID)
925 add_extension(dp, dp->queryString[i].extensions,
926 "EGL_ANDROID_query_string_config");
927 }
928 }
929
930 // Build the extension list that depends on the current config.
931 // It is the intersection of our extension list and the
932 // underlaying EGL's extensions list
933 EGLBoolean res = EGL_FALSE;
934 for (int i=0 ; i<2 ; i++) {
935 egl_connection_t* const cnx = &gEGLImpl[i];
936 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
937 char const* const their_extensions = dp->queryString[i].extensions;
938 char* our_extensions = strdup(dp->extensionsString);
939 char* const our_extensions_org = our_extensions;
940 char* extensions_config = (char*)calloc(strlen(our_extensions)+2, 1);
941 char* p;
942 do {
943 p = strchr(our_extensions, ' ');
944 if (p) *p++ = 0;
945 else p = strchr(our_extensions, 0);
946 if (strstr(their_extensions, our_extensions)) {
947 strcat(extensions_config, our_extensions);
948 strcat(extensions_config, " ");
949 }
950 our_extensions = p;
951 } while (*p);
952 free((void*)our_extensions_org);
953
954 // remove the trailling white space
955 if (extensions_config[0] != 0) {
956 size_t l = strlen(extensions_config) - 1; // new size
957 extensions_config[l] = 0; // remove the trailling white space
958 extensions_config = (char*)realloc(extensions_config, l+1);
959 } else {
960 extensions_config = (char*)realloc(extensions_config, 1);
961 }
962 dp->queryString[i].extensions_config = extensions_config;
963
964 EGLint n;
965 if (cnx->hooks->egl.eglGetConfigs(dp->dpys[i], 0, 0, &n)) {
966 dp->configs[i] = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
967 if (dp->configs[i]) {
968 if (cnx->hooks->egl.eglGetConfigs(
969 dp->dpys[i], dp->configs[i], n, &dp->numConfigs[i]))
970 {
971 // sort the configurations so we can do binary searches
972 qsort( dp->configs[i],
973 dp->numConfigs[i],
974 sizeof(EGLConfig), cmp_configs);
975
976 dp->numTotalConfigs += n;
977 res = EGL_TRUE;
978 }
979 }
980 }
981 }
982 }
983
984 if (res == EGL_TRUE) {
985 if (major != NULL) *major = 1;
986 if (minor != NULL) *minor = 2;
987 return EGL_TRUE;
988 }
989 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
990}
991
992EGLBoolean eglTerminate(EGLDisplay dpy)
993{
994 egl_display_t* const dp = get_display(dpy);
995 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
996 if (android_atomic_dec(&dp->refs) != 1)
997 return EGL_TRUE;
998
999 EGLBoolean res = EGL_FALSE;
1000 for (int i=0 ; i<2 ; i++) {
1001 egl_connection_t* const cnx = &gEGLImpl[i];
1002 if (cnx->dso) {
1003 cnx->hooks->egl.eglTerminate(dp->dpys[i]);
1004
1005 /* REVISIT: it's unclear what to do if eglTerminate() fails,
1006 * on one end we shouldn't care, on the other end if it fails
1007 * it might not be safe to call dlclose() (there could be some
1008 * threads around). */
1009
1010 free(dp->configs[i]);
1011 free((void*)dp->queryString[i].extensions_config);
1012 free((void*)dp->queryString[i].extensions);
1013 dp->numConfigs[i] = 0;
1014 dp->dpys[i] = EGL_NO_DISPLAY;
1015 dlclose((void*)cnx->dso);
1016 cnx->dso = 0;
1017 res = EGL_TRUE;
1018 }
1019 }
1020 free((void*)dp->extensionsString);
1021 dp->extensionsString = 0;
1022 dp->numTotalConfigs = 0;
1023 clearTLS();
1024 return res;
1025}
1026
1027// ----------------------------------------------------------------------------
1028// configuration
1029// ----------------------------------------------------------------------------
1030
1031EGLBoolean eglGetConfigs( EGLDisplay dpy,
1032 EGLConfig *configs,
1033 EGLint config_size, EGLint *num_config)
1034{
1035 egl_display_t const * const dp = get_display(dpy);
1036 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1037
1038 GLint numConfigs = dp->numTotalConfigs;
1039 if (!configs) {
1040 *num_config = numConfigs;
1041 return EGL_TRUE;
1042 }
1043 GLint n = 0;
1044 for (int j=0 ; j<2 ; j++) {
1045 for (int i=0 ; i<dp->numConfigs[j] && config_size ; i++) {
1046 *configs++ = MAKE_CONFIG(j, i);
1047 config_size--;
1048 n++;
1049 }
1050 }
1051
1052 *num_config = n;
1053 return EGL_TRUE;
1054}
1055
1056EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
1057 EGLConfig *configs, EGLint config_size,
1058 EGLint *num_config)
1059{
1060 egl_display_t const * const dp = get_display(dpy);
1061 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1062
1063 if (configs == 0) {
1064 *num_config = 0;
1065 return EGL_TRUE;
1066 }
1067
1068 EGLBoolean res = EGL_FALSE;
1069 *num_config = 0;
1070 for (int i=0 ; i<2 ; i++) {
1071 egl_connection_t* const cnx = &gEGLImpl[i];
1072 if (cnx->dso) {
1073 EGLint n;
1074 if (cnx->hooks->egl.eglChooseConfig(
1075 dp->dpys[i], attrib_list, configs, config_size, &n))
1076 {
1077 // now we need to convert these client EGLConfig to our
1078 // internal EGLConfig format. This is done in O(n log n).
1079 for (int j=0 ; j<n ; j++) {
1080 int index = binarySearch<EGLConfig>(
1081 dp->configs[i], 0, dp->numConfigs[i]-1, configs[j]);
1082 if (index >= 0) {
1083 configs[j] = MAKE_CONFIG(i, index);
1084 } else {
1085 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1086 }
1087 }
1088 configs += n;
1089 config_size -= n;
1090 *num_config += n;
1091 res = EGL_TRUE;
1092 }
1093 }
1094 }
1095 return res;
1096}
1097
1098EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1099 EGLint attribute, EGLint *value)
1100{
1101 egl_display_t const* dp = 0;
1102 int i=0, index=0;
1103 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1104 if (!cnx) return EGL_FALSE;
1105 return cnx->hooks->egl.eglGetConfigAttrib(
1106 dp->dpys[i], dp->configs[i][index], attribute, value);
1107}
1108
1109// ----------------------------------------------------------------------------
1110// surfaces
1111// ----------------------------------------------------------------------------
1112
1113EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1114 NativeWindowType window,
1115 const EGLint *attrib_list)
1116{
1117 egl_display_t const* dp = 0;
1118 int i=0, index=0;
1119 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1120 if (cnx) {
1121 // window must be connected upon calling underlying
1122 // eglCreateWindowSurface
1123 if (window) {
1124 window->incRef(window);
1125 if (window->connect)
1126 window->connect(window);
1127 }
1128
1129 EGLSurface surface = cnx->hooks->egl.eglCreateWindowSurface(
1130 dp->dpys[i], dp->configs[i][index], window, attrib_list);
1131 if (surface != EGL_NO_SURFACE) {
1132 egl_surface_t* s = new egl_surface_t(dpy, surface, window, i, cnx);
1133 return s;
1134 }
1135
1136 // something went wrong, disconnect and free window
1137 // (will disconnect() automatically)
1138 if (window) {
1139 window->decRef(window);
1140 }
1141 }
1142 return EGL_NO_SURFACE;
1143}
1144
1145EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1146 NativePixmapType pixmap,
1147 const EGLint *attrib_list)
1148{
1149 egl_display_t const* dp = 0;
1150 int i=0, index=0;
1151 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1152 if (cnx) {
1153 EGLSurface surface = cnx->hooks->egl.eglCreatePixmapSurface(
1154 dp->dpys[i], dp->configs[i][index], pixmap, attrib_list);
1155 if (surface != EGL_NO_SURFACE) {
1156 egl_surface_t* s = new egl_surface_t(dpy, surface, NULL, i, cnx);
1157 return s;
1158 }
1159 }
1160 return EGL_NO_SURFACE;
1161}
1162
1163EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1164 const EGLint *attrib_list)
1165{
1166 egl_display_t const* dp = 0;
1167 int i=0, index=0;
1168 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1169 if (cnx) {
1170 EGLSurface surface = cnx->hooks->egl.eglCreatePbufferSurface(
1171 dp->dpys[i], dp->configs[i][index], attrib_list);
1172 if (surface != EGL_NO_SURFACE) {
1173 egl_surface_t* s = new egl_surface_t(dpy, surface, NULL, i, cnx);
1174 return s;
1175 }
1176 }
1177 return EGL_NO_SURFACE;
1178}
1179
1180EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1181{
1182 if (!validate_display_surface(dpy, surface))
1183 return EGL_FALSE;
1184 egl_display_t const * const dp = get_display(dpy);
1185 egl_surface_t const * const s = get_surface(surface);
1186
1187 EGLBoolean result = s->cnx->hooks->egl.eglDestroySurface(
1188 dp->dpys[s->impl], s->surface);
1189
1190 delete s;
1191 return result;
1192}
1193
1194EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1195 EGLint attribute, EGLint *value)
1196{
1197 if (!validate_display_surface(dpy, surface))
1198 return EGL_FALSE;
1199 egl_display_t const * const dp = get_display(dpy);
1200 egl_surface_t const * const s = get_surface(surface);
1201
1202 return s->cnx->hooks->egl.eglQuerySurface(
1203 dp->dpys[s->impl], s->surface, attribute, value);
1204}
1205
1206// ----------------------------------------------------------------------------
1207// contextes
1208// ----------------------------------------------------------------------------
1209
1210EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1211 EGLContext share_list, const EGLint *attrib_list)
1212{
1213 egl_display_t const* dp = 0;
1214 int i=0, index=0;
1215 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1216 if (cnx) {
1217 EGLContext context = cnx->hooks->egl.eglCreateContext(
1218 dp->dpys[i], dp->configs[i][index], share_list, attrib_list);
1219 if (context != EGL_NO_CONTEXT) {
1220 egl_context_t* c = new egl_context_t(dpy, context, i, cnx);
1221 return c;
1222 }
1223 }
1224 return EGL_NO_CONTEXT;
1225}
1226
1227EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1228{
1229 if (!validate_display_context(dpy, ctx))
1230 return EGL_FALSE;
1231 egl_display_t const * const dp = get_display(dpy);
1232 egl_context_t * const c = get_context(ctx);
1233 EGLBoolean result = c->cnx->hooks->egl.eglDestroyContext(
1234 dp->dpys[c->impl], c->context);
1235 delete c;
1236 return result;
1237}
1238
1239EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1240 EGLSurface read, EGLContext ctx)
1241{
1242 egl_display_t const * const dp = get_display(dpy);
1243 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1244
1245 if (read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE &&
1246 ctx == EGL_NO_CONTEXT)
1247 {
1248 EGLBoolean result = EGL_TRUE;
1249 ctx = getContext();
1250 if (ctx) {
1251 egl_context_t * const c = get_context(ctx);
1252 result = c->cnx->hooks->egl.eglMakeCurrent(dp->dpys[c->impl], 0, 0, 0);
1253 if (result == EGL_TRUE) {
1254 setGlThreadSpecific(&gHooks[IMPL_NO_CONTEXT]);
1255 setContext(EGL_NO_CONTEXT);
1256 }
1257 }
1258 return result;
1259 }
1260
1261 if (!validate_display_context(dpy, ctx))
1262 return EGL_FALSE;
1263
1264 egl_context_t * const c = get_context(ctx);
1265 if (draw != EGL_NO_SURFACE) {
1266 egl_surface_t const * d = get_surface(draw);
1267 if (!d) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1268 if (d->impl != c->impl)
1269 return setError(EGL_BAD_MATCH, EGL_FALSE);
1270 draw = d->surface;
1271 }
1272 if (read != EGL_NO_SURFACE) {
1273 egl_surface_t const * r = get_surface(read);
1274 if (!r) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1275 if (r->impl != c->impl)
1276 return setError(EGL_BAD_MATCH, EGL_FALSE);
1277 read = r->surface;
1278 }
1279 EGLBoolean result = c->cnx->hooks->egl.eglMakeCurrent(
1280 dp->dpys[c->impl], draw, read, c->context);
1281
1282 if (result == EGL_TRUE) {
1283 setGlThreadSpecific(c->cnx->hooks);
1284 setContext(ctx);
1285 c->read = read;
1286 c->draw = draw;
1287 }
1288 return result;
1289}
1290
1291
1292EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1293 EGLint attribute, EGLint *value)
1294{
1295 if (!validate_display_context(dpy, ctx))
1296 return EGL_FALSE;
1297
1298 egl_display_t const * const dp = get_display(dpy);
1299 egl_context_t * const c = get_context(ctx);
1300
1301 return c->cnx->hooks->egl.eglQueryContext(
1302 dp->dpys[c->impl], c->context, attribute, value);
1303}
1304
1305EGLContext eglGetCurrentContext(void)
1306{
1307 EGLContext ctx = getContext();
1308 return ctx;
1309}
1310
1311EGLSurface eglGetCurrentSurface(EGLint readdraw)
1312{
1313 EGLContext ctx = getContext();
1314 if (ctx) {
1315 egl_context_t const * const c = get_context(ctx);
1316 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1317 switch (readdraw) {
1318 case EGL_READ: return c->read;
1319 case EGL_DRAW: return c->draw;
1320 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1321 }
1322 }
1323 return EGL_NO_SURFACE;
1324}
1325
1326EGLDisplay eglGetCurrentDisplay(void)
1327{
1328 EGLContext ctx = getContext();
1329 if (ctx) {
1330 egl_context_t const * const c = get_context(ctx);
1331 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1332 return c->dpy;
1333 }
1334 return EGL_NO_DISPLAY;
1335}
1336
1337EGLBoolean eglWaitGL(void)
1338{
1339 EGLBoolean res = EGL_TRUE;
1340 EGLContext ctx = getContext();
1341 if (ctx) {
1342 egl_context_t const * const c = get_context(ctx);
1343 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1344 if (uint32_t(c->impl)>=2)
1345 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1346 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1347 if (!cnx->dso)
1348 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1349 res = cnx->hooks->egl.eglWaitGL();
1350 }
1351 return res;
1352}
1353
1354EGLBoolean eglWaitNative(EGLint engine)
1355{
1356 EGLBoolean res = EGL_TRUE;
1357 EGLContext ctx = getContext();
1358 if (ctx) {
1359 egl_context_t const * const c = get_context(ctx);
1360 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1361 if (uint32_t(c->impl)>=2)
1362 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1363 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1364 if (!cnx->dso)
1365 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1366 res = cnx->hooks->egl.eglWaitNative(engine);
1367 }
1368 return res;
1369}
1370
1371EGLint eglGetError(void)
1372{
1373 EGLint result = EGL_SUCCESS;
1374 for (int i=0 ; i<2 ; i++) {
1375 EGLint err = EGL_SUCCESS;
1376 egl_connection_t* const cnx = &gEGLImpl[i];
1377 if (cnx->dso)
1378 err = cnx->hooks->egl.eglGetError();
1379 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1380 result = err;
1381 }
1382 if (result == EGL_SUCCESS)
1383 result = getError();
1384 return result;
1385}
1386
1387void (*eglGetProcAddress(const char *procname))()
1388{
1389 void (*addr)();
1390 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1391 if (addr) return addr;
1392
1393 return NULL; // TODO: finish implementation below
1394
1395 addr = findProcAddress(procname, gGLExtentionMap, NELEM(gGLExtentionMap));
1396 if (addr) return addr;
1397
1398 addr = 0;
1399 int slot = -1;
1400 for (int i=0 ; i<2 ; i++) {
1401 egl_connection_t* const cnx = &gEGLImpl[i];
1402 if (cnx->dso) {
1403 if (cnx->hooks->egl.eglGetProcAddress) {
1404 addr = cnx->hooks->egl.eglGetProcAddress(procname);
1405 if (addr) {
1406 if (slot == -1) {
1407 slot = 0; // XXX: find free slot
1408 if (slot == -1) {
1409 addr = 0;
1410 break;
1411 }
1412 }
1413 cnx->hooks->ext.extensions[slot] = addr;
1414 }
1415 }
1416 }
1417 }
1418
1419 if (slot >= 0) {
1420 addr = 0; // XXX: address of stub 'slot'
1421 gGLExtentionMap[slot].name = strdup(procname);
1422 gGLExtentionMap[slot].address = addr;
1423 }
1424
1425 return addr;
1426
1427
1428 /*
1429 * TODO: For OpenGL ES extensions, we must generate a stub
1430 * that looks like
1431 * mov r12, #0xFFFF0FFF
1432 * ldr r12, [r12, #-15]
1433 * ldr r12, [r12, #TLS_SLOT_OPENGL_API*4]
1434 * mov r12, [r12, #api_offset]
1435 * ldrne pc, r12
1436 * mov pc, #unsupported_extension
1437 *
1438 * and write the address of the extension in *all*
1439 * gl_hooks_t::gl_ext_t at offset "api_offset" from gl_hooks_t
1440 *
1441 */
1442}
1443
1444EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1445{
1446 if (!validate_display_surface(dpy, draw))
1447 return EGL_FALSE;
1448 egl_display_t const * const dp = get_display(dpy);
1449 egl_surface_t const * const s = get_surface(draw);
1450 return s->cnx->hooks->egl.eglSwapBuffers(dp->dpys[s->impl], s->surface);
1451}
1452
1453EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1454 NativePixmapType target)
1455{
1456 if (!validate_display_surface(dpy, surface))
1457 return EGL_FALSE;
1458 egl_display_t const * const dp = get_display(dpy);
1459 egl_surface_t const * const s = get_surface(surface);
1460 return s->cnx->hooks->egl.eglCopyBuffers(
1461 dp->dpys[s->impl], s->surface, target);
1462}
1463
1464const char* eglQueryString(EGLDisplay dpy, EGLint name)
1465{
1466 egl_display_t const * const dp = get_display(dpy);
1467 switch (name) {
1468 case EGL_VENDOR:
1469 return gVendorString;
1470 case EGL_VERSION:
1471 return gVersionString;
1472 case EGL_EXTENSIONS:
1473 return dp->extensionsString;
1474 case EGL_CLIENT_APIS:
1475 return gClientApiString;
1476 }
1477 return setError(EGL_BAD_PARAMETER, (const char *)0);
1478}
1479
1480
1481// ----------------------------------------------------------------------------
1482// EGL 1.1
1483// ----------------------------------------------------------------------------
1484
1485EGLBoolean eglSurfaceAttrib(
1486 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1487{
1488 if (!validate_display_surface(dpy, surface))
1489 return EGL_FALSE;
1490 egl_display_t const * const dp = get_display(dpy);
1491 egl_surface_t const * const s = get_surface(surface);
1492 if (s->cnx->hooks->egl.eglSurfaceAttrib) {
1493 return s->cnx->hooks->egl.eglSurfaceAttrib(
1494 dp->dpys[s->impl], s->surface, attribute, value);
1495 }
1496 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1497}
1498
1499EGLBoolean eglBindTexImage(
1500 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1501{
1502 if (!validate_display_surface(dpy, surface))
1503 return EGL_FALSE;
1504 egl_display_t const * const dp = get_display(dpy);
1505 egl_surface_t const * const s = get_surface(surface);
1506 if (s->cnx->hooks->egl.eglBindTexImage) {
1507 return s->cnx->hooks->egl.eglBindTexImage(
1508 dp->dpys[s->impl], s->surface, buffer);
1509 }
1510 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1511}
1512
1513EGLBoolean eglReleaseTexImage(
1514 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1515{
1516 if (!validate_display_surface(dpy, surface))
1517 return EGL_FALSE;
1518 egl_display_t const * const dp = get_display(dpy);
1519 egl_surface_t const * const s = get_surface(surface);
1520 if (s->cnx->hooks->egl.eglReleaseTexImage) {
1521 return s->cnx->hooks->egl.eglReleaseTexImage(
1522 dp->dpys[s->impl], s->surface, buffer);
1523 }
1524 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1525}
1526
1527EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1528{
1529 egl_display_t * const dp = get_display(dpy);
1530 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1531
1532 EGLBoolean res = EGL_TRUE;
1533 for (int i=0 ; i<2 ; i++) {
1534 egl_connection_t* const cnx = &gEGLImpl[i];
1535 if (cnx->dso) {
1536 if (cnx->hooks->egl.eglSwapInterval) {
1537 if (cnx->hooks->egl.eglSwapInterval(dp->dpys[i], interval) == EGL_FALSE) {
1538 res = EGL_FALSE;
1539 }
1540 }
1541 }
1542 }
1543 return res;
1544}
1545
1546
1547// ----------------------------------------------------------------------------
1548// EGL 1.2
1549// ----------------------------------------------------------------------------
1550
1551EGLBoolean eglWaitClient(void)
1552{
1553 EGLBoolean res = EGL_TRUE;
1554 EGLContext ctx = getContext();
1555 if (ctx) {
1556 egl_context_t const * const c = get_context(ctx);
1557 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1558 if (uint32_t(c->impl)>=2)
1559 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1560 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1561 if (!cnx->dso)
1562 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1563 if (cnx->hooks->egl.eglWaitClient) {
1564 res = cnx->hooks->egl.eglWaitClient();
1565 } else {
1566 res = cnx->hooks->egl.eglWaitGL();
1567 }
1568 }
1569 return res;
1570}
1571
1572EGLBoolean eglBindAPI(EGLenum api)
1573{
1574 // bind this API on all EGLs
1575 EGLBoolean res = EGL_TRUE;
1576 for (int i=0 ; i<2 ; i++) {
1577 egl_connection_t* const cnx = &gEGLImpl[i];
1578 if (cnx->dso) {
1579 if (cnx->hooks->egl.eglBindAPI) {
1580 if (cnx->hooks->egl.eglBindAPI(api) == EGL_FALSE) {
1581 res = EGL_FALSE;
1582 }
1583 }
1584 }
1585 }
1586 return res;
1587}
1588
1589EGLenum eglQueryAPI(void)
1590{
1591 for (int i=0 ; i<2 ; i++) {
1592 egl_connection_t* const cnx = &gEGLImpl[i];
1593 if (cnx->dso) {
1594 if (cnx->hooks->egl.eglQueryAPI) {
1595 // the first one we find is okay, because they all
1596 // should be the same
1597 return cnx->hooks->egl.eglQueryAPI();
1598 }
1599 }
1600 }
1601 // or, it can only be OpenGL ES
1602 return EGL_OPENGL_ES_API;
1603}
1604
1605EGLBoolean eglReleaseThread(void)
1606{
1607 for (int i=0 ; i<2 ; i++) {
1608 egl_connection_t* const cnx = &gEGLImpl[i];
1609 if (cnx->dso) {
1610 if (cnx->hooks->egl.eglReleaseThread) {
1611 cnx->hooks->egl.eglReleaseThread();
1612 }
1613 }
1614 }
1615 clearTLS();
1616 return EGL_TRUE;
1617}
1618
1619EGLSurface eglCreatePbufferFromClientBuffer(
1620 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1621 EGLConfig config, const EGLint *attrib_list)
1622{
1623 egl_display_t const* dp = 0;
1624 int i=0, index=0;
1625 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1626 if (!cnx) return EGL_FALSE;
1627 if (cnx->hooks->egl.eglCreatePbufferFromClientBuffer) {
1628 return cnx->hooks->egl.eglCreatePbufferFromClientBuffer(
1629 dp->dpys[i], buftype, buffer, dp->configs[i][index], attrib_list);
1630 }
1631 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1632}
1633
1634// ----------------------------------------------------------------------------
1635// Android extentions
1636// ----------------------------------------------------------------------------
1637
1638EGLBoolean eglSwapRectangleANDROID(
1639 EGLDisplay dpy, EGLSurface draw,
1640 EGLint l, EGLint t, EGLint w, EGLint h)
1641{
1642 if (!validate_display_surface(dpy, draw))
1643 return EGL_FALSE;
1644 egl_display_t const * const dp = get_display(dpy);
1645 egl_surface_t const * const s = get_surface(draw);
1646 if (s->cnx->hooks->egl.eglSwapRectangleANDROID) {
1647 return s->cnx->hooks->egl.eglSwapRectangleANDROID(
1648 dp->dpys[s->impl], s->surface, l, t, w, h);
1649 }
1650 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1651}
1652
1653const char* eglQueryStringConfigANDROID(
1654 EGLDisplay dpy, EGLConfig config, EGLint name)
1655{
1656 egl_display_t const* dp = 0;
1657 int i=0, index=0;
1658 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1659 if (cnx) {
1660 return dp->queryString[i].extensions_config;
1661 }
1662 return setError(EGL_BAD_PARAMETER, (const char *)0);
1663}