blob: 8e4b0ee5b132150e9b8bfd60c255868611743639 [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{
David Lib33d5cf2011-03-04 17:50:48 -08001653 EGLBoolean Debug_eglSwapBuffers(EGLDisplay dpy, EGLSurface draw);
1654 if (gEGLDebugLevel > 0)
1655 Debug_eglSwapBuffers(dpy, draw);
1656
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001657 clearError();
1658
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001659 SurfaceRef _s(draw);
1660 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1661
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 if (!validate_display_surface(dpy, draw))
1663 return EGL_FALSE;
1664 egl_display_t const * const dp = get_display(dpy);
1665 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001666 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667}
1668
1669EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1670 NativePixmapType target)
1671{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001672 clearError();
1673
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001674 SurfaceRef _s(surface);
1675 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1676
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 if (!validate_display_surface(dpy, surface))
1678 return EGL_FALSE;
1679 egl_display_t const * const dp = get_display(dpy);
1680 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001681 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001682 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683}
1684
1685const char* eglQueryString(EGLDisplay dpy, EGLint name)
1686{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001687 clearError();
1688
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 egl_display_t const * const dp = get_display(dpy);
1690 switch (name) {
1691 case EGL_VENDOR:
1692 return gVendorString;
1693 case EGL_VERSION:
1694 return gVersionString;
1695 case EGL_EXTENSIONS:
1696 return gExtensionString;
1697 case EGL_CLIENT_APIS:
1698 return gClientApiString;
1699 }
1700 return setError(EGL_BAD_PARAMETER, (const char *)0);
1701}
1702
1703
1704// ----------------------------------------------------------------------------
1705// EGL 1.1
1706// ----------------------------------------------------------------------------
1707
1708EGLBoolean eglSurfaceAttrib(
1709 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1710{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001711 clearError();
1712
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001713 SurfaceRef _s(surface);
1714 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1715
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001716 if (!validate_display_surface(dpy, surface))
1717 return EGL_FALSE;
1718 egl_display_t const * const dp = get_display(dpy);
1719 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001720 if (s->cnx->egl.eglSurfaceAttrib) {
1721 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001722 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723 }
1724 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1725}
1726
1727EGLBoolean eglBindTexImage(
1728 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1729{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001730 clearError();
1731
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001732 SurfaceRef _s(surface);
1733 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1734
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 if (!validate_display_surface(dpy, surface))
1736 return EGL_FALSE;
1737 egl_display_t const * const dp = get_display(dpy);
1738 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001739 if (s->cnx->egl.eglBindTexImage) {
1740 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001741 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742 }
1743 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1744}
1745
1746EGLBoolean eglReleaseTexImage(
1747 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1748{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001749 clearError();
1750
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001751 SurfaceRef _s(surface);
1752 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1753
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001754 if (!validate_display_surface(dpy, surface))
1755 return EGL_FALSE;
1756 egl_display_t const * const dp = get_display(dpy);
1757 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001758 if (s->cnx->egl.eglReleaseTexImage) {
1759 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001760 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001761 }
1762 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1763}
1764
1765EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1766{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001767 clearError();
1768
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769 egl_display_t * const dp = get_display(dpy);
1770 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1771
1772 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001773 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774 egl_connection_t* const cnx = &gEGLImpl[i];
1775 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001776 if (cnx->egl.eglSwapInterval) {
1777 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001778 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779 res = EGL_FALSE;
1780 }
1781 }
1782 }
1783 }
1784 return res;
1785}
1786
1787
1788// ----------------------------------------------------------------------------
1789// EGL 1.2
1790// ----------------------------------------------------------------------------
1791
1792EGLBoolean eglWaitClient(void)
1793{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001794 clearError();
1795
Mathias Agopian923c6612009-08-17 18:07:06 -07001796 // could be called before eglInitialize(), but we wouldn't have a context
1797 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001798 EGLBoolean res = EGL_TRUE;
1799 EGLContext ctx = getContext();
1800 if (ctx) {
1801 egl_context_t const * const c = get_context(ctx);
1802 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1803 if (uint32_t(c->impl)>=2)
1804 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1805 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1806 if (!cnx->dso)
1807 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001808 if (cnx->egl.eglWaitClient) {
1809 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001810 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001811 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001812 }
1813 }
1814 return res;
1815}
1816
1817EGLBoolean eglBindAPI(EGLenum api)
1818{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001819 clearError();
1820
Mathias Agopian923c6612009-08-17 18:07:06 -07001821 if (egl_init_drivers() == EGL_FALSE) {
1822 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1823 }
1824
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 // bind this API on all EGLs
1826 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001827 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828 egl_connection_t* const cnx = &gEGLImpl[i];
1829 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001830 if (cnx->egl.eglBindAPI) {
1831 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832 res = EGL_FALSE;
1833 }
1834 }
1835 }
1836 }
1837 return res;
1838}
1839
1840EGLenum eglQueryAPI(void)
1841{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001842 clearError();
1843
Mathias Agopian923c6612009-08-17 18:07:06 -07001844 if (egl_init_drivers() == EGL_FALSE) {
1845 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1846 }
1847
Mathias Agopian618fa102009-10-14 02:06:37 -07001848 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849 egl_connection_t* const cnx = &gEGLImpl[i];
1850 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001851 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001852 // the first one we find is okay, because they all
1853 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001854 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855 }
1856 }
1857 }
1858 // or, it can only be OpenGL ES
1859 return EGL_OPENGL_ES_API;
1860}
1861
1862EGLBoolean eglReleaseThread(void)
1863{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001864 clearError();
1865
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001866 // If there is context bound to the thread, release it
1867 loseCurrent(get_context(getContext()));
1868
Mathias Agopian618fa102009-10-14 02:06:37 -07001869 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870 egl_connection_t* const cnx = &gEGLImpl[i];
1871 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001872 if (cnx->egl.eglReleaseThread) {
1873 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874 }
1875 }
1876 }
1877 clearTLS();
1878 return EGL_TRUE;
1879}
1880
1881EGLSurface eglCreatePbufferFromClientBuffer(
1882 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1883 EGLConfig config, const EGLint *attrib_list)
1884{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001885 clearError();
1886
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001888 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001890 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1891 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001892 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1893 buftype, buffer,
1894 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895 }
1896 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1897}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001898
1899// ----------------------------------------------------------------------------
1900// EGL_EGLEXT_VERSION 3
1901// ----------------------------------------------------------------------------
1902
1903EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1904 const EGLint *attrib_list)
1905{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001906 clearError();
1907
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001908 SurfaceRef _s(surface);
1909 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1910
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001911 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001912 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001913
1914 egl_display_t const * const dp = get_display(dpy);
1915 egl_surface_t const * const s = get_surface(surface);
1916
Mathias Agopian618fa102009-10-14 02:06:37 -07001917 if (s->cnx->egl.eglLockSurfaceKHR) {
1918 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001919 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001920 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001921 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001922}
1923
1924EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1925{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001926 clearError();
1927
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001928 SurfaceRef _s(surface);
1929 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1930
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001931 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001932 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001933
1934 egl_display_t const * const dp = get_display(dpy);
1935 egl_surface_t const * const s = get_surface(surface);
1936
Mathias Agopian618fa102009-10-14 02:06:37 -07001937 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1938 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001939 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001940 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001941 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001942}
1943
1944EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1945 EGLClientBuffer buffer, const EGLint *attrib_list)
1946{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001947 clearError();
1948
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001949 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001950 ContextRef _c(ctx);
1951 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001952 if (!validate_display_context(dpy, ctx))
1953 return EGL_NO_IMAGE_KHR;
1954 egl_display_t const * const dp = get_display(dpy);
1955 egl_context_t * const c = get_context(ctx);
1956 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07001957 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001958 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001959 if (image == EGL_NO_IMAGE_KHR)
1960 return image;
1961
1962 egl_image_t* result = new egl_image_t(dpy, ctx);
1963 result->images[c->impl] = image;
1964 return (EGLImageKHR)result;
1965 } else {
1966 // EGL_NO_CONTEXT is a valid parameter
1967 egl_display_t const * const dp = get_display(dpy);
1968 if (dp == 0) {
1969 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1970 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001971
1972 /* Since we don't have a way to know which implementation to call,
1973 * we're calling all of them. If at least one of the implementation
1974 * succeeded, this is a success.
1975 */
1976
1977 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001978
Mathias Agopian618fa102009-10-14 02:06:37 -07001979 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001980 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07001981 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001982 egl_connection_t* const cnx = &gEGLImpl[i];
1983 implImages[i] = EGL_NO_IMAGE_KHR;
1984 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001985 if (cnx->egl.eglCreateImageKHR) {
1986 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001987 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001988 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1989 success = true;
1990 }
1991 }
1992 }
1993 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07001994
1995 if (!success) {
1996 // failure, if there was an error when we entered this function,
1997 // the error flag must not be updated.
1998 // Otherwise, the error is whatever happened in the implementation
1999 // that faulted.
2000 if (currentError != EGL_SUCCESS) {
2001 setError(currentError, EGL_NO_IMAGE_KHR);
2002 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002003 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002004 } else {
2005 // In case of success, we need to clear all error flags
2006 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07002007 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002008 // a "full" success (all implementation succeeded).
2009 eglGetError();
2010 }
2011
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002012 egl_image_t* result = new egl_image_t(dpy, ctx);
2013 memcpy(result->images, implImages, sizeof(implImages));
2014 return (EGLImageKHR)result;
2015 }
2016}
2017
2018EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2019{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002020 clearError();
2021
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002022 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002023 if (dp == 0) {
2024 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2025 }
2026
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002027 ImageRef _i(img);
2028 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002029
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002030 egl_image_t* image = get_image(img);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002031 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07002032 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002033 egl_connection_t* const cnx = &gEGLImpl[i];
2034 if (image->images[i] != EGL_NO_IMAGE_KHR) {
2035 if (cnx->dso) {
Mathias Agopian77fbf8d2010-09-09 11:12:54 -07002036 if (cnx->egl.eglDestroyImageKHR) {
Mathias Agopian618fa102009-10-14 02:06:37 -07002037 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002038 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002039 success = true;
2040 }
2041 }
2042 }
2043 }
2044 }
2045 if (!success)
2046 return EGL_FALSE;
2047
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002048 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002049
Mathias Agopian24e5f522009-08-12 21:18:15 -07002050 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002051}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002052
Mathias Agopianc291f5852010-08-27 16:48:56 -07002053// ----------------------------------------------------------------------------
2054// EGL_EGLEXT_VERSION 5
2055// ----------------------------------------------------------------------------
2056
2057
2058EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
2059{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002060 clearError();
2061
Mathias Agopianc291f5852010-08-27 16:48:56 -07002062 EGLContext ctx = eglGetCurrentContext();
2063 ContextRef _c(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002064 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
Mathias Agopianc291f5852010-08-27 16:48:56 -07002065 if (!validate_display_context(dpy, ctx))
Mathias Agopiana93b9572010-09-21 16:22:10 -07002066 return EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002067 egl_display_t const * const dp = get_display(dpy);
2068 egl_context_t * const c = get_context(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002069 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002070 if (c->cnx->egl.eglCreateSyncKHR) {
2071 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
2072 dp->disp[c->impl].dpy, type, attrib_list);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002073 if (sync == EGL_NO_SYNC_KHR)
Mathias Agopianc291f5852010-08-27 16:48:56 -07002074 return sync;
2075 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
2076 }
2077 return (EGLSyncKHR)result;
2078}
2079
2080EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
2081{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002082 clearError();
2083
Mathias Agopianc291f5852010-08-27 16:48:56 -07002084 egl_display_t const * const dp = get_display(dpy);
2085 if (dp == 0) {
2086 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2087 }
2088
2089 SyncRef _s(sync);
2090 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2091 egl_sync_t* syncObject = get_sync(sync);
2092
2093 EGLContext ctx = syncObject->context;
2094 ContextRef _c(ctx);
2095 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2096 if (!validate_display_context(dpy, ctx))
2097 return EGL_FALSE;
2098
2099 egl_context_t * const c = get_context(ctx);
2100
2101 if (c->cnx->egl.eglDestroySyncKHR) {
2102 return c->cnx->egl.eglDestroySyncKHR(
2103 dp->disp[c->impl].dpy, syncObject->sync);
2104 }
2105
2106 return EGL_FALSE;
2107}
2108
2109EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
2110{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002111 clearError();
2112
Mathias Agopianc291f5852010-08-27 16:48:56 -07002113 egl_display_t const * const dp = get_display(dpy);
2114 if (dp == 0) {
2115 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2116 }
2117
2118 SyncRef _s(sync);
2119 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2120 egl_sync_t* syncObject = get_sync(sync);
2121
2122 EGLContext ctx = syncObject->context;
2123 ContextRef _c(ctx);
2124 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2125 if (!validate_display_context(dpy, ctx))
2126 return EGL_FALSE;
2127
2128 egl_context_t * const c = get_context(ctx);
2129
2130 if (c->cnx->egl.eglClientWaitSyncKHR) {
2131 return c->cnx->egl.eglClientWaitSyncKHR(
2132 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
2133 }
2134
2135 return EGL_FALSE;
2136}
2137
2138EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
2139{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002140 clearError();
2141
Mathias Agopianc291f5852010-08-27 16:48:56 -07002142 egl_display_t const * const dp = get_display(dpy);
2143 if (dp == 0) {
2144 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2145 }
2146
2147 SyncRef _s(sync);
2148 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2149 egl_sync_t* syncObject = get_sync(sync);
2150
2151 EGLContext ctx = syncObject->context;
2152 ContextRef _c(ctx);
2153 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2154 if (!validate_display_context(dpy, ctx))
2155 return EGL_FALSE;
2156
2157 egl_context_t * const c = get_context(ctx);
2158
2159 if (c->cnx->egl.eglGetSyncAttribKHR) {
2160 return c->cnx->egl.eglGetSyncAttribKHR(
2161 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
2162 }
2163
2164 return EGL_FALSE;
2165}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002166
2167// ----------------------------------------------------------------------------
2168// ANDROID extensions
2169// ----------------------------------------------------------------------------
2170
2171EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2172 EGLint left, EGLint top, EGLint width, EGLint height)
2173{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002174 clearError();
2175
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002176 SurfaceRef _s(draw);
2177 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
2178
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002179 if (!validate_display_surface(dpy, draw))
2180 return EGL_FALSE;
2181 egl_display_t const * const dp = get_display(dpy);
2182 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07002183 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
2184 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002185 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002186 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07002187 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002188}