blob: 861d7aca5260002b086ef3269557fb25f413653b [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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
51
52// ----------------------------------------------------------------------------
53namespace android {
54// ----------------------------------------------------------------------------
55
56#define VERSION_MAJOR 1
57#define VERSION_MINOR 4
58static char const * const gVendorString = "Android";
Mathias Agopian923c6612009-08-17 18:07:06 -070059static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -070061static char const * const gExtensionString =
62 "EGL_KHR_image "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -070063 "EGL_KHR_image_base "
64 "EGL_KHR_image_pixmap "
Mathias Agopian8e4b5a32010-08-27 16:08:03 -070065 "EGL_KHR_gl_texture_2D_image "
Michael I. Goldca41e362011-01-13 10:13:15 -080066 "EGL_KHR_gl_texture_cubemap_image "
67 "EGL_KHR_gl_renderbuffer_image "
Mathias Agopianc291f5852010-08-27 16:48:56 -070068 "EGL_KHR_fence_sync "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070070 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 ;
72
73// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Mathias Agopian9429e9c2009-08-21 02:18:25 -070075class egl_object_t {
76 static SortedVector<egl_object_t*> sObjects;
77 static Mutex sLock;
78
79 volatile int32_t terminated;
80 mutable volatile int32_t count;
81
82public:
83 egl_object_t() : terminated(0), count(1) {
84 Mutex::Autolock _l(sLock);
85 sObjects.add(this);
86 }
87
88 inline bool isAlive() const { return !terminated; }
89
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -070091 bool get() {
92 Mutex::Autolock _l(sLock);
93 if (egl_object_t::sObjects.indexOf(this) >= 0) {
94 android_atomic_inc(&count);
95 return true;
96 }
97 return false;
98 }
99
100 bool put() {
101 Mutex::Autolock _l(sLock);
102 if (android_atomic_dec(&count) == 1) {
103 sObjects.remove(this);
104 return true;
105 }
106 return false;
107 }
108
109public:
110 template <typename N, typename T>
111 struct LocalRef {
112 N* ref;
113 LocalRef(T o) : ref(0) {
114 N* native = reinterpret_cast<N*>(o);
115 if (o && native->get()) {
116 ref = native;
117 }
118 }
119 ~LocalRef() {
120 if (ref && ref->put()) {
121 delete ref;
122 }
123 }
124 inline N* get() {
125 return ref;
126 }
127 void acquire() const {
128 if (ref) {
129 android_atomic_inc(&ref->count);
130 }
131 }
132 void release() const {
133 if (ref) {
134 int32_t c = android_atomic_dec(&ref->count);
135 // ref->count cannot be 1 prior atomic_dec because we have
136 // a reference, and if we have one, it means there was
137 // already one before us.
138 LOGE_IF(c==1, "refcount is now 0 in release()");
139 }
140 }
141 void terminate() {
142 if (ref) {
143 ref->terminated = 1;
144 release();
145 }
146 }
147 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148};
149
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700150SortedVector<egl_object_t*> egl_object_t::sObjects;
151Mutex egl_object_t::sLock;
152
Mathias Agopiancee79392010-07-26 21:14:59 -0700153
154struct egl_config_t {
155 egl_config_t() {}
156 egl_config_t(int impl, EGLConfig config)
157 : impl(impl), config(config), configId(0), implConfigId(0) { }
158 int impl; // the implementation this config is for
159 EGLConfig config; // the implementation's EGLConfig
160 EGLint configId; // our CONFIG_ID
161 EGLint implConfigId; // the implementation's CONFIG_ID
162 inline bool operator < (const egl_config_t& rhs) const {
163 if (impl < rhs.impl) return true;
164 if (impl > rhs.impl) return false;
165 return config < rhs.config;
166 }
167};
168
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700169struct egl_display_t {
170 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
171
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 struct strings_t {
173 char const * vendor;
174 char const * version;
175 char const * clientApi;
176 char const * extensions;
177 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700178
179 struct DisplayImpl {
180 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
181 state(NOT_INITIALIZED), numConfigs(0) { }
182 EGLDisplay dpy;
183 EGLConfig* config;
184 EGLint state;
185 EGLint numConfigs;
186 strings_t queryString;
187 };
188
Mathias Agopiancee79392010-07-26 21:14:59 -0700189 uint32_t magic;
190 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
191 EGLint numTotalConfigs;
192 egl_config_t* configs;
193 uint32_t refs;
194 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700195
Mathias Agopiancee79392010-07-26 21:14:59 -0700196 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700197 ~egl_display_t() { magic = 0; }
198 inline bool isValid() const { return magic == '_dpy'; }
199 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200};
201
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700202struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700204 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
205
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800206 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win,
207 EGLSurface surface, int impl, egl_connection_t const* cnx)
208 : dpy(dpy), surface(surface), config(config), win(win), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 }
210 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 }
212 EGLDisplay dpy;
213 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700214 EGLConfig config;
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800215 sp<ANativeWindow> win;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 int impl;
217 egl_connection_t const* cnx;
218};
219
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700220struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700222 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
223
Mathias Agopiancee79392010-07-26 21:14:59 -0700224 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian618fa102009-10-14 02:06:37 -0700225 int impl, egl_connection_t const* cnx, int version)
Mathias Agopianc3ce8802010-12-08 15:34:02 -0800226 : dpy(dpy), context(context), config(config), read(0), draw(0), impl(impl),
David Li65948aa2011-03-10 16:40:37 -0800227 cnx(cnx), version(version), dbg(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 {
229 }
David Li65948aa2011-03-10 16:40:37 -0800230 ~egl_context_t()
231 {
232 if (dbg)
233 DestroyDbgContext(dbg);
234 dbg = NULL;
235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 EGLDisplay dpy;
237 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700238 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239 EGLSurface read;
240 EGLSurface draw;
241 int impl;
242 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700243 int version;
David Li65948aa2011-03-10 16:40:37 -0800244 DbgContext * dbg;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245};
246
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700247struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700248{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700249 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
250
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251 egl_image_t(EGLDisplay dpy, EGLContext context)
252 : dpy(dpy), context(context)
253 {
254 memset(images, 0, sizeof(images));
255 }
256 EGLDisplay dpy;
Mathias Agopian77fbf8d2010-09-09 11:12:54 -0700257 EGLContext context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700258 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700259};
260
Mathias Agopianc291f5852010-08-27 16:48:56 -0700261struct egl_sync_t : public egl_object_t
262{
263 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
264
265 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync)
266 : dpy(dpy), context(context), sync(sync)
267 {
268 }
269 EGLDisplay dpy;
270 EGLContext context;
271 EGLSyncKHR sync;
272};
273
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700274typedef egl_surface_t::Ref SurfaceRef;
275typedef egl_context_t::Ref ContextRef;
276typedef egl_image_t::Ref ImageRef;
Mathias Agopianc291f5852010-08-27 16:48:56 -0700277typedef egl_sync_t::Ref SyncRef;
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279struct tls_t
280{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700281 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 EGLint error;
283 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700284 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285};
286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
288// ----------------------------------------------------------------------------
289
Mathias Agopianbf41b112010-04-09 13:37:34 -0700290static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291static egl_display_t gDisplay[NUM_DISPLAYS];
292static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
293static pthread_key_t gEGLThreadLocalStorageKey = -1;
294
295// ----------------------------------------------------------------------------
296
Mathias Agopian618fa102009-10-14 02:06:37 -0700297EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
298EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700299EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700301#if EGL_TRACE
302
303EGLAPI pthread_key_t gGLTraceKey = -1;
304
305// ----------------------------------------------------------------------------
306
David Li2f5a6552011-03-01 16:08:10 -0800307static int gEGLTraceLevel, gEGLDebugLevel;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700308static int gEGLApplicationTraceLevel;
David Li2f5a6552011-03-01 16:08:10 -0800309extern EGLAPI gl_hooks_t gHooksTrace, gHooksDebug;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700310
311static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
312 pthread_setspecific(gGLTraceKey, value);
313}
314
315gl_hooks_t const* getGLTraceThreadSpecific() {
316 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
317}
318
319static void initEglTraceLevel() {
320 char value[PROPERTY_VALUE_MAX];
321 property_get("debug.egl.trace", value, "0");
322 int propertyLevel = atoi(value);
323 int applicationLevel = gEGLApplicationTraceLevel;
324 gEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li2f5a6552011-03-01 16:08:10 -0800325
326 property_get("debug.egl.debug_proc", value, "");
327 long pid = getpid();
328 char procPath[128] = {};
329 sprintf(procPath, "/proc/%ld/cmdline", pid);
330 FILE * file = fopen(procPath, "r");
331 if (file)
332 {
333 char cmdline[256] = {};
334 if (fgets(cmdline, sizeof(cmdline) - 1, file))
335 {
David Li2f5a6552011-03-01 16:08:10 -0800336 if (!strcmp(value, cmdline))
337 gEGLDebugLevel = 1;
338 }
339 fclose(file);
340 }
341
David Li2f5a6552011-03-01 16:08:10 -0800342 if (gEGLDebugLevel > 0)
343 StartDebugServer();
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700344}
345
346static void setGLHooksThreadSpecific(gl_hooks_t const *value) {
347 if (gEGLTraceLevel > 0) {
348 setGlTraceThreadSpecific(value);
349 setGlThreadSpecific(&gHooksTrace);
David Li65948aa2011-03-10 16:40:37 -0800350 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800351 setGlTraceThreadSpecific(value);
352 setGlThreadSpecific(&gHooksDebug);
353 LOGD("\n* setGLHooksThreadSpecific gHooksDebug");
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700354 } else {
355 setGlThreadSpecific(value);
356 }
357}
358
359/*
360 * Global entry point to allow applications to modify their own trace level.
361 * The effective trace level is the max of this level and the value of debug.egl.trace.
362 */
363extern "C"
364void setGLTraceLevel(int level) {
365 gEGLApplicationTraceLevel = level;
366}
367
368#else
369
370static inline void setGLHooksThreadSpecific(gl_hooks_t const *value) {
371 setGlThreadSpecific(value);
372}
373
374#endif
375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376// ----------------------------------------------------------------------------
377
378static __attribute__((noinline))
379const char *egl_strerror(EGLint err)
380{
381 switch (err){
382 case EGL_SUCCESS: return "EGL_SUCCESS";
383 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
384 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
385 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
386 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
387 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
388 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
389 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
390 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
391 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
392 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
393 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
394 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
395 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
396 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
397 default: return "UNKNOWN";
398 }
399}
400
401static __attribute__((noinline))
402void clearTLS() {
403 if (gEGLThreadLocalStorageKey != -1) {
404 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
405 if (tls) {
406 delete tls;
407 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
408 }
409 }
410}
411
412static tls_t* getTLS()
413{
414 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
415 if (tls == 0) {
416 tls = new tls_t;
417 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
418 }
419 return tls;
420}
421
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800422static inline void clearError() {
Jamie Gennisf1cde8e2011-01-30 16:50:04 -0800423 // This must clear the error from all the underlying EGL implementations as
424 // well as the EGL wrapper layer.
425 eglGetError();
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800426}
427
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428template<typename T>
429static __attribute__((noinline))
430T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
431 if (gEGLThreadLocalStorageKey == -1) {
432 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
433 if (gEGLThreadLocalStorageKey == -1)
434 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
435 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
436 }
437 tls_t* tls = getTLS();
438 if (tls->error != error) {
439 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
440 tls->error = error;
441 }
442 return returnValue;
443}
444
445static __attribute__((noinline))
446GLint getError() {
447 if (gEGLThreadLocalStorageKey == -1)
448 return EGL_SUCCESS;
449 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
450 if (!tls) return EGL_SUCCESS;
451 GLint error = tls->error;
452 tls->error = EGL_SUCCESS;
453 return error;
454}
455
456static __attribute__((noinline))
457void setContext(EGLContext ctx) {
458 if (gEGLThreadLocalStorageKey == -1) {
459 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
460 if (gEGLThreadLocalStorageKey == -1)
461 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
462 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
463 }
464 tls_t* tls = getTLS();
465 tls->ctx = ctx;
466}
467
468static __attribute__((noinline))
469EGLContext getContext() {
470 if (gEGLThreadLocalStorageKey == -1)
471 return EGL_NO_CONTEXT;
472 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
473 if (!tls) return EGL_NO_CONTEXT;
474 return tls->ctx;
475}
476
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477/*****************************************************************************/
478
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479template<typename T>
480static __attribute__((noinline))
481int binarySearch(
482 T const sortedArray[], int first, int last, T key)
483{
484 while (first <= last) {
485 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700486 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800487 first = mid + 1;
488 } else if (key < sortedArray[mid]) {
489 last = mid - 1;
490 } else {
491 return mid;
492 }
493 }
494 return -1;
495}
496
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497static int cmp_configs(const void* a, const void *b)
498{
Mathias Agopiancee79392010-07-26 21:14:59 -0700499 const egl_config_t& c0 = *(egl_config_t const *)a;
500 const egl_config_t& c1 = *(egl_config_t const *)b;
501 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502}
503
504struct extention_map_t {
505 const char* name;
506 __eglMustCastToProperFunctionPointerType address;
507};
508
509static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700510 { "eglLockSurfaceKHR",
511 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
512 { "eglUnlockSurfaceKHR",
513 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
514 { "eglCreateImageKHR",
515 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
516 { "eglDestroyImageKHR",
517 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700518 { "eglSetSwapRectangleANDROID",
519 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520};
521
Mathias Agopian24035332010-08-02 17:34:32 -0700522extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
523
524// accesses protected by gInitDriverMutex
525static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
526static int gGLExtentionSlot = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527
528static void(*findProcAddress(const char* name,
529 const extention_map_t* map, size_t n))()
530{
531 for (uint32_t i=0 ; i<n ; i++) {
532 if (!strcmp(name, map[i].name)) {
533 return map[i].address;
534 }
535 }
536 return NULL;
537}
538
539// ----------------------------------------------------------------------------
540
Mathias Agopian6f087122010-09-23 16:38:38 -0700541static int gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700542 tls_t* tls = getTLS();
543 if (tls->logCallWithNoContext == EGL_TRUE) {
544 tls->logCallWithNoContext = EGL_FALSE;
545 LOGE("call to OpenGL ES API with no current context "
546 "(logged once per thread)");
547 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700548 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700549}
550
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800551static void early_egl_init(void)
552{
553#if !USE_FAST_TLS_KEY
554 pthread_key_create(&gGLWrapperKey, NULL);
555#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700556#if EGL_TRACE
557 pthread_key_create(&gGLTraceKey, NULL);
558 initEglTraceLevel();
559#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800560 uint32_t addr = (uint32_t)((void*)gl_no_context);
561 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700562 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700564 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700565
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700566 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567}
568
569static pthread_once_t once_control = PTHREAD_ONCE_INIT;
570static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
571
572
573static inline
574egl_display_t* get_display(EGLDisplay dpy)
575{
576 uintptr_t index = uintptr_t(dpy)-1U;
577 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
578}
579
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700580template<typename NATIVE, typename EGL>
581static inline NATIVE* egl_to_native_cast(EGL arg) {
582 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583}
584
585static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700586egl_surface_t* get_surface(EGLSurface surface) {
587 return egl_to_native_cast<egl_surface_t>(surface);
588}
589
590static inline
591egl_context_t* get_context(EGLContext context) {
592 return egl_to_native_cast<egl_context_t>(context);
593}
594
David Li65948aa2011-03-10 16:40:37 -0800595DbgContext * getDbgContextThreadSpecific()
596{
597 return get_context(getContext())->dbg;
598}
599
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700600static inline
601egl_image_t* get_image(EGLImageKHR image) {
602 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603}
604
Mathias Agopianc291f5852010-08-27 16:48:56 -0700605static inline
606egl_sync_t* get_sync(EGLSyncKHR sync) {
607 return egl_to_native_cast<egl_sync_t>(sync);
608}
609
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610static egl_connection_t* validate_display_config(
611 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700612 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613{
614 dp = get_display(dpy);
615 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
616
Mathias Agopiancee79392010-07-26 21:14:59 -0700617 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
619 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700620 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 if (cnx->dso == 0) {
622 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
623 }
624 return cnx;
625}
626
627static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
628{
629 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
630 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700631 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700633 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
635 return EGL_TRUE;
636}
637
638static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
639{
640 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
641 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700642 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800643 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700644 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 return setError(EGL_BAD_SURFACE, EGL_FALSE);
646 return EGL_TRUE;
647}
648
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
650{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700651 ImageRef _i(image);
652 if (!_i.get()) return EGL_NO_IMAGE_KHR;
653
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700654 EGLContext context = getContext();
655 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
656 return EGL_NO_IMAGE_KHR;
657
658 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700659 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700660 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700662 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700663 return i->images[c->impl];
664}
665
Mathias Agopian923c6612009-08-17 18:07:06 -0700666// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700667
Mathias Agopian923c6612009-08-17 18:07:06 -0700668// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700669// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700670// egl_init_drivers_locked()
671//
672static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
673
674EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675{
676 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700677 // initialized by static ctor. should be set here.
678 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679 }
680
Mathias Agopiande586972009-05-28 17:39:03 -0700681 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700682 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700683
Mathias Agopian923c6612009-08-17 18:07:06 -0700684 // dynamically load all our EGL implementations for all displays
685 // and retrieve the corresponding EGLDisplay
686 // if that fails, don't use this driver.
687 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
688 egl_connection_t* cnx;
689 egl_display_t* d = &gDisplay[0];
690
691 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700693 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
694 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
695 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700696 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700697 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700698 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700699 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700700 if (dpy == EGL_NO_DISPLAY) {
701 loader.close(cnx->dso);
702 cnx->dso = NULL;
703 }
704 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 }
706
707 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700708 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 char value[PROPERTY_VALUE_MAX];
710 property_get("debug.egl.hw", value, "1");
711 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700712 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
713 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
714 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700715 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700716 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700717 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700718 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700719 if (dpy == EGL_NO_DISPLAY) {
720 loader.close(cnx->dso);
721 cnx->dso = NULL;
722 }
723 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800724 } else {
725 LOGD("3D hardware acceleration is disabled");
726 }
727 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700728
Mathias Agopian923c6612009-08-17 18:07:06 -0700729 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
730 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731 }
732
Mathias Agopian923c6612009-08-17 18:07:06 -0700733 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734}
735
Mathias Agopian923c6612009-08-17 18:07:06 -0700736EGLBoolean egl_init_drivers()
737{
738 EGLBoolean res;
739 pthread_mutex_lock(&gInitDriverMutex);
740 res = egl_init_drivers_locked();
741 pthread_mutex_unlock(&gInitDriverMutex);
742 return res;
743}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700744
745// ----------------------------------------------------------------------------
746}; // namespace android
747// ----------------------------------------------------------------------------
748
749using namespace android;
750
751EGLDisplay eglGetDisplay(NativeDisplayType display)
752{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800753 clearError();
754
Mathias Agopian923c6612009-08-17 18:07:06 -0700755 uint32_t index = uint32_t(display);
756 if (index >= NUM_DISPLAYS) {
757 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
758 }
759
760 if (egl_init_drivers() == EGL_FALSE) {
761 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
762 }
763
764 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
765 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700766}
767
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768// ----------------------------------------------------------------------------
769// Initialization
770// ----------------------------------------------------------------------------
771
772EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
773{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800774 clearError();
775
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800776 egl_display_t * const dp = get_display(dpy);
777 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
778
Mathias Agopian75bc2782010-02-05 16:17:01 -0800779 Mutex::Autolock _l(dp->lock);
780
781 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 if (major != NULL) *major = VERSION_MAJOR;
783 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700784 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 return EGL_TRUE;
786 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700787
788#if EGL_TRACE
789
790 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
791 // the information from that call may be stale.)
792 initEglTraceLevel();
793
794#endif
795
796 setGLHooksThreadSpecific(&gHooksNoContext);
797
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 // initialize each EGL and
799 // build our own extension string first, based on the extension we know
800 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700801 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 egl_connection_t* const cnx = &gEGLImpl[i];
803 cnx->major = -1;
804 cnx->minor = -1;
805 if (!cnx->dso)
806 continue;
807
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700808#if defined(ADRENO130)
809#warning "Adreno-130 eglInitialize() workaround"
810 /*
811 * The ADRENO 130 driver returns a different EGLDisplay each time
812 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
813 * after eglTerminate() has been called, so that eglInitialize()
814 * cannot be called again. Therefore, we need to make sure to call
815 * eglGetDisplay() before calling eglInitialize();
816 */
817 if (i == IMPL_HARDWARE) {
818 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700819 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700820 }
821#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700823
824 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700825 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800826 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700827 // i, idpy, cnx->major, cnx->minor, cnx);
828
829 // display is now initialized
830 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831
832 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700833 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700834 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700835 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700836 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700837 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700838 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700839 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700840 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800841
842 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700843 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700844 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800845 }
846 }
847
848 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700849 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 egl_connection_t* const cnx = &gEGLImpl[i];
851 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
852 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700853 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700854 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
855 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700856 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700857 dp->disp[i].dpy, dp->disp[i].config, n,
858 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800859 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800860 dp->numTotalConfigs += n;
861 res = EGL_TRUE;
862 }
863 }
864 }
865 }
866 }
867
868 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700869 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
870 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
871 egl_connection_t* const cnx = &gEGLImpl[i];
872 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
873 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
874 dp->configs[k].impl = i;
875 dp->configs[k].config = dp->disp[i].config[j];
876 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
877 // store the implementation's CONFIG_ID
878 cnx->egl.eglGetConfigAttrib(
879 dp->disp[i].dpy,
880 dp->disp[i].config[j],
881 EGL_CONFIG_ID,
882 &dp->configs[k].implConfigId);
883 k++;
884 }
885 }
886 }
887
888 // sort our configurations so we can do binary-searches
889 qsort( dp->configs,
890 dp->numTotalConfigs,
891 sizeof(egl_config_t), cmp_configs);
892
Mathias Agopian75bc2782010-02-05 16:17:01 -0800893 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 if (major != NULL) *major = VERSION_MAJOR;
895 if (minor != NULL) *minor = VERSION_MINOR;
896 return EGL_TRUE;
897 }
898 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
899}
900
901EGLBoolean eglTerminate(EGLDisplay dpy)
902{
Mathias Agopian923c6612009-08-17 18:07:06 -0700903 // NOTE: don't unload the drivers b/c some APIs can be called
904 // after eglTerminate() has been called. eglTerminate() only
905 // terminates an EGLDisplay, not a EGL itself.
906
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800907 clearError();
908
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 egl_display_t* const dp = get_display(dpy);
910 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800911
912 Mutex::Autolock _l(dp->lock);
913
914 if (dp->refs == 0) {
915 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
916 }
917
918 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700919 if (dp->refs > 1) {
920 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800921 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700922 }
Mathias Agopiande586972009-05-28 17:39:03 -0700923
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700925 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800926 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700927 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700928 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700929 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700930 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700931 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700932 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700933 free(dp->disp[i].config);
934
935 dp->disp[i].numConfigs = 0;
936 dp->disp[i].config = 0;
937 dp->disp[i].state = egl_display_t::TERMINATED;
938
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 res = EGL_TRUE;
940 }
941 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700942
943 // TODO: all egl_object_t should be marked for termination
944
Mathias Agopian75bc2782010-02-05 16:17:01 -0800945 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700947 delete [] dp->configs;
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -0800948
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949 return res;
950}
951
952// ----------------------------------------------------------------------------
953// configuration
954// ----------------------------------------------------------------------------
955
956EGLBoolean eglGetConfigs( EGLDisplay dpy,
957 EGLConfig *configs,
958 EGLint config_size, EGLint *num_config)
959{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800960 clearError();
961
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 egl_display_t const * const dp = get_display(dpy);
963 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
964
965 GLint numConfigs = dp->numTotalConfigs;
966 if (!configs) {
967 *num_config = numConfigs;
968 return EGL_TRUE;
969 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700970
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700972 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
973 *configs++ = EGLConfig(i);
974 config_size--;
975 n++;
976 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977
978 *num_config = n;
979 return EGL_TRUE;
980}
981
982EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
983 EGLConfig *configs, EGLint config_size,
984 EGLint *num_config)
985{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800986 clearError();
987
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988 egl_display_t const * const dp = get_display(dpy);
989 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
990
Jack Palevich749c63d2009-03-25 15:12:17 -0700991 if (num_config==0) {
992 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993 }
994
995 EGLint n;
996 EGLBoolean res = EGL_FALSE;
997 *num_config = 0;
998
999
1000 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -07001001 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001002 // to figure out both its size and if it contains an EGL_CONFIG_ID
1003 // key. If so, the full array is copied and patched.
1004 // NOTE: we assume that there can be only one occurrence
1005 // of EGL_CONFIG_ID.
1006
1007 EGLint patch_index = -1;
1008 GLint attr;
1009 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -07001010 if (attrib_list) {
1011 while ((attr=attrib_list[size]) != EGL_NONE) {
1012 if (attr == EGL_CONFIG_ID)
1013 patch_index = size;
1014 size += 2;
1015 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 }
1017 if (patch_index >= 0) {
1018 size += 2; // we need copy the sentinel as well
1019 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
1020 if (new_list == 0)
1021 return setError(EGL_BAD_ALLOC, EGL_FALSE);
1022 memcpy(new_list, attrib_list, size*sizeof(EGLint));
1023
1024 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -07001025 bool found = false;
1026 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -07001028 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
1029 if (dp->configs[i].configId == configId) {
1030 ourConfig = EGLConfig(i);
1031 configId = dp->configs[i].implConfigId;
1032 found = true;
1033 break;
1034 }
1035 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036
Mathias Agopiancee79392010-07-26 21:14:59 -07001037 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
1038 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 // and switch to the new list
1040 attrib_list = const_cast<const EGLint *>(new_list);
1041
1042 // At this point, the only configuration that can match is
1043 // dp->configs[i][index], however, we don't know if it would be
1044 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -07001045 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 // through all the EGLimpl[].
1047 // We also know we can only get a single config back, and we know
1048 // which one.
1049
Mathias Agopian618fa102009-10-14 02:06:37 -07001050 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -07001051 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
1052 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 if (res && n>0) {
1054 // n has to be 0 or 1, by construction, and we already know
1055 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -07001056 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001057 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -07001058 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 *num_config = 1;
1060 }
1061 }
1062
1063 free(const_cast<EGLint *>(attrib_list));
1064 return res;
1065 }
1066
Mathias Agopiancee79392010-07-26 21:14:59 -07001067
Mathias Agopian618fa102009-10-14 02:06:37 -07001068 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001069 egl_connection_t* const cnx = &gEGLImpl[i];
1070 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001071 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001072 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001073 if (configs) {
1074 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -07001075 // internal EGLConfig format.
1076 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -07001077 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001078 egl_config_t key(i, configs[j]);
1079 intptr_t index = binarySearch<egl_config_t>(
1080 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -07001081 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001082 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -07001083 } else {
1084 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1085 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 }
Jack Palevich749c63d2009-03-25 15:12:17 -07001087 configs += n;
1088 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 *num_config += n;
1091 res = EGL_TRUE;
1092 }
1093 }
1094 }
1095 return res;
1096}
1097
1098EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1099 EGLint attribute, EGLint *value)
1100{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001101 clearError();
1102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001104 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 if (!cnx) return EGL_FALSE;
1106
1107 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001108 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 return EGL_TRUE;
1110 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001111 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -07001112 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1113 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001114}
1115
1116// ----------------------------------------------------------------------------
1117// surfaces
1118// ----------------------------------------------------------------------------
1119
1120EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1121 NativeWindowType window,
1122 const EGLint *attrib_list)
1123{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001124 clearError();
1125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001126 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001127 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128 if (cnx) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001129 EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy;
1130 EGLConfig iConfig = dp->configs[intptr_t(config)].config;
1131 EGLint format;
1132
1133 // set the native window's buffers format to match this config
1134 if (cnx->egl.eglGetConfigAttrib(iDpy,
1135 iConfig, EGL_NATIVE_VISUAL_ID, &format)) {
1136 if (format != 0) {
1137 native_window_set_buffers_geometry(window, 0, 0, format);
1138 }
1139 }
1140
Mathias Agopian618fa102009-10-14 02:06:37 -07001141 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001142 iDpy, iConfig, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001144 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001145 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001146 return s;
1147 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148 }
1149 return EGL_NO_SURFACE;
1150}
1151
1152EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1153 NativePixmapType pixmap,
1154 const EGLint *attrib_list)
1155{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001156 clearError();
1157
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001158 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001159 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001161 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001162 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1163 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001165 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001166 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 return s;
1168 }
1169 }
1170 return EGL_NO_SURFACE;
1171}
1172
1173EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1174 const EGLint *attrib_list)
1175{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001176 clearError();
1177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001179 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001181 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001182 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1183 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001185 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001186 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 return s;
1188 }
1189 }
1190 return EGL_NO_SURFACE;
1191}
1192
1193EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1194{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001195 clearError();
1196
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001197 SurfaceRef _s(surface);
1198 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 if (!validate_display_surface(dpy, surface))
1201 return EGL_FALSE;
1202 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001204 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001205 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001206 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001207 if (result == EGL_TRUE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001208 if (s->win != NULL) {
1209 native_window_set_buffers_geometry(s->win.get(), 0, 0, 0);
1210 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001211 _s.terminate();
1212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 return result;
1214}
1215
1216EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1217 EGLint attribute, EGLint *value)
1218{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001219 clearError();
1220
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001221 SurfaceRef _s(surface);
1222 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001224 if (!validate_display_surface(dpy, surface))
1225 return EGL_FALSE;
1226 egl_display_t const * const dp = get_display(dpy);
1227 egl_surface_t const * const s = get_surface(surface);
1228
Mathias Agopiancee79392010-07-26 21:14:59 -07001229 EGLBoolean result(EGL_TRUE);
1230 if (attribute == EGL_CONFIG_ID) {
1231 // We need to remap EGL_CONFIG_IDs
1232 *value = dp->configs[intptr_t(s->config)].configId;
1233 } else {
1234 result = s->cnx->egl.eglQuerySurface(
1235 dp->disp[s->impl].dpy, s->surface, attribute, value);
1236 }
1237
1238 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239}
1240
1241// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001242// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243// ----------------------------------------------------------------------------
1244
1245EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1246 EGLContext share_list, const EGLint *attrib_list)
1247{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001248 clearError();
1249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001251 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001253 if (share_list != EGL_NO_CONTEXT) {
1254 egl_context_t* const c = get_context(share_list);
1255 share_list = c->context;
1256 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001257 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001258 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1259 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001260 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001262 // figure out if it's a GLESv1 or GLESv2
1263 int version = 0;
1264 if (attrib_list) {
1265 while (*attrib_list != EGL_NONE) {
1266 GLint attr = *attrib_list++;
1267 GLint value = *attrib_list++;
1268 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1269 if (value == 1) {
1270 version = GLESv1_INDEX;
1271 } else if (value == 2) {
1272 version = GLESv2_INDEX;
1273 }
1274 }
1275 };
1276 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001277 egl_context_t* c = new egl_context_t(dpy, context, config,
1278 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 return c;
1280 }
1281 }
1282 return EGL_NO_CONTEXT;
1283}
1284
1285EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1286{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001287 clearError();
1288
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001289 ContextRef _c(ctx);
1290 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1291
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 if (!validate_display_context(dpy, ctx))
1293 return EGL_FALSE;
1294 egl_display_t const * const dp = get_display(dpy);
1295 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001296 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001297 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001298 if (result == EGL_TRUE) {
1299 _c.terminate();
1300 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 return result;
1302}
1303
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001304static void loseCurrent(egl_context_t * cur_c)
1305{
1306 if (cur_c) {
1307 egl_surface_t * cur_r = get_surface(cur_c->read);
1308 egl_surface_t * cur_d = get_surface(cur_c->draw);
1309
1310 // by construction, these are either 0 or valid (possibly terminated)
1311 // it should be impossible for these to be invalid
1312 ContextRef _cur_c(cur_c);
1313 SurfaceRef _cur_r(cur_r);
1314 SurfaceRef _cur_d(cur_d);
1315
1316 cur_c->read = NULL;
1317 cur_c->draw = NULL;
1318
1319 _cur_c.release();
1320 _cur_r.release();
1321 _cur_d.release();
1322 }
1323}
1324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1326 EGLSurface read, EGLContext ctx)
1327{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001328 clearError();
1329
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001330 // get a reference to the object passed in
1331 ContextRef _c(ctx);
1332 SurfaceRef _d(draw);
1333 SurfaceRef _r(read);
1334
1335 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336 egl_display_t const * const dp = get_display(dpy);
1337 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001338 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1339 // EGL_NO_CONTEXT is valid
1340 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001341 }
1342
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001343 // these are the underlying implementation's object
1344 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001345 EGLSurface impl_draw = EGL_NO_SURFACE;
1346 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001347
1348 // these are our objects structs passed in
1349 egl_context_t * c = NULL;
1350 egl_surface_t const * d = NULL;
1351 egl_surface_t const * r = NULL;
1352
1353 // these are the current objects structs
1354 egl_context_t * cur_c = get_context(getContext());
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001355
1356 if (ctx != EGL_NO_CONTEXT) {
1357 c = get_context(ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001358 impl_ctx = c->context;
1359 } else {
1360 // no context given, use the implementation of the current context
1361 if (cur_c == NULL) {
1362 // no current context
1363 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001364 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1365 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001366 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001367 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001368 return EGL_TRUE;
1369 }
1370 }
1371
1372 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001374 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001375 // make sure the EGLContext and EGLSurface passed in are for
1376 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001377 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001379 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001381
1382 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001384 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001385 // make sure the EGLContext and EGLSurface passed in are for
1386 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001387 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001389 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001391
1392 EGLBoolean result;
1393
1394 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001395 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001396 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001397 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001398 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001399 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001400 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401
1402 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001403
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001404 loseCurrent(cur_c);
1405
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001406 if (ctx != EGL_NO_CONTEXT) {
David Li65948aa2011-03-10 16:40:37 -08001407 if (!c->dbg && gEGLDebugLevel > 0)
1408 c->dbg = CreateDbgContext(c->version, c->cnx->hooks[c->version]);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001409 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001410 setContext(ctx);
1411 _c.acquire();
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001412 _r.acquire();
1413 _d.acquire();
1414 c->read = read;
1415 c->draw = draw;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001416 } else {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001417 setGLHooksThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001418 setContext(EGL_NO_CONTEXT);
1419 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 }
1421 return result;
1422}
1423
1424
1425EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1426 EGLint attribute, EGLint *value)
1427{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001428 clearError();
1429
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001430 ContextRef _c(ctx);
1431 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1432
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 if (!validate_display_context(dpy, ctx))
1434 return EGL_FALSE;
1435
1436 egl_display_t const * const dp = get_display(dpy);
1437 egl_context_t * const c = get_context(ctx);
1438
Mathias Agopiancee79392010-07-26 21:14:59 -07001439 EGLBoolean result(EGL_TRUE);
1440 if (attribute == EGL_CONFIG_ID) {
1441 *value = dp->configs[intptr_t(c->config)].configId;
1442 } else {
1443 // We need to remap EGL_CONFIG_IDs
1444 result = c->cnx->egl.eglQueryContext(
1445 dp->disp[c->impl].dpy, c->context, attribute, value);
1446 }
1447
1448 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449}
1450
1451EGLContext eglGetCurrentContext(void)
1452{
Mathias Agopian923c6612009-08-17 18:07:06 -07001453 // could be called before eglInitialize(), but we wouldn't have a context
1454 // then, and this function would correctly return EGL_NO_CONTEXT.
1455
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001456 clearError();
1457
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 EGLContext ctx = getContext();
1459 return ctx;
1460}
1461
1462EGLSurface eglGetCurrentSurface(EGLint readdraw)
1463{
Mathias Agopian923c6612009-08-17 18:07:06 -07001464 // could be called before eglInitialize(), but we wouldn't have a context
1465 // then, and this function would correctly return EGL_NO_SURFACE.
1466
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001467 clearError();
1468
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469 EGLContext ctx = getContext();
1470 if (ctx) {
1471 egl_context_t const * const c = get_context(ctx);
1472 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1473 switch (readdraw) {
1474 case EGL_READ: return c->read;
1475 case EGL_DRAW: return c->draw;
1476 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1477 }
1478 }
1479 return EGL_NO_SURFACE;
1480}
1481
1482EGLDisplay eglGetCurrentDisplay(void)
1483{
Mathias Agopian923c6612009-08-17 18:07:06 -07001484 // could be called before eglInitialize(), but we wouldn't have a context
1485 // then, and this function would correctly return EGL_NO_DISPLAY.
1486
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001487 clearError();
1488
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 EGLContext ctx = getContext();
1490 if (ctx) {
1491 egl_context_t const * const c = get_context(ctx);
1492 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1493 return c->dpy;
1494 }
1495 return EGL_NO_DISPLAY;
1496}
1497
1498EGLBoolean eglWaitGL(void)
1499{
Mathias Agopian923c6612009-08-17 18:07:06 -07001500 // could be called before eglInitialize(), but we wouldn't have a context
1501 // then, and this function would return GL_TRUE, which isn't wrong.
1502
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001503 clearError();
1504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505 EGLBoolean res = EGL_TRUE;
1506 EGLContext ctx = getContext();
1507 if (ctx) {
1508 egl_context_t const * const c = get_context(ctx);
1509 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1510 if (uint32_t(c->impl)>=2)
1511 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1512 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1513 if (!cnx->dso)
1514 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001515 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 }
1517 return res;
1518}
1519
1520EGLBoolean eglWaitNative(EGLint engine)
1521{
Mathias Agopian923c6612009-08-17 18:07:06 -07001522 // could be called before eglInitialize(), but we wouldn't have a context
1523 // then, and this function would return GL_TRUE, which isn't wrong.
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001524
1525 clearError();
1526
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527 EGLBoolean res = EGL_TRUE;
1528 EGLContext ctx = getContext();
1529 if (ctx) {
1530 egl_context_t const * const c = get_context(ctx);
1531 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1532 if (uint32_t(c->impl)>=2)
1533 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1534 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1535 if (!cnx->dso)
1536 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001537 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 }
1539 return res;
1540}
1541
1542EGLint eglGetError(void)
1543{
1544 EGLint result = EGL_SUCCESS;
Mathias Agopian02dafb52010-09-21 15:43:59 -07001545 EGLint err;
Mathias Agopian618fa102009-10-14 02:06:37 -07001546 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian02dafb52010-09-21 15:43:59 -07001547 err = EGL_SUCCESS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548 egl_connection_t* const cnx = &gEGLImpl[i];
1549 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001550 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1552 result = err;
1553 }
Mathias Agopian02dafb52010-09-21 15:43:59 -07001554 err = getError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555 if (result == EGL_SUCCESS)
Mathias Agopian02dafb52010-09-21 15:43:59 -07001556 result = err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557 return result;
1558}
1559
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001560// Note: Similar implementations of these functions also exist in
1561// gl2.cpp and gl.cpp, and are used by applications that call the
1562// exported entry points directly.
1563typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
1564typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
1565
1566static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL;
1567static PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL;
1568
1569static void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image)
1570{
1571 GLeglImageOES implImage =
1572 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1573 glEGLImageTargetTexture2DOES_impl(target, implImage);
1574}
1575
1576static void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image)
1577{
1578 GLeglImageOES implImage =
1579 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1580 glEGLImageTargetRenderbufferStorageOES_impl(target, implImage);
1581}
1582
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001583__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001585 // eglGetProcAddress() could be the very first function called
1586 // in which case we must make sure we've initialized ourselves, this
1587 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001588
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001589 clearError();
1590
Mathias Agopian923c6612009-08-17 18:07:06 -07001591 if (egl_init_drivers() == EGL_FALSE) {
1592 setError(EGL_BAD_PARAMETER, NULL);
1593 return NULL;
1594 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001595
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596 __eglMustCastToProperFunctionPointerType addr;
1597 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1598 if (addr) return addr;
1599
Mathias Agopian24035332010-08-02 17:34:32 -07001600 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1601 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602
Mathias Agopian24035332010-08-02 17:34:32 -07001603 /*
1604 * Since eglGetProcAddress() is not associated to anything, it needs
1605 * to return a function pointer that "works" regardless of what
1606 * the current context is.
1607 *
1608 * For this reason, we return a "forwarder", a small stub that takes
1609 * care of calling the function associated with the context
1610 * currently bound.
1611 *
1612 * We first look for extensions we've already resolved, if we're seeing
1613 * this extension for the first time, we go through all our
1614 * implementations and call eglGetProcAddress() and record the
1615 * result in the appropriate implementation hooks and return the
1616 * address of the forwarder corresponding to that hook set.
1617 *
1618 */
1619
1620 const String8 name(procname);
1621 addr = gGLExtentionMap.valueFor(name);
1622 const int slot = gGLExtentionSlot;
1623
1624 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1625 "no more slots for eglGetProcAddress(\"%s\")",
1626 procname);
1627
1628 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1629 bool found = false;
1630 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1631 egl_connection_t* const cnx = &gEGLImpl[i];
1632 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1633 found = true;
Mathias Agopian4a88b522010-08-13 12:19:04 -07001634 // Extensions are independent of the bound context
1635 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1636 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001637#if EGL_TRACE
David Li2f5a6552011-03-01 16:08:10 -08001638 gHooksDebug.ext.extensions[slot] = gHooksTrace.ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001639#endif
Mathias Agopian24035332010-08-02 17:34:32 -07001640 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 }
1642 }
Mathias Agopian24035332010-08-02 17:34:32 -07001643 if (found) {
1644 addr = gExtensionForwarders[slot];
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001645
1646 if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) {
1647 glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr;
1648 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper;
1649 }
1650 if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) {
1651 glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr;
1652 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper;
1653 }
1654
Mathias Agopian24035332010-08-02 17:34:32 -07001655 gGLExtentionMap.add(name, addr);
1656 gGLExtentionSlot++;
1657 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659
Mathias Agopian24035332010-08-02 17:34:32 -07001660 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian24035332010-08-02 17:34:32 -07001661 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662}
1663
1664EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1665{
David Lib33d5cf2011-03-04 17:50:48 -08001666 EGLBoolean Debug_eglSwapBuffers(EGLDisplay dpy, EGLSurface draw);
1667 if (gEGLDebugLevel > 0)
1668 Debug_eglSwapBuffers(dpy, draw);
1669
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001670 clearError();
1671
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001672 SurfaceRef _s(draw);
1673 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1674
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675 if (!validate_display_surface(dpy, draw))
1676 return EGL_FALSE;
1677 egl_display_t const * const dp = get_display(dpy);
1678 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001679 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680}
1681
1682EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1683 NativePixmapType target)
1684{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001685 clearError();
1686
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001687 SurfaceRef _s(surface);
1688 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1689
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 if (!validate_display_surface(dpy, surface))
1691 return EGL_FALSE;
1692 egl_display_t const * const dp = get_display(dpy);
1693 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001694 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001695 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001696}
1697
1698const char* eglQueryString(EGLDisplay dpy, EGLint name)
1699{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001700 clearError();
1701
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001702 egl_display_t const * const dp = get_display(dpy);
1703 switch (name) {
1704 case EGL_VENDOR:
1705 return gVendorString;
1706 case EGL_VERSION:
1707 return gVersionString;
1708 case EGL_EXTENSIONS:
1709 return gExtensionString;
1710 case EGL_CLIENT_APIS:
1711 return gClientApiString;
1712 }
1713 return setError(EGL_BAD_PARAMETER, (const char *)0);
1714}
1715
1716
1717// ----------------------------------------------------------------------------
1718// EGL 1.1
1719// ----------------------------------------------------------------------------
1720
1721EGLBoolean eglSurfaceAttrib(
1722 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1723{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001724 clearError();
1725
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001726 SurfaceRef _s(surface);
1727 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 if (!validate_display_surface(dpy, surface))
1730 return EGL_FALSE;
1731 egl_display_t const * const dp = get_display(dpy);
1732 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001733 if (s->cnx->egl.eglSurfaceAttrib) {
1734 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001735 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001736 }
1737 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1738}
1739
1740EGLBoolean eglBindTexImage(
1741 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1742{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001743 clearError();
1744
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001745 SurfaceRef _s(surface);
1746 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1747
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001748 if (!validate_display_surface(dpy, surface))
1749 return EGL_FALSE;
1750 egl_display_t const * const dp = get_display(dpy);
1751 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001752 if (s->cnx->egl.eglBindTexImage) {
1753 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001754 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 }
1756 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1757}
1758
1759EGLBoolean eglReleaseTexImage(
1760 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1761{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001762 clearError();
1763
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001764 SurfaceRef _s(surface);
1765 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1766
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 if (!validate_display_surface(dpy, surface))
1768 return EGL_FALSE;
1769 egl_display_t const * const dp = get_display(dpy);
1770 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001771 if (s->cnx->egl.eglReleaseTexImage) {
1772 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001773 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774 }
1775 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1776}
1777
1778EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1779{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001780 clearError();
1781
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782 egl_display_t * const dp = get_display(dpy);
1783 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1784
1785 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001786 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001787 egl_connection_t* const cnx = &gEGLImpl[i];
1788 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001789 if (cnx->egl.eglSwapInterval) {
1790 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001791 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001792 res = EGL_FALSE;
1793 }
1794 }
1795 }
1796 }
1797 return res;
1798}
1799
1800
1801// ----------------------------------------------------------------------------
1802// EGL 1.2
1803// ----------------------------------------------------------------------------
1804
1805EGLBoolean eglWaitClient(void)
1806{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001807 clearError();
1808
Mathias Agopian923c6612009-08-17 18:07:06 -07001809 // could be called before eglInitialize(), but we wouldn't have a context
1810 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811 EGLBoolean res = EGL_TRUE;
1812 EGLContext ctx = getContext();
1813 if (ctx) {
1814 egl_context_t const * const c = get_context(ctx);
1815 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1816 if (uint32_t(c->impl)>=2)
1817 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1818 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1819 if (!cnx->dso)
1820 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001821 if (cnx->egl.eglWaitClient) {
1822 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001823 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001824 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 }
1826 }
1827 return res;
1828}
1829
1830EGLBoolean eglBindAPI(EGLenum api)
1831{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001832 clearError();
1833
Mathias Agopian923c6612009-08-17 18:07:06 -07001834 if (egl_init_drivers() == EGL_FALSE) {
1835 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1836 }
1837
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001838 // bind this API on all EGLs
1839 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001840 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001841 egl_connection_t* const cnx = &gEGLImpl[i];
1842 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001843 if (cnx->egl.eglBindAPI) {
1844 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001845 res = EGL_FALSE;
1846 }
1847 }
1848 }
1849 }
1850 return res;
1851}
1852
1853EGLenum eglQueryAPI(void)
1854{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001855 clearError();
1856
Mathias Agopian923c6612009-08-17 18:07:06 -07001857 if (egl_init_drivers() == EGL_FALSE) {
1858 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1859 }
1860
Mathias Agopian618fa102009-10-14 02:06:37 -07001861 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001862 egl_connection_t* const cnx = &gEGLImpl[i];
1863 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001864 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001865 // the first one we find is okay, because they all
1866 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001867 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001868 }
1869 }
1870 }
1871 // or, it can only be OpenGL ES
1872 return EGL_OPENGL_ES_API;
1873}
1874
1875EGLBoolean eglReleaseThread(void)
1876{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001877 clearError();
1878
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001879 // If there is context bound to the thread, release it
1880 loseCurrent(get_context(getContext()));
1881
Mathias Agopian618fa102009-10-14 02:06:37 -07001882 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883 egl_connection_t* const cnx = &gEGLImpl[i];
1884 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001885 if (cnx->egl.eglReleaseThread) {
1886 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 }
1888 }
1889 }
1890 clearTLS();
1891 return EGL_TRUE;
1892}
1893
1894EGLSurface eglCreatePbufferFromClientBuffer(
1895 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1896 EGLConfig config, const EGLint *attrib_list)
1897{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001898 clearError();
1899
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001901 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001903 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1904 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001905 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1906 buftype, buffer,
1907 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908 }
1909 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1910}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001911
1912// ----------------------------------------------------------------------------
1913// EGL_EGLEXT_VERSION 3
1914// ----------------------------------------------------------------------------
1915
1916EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1917 const EGLint *attrib_list)
1918{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001919 clearError();
1920
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001921 SurfaceRef _s(surface);
1922 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1923
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001924 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001925 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001926
1927 egl_display_t const * const dp = get_display(dpy);
1928 egl_surface_t const * const s = get_surface(surface);
1929
Mathias Agopian618fa102009-10-14 02:06:37 -07001930 if (s->cnx->egl.eglLockSurfaceKHR) {
1931 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001932 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001933 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001934 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001935}
1936
1937EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1938{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001939 clearError();
1940
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001941 SurfaceRef _s(surface);
1942 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1943
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001944 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001945 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001946
1947 egl_display_t const * const dp = get_display(dpy);
1948 egl_surface_t const * const s = get_surface(surface);
1949
Mathias Agopian618fa102009-10-14 02:06:37 -07001950 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1951 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001952 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001953 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001954 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001955}
1956
1957EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1958 EGLClientBuffer buffer, const EGLint *attrib_list)
1959{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001960 clearError();
1961
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001962 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001963 ContextRef _c(ctx);
1964 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001965 if (!validate_display_context(dpy, ctx))
1966 return EGL_NO_IMAGE_KHR;
1967 egl_display_t const * const dp = get_display(dpy);
1968 egl_context_t * const c = get_context(ctx);
1969 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001970 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001971 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001972 if (image == EGL_NO_IMAGE_KHR)
1973 return image;
1974
1975 egl_image_t* result = new egl_image_t(dpy, ctx);
1976 result->images[c->impl] = image;
1977 return (EGLImageKHR)result;
1978 } else {
1979 // EGL_NO_CONTEXT is a valid parameter
1980 egl_display_t const * const dp = get_display(dpy);
1981 if (dp == 0) {
1982 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1983 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001984
1985 /* Since we don't have a way to know which implementation to call,
1986 * we're calling all of them. If at least one of the implementation
1987 * succeeded, this is a success.
1988 */
1989
1990 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001991
Mathias Agopian618fa102009-10-14 02:06:37 -07001992 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001993 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001994 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001995 egl_connection_t* const cnx = &gEGLImpl[i];
1996 implImages[i] = EGL_NO_IMAGE_KHR;
1997 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001998 if (cnx->egl.eglCreateImageKHR) {
1999 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002000 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002001 if (implImages[i] != EGL_NO_IMAGE_KHR) {
2002 success = true;
2003 }
2004 }
2005 }
2006 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002007
2008 if (!success) {
2009 // failure, if there was an error when we entered this function,
2010 // the error flag must not be updated.
2011 // Otherwise, the error is whatever happened in the implementation
2012 // that faulted.
2013 if (currentError != EGL_SUCCESS) {
2014 setError(currentError, EGL_NO_IMAGE_KHR);
2015 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002016 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002017 } else {
2018 // In case of success, we need to clear all error flags
2019 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07002020 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002021 // a "full" success (all implementation succeeded).
2022 eglGetError();
2023 }
2024
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002025 egl_image_t* result = new egl_image_t(dpy, ctx);
2026 memcpy(result->images, implImages, sizeof(implImages));
2027 return (EGLImageKHR)result;
2028 }
2029}
2030
2031EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2032{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002033 clearError();
2034
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002035 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002036 if (dp == 0) {
2037 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2038 }
2039
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002040 ImageRef _i(img);
2041 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002042
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002043 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002044 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07002045 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002046 egl_connection_t* const cnx = &gEGLImpl[i];
2047 if (image->images[i] != EGL_NO_IMAGE_KHR) {
2048 if (cnx->dso) {
Mathias Agopian77fbf8d2010-09-09 11:12:54 -07002049 if (cnx->egl.eglDestroyImageKHR) {
Mathias Agopian618fa102009-10-14 02:06:37 -07002050 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002051 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002052 success = true;
2053 }
2054 }
2055 }
2056 }
2057 }
2058 if (!success)
2059 return EGL_FALSE;
2060
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002061 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002062
Mathias Agopian24e5f522009-08-12 21:18:15 -07002063 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002064}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002065
Mathias Agopianc291f5852010-08-27 16:48:56 -07002066// ----------------------------------------------------------------------------
2067// EGL_EGLEXT_VERSION 5
2068// ----------------------------------------------------------------------------
2069
2070
2071EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
2072{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002073 clearError();
2074
Mathias Agopianc291f5852010-08-27 16:48:56 -07002075 EGLContext ctx = eglGetCurrentContext();
2076 ContextRef _c(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002077 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
Mathias Agopianc291f5852010-08-27 16:48:56 -07002078 if (!validate_display_context(dpy, ctx))
Mathias Agopiana93b9572010-09-21 16:22:10 -07002079 return EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002080 egl_display_t const * const dp = get_display(dpy);
2081 egl_context_t * const c = get_context(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002082 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002083 if (c->cnx->egl.eglCreateSyncKHR) {
2084 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
2085 dp->disp[c->impl].dpy, type, attrib_list);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002086 if (sync == EGL_NO_SYNC_KHR)
Mathias Agopianc291f5852010-08-27 16:48:56 -07002087 return sync;
2088 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
2089 }
2090 return (EGLSyncKHR)result;
2091}
2092
2093EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
2094{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002095 clearError();
2096
Mathias Agopianc291f5852010-08-27 16:48:56 -07002097 egl_display_t const * const dp = get_display(dpy);
2098 if (dp == 0) {
2099 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2100 }
2101
2102 SyncRef _s(sync);
2103 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2104 egl_sync_t* syncObject = get_sync(sync);
2105
2106 EGLContext ctx = syncObject->context;
2107 ContextRef _c(ctx);
2108 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2109 if (!validate_display_context(dpy, ctx))
2110 return EGL_FALSE;
2111
2112 egl_context_t * const c = get_context(ctx);
2113
2114 if (c->cnx->egl.eglDestroySyncKHR) {
2115 return c->cnx->egl.eglDestroySyncKHR(
2116 dp->disp[c->impl].dpy, syncObject->sync);
2117 }
2118
2119 return EGL_FALSE;
2120}
2121
2122EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
2123{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002124 clearError();
2125
Mathias Agopianc291f5852010-08-27 16:48:56 -07002126 egl_display_t const * const dp = get_display(dpy);
2127 if (dp == 0) {
2128 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2129 }
2130
2131 SyncRef _s(sync);
2132 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2133 egl_sync_t* syncObject = get_sync(sync);
2134
2135 EGLContext ctx = syncObject->context;
2136 ContextRef _c(ctx);
2137 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2138 if (!validate_display_context(dpy, ctx))
2139 return EGL_FALSE;
2140
2141 egl_context_t * const c = get_context(ctx);
2142
2143 if (c->cnx->egl.eglClientWaitSyncKHR) {
2144 return c->cnx->egl.eglClientWaitSyncKHR(
2145 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
2146 }
2147
2148 return EGL_FALSE;
2149}
2150
2151EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
2152{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002153 clearError();
2154
Mathias Agopianc291f5852010-08-27 16:48:56 -07002155 egl_display_t const * const dp = get_display(dpy);
2156 if (dp == 0) {
2157 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2158 }
2159
2160 SyncRef _s(sync);
2161 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2162 egl_sync_t* syncObject = get_sync(sync);
2163
2164 EGLContext ctx = syncObject->context;
2165 ContextRef _c(ctx);
2166 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2167 if (!validate_display_context(dpy, ctx))
2168 return EGL_FALSE;
2169
2170 egl_context_t * const c = get_context(ctx);
2171
2172 if (c->cnx->egl.eglGetSyncAttribKHR) {
2173 return c->cnx->egl.eglGetSyncAttribKHR(
2174 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
2175 }
2176
2177 return EGL_FALSE;
2178}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002179
2180// ----------------------------------------------------------------------------
2181// ANDROID extensions
2182// ----------------------------------------------------------------------------
2183
2184EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2185 EGLint left, EGLint top, EGLint width, EGLint height)
2186{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002187 clearError();
2188
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002189 SurfaceRef _s(draw);
2190 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
2191
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002192 if (!validate_display_surface(dpy, draw))
2193 return EGL_FALSE;
2194 egl_display_t const * const dp = get_display(dpy);
2195 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07002196 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
2197 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002198 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002199 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07002200 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002201}