blob: 2d1a2786a41a02b31191461d83537fb3999b381c [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 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 Agopiancee79392010-07-26 21:14:59 -0700146
147struct egl_config_t {
148 egl_config_t() {}
149 egl_config_t(int impl, EGLConfig config)
150 : impl(impl), config(config), configId(0), implConfigId(0) { }
151 int impl; // the implementation this config is for
152 EGLConfig config; // the implementation's EGLConfig
153 EGLint configId; // our CONFIG_ID
154 EGLint implConfigId; // the implementation's CONFIG_ID
155 inline bool operator < (const egl_config_t& rhs) const {
156 if (impl < rhs.impl) return true;
157 if (impl > rhs.impl) return false;
158 return config < rhs.config;
159 }
160};
161
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700162struct egl_display_t {
163 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 struct strings_t {
166 char const * vendor;
167 char const * version;
168 char const * clientApi;
169 char const * extensions;
170 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700171
172 struct DisplayImpl {
173 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
174 state(NOT_INITIALIZED), numConfigs(0) { }
175 EGLDisplay dpy;
176 EGLConfig* config;
177 EGLint state;
178 EGLint numConfigs;
179 strings_t queryString;
180 };
181
Mathias Agopiancee79392010-07-26 21:14:59 -0700182 uint32_t magic;
183 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
184 EGLint numTotalConfigs;
185 egl_config_t* configs;
186 uint32_t refs;
187 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700188
Mathias Agopiancee79392010-07-26 21:14:59 -0700189 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700190 ~egl_display_t() { magic = 0; }
191 inline bool isValid() const { return magic == '_dpy'; }
192 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193};
194
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700195struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700197 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
198
Mathias Agopiancee79392010-07-26 21:14:59 -0700199 egl_surface_t(EGLDisplay dpy, EGLSurface surface, EGLConfig config,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 int impl, egl_connection_t const* cnx)
Mathias Agopiancee79392010-07-26 21:14:59 -0700201 : dpy(dpy), surface(surface), config(config), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 }
203 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 }
205 EGLDisplay dpy;
206 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700207 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 int impl;
209 egl_connection_t const* cnx;
210};
211
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700212struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700214 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
215
Mathias Agopiancee79392010-07-26 21:14:59 -0700216 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian618fa102009-10-14 02:06:37 -0700217 int impl, egl_connection_t const* cnx, int version)
218 : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx),
219 version(version)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 {
221 }
222 EGLDisplay dpy;
223 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700224 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225 EGLSurface read;
226 EGLSurface draw;
227 int impl;
228 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700229 int version;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230};
231
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700232struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700233{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700234 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
235
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700236 egl_image_t(EGLDisplay dpy, EGLContext context)
237 : dpy(dpy), context(context)
238 {
239 memset(images, 0, sizeof(images));
240 }
241 EGLDisplay dpy;
Mathias Agopian77fbf8d2010-09-09 11:12:54 -0700242 EGLContext context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700243 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700244};
245
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700246typedef egl_surface_t::Ref SurfaceRef;
247typedef egl_context_t::Ref ContextRef;
248typedef egl_image_t::Ref ImageRef;
249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250struct tls_t
251{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700252 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 EGLint error;
254 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700255 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256};
257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258
259// ----------------------------------------------------------------------------
260
Mathias Agopianbf41b112010-04-09 13:37:34 -0700261static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262static egl_display_t gDisplay[NUM_DISPLAYS];
263static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
264static pthread_key_t gEGLThreadLocalStorageKey = -1;
265
266// ----------------------------------------------------------------------------
267
Mathias Agopian618fa102009-10-14 02:06:37 -0700268EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
269EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700270EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271
272// ----------------------------------------------------------------------------
273
274static __attribute__((noinline))
275const char *egl_strerror(EGLint err)
276{
277 switch (err){
278 case EGL_SUCCESS: return "EGL_SUCCESS";
279 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
280 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
281 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
282 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
283 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
284 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
285 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
286 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
287 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
288 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
289 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
290 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
291 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
292 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
293 default: return "UNKNOWN";
294 }
295}
296
297static __attribute__((noinline))
298void clearTLS() {
299 if (gEGLThreadLocalStorageKey != -1) {
300 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
301 if (tls) {
302 delete tls;
303 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
304 }
305 }
306}
307
308static tls_t* getTLS()
309{
310 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
311 if (tls == 0) {
312 tls = new tls_t;
313 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
314 }
315 return tls;
316}
317
318template<typename T>
319static __attribute__((noinline))
320T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
321 if (gEGLThreadLocalStorageKey == -1) {
322 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
323 if (gEGLThreadLocalStorageKey == -1)
324 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
325 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
326 }
327 tls_t* tls = getTLS();
328 if (tls->error != error) {
329 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
330 tls->error = error;
331 }
332 return returnValue;
333}
334
335static __attribute__((noinline))
336GLint getError() {
337 if (gEGLThreadLocalStorageKey == -1)
338 return EGL_SUCCESS;
339 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
340 if (!tls) return EGL_SUCCESS;
341 GLint error = tls->error;
342 tls->error = EGL_SUCCESS;
343 return error;
344}
345
346static __attribute__((noinline))
347void setContext(EGLContext ctx) {
348 if (gEGLThreadLocalStorageKey == -1) {
349 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
350 if (gEGLThreadLocalStorageKey == -1)
351 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
352 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
353 }
354 tls_t* tls = getTLS();
355 tls->ctx = ctx;
356}
357
358static __attribute__((noinline))
359EGLContext getContext() {
360 if (gEGLThreadLocalStorageKey == -1)
361 return EGL_NO_CONTEXT;
362 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
363 if (!tls) return EGL_NO_CONTEXT;
364 return tls->ctx;
365}
366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367/*****************************************************************************/
368
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369template<typename T>
370static __attribute__((noinline))
371int binarySearch(
372 T const sortedArray[], int first, int last, T key)
373{
374 while (first <= last) {
375 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700376 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 first = mid + 1;
378 } else if (key < sortedArray[mid]) {
379 last = mid - 1;
380 } else {
381 return mid;
382 }
383 }
384 return -1;
385}
386
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387static int cmp_configs(const void* a, const void *b)
388{
Mathias Agopiancee79392010-07-26 21:14:59 -0700389 const egl_config_t& c0 = *(egl_config_t const *)a;
390 const egl_config_t& c1 = *(egl_config_t const *)b;
391 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392}
393
394struct extention_map_t {
395 const char* name;
396 __eglMustCastToProperFunctionPointerType address;
397};
398
399static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700400 { "eglLockSurfaceKHR",
401 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
402 { "eglUnlockSurfaceKHR",
403 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
404 { "eglCreateImageKHR",
405 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
406 { "eglDestroyImageKHR",
407 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700408 { "eglSetSwapRectangleANDROID",
409 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410};
411
Mathias Agopian24035332010-08-02 17:34:32 -0700412extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
413
414// accesses protected by gInitDriverMutex
415static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
416static int gGLExtentionSlot = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417
418static void(*findProcAddress(const char* name,
419 const extention_map_t* map, size_t n))()
420{
421 for (uint32_t i=0 ; i<n ; i++) {
422 if (!strcmp(name, map[i].name)) {
423 return map[i].address;
424 }
425 }
426 return NULL;
427}
428
429// ----------------------------------------------------------------------------
430
Mathias Agopian6f087122010-09-23 16:38:38 -0700431static int gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700432 tls_t* tls = getTLS();
433 if (tls->logCallWithNoContext == EGL_TRUE) {
434 tls->logCallWithNoContext = EGL_FALSE;
435 LOGE("call to OpenGL ES API with no current context "
436 "(logged once per thread)");
437 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700438 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700439}
440
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441static void early_egl_init(void)
442{
443#if !USE_FAST_TLS_KEY
444 pthread_key_create(&gGLWrapperKey, NULL);
445#endif
446 uint32_t addr = (uint32_t)((void*)gl_no_context);
447 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700448 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800449 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700450 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700451
Mathias Agopian618fa102009-10-14 02:06:37 -0700452 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453}
454
455static pthread_once_t once_control = PTHREAD_ONCE_INIT;
456static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
457
458
459static inline
460egl_display_t* get_display(EGLDisplay dpy)
461{
462 uintptr_t index = uintptr_t(dpy)-1U;
463 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
464}
465
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700466template<typename NATIVE, typename EGL>
467static inline NATIVE* egl_to_native_cast(EGL arg) {
468 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469}
470
471static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700472egl_surface_t* get_surface(EGLSurface surface) {
473 return egl_to_native_cast<egl_surface_t>(surface);
474}
475
476static inline
477egl_context_t* get_context(EGLContext context) {
478 return egl_to_native_cast<egl_context_t>(context);
479}
480
481static inline
482egl_image_t* get_image(EGLImageKHR image) {
483 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484}
485
486static egl_connection_t* validate_display_config(
487 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700488 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489{
490 dp = get_display(dpy);
491 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
492
Mathias Agopiancee79392010-07-26 21:14:59 -0700493 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
495 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700496 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 if (cnx->dso == 0) {
498 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
499 }
500 return cnx;
501}
502
503static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
504{
505 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
506 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700507 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700509 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
511 return EGL_TRUE;
512}
513
514static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
515{
516 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
517 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700518 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700520 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521 return setError(EGL_BAD_SURFACE, EGL_FALSE);
522 return EGL_TRUE;
523}
524
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700525EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
526{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700527 ImageRef _i(image);
528 if (!_i.get()) return EGL_NO_IMAGE_KHR;
529
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700530 EGLContext context = getContext();
531 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
532 return EGL_NO_IMAGE_KHR;
533
534 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700535 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700536 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700538 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700539 return i->images[c->impl];
540}
541
Mathias Agopian923c6612009-08-17 18:07:06 -0700542// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700543
Mathias Agopian923c6612009-08-17 18:07:06 -0700544// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700545// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700546// egl_init_drivers_locked()
547//
548static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
549
550EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551{
552 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700553 // initialized by static ctor. should be set here.
554 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800555 }
556
Mathias Agopiande586972009-05-28 17:39:03 -0700557 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700558 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700559
Mathias Agopian923c6612009-08-17 18:07:06 -0700560 // dynamically load all our EGL implementations for all displays
561 // and retrieve the corresponding EGLDisplay
562 // if that fails, don't use this driver.
563 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
564 egl_connection_t* cnx;
565 egl_display_t* d = &gDisplay[0];
566
567 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700569 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
570 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
571 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700572 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700573 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700574 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700575 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700576 if (dpy == EGL_NO_DISPLAY) {
577 loader.close(cnx->dso);
578 cnx->dso = NULL;
579 }
580 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800581 }
582
583 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700584 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800585 char value[PROPERTY_VALUE_MAX];
586 property_get("debug.egl.hw", value, "1");
587 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700588 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
589 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
590 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700591 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700592 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700593 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700594 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700595 if (dpy == EGL_NO_DISPLAY) {
596 loader.close(cnx->dso);
597 cnx->dso = NULL;
598 }
599 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600 } else {
601 LOGD("3D hardware acceleration is disabled");
602 }
603 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700604
Mathias Agopian923c6612009-08-17 18:07:06 -0700605 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
606 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 }
608
Mathias Agopian923c6612009-08-17 18:07:06 -0700609 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610}
611
Mathias Agopian923c6612009-08-17 18:07:06 -0700612EGLBoolean egl_init_drivers()
613{
614 EGLBoolean res;
615 pthread_mutex_lock(&gInitDriverMutex);
616 res = egl_init_drivers_locked();
617 pthread_mutex_unlock(&gInitDriverMutex);
618 return res;
619}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620
621// ----------------------------------------------------------------------------
622}; // namespace android
623// ----------------------------------------------------------------------------
624
625using namespace android;
626
627EGLDisplay eglGetDisplay(NativeDisplayType display)
628{
Mathias Agopian923c6612009-08-17 18:07:06 -0700629 uint32_t index = uint32_t(display);
630 if (index >= NUM_DISPLAYS) {
631 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
632 }
633
634 if (egl_init_drivers() == EGL_FALSE) {
635 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
636 }
637
638 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
639 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700640}
641
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642// ----------------------------------------------------------------------------
643// Initialization
644// ----------------------------------------------------------------------------
645
646EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
647{
648 egl_display_t * const dp = get_display(dpy);
649 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
650
Mathias Agopian75bc2782010-02-05 16:17:01 -0800651 Mutex::Autolock _l(dp->lock);
652
653 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654 if (major != NULL) *major = VERSION_MAJOR;
655 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700656 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657 return EGL_TRUE;
658 }
659
Mathias Agopian618fa102009-10-14 02:06:37 -0700660 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661
662 // initialize each EGL and
663 // build our own extension string first, based on the extension we know
664 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700665 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800666 egl_connection_t* const cnx = &gEGLImpl[i];
667 cnx->major = -1;
668 cnx->minor = -1;
669 if (!cnx->dso)
670 continue;
671
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700672#if defined(ADRENO130)
673#warning "Adreno-130 eglInitialize() workaround"
674 /*
675 * The ADRENO 130 driver returns a different EGLDisplay each time
676 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
677 * after eglTerminate() has been called, so that eglInitialize()
678 * cannot be called again. Therefore, we need to make sure to call
679 * eglGetDisplay() before calling eglInitialize();
680 */
681 if (i == IMPL_HARDWARE) {
682 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700683 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700684 }
685#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700687
688 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700689 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700691 // i, idpy, cnx->major, cnx->minor, cnx);
692
693 // display is now initialized
694 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695
696 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700697 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700698 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700699 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700700 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700701 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700702 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700703 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700704 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705
706 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700707 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700708 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 }
710 }
711
712 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700713 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714 egl_connection_t* const cnx = &gEGLImpl[i];
715 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
716 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700717 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700718 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
719 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700720 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700721 dp->disp[i].dpy, dp->disp[i].config, n,
722 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 dp->numTotalConfigs += n;
725 res = EGL_TRUE;
726 }
727 }
728 }
729 }
730 }
731
732 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700733 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
734 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
735 egl_connection_t* const cnx = &gEGLImpl[i];
736 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
737 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
738 dp->configs[k].impl = i;
739 dp->configs[k].config = dp->disp[i].config[j];
740 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
741 // store the implementation's CONFIG_ID
742 cnx->egl.eglGetConfigAttrib(
743 dp->disp[i].dpy,
744 dp->disp[i].config[j],
745 EGL_CONFIG_ID,
746 &dp->configs[k].implConfigId);
747 k++;
748 }
749 }
750 }
751
752 // sort our configurations so we can do binary-searches
753 qsort( dp->configs,
754 dp->numTotalConfigs,
755 sizeof(egl_config_t), cmp_configs);
756
Mathias Agopian75bc2782010-02-05 16:17:01 -0800757 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 if (major != NULL) *major = VERSION_MAJOR;
759 if (minor != NULL) *minor = VERSION_MINOR;
760 return EGL_TRUE;
761 }
762 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
763}
764
765EGLBoolean eglTerminate(EGLDisplay dpy)
766{
Mathias Agopian923c6612009-08-17 18:07:06 -0700767 // NOTE: don't unload the drivers b/c some APIs can be called
768 // after eglTerminate() has been called. eglTerminate() only
769 // terminates an EGLDisplay, not a EGL itself.
770
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771 egl_display_t* const dp = get_display(dpy);
772 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800773
774 Mutex::Autolock _l(dp->lock);
775
776 if (dp->refs == 0) {
777 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
778 }
779
780 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700781 if (dp->refs > 1) {
782 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700784 }
Mathias Agopiande586972009-05-28 17:39:03 -0700785
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700787 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700789 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700790 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700791 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700792 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700793 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700794 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700795 free(dp->disp[i].config);
796
797 dp->disp[i].numConfigs = 0;
798 dp->disp[i].config = 0;
799 dp->disp[i].state = egl_display_t::TERMINATED;
800
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801 res = EGL_TRUE;
802 }
803 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700804
805 // TODO: all egl_object_t should be marked for termination
806
Mathias Agopian75bc2782010-02-05 16:17:01 -0800807 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700809 delete [] dp->configs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810 clearTLS();
811 return res;
812}
813
814// ----------------------------------------------------------------------------
815// configuration
816// ----------------------------------------------------------------------------
817
818EGLBoolean eglGetConfigs( EGLDisplay dpy,
819 EGLConfig *configs,
820 EGLint config_size, EGLint *num_config)
821{
822 egl_display_t const * const dp = get_display(dpy);
823 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
824
825 GLint numConfigs = dp->numTotalConfigs;
826 if (!configs) {
827 *num_config = numConfigs;
828 return EGL_TRUE;
829 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700832 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
833 *configs++ = EGLConfig(i);
834 config_size--;
835 n++;
836 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837
838 *num_config = n;
839 return EGL_TRUE;
840}
841
842EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
843 EGLConfig *configs, EGLint config_size,
844 EGLint *num_config)
845{
846 egl_display_t const * const dp = get_display(dpy);
847 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
848
Jack Palevich749c63d2009-03-25 15:12:17 -0700849 if (num_config==0) {
850 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800851 }
852
853 EGLint n;
854 EGLBoolean res = EGL_FALSE;
855 *num_config = 0;
856
857
858 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -0700859 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 // to figure out both its size and if it contains an EGL_CONFIG_ID
861 // key. If so, the full array is copied and patched.
862 // NOTE: we assume that there can be only one occurrence
863 // of EGL_CONFIG_ID.
864
865 EGLint patch_index = -1;
866 GLint attr;
867 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -0700868 if (attrib_list) {
869 while ((attr=attrib_list[size]) != EGL_NONE) {
870 if (attr == EGL_CONFIG_ID)
871 patch_index = size;
872 size += 2;
873 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874 }
875 if (patch_index >= 0) {
876 size += 2; // we need copy the sentinel as well
877 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
878 if (new_list == 0)
879 return setError(EGL_BAD_ALLOC, EGL_FALSE);
880 memcpy(new_list, attrib_list, size*sizeof(EGLint));
881
882 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -0700883 bool found = false;
884 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -0700886 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
887 if (dp->configs[i].configId == configId) {
888 ourConfig = EGLConfig(i);
889 configId = dp->configs[i].implConfigId;
890 found = true;
891 break;
892 }
893 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894
Mathias Agopiancee79392010-07-26 21:14:59 -0700895 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
896 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897 // and switch to the new list
898 attrib_list = const_cast<const EGLint *>(new_list);
899
900 // At this point, the only configuration that can match is
901 // dp->configs[i][index], however, we don't know if it would be
902 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -0700903 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 // through all the EGLimpl[].
905 // We also know we can only get a single config back, and we know
906 // which one.
907
Mathias Agopian618fa102009-10-14 02:06:37 -0700908 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -0700909 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
910 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911 if (res && n>0) {
912 // n has to be 0 or 1, by construction, and we already know
913 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -0700914 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700915 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -0700916 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 *num_config = 1;
918 }
919 }
920
921 free(const_cast<EGLint *>(attrib_list));
922 return res;
923 }
924
Mathias Agopiancee79392010-07-26 21:14:59 -0700925
Mathias Agopian618fa102009-10-14 02:06:37 -0700926 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 egl_connection_t* const cnx = &gEGLImpl[i];
928 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700929 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700930 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -0700931 if (configs) {
932 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -0700933 // internal EGLConfig format.
934 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -0700935 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700936 egl_config_t key(i, configs[j]);
937 intptr_t index = binarySearch<egl_config_t>(
938 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -0700939 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700940 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -0700941 } else {
942 return setError(EGL_BAD_CONFIG, EGL_FALSE);
943 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944 }
Jack Palevich749c63d2009-03-25 15:12:17 -0700945 configs += n;
946 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800947 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 *num_config += n;
949 res = EGL_TRUE;
950 }
951 }
952 }
953 return res;
954}
955
956EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
957 EGLint attribute, EGLint *value)
958{
959 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700960 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800961 if (!cnx) return EGL_FALSE;
962
963 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700964 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965 return EGL_TRUE;
966 }
Mathias Agopian618fa102009-10-14 02:06:37 -0700967 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -0700968 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
969 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970}
971
972// ----------------------------------------------------------------------------
973// surfaces
974// ----------------------------------------------------------------------------
975
976EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
977 NativeWindowType window,
978 const EGLint *attrib_list)
979{
980 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700981 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700983 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -0700984 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
985 dp->configs[intptr_t(config)].config, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700987 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
988 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989 return s;
990 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 }
992 return EGL_NO_SURFACE;
993}
994
995EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
996 NativePixmapType pixmap,
997 const EGLint *attrib_list)
998{
999 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001000 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001002 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001003 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1004 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001006 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1007 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 return s;
1009 }
1010 }
1011 return EGL_NO_SURFACE;
1012}
1013
1014EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1015 const EGLint *attrib_list)
1016{
1017 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001018 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001020 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001021 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1022 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001024 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1025 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 return s;
1027 }
1028 }
1029 return EGL_NO_SURFACE;
1030}
1031
1032EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1033{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001034 SurfaceRef _s(surface);
1035 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 if (!validate_display_surface(dpy, surface))
1038 return EGL_FALSE;
1039 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001041 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001042 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001043 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001044 if (result == EGL_TRUE) {
1045 _s.terminate();
1046 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 return result;
1048}
1049
1050EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1051 EGLint attribute, EGLint *value)
1052{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001053 SurfaceRef _s(surface);
1054 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1055
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 if (!validate_display_surface(dpy, surface))
1057 return EGL_FALSE;
1058 egl_display_t const * const dp = get_display(dpy);
1059 egl_surface_t const * const s = get_surface(surface);
1060
Mathias Agopiancee79392010-07-26 21:14:59 -07001061 EGLBoolean result(EGL_TRUE);
1062 if (attribute == EGL_CONFIG_ID) {
1063 // We need to remap EGL_CONFIG_IDs
1064 *value = dp->configs[intptr_t(s->config)].configId;
1065 } else {
1066 result = s->cnx->egl.eglQuerySurface(
1067 dp->disp[s->impl].dpy, s->surface, attribute, value);
1068 }
1069
1070 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071}
1072
1073// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001074// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075// ----------------------------------------------------------------------------
1076
1077EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1078 EGLContext share_list, const EGLint *attrib_list)
1079{
1080 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001081 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001083 if (share_list != EGL_NO_CONTEXT) {
1084 egl_context_t* const c = get_context(share_list);
1085 share_list = c->context;
1086 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001087 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001088 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1089 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001090 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001092 // figure out if it's a GLESv1 or GLESv2
1093 int version = 0;
1094 if (attrib_list) {
1095 while (*attrib_list != EGL_NONE) {
1096 GLint attr = *attrib_list++;
1097 GLint value = *attrib_list++;
1098 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1099 if (value == 1) {
1100 version = GLESv1_INDEX;
1101 } else if (value == 2) {
1102 version = GLESv2_INDEX;
1103 }
1104 }
1105 };
1106 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001107 egl_context_t* c = new egl_context_t(dpy, context, config,
1108 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 return c;
1110 }
1111 }
1112 return EGL_NO_CONTEXT;
1113}
1114
1115EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1116{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001117 ContextRef _c(ctx);
1118 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 if (!validate_display_context(dpy, ctx))
1121 return EGL_FALSE;
1122 egl_display_t const * const dp = get_display(dpy);
1123 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001124 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001125 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001126 if (result == EGL_TRUE) {
1127 _c.terminate();
1128 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 return result;
1130}
1131
1132EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1133 EGLSurface read, EGLContext ctx)
1134{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001135 // get a reference to the object passed in
1136 ContextRef _c(ctx);
1137 SurfaceRef _d(draw);
1138 SurfaceRef _r(read);
1139
1140 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 egl_display_t const * const dp = get_display(dpy);
1142 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001143 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1144 // EGL_NO_CONTEXT is valid
1145 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 }
1147
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001148 // these are the underlying implementation's object
1149 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001150 EGLSurface impl_draw = EGL_NO_SURFACE;
1151 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001152
1153 // these are our objects structs passed in
1154 egl_context_t * c = NULL;
1155 egl_surface_t const * d = NULL;
1156 egl_surface_t const * r = NULL;
1157
1158 // these are the current objects structs
1159 egl_context_t * cur_c = get_context(getContext());
1160 egl_surface_t * cur_r = NULL;
1161 egl_surface_t * cur_d = NULL;
1162
1163 if (ctx != EGL_NO_CONTEXT) {
1164 c = get_context(ctx);
1165 cur_r = get_surface(c->read);
1166 cur_d = get_surface(c->draw);
1167 impl_ctx = c->context;
1168 } else {
1169 // no context given, use the implementation of the current context
1170 if (cur_c == NULL) {
1171 // no current context
1172 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001173 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1174 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001175 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001176 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001177 return EGL_TRUE;
1178 }
1179 }
1180
1181 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001183 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001184 // make sure the EGLContext and EGLSurface passed in are for
1185 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001186 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001188 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001190
1191 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001193 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001194 // make sure the EGLContext and EGLSurface passed in are for
1195 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001196 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001198 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001200
1201 EGLBoolean result;
1202
1203 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001204 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001205 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001206 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001207 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001208 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001209 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210
1211 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001212 // by construction, these are either 0 or valid (possibly terminated)
1213 // it should be impossible for these to be invalid
1214 ContextRef _cur_c(cur_c);
1215 SurfaceRef _cur_r(cur_r);
1216 SurfaceRef _cur_d(cur_d);
1217
1218 // cur_c has to be valid here (but could be terminated)
1219 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001220 setGlThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001221 setContext(ctx);
1222 _c.acquire();
1223 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001224 setGlThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001225 setContext(EGL_NO_CONTEXT);
1226 }
1227 _cur_c.release();
1228
1229 _r.acquire();
1230 _cur_r.release();
1231 if (c) c->read = read;
1232
1233 _d.acquire();
1234 _cur_d.release();
1235 if (c) c->draw = draw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 }
1237 return result;
1238}
1239
1240
1241EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1242 EGLint attribute, EGLint *value)
1243{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001244 ContextRef _c(ctx);
1245 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 if (!validate_display_context(dpy, ctx))
1248 return EGL_FALSE;
1249
1250 egl_display_t const * const dp = get_display(dpy);
1251 egl_context_t * const c = get_context(ctx);
1252
Mathias Agopiancee79392010-07-26 21:14:59 -07001253 EGLBoolean result(EGL_TRUE);
1254 if (attribute == EGL_CONFIG_ID) {
1255 *value = dp->configs[intptr_t(c->config)].configId;
1256 } else {
1257 // We need to remap EGL_CONFIG_IDs
1258 result = c->cnx->egl.eglQueryContext(
1259 dp->disp[c->impl].dpy, c->context, attribute, value);
1260 }
1261
1262 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263}
1264
1265EGLContext eglGetCurrentContext(void)
1266{
Mathias Agopian923c6612009-08-17 18:07:06 -07001267 // could be called before eglInitialize(), but we wouldn't have a context
1268 // then, and this function would correctly return EGL_NO_CONTEXT.
1269
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270 EGLContext ctx = getContext();
1271 return ctx;
1272}
1273
1274EGLSurface eglGetCurrentSurface(EGLint readdraw)
1275{
Mathias Agopian923c6612009-08-17 18:07:06 -07001276 // could be called before eglInitialize(), but we wouldn't have a context
1277 // then, and this function would correctly return EGL_NO_SURFACE.
1278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 EGLContext ctx = getContext();
1280 if (ctx) {
1281 egl_context_t const * const c = get_context(ctx);
1282 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1283 switch (readdraw) {
1284 case EGL_READ: return c->read;
1285 case EGL_DRAW: return c->draw;
1286 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1287 }
1288 }
1289 return EGL_NO_SURFACE;
1290}
1291
1292EGLDisplay eglGetCurrentDisplay(void)
1293{
Mathias Agopian923c6612009-08-17 18:07:06 -07001294 // could be called before eglInitialize(), but we wouldn't have a context
1295 // then, and this function would correctly return EGL_NO_DISPLAY.
1296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297 EGLContext ctx = getContext();
1298 if (ctx) {
1299 egl_context_t const * const c = get_context(ctx);
1300 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1301 return c->dpy;
1302 }
1303 return EGL_NO_DISPLAY;
1304}
1305
1306EGLBoolean eglWaitGL(void)
1307{
Mathias Agopian923c6612009-08-17 18:07:06 -07001308 // could be called before eglInitialize(), but we wouldn't have a context
1309 // then, and this function would return GL_TRUE, which isn't wrong.
1310
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 EGLBoolean res = EGL_TRUE;
1312 EGLContext ctx = getContext();
1313 if (ctx) {
1314 egl_context_t const * const c = get_context(ctx);
1315 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1316 if (uint32_t(c->impl)>=2)
1317 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1318 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1319 if (!cnx->dso)
1320 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001321 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 }
1323 return res;
1324}
1325
1326EGLBoolean eglWaitNative(EGLint engine)
1327{
Mathias Agopian923c6612009-08-17 18:07:06 -07001328 // could be called before eglInitialize(), but we wouldn't have a context
1329 // then, and this function would return GL_TRUE, which isn't wrong.
1330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 EGLBoolean res = EGL_TRUE;
1332 EGLContext ctx = getContext();
1333 if (ctx) {
1334 egl_context_t const * const c = get_context(ctx);
1335 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1336 if (uint32_t(c->impl)>=2)
1337 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1338 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1339 if (!cnx->dso)
1340 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001341 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342 }
1343 return res;
1344}
1345
1346EGLint eglGetError(void)
1347{
1348 EGLint result = EGL_SUCCESS;
Mathias Agopian02dafb52010-09-21 15:43:59 -07001349 EGLint err;
Mathias Agopian618fa102009-10-14 02:06:37 -07001350 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian02dafb52010-09-21 15:43:59 -07001351 err = EGL_SUCCESS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352 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 }
Mathias Agopian02dafb52010-09-21 15:43:59 -07001358 err = getError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 if (result == EGL_SUCCESS)
Mathias Agopian02dafb52010-09-21 15:43:59 -07001360 result = err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 return result;
1362}
1363
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001364__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001366 // eglGetProcAddress() could be the very first function called
1367 // in which case we must make sure we've initialized ourselves, this
1368 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001369
1370 if (egl_init_drivers() == EGL_FALSE) {
1371 setError(EGL_BAD_PARAMETER, NULL);
1372 return NULL;
1373 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375 __eglMustCastToProperFunctionPointerType addr;
1376 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1377 if (addr) return addr;
1378
Mathias Agopian24035332010-08-02 17:34:32 -07001379 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1380 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381
Mathias Agopian24035332010-08-02 17:34:32 -07001382 /*
1383 * Since eglGetProcAddress() is not associated to anything, it needs
1384 * to return a function pointer that "works" regardless of what
1385 * the current context is.
1386 *
1387 * For this reason, we return a "forwarder", a small stub that takes
1388 * care of calling the function associated with the context
1389 * currently bound.
1390 *
1391 * We first look for extensions we've already resolved, if we're seeing
1392 * this extension for the first time, we go through all our
1393 * implementations and call eglGetProcAddress() and record the
1394 * result in the appropriate implementation hooks and return the
1395 * address of the forwarder corresponding to that hook set.
1396 *
1397 */
1398
1399 const String8 name(procname);
1400 addr = gGLExtentionMap.valueFor(name);
1401 const int slot = gGLExtentionSlot;
1402
1403 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1404 "no more slots for eglGetProcAddress(\"%s\")",
1405 procname);
1406
1407 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1408 bool found = false;
1409 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1410 egl_connection_t* const cnx = &gEGLImpl[i];
1411 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1412 found = true;
Mathias Agopian4a88b522010-08-13 12:19:04 -07001413 // Extensions are independent of the bound context
1414 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1415 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian24035332010-08-02 17:34:32 -07001416 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 }
1418 }
Mathias Agopian24035332010-08-02 17:34:32 -07001419 if (found) {
1420 addr = gExtensionForwarders[slot];
1421 gGLExtentionMap.add(name, addr);
1422 gGLExtentionSlot++;
1423 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425
Mathias Agopian24035332010-08-02 17:34:32 -07001426 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian24035332010-08-02 17:34:32 -07001427 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428}
1429
1430EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1431{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001432 SurfaceRef _s(draw);
1433 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1434
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 if (!validate_display_surface(dpy, draw))
1436 return EGL_FALSE;
1437 egl_display_t const * const dp = get_display(dpy);
1438 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001439 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440}
1441
1442EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1443 NativePixmapType target)
1444{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001445 SurfaceRef _s(surface);
1446 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1447
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 if (!validate_display_surface(dpy, surface))
1449 return EGL_FALSE;
1450 egl_display_t const * const dp = get_display(dpy);
1451 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001452 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001453 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454}
1455
1456const char* eglQueryString(EGLDisplay dpy, EGLint name)
1457{
1458 egl_display_t const * const dp = get_display(dpy);
1459 switch (name) {
1460 case EGL_VENDOR:
1461 return gVendorString;
1462 case EGL_VERSION:
1463 return gVersionString;
1464 case EGL_EXTENSIONS:
1465 return gExtensionString;
1466 case EGL_CLIENT_APIS:
1467 return gClientApiString;
1468 }
1469 return setError(EGL_BAD_PARAMETER, (const char *)0);
1470}
1471
1472
1473// ----------------------------------------------------------------------------
1474// EGL 1.1
1475// ----------------------------------------------------------------------------
1476
1477EGLBoolean eglSurfaceAttrib(
1478 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1479{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001480 SurfaceRef _s(surface);
1481 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1482
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 if (!validate_display_surface(dpy, surface))
1484 return EGL_FALSE;
1485 egl_display_t const * const dp = get_display(dpy);
1486 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001487 if (s->cnx->egl.eglSurfaceAttrib) {
1488 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001489 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 }
1491 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1492}
1493
1494EGLBoolean eglBindTexImage(
1495 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1496{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001497 SurfaceRef _s(surface);
1498 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 if (!validate_display_surface(dpy, surface))
1501 return EGL_FALSE;
1502 egl_display_t const * const dp = get_display(dpy);
1503 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001504 if (s->cnx->egl.eglBindTexImage) {
1505 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001506 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 }
1508 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1509}
1510
1511EGLBoolean eglReleaseTexImage(
1512 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1513{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001514 SurfaceRef _s(surface);
1515 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1516
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517 if (!validate_display_surface(dpy, surface))
1518 return EGL_FALSE;
1519 egl_display_t const * const dp = get_display(dpy);
1520 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001521 if (s->cnx->egl.eglReleaseTexImage) {
1522 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001523 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 }
1525 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1526}
1527
1528EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1529{
1530 egl_display_t * const dp = get_display(dpy);
1531 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1532
1533 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001534 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 egl_connection_t* const cnx = &gEGLImpl[i];
1536 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001537 if (cnx->egl.eglSwapInterval) {
1538 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001539 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540 res = EGL_FALSE;
1541 }
1542 }
1543 }
1544 }
1545 return res;
1546}
1547
1548
1549// ----------------------------------------------------------------------------
1550// EGL 1.2
1551// ----------------------------------------------------------------------------
1552
1553EGLBoolean eglWaitClient(void)
1554{
Mathias Agopian923c6612009-08-17 18:07:06 -07001555 // could be called before eglInitialize(), but we wouldn't have a context
1556 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557 EGLBoolean res = EGL_TRUE;
1558 EGLContext ctx = getContext();
1559 if (ctx) {
1560 egl_context_t const * const c = get_context(ctx);
1561 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1562 if (uint32_t(c->impl)>=2)
1563 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1564 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1565 if (!cnx->dso)
1566 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001567 if (cnx->egl.eglWaitClient) {
1568 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001570 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571 }
1572 }
1573 return res;
1574}
1575
1576EGLBoolean eglBindAPI(EGLenum api)
1577{
Mathias Agopian923c6612009-08-17 18:07:06 -07001578 if (egl_init_drivers() == EGL_FALSE) {
1579 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1580 }
1581
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 // bind this API on all EGLs
1583 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001584 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585 egl_connection_t* const cnx = &gEGLImpl[i];
1586 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001587 if (cnx->egl.eglBindAPI) {
1588 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 res = EGL_FALSE;
1590 }
1591 }
1592 }
1593 }
1594 return res;
1595}
1596
1597EGLenum eglQueryAPI(void)
1598{
Mathias Agopian923c6612009-08-17 18:07:06 -07001599 if (egl_init_drivers() == EGL_FALSE) {
1600 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1601 }
1602
Mathias Agopian618fa102009-10-14 02:06:37 -07001603 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 egl_connection_t* const cnx = &gEGLImpl[i];
1605 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001606 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 // the first one we find is okay, because they all
1608 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001609 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 }
1611 }
1612 }
1613 // or, it can only be OpenGL ES
1614 return EGL_OPENGL_ES_API;
1615}
1616
1617EGLBoolean eglReleaseThread(void)
1618{
Mathias Agopian618fa102009-10-14 02:06:37 -07001619 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 egl_connection_t* const cnx = &gEGLImpl[i];
1621 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001622 if (cnx->egl.eglReleaseThread) {
1623 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624 }
1625 }
1626 }
1627 clearTLS();
1628 return EGL_TRUE;
1629}
1630
1631EGLSurface eglCreatePbufferFromClientBuffer(
1632 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1633 EGLConfig config, const EGLint *attrib_list)
1634{
1635 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001636 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001638 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1639 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001640 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1641 buftype, buffer,
1642 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643 }
1644 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1645}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001646
1647// ----------------------------------------------------------------------------
1648// EGL_EGLEXT_VERSION 3
1649// ----------------------------------------------------------------------------
1650
1651EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1652 const EGLint *attrib_list)
1653{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001654 SurfaceRef _s(surface);
1655 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1656
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001657 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001658 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001659
1660 egl_display_t const * const dp = get_display(dpy);
1661 egl_surface_t const * const s = get_surface(surface);
1662
Mathias Agopian618fa102009-10-14 02:06:37 -07001663 if (s->cnx->egl.eglLockSurfaceKHR) {
1664 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001665 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001666 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001667 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001668}
1669
1670EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1671{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001672 SurfaceRef _s(surface);
1673 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1674
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001675 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001676 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001677
1678 egl_display_t const * const dp = get_display(dpy);
1679 egl_surface_t const * const s = get_surface(surface);
1680
Mathias Agopian618fa102009-10-14 02:06:37 -07001681 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1682 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001683 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001684 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001685 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001686}
1687
1688EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1689 EGLClientBuffer buffer, const EGLint *attrib_list)
1690{
1691 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001692 ContextRef _c(ctx);
1693 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001694 if (!validate_display_context(dpy, ctx))
1695 return EGL_NO_IMAGE_KHR;
1696 egl_display_t const * const dp = get_display(dpy);
1697 egl_context_t * const c = get_context(ctx);
1698 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001699 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001700 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001701 if (image == EGL_NO_IMAGE_KHR)
1702 return image;
1703
1704 egl_image_t* result = new egl_image_t(dpy, ctx);
1705 result->images[c->impl] = image;
1706 return (EGLImageKHR)result;
1707 } else {
1708 // EGL_NO_CONTEXT is a valid parameter
1709 egl_display_t const * const dp = get_display(dpy);
1710 if (dp == 0) {
1711 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1712 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001713
1714 /* Since we don't have a way to know which implementation to call,
1715 * we're calling all of them. If at least one of the implementation
1716 * succeeded, this is a success.
1717 */
1718
1719 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001720
Mathias Agopian618fa102009-10-14 02:06:37 -07001721 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001722 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001723 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001724 egl_connection_t* const cnx = &gEGLImpl[i];
1725 implImages[i] = EGL_NO_IMAGE_KHR;
1726 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001727 if (cnx->egl.eglCreateImageKHR) {
1728 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001729 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001730 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1731 success = true;
1732 }
1733 }
1734 }
1735 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001736
1737 if (!success) {
1738 // failure, if there was an error when we entered this function,
1739 // the error flag must not be updated.
1740 // Otherwise, the error is whatever happened in the implementation
1741 // that faulted.
1742 if (currentError != EGL_SUCCESS) {
1743 setError(currentError, EGL_NO_IMAGE_KHR);
1744 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001745 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001746 } else {
1747 // In case of success, we need to clear all error flags
1748 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07001749 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001750 // a "full" success (all implementation succeeded).
1751 eglGetError();
1752 }
1753
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001754 egl_image_t* result = new egl_image_t(dpy, ctx);
1755 memcpy(result->images, implImages, sizeof(implImages));
1756 return (EGLImageKHR)result;
1757 }
1758}
1759
1760EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1761{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001762 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001763 if (dp == 0) {
1764 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1765 }
1766
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001767 ImageRef _i(img);
1768 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001769
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001770 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001771 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001772 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001773 egl_connection_t* const cnx = &gEGLImpl[i];
1774 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1775 if (cnx->dso) {
Mathias Agopian77fbf8d2010-09-09 11:12:54 -07001776 if (cnx->egl.eglDestroyImageKHR) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001777 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001778 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001779 success = true;
1780 }
1781 }
1782 }
1783 }
1784 }
1785 if (!success)
1786 return EGL_FALSE;
1787
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001788 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001789
Mathias Agopian24e5f522009-08-12 21:18:15 -07001790 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001791}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001792
1793
1794// ----------------------------------------------------------------------------
1795// ANDROID extensions
1796// ----------------------------------------------------------------------------
1797
1798EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1799 EGLint left, EGLint top, EGLint width, EGLint height)
1800{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001801 SurfaceRef _s(draw);
1802 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1803
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001804 if (!validate_display_surface(dpy, draw))
1805 return EGL_FALSE;
1806 egl_display_t const * const dp = get_display(dpy);
1807 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001808 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
1809 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001810 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001811 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001812 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001813}