blob: e427504ab9361b37bf648d90cd6e25f75cbc0bc8 [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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
50
51// ----------------------------------------------------------------------------
52namespace android {
53// ----------------------------------------------------------------------------
54
55#define VERSION_MAJOR 1
56#define VERSION_MINOR 4
57static char const * const gVendorString = "Android";
Mathias Agopian923c6612009-08-17 18:07:06 -070058static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -070060static char const * const gExtensionString =
61 "EGL_KHR_image "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -070062 "EGL_KHR_image_base "
63 "EGL_KHR_image_pixmap "
Mathias Agopian8e4b5a32010-08-27 16:08:03 -070064 "EGL_KHR_gl_texture_2D_image "
Michael I. Goldca41e362011-01-13 10:13:15 -080065 "EGL_KHR_gl_texture_cubemap_image "
66 "EGL_KHR_gl_renderbuffer_image "
Mathias Agopianc291f5852010-08-27 16:48:56 -070067 "EGL_KHR_fence_sync "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070068 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070069 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 ;
71
72// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
Mathias Agopian9429e9c2009-08-21 02:18:25 -070074class egl_object_t {
75 static SortedVector<egl_object_t*> sObjects;
76 static Mutex sLock;
77
78 volatile int32_t terminated;
79 mutable volatile int32_t count;
80
81public:
82 egl_object_t() : terminated(0), count(1) {
83 Mutex::Autolock _l(sLock);
84 sObjects.add(this);
85 }
86
87 inline bool isAlive() const { return !terminated; }
88
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -070090 bool get() {
91 Mutex::Autolock _l(sLock);
92 if (egl_object_t::sObjects.indexOf(this) >= 0) {
93 android_atomic_inc(&count);
94 return true;
95 }
96 return false;
97 }
98
99 bool put() {
100 Mutex::Autolock _l(sLock);
101 if (android_atomic_dec(&count) == 1) {
102 sObjects.remove(this);
103 return true;
104 }
105 return false;
106 }
107
108public:
109 template <typename N, typename T>
110 struct LocalRef {
111 N* ref;
112 LocalRef(T o) : ref(0) {
113 N* native = reinterpret_cast<N*>(o);
114 if (o && native->get()) {
115 ref = native;
116 }
117 }
118 ~LocalRef() {
119 if (ref && ref->put()) {
120 delete ref;
121 }
122 }
123 inline N* get() {
124 return ref;
125 }
126 void acquire() const {
127 if (ref) {
128 android_atomic_inc(&ref->count);
129 }
130 }
131 void release() const {
132 if (ref) {
133 int32_t c = android_atomic_dec(&ref->count);
134 // ref->count cannot be 1 prior atomic_dec because we have
135 // a reference, and if we have one, it means there was
136 // already one before us.
137 LOGE_IF(c==1, "refcount is now 0 in release()");
138 }
139 }
140 void terminate() {
141 if (ref) {
142 ref->terminated = 1;
143 release();
144 }
145 }
146 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147};
148
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700149SortedVector<egl_object_t*> egl_object_t::sObjects;
150Mutex egl_object_t::sLock;
151
Mathias Agopiancee79392010-07-26 21:14:59 -0700152
153struct egl_config_t {
154 egl_config_t() {}
155 egl_config_t(int impl, EGLConfig config)
156 : impl(impl), config(config), configId(0), implConfigId(0) { }
157 int impl; // the implementation this config is for
158 EGLConfig config; // the implementation's EGLConfig
159 EGLint configId; // our CONFIG_ID
160 EGLint implConfigId; // the implementation's CONFIG_ID
161 inline bool operator < (const egl_config_t& rhs) const {
162 if (impl < rhs.impl) return true;
163 if (impl > rhs.impl) return false;
164 return config < rhs.config;
165 }
166};
167
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700168struct egl_display_t {
169 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
170
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 struct strings_t {
172 char const * vendor;
173 char const * version;
174 char const * clientApi;
175 char const * extensions;
176 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700177
178 struct DisplayImpl {
179 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
180 state(NOT_INITIALIZED), numConfigs(0) { }
181 EGLDisplay dpy;
182 EGLConfig* config;
183 EGLint state;
184 EGLint numConfigs;
185 strings_t queryString;
186 };
187
Mathias Agopiancee79392010-07-26 21:14:59 -0700188 uint32_t magic;
189 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
190 EGLint numTotalConfigs;
191 egl_config_t* configs;
192 uint32_t refs;
193 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700194
Mathias Agopiancee79392010-07-26 21:14:59 -0700195 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700196 ~egl_display_t() { magic = 0; }
197 inline bool isValid() const { return magic == '_dpy'; }
198 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199};
200
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700201struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700203 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
204
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800205 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win,
206 EGLSurface surface, int impl, egl_connection_t const* cnx)
207 : dpy(dpy), surface(surface), config(config), win(win), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 }
209 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 }
211 EGLDisplay dpy;
212 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700213 EGLConfig config;
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800214 sp<ANativeWindow> win;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 int impl;
216 egl_connection_t const* cnx;
217};
218
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700219struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700221 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
222
Mathias Agopiancee79392010-07-26 21:14:59 -0700223 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian618fa102009-10-14 02:06:37 -0700224 int impl, egl_connection_t const* cnx, int version)
Mathias Agopianc3ce8802010-12-08 15:34:02 -0800225 : dpy(dpy), context(context), config(config), read(0), draw(0), impl(impl),
226 cnx(cnx), version(version)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 {
228 }
229 EGLDisplay dpy;
230 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700231 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 EGLSurface read;
233 EGLSurface draw;
234 int impl;
235 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700236 int version;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237};
238
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700239struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700240{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700241 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
242
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700243 egl_image_t(EGLDisplay dpy, EGLContext context)
244 : dpy(dpy), context(context)
245 {
246 memset(images, 0, sizeof(images));
247 }
248 EGLDisplay dpy;
Mathias Agopian77fbf8d2010-09-09 11:12:54 -0700249 EGLContext context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700250 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700251};
252
Mathias Agopianc291f5852010-08-27 16:48:56 -0700253struct egl_sync_t : public egl_object_t
254{
255 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
256
257 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync)
258 : dpy(dpy), context(context), sync(sync)
259 {
260 }
261 EGLDisplay dpy;
262 EGLContext context;
263 EGLSyncKHR sync;
264};
265
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700266typedef egl_surface_t::Ref SurfaceRef;
267typedef egl_context_t::Ref ContextRef;
268typedef egl_image_t::Ref ImageRef;
Mathias Agopianc291f5852010-08-27 16:48:56 -0700269typedef egl_sync_t::Ref SyncRef;
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271struct tls_t
272{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700273 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274 EGLint error;
275 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700276 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277};
278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279
280// ----------------------------------------------------------------------------
281
Mathias Agopianbf41b112010-04-09 13:37:34 -0700282static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283static egl_display_t gDisplay[NUM_DISPLAYS];
284static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
285static pthread_key_t gEGLThreadLocalStorageKey = -1;
286
287// ----------------------------------------------------------------------------
288
Mathias Agopian618fa102009-10-14 02:06:37 -0700289EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
290EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700291EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700293#if EGL_TRACE
294
295EGLAPI pthread_key_t gGLTraceKey = -1;
296
297// ----------------------------------------------------------------------------
298
David Li2f5a6552011-03-01 16:08:10 -0800299static int gEGLTraceLevel, gEGLDebugLevel;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700300static int gEGLApplicationTraceLevel;
David Li2f5a6552011-03-01 16:08:10 -0800301extern EGLAPI gl_hooks_t gHooksTrace, gHooksDebug;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700302
303static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
304 pthread_setspecific(gGLTraceKey, value);
305}
306
307gl_hooks_t const* getGLTraceThreadSpecific() {
308 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
309}
310
311static void initEglTraceLevel() {
312 char value[PROPERTY_VALUE_MAX];
313 property_get("debug.egl.trace", value, "0");
314 int propertyLevel = atoi(value);
315 int applicationLevel = gEGLApplicationTraceLevel;
316 gEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li2f5a6552011-03-01 16:08:10 -0800317
318 property_get("debug.egl.debug_proc", value, "");
319 long pid = getpid();
320 char procPath[128] = {};
321 sprintf(procPath, "/proc/%ld/cmdline", pid);
322 FILE * file = fopen(procPath, "r");
323 if (file)
324 {
325 char cmdline[256] = {};
326 if (fgets(cmdline, sizeof(cmdline) - 1, file))
327 {
328 LOGD("\n*\n*\n* initEglTraceLevel cmdline='%s' \n*\n*", cmdline);
329 if (!strcmp(value, cmdline))
330 gEGLDebugLevel = 1;
331 }
332 fclose(file);
333 }
334
335 extern void StartDebugServer();
336 if (gEGLDebugLevel > 0)
337 StartDebugServer();
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700338}
339
340static void setGLHooksThreadSpecific(gl_hooks_t const *value) {
341 if (gEGLTraceLevel > 0) {
342 setGlTraceThreadSpecific(value);
343 setGlThreadSpecific(&gHooksTrace);
David Li2f5a6552011-03-01 16:08:10 -0800344 } else if (gEGLDebugLevel > 0) {
345 setGlTraceThreadSpecific(value);
346 setGlThreadSpecific(&gHooksDebug);
347 LOGD("\n* setGLHooksThreadSpecific gHooksDebug");
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700348 } else {
349 setGlThreadSpecific(value);
350 }
351}
352
353/*
354 * Global entry point to allow applications to modify their own trace level.
355 * The effective trace level is the max of this level and the value of debug.egl.trace.
356 */
357extern "C"
358void setGLTraceLevel(int level) {
359 gEGLApplicationTraceLevel = level;
360}
361
362#else
363
364static inline void setGLHooksThreadSpecific(gl_hooks_t const *value) {
365 setGlThreadSpecific(value);
366}
367
368#endif
369
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370// ----------------------------------------------------------------------------
371
372static __attribute__((noinline))
373const char *egl_strerror(EGLint err)
374{
375 switch (err){
376 case EGL_SUCCESS: return "EGL_SUCCESS";
377 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
378 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
379 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
380 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
381 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
382 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
383 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
384 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
385 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
386 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
387 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
388 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
389 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
390 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
391 default: return "UNKNOWN";
392 }
393}
394
395static __attribute__((noinline))
396void clearTLS() {
397 if (gEGLThreadLocalStorageKey != -1) {
398 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
399 if (tls) {
400 delete tls;
401 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
402 }
403 }
404}
405
406static tls_t* getTLS()
407{
408 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
409 if (tls == 0) {
410 tls = new tls_t;
411 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
412 }
413 return tls;
414}
415
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800416static inline void clearError() {
Jamie Gennisf1cde8e2011-01-30 16:50:04 -0800417 // This must clear the error from all the underlying EGL implementations as
418 // well as the EGL wrapper layer.
419 eglGetError();
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800420}
421
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422template<typename T>
423static __attribute__((noinline))
424T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
425 if (gEGLThreadLocalStorageKey == -1) {
426 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
427 if (gEGLThreadLocalStorageKey == -1)
428 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
429 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
430 }
431 tls_t* tls = getTLS();
432 if (tls->error != error) {
433 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
434 tls->error = error;
435 }
436 return returnValue;
437}
438
439static __attribute__((noinline))
440GLint getError() {
441 if (gEGLThreadLocalStorageKey == -1)
442 return EGL_SUCCESS;
443 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
444 if (!tls) return EGL_SUCCESS;
445 GLint error = tls->error;
446 tls->error = EGL_SUCCESS;
447 return error;
448}
449
450static __attribute__((noinline))
451void setContext(EGLContext ctx) {
452 if (gEGLThreadLocalStorageKey == -1) {
453 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
454 if (gEGLThreadLocalStorageKey == -1)
455 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
456 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
457 }
458 tls_t* tls = getTLS();
459 tls->ctx = ctx;
460}
461
462static __attribute__((noinline))
463EGLContext getContext() {
464 if (gEGLThreadLocalStorageKey == -1)
465 return EGL_NO_CONTEXT;
466 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
467 if (!tls) return EGL_NO_CONTEXT;
468 return tls->ctx;
469}
470
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471/*****************************************************************************/
472
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473template<typename T>
474static __attribute__((noinline))
475int binarySearch(
476 T const sortedArray[], int first, int last, T key)
477{
478 while (first <= last) {
479 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700480 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481 first = mid + 1;
482 } else if (key < sortedArray[mid]) {
483 last = mid - 1;
484 } else {
485 return mid;
486 }
487 }
488 return -1;
489}
490
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491static int cmp_configs(const void* a, const void *b)
492{
Mathias Agopiancee79392010-07-26 21:14:59 -0700493 const egl_config_t& c0 = *(egl_config_t const *)a;
494 const egl_config_t& c1 = *(egl_config_t const *)b;
495 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496}
497
498struct extention_map_t {
499 const char* name;
500 __eglMustCastToProperFunctionPointerType address;
501};
502
503static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700504 { "eglLockSurfaceKHR",
505 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
506 { "eglUnlockSurfaceKHR",
507 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
508 { "eglCreateImageKHR",
509 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
510 { "eglDestroyImageKHR",
511 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700512 { "eglSetSwapRectangleANDROID",
513 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514};
515
Mathias Agopian24035332010-08-02 17:34:32 -0700516extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
517
518// accesses protected by gInitDriverMutex
519static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
520static int gGLExtentionSlot = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521
522static void(*findProcAddress(const char* name,
523 const extention_map_t* map, size_t n))()
524{
525 for (uint32_t i=0 ; i<n ; i++) {
526 if (!strcmp(name, map[i].name)) {
527 return map[i].address;
528 }
529 }
530 return NULL;
531}
532
533// ----------------------------------------------------------------------------
534
Mathias Agopian6f087122010-09-23 16:38:38 -0700535static int gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700536 tls_t* tls = getTLS();
537 if (tls->logCallWithNoContext == EGL_TRUE) {
538 tls->logCallWithNoContext = EGL_FALSE;
539 LOGE("call to OpenGL ES API with no current context "
540 "(logged once per thread)");
541 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700542 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700543}
544
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545static void early_egl_init(void)
546{
547#if !USE_FAST_TLS_KEY
548 pthread_key_create(&gGLWrapperKey, NULL);
549#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700550#if EGL_TRACE
551 pthread_key_create(&gGLTraceKey, NULL);
552 initEglTraceLevel();
553#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554 uint32_t addr = (uint32_t)((void*)gl_no_context);
555 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700556 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700558 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700559
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700560 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561}
562
563static pthread_once_t once_control = PTHREAD_ONCE_INIT;
564static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
565
566
567static inline
568egl_display_t* get_display(EGLDisplay dpy)
569{
570 uintptr_t index = uintptr_t(dpy)-1U;
571 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
572}
573
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700574template<typename NATIVE, typename EGL>
575static inline NATIVE* egl_to_native_cast(EGL arg) {
576 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577}
578
579static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700580egl_surface_t* get_surface(EGLSurface surface) {
581 return egl_to_native_cast<egl_surface_t>(surface);
582}
583
584static inline
585egl_context_t* get_context(EGLContext context) {
586 return egl_to_native_cast<egl_context_t>(context);
587}
588
589static inline
590egl_image_t* get_image(EGLImageKHR image) {
591 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592}
593
Mathias Agopianc291f5852010-08-27 16:48:56 -0700594static inline
595egl_sync_t* get_sync(EGLSyncKHR sync) {
596 return egl_to_native_cast<egl_sync_t>(sync);
597}
598
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800599static egl_connection_t* validate_display_config(
600 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700601 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800602{
603 dp = get_display(dpy);
604 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
605
Mathias Agopiancee79392010-07-26 21:14:59 -0700606 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800607 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
608 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700609 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610 if (cnx->dso == 0) {
611 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
612 }
613 return cnx;
614}
615
616static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
617{
618 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
619 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700620 if (!get_display(dpy)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700622 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
624 return EGL_TRUE;
625}
626
627static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
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_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 return setError(EGL_BAD_SURFACE, EGL_FALSE);
635 return EGL_TRUE;
636}
637
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700638EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
639{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700640 ImageRef _i(image);
641 if (!_i.get()) return EGL_NO_IMAGE_KHR;
642
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700643 EGLContext context = getContext();
644 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
645 return EGL_NO_IMAGE_KHR;
646
647 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700648 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700649 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700651 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700652 return i->images[c->impl];
653}
654
Mathias Agopian923c6612009-08-17 18:07:06 -0700655// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700656
Mathias Agopian923c6612009-08-17 18:07:06 -0700657// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700658// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700659// egl_init_drivers_locked()
660//
661static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
662
663EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664{
665 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700666 // initialized by static ctor. should be set here.
667 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668 }
669
Mathias Agopiande586972009-05-28 17:39:03 -0700670 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700671 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700672
Mathias Agopian923c6612009-08-17 18:07:06 -0700673 // dynamically load all our EGL implementations for all displays
674 // and retrieve the corresponding EGLDisplay
675 // if that fails, don't use this driver.
676 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
677 egl_connection_t* cnx;
678 egl_display_t* d = &gDisplay[0];
679
680 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700682 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
683 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
684 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700685 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700686 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700687 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700688 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700689 if (dpy == EGL_NO_DISPLAY) {
690 loader.close(cnx->dso);
691 cnx->dso = NULL;
692 }
693 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 }
695
696 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700697 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698 char value[PROPERTY_VALUE_MAX];
699 property_get("debug.egl.hw", value, "1");
700 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700701 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
702 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
703 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700704 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700705 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700706 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700707 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700708 if (dpy == EGL_NO_DISPLAY) {
709 loader.close(cnx->dso);
710 cnx->dso = NULL;
711 }
712 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 } else {
714 LOGD("3D hardware acceleration is disabled");
715 }
716 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700717
Mathias Agopian923c6612009-08-17 18:07:06 -0700718 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
719 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 }
721
Mathias Agopian923c6612009-08-17 18:07:06 -0700722 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723}
724
Mathias Agopian923c6612009-08-17 18:07:06 -0700725EGLBoolean egl_init_drivers()
726{
727 EGLBoolean res;
728 pthread_mutex_lock(&gInitDriverMutex);
729 res = egl_init_drivers_locked();
730 pthread_mutex_unlock(&gInitDriverMutex);
731 return res;
732}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700733
734// ----------------------------------------------------------------------------
735}; // namespace android
736// ----------------------------------------------------------------------------
737
738using namespace android;
739
740EGLDisplay eglGetDisplay(NativeDisplayType display)
741{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800742 clearError();
743
Mathias Agopian923c6612009-08-17 18:07:06 -0700744 uint32_t index = uint32_t(display);
745 if (index >= NUM_DISPLAYS) {
746 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
747 }
748
749 if (egl_init_drivers() == EGL_FALSE) {
750 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
751 }
752
753 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
754 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700755}
756
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757// ----------------------------------------------------------------------------
758// Initialization
759// ----------------------------------------------------------------------------
760
761EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
762{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800763 clearError();
764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800765 egl_display_t * const dp = get_display(dpy);
766 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
767
Mathias Agopian75bc2782010-02-05 16:17:01 -0800768 Mutex::Autolock _l(dp->lock);
769
770 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771 if (major != NULL) *major = VERSION_MAJOR;
772 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700773 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774 return EGL_TRUE;
775 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700776
777#if EGL_TRACE
778
779 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
780 // the information from that call may be stale.)
781 initEglTraceLevel();
782
783#endif
784
785 setGLHooksThreadSpecific(&gHooksNoContext);
786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787 // initialize each EGL and
788 // build our own extension string first, based on the extension we know
789 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700790 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791 egl_connection_t* const cnx = &gEGLImpl[i];
792 cnx->major = -1;
793 cnx->minor = -1;
794 if (!cnx->dso)
795 continue;
796
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700797#if defined(ADRENO130)
798#warning "Adreno-130 eglInitialize() workaround"
799 /*
800 * The ADRENO 130 driver returns a different EGLDisplay each time
801 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
802 * after eglTerminate() has been called, so that eglInitialize()
803 * cannot be called again. Therefore, we need to make sure to call
804 * eglGetDisplay() before calling eglInitialize();
805 */
806 if (i == IMPL_HARDWARE) {
807 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700808 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700809 }
810#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700812
813 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700814 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700816 // i, idpy, cnx->major, cnx->minor, cnx);
817
818 // display is now initialized
819 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820
821 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700822 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700823 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700824 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700825 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700826 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700827 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700828 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700829 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830
831 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700832 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700833 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800834 }
835 }
836
837 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700838 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 egl_connection_t* const cnx = &gEGLImpl[i];
840 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
841 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700842 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700843 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
844 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700845 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700846 dp->disp[i].dpy, dp->disp[i].config, n,
847 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849 dp->numTotalConfigs += n;
850 res = EGL_TRUE;
851 }
852 }
853 }
854 }
855 }
856
857 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700858 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
859 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
860 egl_connection_t* const cnx = &gEGLImpl[i];
861 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
862 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
863 dp->configs[k].impl = i;
864 dp->configs[k].config = dp->disp[i].config[j];
865 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
866 // store the implementation's CONFIG_ID
867 cnx->egl.eglGetConfigAttrib(
868 dp->disp[i].dpy,
869 dp->disp[i].config[j],
870 EGL_CONFIG_ID,
871 &dp->configs[k].implConfigId);
872 k++;
873 }
874 }
875 }
876
877 // sort our configurations so we can do binary-searches
878 qsort( dp->configs,
879 dp->numTotalConfigs,
880 sizeof(egl_config_t), cmp_configs);
881
Mathias Agopian75bc2782010-02-05 16:17:01 -0800882 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800883 if (major != NULL) *major = VERSION_MAJOR;
884 if (minor != NULL) *minor = VERSION_MINOR;
885 return EGL_TRUE;
886 }
887 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
888}
889
890EGLBoolean eglTerminate(EGLDisplay dpy)
891{
Mathias Agopian923c6612009-08-17 18:07:06 -0700892 // NOTE: don't unload the drivers b/c some APIs can be called
893 // after eglTerminate() has been called. eglTerminate() only
894 // terminates an EGLDisplay, not a EGL itself.
895
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800896 clearError();
897
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898 egl_display_t* const dp = get_display(dpy);
899 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800900
901 Mutex::Autolock _l(dp->lock);
902
903 if (dp->refs == 0) {
904 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
905 }
906
907 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700908 if (dp->refs > 1) {
909 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800910 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700911 }
Mathias Agopiande586972009-05-28 17:39:03 -0700912
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700914 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700916 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700917 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700918 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700919 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700920 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700921 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700922 free(dp->disp[i].config);
923
924 dp->disp[i].numConfigs = 0;
925 dp->disp[i].config = 0;
926 dp->disp[i].state = egl_display_t::TERMINATED;
927
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 res = EGL_TRUE;
929 }
930 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700931
932 // TODO: all egl_object_t should be marked for termination
933
Mathias Agopian75bc2782010-02-05 16:17:01 -0800934 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800935 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700936 delete [] dp->configs;
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -0800937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800938 return res;
939}
940
941// ----------------------------------------------------------------------------
942// configuration
943// ----------------------------------------------------------------------------
944
945EGLBoolean eglGetConfigs( EGLDisplay dpy,
946 EGLConfig *configs,
947 EGLint config_size, EGLint *num_config)
948{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800949 clearError();
950
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800951 egl_display_t const * const dp = get_display(dpy);
952 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
953
954 GLint numConfigs = dp->numTotalConfigs;
955 if (!configs) {
956 *num_config = numConfigs;
957 return EGL_TRUE;
958 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700959
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800960 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700961 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
962 *configs++ = EGLConfig(i);
963 config_size--;
964 n++;
965 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966
967 *num_config = n;
968 return EGL_TRUE;
969}
970
971EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
972 EGLConfig *configs, EGLint config_size,
973 EGLint *num_config)
974{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800975 clearError();
976
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977 egl_display_t const * const dp = get_display(dpy);
978 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
979
Jack Palevich749c63d2009-03-25 15:12:17 -0700980 if (num_config==0) {
981 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800982 }
983
984 EGLint n;
985 EGLBoolean res = EGL_FALSE;
986 *num_config = 0;
987
988
989 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -0700990 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991 // to figure out both its size and if it contains an EGL_CONFIG_ID
992 // key. If so, the full array is copied and patched.
993 // NOTE: we assume that there can be only one occurrence
994 // of EGL_CONFIG_ID.
995
996 EGLint patch_index = -1;
997 GLint attr;
998 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -0700999 if (attrib_list) {
1000 while ((attr=attrib_list[size]) != EGL_NONE) {
1001 if (attr == EGL_CONFIG_ID)
1002 patch_index = size;
1003 size += 2;
1004 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005 }
1006 if (patch_index >= 0) {
1007 size += 2; // we need copy the sentinel as well
1008 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
1009 if (new_list == 0)
1010 return setError(EGL_BAD_ALLOC, EGL_FALSE);
1011 memcpy(new_list, attrib_list, size*sizeof(EGLint));
1012
1013 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -07001014 bool found = false;
1015 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -07001017 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
1018 if (dp->configs[i].configId == configId) {
1019 ourConfig = EGLConfig(i);
1020 configId = dp->configs[i].implConfigId;
1021 found = true;
1022 break;
1023 }
1024 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025
Mathias Agopiancee79392010-07-26 21:14:59 -07001026 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
1027 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 // and switch to the new list
1029 attrib_list = const_cast<const EGLint *>(new_list);
1030
1031 // At this point, the only configuration that can match is
1032 // dp->configs[i][index], however, we don't know if it would be
1033 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -07001034 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 // through all the EGLimpl[].
1036 // We also know we can only get a single config back, and we know
1037 // which one.
1038
Mathias Agopian618fa102009-10-14 02:06:37 -07001039 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -07001040 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
1041 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 if (res && n>0) {
1043 // n has to be 0 or 1, by construction, and we already know
1044 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -07001045 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001046 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -07001047 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048 *num_config = 1;
1049 }
1050 }
1051
1052 free(const_cast<EGLint *>(attrib_list));
1053 return res;
1054 }
1055
Mathias Agopiancee79392010-07-26 21:14:59 -07001056
Mathias Agopian618fa102009-10-14 02:06:37 -07001057 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 egl_connection_t* const cnx = &gEGLImpl[i];
1059 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001060 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001061 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001062 if (configs) {
1063 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -07001064 // internal EGLConfig format.
1065 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -07001066 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001067 egl_config_t key(i, configs[j]);
1068 intptr_t index = binarySearch<egl_config_t>(
1069 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -07001070 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001071 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -07001072 } else {
1073 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1074 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 }
Jack Palevich749c63d2009-03-25 15:12:17 -07001076 configs += n;
1077 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001079 *num_config += n;
1080 res = EGL_TRUE;
1081 }
1082 }
1083 }
1084 return res;
1085}
1086
1087EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1088 EGLint attribute, EGLint *value)
1089{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001090 clearError();
1091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001093 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094 if (!cnx) return EGL_FALSE;
1095
1096 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001097 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098 return EGL_TRUE;
1099 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001100 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -07001101 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1102 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103}
1104
1105// ----------------------------------------------------------------------------
1106// surfaces
1107// ----------------------------------------------------------------------------
1108
1109EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1110 NativeWindowType window,
1111 const EGLint *attrib_list)
1112{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001113 clearError();
1114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001115 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001116 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 if (cnx) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001118 EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy;
1119 EGLConfig iConfig = dp->configs[intptr_t(config)].config;
1120 EGLint format;
1121
1122 // set the native window's buffers format to match this config
1123 if (cnx->egl.eglGetConfigAttrib(iDpy,
1124 iConfig, EGL_NATIVE_VISUAL_ID, &format)) {
1125 if (format != 0) {
1126 native_window_set_buffers_geometry(window, 0, 0, format);
1127 }
1128 }
1129
Mathias Agopian618fa102009-10-14 02:06:37 -07001130 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001131 iDpy, iConfig, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001133 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001134 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135 return s;
1136 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 }
1138 return EGL_NO_SURFACE;
1139}
1140
1141EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1142 NativePixmapType pixmap,
1143 const EGLint *attrib_list)
1144{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001145 clearError();
1146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001147 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001148 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001150 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001151 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1152 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001153 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001154 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001155 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001156 return s;
1157 }
1158 }
1159 return EGL_NO_SURFACE;
1160}
1161
1162EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1163 const EGLint *attrib_list)
1164{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001165 clearError();
1166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001168 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001170 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001171 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1172 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001174 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001175 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176 return s;
1177 }
1178 }
1179 return EGL_NO_SURFACE;
1180}
1181
1182EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1183{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001184 clearError();
1185
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001186 SurfaceRef _s(surface);
1187 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1188
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 if (!validate_display_surface(dpy, surface))
1190 return EGL_FALSE;
1191 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001192
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001193 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001194 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001195 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001196 if (result == EGL_TRUE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001197 if (s->win != NULL) {
1198 native_window_set_buffers_geometry(s->win.get(), 0, 0, 0);
1199 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001200 _s.terminate();
1201 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202 return result;
1203}
1204
1205EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1206 EGLint attribute, EGLint *value)
1207{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001208 clearError();
1209
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001210 SurfaceRef _s(surface);
1211 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 if (!validate_display_surface(dpy, surface))
1214 return EGL_FALSE;
1215 egl_display_t const * const dp = get_display(dpy);
1216 egl_surface_t const * const s = get_surface(surface);
1217
Mathias Agopiancee79392010-07-26 21:14:59 -07001218 EGLBoolean result(EGL_TRUE);
1219 if (attribute == EGL_CONFIG_ID) {
1220 // We need to remap EGL_CONFIG_IDs
1221 *value = dp->configs[intptr_t(s->config)].configId;
1222 } else {
1223 result = s->cnx->egl.eglQuerySurface(
1224 dp->disp[s->impl].dpy, s->surface, attribute, value);
1225 }
1226
1227 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228}
1229
1230// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001231// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001232// ----------------------------------------------------------------------------
1233
1234EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1235 EGLContext share_list, const EGLint *attrib_list)
1236{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001237 clearError();
1238
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001240 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001242 if (share_list != EGL_NO_CONTEXT) {
1243 egl_context_t* const c = get_context(share_list);
1244 share_list = c->context;
1245 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001246 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001247 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1248 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001249 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001251 // figure out if it's a GLESv1 or GLESv2
1252 int version = 0;
1253 if (attrib_list) {
1254 while (*attrib_list != EGL_NONE) {
1255 GLint attr = *attrib_list++;
1256 GLint value = *attrib_list++;
1257 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1258 if (value == 1) {
1259 version = GLESv1_INDEX;
1260 } else if (value == 2) {
1261 version = GLESv2_INDEX;
1262 }
1263 }
1264 };
1265 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001266 egl_context_t* c = new egl_context_t(dpy, context, config,
1267 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 return c;
1269 }
1270 }
1271 return EGL_NO_CONTEXT;
1272}
1273
1274EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1275{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001276 clearError();
1277
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001278 ContextRef _c(ctx);
1279 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 if (!validate_display_context(dpy, ctx))
1282 return EGL_FALSE;
1283 egl_display_t const * const dp = get_display(dpy);
1284 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001285 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001286 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001287 if (result == EGL_TRUE) {
1288 _c.terminate();
1289 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001290 return result;
1291}
1292
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001293static void loseCurrent(egl_context_t * cur_c)
1294{
1295 if (cur_c) {
1296 egl_surface_t * cur_r = get_surface(cur_c->read);
1297 egl_surface_t * cur_d = get_surface(cur_c->draw);
1298
1299 // by construction, these are either 0 or valid (possibly terminated)
1300 // it should be impossible for these to be invalid
1301 ContextRef _cur_c(cur_c);
1302 SurfaceRef _cur_r(cur_r);
1303 SurfaceRef _cur_d(cur_d);
1304
1305 cur_c->read = NULL;
1306 cur_c->draw = NULL;
1307
1308 _cur_c.release();
1309 _cur_r.release();
1310 _cur_d.release();
1311 }
1312}
1313
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1315 EGLSurface read, EGLContext ctx)
1316{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001317 clearError();
1318
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001319 // get a reference to the object passed in
1320 ContextRef _c(ctx);
1321 SurfaceRef _d(draw);
1322 SurfaceRef _r(read);
1323
1324 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 egl_display_t const * const dp = get_display(dpy);
1326 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001327 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1328 // EGL_NO_CONTEXT is valid
1329 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 }
1331
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001332 // these are the underlying implementation's object
1333 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001334 EGLSurface impl_draw = EGL_NO_SURFACE;
1335 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001336
1337 // these are our objects structs passed in
1338 egl_context_t * c = NULL;
1339 egl_surface_t const * d = NULL;
1340 egl_surface_t const * r = NULL;
1341
1342 // these are the current objects structs
1343 egl_context_t * cur_c = get_context(getContext());
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001344
1345 if (ctx != EGL_NO_CONTEXT) {
1346 c = get_context(ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001347 impl_ctx = c->context;
1348 } else {
1349 // no context given, use the implementation of the current context
1350 if (cur_c == NULL) {
1351 // no current context
1352 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001353 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1354 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001355 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001356 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001357 return EGL_TRUE;
1358 }
1359 }
1360
1361 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001363 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001364 // make sure the EGLContext and EGLSurface passed in are for
1365 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001366 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001368 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001370
1371 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001373 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001374 // make sure the EGLContext and EGLSurface passed in are for
1375 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001376 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001378 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001380
1381 EGLBoolean result;
1382
1383 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001384 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001385 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001386 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001387 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001388 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001389 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390
1391 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001392
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001393 loseCurrent(cur_c);
1394
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001395 if (ctx != EGL_NO_CONTEXT) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001396 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001397 setContext(ctx);
1398 _c.acquire();
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001399 _r.acquire();
1400 _d.acquire();
1401 c->read = read;
1402 c->draw = draw;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001403 } else {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001404 setGLHooksThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001405 setContext(EGL_NO_CONTEXT);
1406 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 }
1408 return result;
1409}
1410
1411
1412EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1413 EGLint attribute, EGLint *value)
1414{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001415 clearError();
1416
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001417 ContextRef _c(ctx);
1418 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1419
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 if (!validate_display_context(dpy, ctx))
1421 return EGL_FALSE;
1422
1423 egl_display_t const * const dp = get_display(dpy);
1424 egl_context_t * const c = get_context(ctx);
1425
Mathias Agopiancee79392010-07-26 21:14:59 -07001426 EGLBoolean result(EGL_TRUE);
1427 if (attribute == EGL_CONFIG_ID) {
1428 *value = dp->configs[intptr_t(c->config)].configId;
1429 } else {
1430 // We need to remap EGL_CONFIG_IDs
1431 result = c->cnx->egl.eglQueryContext(
1432 dp->disp[c->impl].dpy, c->context, attribute, value);
1433 }
1434
1435 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436}
1437
1438EGLContext eglGetCurrentContext(void)
1439{
Mathias Agopian923c6612009-08-17 18:07:06 -07001440 // could be called before eglInitialize(), but we wouldn't have a context
1441 // then, and this function would correctly return EGL_NO_CONTEXT.
1442
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001443 clearError();
1444
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 EGLContext ctx = getContext();
1446 return ctx;
1447}
1448
1449EGLSurface eglGetCurrentSurface(EGLint readdraw)
1450{
Mathias Agopian923c6612009-08-17 18:07:06 -07001451 // could be called before eglInitialize(), but we wouldn't have a context
1452 // then, and this function would correctly return EGL_NO_SURFACE.
1453
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001454 clearError();
1455
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 EGLContext ctx = getContext();
1457 if (ctx) {
1458 egl_context_t const * const c = get_context(ctx);
1459 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1460 switch (readdraw) {
1461 case EGL_READ: return c->read;
1462 case EGL_DRAW: return c->draw;
1463 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1464 }
1465 }
1466 return EGL_NO_SURFACE;
1467}
1468
1469EGLDisplay eglGetCurrentDisplay(void)
1470{
Mathias Agopian923c6612009-08-17 18:07:06 -07001471 // could be called before eglInitialize(), but we wouldn't have a context
1472 // then, and this function would correctly return EGL_NO_DISPLAY.
1473
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001474 clearError();
1475
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 EGLContext ctx = getContext();
1477 if (ctx) {
1478 egl_context_t const * const c = get_context(ctx);
1479 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1480 return c->dpy;
1481 }
1482 return EGL_NO_DISPLAY;
1483}
1484
1485EGLBoolean eglWaitGL(void)
1486{
Mathias Agopian923c6612009-08-17 18:07:06 -07001487 // could be called before eglInitialize(), but we wouldn't have a context
1488 // then, and this function would return GL_TRUE, which isn't wrong.
1489
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001490 clearError();
1491
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 EGLBoolean res = EGL_TRUE;
1493 EGLContext ctx = getContext();
1494 if (ctx) {
1495 egl_context_t const * const c = get_context(ctx);
1496 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1497 if (uint32_t(c->impl)>=2)
1498 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1499 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1500 if (!cnx->dso)
1501 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001502 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 }
1504 return res;
1505}
1506
1507EGLBoolean eglWaitNative(EGLint engine)
1508{
Mathias Agopian923c6612009-08-17 18:07:06 -07001509 // could be called before eglInitialize(), but we wouldn't have a context
1510 // then, and this function would return GL_TRUE, which isn't wrong.
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001511
1512 clearError();
1513
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 EGLBoolean res = EGL_TRUE;
1515 EGLContext ctx = getContext();
1516 if (ctx) {
1517 egl_context_t const * const c = get_context(ctx);
1518 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1519 if (uint32_t(c->impl)>=2)
1520 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1521 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1522 if (!cnx->dso)
1523 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001524 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 }
1526 return res;
1527}
1528
1529EGLint eglGetError(void)
1530{
1531 EGLint result = EGL_SUCCESS;
Mathias Agopian02dafb52010-09-21 15:43:59 -07001532 EGLint err;
Mathias Agopian618fa102009-10-14 02:06:37 -07001533 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian02dafb52010-09-21 15:43:59 -07001534 err = EGL_SUCCESS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 egl_connection_t* const cnx = &gEGLImpl[i];
1536 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001537 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1539 result = err;
1540 }
Mathias Agopian02dafb52010-09-21 15:43:59 -07001541 err = getError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001542 if (result == EGL_SUCCESS)
Mathias Agopian02dafb52010-09-21 15:43:59 -07001543 result = err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001544 return result;
1545}
1546
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001547// Note: Similar implementations of these functions also exist in
1548// gl2.cpp and gl.cpp, and are used by applications that call the
1549// exported entry points directly.
1550typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
1551typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
1552
1553static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL;
1554static PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL;
1555
1556static void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image)
1557{
1558 GLeglImageOES implImage =
1559 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1560 glEGLImageTargetTexture2DOES_impl(target, implImage);
1561}
1562
1563static void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image)
1564{
1565 GLeglImageOES implImage =
1566 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1567 glEGLImageTargetRenderbufferStorageOES_impl(target, implImage);
1568}
1569
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001570__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001572 // eglGetProcAddress() could be the very first function called
1573 // in which case we must make sure we've initialized ourselves, this
1574 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001575
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001576 clearError();
1577
Mathias Agopian923c6612009-08-17 18:07:06 -07001578 if (egl_init_drivers() == EGL_FALSE) {
1579 setError(EGL_BAD_PARAMETER, NULL);
1580 return NULL;
1581 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583 __eglMustCastToProperFunctionPointerType addr;
1584 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1585 if (addr) return addr;
1586
Mathias Agopian24035332010-08-02 17:34:32 -07001587 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1588 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589
Mathias Agopian24035332010-08-02 17:34:32 -07001590 /*
1591 * Since eglGetProcAddress() is not associated to anything, it needs
1592 * to return a function pointer that "works" regardless of what
1593 * the current context is.
1594 *
1595 * For this reason, we return a "forwarder", a small stub that takes
1596 * care of calling the function associated with the context
1597 * currently bound.
1598 *
1599 * We first look for extensions we've already resolved, if we're seeing
1600 * this extension for the first time, we go through all our
1601 * implementations and call eglGetProcAddress() and record the
1602 * result in the appropriate implementation hooks and return the
1603 * address of the forwarder corresponding to that hook set.
1604 *
1605 */
1606
1607 const String8 name(procname);
1608 addr = gGLExtentionMap.valueFor(name);
1609 const int slot = gGLExtentionSlot;
1610
1611 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1612 "no more slots for eglGetProcAddress(\"%s\")",
1613 procname);
1614
1615 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1616 bool found = false;
1617 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1618 egl_connection_t* const cnx = &gEGLImpl[i];
1619 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1620 found = true;
Mathias Agopian4a88b522010-08-13 12:19:04 -07001621 // Extensions are independent of the bound context
1622 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1623 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001624#if EGL_TRACE
David Li2f5a6552011-03-01 16:08:10 -08001625 gHooksDebug.ext.extensions[slot] = gHooksTrace.ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001626#endif
Mathias Agopian24035332010-08-02 17:34:32 -07001627 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628 }
1629 }
Mathias Agopian24035332010-08-02 17:34:32 -07001630 if (found) {
1631 addr = gExtensionForwarders[slot];
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001632
1633 if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) {
1634 glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr;
1635 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper;
1636 }
1637 if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) {
1638 glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr;
1639 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper;
1640 }
1641
Mathias Agopian24035332010-08-02 17:34:32 -07001642 gGLExtentionMap.add(name, addr);
1643 gGLExtentionSlot++;
1644 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646
Mathias Agopian24035332010-08-02 17:34:32 -07001647 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian24035332010-08-02 17:34:32 -07001648 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649}
1650
1651EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1652{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001653 clearError();
1654
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001655 SurfaceRef _s(draw);
1656 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1657
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 if (!validate_display_surface(dpy, draw))
1659 return EGL_FALSE;
1660 egl_display_t const * const dp = get_display(dpy);
1661 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001662 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663}
1664
1665EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1666 NativePixmapType target)
1667{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001668 clearError();
1669
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001670 SurfaceRef _s(surface);
1671 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1672
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 if (!validate_display_surface(dpy, surface))
1674 return EGL_FALSE;
1675 egl_display_t const * const dp = get_display(dpy);
1676 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001677 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001678 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679}
1680
1681const char* eglQueryString(EGLDisplay dpy, EGLint name)
1682{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001683 clearError();
1684
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685 egl_display_t const * const dp = get_display(dpy);
1686 switch (name) {
1687 case EGL_VENDOR:
1688 return gVendorString;
1689 case EGL_VERSION:
1690 return gVersionString;
1691 case EGL_EXTENSIONS:
1692 return gExtensionString;
1693 case EGL_CLIENT_APIS:
1694 return gClientApiString;
1695 }
1696 return setError(EGL_BAD_PARAMETER, (const char *)0);
1697}
1698
1699
1700// ----------------------------------------------------------------------------
1701// EGL 1.1
1702// ----------------------------------------------------------------------------
1703
1704EGLBoolean eglSurfaceAttrib(
1705 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1706{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001707 clearError();
1708
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001709 SurfaceRef _s(surface);
1710 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 if (!validate_display_surface(dpy, surface))
1713 return EGL_FALSE;
1714 egl_display_t const * const dp = get_display(dpy);
1715 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001716 if (s->cnx->egl.eglSurfaceAttrib) {
1717 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001718 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719 }
1720 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1721}
1722
1723EGLBoolean eglBindTexImage(
1724 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1725{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001726 clearError();
1727
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001728 SurfaceRef _s(surface);
1729 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1730
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001731 if (!validate_display_surface(dpy, surface))
1732 return EGL_FALSE;
1733 egl_display_t const * const dp = get_display(dpy);
1734 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001735 if (s->cnx->egl.eglBindTexImage) {
1736 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001737 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 }
1739 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1740}
1741
1742EGLBoolean eglReleaseTexImage(
1743 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1744{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001745 clearError();
1746
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001747 SurfaceRef _s(surface);
1748 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1749
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 if (!validate_display_surface(dpy, surface))
1751 return EGL_FALSE;
1752 egl_display_t const * const dp = get_display(dpy);
1753 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001754 if (s->cnx->egl.eglReleaseTexImage) {
1755 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001756 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757 }
1758 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1759}
1760
1761EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1762{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001763 clearError();
1764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765 egl_display_t * const dp = get_display(dpy);
1766 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1767
1768 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001769 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001770 egl_connection_t* const cnx = &gEGLImpl[i];
1771 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001772 if (cnx->egl.eglSwapInterval) {
1773 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001774 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001775 res = EGL_FALSE;
1776 }
1777 }
1778 }
1779 }
1780 return res;
1781}
1782
1783
1784// ----------------------------------------------------------------------------
1785// EGL 1.2
1786// ----------------------------------------------------------------------------
1787
1788EGLBoolean eglWaitClient(void)
1789{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001790 clearError();
1791
Mathias Agopian923c6612009-08-17 18:07:06 -07001792 // could be called before eglInitialize(), but we wouldn't have a context
1793 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794 EGLBoolean res = EGL_TRUE;
1795 EGLContext ctx = getContext();
1796 if (ctx) {
1797 egl_context_t const * const c = get_context(ctx);
1798 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1799 if (uint32_t(c->impl)>=2)
1800 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1801 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1802 if (!cnx->dso)
1803 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001804 if (cnx->egl.eglWaitClient) {
1805 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001806 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001807 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001808 }
1809 }
1810 return res;
1811}
1812
1813EGLBoolean eglBindAPI(EGLenum api)
1814{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001815 clearError();
1816
Mathias Agopian923c6612009-08-17 18:07:06 -07001817 if (egl_init_drivers() == EGL_FALSE) {
1818 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1819 }
1820
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821 // bind this API on all EGLs
1822 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001823 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001824 egl_connection_t* const cnx = &gEGLImpl[i];
1825 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001826 if (cnx->egl.eglBindAPI) {
1827 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828 res = EGL_FALSE;
1829 }
1830 }
1831 }
1832 }
1833 return res;
1834}
1835
1836EGLenum eglQueryAPI(void)
1837{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001838 clearError();
1839
Mathias Agopian923c6612009-08-17 18:07:06 -07001840 if (egl_init_drivers() == EGL_FALSE) {
1841 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1842 }
1843
Mathias Agopian618fa102009-10-14 02:06:37 -07001844 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001845 egl_connection_t* const cnx = &gEGLImpl[i];
1846 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001847 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848 // the first one we find is okay, because they all
1849 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001850 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 }
1852 }
1853 }
1854 // or, it can only be OpenGL ES
1855 return EGL_OPENGL_ES_API;
1856}
1857
1858EGLBoolean eglReleaseThread(void)
1859{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001860 clearError();
1861
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001862 // If there is context bound to the thread, release it
1863 loseCurrent(get_context(getContext()));
1864
Mathias Agopian618fa102009-10-14 02:06:37 -07001865 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866 egl_connection_t* const cnx = &gEGLImpl[i];
1867 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001868 if (cnx->egl.eglReleaseThread) {
1869 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870 }
1871 }
1872 }
1873 clearTLS();
1874 return EGL_TRUE;
1875}
1876
1877EGLSurface eglCreatePbufferFromClientBuffer(
1878 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1879 EGLConfig config, const EGLint *attrib_list)
1880{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001881 clearError();
1882
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001884 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001885 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001886 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1887 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001888 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1889 buftype, buffer,
1890 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891 }
1892 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1893}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001894
1895// ----------------------------------------------------------------------------
1896// EGL_EGLEXT_VERSION 3
1897// ----------------------------------------------------------------------------
1898
1899EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1900 const EGLint *attrib_list)
1901{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001902 clearError();
1903
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001904 SurfaceRef _s(surface);
1905 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1906
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001907 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001908 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001909
1910 egl_display_t const * const dp = get_display(dpy);
1911 egl_surface_t const * const s = get_surface(surface);
1912
Mathias Agopian618fa102009-10-14 02:06:37 -07001913 if (s->cnx->egl.eglLockSurfaceKHR) {
1914 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001915 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001916 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001917 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001918}
1919
1920EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1921{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001922 clearError();
1923
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001924 SurfaceRef _s(surface);
1925 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1926
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001927 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001928 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001929
1930 egl_display_t const * const dp = get_display(dpy);
1931 egl_surface_t const * const s = get_surface(surface);
1932
Mathias Agopian618fa102009-10-14 02:06:37 -07001933 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1934 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001935 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001936 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001937 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001938}
1939
1940EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1941 EGLClientBuffer buffer, const EGLint *attrib_list)
1942{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001943 clearError();
1944
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001945 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001946 ContextRef _c(ctx);
1947 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001948 if (!validate_display_context(dpy, ctx))
1949 return EGL_NO_IMAGE_KHR;
1950 egl_display_t const * const dp = get_display(dpy);
1951 egl_context_t * const c = get_context(ctx);
1952 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001953 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001954 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001955 if (image == EGL_NO_IMAGE_KHR)
1956 return image;
1957
1958 egl_image_t* result = new egl_image_t(dpy, ctx);
1959 result->images[c->impl] = image;
1960 return (EGLImageKHR)result;
1961 } else {
1962 // EGL_NO_CONTEXT is a valid parameter
1963 egl_display_t const * const dp = get_display(dpy);
1964 if (dp == 0) {
1965 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1966 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001967
1968 /* Since we don't have a way to know which implementation to call,
1969 * we're calling all of them. If at least one of the implementation
1970 * succeeded, this is a success.
1971 */
1972
1973 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001974
Mathias Agopian618fa102009-10-14 02:06:37 -07001975 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001976 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001977 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001978 egl_connection_t* const cnx = &gEGLImpl[i];
1979 implImages[i] = EGL_NO_IMAGE_KHR;
1980 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001981 if (cnx->egl.eglCreateImageKHR) {
1982 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001983 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001984 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1985 success = true;
1986 }
1987 }
1988 }
1989 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001990
1991 if (!success) {
1992 // failure, if there was an error when we entered this function,
1993 // the error flag must not be updated.
1994 // Otherwise, the error is whatever happened in the implementation
1995 // that faulted.
1996 if (currentError != EGL_SUCCESS) {
1997 setError(currentError, EGL_NO_IMAGE_KHR);
1998 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001999 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002000 } else {
2001 // In case of success, we need to clear all error flags
2002 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07002003 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002004 // a "full" success (all implementation succeeded).
2005 eglGetError();
2006 }
2007
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002008 egl_image_t* result = new egl_image_t(dpy, ctx);
2009 memcpy(result->images, implImages, sizeof(implImages));
2010 return (EGLImageKHR)result;
2011 }
2012}
2013
2014EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2015{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002016 clearError();
2017
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002018 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002019 if (dp == 0) {
2020 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2021 }
2022
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002023 ImageRef _i(img);
2024 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002025
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002026 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002027 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07002028 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002029 egl_connection_t* const cnx = &gEGLImpl[i];
2030 if (image->images[i] != EGL_NO_IMAGE_KHR) {
2031 if (cnx->dso) {
Mathias Agopian77fbf8d2010-09-09 11:12:54 -07002032 if (cnx->egl.eglDestroyImageKHR) {
Mathias Agopian618fa102009-10-14 02:06:37 -07002033 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002034 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002035 success = true;
2036 }
2037 }
2038 }
2039 }
2040 }
2041 if (!success)
2042 return EGL_FALSE;
2043
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002044 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002045
Mathias Agopian24e5f522009-08-12 21:18:15 -07002046 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002047}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002048
Mathias Agopianc291f5852010-08-27 16:48:56 -07002049// ----------------------------------------------------------------------------
2050// EGL_EGLEXT_VERSION 5
2051// ----------------------------------------------------------------------------
2052
2053
2054EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
2055{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002056 clearError();
2057
Mathias Agopianc291f5852010-08-27 16:48:56 -07002058 EGLContext ctx = eglGetCurrentContext();
2059 ContextRef _c(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002060 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
Mathias Agopianc291f5852010-08-27 16:48:56 -07002061 if (!validate_display_context(dpy, ctx))
Mathias Agopiana93b9572010-09-21 16:22:10 -07002062 return EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002063 egl_display_t const * const dp = get_display(dpy);
2064 egl_context_t * const c = get_context(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002065 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002066 if (c->cnx->egl.eglCreateSyncKHR) {
2067 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
2068 dp->disp[c->impl].dpy, type, attrib_list);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002069 if (sync == EGL_NO_SYNC_KHR)
Mathias Agopianc291f5852010-08-27 16:48:56 -07002070 return sync;
2071 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
2072 }
2073 return (EGLSyncKHR)result;
2074}
2075
2076EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
2077{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002078 clearError();
2079
Mathias Agopianc291f5852010-08-27 16:48:56 -07002080 egl_display_t const * const dp = get_display(dpy);
2081 if (dp == 0) {
2082 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2083 }
2084
2085 SyncRef _s(sync);
2086 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2087 egl_sync_t* syncObject = get_sync(sync);
2088
2089 EGLContext ctx = syncObject->context;
2090 ContextRef _c(ctx);
2091 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2092 if (!validate_display_context(dpy, ctx))
2093 return EGL_FALSE;
2094
2095 egl_context_t * const c = get_context(ctx);
2096
2097 if (c->cnx->egl.eglDestroySyncKHR) {
2098 return c->cnx->egl.eglDestroySyncKHR(
2099 dp->disp[c->impl].dpy, syncObject->sync);
2100 }
2101
2102 return EGL_FALSE;
2103}
2104
2105EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
2106{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002107 clearError();
2108
Mathias Agopianc291f5852010-08-27 16:48:56 -07002109 egl_display_t const * const dp = get_display(dpy);
2110 if (dp == 0) {
2111 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2112 }
2113
2114 SyncRef _s(sync);
2115 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2116 egl_sync_t* syncObject = get_sync(sync);
2117
2118 EGLContext ctx = syncObject->context;
2119 ContextRef _c(ctx);
2120 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2121 if (!validate_display_context(dpy, ctx))
2122 return EGL_FALSE;
2123
2124 egl_context_t * const c = get_context(ctx);
2125
2126 if (c->cnx->egl.eglClientWaitSyncKHR) {
2127 return c->cnx->egl.eglClientWaitSyncKHR(
2128 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
2129 }
2130
2131 return EGL_FALSE;
2132}
2133
2134EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
2135{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002136 clearError();
2137
Mathias Agopianc291f5852010-08-27 16:48:56 -07002138 egl_display_t const * const dp = get_display(dpy);
2139 if (dp == 0) {
2140 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2141 }
2142
2143 SyncRef _s(sync);
2144 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2145 egl_sync_t* syncObject = get_sync(sync);
2146
2147 EGLContext ctx = syncObject->context;
2148 ContextRef _c(ctx);
2149 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2150 if (!validate_display_context(dpy, ctx))
2151 return EGL_FALSE;
2152
2153 egl_context_t * const c = get_context(ctx);
2154
2155 if (c->cnx->egl.eglGetSyncAttribKHR) {
2156 return c->cnx->egl.eglGetSyncAttribKHR(
2157 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
2158 }
2159
2160 return EGL_FALSE;
2161}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002162
2163// ----------------------------------------------------------------------------
2164// ANDROID extensions
2165// ----------------------------------------------------------------------------
2166
2167EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2168 EGLint left, EGLint top, EGLint width, EGLint height)
2169{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002170 clearError();
2171
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002172 SurfaceRef _s(draw);
2173 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
2174
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002175 if (!validate_display_surface(dpy, draw))
2176 return EGL_FALSE;
2177 egl_display_t const * const dp = get_display(dpy);
2178 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07002179 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
2180 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002181 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002182 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07002183 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002184}