blob: bc944a038455b3176e1cce64825d49287af6fcad [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 Agopian8e4b5a32010-08-27 16:08:03 -070062 "EGL_KHR_gl_texture_2D_image "
Mathias Agopianc291f5852010-08-27 16:48:56 -070063 "EGL_KHR_fence_sync "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070064 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070065 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070066 ;
67
68// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Mathias Agopian9429e9c2009-08-21 02:18:25 -070070class egl_object_t {
71 static SortedVector<egl_object_t*> sObjects;
72 static Mutex sLock;
73
74 volatile int32_t terminated;
75 mutable volatile int32_t count;
76
77public:
78 egl_object_t() : terminated(0), count(1) {
79 Mutex::Autolock _l(sLock);
80 sObjects.add(this);
81 }
82
83 inline bool isAlive() const { return !terminated; }
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -070086 bool get() {
87 Mutex::Autolock _l(sLock);
88 if (egl_object_t::sObjects.indexOf(this) >= 0) {
89 android_atomic_inc(&count);
90 return true;
91 }
92 return false;
93 }
94
95 bool put() {
96 Mutex::Autolock _l(sLock);
97 if (android_atomic_dec(&count) == 1) {
98 sObjects.remove(this);
99 return true;
100 }
101 return false;
102 }
103
104public:
105 template <typename N, typename T>
106 struct LocalRef {
107 N* ref;
108 LocalRef(T o) : ref(0) {
109 N* native = reinterpret_cast<N*>(o);
110 if (o && native->get()) {
111 ref = native;
112 }
113 }
114 ~LocalRef() {
115 if (ref && ref->put()) {
116 delete ref;
117 }
118 }
119 inline N* get() {
120 return ref;
121 }
122 void acquire() const {
123 if (ref) {
124 android_atomic_inc(&ref->count);
125 }
126 }
127 void release() const {
128 if (ref) {
129 int32_t c = android_atomic_dec(&ref->count);
130 // ref->count cannot be 1 prior atomic_dec because we have
131 // a reference, and if we have one, it means there was
132 // already one before us.
133 LOGE_IF(c==1, "refcount is now 0 in release()");
134 }
135 }
136 void terminate() {
137 if (ref) {
138 ref->terminated = 1;
139 release();
140 }
141 }
142 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143};
144
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700145SortedVector<egl_object_t*> egl_object_t::sObjects;
146Mutex egl_object_t::sLock;
147
Mathias Agopiancee79392010-07-26 21:14:59 -0700148
149struct egl_config_t {
150 egl_config_t() {}
151 egl_config_t(int impl, EGLConfig config)
152 : impl(impl), config(config), configId(0), implConfigId(0) { }
153 int impl; // the implementation this config is for
154 EGLConfig config; // the implementation's EGLConfig
155 EGLint configId; // our CONFIG_ID
156 EGLint implConfigId; // the implementation's CONFIG_ID
157 inline bool operator < (const egl_config_t& rhs) const {
158 if (impl < rhs.impl) return true;
159 if (impl > rhs.impl) return false;
160 return config < rhs.config;
161 }
162};
163
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700164struct egl_display_t {
165 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 struct strings_t {
168 char const * vendor;
169 char const * version;
170 char const * clientApi;
171 char const * extensions;
172 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700173
174 struct DisplayImpl {
175 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
176 state(NOT_INITIALIZED), numConfigs(0) { }
177 EGLDisplay dpy;
178 EGLConfig* config;
179 EGLint state;
180 EGLint numConfigs;
181 strings_t queryString;
182 };
183
Mathias Agopiancee79392010-07-26 21:14:59 -0700184 uint32_t magic;
185 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
186 EGLint numTotalConfigs;
187 egl_config_t* configs;
188 uint32_t refs;
189 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700190
Mathias Agopiancee79392010-07-26 21:14:59 -0700191 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700192 ~egl_display_t() { magic = 0; }
193 inline bool isValid() const { return magic == '_dpy'; }
194 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195};
196
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700197struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700199 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
200
Mathias Agopiancee79392010-07-26 21:14:59 -0700201 egl_surface_t(EGLDisplay dpy, EGLSurface surface, EGLConfig config,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700202 int impl, egl_connection_t const* cnx)
Mathias Agopiancee79392010-07-26 21:14:59 -0700203 : dpy(dpy), surface(surface), config(config), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 }
205 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 }
207 EGLDisplay dpy;
208 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700209 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 int impl;
211 egl_connection_t const* cnx;
212};
213
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700214struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700216 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
217
Mathias Agopiancee79392010-07-26 21:14:59 -0700218 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian618fa102009-10-14 02:06:37 -0700219 int impl, egl_connection_t const* cnx, int version)
220 : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx),
221 version(version)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 {
223 }
224 EGLDisplay dpy;
225 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700226 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 EGLSurface read;
228 EGLSurface draw;
229 int impl;
230 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700231 int version;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232};
233
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700234struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700235{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700236 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
237
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700238 egl_image_t(EGLDisplay dpy, EGLContext context)
239 : dpy(dpy), context(context)
240 {
241 memset(images, 0, sizeof(images));
242 }
243 EGLDisplay dpy;
Mathias Agopian77fbf8d2010-09-09 11:12:54 -0700244 EGLContext context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700245 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700246};
247
Mathias Agopianc291f5852010-08-27 16:48:56 -0700248struct egl_sync_t : public egl_object_t
249{
250 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
251
252 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync)
253 : dpy(dpy), context(context), sync(sync)
254 {
255 }
256 EGLDisplay dpy;
257 EGLContext context;
258 EGLSyncKHR sync;
259};
260
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700261typedef egl_surface_t::Ref SurfaceRef;
262typedef egl_context_t::Ref ContextRef;
263typedef egl_image_t::Ref ImageRef;
Mathias Agopianc291f5852010-08-27 16:48:56 -0700264typedef egl_sync_t::Ref SyncRef;
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266struct tls_t
267{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700268 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 EGLint error;
270 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700271 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272};
273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274
275// ----------------------------------------------------------------------------
276
Mathias Agopianbf41b112010-04-09 13:37:34 -0700277static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278static egl_display_t gDisplay[NUM_DISPLAYS];
279static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
280static pthread_key_t gEGLThreadLocalStorageKey = -1;
281
282// ----------------------------------------------------------------------------
283
Mathias Agopian618fa102009-10-14 02:06:37 -0700284EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
285EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700286EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
288// ----------------------------------------------------------------------------
289
290static __attribute__((noinline))
291const char *egl_strerror(EGLint err)
292{
293 switch (err){
294 case EGL_SUCCESS: return "EGL_SUCCESS";
295 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
296 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
297 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
298 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
299 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
300 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
301 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
302 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
303 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
304 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
305 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
306 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
307 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
308 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
309 default: return "UNKNOWN";
310 }
311}
312
313static __attribute__((noinline))
314void clearTLS() {
315 if (gEGLThreadLocalStorageKey != -1) {
316 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
317 if (tls) {
318 delete tls;
319 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
320 }
321 }
322}
323
324static tls_t* getTLS()
325{
326 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
327 if (tls == 0) {
328 tls = new tls_t;
329 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
330 }
331 return tls;
332}
333
334template<typename T>
335static __attribute__((noinline))
336T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
337 if (gEGLThreadLocalStorageKey == -1) {
338 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
339 if (gEGLThreadLocalStorageKey == -1)
340 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
341 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
342 }
343 tls_t* tls = getTLS();
344 if (tls->error != error) {
345 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
346 tls->error = error;
347 }
348 return returnValue;
349}
350
351static __attribute__((noinline))
352GLint getError() {
353 if (gEGLThreadLocalStorageKey == -1)
354 return EGL_SUCCESS;
355 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
356 if (!tls) return EGL_SUCCESS;
357 GLint error = tls->error;
358 tls->error = EGL_SUCCESS;
359 return error;
360}
361
362static __attribute__((noinline))
363void setContext(EGLContext ctx) {
364 if (gEGLThreadLocalStorageKey == -1) {
365 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
366 if (gEGLThreadLocalStorageKey == -1)
367 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
368 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
369 }
370 tls_t* tls = getTLS();
371 tls->ctx = ctx;
372}
373
374static __attribute__((noinline))
375EGLContext getContext() {
376 if (gEGLThreadLocalStorageKey == -1)
377 return EGL_NO_CONTEXT;
378 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
379 if (!tls) return EGL_NO_CONTEXT;
380 return tls->ctx;
381}
382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383/*****************************************************************************/
384
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385template<typename T>
386static __attribute__((noinline))
387int binarySearch(
388 T const sortedArray[], int first, int last, T key)
389{
390 while (first <= last) {
391 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700392 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 first = mid + 1;
394 } else if (key < sortedArray[mid]) {
395 last = mid - 1;
396 } else {
397 return mid;
398 }
399 }
400 return -1;
401}
402
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800403static int cmp_configs(const void* a, const void *b)
404{
Mathias Agopiancee79392010-07-26 21:14:59 -0700405 const egl_config_t& c0 = *(egl_config_t const *)a;
406 const egl_config_t& c1 = *(egl_config_t const *)b;
407 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408}
409
410struct extention_map_t {
411 const char* name;
412 __eglMustCastToProperFunctionPointerType address;
413};
414
415static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700416 { "eglLockSurfaceKHR",
417 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
418 { "eglUnlockSurfaceKHR",
419 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
420 { "eglCreateImageKHR",
421 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
422 { "eglDestroyImageKHR",
423 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700424 { "eglSetSwapRectangleANDROID",
425 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426};
427
Mathias Agopian24035332010-08-02 17:34:32 -0700428extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
429
430// accesses protected by gInitDriverMutex
431static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
432static int gGLExtentionSlot = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433
434static void(*findProcAddress(const char* name,
435 const extention_map_t* map, size_t n))()
436{
437 for (uint32_t i=0 ; i<n ; i++) {
438 if (!strcmp(name, map[i].name)) {
439 return map[i].address;
440 }
441 }
442 return NULL;
443}
444
445// ----------------------------------------------------------------------------
446
Mathias Agopian6f087122010-09-23 16:38:38 -0700447static int gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700448 tls_t* tls = getTLS();
449 if (tls->logCallWithNoContext == EGL_TRUE) {
450 tls->logCallWithNoContext = EGL_FALSE;
451 LOGE("call to OpenGL ES API with no current context "
452 "(logged once per thread)");
453 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700454 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700455}
456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457static void early_egl_init(void)
458{
459#if !USE_FAST_TLS_KEY
460 pthread_key_create(&gGLWrapperKey, NULL);
461#endif
462 uint32_t addr = (uint32_t)((void*)gl_no_context);
463 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700464 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700466 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700467
Mathias Agopian618fa102009-10-14 02:06:37 -0700468 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469}
470
471static pthread_once_t once_control = PTHREAD_ONCE_INIT;
472static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
473
474
475static inline
476egl_display_t* get_display(EGLDisplay dpy)
477{
478 uintptr_t index = uintptr_t(dpy)-1U;
479 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
480}
481
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700482template<typename NATIVE, typename EGL>
483static inline NATIVE* egl_to_native_cast(EGL arg) {
484 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800485}
486
487static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700488egl_surface_t* get_surface(EGLSurface surface) {
489 return egl_to_native_cast<egl_surface_t>(surface);
490}
491
492static inline
493egl_context_t* get_context(EGLContext context) {
494 return egl_to_native_cast<egl_context_t>(context);
495}
496
497static inline
498egl_image_t* get_image(EGLImageKHR image) {
499 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500}
501
Mathias Agopianc291f5852010-08-27 16:48:56 -0700502static inline
503egl_sync_t* get_sync(EGLSyncKHR sync) {
504 return egl_to_native_cast<egl_sync_t>(sync);
505}
506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800507static egl_connection_t* validate_display_config(
508 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700509 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510{
511 dp = get_display(dpy);
512 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
513
Mathias Agopiancee79392010-07-26 21:14:59 -0700514 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
516 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700517 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518 if (cnx->dso == 0) {
519 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
520 }
521 return cnx;
522}
523
524static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
525{
526 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
527 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700528 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700530 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800531 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
532 return EGL_TRUE;
533}
534
535static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
536{
537 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
538 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700539 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800540 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700541 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542 return setError(EGL_BAD_SURFACE, EGL_FALSE);
543 return EGL_TRUE;
544}
545
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
547{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700548 ImageRef _i(image);
549 if (!_i.get()) return EGL_NO_IMAGE_KHR;
550
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700551 EGLContext context = getContext();
552 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
553 return EGL_NO_IMAGE_KHR;
554
555 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700556 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700557 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700559 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700560 return i->images[c->impl];
561}
562
Mathias Agopian923c6612009-08-17 18:07:06 -0700563// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700564
Mathias Agopian923c6612009-08-17 18:07:06 -0700565// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700566// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700567// egl_init_drivers_locked()
568//
569static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
570
571EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800572{
573 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700574 // initialized by static ctor. should be set here.
575 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 }
577
Mathias Agopiande586972009-05-28 17:39:03 -0700578 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700579 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700580
Mathias Agopian923c6612009-08-17 18:07:06 -0700581 // dynamically load all our EGL implementations for all displays
582 // and retrieve the corresponding EGLDisplay
583 // if that fails, don't use this driver.
584 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
585 egl_connection_t* cnx;
586 egl_display_t* d = &gDisplay[0];
587
588 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700590 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
591 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
592 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700593 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700594 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700595 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700596 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700597 if (dpy == EGL_NO_DISPLAY) {
598 loader.close(cnx->dso);
599 cnx->dso = NULL;
600 }
601 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 }
603
604 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700605 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 char value[PROPERTY_VALUE_MAX];
607 property_get("debug.egl.hw", value, "1");
608 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700609 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
610 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
611 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700612 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700613 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700614 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700615 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700616 if (dpy == EGL_NO_DISPLAY) {
617 loader.close(cnx->dso);
618 cnx->dso = NULL;
619 }
620 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 } else {
622 LOGD("3D hardware acceleration is disabled");
623 }
624 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700625
Mathias Agopian923c6612009-08-17 18:07:06 -0700626 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
627 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 }
629
Mathias Agopian923c6612009-08-17 18:07:06 -0700630 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631}
632
Mathias Agopian923c6612009-08-17 18:07:06 -0700633EGLBoolean egl_init_drivers()
634{
635 EGLBoolean res;
636 pthread_mutex_lock(&gInitDriverMutex);
637 res = egl_init_drivers_locked();
638 pthread_mutex_unlock(&gInitDriverMutex);
639 return res;
640}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700641
642// ----------------------------------------------------------------------------
643}; // namespace android
644// ----------------------------------------------------------------------------
645
646using namespace android;
647
648EGLDisplay eglGetDisplay(NativeDisplayType display)
649{
Mathias Agopian923c6612009-08-17 18:07:06 -0700650 uint32_t index = uint32_t(display);
651 if (index >= NUM_DISPLAYS) {
652 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
653 }
654
655 if (egl_init_drivers() == EGL_FALSE) {
656 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
657 }
658
659 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
660 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700661}
662
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663// ----------------------------------------------------------------------------
664// Initialization
665// ----------------------------------------------------------------------------
666
667EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
668{
669 egl_display_t * const dp = get_display(dpy);
670 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
671
Mathias Agopian75bc2782010-02-05 16:17:01 -0800672 Mutex::Autolock _l(dp->lock);
673
674 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 if (major != NULL) *major = VERSION_MAJOR;
676 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700677 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800678 return EGL_TRUE;
679 }
680
Mathias Agopian618fa102009-10-14 02:06:37 -0700681 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682
683 // initialize each EGL and
684 // build our own extension string first, based on the extension we know
685 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700686 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800687 egl_connection_t* const cnx = &gEGLImpl[i];
688 cnx->major = -1;
689 cnx->minor = -1;
690 if (!cnx->dso)
691 continue;
692
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700693#if defined(ADRENO130)
694#warning "Adreno-130 eglInitialize() workaround"
695 /*
696 * The ADRENO 130 driver returns a different EGLDisplay each time
697 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
698 * after eglTerminate() has been called, so that eglInitialize()
699 * cannot be called again. Therefore, we need to make sure to call
700 * eglGetDisplay() before calling eglInitialize();
701 */
702 if (i == IMPL_HARDWARE) {
703 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700704 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700705 }
706#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700708
709 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700710 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800711 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700712 // i, idpy, cnx->major, cnx->minor, cnx);
713
714 // display is now initialized
715 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716
717 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700718 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700719 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700720 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700721 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700722 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700723 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700724 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700725 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726
727 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700728 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700729 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 }
731 }
732
733 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700734 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735 egl_connection_t* const cnx = &gEGLImpl[i];
736 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
737 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700738 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700739 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
740 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700741 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700742 dp->disp[i].dpy, dp->disp[i].config, n,
743 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745 dp->numTotalConfigs += n;
746 res = EGL_TRUE;
747 }
748 }
749 }
750 }
751 }
752
753 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700754 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
755 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
756 egl_connection_t* const cnx = &gEGLImpl[i];
757 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
758 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
759 dp->configs[k].impl = i;
760 dp->configs[k].config = dp->disp[i].config[j];
761 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
762 // store the implementation's CONFIG_ID
763 cnx->egl.eglGetConfigAttrib(
764 dp->disp[i].dpy,
765 dp->disp[i].config[j],
766 EGL_CONFIG_ID,
767 &dp->configs[k].implConfigId);
768 k++;
769 }
770 }
771 }
772
773 // sort our configurations so we can do binary-searches
774 qsort( dp->configs,
775 dp->numTotalConfigs,
776 sizeof(egl_config_t), cmp_configs);
777
Mathias Agopian75bc2782010-02-05 16:17:01 -0800778 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800779 if (major != NULL) *major = VERSION_MAJOR;
780 if (minor != NULL) *minor = VERSION_MINOR;
781 return EGL_TRUE;
782 }
783 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
784}
785
786EGLBoolean eglTerminate(EGLDisplay dpy)
787{
Mathias Agopian923c6612009-08-17 18:07:06 -0700788 // NOTE: don't unload the drivers b/c some APIs can be called
789 // after eglTerminate() has been called. eglTerminate() only
790 // terminates an EGLDisplay, not a EGL itself.
791
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792 egl_display_t* const dp = get_display(dpy);
793 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800794
795 Mutex::Autolock _l(dp->lock);
796
797 if (dp->refs == 0) {
798 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
799 }
800
801 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700802 if (dp->refs > 1) {
803 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700805 }
Mathias Agopiande586972009-05-28 17:39:03 -0700806
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800807 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700808 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800809 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700810 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700811 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700812 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700813 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700814 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700815 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700816 free(dp->disp[i].config);
817
818 dp->disp[i].numConfigs = 0;
819 dp->disp[i].config = 0;
820 dp->disp[i].state = egl_display_t::TERMINATED;
821
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 res = EGL_TRUE;
823 }
824 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700825
826 // TODO: all egl_object_t should be marked for termination
827
Mathias Agopian75bc2782010-02-05 16:17:01 -0800828 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700830 delete [] dp->configs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831 clearTLS();
832 return res;
833}
834
835// ----------------------------------------------------------------------------
836// configuration
837// ----------------------------------------------------------------------------
838
839EGLBoolean eglGetConfigs( EGLDisplay dpy,
840 EGLConfig *configs,
841 EGLint config_size, EGLint *num_config)
842{
843 egl_display_t const * const dp = get_display(dpy);
844 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
845
846 GLint numConfigs = dp->numTotalConfigs;
847 if (!configs) {
848 *num_config = numConfigs;
849 return EGL_TRUE;
850 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700851
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800852 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700853 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
854 *configs++ = EGLConfig(i);
855 config_size--;
856 n++;
857 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858
859 *num_config = n;
860 return EGL_TRUE;
861}
862
863EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
864 EGLConfig *configs, EGLint config_size,
865 EGLint *num_config)
866{
867 egl_display_t const * const dp = get_display(dpy);
868 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
869
Jack Palevich749c63d2009-03-25 15:12:17 -0700870 if (num_config==0) {
871 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 }
873
874 EGLint n;
875 EGLBoolean res = EGL_FALSE;
876 *num_config = 0;
877
878
879 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -0700880 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881 // to figure out both its size and if it contains an EGL_CONFIG_ID
882 // key. If so, the full array is copied and patched.
883 // NOTE: we assume that there can be only one occurrence
884 // of EGL_CONFIG_ID.
885
886 EGLint patch_index = -1;
887 GLint attr;
888 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -0700889 if (attrib_list) {
890 while ((attr=attrib_list[size]) != EGL_NONE) {
891 if (attr == EGL_CONFIG_ID)
892 patch_index = size;
893 size += 2;
894 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 }
896 if (patch_index >= 0) {
897 size += 2; // we need copy the sentinel as well
898 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
899 if (new_list == 0)
900 return setError(EGL_BAD_ALLOC, EGL_FALSE);
901 memcpy(new_list, attrib_list, size*sizeof(EGLint));
902
903 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -0700904 bool found = false;
905 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -0700907 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
908 if (dp->configs[i].configId == configId) {
909 ourConfig = EGLConfig(i);
910 configId = dp->configs[i].implConfigId;
911 found = true;
912 break;
913 }
914 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915
Mathias Agopiancee79392010-07-26 21:14:59 -0700916 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
917 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 // and switch to the new list
919 attrib_list = const_cast<const EGLint *>(new_list);
920
921 // At this point, the only configuration that can match is
922 // dp->configs[i][index], however, we don't know if it would be
923 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -0700924 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800925 // through all the EGLimpl[].
926 // We also know we can only get a single config back, and we know
927 // which one.
928
Mathias Agopian618fa102009-10-14 02:06:37 -0700929 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -0700930 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
931 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 if (res && n>0) {
933 // n has to be 0 or 1, by construction, and we already know
934 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -0700935 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700936 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -0700937 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 *num_config = 1;
939 }
940 }
941
942 free(const_cast<EGLint *>(attrib_list));
943 return res;
944 }
945
Mathias Agopiancee79392010-07-26 21:14:59 -0700946
Mathias Agopian618fa102009-10-14 02:06:37 -0700947 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 egl_connection_t* const cnx = &gEGLImpl[i];
949 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700950 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700951 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -0700952 if (configs) {
953 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -0700954 // internal EGLConfig format.
955 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -0700956 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700957 egl_config_t key(i, configs[j]);
958 intptr_t index = binarySearch<egl_config_t>(
959 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -0700960 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700961 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -0700962 } else {
963 return setError(EGL_BAD_CONFIG, EGL_FALSE);
964 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965 }
Jack Palevich749c63d2009-03-25 15:12:17 -0700966 configs += n;
967 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 *num_config += n;
970 res = EGL_TRUE;
971 }
972 }
973 }
974 return res;
975}
976
977EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
978 EGLint attribute, EGLint *value)
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) return EGL_FALSE;
983
984 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700985 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986 return EGL_TRUE;
987 }
Mathias Agopian618fa102009-10-14 02:06:37 -0700988 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -0700989 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
990 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991}
992
993// ----------------------------------------------------------------------------
994// surfaces
995// ----------------------------------------------------------------------------
996
997EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
998 NativeWindowType window,
999 const EGLint *attrib_list)
1000{
1001 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001002 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001004 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001005 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1006 dp->configs[intptr_t(config)].config, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001008 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1009 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010 return s;
1011 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 }
1013 return EGL_NO_SURFACE;
1014}
1015
1016EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1017 NativePixmapType pixmap,
1018 const EGLint *attrib_list)
1019{
1020 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001021 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001023 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001024 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1025 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001027 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1028 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 return s;
1030 }
1031 }
1032 return EGL_NO_SURFACE;
1033}
1034
1035EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1036 const EGLint *attrib_list)
1037{
1038 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001039 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001041 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001042 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1043 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001045 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1046 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047 return s;
1048 }
1049 }
1050 return EGL_NO_SURFACE;
1051}
1052
1053EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1054{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001055 SurfaceRef _s(surface);
1056 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 if (!validate_display_surface(dpy, surface))
1059 return EGL_FALSE;
1060 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001062 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001063 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001064 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001065 if (result == EGL_TRUE) {
1066 _s.terminate();
1067 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 return result;
1069}
1070
1071EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1072 EGLint attribute, EGLint *value)
1073{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001074 SurfaceRef _s(surface);
1075 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 if (!validate_display_surface(dpy, surface))
1078 return EGL_FALSE;
1079 egl_display_t const * const dp = get_display(dpy);
1080 egl_surface_t const * const s = get_surface(surface);
1081
Mathias Agopiancee79392010-07-26 21:14:59 -07001082 EGLBoolean result(EGL_TRUE);
1083 if (attribute == EGL_CONFIG_ID) {
1084 // We need to remap EGL_CONFIG_IDs
1085 *value = dp->configs[intptr_t(s->config)].configId;
1086 } else {
1087 result = s->cnx->egl.eglQuerySurface(
1088 dp->disp[s->impl].dpy, s->surface, attribute, value);
1089 }
1090
1091 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092}
1093
1094// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001095// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096// ----------------------------------------------------------------------------
1097
1098EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1099 EGLContext share_list, const EGLint *attrib_list)
1100{
1101 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001102 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001104 if (share_list != EGL_NO_CONTEXT) {
1105 egl_context_t* const c = get_context(share_list);
1106 share_list = c->context;
1107 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001108 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001109 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1110 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001111 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001113 // figure out if it's a GLESv1 or GLESv2
1114 int version = 0;
1115 if (attrib_list) {
1116 while (*attrib_list != EGL_NONE) {
1117 GLint attr = *attrib_list++;
1118 GLint value = *attrib_list++;
1119 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1120 if (value == 1) {
1121 version = GLESv1_INDEX;
1122 } else if (value == 2) {
1123 version = GLESv2_INDEX;
1124 }
1125 }
1126 };
1127 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001128 egl_context_t* c = new egl_context_t(dpy, context, config,
1129 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001130 return c;
1131 }
1132 }
1133 return EGL_NO_CONTEXT;
1134}
1135
1136EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1137{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001138 ContextRef _c(ctx);
1139 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1140
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 if (!validate_display_context(dpy, ctx))
1142 return EGL_FALSE;
1143 egl_display_t const * const dp = get_display(dpy);
1144 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001145 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001146 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001147 if (result == EGL_TRUE) {
1148 _c.terminate();
1149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 return result;
1151}
1152
1153EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1154 EGLSurface read, EGLContext ctx)
1155{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001156 // get a reference to the object passed in
1157 ContextRef _c(ctx);
1158 SurfaceRef _d(draw);
1159 SurfaceRef _r(read);
1160
1161 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001162 egl_display_t const * const dp = get_display(dpy);
1163 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001164 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1165 // EGL_NO_CONTEXT is valid
1166 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 }
1168
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001169 // these are the underlying implementation's object
1170 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001171 EGLSurface impl_draw = EGL_NO_SURFACE;
1172 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001173
1174 // these are our objects structs passed in
1175 egl_context_t * c = NULL;
1176 egl_surface_t const * d = NULL;
1177 egl_surface_t const * r = NULL;
1178
1179 // these are the current objects structs
1180 egl_context_t * cur_c = get_context(getContext());
1181 egl_surface_t * cur_r = NULL;
1182 egl_surface_t * cur_d = NULL;
1183
1184 if (ctx != EGL_NO_CONTEXT) {
1185 c = get_context(ctx);
1186 cur_r = get_surface(c->read);
1187 cur_d = get_surface(c->draw);
1188 impl_ctx = c->context;
1189 } else {
1190 // no context given, use the implementation of the current context
1191 if (cur_c == NULL) {
1192 // no current context
1193 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001194 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1195 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001196 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001197 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001198 return EGL_TRUE;
1199 }
1200 }
1201
1202 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001204 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001205 // make sure the EGLContext and EGLSurface passed in are for
1206 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001207 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001209 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001211
1212 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001214 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001215 // make sure the EGLContext and EGLSurface passed in are for
1216 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001217 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001219 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001221
1222 EGLBoolean result;
1223
1224 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001225 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001226 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001227 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001228 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001229 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001230 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231
1232 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001233 // by construction, these are either 0 or valid (possibly terminated)
1234 // it should be impossible for these to be invalid
1235 ContextRef _cur_c(cur_c);
1236 SurfaceRef _cur_r(cur_r);
1237 SurfaceRef _cur_d(cur_d);
1238
1239 // cur_c has to be valid here (but could be terminated)
1240 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001241 setGlThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001242 setContext(ctx);
1243 _c.acquire();
1244 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001245 setGlThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001246 setContext(EGL_NO_CONTEXT);
1247 }
1248 _cur_c.release();
1249
1250 _r.acquire();
1251 _cur_r.release();
1252 if (c) c->read = read;
1253
1254 _d.acquire();
1255 _cur_d.release();
1256 if (c) c->draw = draw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257 }
1258 return result;
1259}
1260
1261
1262EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1263 EGLint attribute, EGLint *value)
1264{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001265 ContextRef _c(ctx);
1266 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 if (!validate_display_context(dpy, ctx))
1269 return EGL_FALSE;
1270
1271 egl_display_t const * const dp = get_display(dpy);
1272 egl_context_t * const c = get_context(ctx);
1273
Mathias Agopiancee79392010-07-26 21:14:59 -07001274 EGLBoolean result(EGL_TRUE);
1275 if (attribute == EGL_CONFIG_ID) {
1276 *value = dp->configs[intptr_t(c->config)].configId;
1277 } else {
1278 // We need to remap EGL_CONFIG_IDs
1279 result = c->cnx->egl.eglQueryContext(
1280 dp->disp[c->impl].dpy, c->context, attribute, value);
1281 }
1282
1283 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284}
1285
1286EGLContext eglGetCurrentContext(void)
1287{
Mathias Agopian923c6612009-08-17 18:07:06 -07001288 // could be called before eglInitialize(), but we wouldn't have a context
1289 // then, and this function would correctly return EGL_NO_CONTEXT.
1290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 EGLContext ctx = getContext();
1292 return ctx;
1293}
1294
1295EGLSurface eglGetCurrentSurface(EGLint readdraw)
1296{
Mathias Agopian923c6612009-08-17 18:07:06 -07001297 // could be called before eglInitialize(), but we wouldn't have a context
1298 // then, and this function would correctly return EGL_NO_SURFACE.
1299
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 EGLContext ctx = getContext();
1301 if (ctx) {
1302 egl_context_t const * const c = get_context(ctx);
1303 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1304 switch (readdraw) {
1305 case EGL_READ: return c->read;
1306 case EGL_DRAW: return c->draw;
1307 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1308 }
1309 }
1310 return EGL_NO_SURFACE;
1311}
1312
1313EGLDisplay eglGetCurrentDisplay(void)
1314{
Mathias Agopian923c6612009-08-17 18:07:06 -07001315 // could be called before eglInitialize(), but we wouldn't have a context
1316 // then, and this function would correctly return EGL_NO_DISPLAY.
1317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 EGLContext ctx = getContext();
1319 if (ctx) {
1320 egl_context_t const * const c = get_context(ctx);
1321 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1322 return c->dpy;
1323 }
1324 return EGL_NO_DISPLAY;
1325}
1326
1327EGLBoolean eglWaitGL(void)
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.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343 }
1344 return res;
1345}
1346
1347EGLBoolean eglWaitNative(EGLint engine)
1348{
Mathias Agopian923c6612009-08-17 18:07:06 -07001349 // could be called before eglInitialize(), but we wouldn't have a context
1350 // then, and this function would return GL_TRUE, which isn't wrong.
1351
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352 EGLBoolean res = EGL_TRUE;
1353 EGLContext ctx = getContext();
1354 if (ctx) {
1355 egl_context_t const * const c = get_context(ctx);
1356 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1357 if (uint32_t(c->impl)>=2)
1358 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1359 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1360 if (!cnx->dso)
1361 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001362 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 }
1364 return res;
1365}
1366
1367EGLint eglGetError(void)
1368{
1369 EGLint result = EGL_SUCCESS;
Mathias Agopian02dafb52010-09-21 15:43:59 -07001370 EGLint err;
Mathias Agopian618fa102009-10-14 02:06:37 -07001371 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian02dafb52010-09-21 15:43:59 -07001372 err = EGL_SUCCESS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 egl_connection_t* const cnx = &gEGLImpl[i];
1374 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001375 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1377 result = err;
1378 }
Mathias Agopian02dafb52010-09-21 15:43:59 -07001379 err = getError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 if (result == EGL_SUCCESS)
Mathias Agopian02dafb52010-09-21 15:43:59 -07001381 result = err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 return result;
1383}
1384
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001385__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001387 // eglGetProcAddress() could be the very first function called
1388 // in which case we must make sure we've initialized ourselves, this
1389 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001390
1391 if (egl_init_drivers() == EGL_FALSE) {
1392 setError(EGL_BAD_PARAMETER, NULL);
1393 return NULL;
1394 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 __eglMustCastToProperFunctionPointerType addr;
1397 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1398 if (addr) return addr;
1399
Mathias Agopian24035332010-08-02 17:34:32 -07001400 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1401 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402
Mathias Agopian24035332010-08-02 17:34:32 -07001403 /*
1404 * Since eglGetProcAddress() is not associated to anything, it needs
1405 * to return a function pointer that "works" regardless of what
1406 * the current context is.
1407 *
1408 * For this reason, we return a "forwarder", a small stub that takes
1409 * care of calling the function associated with the context
1410 * currently bound.
1411 *
1412 * We first look for extensions we've already resolved, if we're seeing
1413 * this extension for the first time, we go through all our
1414 * implementations and call eglGetProcAddress() and record the
1415 * result in the appropriate implementation hooks and return the
1416 * address of the forwarder corresponding to that hook set.
1417 *
1418 */
1419
1420 const String8 name(procname);
1421 addr = gGLExtentionMap.valueFor(name);
1422 const int slot = gGLExtentionSlot;
1423
1424 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1425 "no more slots for eglGetProcAddress(\"%s\")",
1426 procname);
1427
1428 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1429 bool found = false;
1430 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1431 egl_connection_t* const cnx = &gEGLImpl[i];
1432 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1433 found = true;
Mathias Agopian4a88b522010-08-13 12:19:04 -07001434 // Extensions are independent of the bound context
1435 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1436 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian24035332010-08-02 17:34:32 -07001437 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 }
1439 }
Mathias Agopian24035332010-08-02 17:34:32 -07001440 if (found) {
1441 addr = gExtensionForwarders[slot];
1442 gGLExtentionMap.add(name, addr);
1443 gGLExtentionSlot++;
1444 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446
Mathias Agopian24035332010-08-02 17:34:32 -07001447 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian24035332010-08-02 17:34:32 -07001448 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449}
1450
1451EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1452{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001453 SurfaceRef _s(draw);
1454 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 if (!validate_display_surface(dpy, draw))
1457 return EGL_FALSE;
1458 egl_display_t const * const dp = get_display(dpy);
1459 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001460 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461}
1462
1463EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1464 NativePixmapType target)
1465{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001466 SurfaceRef _s(surface);
1467 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1468
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469 if (!validate_display_surface(dpy, surface))
1470 return EGL_FALSE;
1471 egl_display_t const * const dp = get_display(dpy);
1472 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001473 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001474 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475}
1476
1477const char* eglQueryString(EGLDisplay dpy, EGLint name)
1478{
1479 egl_display_t const * const dp = get_display(dpy);
1480 switch (name) {
1481 case EGL_VENDOR:
1482 return gVendorString;
1483 case EGL_VERSION:
1484 return gVersionString;
1485 case EGL_EXTENSIONS:
1486 return gExtensionString;
1487 case EGL_CLIENT_APIS:
1488 return gClientApiString;
1489 }
1490 return setError(EGL_BAD_PARAMETER, (const char *)0);
1491}
1492
1493
1494// ----------------------------------------------------------------------------
1495// EGL 1.1
1496// ----------------------------------------------------------------------------
1497
1498EGLBoolean eglSurfaceAttrib(
1499 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1500{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001501 SurfaceRef _s(surface);
1502 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1503
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504 if (!validate_display_surface(dpy, surface))
1505 return EGL_FALSE;
1506 egl_display_t const * const dp = get_display(dpy);
1507 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001508 if (s->cnx->egl.eglSurfaceAttrib) {
1509 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001510 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511 }
1512 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1513}
1514
1515EGLBoolean eglBindTexImage(
1516 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1517{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001518 SurfaceRef _s(surface);
1519 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1520
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521 if (!validate_display_surface(dpy, surface))
1522 return EGL_FALSE;
1523 egl_display_t const * const dp = get_display(dpy);
1524 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001525 if (s->cnx->egl.eglBindTexImage) {
1526 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001527 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528 }
1529 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1530}
1531
1532EGLBoolean eglReleaseTexImage(
1533 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1534{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001535 SurfaceRef _s(surface);
1536 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1537
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 if (!validate_display_surface(dpy, surface))
1539 return EGL_FALSE;
1540 egl_display_t const * const dp = get_display(dpy);
1541 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001542 if (s->cnx->egl.eglReleaseTexImage) {
1543 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001544 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 }
1546 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1547}
1548
1549EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1550{
1551 egl_display_t * const dp = get_display(dpy);
1552 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1553
1554 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001555 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 egl_connection_t* const cnx = &gEGLImpl[i];
1557 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001558 if (cnx->egl.eglSwapInterval) {
1559 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001560 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 res = EGL_FALSE;
1562 }
1563 }
1564 }
1565 }
1566 return res;
1567}
1568
1569
1570// ----------------------------------------------------------------------------
1571// EGL 1.2
1572// ----------------------------------------------------------------------------
1573
1574EGLBoolean eglWaitClient(void)
1575{
Mathias Agopian923c6612009-08-17 18:07:06 -07001576 // could be called before eglInitialize(), but we wouldn't have a context
1577 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 EGLBoolean res = EGL_TRUE;
1579 EGLContext ctx = getContext();
1580 if (ctx) {
1581 egl_context_t const * const c = get_context(ctx);
1582 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1583 if (uint32_t(c->impl)>=2)
1584 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1585 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1586 if (!cnx->dso)
1587 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001588 if (cnx->egl.eglWaitClient) {
1589 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001591 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592 }
1593 }
1594 return res;
1595}
1596
1597EGLBoolean eglBindAPI(EGLenum api)
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 // bind this API on all EGLs
1604 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001605 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 egl_connection_t* const cnx = &gEGLImpl[i];
1607 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001608 if (cnx->egl.eglBindAPI) {
1609 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610 res = EGL_FALSE;
1611 }
1612 }
1613 }
1614 }
1615 return res;
1616}
1617
1618EGLenum eglQueryAPI(void)
1619{
Mathias Agopian923c6612009-08-17 18:07:06 -07001620 if (egl_init_drivers() == EGL_FALSE) {
1621 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1622 }
1623
Mathias Agopian618fa102009-10-14 02:06:37 -07001624 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 egl_connection_t* const cnx = &gEGLImpl[i];
1626 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001627 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 // the first one we find is okay, because they all
1629 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001630 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631 }
1632 }
1633 }
1634 // or, it can only be OpenGL ES
1635 return EGL_OPENGL_ES_API;
1636}
1637
1638EGLBoolean eglReleaseThread(void)
1639{
Mathias Agopian618fa102009-10-14 02:06:37 -07001640 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 egl_connection_t* const cnx = &gEGLImpl[i];
1642 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001643 if (cnx->egl.eglReleaseThread) {
1644 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 }
1646 }
1647 }
1648 clearTLS();
1649 return EGL_TRUE;
1650}
1651
1652EGLSurface eglCreatePbufferFromClientBuffer(
1653 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1654 EGLConfig config, const EGLint *attrib_list)
1655{
1656 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001657 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001659 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1660 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001661 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1662 buftype, buffer,
1663 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 }
1665 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1666}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001667
1668// ----------------------------------------------------------------------------
1669// EGL_EGLEXT_VERSION 3
1670// ----------------------------------------------------------------------------
1671
1672EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1673 const EGLint *attrib_list)
1674{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001675 SurfaceRef _s(surface);
1676 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1677
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001678 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001679 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001680
1681 egl_display_t const * const dp = get_display(dpy);
1682 egl_surface_t const * const s = get_surface(surface);
1683
Mathias Agopian618fa102009-10-14 02:06:37 -07001684 if (s->cnx->egl.eglLockSurfaceKHR) {
1685 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001686 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001687 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001688 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001689}
1690
1691EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1692{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001693 SurfaceRef _s(surface);
1694 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1695
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001696 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001697 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001698
1699 egl_display_t const * const dp = get_display(dpy);
1700 egl_surface_t const * const s = get_surface(surface);
1701
Mathias Agopian618fa102009-10-14 02:06:37 -07001702 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1703 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001704 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001705 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001706 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001707}
1708
1709EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1710 EGLClientBuffer buffer, const EGLint *attrib_list)
1711{
1712 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001713 ContextRef _c(ctx);
1714 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001715 if (!validate_display_context(dpy, ctx))
1716 return EGL_NO_IMAGE_KHR;
1717 egl_display_t const * const dp = get_display(dpy);
1718 egl_context_t * const c = get_context(ctx);
1719 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001720 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001721 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001722 if (image == EGL_NO_IMAGE_KHR)
1723 return image;
1724
1725 egl_image_t* result = new egl_image_t(dpy, ctx);
1726 result->images[c->impl] = image;
1727 return (EGLImageKHR)result;
1728 } else {
1729 // EGL_NO_CONTEXT is a valid parameter
1730 egl_display_t const * const dp = get_display(dpy);
1731 if (dp == 0) {
1732 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1733 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001734
1735 /* Since we don't have a way to know which implementation to call,
1736 * we're calling all of them. If at least one of the implementation
1737 * succeeded, this is a success.
1738 */
1739
1740 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001741
Mathias Agopian618fa102009-10-14 02:06:37 -07001742 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001743 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001744 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001745 egl_connection_t* const cnx = &gEGLImpl[i];
1746 implImages[i] = EGL_NO_IMAGE_KHR;
1747 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001748 if (cnx->egl.eglCreateImageKHR) {
1749 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001750 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001751 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1752 success = true;
1753 }
1754 }
1755 }
1756 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001757
1758 if (!success) {
1759 // failure, if there was an error when we entered this function,
1760 // the error flag must not be updated.
1761 // Otherwise, the error is whatever happened in the implementation
1762 // that faulted.
1763 if (currentError != EGL_SUCCESS) {
1764 setError(currentError, EGL_NO_IMAGE_KHR);
1765 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001766 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001767 } else {
1768 // In case of success, we need to clear all error flags
1769 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07001770 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001771 // a "full" success (all implementation succeeded).
1772 eglGetError();
1773 }
1774
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001775 egl_image_t* result = new egl_image_t(dpy, ctx);
1776 memcpy(result->images, implImages, sizeof(implImages));
1777 return (EGLImageKHR)result;
1778 }
1779}
1780
1781EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1782{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001783 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001784 if (dp == 0) {
1785 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1786 }
1787
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001788 ImageRef _i(img);
1789 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001790
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001791 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001792 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001793 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001794 egl_connection_t* const cnx = &gEGLImpl[i];
1795 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1796 if (cnx->dso) {
Mathias Agopian77fbf8d2010-09-09 11:12:54 -07001797 if (cnx->egl.eglDestroyImageKHR) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001798 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001799 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001800 success = true;
1801 }
1802 }
1803 }
1804 }
1805 }
1806 if (!success)
1807 return EGL_FALSE;
1808
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001809 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001810
Mathias Agopian24e5f522009-08-12 21:18:15 -07001811 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001812}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001813
Mathias Agopianc291f5852010-08-27 16:48:56 -07001814// ----------------------------------------------------------------------------
1815// EGL_EGLEXT_VERSION 5
1816// ----------------------------------------------------------------------------
1817
1818
1819EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1820{
1821 EGLContext ctx = eglGetCurrentContext();
1822 ContextRef _c(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07001823 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
Mathias Agopianc291f5852010-08-27 16:48:56 -07001824 if (!validate_display_context(dpy, ctx))
Mathias Agopiana93b9572010-09-21 16:22:10 -07001825 return EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07001826 egl_display_t const * const dp = get_display(dpy);
1827 egl_context_t * const c = get_context(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07001828 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07001829 if (c->cnx->egl.eglCreateSyncKHR) {
1830 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
1831 dp->disp[c->impl].dpy, type, attrib_list);
Mathias Agopiana93b9572010-09-21 16:22:10 -07001832 if (sync == EGL_NO_SYNC_KHR)
Mathias Agopianc291f5852010-08-27 16:48:56 -07001833 return sync;
1834 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
1835 }
1836 return (EGLSyncKHR)result;
1837}
1838
1839EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1840{
1841 egl_display_t const * const dp = get_display(dpy);
1842 if (dp == 0) {
1843 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1844 }
1845
1846 SyncRef _s(sync);
1847 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1848 egl_sync_t* syncObject = get_sync(sync);
1849
1850 EGLContext ctx = syncObject->context;
1851 ContextRef _c(ctx);
1852 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1853 if (!validate_display_context(dpy, ctx))
1854 return EGL_FALSE;
1855
1856 egl_context_t * const c = get_context(ctx);
1857
1858 if (c->cnx->egl.eglDestroySyncKHR) {
1859 return c->cnx->egl.eglDestroySyncKHR(
1860 dp->disp[c->impl].dpy, syncObject->sync);
1861 }
1862
1863 return EGL_FALSE;
1864}
1865
1866EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
1867{
1868 egl_display_t const * const dp = get_display(dpy);
1869 if (dp == 0) {
1870 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1871 }
1872
1873 SyncRef _s(sync);
1874 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1875 egl_sync_t* syncObject = get_sync(sync);
1876
1877 EGLContext ctx = syncObject->context;
1878 ContextRef _c(ctx);
1879 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1880 if (!validate_display_context(dpy, ctx))
1881 return EGL_FALSE;
1882
1883 egl_context_t * const c = get_context(ctx);
1884
1885 if (c->cnx->egl.eglClientWaitSyncKHR) {
1886 return c->cnx->egl.eglClientWaitSyncKHR(
1887 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
1888 }
1889
1890 return EGL_FALSE;
1891}
1892
1893EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
1894{
1895 egl_display_t const * const dp = get_display(dpy);
1896 if (dp == 0) {
1897 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1898 }
1899
1900 SyncRef _s(sync);
1901 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1902 egl_sync_t* syncObject = get_sync(sync);
1903
1904 EGLContext ctx = syncObject->context;
1905 ContextRef _c(ctx);
1906 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1907 if (!validate_display_context(dpy, ctx))
1908 return EGL_FALSE;
1909
1910 egl_context_t * const c = get_context(ctx);
1911
1912 if (c->cnx->egl.eglGetSyncAttribKHR) {
1913 return c->cnx->egl.eglGetSyncAttribKHR(
1914 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
1915 }
1916
1917 return EGL_FALSE;
1918}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001919
1920// ----------------------------------------------------------------------------
1921// ANDROID extensions
1922// ----------------------------------------------------------------------------
1923
1924EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1925 EGLint left, EGLint top, EGLint width, EGLint height)
1926{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001927 SurfaceRef _s(draw);
1928 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1929
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001930 if (!validate_display_surface(dpy, draw))
1931 return EGL_FALSE;
1932 egl_display_t const * const dp = get_display(dpy);
1933 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001934 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
1935 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001936 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001937 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001938 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001939}