blob: 714fd3eec7c5c658dab77f7ec6c3dce419e0491a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <ctype.h>
Mathias Agopiand8fb7b52009-05-17 18:50:16 -070018#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <string.h>
20#include <errno.h>
21#include <dlfcn.h>
22
23#include <sys/ioctl.h>
24
25#if HAVE_ANDROID_OS
26#include <linux/android_pmem.h>
27#endif
28
29#include <EGL/egl.h>
30#include <EGL/eglext.h>
31#include <GLES/gl.h>
32#include <GLES/glext.h>
33
34#include <cutils/log.h>
35#include <cutils/atomic.h>
36#include <cutils/properties.h>
37#include <cutils/memory.h>
38
Mathias Agopian9429e9c2009-08-21 02:18:25 -070039#include <utils/SortedVector.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include "hooks.h"
42#include "egl_impl.h"
Mathias Agopiande586972009-05-28 17:39:03 -070043#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
45#define MAKE_CONFIG(_impl, _index) ((EGLConfig)(((_impl)<<24) | (_index)))
46#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
47
48// ----------------------------------------------------------------------------
49namespace android {
50// ----------------------------------------------------------------------------
51
52#define VERSION_MAJOR 1
53#define VERSION_MINOR 4
54static char const * const gVendorString = "Android";
Mathias Agopian923c6612009-08-17 18:07:06 -070055static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057static char const * const gExtensionString =
58 "EGL_KHR_image "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -070059 "EGL_KHR_image_base "
60 "EGL_KHR_image_pixmap "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070061 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070062 "EGL_ANDROID_swap_rectangle "
Mathias Agopian8d2e83b2009-06-24 22:37:39 -070063 "EGL_ANDROID_get_render_buffer "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070064 ;
65
66// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopian9429e9c2009-08-21 02:18:25 -070068class egl_object_t {
69 static SortedVector<egl_object_t*> sObjects;
70 static Mutex sLock;
71
72 volatile int32_t terminated;
73 mutable volatile int32_t count;
74
75public:
76 egl_object_t() : terminated(0), count(1) {
77 Mutex::Autolock _l(sLock);
78 sObjects.add(this);
79 }
80
81 inline bool isAlive() const { return !terminated; }
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -070084 bool get() {
85 Mutex::Autolock _l(sLock);
86 if (egl_object_t::sObjects.indexOf(this) >= 0) {
87 android_atomic_inc(&count);
88 return true;
89 }
90 return false;
91 }
92
93 bool put() {
94 Mutex::Autolock _l(sLock);
95 if (android_atomic_dec(&count) == 1) {
96 sObjects.remove(this);
97 return true;
98 }
99 return false;
100 }
101
102public:
103 template <typename N, typename T>
104 struct LocalRef {
105 N* ref;
106 LocalRef(T o) : ref(0) {
107 N* native = reinterpret_cast<N*>(o);
108 if (o && native->get()) {
109 ref = native;
110 }
111 }
112 ~LocalRef() {
113 if (ref && ref->put()) {
114 delete ref;
115 }
116 }
117 inline N* get() {
118 return ref;
119 }
120 void acquire() const {
121 if (ref) {
122 android_atomic_inc(&ref->count);
123 }
124 }
125 void release() const {
126 if (ref) {
127 int32_t c = android_atomic_dec(&ref->count);
128 // ref->count cannot be 1 prior atomic_dec because we have
129 // a reference, and if we have one, it means there was
130 // already one before us.
131 LOGE_IF(c==1, "refcount is now 0 in release()");
132 }
133 }
134 void terminate() {
135 if (ref) {
136 ref->terminated = 1;
137 release();
138 }
139 }
140 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141};
142
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700143SortedVector<egl_object_t*> egl_object_t::sObjects;
144Mutex egl_object_t::sLock;
145
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700146struct egl_display_t {
147 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 struct strings_t {
150 char const * vendor;
151 char const * version;
152 char const * clientApi;
153 char const * extensions;
154 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700155
156 struct DisplayImpl {
157 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
158 state(NOT_INITIALIZED), numConfigs(0) { }
159 EGLDisplay dpy;
160 EGLConfig* config;
161 EGLint state;
162 EGLint numConfigs;
163 strings_t queryString;
164 };
165
166 uint32_t magic;
Mathias Agopian618fa102009-10-14 02:06:37 -0700167 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700168 EGLint numTotalConfigs;
Mathias Agopian75bc2782010-02-05 16:17:01 -0800169 uint32_t refs;
170 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700171
172 egl_display_t() : magic('_dpy'), numTotalConfigs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700173 ~egl_display_t() { magic = 0; }
174 inline bool isValid() const { return magic == '_dpy'; }
175 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176};
177
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700178struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700180 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 egl_surface_t(EGLDisplay dpy, EGLSurface surface,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700183 int impl, egl_connection_t const* cnx)
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700184 : dpy(dpy), surface(surface), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 }
186 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 }
188 EGLDisplay dpy;
189 EGLSurface surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 int impl;
191 egl_connection_t const* cnx;
192};
193
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700194struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700196 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 egl_context_t(EGLDisplay dpy, EGLContext context,
Mathias Agopian618fa102009-10-14 02:06:37 -0700199 int impl, egl_connection_t const* cnx, int version)
200 : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx),
201 version(version)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 {
203 }
204 EGLDisplay dpy;
205 EGLContext context;
206 EGLSurface read;
207 EGLSurface draw;
208 int impl;
209 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700210 int version;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211};
212
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700213struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700215 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
216
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700217 egl_image_t(EGLDisplay dpy, EGLContext context)
218 : dpy(dpy), context(context)
219 {
220 memset(images, 0, sizeof(images));
221 }
222 EGLDisplay dpy;
223 EGLConfig context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700224 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700225};
226
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700227typedef egl_surface_t::Ref SurfaceRef;
228typedef egl_context_t::Ref ContextRef;
229typedef egl_image_t::Ref ImageRef;
230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231struct tls_t
232{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700233 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 EGLint error;
235 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700236 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237};
238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239
240// ----------------------------------------------------------------------------
241
Mathias Agopianbf41b112010-04-09 13:37:34 -0700242static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243static egl_display_t gDisplay[NUM_DISPLAYS];
244static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
245static pthread_key_t gEGLThreadLocalStorageKey = -1;
246
247// ----------------------------------------------------------------------------
248
Mathias Agopian618fa102009-10-14 02:06:37 -0700249EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
250EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700251EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
253// ----------------------------------------------------------------------------
254
255static __attribute__((noinline))
256const char *egl_strerror(EGLint err)
257{
258 switch (err){
259 case EGL_SUCCESS: return "EGL_SUCCESS";
260 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
261 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
262 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
263 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
264 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
265 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
266 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
267 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
268 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
269 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
270 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
271 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
272 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
273 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
274 default: return "UNKNOWN";
275 }
276}
277
278static __attribute__((noinline))
279void clearTLS() {
280 if (gEGLThreadLocalStorageKey != -1) {
281 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
282 if (tls) {
283 delete tls;
284 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
285 }
286 }
287}
288
289static tls_t* getTLS()
290{
291 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
292 if (tls == 0) {
293 tls = new tls_t;
294 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
295 }
296 return tls;
297}
298
299template<typename T>
300static __attribute__((noinline))
301T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
302 if (gEGLThreadLocalStorageKey == -1) {
303 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
304 if (gEGLThreadLocalStorageKey == -1)
305 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
306 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
307 }
308 tls_t* tls = getTLS();
309 if (tls->error != error) {
310 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
311 tls->error = error;
312 }
313 return returnValue;
314}
315
316static __attribute__((noinline))
317GLint getError() {
318 if (gEGLThreadLocalStorageKey == -1)
319 return EGL_SUCCESS;
320 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
321 if (!tls) return EGL_SUCCESS;
322 GLint error = tls->error;
323 tls->error = EGL_SUCCESS;
324 return error;
325}
326
327static __attribute__((noinline))
328void setContext(EGLContext ctx) {
329 if (gEGLThreadLocalStorageKey == -1) {
330 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
331 if (gEGLThreadLocalStorageKey == -1)
332 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
333 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
334 }
335 tls_t* tls = getTLS();
336 tls->ctx = ctx;
337}
338
339static __attribute__((noinline))
340EGLContext getContext() {
341 if (gEGLThreadLocalStorageKey == -1)
342 return EGL_NO_CONTEXT;
343 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
344 if (!tls) return EGL_NO_CONTEXT;
345 return tls->ctx;
346}
347
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348/*****************************************************************************/
349
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350template<typename T>
351static __attribute__((noinline))
352int binarySearch(
353 T const sortedArray[], int first, int last, T key)
354{
355 while (first <= last) {
356 int mid = (first + last) / 2;
357 if (key > sortedArray[mid]) {
358 first = mid + 1;
359 } else if (key < sortedArray[mid]) {
360 last = mid - 1;
361 } else {
362 return mid;
363 }
364 }
365 return -1;
366}
367
368static EGLint configToUniqueId(egl_display_t const* dp, int i, int index)
369{
370 // NOTE: this mapping works only if we have no more than two EGLimpl
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700371 return (i>0 ? dp->disp[0].numConfigs : 0) + index;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372}
373
374static void uniqueIdToConfig(egl_display_t const* dp, EGLint configId,
375 int& i, int& index)
376{
377 // NOTE: this mapping works only if we have no more than two EGLimpl
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700378 size_t numConfigs = dp->disp[0].numConfigs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 i = configId / numConfigs;
380 index = configId % numConfigs;
381}
382
383static int cmp_configs(const void* a, const void *b)
384{
385 EGLConfig c0 = *(EGLConfig const *)a;
386 EGLConfig c1 = *(EGLConfig const *)b;
387 return c0<c1 ? -1 : (c0>c1 ? 1 : 0);
388}
389
390struct extention_map_t {
391 const char* name;
392 __eglMustCastToProperFunctionPointerType address;
393};
394
395static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700396 { "eglLockSurfaceKHR",
397 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
398 { "eglUnlockSurfaceKHR",
399 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
400 { "eglCreateImageKHR",
401 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
402 { "eglDestroyImageKHR",
403 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700404 { "eglSetSwapRectangleANDROID",
405 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
406 { "eglGetRenderBufferANDROID",
407 (__eglMustCastToProperFunctionPointerType)&eglGetRenderBufferANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408};
409
410static extention_map_t gGLExtentionMap[MAX_NUMBER_OF_GL_EXTENSIONS];
411
412static void(*findProcAddress(const char* name,
413 const extention_map_t* map, size_t n))()
414{
415 for (uint32_t i=0 ; i<n ; i++) {
416 if (!strcmp(name, map[i].name)) {
417 return map[i].address;
418 }
419 }
420 return NULL;
421}
422
423// ----------------------------------------------------------------------------
424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425static void gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700426 tls_t* tls = getTLS();
427 if (tls->logCallWithNoContext == EGL_TRUE) {
428 tls->logCallWithNoContext = EGL_FALSE;
429 LOGE("call to OpenGL ES API with no current context "
430 "(logged once per thread)");
431 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432}
Mathias Agopiand274eae2009-07-31 16:21:17 -0700433
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434static void early_egl_init(void)
435{
436#if !USE_FAST_TLS_KEY
437 pthread_key_create(&gGLWrapperKey, NULL);
438#endif
439 uint32_t addr = (uint32_t)((void*)gl_no_context);
440 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700441 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700443 sizeof(gHooksNoContext));
444 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445}
446
447static pthread_once_t once_control = PTHREAD_ONCE_INIT;
448static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
449
450
451static inline
452egl_display_t* get_display(EGLDisplay dpy)
453{
454 uintptr_t index = uintptr_t(dpy)-1U;
455 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
456}
457
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700458template<typename NATIVE, typename EGL>
459static inline NATIVE* egl_to_native_cast(EGL arg) {
460 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461}
462
463static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700464egl_surface_t* get_surface(EGLSurface surface) {
465 return egl_to_native_cast<egl_surface_t>(surface);
466}
467
468static inline
469egl_context_t* get_context(EGLContext context) {
470 return egl_to_native_cast<egl_context_t>(context);
471}
472
473static inline
474egl_image_t* get_image(EGLImageKHR image) {
475 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476}
477
478static egl_connection_t* validate_display_config(
479 EGLDisplay dpy, EGLConfig config,
480 egl_display_t const*& dp, int& impl, int& index)
481{
482 dp = get_display(dpy);
483 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
484
485 impl = uintptr_t(config)>>24;
Mathias Agopian618fa102009-10-14 02:06:37 -0700486 if (uint32_t(impl) >= IMPL_NUM_IMPLEMENTATIONS) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
488 }
489 index = uintptr_t(config) & 0xFFFFFF;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700490 if (index >= dp->disp[impl].numConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
492 }
493 egl_connection_t* const cnx = &gEGLImpl[impl];
494 if (cnx->dso == 0) {
495 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
496 }
497 return cnx;
498}
499
500static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
501{
502 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
503 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700504 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700506 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
508 return EGL_TRUE;
509}
510
511static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
512{
513 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
514 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700515 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700517 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 return setError(EGL_BAD_SURFACE, EGL_FALSE);
519 return EGL_TRUE;
520}
521
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700522EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
523{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700524 ImageRef _i(image);
525 if (!_i.get()) return EGL_NO_IMAGE_KHR;
526
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700527 EGLContext context = getContext();
528 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
529 return EGL_NO_IMAGE_KHR;
530
531 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700532 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700533 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700535 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536 return i->images[c->impl];
537}
538
Mathias Agopian923c6612009-08-17 18:07:06 -0700539// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700540
Mathias Agopian923c6612009-08-17 18:07:06 -0700541// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700542// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700543// egl_init_drivers_locked()
544//
545static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
546
547EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548{
549 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700550 // initialized by static ctor. should be set here.
551 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552 }
553
Mathias Agopiande586972009-05-28 17:39:03 -0700554 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700555 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700556
Mathias Agopian923c6612009-08-17 18:07:06 -0700557 // dynamically load all our EGL implementations for all displays
558 // and retrieve the corresponding EGLDisplay
559 // if that fails, don't use this driver.
560 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
561 egl_connection_t* cnx;
562 egl_display_t* d = &gDisplay[0];
563
564 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700566 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
567 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
568 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700569 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700570 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700571 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700572 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700573 if (dpy == EGL_NO_DISPLAY) {
574 loader.close(cnx->dso);
575 cnx->dso = NULL;
576 }
577 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578 }
579
580 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700581 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582 char value[PROPERTY_VALUE_MAX];
583 property_get("debug.egl.hw", value, "1");
584 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700585 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
586 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
587 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700588 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700589 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700590 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700591 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700592 if (dpy == EGL_NO_DISPLAY) {
593 loader.close(cnx->dso);
594 cnx->dso = NULL;
595 }
596 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597 } else {
598 LOGD("3D hardware acceleration is disabled");
599 }
600 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700601
Mathias Agopian923c6612009-08-17 18:07:06 -0700602 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
603 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 }
605
Mathias Agopian923c6612009-08-17 18:07:06 -0700606 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607}
608
Mathias Agopian923c6612009-08-17 18:07:06 -0700609EGLBoolean egl_init_drivers()
610{
611 EGLBoolean res;
612 pthread_mutex_lock(&gInitDriverMutex);
613 res = egl_init_drivers_locked();
614 pthread_mutex_unlock(&gInitDriverMutex);
615 return res;
616}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700617
618// ----------------------------------------------------------------------------
619}; // namespace android
620// ----------------------------------------------------------------------------
621
622using namespace android;
623
624EGLDisplay eglGetDisplay(NativeDisplayType display)
625{
Mathias Agopian923c6612009-08-17 18:07:06 -0700626 uint32_t index = uint32_t(display);
627 if (index >= NUM_DISPLAYS) {
628 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
629 }
630
631 if (egl_init_drivers() == EGL_FALSE) {
632 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
633 }
634
635 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
636 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700637}
638
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639// ----------------------------------------------------------------------------
640// Initialization
641// ----------------------------------------------------------------------------
642
643EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
644{
645 egl_display_t * const dp = get_display(dpy);
646 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
647
Mathias Agopian75bc2782010-02-05 16:17:01 -0800648 Mutex::Autolock _l(dp->lock);
649
650 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651 if (major != NULL) *major = VERSION_MAJOR;
652 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700653 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 return EGL_TRUE;
655 }
656
Mathias Agopian618fa102009-10-14 02:06:37 -0700657 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658
659 // initialize each EGL and
660 // build our own extension string first, based on the extension we know
661 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700662 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663 egl_connection_t* const cnx = &gEGLImpl[i];
664 cnx->major = -1;
665 cnx->minor = -1;
666 if (!cnx->dso)
667 continue;
668
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700669#if defined(ADRENO130)
670#warning "Adreno-130 eglInitialize() workaround"
671 /*
672 * The ADRENO 130 driver returns a different EGLDisplay each time
673 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
674 * after eglTerminate() has been called, so that eglInitialize()
675 * cannot be called again. Therefore, we need to make sure to call
676 * eglGetDisplay() before calling eglInitialize();
677 */
678 if (i == IMPL_HARDWARE) {
679 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700680 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700681 }
682#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800683
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700684
685 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700686 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700688 // i, idpy, cnx->major, cnx->minor, cnx);
689
690 // display is now initialized
691 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692
693 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700694 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700695 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700696 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700697 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700698 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700699 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700700 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700701 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702
703 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700704 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700705 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 }
707 }
708
709 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700710 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711 egl_connection_t* const cnx = &gEGLImpl[i];
712 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
713 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700714 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700715 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
716 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700717 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700718 dp->disp[i].dpy, dp->disp[i].config, n,
719 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 {
721 // sort the configurations so we can do binary searches
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700722 qsort( dp->disp[i].config,
723 dp->disp[i].numConfigs,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 sizeof(EGLConfig), cmp_configs);
725
726 dp->numTotalConfigs += n;
727 res = EGL_TRUE;
728 }
729 }
730 }
731 }
732 }
733
734 if (res == EGL_TRUE) {
Mathias Agopian75bc2782010-02-05 16:17:01 -0800735 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736 if (major != NULL) *major = VERSION_MAJOR;
737 if (minor != NULL) *minor = VERSION_MINOR;
738 return EGL_TRUE;
739 }
740 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
741}
742
743EGLBoolean eglTerminate(EGLDisplay dpy)
744{
Mathias Agopian923c6612009-08-17 18:07:06 -0700745 // NOTE: don't unload the drivers b/c some APIs can be called
746 // after eglTerminate() has been called. eglTerminate() only
747 // terminates an EGLDisplay, not a EGL itself.
748
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 egl_display_t* const dp = get_display(dpy);
750 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800751
752 Mutex::Autolock _l(dp->lock);
753
754 if (dp->refs == 0) {
755 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
756 }
757
758 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700759 if (dp->refs > 1) {
760 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700762 }
Mathias Agopiande586972009-05-28 17:39:03 -0700763
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700765 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700767 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700768 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700769 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700770 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700771 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700772 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700773 free(dp->disp[i].config);
774
775 dp->disp[i].numConfigs = 0;
776 dp->disp[i].config = 0;
777 dp->disp[i].state = egl_display_t::TERMINATED;
778
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 res = EGL_TRUE;
780 }
781 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700782
783 // TODO: all egl_object_t should be marked for termination
784
Mathias Agopian75bc2782010-02-05 16:17:01 -0800785 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 dp->numTotalConfigs = 0;
787 clearTLS();
788 return res;
789}
790
791// ----------------------------------------------------------------------------
792// configuration
793// ----------------------------------------------------------------------------
794
795EGLBoolean eglGetConfigs( EGLDisplay dpy,
796 EGLConfig *configs,
797 EGLint config_size, EGLint *num_config)
798{
799 egl_display_t const * const dp = get_display(dpy);
800 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
801
802 GLint numConfigs = dp->numTotalConfigs;
803 if (!configs) {
804 *num_config = numConfigs;
805 return EGL_TRUE;
806 }
807 GLint n = 0;
Mathias Agopian618fa102009-10-14 02:06:37 -0700808 for (int j=0 ; j<IMPL_NUM_IMPLEMENTATIONS ; j++) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700809 for (int i=0 ; i<dp->disp[j].numConfigs && config_size ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810 *configs++ = MAKE_CONFIG(j, i);
811 config_size--;
812 n++;
813 }
814 }
815
816 *num_config = n;
817 return EGL_TRUE;
818}
819
820EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
821 EGLConfig *configs, EGLint config_size,
822 EGLint *num_config)
823{
824 egl_display_t const * const dp = get_display(dpy);
825 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
826
Jack Palevich749c63d2009-03-25 15:12:17 -0700827 if (num_config==0) {
828 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829 }
830
831 EGLint n;
832 EGLBoolean res = EGL_FALSE;
833 *num_config = 0;
834
835
836 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
837 // to do this, we have to go through the attrib_list array once
838 // to figure out both its size and if it contains an EGL_CONFIG_ID
839 // key. If so, the full array is copied and patched.
840 // NOTE: we assume that there can be only one occurrence
841 // of EGL_CONFIG_ID.
842
843 EGLint patch_index = -1;
844 GLint attr;
845 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -0700846 if (attrib_list) {
847 while ((attr=attrib_list[size]) != EGL_NONE) {
848 if (attr == EGL_CONFIG_ID)
849 patch_index = size;
850 size += 2;
851 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 }
853 if (patch_index >= 0) {
854 size += 2; // we need copy the sentinel as well
855 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
856 if (new_list == 0)
857 return setError(EGL_BAD_ALLOC, EGL_FALSE);
858 memcpy(new_list, attrib_list, size*sizeof(EGLint));
859
860 // patch the requested EGL_CONFIG_ID
861 int i, index;
862 EGLint& configId(new_list[patch_index+1]);
863 uniqueIdToConfig(dp, configId, i, index);
864
865 egl_connection_t* const cnx = &gEGLImpl[i];
866 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700867 cnx->egl.eglGetConfigAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700868 dp->disp[i].dpy, dp->disp[i].config[index],
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869 EGL_CONFIG_ID, &configId);
870
871 // and switch to the new list
872 attrib_list = const_cast<const EGLint *>(new_list);
873
874 // At this point, the only configuration that can match is
875 // dp->configs[i][index], however, we don't know if it would be
876 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -0700877 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 // through all the EGLimpl[].
879 // We also know we can only get a single config back, and we know
880 // which one.
881
Mathias Agopian618fa102009-10-14 02:06:37 -0700882 res = cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700883 dp->disp[i].dpy, attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800884 if (res && n>0) {
885 // n has to be 0 or 1, by construction, and we already know
886 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -0700887 if (configs) {
888 configs[0] = MAKE_CONFIG(i, index);
889 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890 *num_config = 1;
891 }
892 }
893
894 free(const_cast<EGLint *>(attrib_list));
895 return res;
896 }
897
Mathias Agopian618fa102009-10-14 02:06:37 -0700898 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 egl_connection_t* const cnx = &gEGLImpl[i];
900 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700901 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700902 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -0700903 if (configs) {
904 // now we need to convert these client EGLConfig to our
905 // internal EGLConfig format. This is done in O(n log n).
906 for (int j=0 ; j<n ; j++) {
907 int index = binarySearch<EGLConfig>(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700908 dp->disp[i].config, 0,
909 dp->disp[i].numConfigs-1, configs[j]);
Jack Palevich749c63d2009-03-25 15:12:17 -0700910 if (index >= 0) {
911 if (configs) {
912 configs[j] = MAKE_CONFIG(i, index);
913 }
914 } else {
915 return setError(EGL_BAD_CONFIG, EGL_FALSE);
916 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 }
Jack Palevich749c63d2009-03-25 15:12:17 -0700918 configs += n;
919 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 *num_config += n;
922 res = EGL_TRUE;
923 }
924 }
925 }
926 return res;
927}
928
929EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
930 EGLint attribute, EGLint *value)
931{
932 egl_display_t const* dp = 0;
933 int i=0, index=0;
934 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
935 if (!cnx) return EGL_FALSE;
936
937 if (attribute == EGL_CONFIG_ID) {
938 // EGL_CONFIG_IDs must be unique, just use the order of the selected
939 // EGLConfig.
940 *value = configToUniqueId(dp, i, index);
941 return EGL_TRUE;
942 }
Mathias Agopian618fa102009-10-14 02:06:37 -0700943 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700944 dp->disp[i].dpy, dp->disp[i].config[index], attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945}
946
947// ----------------------------------------------------------------------------
948// surfaces
949// ----------------------------------------------------------------------------
950
951EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
952 NativeWindowType window,
953 const EGLint *attrib_list)
954{
955 egl_display_t const* dp = 0;
956 int i=0, index=0;
957 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
958 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700959 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700960 dp->disp[i].dpy, dp->disp[i].config[index], window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961 if (surface != EGL_NO_SURFACE) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700962 egl_surface_t* s = new egl_surface_t(dpy, surface, i, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800963 return s;
964 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965 }
966 return EGL_NO_SURFACE;
967}
968
969EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
970 NativePixmapType pixmap,
971 const EGLint *attrib_list)
972{
973 egl_display_t const* dp = 0;
974 int i=0, index=0;
975 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
976 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700977 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700978 dp->disp[i].dpy, dp->disp[i].config[index], pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979 if (surface != EGL_NO_SURFACE) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700980 egl_surface_t* s = new egl_surface_t(dpy, surface, i, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 return s;
982 }
983 }
984 return EGL_NO_SURFACE;
985}
986
987EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
988 const EGLint *attrib_list)
989{
990 egl_display_t const* dp = 0;
991 int i=0, index=0;
992 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
993 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700994 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700995 dp->disp[i].dpy, dp->disp[i].config[index], attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 if (surface != EGL_NO_SURFACE) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700997 egl_surface_t* s = new egl_surface_t(dpy, surface, i, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 return s;
999 }
1000 }
1001 return EGL_NO_SURFACE;
1002}
1003
1004EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1005{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001006 SurfaceRef _s(surface);
1007 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1008
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 if (!validate_display_surface(dpy, surface))
1010 return EGL_FALSE;
1011 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001013 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001014 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001015 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001016 if (result == EGL_TRUE) {
1017 _s.terminate();
1018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 return result;
1020}
1021
1022EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1023 EGLint attribute, EGLint *value)
1024{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001025 SurfaceRef _s(surface);
1026 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1027
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 if (!validate_display_surface(dpy, surface))
1029 return EGL_FALSE;
1030 egl_display_t const * const dp = get_display(dpy);
1031 egl_surface_t const * const s = get_surface(surface);
1032
Mathias Agopian618fa102009-10-14 02:06:37 -07001033 return s->cnx->egl.eglQuerySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001034 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035}
1036
1037// ----------------------------------------------------------------------------
1038// contextes
1039// ----------------------------------------------------------------------------
1040
1041EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1042 EGLContext share_list, const EGLint *attrib_list)
1043{
1044 egl_display_t const* dp = 0;
1045 int i=0, index=0;
1046 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1047 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001048 if (share_list != EGL_NO_CONTEXT) {
1049 egl_context_t* const c = get_context(share_list);
1050 share_list = c->context;
1051 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001052 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001053 dp->disp[i].dpy, dp->disp[i].config[index],
1054 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001056 // figure out if it's a GLESv1 or GLESv2
1057 int version = 0;
1058 if (attrib_list) {
1059 while (*attrib_list != EGL_NONE) {
1060 GLint attr = *attrib_list++;
1061 GLint value = *attrib_list++;
1062 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1063 if (value == 1) {
1064 version = GLESv1_INDEX;
1065 } else if (value == 2) {
1066 version = GLESv2_INDEX;
1067 }
1068 }
1069 };
1070 }
1071 egl_context_t* c = new egl_context_t(dpy, context, i, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 return c;
1073 }
1074 }
1075 return EGL_NO_CONTEXT;
1076}
1077
1078EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1079{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001080 ContextRef _c(ctx);
1081 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 if (!validate_display_context(dpy, ctx))
1084 return EGL_FALSE;
1085 egl_display_t const * const dp = get_display(dpy);
1086 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001087 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001088 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001089 if (result == EGL_TRUE) {
1090 _c.terminate();
1091 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 return result;
1093}
1094
1095EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1096 EGLSurface read, EGLContext ctx)
1097{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001098 // get a reference to the object passed in
1099 ContextRef _c(ctx);
1100 SurfaceRef _d(draw);
1101 SurfaceRef _r(read);
1102
1103 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 egl_display_t const * const dp = get_display(dpy);
1105 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001106 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1107 // EGL_NO_CONTEXT is valid
1108 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 }
1110
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001111 // these are the underlying implementation's object
1112 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001113 EGLSurface impl_draw = EGL_NO_SURFACE;
1114 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001115
1116 // these are our objects structs passed in
1117 egl_context_t * c = NULL;
1118 egl_surface_t const * d = NULL;
1119 egl_surface_t const * r = NULL;
1120
1121 // these are the current objects structs
1122 egl_context_t * cur_c = get_context(getContext());
1123 egl_surface_t * cur_r = NULL;
1124 egl_surface_t * cur_d = NULL;
1125
1126 if (ctx != EGL_NO_CONTEXT) {
1127 c = get_context(ctx);
1128 cur_r = get_surface(c->read);
1129 cur_d = get_surface(c->draw);
1130 impl_ctx = c->context;
1131 } else {
1132 // no context given, use the implementation of the current context
1133 if (cur_c == NULL) {
1134 // no current context
1135 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001136 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1137 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001138 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001139 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001140 return EGL_TRUE;
1141 }
1142 }
1143
1144 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001146 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001147 // make sure the EGLContext and EGLSurface passed in are for
1148 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001149 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001151 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001153
1154 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001155 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001156 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001157 // make sure the EGLContext and EGLSurface passed in are for
1158 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001159 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001161 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001163
1164 EGLBoolean result;
1165
1166 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001167 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001168 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001169 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001170 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001171 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001172 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173
1174 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001175 // by construction, these are either 0 or valid (possibly terminated)
1176 // it should be impossible for these to be invalid
1177 ContextRef _cur_c(cur_c);
1178 SurfaceRef _cur_r(cur_r);
1179 SurfaceRef _cur_d(cur_d);
1180
1181 // cur_c has to be valid here (but could be terminated)
1182 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001183 setGlThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001184 setContext(ctx);
1185 _c.acquire();
1186 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001187 setGlThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001188 setContext(EGL_NO_CONTEXT);
1189 }
1190 _cur_c.release();
1191
1192 _r.acquire();
1193 _cur_r.release();
1194 if (c) c->read = read;
1195
1196 _d.acquire();
1197 _cur_d.release();
1198 if (c) c->draw = draw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 }
1200 return result;
1201}
1202
1203
1204EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1205 EGLint attribute, EGLint *value)
1206{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001207 ContextRef _c(ctx);
1208 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 if (!validate_display_context(dpy, ctx))
1211 return EGL_FALSE;
1212
1213 egl_display_t const * const dp = get_display(dpy);
1214 egl_context_t * const c = get_context(ctx);
1215
Mathias Agopian618fa102009-10-14 02:06:37 -07001216 return c->cnx->egl.eglQueryContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001217 dp->disp[c->impl].dpy, c->context, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218}
1219
1220EGLContext eglGetCurrentContext(void)
1221{
Mathias Agopian923c6612009-08-17 18:07:06 -07001222 // could be called before eglInitialize(), but we wouldn't have a context
1223 // then, and this function would correctly return EGL_NO_CONTEXT.
1224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 EGLContext ctx = getContext();
1226 return ctx;
1227}
1228
1229EGLSurface eglGetCurrentSurface(EGLint readdraw)
1230{
Mathias Agopian923c6612009-08-17 18:07:06 -07001231 // could be called before eglInitialize(), but we wouldn't have a context
1232 // then, and this function would correctly return EGL_NO_SURFACE.
1233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 EGLContext ctx = getContext();
1235 if (ctx) {
1236 egl_context_t const * const c = get_context(ctx);
1237 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1238 switch (readdraw) {
1239 case EGL_READ: return c->read;
1240 case EGL_DRAW: return c->draw;
1241 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1242 }
1243 }
1244 return EGL_NO_SURFACE;
1245}
1246
1247EGLDisplay eglGetCurrentDisplay(void)
1248{
Mathias Agopian923c6612009-08-17 18:07:06 -07001249 // could be called before eglInitialize(), but we wouldn't have a context
1250 // then, and this function would correctly return EGL_NO_DISPLAY.
1251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 EGLContext ctx = getContext();
1253 if (ctx) {
1254 egl_context_t const * const c = get_context(ctx);
1255 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1256 return c->dpy;
1257 }
1258 return EGL_NO_DISPLAY;
1259}
1260
1261EGLBoolean eglWaitGL(void)
1262{
Mathias Agopian923c6612009-08-17 18:07:06 -07001263 // could be called before eglInitialize(), but we wouldn't have a context
1264 // then, and this function would return GL_TRUE, which isn't wrong.
1265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 EGLBoolean res = EGL_TRUE;
1267 EGLContext ctx = getContext();
1268 if (ctx) {
1269 egl_context_t const * const c = get_context(ctx);
1270 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1271 if (uint32_t(c->impl)>=2)
1272 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1273 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1274 if (!cnx->dso)
1275 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001276 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 }
1278 return res;
1279}
1280
1281EGLBoolean eglWaitNative(EGLint engine)
1282{
Mathias Agopian923c6612009-08-17 18:07:06 -07001283 // could be called before eglInitialize(), but we wouldn't have a context
1284 // then, and this function would return GL_TRUE, which isn't wrong.
1285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 EGLBoolean res = EGL_TRUE;
1287 EGLContext ctx = getContext();
1288 if (ctx) {
1289 egl_context_t const * const c = get_context(ctx);
1290 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1291 if (uint32_t(c->impl)>=2)
1292 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1293 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1294 if (!cnx->dso)
1295 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001296 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 }
1298 return res;
1299}
1300
1301EGLint eglGetError(void)
1302{
1303 EGLint result = EGL_SUCCESS;
Mathias Agopian618fa102009-10-14 02:06:37 -07001304 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 EGLint err = EGL_SUCCESS;
1306 egl_connection_t* const cnx = &gEGLImpl[i];
1307 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001308 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1310 result = err;
1311 }
1312 if (result == EGL_SUCCESS)
1313 result = getError();
1314 return result;
1315}
1316
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001317__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001319 // eglGetProcAddress() could be the very first function called
1320 // in which case we must make sure we've initialized ourselves, this
1321 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001322
1323 if (egl_init_drivers() == EGL_FALSE) {
1324 setError(EGL_BAD_PARAMETER, NULL);
1325 return NULL;
1326 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 __eglMustCastToProperFunctionPointerType addr;
1329 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1330 if (addr) return addr;
1331
1332 return NULL; // TODO: finish implementation below
1333
1334 addr = findProcAddress(procname, gGLExtentionMap, NELEM(gGLExtentionMap));
1335 if (addr) return addr;
1336
1337 addr = 0;
1338 int slot = -1;
Mathias Agopian618fa102009-10-14 02:06:37 -07001339 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 egl_connection_t* const cnx = &gEGLImpl[i];
1341 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001342 if (cnx->egl.eglGetProcAddress) {
1343 addr = cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 if (addr) {
1345 if (slot == -1) {
1346 slot = 0; // XXX: find free slot
1347 if (slot == -1) {
1348 addr = 0;
1349 break;
1350 }
1351 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001352 //cnx->hooks->ext.extensions[slot] = addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 }
1354 }
1355 }
1356 }
1357
1358 if (slot >= 0) {
1359 addr = 0; // XXX: address of stub 'slot'
1360 gGLExtentionMap[slot].name = strdup(procname);
1361 gGLExtentionMap[slot].address = addr;
1362 }
1363
1364 return addr;
1365
1366
1367 /*
1368 * TODO: For OpenGL ES extensions, we must generate a stub
1369 * that looks like
1370 * mov r12, #0xFFFF0FFF
1371 * ldr r12, [r12, #-15]
1372 * ldr r12, [r12, #TLS_SLOT_OPENGL_API*4]
1373 * mov r12, [r12, #api_offset]
1374 * ldrne pc, r12
1375 * mov pc, #unsupported_extension
1376 *
1377 * and write the address of the extension in *all*
1378 * gl_hooks_t::gl_ext_t at offset "api_offset" from gl_hooks_t
1379 *
1380 */
1381}
1382
1383EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1384{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001385 SurfaceRef _s(draw);
1386 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1387
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 if (!validate_display_surface(dpy, draw))
1389 return EGL_FALSE;
1390 egl_display_t const * const dp = get_display(dpy);
1391 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001392 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393}
1394
1395EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1396 NativePixmapType target)
1397{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001398 SurfaceRef _s(surface);
1399 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1400
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 if (!validate_display_surface(dpy, surface))
1402 return EGL_FALSE;
1403 egl_display_t const * const dp = get_display(dpy);
1404 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001405 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001406 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407}
1408
1409const char* eglQueryString(EGLDisplay dpy, EGLint name)
1410{
1411 egl_display_t const * const dp = get_display(dpy);
1412 switch (name) {
1413 case EGL_VENDOR:
1414 return gVendorString;
1415 case EGL_VERSION:
1416 return gVersionString;
1417 case EGL_EXTENSIONS:
1418 return gExtensionString;
1419 case EGL_CLIENT_APIS:
1420 return gClientApiString;
1421 }
1422 return setError(EGL_BAD_PARAMETER, (const char *)0);
1423}
1424
1425
1426// ----------------------------------------------------------------------------
1427// EGL 1.1
1428// ----------------------------------------------------------------------------
1429
1430EGLBoolean eglSurfaceAttrib(
1431 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1432{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001433 SurfaceRef _s(surface);
1434 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1435
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 if (!validate_display_surface(dpy, surface))
1437 return EGL_FALSE;
1438 egl_display_t const * const dp = get_display(dpy);
1439 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001440 if (s->cnx->egl.eglSurfaceAttrib) {
1441 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001442 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
1444 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1445}
1446
1447EGLBoolean eglBindTexImage(
1448 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1449{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001450 SurfaceRef _s(surface);
1451 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1452
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453 if (!validate_display_surface(dpy, surface))
1454 return EGL_FALSE;
1455 egl_display_t const * const dp = get_display(dpy);
1456 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001457 if (s->cnx->egl.eglBindTexImage) {
1458 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001459 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 }
1461 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1462}
1463
1464EGLBoolean eglReleaseTexImage(
1465 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1466{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001467 SurfaceRef _s(surface);
1468 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1469
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 if (!validate_display_surface(dpy, surface))
1471 return EGL_FALSE;
1472 egl_display_t const * const dp = get_display(dpy);
1473 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001474 if (s->cnx->egl.eglReleaseTexImage) {
1475 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001476 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 }
1478 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1479}
1480
1481EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1482{
1483 egl_display_t * const dp = get_display(dpy);
1484 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1485
1486 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001487 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 egl_connection_t* const cnx = &gEGLImpl[i];
1489 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001490 if (cnx->egl.eglSwapInterval) {
1491 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001492 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493 res = EGL_FALSE;
1494 }
1495 }
1496 }
1497 }
1498 return res;
1499}
1500
1501
1502// ----------------------------------------------------------------------------
1503// EGL 1.2
1504// ----------------------------------------------------------------------------
1505
1506EGLBoolean eglWaitClient(void)
1507{
Mathias Agopian923c6612009-08-17 18:07:06 -07001508 // could be called before eglInitialize(), but we wouldn't have a context
1509 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 EGLBoolean res = EGL_TRUE;
1511 EGLContext ctx = getContext();
1512 if (ctx) {
1513 egl_context_t const * const c = get_context(ctx);
1514 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1515 if (uint32_t(c->impl)>=2)
1516 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1517 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1518 if (!cnx->dso)
1519 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001520 if (cnx->egl.eglWaitClient) {
1521 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001523 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 }
1525 }
1526 return res;
1527}
1528
1529EGLBoolean eglBindAPI(EGLenum api)
1530{
Mathias Agopian923c6612009-08-17 18:07:06 -07001531 if (egl_init_drivers() == EGL_FALSE) {
1532 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1533 }
1534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 // bind this API on all EGLs
1536 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001537 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 egl_connection_t* const cnx = &gEGLImpl[i];
1539 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001540 if (cnx->egl.eglBindAPI) {
1541 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001542 res = EGL_FALSE;
1543 }
1544 }
1545 }
1546 }
1547 return res;
1548}
1549
1550EGLenum eglQueryAPI(void)
1551{
Mathias Agopian923c6612009-08-17 18:07:06 -07001552 if (egl_init_drivers() == EGL_FALSE) {
1553 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1554 }
1555
Mathias Agopian618fa102009-10-14 02:06:37 -07001556 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557 egl_connection_t* const cnx = &gEGLImpl[i];
1558 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001559 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560 // the first one we find is okay, because they all
1561 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001562 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563 }
1564 }
1565 }
1566 // or, it can only be OpenGL ES
1567 return EGL_OPENGL_ES_API;
1568}
1569
1570EGLBoolean eglReleaseThread(void)
1571{
Mathias Agopian618fa102009-10-14 02:06:37 -07001572 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 egl_connection_t* const cnx = &gEGLImpl[i];
1574 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001575 if (cnx->egl.eglReleaseThread) {
1576 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577 }
1578 }
1579 }
1580 clearTLS();
1581 return EGL_TRUE;
1582}
1583
1584EGLSurface eglCreatePbufferFromClientBuffer(
1585 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1586 EGLConfig config, const EGLint *attrib_list)
1587{
1588 egl_display_t const* dp = 0;
1589 int i=0, index=0;
1590 egl_connection_t* cnx = validate_display_config(dpy, config, dp, i, index);
1591 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001592 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1593 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001594 dp->disp[i].dpy, buftype, buffer,
1595 dp->disp[i].config[index], attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596 }
1597 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1598}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001599
1600// ----------------------------------------------------------------------------
1601// EGL_EGLEXT_VERSION 3
1602// ----------------------------------------------------------------------------
1603
1604EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1605 const EGLint *attrib_list)
1606{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001607 SurfaceRef _s(surface);
1608 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1609
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001610 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001611 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001612
1613 egl_display_t const * const dp = get_display(dpy);
1614 egl_surface_t const * const s = get_surface(surface);
1615
Mathias Agopian618fa102009-10-14 02:06:37 -07001616 if (s->cnx->egl.eglLockSurfaceKHR) {
1617 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001618 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001619 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001620 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001621}
1622
1623EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1624{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001625 SurfaceRef _s(surface);
1626 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1627
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001628 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001629 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001630
1631 egl_display_t const * const dp = get_display(dpy);
1632 egl_surface_t const * const s = get_surface(surface);
1633
Mathias Agopian618fa102009-10-14 02:06:37 -07001634 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1635 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001636 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001637 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001638 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001639}
1640
1641EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1642 EGLClientBuffer buffer, const EGLint *attrib_list)
1643{
1644 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001645 ContextRef _c(ctx);
1646 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001647 if (!validate_display_context(dpy, ctx))
1648 return EGL_NO_IMAGE_KHR;
1649 egl_display_t const * const dp = get_display(dpy);
1650 egl_context_t * const c = get_context(ctx);
1651 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001652 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001653 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001654 if (image == EGL_NO_IMAGE_KHR)
1655 return image;
1656
1657 egl_image_t* result = new egl_image_t(dpy, ctx);
1658 result->images[c->impl] = image;
1659 return (EGLImageKHR)result;
1660 } else {
1661 // EGL_NO_CONTEXT is a valid parameter
1662 egl_display_t const * const dp = get_display(dpy);
1663 if (dp == 0) {
1664 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1665 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001666
1667 /* Since we don't have a way to know which implementation to call,
1668 * we're calling all of them. If at least one of the implementation
1669 * succeeded, this is a success.
1670 */
1671
1672 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001673
Mathias Agopian618fa102009-10-14 02:06:37 -07001674 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001675 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001676 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001677 egl_connection_t* const cnx = &gEGLImpl[i];
1678 implImages[i] = EGL_NO_IMAGE_KHR;
1679 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001680 if (cnx->egl.eglCreateImageKHR) {
1681 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001682 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001683 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1684 success = true;
1685 }
1686 }
1687 }
1688 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001689
1690 if (!success) {
1691 // failure, if there was an error when we entered this function,
1692 // the error flag must not be updated.
1693 // Otherwise, the error is whatever happened in the implementation
1694 // that faulted.
1695 if (currentError != EGL_SUCCESS) {
1696 setError(currentError, EGL_NO_IMAGE_KHR);
1697 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001698 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001699 } else {
1700 // In case of success, we need to clear all error flags
1701 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07001702 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001703 // a "full" success (all implementation succeeded).
1704 eglGetError();
1705 }
1706
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001707 egl_image_t* result = new egl_image_t(dpy, ctx);
1708 memcpy(result->images, implImages, sizeof(implImages));
1709 return (EGLImageKHR)result;
1710 }
1711}
1712
1713EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1714{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001715 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001716 if (dp == 0) {
1717 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1718 }
1719
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001720 ImageRef _i(img);
1721 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001722
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001723 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001724 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001725 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001726 egl_connection_t* const cnx = &gEGLImpl[i];
1727 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1728 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001729 if (cnx->egl.eglCreateImageKHR) {
1730 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001731 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001732 success = true;
1733 }
1734 }
1735 }
1736 }
1737 }
1738 if (!success)
1739 return EGL_FALSE;
1740
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001741 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001742
Mathias Agopian24e5f522009-08-12 21:18:15 -07001743 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001744}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001745
1746
1747// ----------------------------------------------------------------------------
1748// ANDROID extensions
1749// ----------------------------------------------------------------------------
1750
1751EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1752 EGLint left, EGLint top, EGLint width, EGLint height)
1753{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001754 SurfaceRef _s(draw);
1755 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1756
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001757 if (!validate_display_surface(dpy, draw))
1758 return EGL_FALSE;
1759 egl_display_t const * const dp = get_display(dpy);
1760 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001761 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
1762 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001763 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001764 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001765 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001766}
1767
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001768EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
1769{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001770 SurfaceRef _s(draw);
1771 if (!_s.get()) return setError(EGL_BAD_SURFACE, (EGLClientBuffer*)0);
1772
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001773 if (!validate_display_surface(dpy, draw))
1774 return 0;
1775 egl_display_t const * const dp = get_display(dpy);
1776 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001777 if (s->cnx->egl.eglGetRenderBufferANDROID) {
1778 return s->cnx->egl.eglGetRenderBufferANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001779 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001780 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001781 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer*)0);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001782}