blob: 315a2a361631ec06281e234ce748c603f73db0cb [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>
Mathias Agopian24035332010-08-02 17:34:32 -070040#include <utils/KeyedVector.h>
41#include <utils/String8.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070042
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043#include "hooks.h"
44#include "egl_impl.h"
Mathias Agopiande586972009-05-28 17:39:03 -070045#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
48
49// ----------------------------------------------------------------------------
50namespace android {
51// ----------------------------------------------------------------------------
52
53#define VERSION_MAJOR 1
54#define VERSION_MINOR 4
55static char const * const gVendorString = "Android";
Mathias Agopian923c6612009-08-17 18:07:06 -070056static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -070058static char const * const gExtensionString =
59 "EGL_KHR_image "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -070060 "EGL_KHR_image_base "
61 "EGL_KHR_image_pixmap "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070062 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070063 "EGL_ANDROID_swap_rectangle "
Mathias Agopian8d2e83b2009-06-24 22:37:39 -070064 "EGL_ANDROID_get_render_buffer "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070065 ;
66
67// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopian9429e9c2009-08-21 02:18:25 -070069class egl_object_t {
70 static SortedVector<egl_object_t*> sObjects;
71 static Mutex sLock;
72
73 volatile int32_t terminated;
74 mutable volatile int32_t count;
75
76public:
77 egl_object_t() : terminated(0), count(1) {
78 Mutex::Autolock _l(sLock);
79 sObjects.add(this);
80 }
81
82 inline bool isAlive() const { return !terminated; }
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -070085 bool get() {
86 Mutex::Autolock _l(sLock);
87 if (egl_object_t::sObjects.indexOf(this) >= 0) {
88 android_atomic_inc(&count);
89 return true;
90 }
91 return false;
92 }
93
94 bool put() {
95 Mutex::Autolock _l(sLock);
96 if (android_atomic_dec(&count) == 1) {
97 sObjects.remove(this);
98 return true;
99 }
100 return false;
101 }
102
103public:
104 template <typename N, typename T>
105 struct LocalRef {
106 N* ref;
107 LocalRef(T o) : ref(0) {
108 N* native = reinterpret_cast<N*>(o);
109 if (o && native->get()) {
110 ref = native;
111 }
112 }
113 ~LocalRef() {
114 if (ref && ref->put()) {
115 delete ref;
116 }
117 }
118 inline N* get() {
119 return ref;
120 }
121 void acquire() const {
122 if (ref) {
123 android_atomic_inc(&ref->count);
124 }
125 }
126 void release() const {
127 if (ref) {
128 int32_t c = android_atomic_dec(&ref->count);
129 // ref->count cannot be 1 prior atomic_dec because we have
130 // a reference, and if we have one, it means there was
131 // already one before us.
132 LOGE_IF(c==1, "refcount is now 0 in release()");
133 }
134 }
135 void terminate() {
136 if (ref) {
137 ref->terminated = 1;
138 release();
139 }
140 }
141 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142};
143
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700144SortedVector<egl_object_t*> egl_object_t::sObjects;
145Mutex egl_object_t::sLock;
146
Mathias Agopiancee79392010-07-26 21:14:59 -0700147
148struct egl_config_t {
149 egl_config_t() {}
150 egl_config_t(int impl, EGLConfig config)
151 : impl(impl), config(config), configId(0), implConfigId(0) { }
152 int impl; // the implementation this config is for
153 EGLConfig config; // the implementation's EGLConfig
154 EGLint configId; // our CONFIG_ID
155 EGLint implConfigId; // the implementation's CONFIG_ID
156 inline bool operator < (const egl_config_t& rhs) const {
157 if (impl < rhs.impl) return true;
158 if (impl > rhs.impl) return false;
159 return config < rhs.config;
160 }
161};
162
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700163struct egl_display_t {
164 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 struct strings_t {
167 char const * vendor;
168 char const * version;
169 char const * clientApi;
170 char const * extensions;
171 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700172
173 struct DisplayImpl {
174 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
175 state(NOT_INITIALIZED), numConfigs(0) { }
176 EGLDisplay dpy;
177 EGLConfig* config;
178 EGLint state;
179 EGLint numConfigs;
180 strings_t queryString;
181 };
182
Mathias Agopiancee79392010-07-26 21:14:59 -0700183 uint32_t magic;
184 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
185 EGLint numTotalConfigs;
186 egl_config_t* configs;
187 uint32_t refs;
188 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700189
Mathias Agopiancee79392010-07-26 21:14:59 -0700190 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700191 ~egl_display_t() { magic = 0; }
192 inline bool isValid() const { return magic == '_dpy'; }
193 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194};
195
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700196struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700198 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
199
Mathias Agopiancee79392010-07-26 21:14:59 -0700200 egl_surface_t(EGLDisplay dpy, EGLSurface surface, EGLConfig config,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700201 int impl, egl_connection_t const* cnx)
Mathias Agopiancee79392010-07-26 21:14:59 -0700202 : dpy(dpy), surface(surface), config(config), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 }
204 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 }
206 EGLDisplay dpy;
207 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700208 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 int impl;
210 egl_connection_t const* cnx;
211};
212
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700213struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700215 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
216
Mathias Agopiancee79392010-07-26 21:14:59 -0700217 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian618fa102009-10-14 02:06:37 -0700218 int impl, egl_connection_t const* cnx, int version)
219 : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx),
220 version(version)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 {
222 }
223 EGLDisplay dpy;
224 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700225 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 EGLSurface read;
227 EGLSurface draw;
228 int impl;
229 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700230 int version;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231};
232
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700233struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700234{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700235 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
236
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700237 egl_image_t(EGLDisplay dpy, EGLContext context)
238 : dpy(dpy), context(context)
239 {
240 memset(images, 0, sizeof(images));
241 }
242 EGLDisplay dpy;
243 EGLConfig context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700244 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700245};
246
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700247typedef egl_surface_t::Ref SurfaceRef;
248typedef egl_context_t::Ref ContextRef;
249typedef egl_image_t::Ref ImageRef;
250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251struct tls_t
252{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700253 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 EGLint error;
255 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700256 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257};
258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259
260// ----------------------------------------------------------------------------
261
Mathias Agopianbf41b112010-04-09 13:37:34 -0700262static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263static egl_display_t gDisplay[NUM_DISPLAYS];
264static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
265static pthread_key_t gEGLThreadLocalStorageKey = -1;
266
267// ----------------------------------------------------------------------------
268
Mathias Agopian618fa102009-10-14 02:06:37 -0700269EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
270EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700271EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272
273// ----------------------------------------------------------------------------
274
275static __attribute__((noinline))
276const char *egl_strerror(EGLint err)
277{
278 switch (err){
279 case EGL_SUCCESS: return "EGL_SUCCESS";
280 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
281 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
282 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
283 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
284 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
285 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
286 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
287 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
288 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
289 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
290 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
291 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
292 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
293 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
294 default: return "UNKNOWN";
295 }
296}
297
298static __attribute__((noinline))
299void clearTLS() {
300 if (gEGLThreadLocalStorageKey != -1) {
301 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
302 if (tls) {
303 delete tls;
304 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
305 }
306 }
307}
308
309static tls_t* getTLS()
310{
311 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
312 if (tls == 0) {
313 tls = new tls_t;
314 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
315 }
316 return tls;
317}
318
319template<typename T>
320static __attribute__((noinline))
321T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
322 if (gEGLThreadLocalStorageKey == -1) {
323 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
324 if (gEGLThreadLocalStorageKey == -1)
325 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
326 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
327 }
328 tls_t* tls = getTLS();
329 if (tls->error != error) {
330 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
331 tls->error = error;
332 }
333 return returnValue;
334}
335
336static __attribute__((noinline))
337GLint getError() {
338 if (gEGLThreadLocalStorageKey == -1)
339 return EGL_SUCCESS;
340 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
341 if (!tls) return EGL_SUCCESS;
342 GLint error = tls->error;
343 tls->error = EGL_SUCCESS;
344 return error;
345}
346
347static __attribute__((noinline))
348void setContext(EGLContext ctx) {
349 if (gEGLThreadLocalStorageKey == -1) {
350 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
351 if (gEGLThreadLocalStorageKey == -1)
352 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
353 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
354 }
355 tls_t* tls = getTLS();
356 tls->ctx = ctx;
357}
358
359static __attribute__((noinline))
360EGLContext getContext() {
361 if (gEGLThreadLocalStorageKey == -1)
362 return EGL_NO_CONTEXT;
363 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
364 if (!tls) return EGL_NO_CONTEXT;
365 return tls->ctx;
366}
367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368/*****************************************************************************/
369
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370template<typename T>
371static __attribute__((noinline))
372int binarySearch(
373 T const sortedArray[], int first, int last, T key)
374{
375 while (first <= last) {
376 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700377 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378 first = mid + 1;
379 } else if (key < sortedArray[mid]) {
380 last = mid - 1;
381 } else {
382 return mid;
383 }
384 }
385 return -1;
386}
387
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388static int cmp_configs(const void* a, const void *b)
389{
Mathias Agopiancee79392010-07-26 21:14:59 -0700390 const egl_config_t& c0 = *(egl_config_t const *)a;
391 const egl_config_t& c1 = *(egl_config_t const *)b;
392 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393}
394
395struct extention_map_t {
396 const char* name;
397 __eglMustCastToProperFunctionPointerType address;
398};
399
400static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700401 { "eglLockSurfaceKHR",
402 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
403 { "eglUnlockSurfaceKHR",
404 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
405 { "eglCreateImageKHR",
406 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
407 { "eglDestroyImageKHR",
408 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700409 { "eglSetSwapRectangleANDROID",
410 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
411 { "eglGetRenderBufferANDROID",
412 (__eglMustCastToProperFunctionPointerType)&eglGetRenderBufferANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413};
414
Mathias Agopian24035332010-08-02 17:34:32 -0700415extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
416
417// accesses protected by gInitDriverMutex
418static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
419static int gGLExtentionSlot = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420
421static void(*findProcAddress(const char* name,
422 const extention_map_t* map, size_t n))()
423{
424 for (uint32_t i=0 ; i<n ; i++) {
425 if (!strcmp(name, map[i].name)) {
426 return map[i].address;
427 }
428 }
429 return NULL;
430}
431
432// ----------------------------------------------------------------------------
433
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434static void gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700435 tls_t* tls = getTLS();
436 if (tls->logCallWithNoContext == EGL_TRUE) {
437 tls->logCallWithNoContext = EGL_FALSE;
438 LOGE("call to OpenGL ES API with no current context "
439 "(logged once per thread)");
440 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441}
Mathias Agopiand274eae2009-07-31 16:21:17 -0700442
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443static void early_egl_init(void)
444{
445#if !USE_FAST_TLS_KEY
446 pthread_key_create(&gGLWrapperKey, NULL);
447#endif
448 uint32_t addr = (uint32_t)((void*)gl_no_context);
449 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700450 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700452 sizeof(gHooksNoContext));
453 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454}
455
456static pthread_once_t once_control = PTHREAD_ONCE_INIT;
457static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
458
459
460static inline
461egl_display_t* get_display(EGLDisplay dpy)
462{
463 uintptr_t index = uintptr_t(dpy)-1U;
464 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
465}
466
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700467template<typename NATIVE, typename EGL>
468static inline NATIVE* egl_to_native_cast(EGL arg) {
469 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470}
471
472static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700473egl_surface_t* get_surface(EGLSurface surface) {
474 return egl_to_native_cast<egl_surface_t>(surface);
475}
476
477static inline
478egl_context_t* get_context(EGLContext context) {
479 return egl_to_native_cast<egl_context_t>(context);
480}
481
482static inline
483egl_image_t* get_image(EGLImageKHR image) {
484 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485}
486
487static egl_connection_t* validate_display_config(
488 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700489 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490{
491 dp = get_display(dpy);
492 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
493
Mathias Agopiancee79392010-07-26 21:14:59 -0700494 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
496 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700497 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498 if (cnx->dso == 0) {
499 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
500 }
501 return cnx;
502}
503
504static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
505{
506 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
507 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700508 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700510 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
512 return EGL_TRUE;
513}
514
515static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
516{
517 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
518 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700519 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700521 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800522 return setError(EGL_BAD_SURFACE, EGL_FALSE);
523 return EGL_TRUE;
524}
525
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700526EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
527{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700528 ImageRef _i(image);
529 if (!_i.get()) return EGL_NO_IMAGE_KHR;
530
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700531 EGLContext context = getContext();
532 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
533 return EGL_NO_IMAGE_KHR;
534
535 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700536 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700540 return i->images[c->impl];
541}
542
Mathias Agopian923c6612009-08-17 18:07:06 -0700543// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700544
Mathias Agopian923c6612009-08-17 18:07:06 -0700545// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700546// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700547// egl_init_drivers_locked()
548//
549static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
550
551EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800552{
553 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700554 // initialized by static ctor. should be set here.
555 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556 }
557
Mathias Agopiande586972009-05-28 17:39:03 -0700558 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700559 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700560
Mathias Agopian923c6612009-08-17 18:07:06 -0700561 // dynamically load all our EGL implementations for all displays
562 // and retrieve the corresponding EGLDisplay
563 // if that fails, don't use this driver.
564 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
565 egl_connection_t* cnx;
566 egl_display_t* d = &gDisplay[0];
567
568 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800569 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700570 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
571 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
572 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700573 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700574 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700575 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700576 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700577 if (dpy == EGL_NO_DISPLAY) {
578 loader.close(cnx->dso);
579 cnx->dso = NULL;
580 }
581 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582 }
583
584 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700585 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 char value[PROPERTY_VALUE_MAX];
587 property_get("debug.egl.hw", value, "1");
588 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700589 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
590 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
591 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700592 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700593 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700594 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700595 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700596 if (dpy == EGL_NO_DISPLAY) {
597 loader.close(cnx->dso);
598 cnx->dso = NULL;
599 }
600 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 } else {
602 LOGD("3D hardware acceleration is disabled");
603 }
604 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700605
Mathias Agopian923c6612009-08-17 18:07:06 -0700606 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
607 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 }
609
Mathias Agopian923c6612009-08-17 18:07:06 -0700610 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611}
612
Mathias Agopian923c6612009-08-17 18:07:06 -0700613EGLBoolean egl_init_drivers()
614{
615 EGLBoolean res;
616 pthread_mutex_lock(&gInitDriverMutex);
617 res = egl_init_drivers_locked();
618 pthread_mutex_unlock(&gInitDriverMutex);
619 return res;
620}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700621
622// ----------------------------------------------------------------------------
623}; // namespace android
624// ----------------------------------------------------------------------------
625
626using namespace android;
627
628EGLDisplay eglGetDisplay(NativeDisplayType display)
629{
Mathias Agopian923c6612009-08-17 18:07:06 -0700630 uint32_t index = uint32_t(display);
631 if (index >= NUM_DISPLAYS) {
632 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
633 }
634
635 if (egl_init_drivers() == EGL_FALSE) {
636 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
637 }
638
639 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
640 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641}
642
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643// ----------------------------------------------------------------------------
644// Initialization
645// ----------------------------------------------------------------------------
646
647EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
648{
649 egl_display_t * const dp = get_display(dpy);
650 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
651
Mathias Agopian75bc2782010-02-05 16:17:01 -0800652 Mutex::Autolock _l(dp->lock);
653
654 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655 if (major != NULL) *major = VERSION_MAJOR;
656 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700657 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 return EGL_TRUE;
659 }
660
Mathias Agopian618fa102009-10-14 02:06:37 -0700661 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662
663 // initialize each EGL and
664 // build our own extension string first, based on the extension we know
665 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700666 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 egl_connection_t* const cnx = &gEGLImpl[i];
668 cnx->major = -1;
669 cnx->minor = -1;
670 if (!cnx->dso)
671 continue;
672
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700673#if defined(ADRENO130)
674#warning "Adreno-130 eglInitialize() workaround"
675 /*
676 * The ADRENO 130 driver returns a different EGLDisplay each time
677 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
678 * after eglTerminate() has been called, so that eglInitialize()
679 * cannot be called again. Therefore, we need to make sure to call
680 * eglGetDisplay() before calling eglInitialize();
681 */
682 if (i == IMPL_HARDWARE) {
683 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700684 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700685 }
686#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700688
689 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700690 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800691 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700692 // i, idpy, cnx->major, cnx->minor, cnx);
693
694 // display is now initialized
695 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696
697 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700698 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700699 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700700 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700701 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700702 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700703 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700704 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700705 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706
707 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700708 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700709 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 }
711 }
712
713 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700714 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715 egl_connection_t* const cnx = &gEGLImpl[i];
716 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
717 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700718 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700719 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
720 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700721 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700722 dp->disp[i].dpy, dp->disp[i].config, n,
723 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 dp->numTotalConfigs += n;
726 res = EGL_TRUE;
727 }
728 }
729 }
730 }
731 }
732
733 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700734 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
735 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
736 egl_connection_t* const cnx = &gEGLImpl[i];
737 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
738 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
739 dp->configs[k].impl = i;
740 dp->configs[k].config = dp->disp[i].config[j];
741 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
742 // store the implementation's CONFIG_ID
743 cnx->egl.eglGetConfigAttrib(
744 dp->disp[i].dpy,
745 dp->disp[i].config[j],
746 EGL_CONFIG_ID,
747 &dp->configs[k].implConfigId);
748 k++;
749 }
750 }
751 }
752
753 // sort our configurations so we can do binary-searches
754 qsort( dp->configs,
755 dp->numTotalConfigs,
756 sizeof(egl_config_t), cmp_configs);
757
Mathias Agopian75bc2782010-02-05 16:17:01 -0800758 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759 if (major != NULL) *major = VERSION_MAJOR;
760 if (minor != NULL) *minor = VERSION_MINOR;
761 return EGL_TRUE;
762 }
763 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
764}
765
766EGLBoolean eglTerminate(EGLDisplay dpy)
767{
Mathias Agopian923c6612009-08-17 18:07:06 -0700768 // NOTE: don't unload the drivers b/c some APIs can be called
769 // after eglTerminate() has been called. eglTerminate() only
770 // terminates an EGLDisplay, not a EGL itself.
771
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 egl_display_t* const dp = get_display(dpy);
773 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800774
775 Mutex::Autolock _l(dp->lock);
776
777 if (dp->refs == 0) {
778 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
779 }
780
781 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700782 if (dp->refs > 1) {
783 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700785 }
Mathias Agopiande586972009-05-28 17:39:03 -0700786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700788 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700790 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700791 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700792 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700793 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700794 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700795 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700796 free(dp->disp[i].config);
797
798 dp->disp[i].numConfigs = 0;
799 dp->disp[i].config = 0;
800 dp->disp[i].state = egl_display_t::TERMINATED;
801
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 res = EGL_TRUE;
803 }
804 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700805
806 // TODO: all egl_object_t should be marked for termination
807
Mathias Agopian75bc2782010-02-05 16:17:01 -0800808 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700810 delete [] dp->configs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 clearTLS();
812 return res;
813}
814
815// ----------------------------------------------------------------------------
816// configuration
817// ----------------------------------------------------------------------------
818
819EGLBoolean eglGetConfigs( EGLDisplay dpy,
820 EGLConfig *configs,
821 EGLint config_size, EGLint *num_config)
822{
823 egl_display_t const * const dp = get_display(dpy);
824 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
825
826 GLint numConfigs = dp->numTotalConfigs;
827 if (!configs) {
828 *num_config = numConfigs;
829 return EGL_TRUE;
830 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700831
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700833 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
834 *configs++ = EGLConfig(i);
835 config_size--;
836 n++;
837 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838
839 *num_config = n;
840 return EGL_TRUE;
841}
842
843EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
844 EGLConfig *configs, EGLint config_size,
845 EGLint *num_config)
846{
847 egl_display_t const * const dp = get_display(dpy);
848 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
849
Jack Palevich749c63d2009-03-25 15:12:17 -0700850 if (num_config==0) {
851 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 }
853
854 EGLint n;
855 EGLBoolean res = EGL_FALSE;
856 *num_config = 0;
857
858
859 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -0700860 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 // to figure out both its size and if it contains an EGL_CONFIG_ID
862 // key. If so, the full array is copied and patched.
863 // NOTE: we assume that there can be only one occurrence
864 // of EGL_CONFIG_ID.
865
866 EGLint patch_index = -1;
867 GLint attr;
868 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -0700869 if (attrib_list) {
870 while ((attr=attrib_list[size]) != EGL_NONE) {
871 if (attr == EGL_CONFIG_ID)
872 patch_index = size;
873 size += 2;
874 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800875 }
876 if (patch_index >= 0) {
877 size += 2; // we need copy the sentinel as well
878 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
879 if (new_list == 0)
880 return setError(EGL_BAD_ALLOC, EGL_FALSE);
881 memcpy(new_list, attrib_list, size*sizeof(EGLint));
882
883 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -0700884 bool found = false;
885 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -0700887 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
888 if (dp->configs[i].configId == configId) {
889 ourConfig = EGLConfig(i);
890 configId = dp->configs[i].implConfigId;
891 found = true;
892 break;
893 }
894 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895
Mathias Agopiancee79392010-07-26 21:14:59 -0700896 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
897 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 // and switch to the new list
899 attrib_list = const_cast<const EGLint *>(new_list);
900
901 // At this point, the only configuration that can match is
902 // dp->configs[i][index], however, we don't know if it would be
903 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -0700904 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 // through all the EGLimpl[].
906 // We also know we can only get a single config back, and we know
907 // which one.
908
Mathias Agopian618fa102009-10-14 02:06:37 -0700909 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -0700910 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
911 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912 if (res && n>0) {
913 // n has to be 0 or 1, by construction, and we already know
914 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -0700915 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700916 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -0700917 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 *num_config = 1;
919 }
920 }
921
922 free(const_cast<EGLint *>(attrib_list));
923 return res;
924 }
925
Mathias Agopiancee79392010-07-26 21:14:59 -0700926
Mathias Agopian618fa102009-10-14 02:06:37 -0700927 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 egl_connection_t* const cnx = &gEGLImpl[i];
929 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700930 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700931 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -0700932 if (configs) {
933 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -0700934 // internal EGLConfig format.
935 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -0700936 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700937 egl_config_t key(i, configs[j]);
938 intptr_t index = binarySearch<egl_config_t>(
939 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -0700940 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700941 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -0700942 } else {
943 return setError(EGL_BAD_CONFIG, EGL_FALSE);
944 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945 }
Jack Palevich749c63d2009-03-25 15:12:17 -0700946 configs += n;
947 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 *num_config += n;
950 res = EGL_TRUE;
951 }
952 }
953 }
954 return res;
955}
956
957EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
958 EGLint attribute, EGLint *value)
959{
960 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700961 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 if (!cnx) return EGL_FALSE;
963
964 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700965 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 return EGL_TRUE;
967 }
Mathias Agopian618fa102009-10-14 02:06:37 -0700968 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -0700969 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
970 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971}
972
973// ----------------------------------------------------------------------------
974// surfaces
975// ----------------------------------------------------------------------------
976
977EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
978 NativeWindowType window,
979 const EGLint *attrib_list)
980{
981 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700982 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700984 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -0700985 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
986 dp->configs[intptr_t(config)].config, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700988 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
989 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 return s;
991 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992 }
993 return EGL_NO_SURFACE;
994}
995
996EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
997 NativePixmapType pixmap,
998 const EGLint *attrib_list)
999{
1000 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001001 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001003 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001004 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1005 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001007 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1008 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 return s;
1010 }
1011 }
1012 return EGL_NO_SURFACE;
1013}
1014
1015EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1016 const EGLint *attrib_list)
1017{
1018 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001019 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001021 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001022 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1023 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001025 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1026 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 return s;
1028 }
1029 }
1030 return EGL_NO_SURFACE;
1031}
1032
1033EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1034{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001035 SurfaceRef _s(surface);
1036 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 if (!validate_display_surface(dpy, surface))
1039 return EGL_FALSE;
1040 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001042 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001043 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001044 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001045 if (result == EGL_TRUE) {
1046 _s.terminate();
1047 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048 return result;
1049}
1050
1051EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1052 EGLint attribute, EGLint *value)
1053{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001054 SurfaceRef _s(surface);
1055 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1056
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001057 if (!validate_display_surface(dpy, surface))
1058 return EGL_FALSE;
1059 egl_display_t const * const dp = get_display(dpy);
1060 egl_surface_t const * const s = get_surface(surface);
1061
Mathias Agopiancee79392010-07-26 21:14:59 -07001062 EGLBoolean result(EGL_TRUE);
1063 if (attribute == EGL_CONFIG_ID) {
1064 // We need to remap EGL_CONFIG_IDs
1065 *value = dp->configs[intptr_t(s->config)].configId;
1066 } else {
1067 result = s->cnx->egl.eglQuerySurface(
1068 dp->disp[s->impl].dpy, s->surface, attribute, value);
1069 }
1070
1071 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072}
1073
1074// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001075// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076// ----------------------------------------------------------------------------
1077
1078EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1079 EGLContext share_list, const EGLint *attrib_list)
1080{
1081 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001082 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001083 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001084 if (share_list != EGL_NO_CONTEXT) {
1085 egl_context_t* const c = get_context(share_list);
1086 share_list = c->context;
1087 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001088 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001089 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1090 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001091 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001093 // figure out if it's a GLESv1 or GLESv2
1094 int version = 0;
1095 if (attrib_list) {
1096 while (*attrib_list != EGL_NONE) {
1097 GLint attr = *attrib_list++;
1098 GLint value = *attrib_list++;
1099 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1100 if (value == 1) {
1101 version = GLESv1_INDEX;
1102 } else if (value == 2) {
1103 version = GLESv2_INDEX;
1104 }
1105 }
1106 };
1107 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001108 egl_context_t* c = new egl_context_t(dpy, context, config,
1109 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110 return c;
1111 }
1112 }
1113 return EGL_NO_CONTEXT;
1114}
1115
1116EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1117{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001118 ContextRef _c(ctx);
1119 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001121 if (!validate_display_context(dpy, ctx))
1122 return EGL_FALSE;
1123 egl_display_t const * const dp = get_display(dpy);
1124 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001125 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001126 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001127 if (result == EGL_TRUE) {
1128 _c.terminate();
1129 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 return result;
1131}
1132
1133EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1134 EGLSurface read, EGLContext ctx)
1135{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001136 // get a reference to the object passed in
1137 ContextRef _c(ctx);
1138 SurfaceRef _d(draw);
1139 SurfaceRef _r(read);
1140
1141 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142 egl_display_t const * const dp = get_display(dpy);
1143 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001144 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1145 // EGL_NO_CONTEXT is valid
1146 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 }
1148
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001149 // these are the underlying implementation's object
1150 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001151 EGLSurface impl_draw = EGL_NO_SURFACE;
1152 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001153
1154 // these are our objects structs passed in
1155 egl_context_t * c = NULL;
1156 egl_surface_t const * d = NULL;
1157 egl_surface_t const * r = NULL;
1158
1159 // these are the current objects structs
1160 egl_context_t * cur_c = get_context(getContext());
1161 egl_surface_t * cur_r = NULL;
1162 egl_surface_t * cur_d = NULL;
1163
1164 if (ctx != EGL_NO_CONTEXT) {
1165 c = get_context(ctx);
1166 cur_r = get_surface(c->read);
1167 cur_d = get_surface(c->draw);
1168 impl_ctx = c->context;
1169 } else {
1170 // no context given, use the implementation of the current context
1171 if (cur_c == NULL) {
1172 // no current context
1173 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001174 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1175 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001176 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001177 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001178 return EGL_TRUE;
1179 }
1180 }
1181
1182 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001184 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001185 // make sure the EGLContext and EGLSurface passed in are for
1186 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001187 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001189 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001191
1192 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001194 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001195 // make sure the EGLContext and EGLSurface passed in are for
1196 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001197 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001199 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001201
1202 EGLBoolean result;
1203
1204 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001205 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001206 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001207 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001208 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001209 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211
1212 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001213 // by construction, these are either 0 or valid (possibly terminated)
1214 // it should be impossible for these to be invalid
1215 ContextRef _cur_c(cur_c);
1216 SurfaceRef _cur_r(cur_r);
1217 SurfaceRef _cur_d(cur_d);
1218
1219 // cur_c has to be valid here (but could be terminated)
1220 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001221 setGlThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001222 setContext(ctx);
1223 _c.acquire();
1224 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001225 setGlThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001226 setContext(EGL_NO_CONTEXT);
1227 }
1228 _cur_c.release();
1229
1230 _r.acquire();
1231 _cur_r.release();
1232 if (c) c->read = read;
1233
1234 _d.acquire();
1235 _cur_d.release();
1236 if (c) c->draw = draw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 }
1238 return result;
1239}
1240
1241
1242EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1243 EGLint attribute, EGLint *value)
1244{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001245 ContextRef _c(ctx);
1246 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1247
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248 if (!validate_display_context(dpy, ctx))
1249 return EGL_FALSE;
1250
1251 egl_display_t const * const dp = get_display(dpy);
1252 egl_context_t * const c = get_context(ctx);
1253
Mathias Agopiancee79392010-07-26 21:14:59 -07001254 EGLBoolean result(EGL_TRUE);
1255 if (attribute == EGL_CONFIG_ID) {
1256 *value = dp->configs[intptr_t(c->config)].configId;
1257 } else {
1258 // We need to remap EGL_CONFIG_IDs
1259 result = c->cnx->egl.eglQueryContext(
1260 dp->disp[c->impl].dpy, c->context, attribute, value);
1261 }
1262
1263 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264}
1265
1266EGLContext eglGetCurrentContext(void)
1267{
Mathias Agopian923c6612009-08-17 18:07:06 -07001268 // could be called before eglInitialize(), but we wouldn't have a context
1269 // then, and this function would correctly return EGL_NO_CONTEXT.
1270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 EGLContext ctx = getContext();
1272 return ctx;
1273}
1274
1275EGLSurface eglGetCurrentSurface(EGLint readdraw)
1276{
Mathias Agopian923c6612009-08-17 18:07:06 -07001277 // could be called before eglInitialize(), but we wouldn't have a context
1278 // then, and this function would correctly return EGL_NO_SURFACE.
1279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 EGLContext ctx = getContext();
1281 if (ctx) {
1282 egl_context_t const * const c = get_context(ctx);
1283 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1284 switch (readdraw) {
1285 case EGL_READ: return c->read;
1286 case EGL_DRAW: return c->draw;
1287 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1288 }
1289 }
1290 return EGL_NO_SURFACE;
1291}
1292
1293EGLDisplay eglGetCurrentDisplay(void)
1294{
Mathias Agopian923c6612009-08-17 18:07:06 -07001295 // could be called before eglInitialize(), but we wouldn't have a context
1296 // then, and this function would correctly return EGL_NO_DISPLAY.
1297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 EGLContext ctx = getContext();
1299 if (ctx) {
1300 egl_context_t const * const c = get_context(ctx);
1301 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1302 return c->dpy;
1303 }
1304 return EGL_NO_DISPLAY;
1305}
1306
1307EGLBoolean eglWaitGL(void)
1308{
Mathias Agopian923c6612009-08-17 18:07:06 -07001309 // could be called before eglInitialize(), but we wouldn't have a context
1310 // then, and this function would return GL_TRUE, which isn't wrong.
1311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 EGLBoolean res = EGL_TRUE;
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_FALSE);
1317 if (uint32_t(c->impl)>=2)
1318 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1319 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1320 if (!cnx->dso)
1321 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001322 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323 }
1324 return res;
1325}
1326
1327EGLBoolean eglWaitNative(EGLint engine)
1328{
Mathias Agopian923c6612009-08-17 18:07:06 -07001329 // could be called before eglInitialize(), but we wouldn't have a context
1330 // then, and this function would return GL_TRUE, which isn't wrong.
1331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 EGLBoolean res = EGL_TRUE;
1333 EGLContext ctx = getContext();
1334 if (ctx) {
1335 egl_context_t const * const c = get_context(ctx);
1336 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1337 if (uint32_t(c->impl)>=2)
1338 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1339 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1340 if (!cnx->dso)
1341 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001342 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343 }
1344 return res;
1345}
1346
1347EGLint eglGetError(void)
1348{
1349 EGLint result = EGL_SUCCESS;
Mathias Agopian618fa102009-10-14 02:06:37 -07001350 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351 EGLint err = EGL_SUCCESS;
1352 egl_connection_t* const cnx = &gEGLImpl[i];
1353 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001354 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1356 result = err;
1357 }
1358 if (result == EGL_SUCCESS)
1359 result = getError();
1360 return result;
1361}
1362
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001363__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001365 // eglGetProcAddress() could be the very first function called
1366 // in which case we must make sure we've initialized ourselves, this
1367 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001368
1369 if (egl_init_drivers() == EGL_FALSE) {
1370 setError(EGL_BAD_PARAMETER, NULL);
1371 return NULL;
1372 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001373
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 __eglMustCastToProperFunctionPointerType addr;
1375 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1376 if (addr) return addr;
1377
Mathias Agopian24035332010-08-02 17:34:32 -07001378 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1379 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380
Mathias Agopian24035332010-08-02 17:34:32 -07001381 /*
1382 * Since eglGetProcAddress() is not associated to anything, it needs
1383 * to return a function pointer that "works" regardless of what
1384 * the current context is.
1385 *
1386 * For this reason, we return a "forwarder", a small stub that takes
1387 * care of calling the function associated with the context
1388 * currently bound.
1389 *
1390 * We first look for extensions we've already resolved, if we're seeing
1391 * this extension for the first time, we go through all our
1392 * implementations and call eglGetProcAddress() and record the
1393 * result in the appropriate implementation hooks and return the
1394 * address of the forwarder corresponding to that hook set.
1395 *
1396 */
1397
1398 const String8 name(procname);
1399 addr = gGLExtentionMap.valueFor(name);
1400 const int slot = gGLExtentionSlot;
1401
1402 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1403 "no more slots for eglGetProcAddress(\"%s\")",
1404 procname);
1405
1406 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1407 bool found = false;
1408 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1409 egl_connection_t* const cnx = &gEGLImpl[i];
1410 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1411 found = true;
1412 cnx->hooks[i]->ext.extensions[slot] =
1413 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 }
1415 }
Mathias Agopian24035332010-08-02 17:34:32 -07001416 if (found) {
1417 addr = gExtensionForwarders[slot];
1418 gGLExtentionMap.add(name, addr);
1419 gGLExtentionSlot++;
1420 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422
Mathias Agopian24035332010-08-02 17:34:32 -07001423 pthread_mutex_unlock(&gInitDriverMutex);
1424
1425 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426}
1427
1428EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1429{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001430 SurfaceRef _s(draw);
1431 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1432
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 if (!validate_display_surface(dpy, draw))
1434 return EGL_FALSE;
1435 egl_display_t const * const dp = get_display(dpy);
1436 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001437 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438}
1439
1440EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1441 NativePixmapType target)
1442{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001443 SurfaceRef _s(surface);
1444 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1445
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446 if (!validate_display_surface(dpy, surface))
1447 return EGL_FALSE;
1448 egl_display_t const * const dp = get_display(dpy);
1449 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001450 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001451 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452}
1453
1454const char* eglQueryString(EGLDisplay dpy, EGLint name)
1455{
1456 egl_display_t const * const dp = get_display(dpy);
1457 switch (name) {
1458 case EGL_VENDOR:
1459 return gVendorString;
1460 case EGL_VERSION:
1461 return gVersionString;
1462 case EGL_EXTENSIONS:
1463 return gExtensionString;
1464 case EGL_CLIENT_APIS:
1465 return gClientApiString;
1466 }
1467 return setError(EGL_BAD_PARAMETER, (const char *)0);
1468}
1469
1470
1471// ----------------------------------------------------------------------------
1472// EGL 1.1
1473// ----------------------------------------------------------------------------
1474
1475EGLBoolean eglSurfaceAttrib(
1476 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1477{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001478 SurfaceRef _s(surface);
1479 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 if (!validate_display_surface(dpy, surface))
1482 return EGL_FALSE;
1483 egl_display_t const * const dp = get_display(dpy);
1484 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001485 if (s->cnx->egl.eglSurfaceAttrib) {
1486 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001487 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 }
1489 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1490}
1491
1492EGLBoolean eglBindTexImage(
1493 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1494{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001495 SurfaceRef _s(surface);
1496 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1497
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 if (!validate_display_surface(dpy, surface))
1499 return EGL_FALSE;
1500 egl_display_t const * const dp = get_display(dpy);
1501 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001502 if (s->cnx->egl.eglBindTexImage) {
1503 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001504 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505 }
1506 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1507}
1508
1509EGLBoolean eglReleaseTexImage(
1510 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1511{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001512 SurfaceRef _s(surface);
1513 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1514
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001515 if (!validate_display_surface(dpy, surface))
1516 return EGL_FALSE;
1517 egl_display_t const * const dp = get_display(dpy);
1518 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001519 if (s->cnx->egl.eglReleaseTexImage) {
1520 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001521 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 }
1523 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1524}
1525
1526EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1527{
1528 egl_display_t * const dp = get_display(dpy);
1529 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1530
1531 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001532 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533 egl_connection_t* const cnx = &gEGLImpl[i];
1534 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001535 if (cnx->egl.eglSwapInterval) {
1536 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001537 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 res = EGL_FALSE;
1539 }
1540 }
1541 }
1542 }
1543 return res;
1544}
1545
1546
1547// ----------------------------------------------------------------------------
1548// EGL 1.2
1549// ----------------------------------------------------------------------------
1550
1551EGLBoolean eglWaitClient(void)
1552{
Mathias Agopian923c6612009-08-17 18:07:06 -07001553 // could be called before eglInitialize(), but we wouldn't have a context
1554 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555 EGLBoolean res = EGL_TRUE;
1556 EGLContext ctx = getContext();
1557 if (ctx) {
1558 egl_context_t const * const c = get_context(ctx);
1559 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1560 if (uint32_t(c->impl)>=2)
1561 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1562 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1563 if (!cnx->dso)
1564 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001565 if (cnx->egl.eglWaitClient) {
1566 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001568 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 }
1570 }
1571 return res;
1572}
1573
1574EGLBoolean eglBindAPI(EGLenum api)
1575{
Mathias Agopian923c6612009-08-17 18:07:06 -07001576 if (egl_init_drivers() == EGL_FALSE) {
1577 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1578 }
1579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 // bind this API on all EGLs
1581 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001582 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583 egl_connection_t* const cnx = &gEGLImpl[i];
1584 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001585 if (cnx->egl.eglBindAPI) {
1586 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 res = EGL_FALSE;
1588 }
1589 }
1590 }
1591 }
1592 return res;
1593}
1594
1595EGLenum eglQueryAPI(void)
1596{
Mathias Agopian923c6612009-08-17 18:07:06 -07001597 if (egl_init_drivers() == EGL_FALSE) {
1598 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1599 }
1600
Mathias Agopian618fa102009-10-14 02:06:37 -07001601 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602 egl_connection_t* const cnx = &gEGLImpl[i];
1603 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001604 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 // the first one we find is okay, because they all
1606 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001607 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608 }
1609 }
1610 }
1611 // or, it can only be OpenGL ES
1612 return EGL_OPENGL_ES_API;
1613}
1614
1615EGLBoolean eglReleaseThread(void)
1616{
Mathias Agopian618fa102009-10-14 02:06:37 -07001617 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 egl_connection_t* const cnx = &gEGLImpl[i];
1619 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001620 if (cnx->egl.eglReleaseThread) {
1621 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622 }
1623 }
1624 }
1625 clearTLS();
1626 return EGL_TRUE;
1627}
1628
1629EGLSurface eglCreatePbufferFromClientBuffer(
1630 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1631 EGLConfig config, const EGLint *attrib_list)
1632{
1633 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001634 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001636 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1637 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001638 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1639 buftype, buffer,
1640 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 }
1642 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1643}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001644
1645// ----------------------------------------------------------------------------
1646// EGL_EGLEXT_VERSION 3
1647// ----------------------------------------------------------------------------
1648
1649EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1650 const EGLint *attrib_list)
1651{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001652 SurfaceRef _s(surface);
1653 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1654
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001655 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001656 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001657
1658 egl_display_t const * const dp = get_display(dpy);
1659 egl_surface_t const * const s = get_surface(surface);
1660
Mathias Agopian618fa102009-10-14 02:06:37 -07001661 if (s->cnx->egl.eglLockSurfaceKHR) {
1662 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001663 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001664 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001665 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001666}
1667
1668EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1669{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001670 SurfaceRef _s(surface);
1671 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1672
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001673 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001674 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001675
1676 egl_display_t const * const dp = get_display(dpy);
1677 egl_surface_t const * const s = get_surface(surface);
1678
Mathias Agopian618fa102009-10-14 02:06:37 -07001679 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1680 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001681 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001682 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001683 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001684}
1685
1686EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1687 EGLClientBuffer buffer, const EGLint *attrib_list)
1688{
1689 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001690 ContextRef _c(ctx);
1691 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001692 if (!validate_display_context(dpy, ctx))
1693 return EGL_NO_IMAGE_KHR;
1694 egl_display_t const * const dp = get_display(dpy);
1695 egl_context_t * const c = get_context(ctx);
1696 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001697 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001698 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001699 if (image == EGL_NO_IMAGE_KHR)
1700 return image;
1701
1702 egl_image_t* result = new egl_image_t(dpy, ctx);
1703 result->images[c->impl] = image;
1704 return (EGLImageKHR)result;
1705 } else {
1706 // EGL_NO_CONTEXT is a valid parameter
1707 egl_display_t const * const dp = get_display(dpy);
1708 if (dp == 0) {
1709 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1710 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001711
1712 /* Since we don't have a way to know which implementation to call,
1713 * we're calling all of them. If at least one of the implementation
1714 * succeeded, this is a success.
1715 */
1716
1717 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001718
Mathias Agopian618fa102009-10-14 02:06:37 -07001719 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001720 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001721 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001722 egl_connection_t* const cnx = &gEGLImpl[i];
1723 implImages[i] = EGL_NO_IMAGE_KHR;
1724 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001725 if (cnx->egl.eglCreateImageKHR) {
1726 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001727 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001728 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1729 success = true;
1730 }
1731 }
1732 }
1733 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001734
1735 if (!success) {
1736 // failure, if there was an error when we entered this function,
1737 // the error flag must not be updated.
1738 // Otherwise, the error is whatever happened in the implementation
1739 // that faulted.
1740 if (currentError != EGL_SUCCESS) {
1741 setError(currentError, EGL_NO_IMAGE_KHR);
1742 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001743 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001744 } else {
1745 // In case of success, we need to clear all error flags
1746 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07001747 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001748 // a "full" success (all implementation succeeded).
1749 eglGetError();
1750 }
1751
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001752 egl_image_t* result = new egl_image_t(dpy, ctx);
1753 memcpy(result->images, implImages, sizeof(implImages));
1754 return (EGLImageKHR)result;
1755 }
1756}
1757
1758EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1759{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001760 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001761 if (dp == 0) {
1762 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1763 }
1764
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001765 ImageRef _i(img);
1766 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001767
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001768 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001769 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001770 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001771 egl_connection_t* const cnx = &gEGLImpl[i];
1772 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1773 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001774 if (cnx->egl.eglCreateImageKHR) {
1775 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001776 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001777 success = true;
1778 }
1779 }
1780 }
1781 }
1782 }
1783 if (!success)
1784 return EGL_FALSE;
1785
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001786 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001787
Mathias Agopian24e5f522009-08-12 21:18:15 -07001788 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001789}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001790
1791
1792// ----------------------------------------------------------------------------
1793// ANDROID extensions
1794// ----------------------------------------------------------------------------
1795
1796EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1797 EGLint left, EGLint top, EGLint width, EGLint height)
1798{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001799 SurfaceRef _s(draw);
1800 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1801
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001802 if (!validate_display_surface(dpy, draw))
1803 return EGL_FALSE;
1804 egl_display_t const * const dp = get_display(dpy);
1805 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001806 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
1807 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001808 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001809 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001810 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001811}
1812
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001813EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
1814{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001815 SurfaceRef _s(draw);
1816 if (!_s.get()) return setError(EGL_BAD_SURFACE, (EGLClientBuffer*)0);
1817
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001818 if (!validate_display_surface(dpy, draw))
1819 return 0;
1820 egl_display_t const * const dp = get_display(dpy);
1821 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001822 if (s->cnx->egl.eglGetRenderBufferANDROID) {
1823 return s->cnx->egl.eglGetRenderBufferANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001824 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001825 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001826 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer*)0);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001827}