blob: 7183e83fa1e684672ea2a768461b966801392034 [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
Kenny Rootaf1cf072011-02-16 10:13:53 -080025#ifdef HAVE_ANDROID_OS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#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"
David Li65948aa2011-03-10 16:40:37 -080048#include "glesv2dbg.h"
David Li864f8392011-03-28 10:39:28 -070049#include "egl_tls.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
52
53// ----------------------------------------------------------------------------
54namespace android {
55// ----------------------------------------------------------------------------
56
57#define VERSION_MAJOR 1
58#define VERSION_MINOR 4
59static char const * const gVendorString = "Android";
Mathias Agopian923c6612009-08-17 18:07:06 -070060static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061static char const * const gClientApiString = "OpenGL ES";
David Li864f8392011-03-28 10:39:28 -070062static char const * const gExtensionString =
Mathias Agopian076b1cc2009-04-10 14:24:30 -070063 "EGL_KHR_image "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -070064 "EGL_KHR_image_base "
65 "EGL_KHR_image_pixmap "
Mathias Agopian8e4b5a32010-08-27 16:08:03 -070066 "EGL_KHR_gl_texture_2D_image "
Michael I. Goldca41e362011-01-13 10:13:15 -080067 "EGL_KHR_gl_texture_cubemap_image "
68 "EGL_KHR_gl_renderbuffer_image "
Mathias Agopianc291f5852010-08-27 16:48:56 -070069 "EGL_KHR_fence_sync "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070071 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072 ;
73
74// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Eric Hassold11c01562011-03-25 11:26:02 -070076class egl_object_t;
77struct egl_display_t;
78static egl_display_t* get_display(EGLDisplay dpy);
Mathias Agopian9429e9c2009-08-21 02:18:25 -070079
Eric Hassold11c01562011-03-25 11:26:02 -070080struct egl_config_t {
81 egl_config_t() {}
82 egl_config_t(int impl, EGLConfig config)
83 : impl(impl), config(config), configId(0), implConfigId(0) { }
84 int impl; // the implementation this config is for
85 EGLConfig config; // the implementation's EGLConfig
86 EGLint configId; // our CONFIG_ID
87 EGLint implConfigId; // the implementation's CONFIG_ID
88 inline bool operator < (const egl_config_t& rhs) const {
89 if (impl < rhs.impl) return true;
90 if (impl > rhs.impl) return false;
91 return config < rhs.config;
92 }
93};
94
95struct egl_display_t {
96 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
97
98 struct strings_t {
99 char const * vendor;
100 char const * version;
101 char const * clientApi;
102 char const * extensions;
103 };
104
105 struct DisplayImpl {
106 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
107 state(NOT_INITIALIZED), numConfigs(0) { }
108 EGLDisplay dpy;
109 EGLConfig* config;
110 EGLint state;
111 EGLint numConfigs;
112 strings_t queryString;
113 };
114
115 uint32_t magic;
116 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
117 EGLint numTotalConfigs;
118 egl_config_t* configs;
119 uint32_t refs;
120 Mutex lock;
121
122 SortedVector<egl_object_t*> objects;
123
Eric Hassold7fecf8c2011-03-31 16:52:02 -0700124 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0), refs(0) { }
Eric Hassold11c01562011-03-25 11:26:02 -0700125 ~egl_display_t() { magic = 0; }
126 inline bool isReady() const { return (refs > 0); }
127 inline bool isValid() const { return magic == '_dpy'; }
128 inline bool isAlive() const { return isValid(); }
129};
130
131class egl_object_t {
132 egl_display_t *display;
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700133 volatile int32_t terminated;
134 mutable volatile int32_t count;
135
136public:
Eric Hassold11c01562011-03-25 11:26:02 -0700137 egl_object_t(EGLDisplay dpy) : display(get_display(dpy)), terminated(0), count(1) {
138 Mutex::Autolock _l(display->lock);
139 display->objects.add(this);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700140 }
141
142 inline bool isAlive() const { return !terminated; }
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700145 bool get() {
Eric Hassold11c01562011-03-25 11:26:02 -0700146 Mutex::Autolock _l(display->lock);
147 if (display->objects.indexOf(this) >= 0) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700148 android_atomic_inc(&count);
149 return true;
150 }
151 return false;
152 }
153
154 bool put() {
Eric Hassold11c01562011-03-25 11:26:02 -0700155 Mutex::Autolock _l(display->lock);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700156 if (android_atomic_dec(&count) == 1) {
Eric Hassold11c01562011-03-25 11:26:02 -0700157 display->objects.remove(this);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700158 return true;
159 }
160 return false;
161 }
162
163public:
164 template <typename N, typename T>
165 struct LocalRef {
166 N* ref;
167 LocalRef(T o) : ref(0) {
168 N* native = reinterpret_cast<N*>(o);
169 if (o && native->get()) {
170 ref = native;
171 }
172 }
173 ~LocalRef() {
174 if (ref && ref->put()) {
175 delete ref;
176 }
177 }
178 inline N* get() {
179 return ref;
180 }
181 void acquire() const {
182 if (ref) {
183 android_atomic_inc(&ref->count);
184 }
185 }
186 void release() const {
187 if (ref) {
188 int32_t c = android_atomic_dec(&ref->count);
189 // ref->count cannot be 1 prior atomic_dec because we have
190 // a reference, and if we have one, it means there was
191 // already one before us.
192 LOGE_IF(c==1, "refcount is now 0 in release()");
193 }
194 }
195 void terminate() {
196 if (ref) {
197 ref->terminated = 1;
198 release();
199 }
200 }
201 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202};
203
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700204struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700206 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
207
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800208 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win,
209 EGLSurface surface, int impl, egl_connection_t const* cnx)
Eric Hassold11c01562011-03-25 11:26:02 -0700210 : egl_object_t(dpy), dpy(dpy), surface(surface), config(config), win(win), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 }
212 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 }
214 EGLDisplay dpy;
215 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700216 EGLConfig config;
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800217 sp<ANativeWindow> win;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 int impl;
219 egl_connection_t const* cnx;
220};
221
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700222struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700224 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
David Li864f8392011-03-28 10:39:28 -0700225
Mathias Agopiancee79392010-07-26 21:14:59 -0700226 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
David Li864f8392011-03-28 10:39:28 -0700227 int impl, egl_connection_t const* cnx, int version)
228 : egl_object_t(dpy), dpy(dpy), context(context), config(config), read(0), draw(0),
229 impl(impl), cnx(cnx), version(version)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 {
231 }
David Li65948aa2011-03-10 16:40:37 -0800232 ~egl_context_t()
233 {
David Li65948aa2011-03-10 16:40:37 -0800234 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 EGLDisplay dpy;
236 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700237 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 EGLSurface read;
239 EGLSurface draw;
240 int impl;
241 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700242 int version;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243};
244
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700245struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700246{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700247 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
248
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249 egl_image_t(EGLDisplay dpy, EGLContext context)
Eric Hassold11c01562011-03-25 11:26:02 -0700250 : egl_object_t(dpy), dpy(dpy), context(context)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251 {
252 memset(images, 0, sizeof(images));
253 }
254 EGLDisplay dpy;
Mathias Agopian77fbf8d2010-09-09 11:12:54 -0700255 EGLContext context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700256 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700257};
258
Mathias Agopianc291f5852010-08-27 16:48:56 -0700259struct egl_sync_t : public egl_object_t
260{
261 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
262
263 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync)
Eric Hassold11c01562011-03-25 11:26:02 -0700264 : egl_object_t(dpy), dpy(dpy), context(context), sync(sync)
Mathias Agopianc291f5852010-08-27 16:48:56 -0700265 {
266 }
267 EGLDisplay dpy;
268 EGLContext context;
269 EGLSyncKHR sync;
270};
271
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700272typedef egl_surface_t::Ref SurfaceRef;
273typedef egl_context_t::Ref ContextRef;
274typedef egl_image_t::Ref ImageRef;
Mathias Agopianc291f5852010-08-27 16:48:56 -0700275typedef egl_sync_t::Ref SyncRef;
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277// ----------------------------------------------------------------------------
278
Mathias Agopianbf41b112010-04-09 13:37:34 -0700279static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280static egl_display_t gDisplay[NUM_DISPLAYS];
281static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
282static pthread_key_t gEGLThreadLocalStorageKey = -1;
283
284// ----------------------------------------------------------------------------
285
Mathias Agopian618fa102009-10-14 02:06:37 -0700286EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
287EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700288EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800289
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700290#if EGL_TRACE
291
292EGLAPI pthread_key_t gGLTraceKey = -1;
293
294// ----------------------------------------------------------------------------
295
David Li2f5a6552011-03-01 16:08:10 -0800296static int gEGLTraceLevel, gEGLDebugLevel;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700297static int gEGLApplicationTraceLevel;
David Li2f5a6552011-03-01 16:08:10 -0800298extern EGLAPI gl_hooks_t gHooksTrace, gHooksDebug;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700299
300static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
301 pthread_setspecific(gGLTraceKey, value);
302}
303
304gl_hooks_t const* getGLTraceThreadSpecific() {
305 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
306}
307
308static void initEglTraceLevel() {
309 char value[PROPERTY_VALUE_MAX];
310 property_get("debug.egl.trace", value, "0");
311 int propertyLevel = atoi(value);
312 int applicationLevel = gEGLApplicationTraceLevel;
313 gEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li2f5a6552011-03-01 16:08:10 -0800314
315 property_get("debug.egl.debug_proc", value, "");
316 long pid = getpid();
317 char procPath[128] = {};
318 sprintf(procPath, "/proc/%ld/cmdline", pid);
319 FILE * file = fopen(procPath, "r");
320 if (file)
321 {
322 char cmdline[256] = {};
323 if (fgets(cmdline, sizeof(cmdline) - 1, file))
324 {
David Li2f5a6552011-03-01 16:08:10 -0800325 if (!strcmp(value, cmdline))
326 gEGLDebugLevel = 1;
327 }
328 fclose(file);
329 }
330
David Li2f5a6552011-03-01 16:08:10 -0800331 if (gEGLDebugLevel > 0)
David Li85f33a72011-03-10 19:07:42 -0800332 {
333 property_get("debug.egl.debug_port", value, "5039");
334 StartDebugServer(atoi(value));
335 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700336}
337
338static void setGLHooksThreadSpecific(gl_hooks_t const *value) {
339 if (gEGLTraceLevel > 0) {
340 setGlTraceThreadSpecific(value);
341 setGlThreadSpecific(&gHooksTrace);
David Li65948aa2011-03-10 16:40:37 -0800342 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800343 setGlTraceThreadSpecific(value);
344 setGlThreadSpecific(&gHooksDebug);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700345 } else {
346 setGlThreadSpecific(value);
347 }
348}
349
350/*
351 * Global entry point to allow applications to modify their own trace level.
352 * The effective trace level is the max of this level and the value of debug.egl.trace.
353 */
354extern "C"
355void setGLTraceLevel(int level) {
356 gEGLApplicationTraceLevel = level;
357}
358
359#else
360
361static inline void setGLHooksThreadSpecific(gl_hooks_t const *value) {
362 setGlThreadSpecific(value);
363}
364
365#endif
366
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367// ----------------------------------------------------------------------------
368
369static __attribute__((noinline))
370const char *egl_strerror(EGLint err)
371{
372 switch (err){
373 case EGL_SUCCESS: return "EGL_SUCCESS";
374 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
375 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
376 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
377 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
378 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
379 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
380 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
381 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
382 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
383 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
384 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
385 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
386 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
387 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
388 default: return "UNKNOWN";
389 }
390}
391
392static __attribute__((noinline))
393void clearTLS() {
394 if (gEGLThreadLocalStorageKey != -1) {
395 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
396 if (tls) {
397 delete tls;
398 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
399 }
400 }
401}
402
403static tls_t* getTLS()
404{
405 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
406 if (tls == 0) {
407 tls = new tls_t;
408 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
409 }
410 return tls;
411}
412
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800413static inline void clearError() {
Jamie Gennisf1cde8e2011-01-30 16:50:04 -0800414 // This must clear the error from all the underlying EGL implementations as
415 // well as the EGL wrapper layer.
416 eglGetError();
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800417}
418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419template<typename T>
420static __attribute__((noinline))
421T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
422 if (gEGLThreadLocalStorageKey == -1) {
423 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
424 if (gEGLThreadLocalStorageKey == -1)
425 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
426 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
427 }
428 tls_t* tls = getTLS();
429 if (tls->error != error) {
430 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
431 tls->error = error;
432 }
433 return returnValue;
434}
435
436static __attribute__((noinline))
437GLint getError() {
438 if (gEGLThreadLocalStorageKey == -1)
439 return EGL_SUCCESS;
440 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
441 if (!tls) return EGL_SUCCESS;
442 GLint error = tls->error;
443 tls->error = EGL_SUCCESS;
444 return error;
445}
446
447static __attribute__((noinline))
448void setContext(EGLContext ctx) {
449 if (gEGLThreadLocalStorageKey == -1) {
450 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
451 if (gEGLThreadLocalStorageKey == -1)
452 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
453 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
454 }
455 tls_t* tls = getTLS();
456 tls->ctx = ctx;
457}
458
459static __attribute__((noinline))
460EGLContext getContext() {
461 if (gEGLThreadLocalStorageKey == -1)
462 return EGL_NO_CONTEXT;
463 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
464 if (!tls) return EGL_NO_CONTEXT;
465 return tls->ctx;
466}
467
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800468/*****************************************************************************/
469
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470template<typename T>
471static __attribute__((noinline))
472int binarySearch(
473 T const sortedArray[], int first, int last, T key)
474{
475 while (first <= last) {
476 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700477 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 first = mid + 1;
479 } else if (key < sortedArray[mid]) {
480 last = mid - 1;
481 } else {
482 return mid;
483 }
484 }
485 return -1;
486}
487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488static int cmp_configs(const void* a, const void *b)
489{
Mathias Agopiancee79392010-07-26 21:14:59 -0700490 const egl_config_t& c0 = *(egl_config_t const *)a;
491 const egl_config_t& c1 = *(egl_config_t const *)b;
492 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493}
494
495struct extention_map_t {
496 const char* name;
497 __eglMustCastToProperFunctionPointerType address;
498};
499
500static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700501 { "eglLockSurfaceKHR",
502 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
503 { "eglUnlockSurfaceKHR",
504 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
505 { "eglCreateImageKHR",
506 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
507 { "eglDestroyImageKHR",
508 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700509 { "eglSetSwapRectangleANDROID",
510 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511};
512
Mathias Agopian24035332010-08-02 17:34:32 -0700513extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
514
515// accesses protected by gInitDriverMutex
516static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
517static int gGLExtentionSlot = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518
519static void(*findProcAddress(const char* name,
520 const extention_map_t* map, size_t n))()
521{
522 for (uint32_t i=0 ; i<n ; i++) {
523 if (!strcmp(name, map[i].name)) {
524 return map[i].address;
525 }
526 }
527 return NULL;
528}
529
530// ----------------------------------------------------------------------------
531
Mathias Agopian6f087122010-09-23 16:38:38 -0700532static int gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700533 tls_t* tls = getTLS();
534 if (tls->logCallWithNoContext == EGL_TRUE) {
535 tls->logCallWithNoContext = EGL_FALSE;
536 LOGE("call to OpenGL ES API with no current context "
537 "(logged once per thread)");
538 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700539 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700540}
541
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542static void early_egl_init(void)
543{
544#if !USE_FAST_TLS_KEY
545 pthread_key_create(&gGLWrapperKey, NULL);
546#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700547#if EGL_TRACE
548 pthread_key_create(&gGLTraceKey, NULL);
549 initEglTraceLevel();
550#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551 uint32_t addr = (uint32_t)((void*)gl_no_context);
552 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700553 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700555 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700556
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700557 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558}
559
560static pthread_once_t once_control = PTHREAD_ONCE_INIT;
561static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
562
563
564static inline
565egl_display_t* get_display(EGLDisplay dpy)
566{
567 uintptr_t index = uintptr_t(dpy)-1U;
568 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
569}
570
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700571template<typename NATIVE, typename EGL>
572static inline NATIVE* egl_to_native_cast(EGL arg) {
573 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574}
575
576static inline
David Li864f8392011-03-28 10:39:28 -0700577egl_surface_t* get_surface(EGLSurface surface) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700578 return egl_to_native_cast<egl_surface_t>(surface);
579}
580
581static inline
582egl_context_t* get_context(EGLContext context) {
583 return egl_to_native_cast<egl_context_t>(context);
584}
585
586static inline
587egl_image_t* get_image(EGLImageKHR image) {
588 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589}
590
Mathias Agopianc291f5852010-08-27 16:48:56 -0700591static inline
592egl_sync_t* get_sync(EGLSyncKHR sync) {
593 return egl_to_native_cast<egl_sync_t>(sync);
594}
595
Eric Hassold3ede7c12011-03-23 15:59:00 -0700596static inline
597egl_display_t* validate_display(EGLDisplay dpy)
598{
599 egl_display_t * const dp = get_display(dpy);
600 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_display_t*)NULL);
601 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (egl_display_t*)NULL);
602
603 return dp;
604}
605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606static egl_connection_t* validate_display_config(
607 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700608 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700610 dp = validate_display(dpy);
611 if (!dp) return (egl_connection_t*) NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612
Mathias Agopiancee79392010-07-26 21:14:59 -0700613 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
615 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700616 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800617 if (cnx->dso == 0) {
618 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
619 }
620 return cnx;
621}
622
623static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
624{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700625 egl_display_t const * const dp = validate_display(dpy);
626 if (!dp) return EGL_FALSE;
627 if (!dp->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700629 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
631 return EGL_TRUE;
632}
633
634static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
635{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700636 egl_display_t const * const dp = validate_display(dpy);
637 if (!dp) return EGL_FALSE;
638 if (!dp->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700640 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641 return setError(EGL_BAD_SURFACE, EGL_FALSE);
642 return EGL_TRUE;
643}
644
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700645EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
646{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700647 ImageRef _i(image);
648 if (!_i.get()) return EGL_NO_IMAGE_KHR;
649
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700650 EGLContext context = getContext();
651 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
652 return EGL_NO_IMAGE_KHR;
653
654 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700655 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700656 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700658 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700659 return i->images[c->impl];
660}
661
Mathias Agopian923c6612009-08-17 18:07:06 -0700662// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700663
Mathias Agopian923c6612009-08-17 18:07:06 -0700664// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700665// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700666// egl_init_drivers_locked()
667//
668static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
669
670EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800671{
672 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700673 // initialized by static ctor. should be set here.
674 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 }
676
Mathias Agopiande586972009-05-28 17:39:03 -0700677 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700678 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700679
Mathias Agopian923c6612009-08-17 18:07:06 -0700680 // dynamically load all our EGL implementations for all displays
681 // and retrieve the corresponding EGLDisplay
682 // if that fails, don't use this driver.
683 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
684 egl_connection_t* cnx;
685 egl_display_t* d = &gDisplay[0];
686
687 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700689 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
690 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
691 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700692 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700693 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700694 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700695 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700696 if (dpy == EGL_NO_DISPLAY) {
697 loader.close(cnx->dso);
698 cnx->dso = NULL;
699 }
700 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701 }
702
703 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700704 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 char value[PROPERTY_VALUE_MAX];
706 property_get("debug.egl.hw", value, "1");
707 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700708 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
709 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
710 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700711 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700712 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700713 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700714 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700715 if (dpy == EGL_NO_DISPLAY) {
716 loader.close(cnx->dso);
717 cnx->dso = NULL;
718 }
719 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 } else {
721 LOGD("3D hardware acceleration is disabled");
722 }
723 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700724
Mathias Agopian923c6612009-08-17 18:07:06 -0700725 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
726 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727 }
728
Mathias Agopian923c6612009-08-17 18:07:06 -0700729 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730}
731
Mathias Agopian923c6612009-08-17 18:07:06 -0700732EGLBoolean egl_init_drivers()
733{
734 EGLBoolean res;
735 pthread_mutex_lock(&gInitDriverMutex);
736 res = egl_init_drivers_locked();
737 pthread_mutex_unlock(&gInitDriverMutex);
738 return res;
739}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700740
741// ----------------------------------------------------------------------------
742}; // namespace android
743// ----------------------------------------------------------------------------
744
745using namespace android;
746
747EGLDisplay eglGetDisplay(NativeDisplayType display)
748{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800749 clearError();
750
Mathias Agopian923c6612009-08-17 18:07:06 -0700751 uint32_t index = uint32_t(display);
752 if (index >= NUM_DISPLAYS) {
753 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
754 }
755
756 if (egl_init_drivers() == EGL_FALSE) {
757 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
758 }
759
760 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
761 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700762}
763
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764// ----------------------------------------------------------------------------
765// Initialization
766// ----------------------------------------------------------------------------
767
768EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
769{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800770 clearError();
771
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800772 egl_display_t * const dp = get_display(dpy);
773 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
774
Mathias Agopian75bc2782010-02-05 16:17:01 -0800775 Mutex::Autolock _l(dp->lock);
776
777 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 if (major != NULL) *major = VERSION_MAJOR;
779 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700780 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781 return EGL_TRUE;
782 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700783
784#if EGL_TRACE
785
786 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
787 // the information from that call may be stale.)
788 initEglTraceLevel();
789
790#endif
791
792 setGLHooksThreadSpecific(&gHooksNoContext);
793
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 // initialize each EGL and
795 // build our own extension string first, based on the extension we know
796 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700797 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 egl_connection_t* const cnx = &gEGLImpl[i];
799 cnx->major = -1;
800 cnx->minor = -1;
801 if (!cnx->dso)
802 continue;
803
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700804#if defined(ADRENO130)
805#warning "Adreno-130 eglInitialize() workaround"
806 /*
807 * The ADRENO 130 driver returns a different EGLDisplay each time
808 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
809 * after eglTerminate() has been called, so that eglInitialize()
810 * cannot be called again. Therefore, we need to make sure to call
811 * eglGetDisplay() before calling eglInitialize();
812 */
813 if (i == IMPL_HARDWARE) {
814 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700815 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700816 }
817#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800818
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700819
820 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700821 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700823 // i, idpy, cnx->major, cnx->minor, cnx);
824
825 // display is now initialized
826 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800827
828 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700829 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700830 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700831 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700832 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700833 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700834 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700835 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700836 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800837
838 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700839 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700840 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841 }
842 }
843
844 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700845 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846 egl_connection_t* const cnx = &gEGLImpl[i];
847 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
848 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700849 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700850 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
851 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700852 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700853 dp->disp[i].dpy, dp->disp[i].config, n,
854 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856 dp->numTotalConfigs += n;
857 res = EGL_TRUE;
858 }
859 }
860 }
861 }
862 }
863
864 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700865 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
866 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
867 egl_connection_t* const cnx = &gEGLImpl[i];
868 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
869 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
870 dp->configs[k].impl = i;
871 dp->configs[k].config = dp->disp[i].config[j];
872 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
873 // store the implementation's CONFIG_ID
874 cnx->egl.eglGetConfigAttrib(
875 dp->disp[i].dpy,
876 dp->disp[i].config[j],
877 EGL_CONFIG_ID,
878 &dp->configs[k].implConfigId);
879 k++;
880 }
881 }
882 }
883
884 // sort our configurations so we can do binary-searches
885 qsort( dp->configs,
886 dp->numTotalConfigs,
887 sizeof(egl_config_t), cmp_configs);
888
Mathias Agopian75bc2782010-02-05 16:17:01 -0800889 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890 if (major != NULL) *major = VERSION_MAJOR;
891 if (minor != NULL) *minor = VERSION_MINOR;
892 return EGL_TRUE;
893 }
894 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
895}
896
897EGLBoolean eglTerminate(EGLDisplay dpy)
898{
Mathias Agopian923c6612009-08-17 18:07:06 -0700899 // NOTE: don't unload the drivers b/c some APIs can be called
900 // after eglTerminate() has been called. eglTerminate() only
901 // terminates an EGLDisplay, not a EGL itself.
902
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800903 clearError();
904
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 egl_display_t* const dp = get_display(dpy);
906 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800907
908 Mutex::Autolock _l(dp->lock);
909
910 if (dp->refs == 0) {
911 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
912 }
913
914 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700915 if (dp->refs > 1) {
916 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800917 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700918 }
Mathias Agopiande586972009-05-28 17:39:03 -0700919
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700921 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700923 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700924 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700925 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700926 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700927 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700928 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700929 free(dp->disp[i].config);
930
931 dp->disp[i].numConfigs = 0;
932 dp->disp[i].config = 0;
933 dp->disp[i].state = egl_display_t::TERMINATED;
934
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 res = EGL_TRUE;
936 }
937 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700938
939 // TODO: all egl_object_t should be marked for termination
940
Mathias Agopian75bc2782010-02-05 16:17:01 -0800941 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700943 delete [] dp->configs;
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -0800944
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945 return res;
946}
947
948// ----------------------------------------------------------------------------
949// configuration
950// ----------------------------------------------------------------------------
951
952EGLBoolean eglGetConfigs( EGLDisplay dpy,
953 EGLConfig *configs,
954 EGLint config_size, EGLint *num_config)
955{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800956 clearError();
957
Eric Hassold3ede7c12011-03-23 15:59:00 -0700958 egl_display_t const * const dp = validate_display(dpy);
959 if (!dp) return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960
961 GLint numConfigs = dp->numTotalConfigs;
962 if (!configs) {
963 *num_config = numConfigs;
964 return EGL_TRUE;
965 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700966
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800967 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700968 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
969 *configs++ = EGLConfig(i);
970 config_size--;
971 n++;
972 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973
974 *num_config = n;
975 return EGL_TRUE;
976}
977
978EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
979 EGLConfig *configs, EGLint config_size,
980 EGLint *num_config)
981{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800982 clearError();
983
Eric Hassold3ede7c12011-03-23 15:59:00 -0700984 egl_display_t const * const dp = validate_display(dpy);
985 if (!dp) return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986
Jack Palevich749c63d2009-03-25 15:12:17 -0700987 if (num_config==0) {
988 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989 }
990
991 EGLint n;
992 EGLBoolean res = EGL_FALSE;
993 *num_config = 0;
994
995
996 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -0700997 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 // to figure out both its size and if it contains an EGL_CONFIG_ID
999 // key. If so, the full array is copied and patched.
1000 // NOTE: we assume that there can be only one occurrence
1001 // of EGL_CONFIG_ID.
1002
1003 EGLint patch_index = -1;
1004 GLint attr;
1005 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -07001006 if (attrib_list) {
1007 while ((attr=attrib_list[size]) != EGL_NONE) {
1008 if (attr == EGL_CONFIG_ID)
1009 patch_index = size;
1010 size += 2;
1011 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 }
1013 if (patch_index >= 0) {
1014 size += 2; // we need copy the sentinel as well
1015 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
1016 if (new_list == 0)
1017 return setError(EGL_BAD_ALLOC, EGL_FALSE);
1018 memcpy(new_list, attrib_list, size*sizeof(EGLint));
1019
1020 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -07001021 bool found = false;
1022 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -07001024 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
1025 if (dp->configs[i].configId == configId) {
1026 ourConfig = EGLConfig(i);
1027 configId = dp->configs[i].implConfigId;
1028 found = true;
1029 break;
1030 }
1031 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032
Mathias Agopiancee79392010-07-26 21:14:59 -07001033 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
1034 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 // and switch to the new list
1036 attrib_list = const_cast<const EGLint *>(new_list);
1037
1038 // At this point, the only configuration that can match is
1039 // dp->configs[i][index], however, we don't know if it would be
1040 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -07001041 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 // through all the EGLimpl[].
1043 // We also know we can only get a single config back, and we know
1044 // which one.
1045
Mathias Agopian618fa102009-10-14 02:06:37 -07001046 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -07001047 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
1048 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049 if (res && n>0) {
1050 // n has to be 0 or 1, by construction, and we already know
1051 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -07001052 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001053 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -07001054 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 *num_config = 1;
1056 }
1057 }
1058
1059 free(const_cast<EGLint *>(attrib_list));
1060 return res;
1061 }
1062
Mathias Agopiancee79392010-07-26 21:14:59 -07001063
Mathias Agopian618fa102009-10-14 02:06:37 -07001064 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 egl_connection_t* const cnx = &gEGLImpl[i];
1066 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001067 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001068 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001069 if (configs) {
1070 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -07001071 // internal EGLConfig format.
1072 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -07001073 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001074 egl_config_t key(i, configs[j]);
1075 intptr_t index = binarySearch<egl_config_t>(
1076 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -07001077 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001078 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -07001079 } else {
1080 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1081 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 }
Jack Palevich749c63d2009-03-25 15:12:17 -07001083 configs += n;
1084 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 *num_config += n;
1087 res = EGL_TRUE;
1088 }
1089 }
1090 }
1091 return res;
1092}
1093
1094EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1095 EGLint attribute, EGLint *value)
1096{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001097 clearError();
1098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001100 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001101 if (!cnx) return EGL_FALSE;
1102
1103 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001104 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 return EGL_TRUE;
1106 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001107 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -07001108 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1109 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110}
1111
1112// ----------------------------------------------------------------------------
1113// surfaces
1114// ----------------------------------------------------------------------------
1115
1116EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1117 NativeWindowType window,
1118 const EGLint *attrib_list)
1119{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001120 clearError();
1121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001123 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 if (cnx) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001125 EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy;
1126 EGLConfig iConfig = dp->configs[intptr_t(config)].config;
1127 EGLint format;
1128
Jamie Gennis5c0c93a2011-03-14 15:34:04 -07001129 // for now fail if the window is not a Surface.
1130 int type = -1;
1131 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
1132 if ((anw->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &type) != 0) ||
1133 (type == NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT)) {
1134 LOGE("native window is a SurfaceTextureClient (currently "
1135 "unsupported)");
1136 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1137 }
1138
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001139 // set the native window's buffers format to match this config
1140 if (cnx->egl.eglGetConfigAttrib(iDpy,
1141 iConfig, EGL_NATIVE_VISUAL_ID, &format)) {
1142 if (format != 0) {
1143 native_window_set_buffers_geometry(window, 0, 0, format);
1144 }
1145 }
1146
Mathias Agopian618fa102009-10-14 02:06:37 -07001147 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001148 iDpy, iConfig, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001150 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001151 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152 return s;
1153 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154 }
1155 return EGL_NO_SURFACE;
1156}
1157
1158EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1159 NativePixmapType pixmap,
1160 const EGLint *attrib_list)
1161{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001162 clearError();
1163
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001165 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001167 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001168 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1169 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001171 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001172 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 return s;
1174 }
1175 }
1176 return EGL_NO_SURFACE;
1177}
1178
1179EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1180 const EGLint *attrib_list)
1181{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001182 clearError();
1183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 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) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001187 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001188 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1189 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001191 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001192 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 return s;
1194 }
1195 }
1196 return EGL_NO_SURFACE;
1197}
1198
1199EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1200{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001201 clearError();
1202
Eric Hassold3ede7c12011-03-23 15:59:00 -07001203 egl_display_t const * const dp = validate_display(dpy);
1204 if (!dp) return EGL_FALSE;
1205
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001206 SurfaceRef _s(surface);
1207 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 if (!validate_display_surface(dpy, surface))
1210 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001212 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001213 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001214 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001215 if (result == EGL_TRUE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001216 if (s->win != NULL) {
1217 native_window_set_buffers_geometry(s->win.get(), 0, 0, 0);
1218 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001219 _s.terminate();
1220 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221 return result;
1222}
1223
1224EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1225 EGLint attribute, EGLint *value)
1226{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001227 clearError();
1228
Eric Hassold3ede7c12011-03-23 15:59:00 -07001229 egl_display_t const * const dp = validate_display(dpy);
1230 if (!dp) return EGL_FALSE;
1231
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001232 SurfaceRef _s(surface);
1233 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1234
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 if (!validate_display_surface(dpy, surface))
1236 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 egl_surface_t const * const s = get_surface(surface);
1238
Mathias Agopiancee79392010-07-26 21:14:59 -07001239 EGLBoolean result(EGL_TRUE);
1240 if (attribute == EGL_CONFIG_ID) {
1241 // We need to remap EGL_CONFIG_IDs
1242 *value = dp->configs[intptr_t(s->config)].configId;
1243 } else {
1244 result = s->cnx->egl.eglQuerySurface(
1245 dp->disp[s->impl].dpy, s->surface, attribute, value);
1246 }
1247
1248 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249}
1250
1251// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001252// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253// ----------------------------------------------------------------------------
1254
1255EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1256 EGLContext share_list, const EGLint *attrib_list)
1257{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001258 clearError();
1259
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001261 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001262 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001263 if (share_list != EGL_NO_CONTEXT) {
1264 egl_context_t* const c = get_context(share_list);
1265 share_list = c->context;
1266 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001267 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001268 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1269 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001270 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001272 // figure out if it's a GLESv1 or GLESv2
1273 int version = 0;
1274 if (attrib_list) {
1275 while (*attrib_list != EGL_NONE) {
1276 GLint attr = *attrib_list++;
1277 GLint value = *attrib_list++;
1278 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1279 if (value == 1) {
1280 version = GLESv1_INDEX;
1281 } else if (value == 2) {
1282 version = GLESv2_INDEX;
1283 }
1284 }
1285 };
1286 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001287 egl_context_t* c = new egl_context_t(dpy, context, config,
1288 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289 return c;
1290 }
1291 }
1292 return EGL_NO_CONTEXT;
1293}
1294
1295EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1296{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001297 clearError();
1298
Eric Hassold3ede7c12011-03-23 15:59:00 -07001299 egl_display_t const * const dp = validate_display(dpy);
1300 if (!dp) return EGL_FALSE;
1301
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001302 ContextRef _c(ctx);
1303 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 if (!validate_display_context(dpy, ctx))
1306 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001308 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001309 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001310 if (result == EGL_TRUE) {
1311 _c.terminate();
1312 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313 return result;
1314}
1315
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001316static void loseCurrent(egl_context_t * cur_c)
1317{
1318 if (cur_c) {
1319 egl_surface_t * cur_r = get_surface(cur_c->read);
1320 egl_surface_t * cur_d = get_surface(cur_c->draw);
1321
1322 // by construction, these are either 0 or valid (possibly terminated)
1323 // it should be impossible for these to be invalid
1324 ContextRef _cur_c(cur_c);
1325 SurfaceRef _cur_r(cur_r);
1326 SurfaceRef _cur_d(cur_d);
1327
1328 cur_c->read = NULL;
1329 cur_c->draw = NULL;
1330
1331 _cur_c.release();
1332 _cur_r.release();
1333 _cur_d.release();
1334 }
1335}
1336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1338 EGLSurface read, EGLContext ctx)
1339{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001340 clearError();
1341
Eric Hassold3ede7c12011-03-23 15:59:00 -07001342 egl_display_t const * const dp = get_display(dpy);
1343 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1344
1345 /* If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
1346 EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
1347 a valid but uninitialized display. */
1348 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
1349 (draw != EGL_NO_SURFACE) ) {
1350 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
1351 }
1352
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001353 // get a reference to the object passed in
1354 ContextRef _c(ctx);
1355 SurfaceRef _d(draw);
1356 SurfaceRef _r(read);
1357
Eric Hassold3ede7c12011-03-23 15:59:00 -07001358 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001359 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1360 // EGL_NO_CONTEXT is valid
1361 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 }
1363
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001364 // these are the underlying implementation's object
1365 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001366 EGLSurface impl_draw = EGL_NO_SURFACE;
1367 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001368
1369 // these are our objects structs passed in
1370 egl_context_t * c = NULL;
1371 egl_surface_t const * d = NULL;
1372 egl_surface_t const * r = NULL;
1373
1374 // these are the current objects structs
1375 egl_context_t * cur_c = get_context(getContext());
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001376
1377 if (ctx != EGL_NO_CONTEXT) {
1378 c = get_context(ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001379 impl_ctx = c->context;
1380 } else {
1381 // no context given, use the implementation of the current context
1382 if (cur_c == NULL) {
1383 // no current context
1384 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001385 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1386 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001387 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001388 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001389 return EGL_TRUE;
1390 }
1391 }
1392
1393 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001395 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001396 // make sure the EGLContext and EGLSurface passed in are for
1397 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001398 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001400 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001402
1403 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001405 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001406 // make sure the EGLContext and EGLSurface passed in are for
1407 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001408 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001410 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001412
1413 EGLBoolean result;
1414
1415 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001416 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001417 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001418 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001419 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001420 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422
1423 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001424
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001425 loseCurrent(cur_c);
1426
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001427 if (ctx != EGL_NO_CONTEXT) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001428 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001429 setContext(ctx);
David Li864f8392011-03-28 10:39:28 -07001430 tls_t * const tls = getTLS();
1431 if (!tls->dbg && gEGLDebugLevel > 0)
1432 tls->dbg = CreateDbgContext(gEGLThreadLocalStorageKey, c->version,
1433 c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001434 _c.acquire();
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001435 _r.acquire();
1436 _d.acquire();
1437 c->read = read;
1438 c->draw = draw;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001439 } else {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001440 setGLHooksThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001441 setContext(EGL_NO_CONTEXT);
1442 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
1444 return result;
1445}
1446
1447
1448EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1449 EGLint attribute, EGLint *value)
1450{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001451 clearError();
1452
Eric Hassold3ede7c12011-03-23 15:59:00 -07001453 egl_display_t const * const dp = validate_display(dpy);
1454 if (!dp) return EGL_FALSE;
1455
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001456 ContextRef _c(ctx);
1457 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1458
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 if (!validate_display_context(dpy, ctx))
1460 return EGL_FALSE;
1461
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 egl_context_t * const c = get_context(ctx);
1463
Mathias Agopiancee79392010-07-26 21:14:59 -07001464 EGLBoolean result(EGL_TRUE);
1465 if (attribute == EGL_CONFIG_ID) {
1466 *value = dp->configs[intptr_t(c->config)].configId;
1467 } else {
1468 // We need to remap EGL_CONFIG_IDs
1469 result = c->cnx->egl.eglQueryContext(
1470 dp->disp[c->impl].dpy, c->context, attribute, value);
1471 }
1472
1473 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474}
1475
1476EGLContext eglGetCurrentContext(void)
1477{
Mathias Agopian923c6612009-08-17 18:07:06 -07001478 // could be called before eglInitialize(), but we wouldn't have a context
1479 // then, and this function would correctly return EGL_NO_CONTEXT.
1480
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001481 clearError();
1482
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 EGLContext ctx = getContext();
1484 return ctx;
1485}
1486
1487EGLSurface eglGetCurrentSurface(EGLint readdraw)
1488{
Mathias Agopian923c6612009-08-17 18:07:06 -07001489 // could be called before eglInitialize(), but we wouldn't have a context
1490 // then, and this function would correctly return EGL_NO_SURFACE.
1491
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001492 clearError();
1493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494 EGLContext ctx = getContext();
1495 if (ctx) {
1496 egl_context_t const * const c = get_context(ctx);
1497 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1498 switch (readdraw) {
1499 case EGL_READ: return c->read;
1500 case EGL_DRAW: return c->draw;
1501 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1502 }
1503 }
1504 return EGL_NO_SURFACE;
1505}
1506
1507EGLDisplay eglGetCurrentDisplay(void)
1508{
Mathias Agopian923c6612009-08-17 18:07:06 -07001509 // could be called before eglInitialize(), but we wouldn't have a context
1510 // then, and this function would correctly return EGL_NO_DISPLAY.
1511
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001512 clearError();
1513
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 EGLContext ctx = getContext();
1515 if (ctx) {
1516 egl_context_t const * const c = get_context(ctx);
1517 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1518 return c->dpy;
1519 }
1520 return EGL_NO_DISPLAY;
1521}
1522
1523EGLBoolean eglWaitGL(void)
1524{
Mathias Agopian923c6612009-08-17 18:07:06 -07001525 // could be called before eglInitialize(), but we wouldn't have a context
1526 // then, and this function would return GL_TRUE, which isn't wrong.
1527
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001528 clearError();
1529
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530 EGLBoolean res = EGL_TRUE;
1531 EGLContext ctx = getContext();
1532 if (ctx) {
1533 egl_context_t const * const c = get_context(ctx);
1534 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1535 if (uint32_t(c->impl)>=2)
1536 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1537 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1538 if (!cnx->dso)
1539 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001540 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 }
1542 return res;
1543}
1544
1545EGLBoolean eglWaitNative(EGLint engine)
1546{
Mathias Agopian923c6612009-08-17 18:07:06 -07001547 // could be called before eglInitialize(), but we wouldn't have a context
1548 // then, and this function would return GL_TRUE, which isn't wrong.
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001549
1550 clearError();
1551
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552 EGLBoolean res = EGL_TRUE;
1553 EGLContext ctx = getContext();
1554 if (ctx) {
1555 egl_context_t const * const c = get_context(ctx);
1556 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1557 if (uint32_t(c->impl)>=2)
1558 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1559 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1560 if (!cnx->dso)
1561 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001562 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563 }
1564 return res;
1565}
1566
1567EGLint eglGetError(void)
1568{
1569 EGLint result = EGL_SUCCESS;
Mathias Agopian02dafb52010-09-21 15:43:59 -07001570 EGLint err;
Mathias Agopian618fa102009-10-14 02:06:37 -07001571 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian02dafb52010-09-21 15:43:59 -07001572 err = EGL_SUCCESS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 egl_connection_t* const cnx = &gEGLImpl[i];
1574 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001575 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1577 result = err;
1578 }
Mathias Agopian02dafb52010-09-21 15:43:59 -07001579 err = getError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 if (result == EGL_SUCCESS)
Mathias Agopian02dafb52010-09-21 15:43:59 -07001581 result = err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582 return result;
1583}
1584
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001585// Note: Similar implementations of these functions also exist in
1586// gl2.cpp and gl.cpp, and are used by applications that call the
1587// exported entry points directly.
1588typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
1589typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
1590
1591static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL;
1592static PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL;
1593
1594static void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image)
1595{
1596 GLeglImageOES implImage =
1597 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1598 glEGLImageTargetTexture2DOES_impl(target, implImage);
1599}
1600
1601static void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image)
1602{
1603 GLeglImageOES implImage =
1604 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1605 glEGLImageTargetRenderbufferStorageOES_impl(target, implImage);
1606}
1607
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001608__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001610 // eglGetProcAddress() could be the very first function called
1611 // in which case we must make sure we've initialized ourselves, this
1612 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001613
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001614 clearError();
1615
Mathias Agopian923c6612009-08-17 18:07:06 -07001616 if (egl_init_drivers() == EGL_FALSE) {
1617 setError(EGL_BAD_PARAMETER, NULL);
1618 return NULL;
1619 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001620
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621 __eglMustCastToProperFunctionPointerType addr;
1622 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1623 if (addr) return addr;
1624
Mathias Agopian24035332010-08-02 17:34:32 -07001625 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1626 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627
Mathias Agopian24035332010-08-02 17:34:32 -07001628 /*
1629 * Since eglGetProcAddress() is not associated to anything, it needs
1630 * to return a function pointer that "works" regardless of what
1631 * the current context is.
1632 *
1633 * For this reason, we return a "forwarder", a small stub that takes
1634 * care of calling the function associated with the context
1635 * currently bound.
1636 *
1637 * We first look for extensions we've already resolved, if we're seeing
1638 * this extension for the first time, we go through all our
1639 * implementations and call eglGetProcAddress() and record the
1640 * result in the appropriate implementation hooks and return the
1641 * address of the forwarder corresponding to that hook set.
1642 *
1643 */
1644
1645 const String8 name(procname);
1646 addr = gGLExtentionMap.valueFor(name);
1647 const int slot = gGLExtentionSlot;
1648
1649 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1650 "no more slots for eglGetProcAddress(\"%s\")",
1651 procname);
1652
1653 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1654 bool found = false;
1655 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1656 egl_connection_t* const cnx = &gEGLImpl[i];
1657 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1658 found = true;
Mathias Agopian4a88b522010-08-13 12:19:04 -07001659 // Extensions are independent of the bound context
1660 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1661 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001662#if EGL_TRACE
David Li2f5a6552011-03-01 16:08:10 -08001663 gHooksDebug.ext.extensions[slot] = gHooksTrace.ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001664#endif
Mathias Agopian24035332010-08-02 17:34:32 -07001665 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 }
1667 }
Mathias Agopian24035332010-08-02 17:34:32 -07001668 if (found) {
1669 addr = gExtensionForwarders[slot];
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001670
1671 if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) {
1672 glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr;
1673 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper;
1674 }
1675 if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) {
1676 glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr;
1677 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper;
1678 }
1679
Mathias Agopian24035332010-08-02 17:34:32 -07001680 gGLExtentionMap.add(name, addr);
1681 gGLExtentionSlot++;
1682 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684
Mathias Agopian24035332010-08-02 17:34:32 -07001685 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian24035332010-08-02 17:34:32 -07001686 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687}
1688
1689EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1690{
David Lib33d5cf2011-03-04 17:50:48 -08001691 EGLBoolean Debug_eglSwapBuffers(EGLDisplay dpy, EGLSurface draw);
1692 if (gEGLDebugLevel > 0)
1693 Debug_eglSwapBuffers(dpy, draw);
1694
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001695 clearError();
1696
Eric Hassold3ede7c12011-03-23 15:59:00 -07001697 egl_display_t const * const dp = validate_display(dpy);
1698 if (!dp) return EGL_FALSE;
1699
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001700 SurfaceRef _s(draw);
1701 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1702
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 if (!validate_display_surface(dpy, draw))
1704 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001706 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707}
1708
1709EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1710 NativePixmapType target)
1711{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001712 clearError();
1713
Eric Hassold3ede7c12011-03-23 15:59:00 -07001714 egl_display_t const * const dp = validate_display(dpy);
1715 if (!dp) return EGL_FALSE;
1716
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001717 SurfaceRef _s(surface);
1718 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 if (!validate_display_surface(dpy, surface))
1721 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001722 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001723 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001724 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725}
1726
1727const char* eglQueryString(EGLDisplay dpy, EGLint name)
1728{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001729 clearError();
1730
Eric Hassold3ede7c12011-03-23 15:59:00 -07001731 egl_display_t const * const dp = validate_display(dpy);
1732 if (!dp) return (const char *) NULL;
1733
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 switch (name) {
1735 case EGL_VENDOR:
1736 return gVendorString;
1737 case EGL_VERSION:
1738 return gVersionString;
1739 case EGL_EXTENSIONS:
1740 return gExtensionString;
1741 case EGL_CLIENT_APIS:
1742 return gClientApiString;
1743 }
1744 return setError(EGL_BAD_PARAMETER, (const char *)0);
1745}
1746
1747
1748// ----------------------------------------------------------------------------
1749// EGL 1.1
1750// ----------------------------------------------------------------------------
1751
1752EGLBoolean eglSurfaceAttrib(
1753 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1754{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001755 clearError();
1756
Eric Hassold3ede7c12011-03-23 15:59:00 -07001757 egl_display_t const * const dp = validate_display(dpy);
1758 if (!dp) return EGL_FALSE;
1759
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001760 SurfaceRef _s(surface);
1761 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1762
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763 if (!validate_display_surface(dpy, surface))
1764 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001766 if (s->cnx->egl.eglSurfaceAttrib) {
1767 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001768 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769 }
1770 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1771}
1772
1773EGLBoolean eglBindTexImage(
1774 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1775{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001776 clearError();
1777
Eric Hassold3ede7c12011-03-23 15:59:00 -07001778 egl_display_t const * const dp = validate_display(dpy);
1779 if (!dp) return EGL_FALSE;
1780
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001781 SurfaceRef _s(surface);
1782 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1783
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001784 if (!validate_display_surface(dpy, surface))
1785 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001787 if (s->cnx->egl.eglBindTexImage) {
1788 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001789 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001790 }
1791 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1792}
1793
1794EGLBoolean eglReleaseTexImage(
1795 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1796{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001797 clearError();
1798
Eric Hassold3ede7c12011-03-23 15:59:00 -07001799 egl_display_t const * const dp = validate_display(dpy);
1800 if (!dp) return EGL_FALSE;
1801
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001802 SurfaceRef _s(surface);
1803 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1804
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001805 if (!validate_display_surface(dpy, surface))
1806 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001808 if (s->cnx->egl.eglReleaseTexImage) {
1809 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001810 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811 }
1812 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1813}
1814
1815EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1816{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001817 clearError();
1818
Eric Hassold3ede7c12011-03-23 15:59:00 -07001819 egl_display_t const * const dp = validate_display(dpy);
1820 if (!dp) return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821
1822 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001823 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001824 egl_connection_t* const cnx = &gEGLImpl[i];
1825 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001826 if (cnx->egl.eglSwapInterval) {
1827 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001828 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001829 res = EGL_FALSE;
1830 }
1831 }
1832 }
1833 }
1834 return res;
1835}
1836
1837
1838// ----------------------------------------------------------------------------
1839// EGL 1.2
1840// ----------------------------------------------------------------------------
1841
1842EGLBoolean eglWaitClient(void)
1843{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001844 clearError();
1845
Mathias Agopian923c6612009-08-17 18:07:06 -07001846 // could be called before eglInitialize(), but we wouldn't have a context
1847 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848 EGLBoolean res = EGL_TRUE;
1849 EGLContext ctx = getContext();
1850 if (ctx) {
1851 egl_context_t const * const c = get_context(ctx);
1852 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1853 if (uint32_t(c->impl)>=2)
1854 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1855 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1856 if (!cnx->dso)
1857 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001858 if (cnx->egl.eglWaitClient) {
1859 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001860 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001861 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001862 }
1863 }
1864 return res;
1865}
1866
1867EGLBoolean eglBindAPI(EGLenum api)
1868{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001869 clearError();
1870
Mathias Agopian923c6612009-08-17 18:07:06 -07001871 if (egl_init_drivers() == EGL_FALSE) {
1872 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1873 }
1874
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875 // bind this API on all EGLs
1876 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001877 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001878 egl_connection_t* const cnx = &gEGLImpl[i];
1879 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001880 if (cnx->egl.eglBindAPI) {
1881 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882 res = EGL_FALSE;
1883 }
1884 }
1885 }
1886 }
1887 return res;
1888}
1889
1890EGLenum eglQueryAPI(void)
1891{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001892 clearError();
1893
Mathias Agopian923c6612009-08-17 18:07:06 -07001894 if (egl_init_drivers() == EGL_FALSE) {
1895 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1896 }
1897
Mathias Agopian618fa102009-10-14 02:06:37 -07001898 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899 egl_connection_t* const cnx = &gEGLImpl[i];
1900 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001901 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 // the first one we find is okay, because they all
1903 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001904 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 }
1906 }
1907 }
1908 // or, it can only be OpenGL ES
1909 return EGL_OPENGL_ES_API;
1910}
1911
1912EGLBoolean eglReleaseThread(void)
1913{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001914 clearError();
1915
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001916 // If there is context bound to the thread, release it
1917 loseCurrent(get_context(getContext()));
1918
Mathias Agopian618fa102009-10-14 02:06:37 -07001919 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001920 egl_connection_t* const cnx = &gEGLImpl[i];
1921 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001922 if (cnx->egl.eglReleaseThread) {
1923 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001924 }
1925 }
1926 }
1927 clearTLS();
1928 return EGL_TRUE;
1929}
1930
1931EGLSurface eglCreatePbufferFromClientBuffer(
1932 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1933 EGLConfig config, const EGLint *attrib_list)
1934{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001935 clearError();
1936
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001938 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001940 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1941 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001942 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1943 buftype, buffer,
1944 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945 }
1946 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1947}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001948
1949// ----------------------------------------------------------------------------
1950// EGL_EGLEXT_VERSION 3
1951// ----------------------------------------------------------------------------
1952
1953EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1954 const EGLint *attrib_list)
1955{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001956 clearError();
1957
Eric Hassold3ede7c12011-03-23 15:59:00 -07001958 egl_display_t const * const dp = validate_display(dpy);
1959 if (!dp) return EGL_FALSE;
1960
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001961 SurfaceRef _s(surface);
1962 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1963
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001964 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001965 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001966
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001967 egl_surface_t const * const s = get_surface(surface);
1968
Mathias Agopian618fa102009-10-14 02:06:37 -07001969 if (s->cnx->egl.eglLockSurfaceKHR) {
1970 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001971 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001972 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001973 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001974}
1975
1976EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1977{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001978 clearError();
1979
Eric Hassold3ede7c12011-03-23 15:59:00 -07001980 egl_display_t const * const dp = validate_display(dpy);
1981 if (!dp) return EGL_FALSE;
1982
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001983 SurfaceRef _s(surface);
1984 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1985
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001986 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001987 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001988
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001989 egl_surface_t const * const s = get_surface(surface);
1990
Mathias Agopian618fa102009-10-14 02:06:37 -07001991 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1992 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001993 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001994 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001995 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001996}
1997
1998EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1999 EGLClientBuffer buffer, const EGLint *attrib_list)
2000{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002001 clearError();
2002
Eric Hassold3ede7c12011-03-23 15:59:00 -07002003 egl_display_t const * const dp = validate_display(dpy);
2004 if (!dp) return EGL_NO_IMAGE_KHR;
2005
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002006 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002007 ContextRef _c(ctx);
2008 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002009 if (!validate_display_context(dpy, ctx))
2010 return EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002011 egl_context_t * const c = get_context(ctx);
2012 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07002013 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002014 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002015 if (image == EGL_NO_IMAGE_KHR)
2016 return image;
2017
2018 egl_image_t* result = new egl_image_t(dpy, ctx);
2019 result->images[c->impl] = image;
2020 return (EGLImageKHR)result;
2021 } else {
2022 // EGL_NO_CONTEXT is a valid parameter
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002023
2024 /* Since we don't have a way to know which implementation to call,
2025 * we're calling all of them. If at least one of the implementation
2026 * succeeded, this is a success.
2027 */
2028
2029 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002030
Mathias Agopian618fa102009-10-14 02:06:37 -07002031 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002032 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07002033 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002034 egl_connection_t* const cnx = &gEGLImpl[i];
2035 implImages[i] = EGL_NO_IMAGE_KHR;
2036 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07002037 if (cnx->egl.eglCreateImageKHR) {
2038 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002039 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002040 if (implImages[i] != EGL_NO_IMAGE_KHR) {
2041 success = true;
2042 }
2043 }
2044 }
2045 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002046
2047 if (!success) {
2048 // failure, if there was an error when we entered this function,
2049 // the error flag must not be updated.
2050 // Otherwise, the error is whatever happened in the implementation
2051 // that faulted.
2052 if (currentError != EGL_SUCCESS) {
2053 setError(currentError, EGL_NO_IMAGE_KHR);
2054 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002055 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002056 } else {
2057 // In case of success, we need to clear all error flags
2058 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07002059 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002060 // a "full" success (all implementation succeeded).
2061 eglGetError();
2062 }
2063
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002064 egl_image_t* result = new egl_image_t(dpy, ctx);
2065 memcpy(result->images, implImages, sizeof(implImages));
2066 return (EGLImageKHR)result;
2067 }
2068}
2069
2070EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2071{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002072 clearError();
2073
Eric Hassold3ede7c12011-03-23 15:59:00 -07002074 egl_display_t const * const dp = validate_display(dpy);
2075 if (!dp) return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002076
Eric Hassold3ede7c12011-03-23 15:59:00 -07002077 ImageRef _i(img);
2078 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002079
Eric Hassold3ede7c12011-03-23 15:59:00 -07002080 egl_image_t* image = get_image(img);
2081 bool success = false;
2082 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
2083 egl_connection_t* const cnx = &gEGLImpl[i];
2084 if (image->images[i] != EGL_NO_IMAGE_KHR) {
2085 if (cnx->dso) {
2086 if (cnx->egl.eglDestroyImageKHR) {
2087 if (cnx->egl.eglDestroyImageKHR(
2088 dp->disp[i].dpy, image->images[i])) {
2089 success = true;
2090 }
2091 }
2092 }
2093 }
2094 }
2095 if (!success)
2096 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002097
Eric Hassold3ede7c12011-03-23 15:59:00 -07002098 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002099
Eric Hassold3ede7c12011-03-23 15:59:00 -07002100 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002101}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002102
Mathias Agopianc291f5852010-08-27 16:48:56 -07002103// ----------------------------------------------------------------------------
2104// EGL_EGLEXT_VERSION 5
2105// ----------------------------------------------------------------------------
2106
2107
2108EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
2109{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002110 clearError();
2111
Eric Hassold3ede7c12011-03-23 15:59:00 -07002112 egl_display_t const * const dp = validate_display(dpy);
2113 if (!dp) return EGL_NO_SYNC_KHR;
2114
Mathias Agopianc291f5852010-08-27 16:48:56 -07002115 EGLContext ctx = eglGetCurrentContext();
2116 ContextRef _c(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002117 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
Mathias Agopianc291f5852010-08-27 16:48:56 -07002118 if (!validate_display_context(dpy, ctx))
Mathias Agopiana93b9572010-09-21 16:22:10 -07002119 return EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002120 egl_context_t * const c = get_context(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002121 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002122 if (c->cnx->egl.eglCreateSyncKHR) {
2123 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
2124 dp->disp[c->impl].dpy, type, attrib_list);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002125 if (sync == EGL_NO_SYNC_KHR)
Mathias Agopianc291f5852010-08-27 16:48:56 -07002126 return sync;
2127 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
2128 }
2129 return (EGLSyncKHR)result;
2130}
2131
2132EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
2133{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002134 clearError();
2135
Eric Hassold3ede7c12011-03-23 15:59:00 -07002136 egl_display_t const * const dp = validate_display(dpy);
2137 if (!dp) return EGL_FALSE;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002138
2139 SyncRef _s(sync);
2140 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2141 egl_sync_t* syncObject = get_sync(sync);
2142
2143 EGLContext ctx = syncObject->context;
2144 ContextRef _c(ctx);
2145 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2146 if (!validate_display_context(dpy, ctx))
2147 return EGL_FALSE;
2148
Mathias Agopian36bdf142011-03-16 14:19:03 -07002149 EGLBoolean result = EGL_FALSE;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002150 egl_context_t * const c = get_context(ctx);
Mathias Agopianc291f5852010-08-27 16:48:56 -07002151 if (c->cnx->egl.eglDestroySyncKHR) {
Mathias Agopian36bdf142011-03-16 14:19:03 -07002152 result = c->cnx->egl.eglDestroySyncKHR(
Mathias Agopianc291f5852010-08-27 16:48:56 -07002153 dp->disp[c->impl].dpy, syncObject->sync);
Mathias Agopian36bdf142011-03-16 14:19:03 -07002154 if (result)
2155 _s.terminate();
Mathias Agopianc291f5852010-08-27 16:48:56 -07002156 }
Mathias Agopian36bdf142011-03-16 14:19:03 -07002157 return result;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002158}
2159
2160EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
2161{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002162 clearError();
2163
Eric Hassold3ede7c12011-03-23 15:59:00 -07002164 egl_display_t const * const dp = validate_display(dpy);
2165 if (!dp) return EGL_FALSE;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002166
2167 SyncRef _s(sync);
2168 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2169 egl_sync_t* syncObject = get_sync(sync);
2170
2171 EGLContext ctx = syncObject->context;
2172 ContextRef _c(ctx);
2173 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2174 if (!validate_display_context(dpy, ctx))
2175 return EGL_FALSE;
2176
2177 egl_context_t * const c = get_context(ctx);
2178
2179 if (c->cnx->egl.eglClientWaitSyncKHR) {
2180 return c->cnx->egl.eglClientWaitSyncKHR(
2181 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
2182 }
2183
2184 return EGL_FALSE;
2185}
2186
2187EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
2188{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002189 clearError();
2190
Eric Hassold3ede7c12011-03-23 15:59:00 -07002191 egl_display_t const * const dp = validate_display(dpy);
2192 if (!dp) return EGL_FALSE;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002193
2194 SyncRef _s(sync);
2195 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2196 egl_sync_t* syncObject = get_sync(sync);
2197
2198 EGLContext ctx = syncObject->context;
2199 ContextRef _c(ctx);
2200 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2201 if (!validate_display_context(dpy, ctx))
2202 return EGL_FALSE;
2203
2204 egl_context_t * const c = get_context(ctx);
2205
2206 if (c->cnx->egl.eglGetSyncAttribKHR) {
2207 return c->cnx->egl.eglGetSyncAttribKHR(
2208 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
2209 }
2210
2211 return EGL_FALSE;
2212}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002213
2214// ----------------------------------------------------------------------------
2215// ANDROID extensions
2216// ----------------------------------------------------------------------------
2217
2218EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2219 EGLint left, EGLint top, EGLint width, EGLint height)
2220{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002221 clearError();
2222
Eric Hassold3ede7c12011-03-23 15:59:00 -07002223 egl_display_t const * const dp = validate_display(dpy);
2224 if (!dp) return EGL_FALSE;
2225
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002226 SurfaceRef _s(draw);
2227 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
2228
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002229 if (!validate_display_surface(dpy, draw))
2230 return EGL_FALSE;
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002231 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07002232 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
2233 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002234 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002235 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07002236 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002237}