blob: 665446a56a016912f03e54d7d8fbf75e068c22e5 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 ** Copyright 2007, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <ctype.h>
Mathias Agopiand8fb7b52009-05-17 18:50:16 -070018#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <string.h>
20#include <errno.h>
21#include <dlfcn.h>
22
23#include <sys/ioctl.h>
24
25#if HAVE_ANDROID_OS
26#include <linux/android_pmem.h>
27#endif
28
29#include <EGL/egl.h>
30#include <EGL/eglext.h>
31#include <GLES/gl.h>
32#include <GLES/glext.h>
33
34#include <cutils/log.h>
35#include <cutils/atomic.h>
36#include <cutils/properties.h>
37#include <cutils/memory.h>
38
Mathias Agopian9429e9c2009-08-21 02:18:25 -070039#include <utils/SortedVector.h>
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041#include "hooks.h"
42#include "egl_impl.h"
Mathias Agopiande586972009-05-28 17:39:03 -070043#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
46
47// ----------------------------------------------------------------------------
48namespace android {
49// ----------------------------------------------------------------------------
50
51#define VERSION_MAJOR 1
52#define VERSION_MINOR 4
53static char const * const gVendorString = "Android";
Mathias Agopian923c6612009-08-17 18:07:06 -070054static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056static char const * const gExtensionString =
57 "EGL_KHR_image "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -070058 "EGL_KHR_image_base "
59 "EGL_KHR_image_pixmap "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070061 "EGL_ANDROID_swap_rectangle "
Mathias Agopian8d2e83b2009-06-24 22:37:39 -070062 "EGL_ANDROID_get_render_buffer "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070063 ;
64
65// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopian9429e9c2009-08-21 02:18:25 -070067class egl_object_t {
68 static SortedVector<egl_object_t*> sObjects;
69 static Mutex sLock;
70
71 volatile int32_t terminated;
72 mutable volatile int32_t count;
73
74public:
75 egl_object_t() : terminated(0), count(1) {
76 Mutex::Autolock _l(sLock);
77 sObjects.add(this);
78 }
79
80 inline bool isAlive() const { return !terminated; }
81
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -070083 bool get() {
84 Mutex::Autolock _l(sLock);
85 if (egl_object_t::sObjects.indexOf(this) >= 0) {
86 android_atomic_inc(&count);
87 return true;
88 }
89 return false;
90 }
91
92 bool put() {
93 Mutex::Autolock _l(sLock);
94 if (android_atomic_dec(&count) == 1) {
95 sObjects.remove(this);
96 return true;
97 }
98 return false;
99 }
100
101public:
102 template <typename N, typename T>
103 struct LocalRef {
104 N* ref;
105 LocalRef(T o) : ref(0) {
106 N* native = reinterpret_cast<N*>(o);
107 if (o && native->get()) {
108 ref = native;
109 }
110 }
111 ~LocalRef() {
112 if (ref && ref->put()) {
113 delete ref;
114 }
115 }
116 inline N* get() {
117 return ref;
118 }
119 void acquire() const {
120 if (ref) {
121 android_atomic_inc(&ref->count);
122 }
123 }
124 void release() const {
125 if (ref) {
126 int32_t c = android_atomic_dec(&ref->count);
127 // ref->count cannot be 1 prior atomic_dec because we have
128 // a reference, and if we have one, it means there was
129 // already one before us.
130 LOGE_IF(c==1, "refcount is now 0 in release()");
131 }
132 }
133 void terminate() {
134 if (ref) {
135 ref->terminated = 1;
136 release();
137 }
138 }
139 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140};
141
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700142SortedVector<egl_object_t*> egl_object_t::sObjects;
143Mutex egl_object_t::sLock;
144
Mathias Agopiancee79392010-07-26 21:14:59 -0700145
146struct egl_config_t {
147 egl_config_t() {}
148 egl_config_t(int impl, EGLConfig config)
149 : impl(impl), config(config), configId(0), implConfigId(0) { }
150 int impl; // the implementation this config is for
151 EGLConfig config; // the implementation's EGLConfig
152 EGLint configId; // our CONFIG_ID
153 EGLint implConfigId; // the implementation's CONFIG_ID
154 inline bool operator < (const egl_config_t& rhs) const {
155 if (impl < rhs.impl) return true;
156 if (impl > rhs.impl) return false;
157 return config < rhs.config;
158 }
159};
160
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700161struct egl_display_t {
162 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
163
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 struct strings_t {
165 char const * vendor;
166 char const * version;
167 char const * clientApi;
168 char const * extensions;
169 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700170
171 struct DisplayImpl {
172 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
173 state(NOT_INITIALIZED), numConfigs(0) { }
174 EGLDisplay dpy;
175 EGLConfig* config;
176 EGLint state;
177 EGLint numConfigs;
178 strings_t queryString;
179 };
180
Mathias Agopiancee79392010-07-26 21:14:59 -0700181 uint32_t magic;
182 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
183 EGLint numTotalConfigs;
184 egl_config_t* configs;
185 uint32_t refs;
186 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700187
Mathias Agopiancee79392010-07-26 21:14:59 -0700188 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700189 ~egl_display_t() { magic = 0; }
190 inline bool isValid() const { return magic == '_dpy'; }
191 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192};
193
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700194struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700196 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
197
Mathias Agopiancee79392010-07-26 21:14:59 -0700198 egl_surface_t(EGLDisplay dpy, EGLSurface surface, EGLConfig config,
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199 int impl, egl_connection_t const* cnx)
Mathias Agopiancee79392010-07-26 21:14:59 -0700200 : dpy(dpy), surface(surface), config(config), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 }
202 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 }
204 EGLDisplay dpy;
205 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700206 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 int impl;
208 egl_connection_t const* cnx;
209};
210
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700211struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700213 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
214
Mathias Agopiancee79392010-07-26 21:14:59 -0700215 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian618fa102009-10-14 02:06:37 -0700216 int impl, egl_connection_t const* cnx, int version)
217 : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx),
218 version(version)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 {
220 }
221 EGLDisplay dpy;
222 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700223 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 EGLSurface read;
225 EGLSurface draw;
226 int impl;
227 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700228 int version;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229};
230
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700231struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700232{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700233 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
234
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700235 egl_image_t(EGLDisplay dpy, EGLContext context)
236 : dpy(dpy), context(context)
237 {
238 memset(images, 0, sizeof(images));
239 }
240 EGLDisplay dpy;
241 EGLConfig context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700242 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243};
244
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700245typedef egl_surface_t::Ref SurfaceRef;
246typedef egl_context_t::Ref ContextRef;
247typedef egl_image_t::Ref ImageRef;
248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249struct tls_t
250{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700251 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 EGLint error;
253 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700254 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255};
256
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
258// ----------------------------------------------------------------------------
259
Mathias Agopianbf41b112010-04-09 13:37:34 -0700260static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261static egl_display_t gDisplay[NUM_DISPLAYS];
262static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
263static pthread_key_t gEGLThreadLocalStorageKey = -1;
264
265// ----------------------------------------------------------------------------
266
Mathias Agopian618fa102009-10-14 02:06:37 -0700267EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
268EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700269EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270
271// ----------------------------------------------------------------------------
272
273static __attribute__((noinline))
274const char *egl_strerror(EGLint err)
275{
276 switch (err){
277 case EGL_SUCCESS: return "EGL_SUCCESS";
278 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
279 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
280 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
281 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
282 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
283 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
284 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
285 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
286 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
287 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
288 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
289 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
290 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
291 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
292 default: return "UNKNOWN";
293 }
294}
295
296static __attribute__((noinline))
297void clearTLS() {
298 if (gEGLThreadLocalStorageKey != -1) {
299 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
300 if (tls) {
301 delete tls;
302 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
303 }
304 }
305}
306
307static tls_t* getTLS()
308{
309 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
310 if (tls == 0) {
311 tls = new tls_t;
312 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
313 }
314 return tls;
315}
316
317template<typename T>
318static __attribute__((noinline))
319T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
320 if (gEGLThreadLocalStorageKey == -1) {
321 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
322 if (gEGLThreadLocalStorageKey == -1)
323 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
324 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
325 }
326 tls_t* tls = getTLS();
327 if (tls->error != error) {
328 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
329 tls->error = error;
330 }
331 return returnValue;
332}
333
334static __attribute__((noinline))
335GLint getError() {
336 if (gEGLThreadLocalStorageKey == -1)
337 return EGL_SUCCESS;
338 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
339 if (!tls) return EGL_SUCCESS;
340 GLint error = tls->error;
341 tls->error = EGL_SUCCESS;
342 return error;
343}
344
345static __attribute__((noinline))
346void setContext(EGLContext ctx) {
347 if (gEGLThreadLocalStorageKey == -1) {
348 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
349 if (gEGLThreadLocalStorageKey == -1)
350 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
351 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
352 }
353 tls_t* tls = getTLS();
354 tls->ctx = ctx;
355}
356
357static __attribute__((noinline))
358EGLContext getContext() {
359 if (gEGLThreadLocalStorageKey == -1)
360 return EGL_NO_CONTEXT;
361 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
362 if (!tls) return EGL_NO_CONTEXT;
363 return tls->ctx;
364}
365
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366/*****************************************************************************/
367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368template<typename T>
369static __attribute__((noinline))
370int binarySearch(
371 T const sortedArray[], int first, int last, T key)
372{
373 while (first <= last) {
374 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700375 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376 first = mid + 1;
377 } else if (key < sortedArray[mid]) {
378 last = mid - 1;
379 } else {
380 return mid;
381 }
382 }
383 return -1;
384}
385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386static int cmp_configs(const void* a, const void *b)
387{
Mathias Agopiancee79392010-07-26 21:14:59 -0700388 const egl_config_t& c0 = *(egl_config_t const *)a;
389 const egl_config_t& c1 = *(egl_config_t const *)b;
390 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391}
392
393struct extention_map_t {
394 const char* name;
395 __eglMustCastToProperFunctionPointerType address;
396};
397
398static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700399 { "eglLockSurfaceKHR",
400 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
401 { "eglUnlockSurfaceKHR",
402 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
403 { "eglCreateImageKHR",
404 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
405 { "eglDestroyImageKHR",
406 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700407 { "eglSetSwapRectangleANDROID",
408 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
409 { "eglGetRenderBufferANDROID",
410 (__eglMustCastToProperFunctionPointerType)&eglGetRenderBufferANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800411};
412
413static extention_map_t gGLExtentionMap[MAX_NUMBER_OF_GL_EXTENSIONS];
414
415static void(*findProcAddress(const char* name,
416 const extention_map_t* map, size_t n))()
417{
418 for (uint32_t i=0 ; i<n ; i++) {
419 if (!strcmp(name, map[i].name)) {
420 return map[i].address;
421 }
422 }
423 return NULL;
424}
425
426// ----------------------------------------------------------------------------
427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428static void gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700429 tls_t* tls = getTLS();
430 if (tls->logCallWithNoContext == EGL_TRUE) {
431 tls->logCallWithNoContext = EGL_FALSE;
432 LOGE("call to OpenGL ES API with no current context "
433 "(logged once per thread)");
434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435}
Mathias Agopiand274eae2009-07-31 16:21:17 -0700436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437static void early_egl_init(void)
438{
439#if !USE_FAST_TLS_KEY
440 pthread_key_create(&gGLWrapperKey, NULL);
441#endif
442 uint32_t addr = (uint32_t)((void*)gl_no_context);
443 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700444 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700446 sizeof(gHooksNoContext));
447 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448}
449
450static pthread_once_t once_control = PTHREAD_ONCE_INIT;
451static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
452
453
454static inline
455egl_display_t* get_display(EGLDisplay dpy)
456{
457 uintptr_t index = uintptr_t(dpy)-1U;
458 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
459}
460
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700461template<typename NATIVE, typename EGL>
462static inline NATIVE* egl_to_native_cast(EGL arg) {
463 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464}
465
466static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700467egl_surface_t* get_surface(EGLSurface surface) {
468 return egl_to_native_cast<egl_surface_t>(surface);
469}
470
471static inline
472egl_context_t* get_context(EGLContext context) {
473 return egl_to_native_cast<egl_context_t>(context);
474}
475
476static inline
477egl_image_t* get_image(EGLImageKHR image) {
478 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479}
480
481static egl_connection_t* validate_display_config(
482 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700483 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800484{
485 dp = get_display(dpy);
486 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
487
Mathias Agopiancee79392010-07-26 21:14:59 -0700488 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
490 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700491 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492 if (cnx->dso == 0) {
493 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
494 }
495 return cnx;
496}
497
498static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
499{
500 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
501 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700502 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700504 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
506 return EGL_TRUE;
507}
508
509static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
510{
511 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
512 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700513 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700515 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516 return setError(EGL_BAD_SURFACE, EGL_FALSE);
517 return EGL_TRUE;
518}
519
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700520EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
521{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700522 ImageRef _i(image);
523 if (!_i.get()) return EGL_NO_IMAGE_KHR;
524
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700525 EGLContext context = getContext();
526 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
527 return EGL_NO_IMAGE_KHR;
528
529 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700530 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700531 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700533 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700534 return i->images[c->impl];
535}
536
Mathias Agopian923c6612009-08-17 18:07:06 -0700537// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700538
Mathias Agopian923c6612009-08-17 18:07:06 -0700539// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700540// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700541// egl_init_drivers_locked()
542//
543static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
544
545EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546{
547 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700548 // initialized by static ctor. should be set here.
549 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800550 }
551
Mathias Agopiande586972009-05-28 17:39:03 -0700552 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700553 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700554
Mathias Agopian923c6612009-08-17 18:07:06 -0700555 // dynamically load all our EGL implementations for all displays
556 // and retrieve the corresponding EGLDisplay
557 // if that fails, don't use this driver.
558 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
559 egl_connection_t* cnx;
560 egl_display_t* d = &gDisplay[0];
561
562 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700564 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
565 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
566 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700567 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700568 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700569 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700570 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700571 if (dpy == EGL_NO_DISPLAY) {
572 loader.close(cnx->dso);
573 cnx->dso = NULL;
574 }
575 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 }
577
578 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700579 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580 char value[PROPERTY_VALUE_MAX];
581 property_get("debug.egl.hw", value, "1");
582 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700583 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
584 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
585 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700586 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700587 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700588 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700589 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700590 if (dpy == EGL_NO_DISPLAY) {
591 loader.close(cnx->dso);
592 cnx->dso = NULL;
593 }
594 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595 } else {
596 LOGD("3D hardware acceleration is disabled");
597 }
598 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700599
Mathias Agopian923c6612009-08-17 18:07:06 -0700600 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
601 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602 }
603
Mathias Agopian923c6612009-08-17 18:07:06 -0700604 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800605}
606
Mathias Agopian923c6612009-08-17 18:07:06 -0700607EGLBoolean egl_init_drivers()
608{
609 EGLBoolean res;
610 pthread_mutex_lock(&gInitDriverMutex);
611 res = egl_init_drivers_locked();
612 pthread_mutex_unlock(&gInitDriverMutex);
613 return res;
614}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700615
616// ----------------------------------------------------------------------------
617}; // namespace android
618// ----------------------------------------------------------------------------
619
620using namespace android;
621
622EGLDisplay eglGetDisplay(NativeDisplayType display)
623{
Mathias Agopian923c6612009-08-17 18:07:06 -0700624 uint32_t index = uint32_t(display);
625 if (index >= NUM_DISPLAYS) {
626 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
627 }
628
629 if (egl_init_drivers() == EGL_FALSE) {
630 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
631 }
632
633 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
634 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700635}
636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637// ----------------------------------------------------------------------------
638// Initialization
639// ----------------------------------------------------------------------------
640
641EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
642{
643 egl_display_t * const dp = get_display(dpy);
644 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
645
Mathias Agopian75bc2782010-02-05 16:17:01 -0800646 Mutex::Autolock _l(dp->lock);
647
648 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649 if (major != NULL) *major = VERSION_MAJOR;
650 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700651 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 return EGL_TRUE;
653 }
654
Mathias Agopian618fa102009-10-14 02:06:37 -0700655 setGlThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656
657 // initialize each EGL and
658 // build our own extension string first, based on the extension we know
659 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700660 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 egl_connection_t* const cnx = &gEGLImpl[i];
662 cnx->major = -1;
663 cnx->minor = -1;
664 if (!cnx->dso)
665 continue;
666
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700667#if defined(ADRENO130)
668#warning "Adreno-130 eglInitialize() workaround"
669 /*
670 * The ADRENO 130 driver returns a different EGLDisplay each time
671 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
672 * after eglTerminate() has been called, so that eglInitialize()
673 * cannot be called again. Therefore, we need to make sure to call
674 * eglGetDisplay() before calling eglInitialize();
675 */
676 if (i == IMPL_HARDWARE) {
677 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700678 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700679 }
680#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700682
683 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700684 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700686 // i, idpy, cnx->major, cnx->minor, cnx);
687
688 // display is now initialized
689 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800690
691 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700692 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700693 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700694 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700695 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700696 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700697 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700698 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700699 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800700
701 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700702 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700703 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704 }
705 }
706
707 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700708 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 egl_connection_t* const cnx = &gEGLImpl[i];
710 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
711 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700712 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700713 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
714 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700715 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700716 dp->disp[i].dpy, dp->disp[i].config, n,
717 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719 dp->numTotalConfigs += n;
720 res = EGL_TRUE;
721 }
722 }
723 }
724 }
725 }
726
727 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700728 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
729 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
730 egl_connection_t* const cnx = &gEGLImpl[i];
731 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
732 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
733 dp->configs[k].impl = i;
734 dp->configs[k].config = dp->disp[i].config[j];
735 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
736 // store the implementation's CONFIG_ID
737 cnx->egl.eglGetConfigAttrib(
738 dp->disp[i].dpy,
739 dp->disp[i].config[j],
740 EGL_CONFIG_ID,
741 &dp->configs[k].implConfigId);
742 k++;
743 }
744 }
745 }
746
747 // sort our configurations so we can do binary-searches
748 qsort( dp->configs,
749 dp->numTotalConfigs,
750 sizeof(egl_config_t), cmp_configs);
751
Mathias Agopian75bc2782010-02-05 16:17:01 -0800752 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753 if (major != NULL) *major = VERSION_MAJOR;
754 if (minor != NULL) *minor = VERSION_MINOR;
755 return EGL_TRUE;
756 }
757 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
758}
759
760EGLBoolean eglTerminate(EGLDisplay dpy)
761{
Mathias Agopian923c6612009-08-17 18:07:06 -0700762 // NOTE: don't unload the drivers b/c some APIs can be called
763 // after eglTerminate() has been called. eglTerminate() only
764 // terminates an EGLDisplay, not a EGL itself.
765
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800766 egl_display_t* const dp = get_display(dpy);
767 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800768
769 Mutex::Autolock _l(dp->lock);
770
771 if (dp->refs == 0) {
772 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
773 }
774
775 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700776 if (dp->refs > 1) {
777 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700779 }
Mathias Agopiande586972009-05-28 17:39:03 -0700780
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700782 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800783 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700784 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700785 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700786 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700787 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700788 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700789 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700790 free(dp->disp[i].config);
791
792 dp->disp[i].numConfigs = 0;
793 dp->disp[i].config = 0;
794 dp->disp[i].state = egl_display_t::TERMINATED;
795
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800796 res = EGL_TRUE;
797 }
798 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700799
800 // TODO: all egl_object_t should be marked for termination
801
Mathias Agopian75bc2782010-02-05 16:17:01 -0800802 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800803 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700804 delete [] dp->configs;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800805 clearTLS();
806 return res;
807}
808
809// ----------------------------------------------------------------------------
810// configuration
811// ----------------------------------------------------------------------------
812
813EGLBoolean eglGetConfigs( EGLDisplay dpy,
814 EGLConfig *configs,
815 EGLint config_size, EGLint *num_config)
816{
817 egl_display_t const * const dp = get_display(dpy);
818 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
819
820 GLint numConfigs = dp->numTotalConfigs;
821 if (!configs) {
822 *num_config = numConfigs;
823 return EGL_TRUE;
824 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700825
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700827 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
828 *configs++ = EGLConfig(i);
829 config_size--;
830 n++;
831 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800832
833 *num_config = n;
834 return EGL_TRUE;
835}
836
837EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
838 EGLConfig *configs, EGLint config_size,
839 EGLint *num_config)
840{
841 egl_display_t const * const dp = get_display(dpy);
842 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
843
Jack Palevich749c63d2009-03-25 15:12:17 -0700844 if (num_config==0) {
845 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 }
847
848 EGLint n;
849 EGLBoolean res = EGL_FALSE;
850 *num_config = 0;
851
852
853 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -0700854 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 // to figure out both its size and if it contains an EGL_CONFIG_ID
856 // key. If so, the full array is copied and patched.
857 // NOTE: we assume that there can be only one occurrence
858 // of EGL_CONFIG_ID.
859
860 EGLint patch_index = -1;
861 GLint attr;
862 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -0700863 if (attrib_list) {
864 while ((attr=attrib_list[size]) != EGL_NONE) {
865 if (attr == EGL_CONFIG_ID)
866 patch_index = size;
867 size += 2;
868 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869 }
870 if (patch_index >= 0) {
871 size += 2; // we need copy the sentinel as well
872 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
873 if (new_list == 0)
874 return setError(EGL_BAD_ALLOC, EGL_FALSE);
875 memcpy(new_list, attrib_list, size*sizeof(EGLint));
876
877 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -0700878 bool found = false;
879 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800880 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -0700881 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
882 if (dp->configs[i].configId == configId) {
883 ourConfig = EGLConfig(i);
884 configId = dp->configs[i].implConfigId;
885 found = true;
886 break;
887 }
888 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800889
Mathias Agopiancee79392010-07-26 21:14:59 -0700890 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
891 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 // and switch to the new list
893 attrib_list = const_cast<const EGLint *>(new_list);
894
895 // At this point, the only configuration that can match is
896 // dp->configs[i][index], however, we don't know if it would be
897 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -0700898 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899 // through all the EGLimpl[].
900 // We also know we can only get a single config back, and we know
901 // which one.
902
Mathias Agopian618fa102009-10-14 02:06:37 -0700903 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -0700904 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
905 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 if (res && n>0) {
907 // n has to be 0 or 1, by construction, and we already know
908 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -0700909 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700910 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -0700911 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912 *num_config = 1;
913 }
914 }
915
916 free(const_cast<EGLint *>(attrib_list));
917 return res;
918 }
919
Mathias Agopiancee79392010-07-26 21:14:59 -0700920
Mathias Agopian618fa102009-10-14 02:06:37 -0700921 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 egl_connection_t* const cnx = &gEGLImpl[i];
923 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700924 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700925 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -0700926 if (configs) {
927 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -0700928 // internal EGLConfig format.
929 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -0700930 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700931 egl_config_t key(i, configs[j]);
932 intptr_t index = binarySearch<egl_config_t>(
933 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -0700934 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700935 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -0700936 } else {
937 return setError(EGL_BAD_CONFIG, EGL_FALSE);
938 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 }
Jack Palevich749c63d2009-03-25 15:12:17 -0700940 configs += n;
941 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 *num_config += n;
944 res = EGL_TRUE;
945 }
946 }
947 }
948 return res;
949}
950
951EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
952 EGLint attribute, EGLint *value)
953{
954 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700955 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956 if (!cnx) return EGL_FALSE;
957
958 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700959 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 return EGL_TRUE;
961 }
Mathias Agopian618fa102009-10-14 02:06:37 -0700962 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -0700963 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
964 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965}
966
967// ----------------------------------------------------------------------------
968// surfaces
969// ----------------------------------------------------------------------------
970
971EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
972 NativeWindowType window,
973 const EGLint *attrib_list)
974{
975 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700976 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700978 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -0700979 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
980 dp->configs[intptr_t(config)].config, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700982 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
983 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 return s;
985 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986 }
987 return EGL_NO_SURFACE;
988}
989
990EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
991 NativePixmapType pixmap,
992 const EGLint *attrib_list)
993{
994 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700995 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700997 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -0700998 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
999 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001001 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1002 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003 return s;
1004 }
1005 }
1006 return EGL_NO_SURFACE;
1007}
1008
1009EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1010 const EGLint *attrib_list)
1011{
1012 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001013 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001015 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001016 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1017 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 if (surface != EGL_NO_SURFACE) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001019 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1020 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 return s;
1022 }
1023 }
1024 return EGL_NO_SURFACE;
1025}
1026
1027EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1028{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001029 SurfaceRef _s(surface);
1030 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1031
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 if (!validate_display_surface(dpy, surface))
1033 return EGL_FALSE;
1034 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001036 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001037 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001038 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001039 if (result == EGL_TRUE) {
1040 _s.terminate();
1041 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 return result;
1043}
1044
1045EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1046 EGLint attribute, EGLint *value)
1047{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001048 SurfaceRef _s(surface);
1049 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 if (!validate_display_surface(dpy, surface))
1052 return EGL_FALSE;
1053 egl_display_t const * const dp = get_display(dpy);
1054 egl_surface_t const * const s = get_surface(surface);
1055
Mathias Agopiancee79392010-07-26 21:14:59 -07001056 EGLBoolean result(EGL_TRUE);
1057 if (attribute == EGL_CONFIG_ID) {
1058 // We need to remap EGL_CONFIG_IDs
1059 *value = dp->configs[intptr_t(s->config)].configId;
1060 } else {
1061 result = s->cnx->egl.eglQuerySurface(
1062 dp->disp[s->impl].dpy, s->surface, attribute, value);
1063 }
1064
1065 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066}
1067
1068// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001069// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070// ----------------------------------------------------------------------------
1071
1072EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1073 EGLContext share_list, const EGLint *attrib_list)
1074{
1075 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001076 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001078 if (share_list != EGL_NO_CONTEXT) {
1079 egl_context_t* const c = get_context(share_list);
1080 share_list = c->context;
1081 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001082 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001083 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1084 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001085 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001087 // figure out if it's a GLESv1 or GLESv2
1088 int version = 0;
1089 if (attrib_list) {
1090 while (*attrib_list != EGL_NONE) {
1091 GLint attr = *attrib_list++;
1092 GLint value = *attrib_list++;
1093 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1094 if (value == 1) {
1095 version = GLESv1_INDEX;
1096 } else if (value == 2) {
1097 version = GLESv2_INDEX;
1098 }
1099 }
1100 };
1101 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001102 egl_context_t* c = new egl_context_t(dpy, context, config,
1103 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001104 return c;
1105 }
1106 }
1107 return EGL_NO_CONTEXT;
1108}
1109
1110EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1111{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001112 ContextRef _c(ctx);
1113 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 if (!validate_display_context(dpy, ctx))
1116 return EGL_FALSE;
1117 egl_display_t const * const dp = get_display(dpy);
1118 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001119 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001120 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001121 if (result == EGL_TRUE) {
1122 _c.terminate();
1123 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 return result;
1125}
1126
1127EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1128 EGLSurface read, EGLContext ctx)
1129{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001130 // get a reference to the object passed in
1131 ContextRef _c(ctx);
1132 SurfaceRef _d(draw);
1133 SurfaceRef _r(read);
1134
1135 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136 egl_display_t const * const dp = get_display(dpy);
1137 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001138 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1139 // EGL_NO_CONTEXT is valid
1140 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 }
1142
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001143 // these are the underlying implementation's object
1144 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001145 EGLSurface impl_draw = EGL_NO_SURFACE;
1146 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001147
1148 // these are our objects structs passed in
1149 egl_context_t * c = NULL;
1150 egl_surface_t const * d = NULL;
1151 egl_surface_t const * r = NULL;
1152
1153 // these are the current objects structs
1154 egl_context_t * cur_c = get_context(getContext());
1155 egl_surface_t * cur_r = NULL;
1156 egl_surface_t * cur_d = NULL;
1157
1158 if (ctx != EGL_NO_CONTEXT) {
1159 c = get_context(ctx);
1160 cur_r = get_surface(c->read);
1161 cur_d = get_surface(c->draw);
1162 impl_ctx = c->context;
1163 } else {
1164 // no context given, use the implementation of the current context
1165 if (cur_c == NULL) {
1166 // no current context
1167 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001168 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1169 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001170 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001171 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001172 return EGL_TRUE;
1173 }
1174 }
1175
1176 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001178 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001179 // make sure the EGLContext and EGLSurface passed in are for
1180 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001181 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001183 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001185
1186 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001188 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001189 // make sure the EGLContext and EGLSurface passed in are for
1190 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001191 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001193 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001195
1196 EGLBoolean result;
1197
1198 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001199 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001200 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001201 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001202 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001203 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001204 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205
1206 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001207 // by construction, these are either 0 or valid (possibly terminated)
1208 // it should be impossible for these to be invalid
1209 ContextRef _cur_c(cur_c);
1210 SurfaceRef _cur_r(cur_r);
1211 SurfaceRef _cur_d(cur_d);
1212
1213 // cur_c has to be valid here (but could be terminated)
1214 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001215 setGlThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001216 setContext(ctx);
1217 _c.acquire();
1218 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001219 setGlThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001220 setContext(EGL_NO_CONTEXT);
1221 }
1222 _cur_c.release();
1223
1224 _r.acquire();
1225 _cur_r.release();
1226 if (c) c->read = read;
1227
1228 _d.acquire();
1229 _cur_d.release();
1230 if (c) c->draw = draw;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 }
1232 return result;
1233}
1234
1235
1236EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1237 EGLint attribute, EGLint *value)
1238{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001239 ContextRef _c(ctx);
1240 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 if (!validate_display_context(dpy, ctx))
1243 return EGL_FALSE;
1244
1245 egl_display_t const * const dp = get_display(dpy);
1246 egl_context_t * const c = get_context(ctx);
1247
Mathias Agopiancee79392010-07-26 21:14:59 -07001248 EGLBoolean result(EGL_TRUE);
1249 if (attribute == EGL_CONFIG_ID) {
1250 *value = dp->configs[intptr_t(c->config)].configId;
1251 } else {
1252 // We need to remap EGL_CONFIG_IDs
1253 result = c->cnx->egl.eglQueryContext(
1254 dp->disp[c->impl].dpy, c->context, attribute, value);
1255 }
1256
1257 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258}
1259
1260EGLContext eglGetCurrentContext(void)
1261{
Mathias Agopian923c6612009-08-17 18:07:06 -07001262 // could be called before eglInitialize(), but we wouldn't have a context
1263 // then, and this function would correctly return EGL_NO_CONTEXT.
1264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001265 EGLContext ctx = getContext();
1266 return ctx;
1267}
1268
1269EGLSurface eglGetCurrentSurface(EGLint readdraw)
1270{
Mathias Agopian923c6612009-08-17 18:07:06 -07001271 // could be called before eglInitialize(), but we wouldn't have a context
1272 // then, and this function would correctly return EGL_NO_SURFACE.
1273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 EGLContext ctx = getContext();
1275 if (ctx) {
1276 egl_context_t const * const c = get_context(ctx);
1277 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1278 switch (readdraw) {
1279 case EGL_READ: return c->read;
1280 case EGL_DRAW: return c->draw;
1281 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1282 }
1283 }
1284 return EGL_NO_SURFACE;
1285}
1286
1287EGLDisplay eglGetCurrentDisplay(void)
1288{
Mathias Agopian923c6612009-08-17 18:07:06 -07001289 // could be called before eglInitialize(), but we wouldn't have a context
1290 // then, and this function would correctly return EGL_NO_DISPLAY.
1291
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 EGLContext ctx = getContext();
1293 if (ctx) {
1294 egl_context_t const * const c = get_context(ctx);
1295 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1296 return c->dpy;
1297 }
1298 return EGL_NO_DISPLAY;
1299}
1300
1301EGLBoolean eglWaitGL(void)
1302{
Mathias Agopian923c6612009-08-17 18:07:06 -07001303 // could be called before eglInitialize(), but we wouldn't have a context
1304 // then, and this function would return GL_TRUE, which isn't wrong.
1305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 EGLBoolean res = EGL_TRUE;
1307 EGLContext ctx = getContext();
1308 if (ctx) {
1309 egl_context_t const * const c = get_context(ctx);
1310 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1311 if (uint32_t(c->impl)>=2)
1312 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1313 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1314 if (!cnx->dso)
1315 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001316 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 }
1318 return res;
1319}
1320
1321EGLBoolean eglWaitNative(EGLint engine)
1322{
Mathias Agopian923c6612009-08-17 18:07:06 -07001323 // could be called before eglInitialize(), but we wouldn't have a context
1324 // then, and this function would return GL_TRUE, which isn't wrong.
1325
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326 EGLBoolean res = EGL_TRUE;
1327 EGLContext ctx = getContext();
1328 if (ctx) {
1329 egl_context_t const * const c = get_context(ctx);
1330 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1331 if (uint32_t(c->impl)>=2)
1332 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1333 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1334 if (!cnx->dso)
1335 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001336 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 }
1338 return res;
1339}
1340
1341EGLint eglGetError(void)
1342{
1343 EGLint result = EGL_SUCCESS;
Mathias Agopian618fa102009-10-14 02:06:37 -07001344 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345 EGLint err = EGL_SUCCESS;
1346 egl_connection_t* const cnx = &gEGLImpl[i];
1347 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001348 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1350 result = err;
1351 }
1352 if (result == EGL_SUCCESS)
1353 result = getError();
1354 return result;
1355}
1356
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001357__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001359 // eglGetProcAddress() could be the very first function called
1360 // in which case we must make sure we've initialized ourselves, this
1361 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001362
1363 if (egl_init_drivers() == EGL_FALSE) {
1364 setError(EGL_BAD_PARAMETER, NULL);
1365 return NULL;
1366 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 __eglMustCastToProperFunctionPointerType addr;
1369 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1370 if (addr) return addr;
1371
1372 return NULL; // TODO: finish implementation below
1373
1374 addr = findProcAddress(procname, gGLExtentionMap, NELEM(gGLExtentionMap));
1375 if (addr) return addr;
1376
1377 addr = 0;
1378 int slot = -1;
Mathias Agopian618fa102009-10-14 02:06:37 -07001379 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 egl_connection_t* const cnx = &gEGLImpl[i];
1381 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001382 if (cnx->egl.eglGetProcAddress) {
1383 addr = cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384 if (addr) {
1385 if (slot == -1) {
1386 slot = 0; // XXX: find free slot
1387 if (slot == -1) {
1388 addr = 0;
1389 break;
1390 }
1391 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001392 //cnx->hooks->ext.extensions[slot] = addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393 }
1394 }
1395 }
1396 }
1397
1398 if (slot >= 0) {
1399 addr = 0; // XXX: address of stub 'slot'
1400 gGLExtentionMap[slot].name = strdup(procname);
1401 gGLExtentionMap[slot].address = addr;
1402 }
1403
1404 return addr;
1405
1406
1407 /*
1408 * TODO: For OpenGL ES extensions, we must generate a stub
1409 * that looks like
1410 * mov r12, #0xFFFF0FFF
1411 * ldr r12, [r12, #-15]
1412 * ldr r12, [r12, #TLS_SLOT_OPENGL_API*4]
1413 * mov r12, [r12, #api_offset]
1414 * ldrne pc, r12
1415 * mov pc, #unsupported_extension
1416 *
1417 * and write the address of the extension in *all*
1418 * gl_hooks_t::gl_ext_t at offset "api_offset" from gl_hooks_t
1419 *
1420 */
1421}
1422
1423EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1424{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001425 SurfaceRef _s(draw);
1426 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 if (!validate_display_surface(dpy, draw))
1429 return EGL_FALSE;
1430 egl_display_t const * const dp = get_display(dpy);
1431 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001432 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433}
1434
1435EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1436 NativePixmapType target)
1437{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001438 SurfaceRef _s(surface);
1439 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1440
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441 if (!validate_display_surface(dpy, surface))
1442 return EGL_FALSE;
1443 egl_display_t const * const dp = get_display(dpy);
1444 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001445 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001446 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447}
1448
1449const char* eglQueryString(EGLDisplay dpy, EGLint name)
1450{
1451 egl_display_t const * const dp = get_display(dpy);
1452 switch (name) {
1453 case EGL_VENDOR:
1454 return gVendorString;
1455 case EGL_VERSION:
1456 return gVersionString;
1457 case EGL_EXTENSIONS:
1458 return gExtensionString;
1459 case EGL_CLIENT_APIS:
1460 return gClientApiString;
1461 }
1462 return setError(EGL_BAD_PARAMETER, (const char *)0);
1463}
1464
1465
1466// ----------------------------------------------------------------------------
1467// EGL 1.1
1468// ----------------------------------------------------------------------------
1469
1470EGLBoolean eglSurfaceAttrib(
1471 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1472{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001473 SurfaceRef _s(surface);
1474 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1475
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 if (!validate_display_surface(dpy, surface))
1477 return EGL_FALSE;
1478 egl_display_t const * const dp = get_display(dpy);
1479 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001480 if (s->cnx->egl.eglSurfaceAttrib) {
1481 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001482 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 }
1484 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1485}
1486
1487EGLBoolean eglBindTexImage(
1488 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1489{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001490 SurfaceRef _s(surface);
1491 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1492
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493 if (!validate_display_surface(dpy, surface))
1494 return EGL_FALSE;
1495 egl_display_t const * const dp = get_display(dpy);
1496 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001497 if (s->cnx->egl.eglBindTexImage) {
1498 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001499 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 }
1501 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1502}
1503
1504EGLBoolean eglReleaseTexImage(
1505 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1506{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001507 SurfaceRef _s(surface);
1508 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1509
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 if (!validate_display_surface(dpy, surface))
1511 return EGL_FALSE;
1512 egl_display_t const * const dp = get_display(dpy);
1513 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001514 if (s->cnx->egl.eglReleaseTexImage) {
1515 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001516 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517 }
1518 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1519}
1520
1521EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1522{
1523 egl_display_t * const dp = get_display(dpy);
1524 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1525
1526 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001527 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001528 egl_connection_t* const cnx = &gEGLImpl[i];
1529 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001530 if (cnx->egl.eglSwapInterval) {
1531 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001532 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533 res = EGL_FALSE;
1534 }
1535 }
1536 }
1537 }
1538 return res;
1539}
1540
1541
1542// ----------------------------------------------------------------------------
1543// EGL 1.2
1544// ----------------------------------------------------------------------------
1545
1546EGLBoolean eglWaitClient(void)
1547{
Mathias Agopian923c6612009-08-17 18:07:06 -07001548 // could be called before eglInitialize(), but we wouldn't have a context
1549 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550 EGLBoolean res = EGL_TRUE;
1551 EGLContext ctx = getContext();
1552 if (ctx) {
1553 egl_context_t const * const c = get_context(ctx);
1554 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1555 if (uint32_t(c->impl)>=2)
1556 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1557 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1558 if (!cnx->dso)
1559 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001560 if (cnx->egl.eglWaitClient) {
1561 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001563 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564 }
1565 }
1566 return res;
1567}
1568
1569EGLBoolean eglBindAPI(EGLenum api)
1570{
Mathias Agopian923c6612009-08-17 18:07:06 -07001571 if (egl_init_drivers() == EGL_FALSE) {
1572 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1573 }
1574
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575 // bind this API on all EGLs
1576 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001577 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 egl_connection_t* const cnx = &gEGLImpl[i];
1579 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001580 if (cnx->egl.eglBindAPI) {
1581 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 res = EGL_FALSE;
1583 }
1584 }
1585 }
1586 }
1587 return res;
1588}
1589
1590EGLenum eglQueryAPI(void)
1591{
Mathias Agopian923c6612009-08-17 18:07:06 -07001592 if (egl_init_drivers() == EGL_FALSE) {
1593 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1594 }
1595
Mathias Agopian618fa102009-10-14 02:06:37 -07001596 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 egl_connection_t* const cnx = &gEGLImpl[i];
1598 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001599 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 // the first one we find is okay, because they all
1601 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001602 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 }
1604 }
1605 }
1606 // or, it can only be OpenGL ES
1607 return EGL_OPENGL_ES_API;
1608}
1609
1610EGLBoolean eglReleaseThread(void)
1611{
Mathias Agopian618fa102009-10-14 02:06:37 -07001612 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 egl_connection_t* const cnx = &gEGLImpl[i];
1614 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001615 if (cnx->egl.eglReleaseThread) {
1616 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 }
1618 }
1619 }
1620 clearTLS();
1621 return EGL_TRUE;
1622}
1623
1624EGLSurface eglCreatePbufferFromClientBuffer(
1625 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1626 EGLConfig config, const EGLint *attrib_list)
1627{
1628 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001629 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001631 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1632 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001633 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1634 buftype, buffer,
1635 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 }
1637 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1638}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001639
1640// ----------------------------------------------------------------------------
1641// EGL_EGLEXT_VERSION 3
1642// ----------------------------------------------------------------------------
1643
1644EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1645 const EGLint *attrib_list)
1646{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001647 SurfaceRef _s(surface);
1648 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1649
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001650 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001651 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001652
1653 egl_display_t const * const dp = get_display(dpy);
1654 egl_surface_t const * const s = get_surface(surface);
1655
Mathias Agopian618fa102009-10-14 02:06:37 -07001656 if (s->cnx->egl.eglLockSurfaceKHR) {
1657 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001658 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001659 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001660 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001661}
1662
1663EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1664{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001665 SurfaceRef _s(surface);
1666 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1667
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001668 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001669 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001670
1671 egl_display_t const * const dp = get_display(dpy);
1672 egl_surface_t const * const s = get_surface(surface);
1673
Mathias Agopian618fa102009-10-14 02:06:37 -07001674 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1675 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001676 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001677 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001678 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001679}
1680
1681EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1682 EGLClientBuffer buffer, const EGLint *attrib_list)
1683{
1684 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001685 ContextRef _c(ctx);
1686 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001687 if (!validate_display_context(dpy, ctx))
1688 return EGL_NO_IMAGE_KHR;
1689 egl_display_t const * const dp = get_display(dpy);
1690 egl_context_t * const c = get_context(ctx);
1691 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001692 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001693 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001694 if (image == EGL_NO_IMAGE_KHR)
1695 return image;
1696
1697 egl_image_t* result = new egl_image_t(dpy, ctx);
1698 result->images[c->impl] = image;
1699 return (EGLImageKHR)result;
1700 } else {
1701 // EGL_NO_CONTEXT is a valid parameter
1702 egl_display_t const * const dp = get_display(dpy);
1703 if (dp == 0) {
1704 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1705 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001706
1707 /* Since we don't have a way to know which implementation to call,
1708 * we're calling all of them. If at least one of the implementation
1709 * succeeded, this is a success.
1710 */
1711
1712 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001713
Mathias Agopian618fa102009-10-14 02:06:37 -07001714 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001715 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001716 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001717 egl_connection_t* const cnx = &gEGLImpl[i];
1718 implImages[i] = EGL_NO_IMAGE_KHR;
1719 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001720 if (cnx->egl.eglCreateImageKHR) {
1721 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001722 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001723 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1724 success = true;
1725 }
1726 }
1727 }
1728 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001729
1730 if (!success) {
1731 // failure, if there was an error when we entered this function,
1732 // the error flag must not be updated.
1733 // Otherwise, the error is whatever happened in the implementation
1734 // that faulted.
1735 if (currentError != EGL_SUCCESS) {
1736 setError(currentError, EGL_NO_IMAGE_KHR);
1737 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001738 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001739 } else {
1740 // In case of success, we need to clear all error flags
1741 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07001742 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001743 // a "full" success (all implementation succeeded).
1744 eglGetError();
1745 }
1746
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001747 egl_image_t* result = new egl_image_t(dpy, ctx);
1748 memcpy(result->images, implImages, sizeof(implImages));
1749 return (EGLImageKHR)result;
1750 }
1751}
1752
1753EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1754{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001755 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001756 if (dp == 0) {
1757 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1758 }
1759
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001760 ImageRef _i(img);
1761 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001762
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001763 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001764 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001765 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001766 egl_connection_t* const cnx = &gEGLImpl[i];
1767 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1768 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001769 if (cnx->egl.eglCreateImageKHR) {
1770 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001771 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001772 success = true;
1773 }
1774 }
1775 }
1776 }
1777 }
1778 if (!success)
1779 return EGL_FALSE;
1780
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001781 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001782
Mathias Agopian24e5f522009-08-12 21:18:15 -07001783 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001784}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001785
1786
1787// ----------------------------------------------------------------------------
1788// ANDROID extensions
1789// ----------------------------------------------------------------------------
1790
1791EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1792 EGLint left, EGLint top, EGLint width, EGLint height)
1793{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001794 SurfaceRef _s(draw);
1795 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1796
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001797 if (!validate_display_surface(dpy, draw))
1798 return EGL_FALSE;
1799 egl_display_t const * const dp = get_display(dpy);
1800 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001801 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
1802 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001803 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001804 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001805 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001806}
1807
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001808EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
1809{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001810 SurfaceRef _s(draw);
1811 if (!_s.get()) return setError(EGL_BAD_SURFACE, (EGLClientBuffer*)0);
1812
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001813 if (!validate_display_surface(dpy, draw))
1814 return 0;
1815 egl_display_t const * const dp = get_display(dpy);
1816 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001817 if (s->cnx->egl.eglGetRenderBufferANDROID) {
1818 return s->cnx->egl.eglGetRenderBufferANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001819 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001820 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001821 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer*)0);
Mathias Agopian8d2e83b2009-06-24 22:37:39 -07001822}