blob: ed3617182cf4b8d1aac54b7c36e9e1b142bc3b92 [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
Mathias Agopian644bb2a2010-11-24 15:59:35 -080043#include <ui/egl/android_natives.h>
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include "hooks.h"
46#include "egl_impl.h"
Mathias Agopiande586972009-05-28 17:39:03 -070047#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
50
51// ----------------------------------------------------------------------------
52namespace android {
53// ----------------------------------------------------------------------------
54
55#define VERSION_MAJOR 1
56#define VERSION_MINOR 4
57static char const * const gVendorString = "Android";
Mathias Agopian923c6612009-08-17 18:07:06 -070058static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060static char const * const gExtensionString =
61 "EGL_KHR_image "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -070062 "EGL_KHR_image_base "
63 "EGL_KHR_image_pixmap "
Mathias Agopian8e4b5a32010-08-27 16:08:03 -070064 "EGL_KHR_gl_texture_2D_image "
Michael I. Goldca41e362011-01-13 10:13:15 -080065 "EGL_KHR_gl_texture_cubemap_image "
66 "EGL_KHR_gl_renderbuffer_image "
Mathias Agopianc291f5852010-08-27 16:48:56 -070067 "EGL_KHR_fence_sync "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070068 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070069 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 ;
71
72// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
Mathias Agopian9429e9c2009-08-21 02:18:25 -070074class egl_object_t {
75 static SortedVector<egl_object_t*> sObjects;
76 static Mutex sLock;
77
78 volatile int32_t terminated;
79 mutable volatile int32_t count;
80
81public:
82 egl_object_t() : terminated(0), count(1) {
83 Mutex::Autolock _l(sLock);
84 sObjects.add(this);
85 }
86
87 inline bool isAlive() const { return !terminated; }
88
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -070090 bool get() {
91 Mutex::Autolock _l(sLock);
92 if (egl_object_t::sObjects.indexOf(this) >= 0) {
93 android_atomic_inc(&count);
94 return true;
95 }
96 return false;
97 }
98
99 bool put() {
100 Mutex::Autolock _l(sLock);
101 if (android_atomic_dec(&count) == 1) {
102 sObjects.remove(this);
103 return true;
104 }
105 return false;
106 }
107
108public:
109 template <typename N, typename T>
110 struct LocalRef {
111 N* ref;
112 LocalRef(T o) : ref(0) {
113 N* native = reinterpret_cast<N*>(o);
114 if (o && native->get()) {
115 ref = native;
116 }
117 }
118 ~LocalRef() {
119 if (ref && ref->put()) {
120 delete ref;
121 }
122 }
123 inline N* get() {
124 return ref;
125 }
126 void acquire() const {
127 if (ref) {
128 android_atomic_inc(&ref->count);
129 }
130 }
131 void release() const {
132 if (ref) {
133 int32_t c = android_atomic_dec(&ref->count);
134 // ref->count cannot be 1 prior atomic_dec because we have
135 // a reference, and if we have one, it means there was
136 // already one before us.
137 LOGE_IF(c==1, "refcount is now 0 in release()");
138 }
139 }
140 void terminate() {
141 if (ref) {
142 ref->terminated = 1;
143 release();
144 }
145 }
146 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147};
148
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700149SortedVector<egl_object_t*> egl_object_t::sObjects;
150Mutex egl_object_t::sLock;
151
Mathias Agopiancee79392010-07-26 21:14:59 -0700152
153struct egl_config_t {
154 egl_config_t() {}
155 egl_config_t(int impl, EGLConfig config)
156 : impl(impl), config(config), configId(0), implConfigId(0) { }
157 int impl; // the implementation this config is for
158 EGLConfig config; // the implementation's EGLConfig
159 EGLint configId; // our CONFIG_ID
160 EGLint implConfigId; // the implementation's CONFIG_ID
161 inline bool operator < (const egl_config_t& rhs) const {
162 if (impl < rhs.impl) return true;
163 if (impl > rhs.impl) return false;
164 return config < rhs.config;
165 }
166};
167
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700168struct egl_display_t {
169 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
170
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 struct strings_t {
172 char const * vendor;
173 char const * version;
174 char const * clientApi;
175 char const * extensions;
176 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700177
178 struct DisplayImpl {
179 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
180 state(NOT_INITIALIZED), numConfigs(0) { }
181 EGLDisplay dpy;
182 EGLConfig* config;
183 EGLint state;
184 EGLint numConfigs;
185 strings_t queryString;
186 };
187
Mathias Agopiancee79392010-07-26 21:14:59 -0700188 uint32_t magic;
189 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
190 EGLint numTotalConfigs;
191 egl_config_t* configs;
192 uint32_t refs;
193 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700194
Mathias Agopiancee79392010-07-26 21:14:59 -0700195 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700196 ~egl_display_t() { magic = 0; }
197 inline bool isValid() const { return magic == '_dpy'; }
198 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199};
200
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700201struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700203 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
204
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800205 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win,
206 EGLSurface surface, int impl, egl_connection_t const* cnx)
207 : dpy(dpy), surface(surface), config(config), win(win), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 }
209 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 }
211 EGLDisplay dpy;
212 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700213 EGLConfig config;
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800214 sp<ANativeWindow> win;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 int impl;
216 egl_connection_t const* cnx;
217};
218
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700219struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700221 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
222
Mathias Agopiancee79392010-07-26 21:14:59 -0700223 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian618fa102009-10-14 02:06:37 -0700224 int impl, egl_connection_t const* cnx, int version)
Mathias Agopianc3ce8802010-12-08 15:34:02 -0800225 : dpy(dpy), context(context), config(config), read(0), draw(0), impl(impl),
226 cnx(cnx), version(version)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 {
228 }
229 EGLDisplay dpy;
230 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700231 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 EGLSurface read;
233 EGLSurface draw;
234 int impl;
235 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700236 int version;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237};
238
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700239struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700241 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
242
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243 egl_image_t(EGLDisplay dpy, EGLContext context)
244 : dpy(dpy), context(context)
245 {
246 memset(images, 0, sizeof(images));
247 }
248 EGLDisplay dpy;
Mathias Agopian77fbf8d2010-09-09 11:12:54 -0700249 EGLContext context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700250 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251};
252
Mathias Agopianc291f5852010-08-27 16:48:56 -0700253struct egl_sync_t : public egl_object_t
254{
255 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
256
257 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync)
258 : dpy(dpy), context(context), sync(sync)
259 {
260 }
261 EGLDisplay dpy;
262 EGLContext context;
263 EGLSyncKHR sync;
264};
265
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700266typedef egl_surface_t::Ref SurfaceRef;
267typedef egl_context_t::Ref ContextRef;
268typedef egl_image_t::Ref ImageRef;
Mathias Agopianc291f5852010-08-27 16:48:56 -0700269typedef egl_sync_t::Ref SyncRef;
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271struct tls_t
272{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700273 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 EGLint error;
275 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700276 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277};
278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
280// ----------------------------------------------------------------------------
281
Mathias Agopianbf41b112010-04-09 13:37:34 -0700282static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283static egl_display_t gDisplay[NUM_DISPLAYS];
284static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
285static pthread_key_t gEGLThreadLocalStorageKey = -1;
286
287// ----------------------------------------------------------------------------
288
Mathias Agopian618fa102009-10-14 02:06:37 -0700289EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
290EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700291EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700293#if EGL_TRACE
294
295EGLAPI pthread_key_t gGLTraceKey = -1;
296
297// ----------------------------------------------------------------------------
298
299static int gEGLTraceLevel;
300static int gEGLApplicationTraceLevel;
301extern EGLAPI gl_hooks_t gHooksTrace;
302
303static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
304 pthread_setspecific(gGLTraceKey, value);
305}
306
307gl_hooks_t const* getGLTraceThreadSpecific() {
308 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
309}
310
311static void initEglTraceLevel() {
312 char value[PROPERTY_VALUE_MAX];
313 property_get("debug.egl.trace", value, "0");
314 int propertyLevel = atoi(value);
315 int applicationLevel = gEGLApplicationTraceLevel;
316 gEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
317}
318
319static void setGLHooksThreadSpecific(gl_hooks_t const *value) {
320 if (gEGLTraceLevel > 0) {
321 setGlTraceThreadSpecific(value);
322 setGlThreadSpecific(&gHooksTrace);
323 } else {
324 setGlThreadSpecific(value);
325 }
326}
327
328/*
329 * Global entry point to allow applications to modify their own trace level.
330 * The effective trace level is the max of this level and the value of debug.egl.trace.
331 */
332extern "C"
333void setGLTraceLevel(int level) {
334 gEGLApplicationTraceLevel = level;
335}
336
337#else
338
339static inline void setGLHooksThreadSpecific(gl_hooks_t const *value) {
340 setGlThreadSpecific(value);
341}
342
343#endif
344
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345// ----------------------------------------------------------------------------
346
347static __attribute__((noinline))
348const char *egl_strerror(EGLint err)
349{
350 switch (err){
351 case EGL_SUCCESS: return "EGL_SUCCESS";
352 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
353 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
354 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
355 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
356 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
357 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
358 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
359 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
360 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
361 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
362 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
363 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
364 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
365 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
366 default: return "UNKNOWN";
367 }
368}
369
370static __attribute__((noinline))
371void clearTLS() {
372 if (gEGLThreadLocalStorageKey != -1) {
373 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
374 if (tls) {
375 delete tls;
376 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
377 }
378 }
379}
380
381static tls_t* getTLS()
382{
383 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
384 if (tls == 0) {
385 tls = new tls_t;
386 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
387 }
388 return tls;
389}
390
391template<typename T>
392static __attribute__((noinline))
393T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
394 if (gEGLThreadLocalStorageKey == -1) {
395 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
396 if (gEGLThreadLocalStorageKey == -1)
397 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
398 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
399 }
400 tls_t* tls = getTLS();
401 if (tls->error != error) {
402 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
403 tls->error = error;
404 }
405 return returnValue;
406}
407
408static __attribute__((noinline))
409GLint getError() {
410 if (gEGLThreadLocalStorageKey == -1)
411 return EGL_SUCCESS;
412 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
413 if (!tls) return EGL_SUCCESS;
414 GLint error = tls->error;
415 tls->error = EGL_SUCCESS;
416 return error;
417}
418
419static __attribute__((noinline))
420void setContext(EGLContext ctx) {
421 if (gEGLThreadLocalStorageKey == -1) {
422 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
423 if (gEGLThreadLocalStorageKey == -1)
424 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
425 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
426 }
427 tls_t* tls = getTLS();
428 tls->ctx = ctx;
429}
430
431static __attribute__((noinline))
432EGLContext getContext() {
433 if (gEGLThreadLocalStorageKey == -1)
434 return EGL_NO_CONTEXT;
435 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
436 if (!tls) return EGL_NO_CONTEXT;
437 return tls->ctx;
438}
439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440/*****************************************************************************/
441
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442template<typename T>
443static __attribute__((noinline))
444int binarySearch(
445 T const sortedArray[], int first, int last, T key)
446{
447 while (first <= last) {
448 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700449 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450 first = mid + 1;
451 } else if (key < sortedArray[mid]) {
452 last = mid - 1;
453 } else {
454 return mid;
455 }
456 }
457 return -1;
458}
459
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460static int cmp_configs(const void* a, const void *b)
461{
Mathias Agopiancee79392010-07-26 21:14:59 -0700462 const egl_config_t& c0 = *(egl_config_t const *)a;
463 const egl_config_t& c1 = *(egl_config_t const *)b;
464 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800465}
466
467struct extention_map_t {
468 const char* name;
469 __eglMustCastToProperFunctionPointerType address;
470};
471
472static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700473 { "eglLockSurfaceKHR",
474 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
475 { "eglUnlockSurfaceKHR",
476 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
477 { "eglCreateImageKHR",
478 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
479 { "eglDestroyImageKHR",
480 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700481 { "eglSetSwapRectangleANDROID",
482 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483};
484
Mathias Agopian24035332010-08-02 17:34:32 -0700485extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
486
487// accesses protected by gInitDriverMutex
488static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
489static int gGLExtentionSlot = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490
491static void(*findProcAddress(const char* name,
492 const extention_map_t* map, size_t n))()
493{
494 for (uint32_t i=0 ; i<n ; i++) {
495 if (!strcmp(name, map[i].name)) {
496 return map[i].address;
497 }
498 }
499 return NULL;
500}
501
502// ----------------------------------------------------------------------------
503
Mathias Agopian6f087122010-09-23 16:38:38 -0700504static int gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700505 tls_t* tls = getTLS();
506 if (tls->logCallWithNoContext == EGL_TRUE) {
507 tls->logCallWithNoContext = EGL_FALSE;
508 LOGE("call to OpenGL ES API with no current context "
509 "(logged once per thread)");
510 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700511 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700512}
513
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514static void early_egl_init(void)
515{
516#if !USE_FAST_TLS_KEY
517 pthread_key_create(&gGLWrapperKey, NULL);
518#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700519#if EGL_TRACE
520 pthread_key_create(&gGLTraceKey, NULL);
521 initEglTraceLevel();
522#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523 uint32_t addr = (uint32_t)((void*)gl_no_context);
524 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700525 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700527 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700528
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700529 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530}
531
532static pthread_once_t once_control = PTHREAD_ONCE_INIT;
533static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
534
535
536static inline
537egl_display_t* get_display(EGLDisplay dpy)
538{
539 uintptr_t index = uintptr_t(dpy)-1U;
540 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
541}
542
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700543template<typename NATIVE, typename EGL>
544static inline NATIVE* egl_to_native_cast(EGL arg) {
545 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546}
547
548static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700549egl_surface_t* get_surface(EGLSurface surface) {
550 return egl_to_native_cast<egl_surface_t>(surface);
551}
552
553static inline
554egl_context_t* get_context(EGLContext context) {
555 return egl_to_native_cast<egl_context_t>(context);
556}
557
558static inline
559egl_image_t* get_image(EGLImageKHR image) {
560 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561}
562
Mathias Agopianc291f5852010-08-27 16:48:56 -0700563static inline
564egl_sync_t* get_sync(EGLSyncKHR sync) {
565 return egl_to_native_cast<egl_sync_t>(sync);
566}
567
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568static egl_connection_t* validate_display_config(
569 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700570 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800571{
572 dp = get_display(dpy);
573 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
574
Mathias Agopiancee79392010-07-26 21:14:59 -0700575 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800576 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
577 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700578 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579 if (cnx->dso == 0) {
580 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
581 }
582 return cnx;
583}
584
585static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
586{
587 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
588 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700589 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700591 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
593 return EGL_TRUE;
594}
595
596static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
597{
598 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
599 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700600 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700602 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 return setError(EGL_BAD_SURFACE, EGL_FALSE);
604 return EGL_TRUE;
605}
606
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700607EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
608{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700609 ImageRef _i(image);
610 if (!_i.get()) return EGL_NO_IMAGE_KHR;
611
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700612 EGLContext context = getContext();
613 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
614 return EGL_NO_IMAGE_KHR;
615
616 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700617 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700618 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700620 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700621 return i->images[c->impl];
622}
623
Mathias Agopian923c6612009-08-17 18:07:06 -0700624// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700625
Mathias Agopian923c6612009-08-17 18:07:06 -0700626// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700627// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700628// egl_init_drivers_locked()
629//
630static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
631
632EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633{
634 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700635 // initialized by static ctor. should be set here.
636 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 }
638
Mathias Agopiande586972009-05-28 17:39:03 -0700639 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700640 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700641
Mathias Agopian923c6612009-08-17 18:07:06 -0700642 // dynamically load all our EGL implementations for all displays
643 // and retrieve the corresponding EGLDisplay
644 // if that fails, don't use this driver.
645 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
646 egl_connection_t* cnx;
647 egl_display_t* d = &gDisplay[0];
648
649 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700651 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
652 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
653 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700654 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700655 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700656 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700657 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700658 if (dpy == EGL_NO_DISPLAY) {
659 loader.close(cnx->dso);
660 cnx->dso = NULL;
661 }
662 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663 }
664
665 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700666 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667 char value[PROPERTY_VALUE_MAX];
668 property_get("debug.egl.hw", value, "1");
669 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700670 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
671 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
672 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700673 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700674 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700675 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700676 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700677 if (dpy == EGL_NO_DISPLAY) {
678 loader.close(cnx->dso);
679 cnx->dso = NULL;
680 }
681 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682 } else {
683 LOGD("3D hardware acceleration is disabled");
684 }
685 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700686
Mathias Agopian923c6612009-08-17 18:07:06 -0700687 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
688 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 }
690
Mathias Agopian923c6612009-08-17 18:07:06 -0700691 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692}
693
Mathias Agopian923c6612009-08-17 18:07:06 -0700694EGLBoolean egl_init_drivers()
695{
696 EGLBoolean res;
697 pthread_mutex_lock(&gInitDriverMutex);
698 res = egl_init_drivers_locked();
699 pthread_mutex_unlock(&gInitDriverMutex);
700 return res;
701}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700702
703// ----------------------------------------------------------------------------
704}; // namespace android
705// ----------------------------------------------------------------------------
706
707using namespace android;
708
709EGLDisplay eglGetDisplay(NativeDisplayType display)
710{
Mathias Agopian923c6612009-08-17 18:07:06 -0700711 uint32_t index = uint32_t(display);
712 if (index >= NUM_DISPLAYS) {
713 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
714 }
715
716 if (egl_init_drivers() == EGL_FALSE) {
717 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
718 }
719
720 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
721 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700722}
723
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724// ----------------------------------------------------------------------------
725// Initialization
726// ----------------------------------------------------------------------------
727
728EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
729{
730 egl_display_t * const dp = get_display(dpy);
731 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
732
Mathias Agopian75bc2782010-02-05 16:17:01 -0800733 Mutex::Autolock _l(dp->lock);
734
735 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736 if (major != NULL) *major = VERSION_MAJOR;
737 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700738 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739 return EGL_TRUE;
740 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700741
742#if EGL_TRACE
743
744 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
745 // the information from that call may be stale.)
746 initEglTraceLevel();
747
748#endif
749
750 setGLHooksThreadSpecific(&gHooksNoContext);
751
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752 // initialize each EGL and
753 // build our own extension string first, based on the extension we know
754 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700755 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756 egl_connection_t* const cnx = &gEGLImpl[i];
757 cnx->major = -1;
758 cnx->minor = -1;
759 if (!cnx->dso)
760 continue;
761
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700762#if defined(ADRENO130)
763#warning "Adreno-130 eglInitialize() workaround"
764 /*
765 * The ADRENO 130 driver returns a different EGLDisplay each time
766 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
767 * after eglTerminate() has been called, so that eglInitialize()
768 * cannot be called again. Therefore, we need to make sure to call
769 * eglGetDisplay() before calling eglInitialize();
770 */
771 if (i == IMPL_HARDWARE) {
772 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700773 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700774 }
775#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700777
778 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700779 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700781 // i, idpy, cnx->major, cnx->minor, cnx);
782
783 // display is now initialized
784 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785
786 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700787 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700788 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700789 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700790 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700791 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700792 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700793 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700794 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795
796 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700797 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700798 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 }
800 }
801
802 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700803 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 egl_connection_t* const cnx = &gEGLImpl[i];
805 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
806 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700807 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700808 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
809 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700810 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700811 dp->disp[i].dpy, dp->disp[i].config, n,
812 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800813 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814 dp->numTotalConfigs += n;
815 res = EGL_TRUE;
816 }
817 }
818 }
819 }
820 }
821
822 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700823 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
824 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
825 egl_connection_t* const cnx = &gEGLImpl[i];
826 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
827 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
828 dp->configs[k].impl = i;
829 dp->configs[k].config = dp->disp[i].config[j];
830 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
831 // store the implementation's CONFIG_ID
832 cnx->egl.eglGetConfigAttrib(
833 dp->disp[i].dpy,
834 dp->disp[i].config[j],
835 EGL_CONFIG_ID,
836 &dp->configs[k].implConfigId);
837 k++;
838 }
839 }
840 }
841
842 // sort our configurations so we can do binary-searches
843 qsort( dp->configs,
844 dp->numTotalConfigs,
845 sizeof(egl_config_t), cmp_configs);
846
Mathias Agopian75bc2782010-02-05 16:17:01 -0800847 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848 if (major != NULL) *major = VERSION_MAJOR;
849 if (minor != NULL) *minor = VERSION_MINOR;
850 return EGL_TRUE;
851 }
852 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
853}
854
855EGLBoolean eglTerminate(EGLDisplay dpy)
856{
Mathias Agopian923c6612009-08-17 18:07:06 -0700857 // NOTE: don't unload the drivers b/c some APIs can be called
858 // after eglTerminate() has been called. eglTerminate() only
859 // terminates an EGLDisplay, not a EGL itself.
860
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861 egl_display_t* const dp = get_display(dpy);
862 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800863
864 Mutex::Autolock _l(dp->lock);
865
866 if (dp->refs == 0) {
867 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
868 }
869
870 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700871 if (dp->refs > 1) {
872 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700874 }
Mathias Agopiande586972009-05-28 17:39:03 -0700875
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700877 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700879 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700880 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700881 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700882 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700883 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700884 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700885 free(dp->disp[i].config);
886
887 dp->disp[i].numConfigs = 0;
888 dp->disp[i].config = 0;
889 dp->disp[i].state = egl_display_t::TERMINATED;
890
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 res = EGL_TRUE;
892 }
893 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700894
895 // TODO: all egl_object_t should be marked for termination
896
Mathias Agopian75bc2782010-02-05 16:17:01 -0800897 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700899 delete [] dp->configs;
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -0800900
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901 return res;
902}
903
904// ----------------------------------------------------------------------------
905// configuration
906// ----------------------------------------------------------------------------
907
908EGLBoolean eglGetConfigs( EGLDisplay dpy,
909 EGLConfig *configs,
910 EGLint config_size, EGLint *num_config)
911{
912 egl_display_t const * const dp = get_display(dpy);
913 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
914
915 GLint numConfigs = dp->numTotalConfigs;
916 if (!configs) {
917 *num_config = numConfigs;
918 return EGL_TRUE;
919 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700920
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700922 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
923 *configs++ = EGLConfig(i);
924 config_size--;
925 n++;
926 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927
928 *num_config = n;
929 return EGL_TRUE;
930}
931
932EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
933 EGLConfig *configs, EGLint config_size,
934 EGLint *num_config)
935{
936 egl_display_t const * const dp = get_display(dpy);
937 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
938
Jack Palevich749c63d2009-03-25 15:12:17 -0700939 if (num_config==0) {
940 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800941 }
942
943 EGLint n;
944 EGLBoolean res = EGL_FALSE;
945 *num_config = 0;
946
947
948 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -0700949 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 // to figure out both its size and if it contains an EGL_CONFIG_ID
951 // key. If so, the full array is copied and patched.
952 // NOTE: we assume that there can be only one occurrence
953 // of EGL_CONFIG_ID.
954
955 EGLint patch_index = -1;
956 GLint attr;
957 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -0700958 if (attrib_list) {
959 while ((attr=attrib_list[size]) != EGL_NONE) {
960 if (attr == EGL_CONFIG_ID)
961 patch_index = size;
962 size += 2;
963 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800964 }
965 if (patch_index >= 0) {
966 size += 2; // we need copy the sentinel as well
967 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
968 if (new_list == 0)
969 return setError(EGL_BAD_ALLOC, EGL_FALSE);
970 memcpy(new_list, attrib_list, size*sizeof(EGLint));
971
972 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -0700973 bool found = false;
974 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -0700976 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
977 if (dp->configs[i].configId == configId) {
978 ourConfig = EGLConfig(i);
979 configId = dp->configs[i].implConfigId;
980 found = true;
981 break;
982 }
983 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984
Mathias Agopiancee79392010-07-26 21:14:59 -0700985 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
986 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800987 // and switch to the new list
988 attrib_list = const_cast<const EGLint *>(new_list);
989
990 // At this point, the only configuration that can match is
991 // dp->configs[i][index], however, we don't know if it would be
992 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -0700993 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994 // through all the EGLimpl[].
995 // We also know we can only get a single config back, and we know
996 // which one.
997
Mathias Agopian618fa102009-10-14 02:06:37 -0700998 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -0700999 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
1000 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001 if (res && n>0) {
1002 // n has to be 0 or 1, by construction, and we already know
1003 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -07001004 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001005 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -07001006 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007 *num_config = 1;
1008 }
1009 }
1010
1011 free(const_cast<EGLint *>(attrib_list));
1012 return res;
1013 }
1014
Mathias Agopiancee79392010-07-26 21:14:59 -07001015
Mathias Agopian618fa102009-10-14 02:06:37 -07001016 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 egl_connection_t* const cnx = &gEGLImpl[i];
1018 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001019 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001020 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001021 if (configs) {
1022 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -07001023 // internal EGLConfig format.
1024 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -07001025 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001026 egl_config_t key(i, configs[j]);
1027 intptr_t index = binarySearch<egl_config_t>(
1028 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -07001029 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001030 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -07001031 } else {
1032 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1033 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 }
Jack Palevich749c63d2009-03-25 15:12:17 -07001035 configs += n;
1036 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 *num_config += n;
1039 res = EGL_TRUE;
1040 }
1041 }
1042 }
1043 return res;
1044}
1045
1046EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1047 EGLint attribute, EGLint *value)
1048{
1049 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001050 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 if (!cnx) return EGL_FALSE;
1052
1053 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001054 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 return EGL_TRUE;
1056 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001057 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -07001058 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1059 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060}
1061
1062// ----------------------------------------------------------------------------
1063// surfaces
1064// ----------------------------------------------------------------------------
1065
1066EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1067 NativeWindowType window,
1068 const EGLint *attrib_list)
1069{
1070 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001071 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 if (cnx) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001073 EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy;
1074 EGLConfig iConfig = dp->configs[intptr_t(config)].config;
1075 EGLint format;
1076
1077 // set the native window's buffers format to match this config
1078 if (cnx->egl.eglGetConfigAttrib(iDpy,
1079 iConfig, EGL_NATIVE_VISUAL_ID, &format)) {
1080 if (format != 0) {
1081 native_window_set_buffers_geometry(window, 0, 0, format);
1082 }
1083 }
1084
Mathias Agopian618fa102009-10-14 02:06:37 -07001085 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001086 iDpy, iConfig, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001088 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001089 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 return s;
1091 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 }
1093 return EGL_NO_SURFACE;
1094}
1095
1096EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1097 NativePixmapType pixmap,
1098 const EGLint *attrib_list)
1099{
1100 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001101 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001103 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001104 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1105 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001107 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001108 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 return s;
1110 }
1111 }
1112 return EGL_NO_SURFACE;
1113}
1114
1115EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1116 const EGLint *attrib_list)
1117{
1118 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001119 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001121 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001122 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1123 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001125 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001126 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127 return s;
1128 }
1129 }
1130 return EGL_NO_SURFACE;
1131}
1132
1133EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1134{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001135 SurfaceRef _s(surface);
1136 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 if (!validate_display_surface(dpy, surface))
1139 return EGL_FALSE;
1140 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001142 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001143 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001144 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001145 if (result == EGL_TRUE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001146 if (s->win != NULL) {
1147 native_window_set_buffers_geometry(s->win.get(), 0, 0, 0);
1148 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001149 _s.terminate();
1150 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001151 return result;
1152}
1153
1154EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1155 EGLint attribute, EGLint *value)
1156{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001157 SurfaceRef _s(surface);
1158 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 if (!validate_display_surface(dpy, surface))
1161 return EGL_FALSE;
1162 egl_display_t const * const dp = get_display(dpy);
1163 egl_surface_t const * const s = get_surface(surface);
1164
Mathias Agopiancee79392010-07-26 21:14:59 -07001165 EGLBoolean result(EGL_TRUE);
1166 if (attribute == EGL_CONFIG_ID) {
1167 // We need to remap EGL_CONFIG_IDs
1168 *value = dp->configs[intptr_t(s->config)].configId;
1169 } else {
1170 result = s->cnx->egl.eglQuerySurface(
1171 dp->disp[s->impl].dpy, s->surface, attribute, value);
1172 }
1173
1174 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175}
1176
1177// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001178// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179// ----------------------------------------------------------------------------
1180
1181EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1182 EGLContext share_list, const EGLint *attrib_list)
1183{
1184 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001185 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001187 if (share_list != EGL_NO_CONTEXT) {
1188 egl_context_t* const c = get_context(share_list);
1189 share_list = c->context;
1190 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001191 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001192 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1193 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001194 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001196 // figure out if it's a GLESv1 or GLESv2
1197 int version = 0;
1198 if (attrib_list) {
1199 while (*attrib_list != EGL_NONE) {
1200 GLint attr = *attrib_list++;
1201 GLint value = *attrib_list++;
1202 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1203 if (value == 1) {
1204 version = GLESv1_INDEX;
1205 } else if (value == 2) {
1206 version = GLESv2_INDEX;
1207 }
1208 }
1209 };
1210 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001211 egl_context_t* c = new egl_context_t(dpy, context, config,
1212 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 return c;
1214 }
1215 }
1216 return EGL_NO_CONTEXT;
1217}
1218
1219EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1220{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001221 ContextRef _c(ctx);
1222 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 if (!validate_display_context(dpy, ctx))
1225 return EGL_FALSE;
1226 egl_display_t const * const dp = get_display(dpy);
1227 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001228 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001229 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001230 if (result == EGL_TRUE) {
1231 _c.terminate();
1232 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 return result;
1234}
1235
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001236static void loseCurrent(egl_context_t * cur_c)
1237{
1238 if (cur_c) {
1239 egl_surface_t * cur_r = get_surface(cur_c->read);
1240 egl_surface_t * cur_d = get_surface(cur_c->draw);
1241
1242 // by construction, these are either 0 or valid (possibly terminated)
1243 // it should be impossible for these to be invalid
1244 ContextRef _cur_c(cur_c);
1245 SurfaceRef _cur_r(cur_r);
1246 SurfaceRef _cur_d(cur_d);
1247
1248 cur_c->read = NULL;
1249 cur_c->draw = NULL;
1250
1251 _cur_c.release();
1252 _cur_r.release();
1253 _cur_d.release();
1254 }
1255}
1256
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1258 EGLSurface read, EGLContext ctx)
1259{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001260 // get a reference to the object passed in
1261 ContextRef _c(ctx);
1262 SurfaceRef _d(draw);
1263 SurfaceRef _r(read);
1264
1265 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 egl_display_t const * const dp = get_display(dpy);
1267 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001268 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1269 // EGL_NO_CONTEXT is valid
1270 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 }
1272
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001273 // these are the underlying implementation's object
1274 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001275 EGLSurface impl_draw = EGL_NO_SURFACE;
1276 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001277
1278 // these are our objects structs passed in
1279 egl_context_t * c = NULL;
1280 egl_surface_t const * d = NULL;
1281 egl_surface_t const * r = NULL;
1282
1283 // these are the current objects structs
1284 egl_context_t * cur_c = get_context(getContext());
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001285
1286 if (ctx != EGL_NO_CONTEXT) {
1287 c = get_context(ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001288 impl_ctx = c->context;
1289 } else {
1290 // no context given, use the implementation of the current context
1291 if (cur_c == NULL) {
1292 // no current context
1293 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001294 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1295 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001296 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001297 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001298 return EGL_TRUE;
1299 }
1300 }
1301
1302 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001304 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001305 // make sure the EGLContext and EGLSurface passed in are for
1306 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001307 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001309 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001311
1312 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001314 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001315 // make sure the EGLContext and EGLSurface passed in are for
1316 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001317 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001319 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001321
1322 EGLBoolean result;
1323
1324 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001325 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001326 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001327 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001328 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001329 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001330 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331
1332 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001333
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001334 loseCurrent(cur_c);
1335
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001336 if (ctx != EGL_NO_CONTEXT) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001337 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001338 setContext(ctx);
1339 _c.acquire();
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001340 _r.acquire();
1341 _d.acquire();
1342 c->read = read;
1343 c->draw = draw;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001344 } else {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001345 setGLHooksThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001346 setContext(EGL_NO_CONTEXT);
1347 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 }
1349 return result;
1350}
1351
1352
1353EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1354 EGLint attribute, EGLint *value)
1355{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001356 ContextRef _c(ctx);
1357 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 if (!validate_display_context(dpy, ctx))
1360 return EGL_FALSE;
1361
1362 egl_display_t const * const dp = get_display(dpy);
1363 egl_context_t * const c = get_context(ctx);
1364
Mathias Agopiancee79392010-07-26 21:14:59 -07001365 EGLBoolean result(EGL_TRUE);
1366 if (attribute == EGL_CONFIG_ID) {
1367 *value = dp->configs[intptr_t(c->config)].configId;
1368 } else {
1369 // We need to remap EGL_CONFIG_IDs
1370 result = c->cnx->egl.eglQueryContext(
1371 dp->disp[c->impl].dpy, c->context, attribute, value);
1372 }
1373
1374 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375}
1376
1377EGLContext eglGetCurrentContext(void)
1378{
Mathias Agopian923c6612009-08-17 18:07:06 -07001379 // could be called before eglInitialize(), but we wouldn't have a context
1380 // then, and this function would correctly return EGL_NO_CONTEXT.
1381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 EGLContext ctx = getContext();
1383 return ctx;
1384}
1385
1386EGLSurface eglGetCurrentSurface(EGLint readdraw)
1387{
Mathias Agopian923c6612009-08-17 18:07:06 -07001388 // could be called before eglInitialize(), but we wouldn't have a context
1389 // then, and this function would correctly return EGL_NO_SURFACE.
1390
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391 EGLContext ctx = getContext();
1392 if (ctx) {
1393 egl_context_t const * const c = get_context(ctx);
1394 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1395 switch (readdraw) {
1396 case EGL_READ: return c->read;
1397 case EGL_DRAW: return c->draw;
1398 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1399 }
1400 }
1401 return EGL_NO_SURFACE;
1402}
1403
1404EGLDisplay eglGetCurrentDisplay(void)
1405{
Mathias Agopian923c6612009-08-17 18:07:06 -07001406 // could be called before eglInitialize(), but we wouldn't have a context
1407 // then, and this function would correctly return EGL_NO_DISPLAY.
1408
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 EGLContext ctx = getContext();
1410 if (ctx) {
1411 egl_context_t const * const c = get_context(ctx);
1412 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1413 return c->dpy;
1414 }
1415 return EGL_NO_DISPLAY;
1416}
1417
1418EGLBoolean eglWaitGL(void)
1419{
Mathias Agopian923c6612009-08-17 18:07:06 -07001420 // could be called before eglInitialize(), but we wouldn't have a context
1421 // then, and this function would return GL_TRUE, which isn't wrong.
1422
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 EGLBoolean res = EGL_TRUE;
1424 EGLContext ctx = getContext();
1425 if (ctx) {
1426 egl_context_t const * const c = get_context(ctx);
1427 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1428 if (uint32_t(c->impl)>=2)
1429 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1430 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1431 if (!cnx->dso)
1432 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001433 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434 }
1435 return res;
1436}
1437
1438EGLBoolean eglWaitNative(EGLint engine)
1439{
Mathias Agopian923c6612009-08-17 18:07:06 -07001440 // could be called before eglInitialize(), but we wouldn't have a context
1441 // then, and this function would return GL_TRUE, which isn't wrong.
1442
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 EGLBoolean res = EGL_TRUE;
1444 EGLContext ctx = getContext();
1445 if (ctx) {
1446 egl_context_t const * const c = get_context(ctx);
1447 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1448 if (uint32_t(c->impl)>=2)
1449 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1450 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1451 if (!cnx->dso)
1452 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001453 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454 }
1455 return res;
1456}
1457
1458EGLint eglGetError(void)
1459{
1460 EGLint result = EGL_SUCCESS;
Mathias Agopian02dafb52010-09-21 15:43:59 -07001461 EGLint err;
Mathias Agopian618fa102009-10-14 02:06:37 -07001462 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian02dafb52010-09-21 15:43:59 -07001463 err = EGL_SUCCESS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 egl_connection_t* const cnx = &gEGLImpl[i];
1465 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001466 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1468 result = err;
1469 }
Mathias Agopian02dafb52010-09-21 15:43:59 -07001470 err = getError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 if (result == EGL_SUCCESS)
Mathias Agopian02dafb52010-09-21 15:43:59 -07001472 result = err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 return result;
1474}
1475
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001476// Note: Similar implementations of these functions also exist in
1477// gl2.cpp and gl.cpp, and are used by applications that call the
1478// exported entry points directly.
1479typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
1480typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
1481
1482static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL;
1483static PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL;
1484
1485static void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image)
1486{
1487 GLeglImageOES implImage =
1488 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1489 glEGLImageTargetTexture2DOES_impl(target, implImage);
1490}
1491
1492static void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image)
1493{
1494 GLeglImageOES implImage =
1495 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1496 glEGLImageTargetRenderbufferStorageOES_impl(target, implImage);
1497}
1498
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001499__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001501 // eglGetProcAddress() could be the very first function called
1502 // in which case we must make sure we've initialized ourselves, this
1503 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001504
1505 if (egl_init_drivers() == EGL_FALSE) {
1506 setError(EGL_BAD_PARAMETER, NULL);
1507 return NULL;
1508 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001509
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 __eglMustCastToProperFunctionPointerType addr;
1511 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1512 if (addr) return addr;
1513
Mathias Agopian24035332010-08-02 17:34:32 -07001514 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1515 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516
Mathias Agopian24035332010-08-02 17:34:32 -07001517 /*
1518 * Since eglGetProcAddress() is not associated to anything, it needs
1519 * to return a function pointer that "works" regardless of what
1520 * the current context is.
1521 *
1522 * For this reason, we return a "forwarder", a small stub that takes
1523 * care of calling the function associated with the context
1524 * currently bound.
1525 *
1526 * We first look for extensions we've already resolved, if we're seeing
1527 * this extension for the first time, we go through all our
1528 * implementations and call eglGetProcAddress() and record the
1529 * result in the appropriate implementation hooks and return the
1530 * address of the forwarder corresponding to that hook set.
1531 *
1532 */
1533
1534 const String8 name(procname);
1535 addr = gGLExtentionMap.valueFor(name);
1536 const int slot = gGLExtentionSlot;
1537
1538 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1539 "no more slots for eglGetProcAddress(\"%s\")",
1540 procname);
1541
1542 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1543 bool found = false;
1544 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1545 egl_connection_t* const cnx = &gEGLImpl[i];
1546 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1547 found = true;
Mathias Agopian4a88b522010-08-13 12:19:04 -07001548 // Extensions are independent of the bound context
1549 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1550 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001551#if EGL_TRACE
1552 gHooksTrace.ext.extensions[slot] =
1553#endif
Mathias Agopian24035332010-08-02 17:34:32 -07001554 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555 }
1556 }
Mathias Agopian24035332010-08-02 17:34:32 -07001557 if (found) {
1558 addr = gExtensionForwarders[slot];
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001559
1560 if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) {
1561 glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr;
1562 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper;
1563 }
1564 if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) {
1565 glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr;
1566 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper;
1567 }
1568
Mathias Agopian24035332010-08-02 17:34:32 -07001569 gGLExtentionMap.add(name, addr);
1570 gGLExtentionSlot++;
1571 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573
Mathias Agopian24035332010-08-02 17:34:32 -07001574 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian24035332010-08-02 17:34:32 -07001575 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576}
1577
1578EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1579{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001580 SurfaceRef _s(draw);
1581 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583 if (!validate_display_surface(dpy, draw))
1584 return EGL_FALSE;
1585 egl_display_t const * const dp = get_display(dpy);
1586 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001587 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588}
1589
1590EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1591 NativePixmapType target)
1592{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001593 SurfaceRef _s(surface);
1594 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1595
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596 if (!validate_display_surface(dpy, surface))
1597 return EGL_FALSE;
1598 egl_display_t const * const dp = get_display(dpy);
1599 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001600 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001601 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602}
1603
1604const char* eglQueryString(EGLDisplay dpy, EGLint name)
1605{
1606 egl_display_t const * const dp = get_display(dpy);
1607 switch (name) {
1608 case EGL_VENDOR:
1609 return gVendorString;
1610 case EGL_VERSION:
1611 return gVersionString;
1612 case EGL_EXTENSIONS:
1613 return gExtensionString;
1614 case EGL_CLIENT_APIS:
1615 return gClientApiString;
1616 }
1617 return setError(EGL_BAD_PARAMETER, (const char *)0);
1618}
1619
1620
1621// ----------------------------------------------------------------------------
1622// EGL 1.1
1623// ----------------------------------------------------------------------------
1624
1625EGLBoolean eglSurfaceAttrib(
1626 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1627{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001628 SurfaceRef _s(surface);
1629 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1630
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631 if (!validate_display_surface(dpy, surface))
1632 return EGL_FALSE;
1633 egl_display_t const * const dp = get_display(dpy);
1634 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001635 if (s->cnx->egl.eglSurfaceAttrib) {
1636 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001637 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 }
1639 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1640}
1641
1642EGLBoolean eglBindTexImage(
1643 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1644{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001645 SurfaceRef _s(surface);
1646 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648 if (!validate_display_surface(dpy, surface))
1649 return EGL_FALSE;
1650 egl_display_t const * const dp = get_display(dpy);
1651 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001652 if (s->cnx->egl.eglBindTexImage) {
1653 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001654 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655 }
1656 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1657}
1658
1659EGLBoolean eglReleaseTexImage(
1660 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1661{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001662 SurfaceRef _s(surface);
1663 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1664
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665 if (!validate_display_surface(dpy, surface))
1666 return EGL_FALSE;
1667 egl_display_t const * const dp = get_display(dpy);
1668 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001669 if (s->cnx->egl.eglReleaseTexImage) {
1670 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001671 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672 }
1673 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1674}
1675
1676EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1677{
1678 egl_display_t * const dp = get_display(dpy);
1679 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1680
1681 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001682 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 egl_connection_t* const cnx = &gEGLImpl[i];
1684 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001685 if (cnx->egl.eglSwapInterval) {
1686 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001687 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001688 res = EGL_FALSE;
1689 }
1690 }
1691 }
1692 }
1693 return res;
1694}
1695
1696
1697// ----------------------------------------------------------------------------
1698// EGL 1.2
1699// ----------------------------------------------------------------------------
1700
1701EGLBoolean eglWaitClient(void)
1702{
Mathias Agopian923c6612009-08-17 18:07:06 -07001703 // could be called before eglInitialize(), but we wouldn't have a context
1704 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 EGLBoolean res = EGL_TRUE;
1706 EGLContext ctx = getContext();
1707 if (ctx) {
1708 egl_context_t const * const c = get_context(ctx);
1709 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1710 if (uint32_t(c->impl)>=2)
1711 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1712 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1713 if (!cnx->dso)
1714 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001715 if (cnx->egl.eglWaitClient) {
1716 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001718 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719 }
1720 }
1721 return res;
1722}
1723
1724EGLBoolean eglBindAPI(EGLenum api)
1725{
Mathias Agopian923c6612009-08-17 18:07:06 -07001726 if (egl_init_drivers() == EGL_FALSE) {
1727 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1728 }
1729
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730 // bind this API on all EGLs
1731 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001732 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 egl_connection_t* const cnx = &gEGLImpl[i];
1734 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001735 if (cnx->egl.eglBindAPI) {
1736 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 res = EGL_FALSE;
1738 }
1739 }
1740 }
1741 }
1742 return res;
1743}
1744
1745EGLenum eglQueryAPI(void)
1746{
Mathias Agopian923c6612009-08-17 18:07:06 -07001747 if (egl_init_drivers() == EGL_FALSE) {
1748 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1749 }
1750
Mathias Agopian618fa102009-10-14 02:06:37 -07001751 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001752 egl_connection_t* const cnx = &gEGLImpl[i];
1753 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001754 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 // the first one we find is okay, because they all
1756 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001757 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758 }
1759 }
1760 }
1761 // or, it can only be OpenGL ES
1762 return EGL_OPENGL_ES_API;
1763}
1764
1765EGLBoolean eglReleaseThread(void)
1766{
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001767 // If there is context bound to the thread, release it
1768 loseCurrent(get_context(getContext()));
1769
Mathias Agopian618fa102009-10-14 02:06:37 -07001770 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 egl_connection_t* const cnx = &gEGLImpl[i];
1772 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001773 if (cnx->egl.eglReleaseThread) {
1774 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001775 }
1776 }
1777 }
1778 clearTLS();
1779 return EGL_TRUE;
1780}
1781
1782EGLSurface eglCreatePbufferFromClientBuffer(
1783 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1784 EGLConfig config, const EGLint *attrib_list)
1785{
1786 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001787 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001789 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1790 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001791 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1792 buftype, buffer,
1793 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794 }
1795 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1796}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001797
1798// ----------------------------------------------------------------------------
1799// EGL_EGLEXT_VERSION 3
1800// ----------------------------------------------------------------------------
1801
1802EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1803 const EGLint *attrib_list)
1804{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001805 SurfaceRef _s(surface);
1806 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1807
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001808 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001809 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001810
1811 egl_display_t const * const dp = get_display(dpy);
1812 egl_surface_t const * const s = get_surface(surface);
1813
Mathias Agopian618fa102009-10-14 02:06:37 -07001814 if (s->cnx->egl.eglLockSurfaceKHR) {
1815 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001816 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001817 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001818 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001819}
1820
1821EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1822{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001823 SurfaceRef _s(surface);
1824 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1825
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001826 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001827 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001828
1829 egl_display_t const * const dp = get_display(dpy);
1830 egl_surface_t const * const s = get_surface(surface);
1831
Mathias Agopian618fa102009-10-14 02:06:37 -07001832 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1833 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001834 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001835 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001836 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001837}
1838
1839EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1840 EGLClientBuffer buffer, const EGLint *attrib_list)
1841{
1842 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001843 ContextRef _c(ctx);
1844 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001845 if (!validate_display_context(dpy, ctx))
1846 return EGL_NO_IMAGE_KHR;
1847 egl_display_t const * const dp = get_display(dpy);
1848 egl_context_t * const c = get_context(ctx);
1849 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001850 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001851 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001852 if (image == EGL_NO_IMAGE_KHR)
1853 return image;
1854
1855 egl_image_t* result = new egl_image_t(dpy, ctx);
1856 result->images[c->impl] = image;
1857 return (EGLImageKHR)result;
1858 } else {
1859 // EGL_NO_CONTEXT is a valid parameter
1860 egl_display_t const * const dp = get_display(dpy);
1861 if (dp == 0) {
1862 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1863 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001864
1865 /* Since we don't have a way to know which implementation to call,
1866 * we're calling all of them. If at least one of the implementation
1867 * succeeded, this is a success.
1868 */
1869
1870 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001871
Mathias Agopian618fa102009-10-14 02:06:37 -07001872 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001873 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001874 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001875 egl_connection_t* const cnx = &gEGLImpl[i];
1876 implImages[i] = EGL_NO_IMAGE_KHR;
1877 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001878 if (cnx->egl.eglCreateImageKHR) {
1879 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001880 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001881 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1882 success = true;
1883 }
1884 }
1885 }
1886 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001887
1888 if (!success) {
1889 // failure, if there was an error when we entered this function,
1890 // the error flag must not be updated.
1891 // Otherwise, the error is whatever happened in the implementation
1892 // that faulted.
1893 if (currentError != EGL_SUCCESS) {
1894 setError(currentError, EGL_NO_IMAGE_KHR);
1895 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001896 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001897 } else {
1898 // In case of success, we need to clear all error flags
1899 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07001900 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001901 // a "full" success (all implementation succeeded).
1902 eglGetError();
1903 }
1904
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001905 egl_image_t* result = new egl_image_t(dpy, ctx);
1906 memcpy(result->images, implImages, sizeof(implImages));
1907 return (EGLImageKHR)result;
1908 }
1909}
1910
1911EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1912{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001913 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001914 if (dp == 0) {
1915 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1916 }
1917
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001918 ImageRef _i(img);
1919 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001920
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001921 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001922 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001923 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001924 egl_connection_t* const cnx = &gEGLImpl[i];
1925 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1926 if (cnx->dso) {
Mathias Agopian77fbf8d2010-09-09 11:12:54 -07001927 if (cnx->egl.eglDestroyImageKHR) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001928 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001929 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001930 success = true;
1931 }
1932 }
1933 }
1934 }
1935 }
1936 if (!success)
1937 return EGL_FALSE;
1938
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001939 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001940
Mathias Agopian24e5f522009-08-12 21:18:15 -07001941 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001942}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07001943
Mathias Agopianc291f5852010-08-27 16:48:56 -07001944// ----------------------------------------------------------------------------
1945// EGL_EGLEXT_VERSION 5
1946// ----------------------------------------------------------------------------
1947
1948
1949EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1950{
1951 EGLContext ctx = eglGetCurrentContext();
1952 ContextRef _c(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07001953 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
Mathias Agopianc291f5852010-08-27 16:48:56 -07001954 if (!validate_display_context(dpy, ctx))
Mathias Agopiana93b9572010-09-21 16:22:10 -07001955 return EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07001956 egl_display_t const * const dp = get_display(dpy);
1957 egl_context_t * const c = get_context(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07001958 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07001959 if (c->cnx->egl.eglCreateSyncKHR) {
1960 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
1961 dp->disp[c->impl].dpy, type, attrib_list);
Mathias Agopiana93b9572010-09-21 16:22:10 -07001962 if (sync == EGL_NO_SYNC_KHR)
Mathias Agopianc291f5852010-08-27 16:48:56 -07001963 return sync;
1964 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
1965 }
1966 return (EGLSyncKHR)result;
1967}
1968
1969EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1970{
1971 egl_display_t const * const dp = get_display(dpy);
1972 if (dp == 0) {
1973 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1974 }
1975
1976 SyncRef _s(sync);
1977 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1978 egl_sync_t* syncObject = get_sync(sync);
1979
1980 EGLContext ctx = syncObject->context;
1981 ContextRef _c(ctx);
1982 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1983 if (!validate_display_context(dpy, ctx))
1984 return EGL_FALSE;
1985
1986 egl_context_t * const c = get_context(ctx);
1987
1988 if (c->cnx->egl.eglDestroySyncKHR) {
1989 return c->cnx->egl.eglDestroySyncKHR(
1990 dp->disp[c->impl].dpy, syncObject->sync);
1991 }
1992
1993 return EGL_FALSE;
1994}
1995
1996EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
1997{
1998 egl_display_t const * const dp = get_display(dpy);
1999 if (dp == 0) {
2000 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2001 }
2002
2003 SyncRef _s(sync);
2004 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2005 egl_sync_t* syncObject = get_sync(sync);
2006
2007 EGLContext ctx = syncObject->context;
2008 ContextRef _c(ctx);
2009 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2010 if (!validate_display_context(dpy, ctx))
2011 return EGL_FALSE;
2012
2013 egl_context_t * const c = get_context(ctx);
2014
2015 if (c->cnx->egl.eglClientWaitSyncKHR) {
2016 return c->cnx->egl.eglClientWaitSyncKHR(
2017 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
2018 }
2019
2020 return EGL_FALSE;
2021}
2022
2023EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
2024{
2025 egl_display_t const * const dp = get_display(dpy);
2026 if (dp == 0) {
2027 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2028 }
2029
2030 SyncRef _s(sync);
2031 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2032 egl_sync_t* syncObject = get_sync(sync);
2033
2034 EGLContext ctx = syncObject->context;
2035 ContextRef _c(ctx);
2036 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2037 if (!validate_display_context(dpy, ctx))
2038 return EGL_FALSE;
2039
2040 egl_context_t * const c = get_context(ctx);
2041
2042 if (c->cnx->egl.eglGetSyncAttribKHR) {
2043 return c->cnx->egl.eglGetSyncAttribKHR(
2044 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
2045 }
2046
2047 return EGL_FALSE;
2048}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002049
2050// ----------------------------------------------------------------------------
2051// ANDROID extensions
2052// ----------------------------------------------------------------------------
2053
2054EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2055 EGLint left, EGLint top, EGLint width, EGLint height)
2056{
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002057 SurfaceRef _s(draw);
2058 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
2059
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002060 if (!validate_display_surface(dpy, draw))
2061 return EGL_FALSE;
2062 egl_display_t const * const dp = get_display(dpy);
2063 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07002064 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
2065 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002066 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002067 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07002068 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002069}