blob: 0437263008f20f99e78ddebe2db5d32f6da87c9a [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431static void 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 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438}
Mathias Agopiand274eae2009-07-31 16:21:17 -0700439
Mathias Agopian05c53112010-09-23 11:32:52 -0700440// Always return GL_INVALID_OPERATION from glGetError() when called from
441// a thread without a bound context.
442static GLenum gl_no_context_glGetError() {
443 return GL_INVALID_OPERATION;
444}
445
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800446static void early_egl_init(void)
447{
448#if !USE_FAST_TLS_KEY
449 pthread_key_create(&gGLWrapperKey, NULL);
450#endif
451 uint32_t addr = (uint32_t)((void*)gl_no_context);
452 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700453 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700455 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700456
457 gHooksNoContext.gl.glGetError = gl_no_context_glGetError;
458
Mathias Agopian618fa102009-10-14 02:06:37 -0700459 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460}
461
462static pthread_once_t once_control = PTHREAD_ONCE_INIT;
463static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
464
465
466static inline
467egl_display_t* get_display(EGLDisplay dpy)
468{
469 uintptr_t index = uintptr_t(dpy)-1U;
470 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
471}
472
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700473template<typename NATIVE, typename EGL>
474static inline NATIVE* egl_to_native_cast(EGL arg) {
475 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476}
477
478static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700479egl_surface_t* get_surface(EGLSurface surface) {
480 return egl_to_native_cast<egl_surface_t>(surface);
481}
482
483static inline
484egl_context_t* get_context(EGLContext context) {
485 return egl_to_native_cast<egl_context_t>(context);
486}
487
488static inline
489egl_image_t* get_image(EGLImageKHR image) {
490 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491}
492
493static egl_connection_t* validate_display_config(
494 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700495 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496{
497 dp = get_display(dpy);
498 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
499
Mathias Agopiancee79392010-07-26 21:14:59 -0700500 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800501 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
502 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700503 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504 if (cnx->dso == 0) {
505 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
506 }
507 return cnx;
508}
509
510static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
511{
512 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
513 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700514 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800515 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700516 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
518 return EGL_TRUE;
519}
520
521static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
522{
523 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
524 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700525 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700527 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528 return setError(EGL_BAD_SURFACE, EGL_FALSE);
529 return EGL_TRUE;
530}
531
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700532EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
533{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700534 ImageRef _i(image);
535 if (!_i.get()) return EGL_NO_IMAGE_KHR;
536
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700537 EGLContext context = getContext();
538 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
539 return EGL_NO_IMAGE_KHR;
540
541 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700542 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700543 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700545 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700546 return i->images[c->impl];
547}
548
Mathias Agopian923c6612009-08-17 18:07:06 -0700549// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700550
Mathias Agopian923c6612009-08-17 18:07:06 -0700551// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700552// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700553// egl_init_drivers_locked()
554//
555static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
556
557EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558{
559 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700560 // initialized by static ctor. should be set here.
561 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562 }
563
Mathias Agopiande586972009-05-28 17:39:03 -0700564 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700565 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700566
Mathias Agopian923c6612009-08-17 18:07:06 -0700567 // dynamically load all our EGL implementations for all displays
568 // and retrieve the corresponding EGLDisplay
569 // if that fails, don't use this driver.
570 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
571 egl_connection_t* cnx;
572 egl_display_t* d = &gDisplay[0];
573
574 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700576 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
577 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
578 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700579 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700580 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700581 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700582 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700583 if (dpy == EGL_NO_DISPLAY) {
584 loader.close(cnx->dso);
585 cnx->dso = NULL;
586 }
587 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588 }
589
590 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700591 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 char value[PROPERTY_VALUE_MAX];
593 property_get("debug.egl.hw", value, "1");
594 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700595 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
596 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
597 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700598 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700599 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700600 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700601 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700602 if (dpy == EGL_NO_DISPLAY) {
603 loader.close(cnx->dso);
604 cnx->dso = NULL;
605 }
606 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 } else {
608 LOGD("3D hardware acceleration is disabled");
609 }
610 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700611
Mathias Agopian923c6612009-08-17 18:07:06 -0700612 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
613 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 }
615
Mathias Agopian923c6612009-08-17 18:07:06 -0700616 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617}
618
Mathias Agopian923c6612009-08-17 18:07:06 -0700619EGLBoolean egl_init_drivers()
620{
621 EGLBoolean res;
622 pthread_mutex_lock(&gInitDriverMutex);
623 res = egl_init_drivers_locked();
624 pthread_mutex_unlock(&gInitDriverMutex);
625 return res;
626}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700627
628// ----------------------------------------------------------------------------
629}; // namespace android
630// ----------------------------------------------------------------------------
631
632using namespace android;
633
634EGLDisplay eglGetDisplay(NativeDisplayType display)
635{
Mathias Agopian923c6612009-08-17 18:07:06 -0700636 uint32_t index = uint32_t(display);
637 if (index >= NUM_DISPLAYS) {
638 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
639 }
640
641 if (egl_init_drivers() == EGL_FALSE) {
642 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
643 }
644
645 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
646 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700647}
648
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649// ----------------------------------------------------------------------------
650// Initialization
651// ----------------------------------------------------------------------------
652
653EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
654{
655 egl_display_t * const dp = get_display(dpy);
656 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
657
Mathias Agopian75bc2782010-02-05 16:17:01 -0800658 Mutex::Autolock _l(dp->lock);
659
660 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 if (major != NULL) *major = VERSION_MAJOR;
662 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700663 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 return EGL_TRUE;
665 }
666
Mathias Agopian618fa102009-10-14 02:06:37 -0700667 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668
669 // initialize each EGL and
670 // build our own extension string first, based on the extension we know
671 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700672 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673 egl_connection_t* const cnx = &gEGLImpl[i];
674 cnx->major = -1;
675 cnx->minor = -1;
676 if (!cnx->dso)
677 continue;
678
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700679#if defined(ADRENO130)
680#warning "Adreno-130 eglInitialize() workaround"
681 /*
682 * The ADRENO 130 driver returns a different EGLDisplay each time
683 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
684 * after eglTerminate() has been called, so that eglInitialize()
685 * cannot be called again. Therefore, we need to make sure to call
686 * eglGetDisplay() before calling eglInitialize();
687 */
688 if (i == IMPL_HARDWARE) {
689 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700690 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700691 }
692#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800693
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700694
695 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700696 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700698 // i, idpy, cnx->major, cnx->minor, cnx);
699
700 // display is now initialized
701 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702
703 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700704 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700705 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700706 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700707 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700708 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700709 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700710 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700711 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712
713 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700714 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700715 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716 }
717 }
718
719 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700720 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721 egl_connection_t* const cnx = &gEGLImpl[i];
722 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
723 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700724 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700725 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
726 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700727 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700728 dp->disp[i].dpy, dp->disp[i].config, n,
729 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 dp->numTotalConfigs += n;
732 res = EGL_TRUE;
733 }
734 }
735 }
736 }
737 }
738
739 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700740 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
741 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
742 egl_connection_t* const cnx = &gEGLImpl[i];
743 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
744 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
745 dp->configs[k].impl = i;
746 dp->configs[k].config = dp->disp[i].config[j];
747 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
748 // store the implementation's CONFIG_ID
749 cnx->egl.eglGetConfigAttrib(
750 dp->disp[i].dpy,
751 dp->disp[i].config[j],
752 EGL_CONFIG_ID,
753 &dp->configs[k].implConfigId);
754 k++;
755 }
756 }
757 }
758
759 // sort our configurations so we can do binary-searches
760 qsort( dp->configs,
761 dp->numTotalConfigs,
762 sizeof(egl_config_t), cmp_configs);
763
Mathias Agopian75bc2782010-02-05 16:17:01 -0800764 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 if (major != NULL) *major = VERSION_MAJOR;
766 if (minor != NULL) *minor = VERSION_MINOR;
767 return EGL_TRUE;
768 }
769 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
770}
771
772EGLBoolean eglTerminate(EGLDisplay dpy)
773{
Mathias Agopian923c6612009-08-17 18:07:06 -0700774 // NOTE: don't unload the drivers b/c some APIs can be called
775 // after eglTerminate() has been called. eglTerminate() only
776 // terminates an EGLDisplay, not a EGL itself.
777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 egl_display_t* const dp = get_display(dpy);
779 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800780
781 Mutex::Autolock _l(dp->lock);
782
783 if (dp->refs == 0) {
784 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
785 }
786
787 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700788 if (dp->refs > 1) {
789 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700791 }
Mathias Agopiande586972009-05-28 17:39:03 -0700792
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700794 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700796 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700797 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700798 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700799 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700800 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700801 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700802 free(dp->disp[i].config);
803
804 dp->disp[i].numConfigs = 0;
805 dp->disp[i].config = 0;
806 dp->disp[i].state = egl_display_t::TERMINATED;
807
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 res = EGL_TRUE;
809 }
810 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700811
812 // TODO: all egl_object_t should be marked for termination
813
Mathias Agopian75bc2782010-02-05 16:17:01 -0800814 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700816 delete [] dp->configs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 clearTLS();
818 return res;
819}
820
821// ----------------------------------------------------------------------------
822// configuration
823// ----------------------------------------------------------------------------
824
825EGLBoolean eglGetConfigs( EGLDisplay dpy,
826 EGLConfig *configs,
827 EGLint config_size, EGLint *num_config)
828{
829 egl_display_t const * const dp = get_display(dpy);
830 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
831
832 GLint numConfigs = dp->numTotalConfigs;
833 if (!configs) {
834 *num_config = numConfigs;
835 return EGL_TRUE;
836 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700837
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700839 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
840 *configs++ = EGLConfig(i);
841 config_size--;
842 n++;
843 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844
845 *num_config = n;
846 return EGL_TRUE;
847}
848
849EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
850 EGLConfig *configs, EGLint config_size,
851 EGLint *num_config)
852{
853 egl_display_t const * const dp = get_display(dpy);
854 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
855
Jack Palevich749c63d2009-03-25 15:12:17 -0700856 if (num_config==0) {
857 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858 }
859
860 EGLint n;
861 EGLBoolean res = EGL_FALSE;
862 *num_config = 0;
863
864
865 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -0700866 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 // to figure out both its size and if it contains an EGL_CONFIG_ID
868 // key. If so, the full array is copied and patched.
869 // NOTE: we assume that there can be only one occurrence
870 // of EGL_CONFIG_ID.
871
872 EGLint patch_index = -1;
873 GLint attr;
874 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -0700875 if (attrib_list) {
876 while ((attr=attrib_list[size]) != EGL_NONE) {
877 if (attr == EGL_CONFIG_ID)
878 patch_index = size;
879 size += 2;
880 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881 }
882 if (patch_index >= 0) {
883 size += 2; // we need copy the sentinel as well
884 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
885 if (new_list == 0)
886 return setError(EGL_BAD_ALLOC, EGL_FALSE);
887 memcpy(new_list, attrib_list, size*sizeof(EGLint));
888
889 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -0700890 bool found = false;
891 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -0700893 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
894 if (dp->configs[i].configId == configId) {
895 ourConfig = EGLConfig(i);
896 configId = dp->configs[i].implConfigId;
897 found = true;
898 break;
899 }
900 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901
Mathias Agopiancee79392010-07-26 21:14:59 -0700902 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
903 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 // and switch to the new list
905 attrib_list = const_cast<const EGLint *>(new_list);
906
907 // At this point, the only configuration that can match is
908 // dp->configs[i][index], however, we don't know if it would be
909 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -0700910 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911 // through all the EGLimpl[].
912 // We also know we can only get a single config back, and we know
913 // which one.
914
Mathias Agopian618fa102009-10-14 02:06:37 -0700915 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -0700916 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
917 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800918 if (res && n>0) {
919 // n has to be 0 or 1, by construction, and we already know
920 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -0700921 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700922 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -0700923 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 *num_config = 1;
925 }
926 }
927
928 free(const_cast<EGLint *>(attrib_list));
929 return res;
930 }
931
Mathias Agopiancee79392010-07-26 21:14:59 -0700932
Mathias Agopian618fa102009-10-14 02:06:37 -0700933 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 egl_connection_t* const cnx = &gEGLImpl[i];
935 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700936 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700937 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -0700938 if (configs) {
939 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -0700940 // internal EGLConfig format.
941 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -0700942 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700943 egl_config_t key(i, configs[j]);
944 intptr_t index = binarySearch<egl_config_t>(
945 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -0700946 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700947 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -0700948 } else {
949 return setError(EGL_BAD_CONFIG, EGL_FALSE);
950 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 }
Jack Palevich749c63d2009-03-25 15:12:17 -0700952 configs += n;
953 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800955 *num_config += n;
956 res = EGL_TRUE;
957 }
958 }
959 }
960 return res;
961}
962
963EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
964 EGLint attribute, EGLint *value)
965{
966 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700967 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968 if (!cnx) return EGL_FALSE;
969
970 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700971 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972 return EGL_TRUE;
973 }
Mathias Agopian618fa102009-10-14 02:06:37 -0700974 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -0700975 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
976 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977}
978
979// ----------------------------------------------------------------------------
980// surfaces
981// ----------------------------------------------------------------------------
982
983EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
984 NativeWindowType window,
985 const EGLint *attrib_list)
986{
987 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700988 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700990 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -0700991 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
992 dp->configs[intptr_t(config)].config, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700994 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
995 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 return s;
997 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 }
999 return EGL_NO_SURFACE;
1000}
1001
1002EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1003 NativePixmapType pixmap,
1004 const EGLint *attrib_list)
1005{
1006 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001007 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001009 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001010 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1011 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001013 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1014 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 return s;
1016 }
1017 }
1018 return EGL_NO_SURFACE;
1019}
1020
1021EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1022 const EGLint *attrib_list)
1023{
1024 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001025 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001027 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001028 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1029 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001031 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1032 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 return s;
1034 }
1035 }
1036 return EGL_NO_SURFACE;
1037}
1038
1039EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1040{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001041 SurfaceRef _s(surface);
1042 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001044 if (!validate_display_surface(dpy, surface))
1045 return EGL_FALSE;
1046 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001047
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001048 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001049 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001050 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001051 if (result == EGL_TRUE) {
1052 _s.terminate();
1053 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 return result;
1055}
1056
1057EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1058 EGLint attribute, EGLint *value)
1059{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001060 SurfaceRef _s(surface);
1061 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 if (!validate_display_surface(dpy, surface))
1064 return EGL_FALSE;
1065 egl_display_t const * const dp = get_display(dpy);
1066 egl_surface_t const * const s = get_surface(surface);
1067
Mathias Agopiancee79392010-07-26 21:14:59 -07001068 EGLBoolean result(EGL_TRUE);
1069 if (attribute == EGL_CONFIG_ID) {
1070 // We need to remap EGL_CONFIG_IDs
1071 *value = dp->configs[intptr_t(s->config)].configId;
1072 } else {
1073 result = s->cnx->egl.eglQuerySurface(
1074 dp->disp[s->impl].dpy, s->surface, attribute, value);
1075 }
1076
1077 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078}
1079
1080// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001081// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082// ----------------------------------------------------------------------------
1083
1084EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1085 EGLContext share_list, const EGLint *attrib_list)
1086{
1087 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001088 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001090 if (share_list != EGL_NO_CONTEXT) {
1091 egl_context_t* const c = get_context(share_list);
1092 share_list = c->context;
1093 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001094 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001095 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1096 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001097 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001099 // figure out if it's a GLESv1 or GLESv2
1100 int version = 0;
1101 if (attrib_list) {
1102 while (*attrib_list != EGL_NONE) {
1103 GLint attr = *attrib_list++;
1104 GLint value = *attrib_list++;
1105 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1106 if (value == 1) {
1107 version = GLESv1_INDEX;
1108 } else if (value == 2) {
1109 version = GLESv2_INDEX;
1110 }
1111 }
1112 };
1113 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001114 egl_context_t* c = new egl_context_t(dpy, context, config,
1115 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 return c;
1117 }
1118 }
1119 return EGL_NO_CONTEXT;
1120}
1121
1122EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1123{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001124 ContextRef _c(ctx);
1125 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 if (!validate_display_context(dpy, ctx))
1128 return EGL_FALSE;
1129 egl_display_t const * const dp = get_display(dpy);
1130 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001131 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001132 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001133 if (result == EGL_TRUE) {
1134 _c.terminate();
1135 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 return result;
1137}
1138
1139EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1140 EGLSurface read, EGLContext ctx)
1141{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001142 // get a reference to the object passed in
1143 ContextRef _c(ctx);
1144 SurfaceRef _d(draw);
1145 SurfaceRef _r(read);
1146
1147 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 egl_display_t const * const dp = get_display(dpy);
1149 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001150 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1151 // EGL_NO_CONTEXT is valid
1152 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 }
1154
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001155 // these are the underlying implementation's object
1156 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001157 EGLSurface impl_draw = EGL_NO_SURFACE;
1158 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001159
1160 // these are our objects structs passed in
1161 egl_context_t * c = NULL;
1162 egl_surface_t const * d = NULL;
1163 egl_surface_t const * r = NULL;
1164
1165 // these are the current objects structs
1166 egl_context_t * cur_c = get_context(getContext());
1167 egl_surface_t * cur_r = NULL;
1168 egl_surface_t * cur_d = NULL;
1169
1170 if (ctx != EGL_NO_CONTEXT) {
1171 c = get_context(ctx);
1172 cur_r = get_surface(c->read);
1173 cur_d = get_surface(c->draw);
1174 impl_ctx = c->context;
1175 } else {
1176 // no context given, use the implementation of the current context
1177 if (cur_c == NULL) {
1178 // no current context
1179 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001180 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1181 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001182 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001183 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001184 return EGL_TRUE;
1185 }
1186 }
1187
1188 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001190 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001191 // make sure the EGLContext and EGLSurface passed in are for
1192 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001193 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001195 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001197
1198 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001200 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001201 // make sure the EGLContext and EGLSurface passed in are for
1202 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001203 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001205 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001206 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001207
1208 EGLBoolean result;
1209
1210 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001211 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001212 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001213 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001214 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001215 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001216 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217
1218 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001219 // by construction, these are either 0 or valid (possibly terminated)
1220 // it should be impossible for these to be invalid
1221 ContextRef _cur_c(cur_c);
1222 SurfaceRef _cur_r(cur_r);
1223 SurfaceRef _cur_d(cur_d);
1224
1225 // cur_c has to be valid here (but could be terminated)
1226 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001227 setGlThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001228 setContext(ctx);
1229 _c.acquire();
1230 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001231 setGlThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001232 setContext(EGL_NO_CONTEXT);
1233 }
1234 _cur_c.release();
1235
1236 _r.acquire();
1237 _cur_r.release();
1238 if (c) c->read = read;
1239
1240 _d.acquire();
1241 _cur_d.release();
1242 if (c) c->draw = draw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243 }
1244 return result;
1245}
1246
1247
1248EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1249 EGLint attribute, EGLint *value)
1250{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001251 ContextRef _c(ctx);
1252 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 if (!validate_display_context(dpy, ctx))
1255 return EGL_FALSE;
1256
1257 egl_display_t const * const dp = get_display(dpy);
1258 egl_context_t * const c = get_context(ctx);
1259
Mathias Agopiancee79392010-07-26 21:14:59 -07001260 EGLBoolean result(EGL_TRUE);
1261 if (attribute == EGL_CONFIG_ID) {
1262 *value = dp->configs[intptr_t(c->config)].configId;
1263 } else {
1264 // We need to remap EGL_CONFIG_IDs
1265 result = c->cnx->egl.eglQueryContext(
1266 dp->disp[c->impl].dpy, c->context, attribute, value);
1267 }
1268
1269 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270}
1271
1272EGLContext eglGetCurrentContext(void)
1273{
Mathias Agopian923c6612009-08-17 18:07:06 -07001274 // could be called before eglInitialize(), but we wouldn't have a context
1275 // then, and this function would correctly return EGL_NO_CONTEXT.
1276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 EGLContext ctx = getContext();
1278 return ctx;
1279}
1280
1281EGLSurface eglGetCurrentSurface(EGLint readdraw)
1282{
Mathias Agopian923c6612009-08-17 18:07:06 -07001283 // could be called before eglInitialize(), but we wouldn't have a context
1284 // then, and this function would correctly return EGL_NO_SURFACE.
1285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286 EGLContext ctx = getContext();
1287 if (ctx) {
1288 egl_context_t const * const c = get_context(ctx);
1289 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1290 switch (readdraw) {
1291 case EGL_READ: return c->read;
1292 case EGL_DRAW: return c->draw;
1293 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1294 }
1295 }
1296 return EGL_NO_SURFACE;
1297}
1298
1299EGLDisplay eglGetCurrentDisplay(void)
1300{
Mathias Agopian923c6612009-08-17 18:07:06 -07001301 // could be called before eglInitialize(), but we wouldn't have a context
1302 // then, and this function would correctly return EGL_NO_DISPLAY.
1303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 EGLContext ctx = getContext();
1305 if (ctx) {
1306 egl_context_t const * const c = get_context(ctx);
1307 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1308 return c->dpy;
1309 }
1310 return EGL_NO_DISPLAY;
1311}
1312
1313EGLBoolean eglWaitGL(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 return GL_TRUE, which isn't wrong.
1317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 EGLBoolean res = EGL_TRUE;
1319 EGLContext ctx = getContext();
1320 if (ctx) {
1321 egl_context_t const * const c = get_context(ctx);
1322 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1323 if (uint32_t(c->impl)>=2)
1324 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1325 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1326 if (!cnx->dso)
1327 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001328 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 }
1330 return res;
1331}
1332
1333EGLBoolean eglWaitNative(EGLint engine)
1334{
Mathias Agopian923c6612009-08-17 18:07:06 -07001335 // could be called before eglInitialize(), but we wouldn't have a context
1336 // then, and this function would return GL_TRUE, which isn't wrong.
1337
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 EGLBoolean res = EGL_TRUE;
1339 EGLContext ctx = getContext();
1340 if (ctx) {
1341 egl_context_t const * const c = get_context(ctx);
1342 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1343 if (uint32_t(c->impl)>=2)
1344 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1345 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1346 if (!cnx->dso)
1347 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001348 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 }
1350 return res;
1351}
1352
1353EGLint eglGetError(void)
1354{
1355 EGLint result = EGL_SUCCESS;
Mathias Agopian02dafb52010-09-21 15:43:59 -07001356 EGLint err;
Mathias Agopian618fa102009-10-14 02:06:37 -07001357 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian02dafb52010-09-21 15:43:59 -07001358 err = EGL_SUCCESS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 egl_connection_t* const cnx = &gEGLImpl[i];
1360 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001361 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1363 result = err;
1364 }
Mathias Agopian02dafb52010-09-21 15:43:59 -07001365 err = getError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366 if (result == EGL_SUCCESS)
Mathias Agopian02dafb52010-09-21 15:43:59 -07001367 result = err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 return result;
1369}
1370
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001371__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001373 // eglGetProcAddress() could be the very first function called
1374 // in which case we must make sure we've initialized ourselves, this
1375 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001376
1377 if (egl_init_drivers() == EGL_FALSE) {
1378 setError(EGL_BAD_PARAMETER, NULL);
1379 return NULL;
1380 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 __eglMustCastToProperFunctionPointerType addr;
1383 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1384 if (addr) return addr;
1385
Mathias Agopian24035332010-08-02 17:34:32 -07001386 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1387 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388
Mathias Agopian24035332010-08-02 17:34:32 -07001389 /*
1390 * Since eglGetProcAddress() is not associated to anything, it needs
1391 * to return a function pointer that "works" regardless of what
1392 * the current context is.
1393 *
1394 * For this reason, we return a "forwarder", a small stub that takes
1395 * care of calling the function associated with the context
1396 * currently bound.
1397 *
1398 * We first look for extensions we've already resolved, if we're seeing
1399 * this extension for the first time, we go through all our
1400 * implementations and call eglGetProcAddress() and record the
1401 * result in the appropriate implementation hooks and return the
1402 * address of the forwarder corresponding to that hook set.
1403 *
1404 */
1405
1406 const String8 name(procname);
1407 addr = gGLExtentionMap.valueFor(name);
1408 const int slot = gGLExtentionSlot;
1409
1410 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1411 "no more slots for eglGetProcAddress(\"%s\")",
1412 procname);
1413
1414 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1415 bool found = false;
1416 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1417 egl_connection_t* const cnx = &gEGLImpl[i];
1418 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1419 found = true;
Mathias Agopian4a88b522010-08-13 12:19:04 -07001420 // Extensions are independent of the bound context
1421 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1422 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian24035332010-08-02 17:34:32 -07001423 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 }
1425 }
Mathias Agopian24035332010-08-02 17:34:32 -07001426 if (found) {
1427 addr = gExtensionForwarders[slot];
1428 gGLExtentionMap.add(name, addr);
1429 gGLExtentionSlot++;
1430 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432
Mathias Agopian24035332010-08-02 17:34:32 -07001433 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian24035332010-08-02 17:34:32 -07001434 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435}
1436
1437EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1438{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001439 SurfaceRef _s(draw);
1440 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1441
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 if (!validate_display_surface(dpy, draw))
1443 return EGL_FALSE;
1444 egl_display_t const * const dp = get_display(dpy);
1445 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001446 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447}
1448
1449EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1450 NativePixmapType target)
1451{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001452 SurfaceRef _s(surface);
1453 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 if (!validate_display_surface(dpy, surface))
1456 return EGL_FALSE;
1457 egl_display_t const * const dp = get_display(dpy);
1458 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001459 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001460 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461}
1462
1463const char* eglQueryString(EGLDisplay dpy, EGLint name)
1464{
1465 egl_display_t const * const dp = get_display(dpy);
1466 switch (name) {
1467 case EGL_VENDOR:
1468 return gVendorString;
1469 case EGL_VERSION:
1470 return gVersionString;
1471 case EGL_EXTENSIONS:
1472 return gExtensionString;
1473 case EGL_CLIENT_APIS:
1474 return gClientApiString;
1475 }
1476 return setError(EGL_BAD_PARAMETER, (const char *)0);
1477}
1478
1479
1480// ----------------------------------------------------------------------------
1481// EGL 1.1
1482// ----------------------------------------------------------------------------
1483
1484EGLBoolean eglSurfaceAttrib(
1485 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1486{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001487 SurfaceRef _s(surface);
1488 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1489
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 if (!validate_display_surface(dpy, surface))
1491 return EGL_FALSE;
1492 egl_display_t const * const dp = get_display(dpy);
1493 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001494 if (s->cnx->egl.eglSurfaceAttrib) {
1495 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001496 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 }
1498 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1499}
1500
1501EGLBoolean eglBindTexImage(
1502 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1503{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001504 SurfaceRef _s(surface);
1505 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1506
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 if (!validate_display_surface(dpy, surface))
1508 return EGL_FALSE;
1509 egl_display_t const * const dp = get_display(dpy);
1510 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001511 if (s->cnx->egl.eglBindTexImage) {
1512 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001513 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 }
1515 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1516}
1517
1518EGLBoolean eglReleaseTexImage(
1519 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1520{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001521 SurfaceRef _s(surface);
1522 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1523
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 if (!validate_display_surface(dpy, surface))
1525 return EGL_FALSE;
1526 egl_display_t const * const dp = get_display(dpy);
1527 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001528 if (s->cnx->egl.eglReleaseTexImage) {
1529 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001530 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531 }
1532 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1533}
1534
1535EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1536{
1537 egl_display_t * const dp = get_display(dpy);
1538 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1539
1540 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001541 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001542 egl_connection_t* const cnx = &gEGLImpl[i];
1543 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001544 if (cnx->egl.eglSwapInterval) {
1545 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001546 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547 res = EGL_FALSE;
1548 }
1549 }
1550 }
1551 }
1552 return res;
1553}
1554
1555
1556// ----------------------------------------------------------------------------
1557// EGL 1.2
1558// ----------------------------------------------------------------------------
1559
1560EGLBoolean eglWaitClient(void)
1561{
Mathias Agopian923c6612009-08-17 18:07:06 -07001562 // could be called before eglInitialize(), but we wouldn't have a context
1563 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564 EGLBoolean res = EGL_TRUE;
1565 EGLContext ctx = getContext();
1566 if (ctx) {
1567 egl_context_t const * const c = get_context(ctx);
1568 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1569 if (uint32_t(c->impl)>=2)
1570 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1571 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1572 if (!cnx->dso)
1573 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001574 if (cnx->egl.eglWaitClient) {
1575 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001577 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 }
1579 }
1580 return res;
1581}
1582
1583EGLBoolean eglBindAPI(EGLenum api)
1584{
Mathias Agopian923c6612009-08-17 18:07:06 -07001585 if (egl_init_drivers() == EGL_FALSE) {
1586 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1587 }
1588
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 // bind this API on all EGLs
1590 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001591 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001592 egl_connection_t* const cnx = &gEGLImpl[i];
1593 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001594 if (cnx->egl.eglBindAPI) {
1595 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596 res = EGL_FALSE;
1597 }
1598 }
1599 }
1600 }
1601 return res;
1602}
1603
1604EGLenum eglQueryAPI(void)
1605{
Mathias Agopian923c6612009-08-17 18:07:06 -07001606 if (egl_init_drivers() == EGL_FALSE) {
1607 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1608 }
1609
Mathias Agopian618fa102009-10-14 02:06:37 -07001610 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611 egl_connection_t* const cnx = &gEGLImpl[i];
1612 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001613 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614 // the first one we find is okay, because they all
1615 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001616 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 }
1618 }
1619 }
1620 // or, it can only be OpenGL ES
1621 return EGL_OPENGL_ES_API;
1622}
1623
1624EGLBoolean eglReleaseThread(void)
1625{
Mathias Agopian618fa102009-10-14 02:06:37 -07001626 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627 egl_connection_t* const cnx = &gEGLImpl[i];
1628 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001629 if (cnx->egl.eglReleaseThread) {
1630 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631 }
1632 }
1633 }
1634 clearTLS();
1635 return EGL_TRUE;
1636}
1637
1638EGLSurface eglCreatePbufferFromClientBuffer(
1639 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1640 EGLConfig config, const EGLint *attrib_list)
1641{
1642 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001643 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001645 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1646 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001647 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1648 buftype, buffer,
1649 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 }
1651 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1652}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001653
1654// ----------------------------------------------------------------------------
1655// EGL_EGLEXT_VERSION 3
1656// ----------------------------------------------------------------------------
1657
1658EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1659 const EGLint *attrib_list)
1660{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001661 SurfaceRef _s(surface);
1662 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1663
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001664 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001665 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001666
1667 egl_display_t const * const dp = get_display(dpy);
1668 egl_surface_t const * const s = get_surface(surface);
1669
Mathias Agopian618fa102009-10-14 02:06:37 -07001670 if (s->cnx->egl.eglLockSurfaceKHR) {
1671 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001672 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001673 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001674 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001675}
1676
1677EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1678{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001679 SurfaceRef _s(surface);
1680 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1681
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001682 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001683 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001684
1685 egl_display_t const * const dp = get_display(dpy);
1686 egl_surface_t const * const s = get_surface(surface);
1687
Mathias Agopian618fa102009-10-14 02:06:37 -07001688 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1689 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001690 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001691 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001692 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001693}
1694
1695EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1696 EGLClientBuffer buffer, const EGLint *attrib_list)
1697{
1698 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001699 ContextRef _c(ctx);
1700 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001701 if (!validate_display_context(dpy, ctx))
1702 return EGL_NO_IMAGE_KHR;
1703 egl_display_t const * const dp = get_display(dpy);
1704 egl_context_t * const c = get_context(ctx);
1705 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001706 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001707 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001708 if (image == EGL_NO_IMAGE_KHR)
1709 return image;
1710
1711 egl_image_t* result = new egl_image_t(dpy, ctx);
1712 result->images[c->impl] = image;
1713 return (EGLImageKHR)result;
1714 } else {
1715 // EGL_NO_CONTEXT is a valid parameter
1716 egl_display_t const * const dp = get_display(dpy);
1717 if (dp == 0) {
1718 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1719 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001720
1721 /* Since we don't have a way to know which implementation to call,
1722 * we're calling all of them. If at least one of the implementation
1723 * succeeded, this is a success.
1724 */
1725
1726 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001727
Mathias Agopian618fa102009-10-14 02:06:37 -07001728 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001729 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001730 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001731 egl_connection_t* const cnx = &gEGLImpl[i];
1732 implImages[i] = EGL_NO_IMAGE_KHR;
1733 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001734 if (cnx->egl.eglCreateImageKHR) {
1735 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001736 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001737 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1738 success = true;
1739 }
1740 }
1741 }
1742 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001743
1744 if (!success) {
1745 // failure, if there was an error when we entered this function,
1746 // the error flag must not be updated.
1747 // Otherwise, the error is whatever happened in the implementation
1748 // that faulted.
1749 if (currentError != EGL_SUCCESS) {
1750 setError(currentError, EGL_NO_IMAGE_KHR);
1751 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001752 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001753 } else {
1754 // In case of success, we need to clear all error flags
1755 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07001756 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001757 // a "full" success (all implementation succeeded).
1758 eglGetError();
1759 }
1760
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001761 egl_image_t* result = new egl_image_t(dpy, ctx);
1762 memcpy(result->images, implImages, sizeof(implImages));
1763 return (EGLImageKHR)result;
1764 }
1765}
1766
1767EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1768{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001769 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001770 if (dp == 0) {
1771 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1772 }
1773
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001774 ImageRef _i(img);
1775 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001776
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001777 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001778 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001779 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001780 egl_connection_t* const cnx = &gEGLImpl[i];
1781 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1782 if (cnx->dso) {
Mathias Agopian77fbf8d2010-09-09 11:12:54 -07001783 if (cnx->egl.eglDestroyImageKHR) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001784 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001785 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001786 success = true;
1787 }
1788 }
1789 }
1790 }
1791 }
1792 if (!success)
1793 return EGL_FALSE;
1794
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001795 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001796
Mathias Agopian24e5f522009-08-12 21:18:15 -07001797 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001798}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001799
1800
1801// ----------------------------------------------------------------------------
1802// ANDROID extensions
1803// ----------------------------------------------------------------------------
1804
1805EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1806 EGLint left, EGLint top, EGLint width, EGLint height)
1807{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001808 SurfaceRef _s(draw);
1809 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1810
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001811 if (!validate_display_surface(dpy, draw))
1812 return EGL_FALSE;
1813 egl_display_t const * const dp = get_display(dpy);
1814 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001815 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
1816 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001817 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001818 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001819 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001820}