blob: e0a7662048bc2da1e1b600ed887b15cc54646f90 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 ** Copyright 2007, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <ctype.h>
Mathias Agopiand8fb7b52009-05-17 18:50:16 -070018#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <string.h>
20#include <errno.h>
21#include <dlfcn.h>
22
23#include <sys/ioctl.h>
24
Kenny Rootaf1cf072011-02-16 10:13:53 -080025#ifdef HAVE_ANDROID_OS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <linux/android_pmem.h>
27#endif
28
29#include <EGL/egl.h>
30#include <EGL/eglext.h>
31#include <GLES/gl.h>
32#include <GLES/glext.h>
33
34#include <cutils/log.h>
35#include <cutils/atomic.h>
36#include <cutils/properties.h>
37#include <cutils/memory.h>
38
Mathias Agopian9429e9c2009-08-21 02:18:25 -070039#include <utils/SortedVector.h>
Mathias Agopian24035332010-08-02 17:34:32 -070040#include <utils/KeyedVector.h>
41#include <utils/String8.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070042
Mathias Agopian644bb2a2010-11-24 15:59:35 -080043#include <ui/egl/android_natives.h>
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include "hooks.h"
46#include "egl_impl.h"
Mathias Agopiande586972009-05-28 17:39:03 -070047#include "Loader.h"
David Li65948aa2011-03-10 16:40:37 -080048#include "glesv2dbg.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
51
52// ----------------------------------------------------------------------------
53namespace android {
54// ----------------------------------------------------------------------------
55
56#define VERSION_MAJOR 1
57#define VERSION_MINOR 4
58static char const * const gVendorString = "Android";
Mathias Agopian923c6612009-08-17 18:07:06 -070059static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian076b1cc2009-04-10 14:24:30 -070061static char const * const gExtensionString =
62 "EGL_KHR_image "
Mathias Agopiane6bf8b32009-05-06 23:47:08 -070063 "EGL_KHR_image_base "
64 "EGL_KHR_image_pixmap "
Mathias Agopian8e4b5a32010-08-27 16:08:03 -070065 "EGL_KHR_gl_texture_2D_image "
Michael I. Goldca41e362011-01-13 10:13:15 -080066 "EGL_KHR_gl_texture_cubemap_image "
67 "EGL_KHR_gl_renderbuffer_image "
Mathias Agopianc291f5852010-08-27 16:48:56 -070068 "EGL_KHR_fence_sync "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069 "EGL_ANDROID_image_native_buffer "
Mathias Agopiandf3ca302009-05-04 19:29:25 -070070 "EGL_ANDROID_swap_rectangle "
Mathias Agopian076b1cc2009-04-10 14:24:30 -070071 ;
72
73// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Mathias Agopian9429e9c2009-08-21 02:18:25 -070075class egl_object_t {
76 static SortedVector<egl_object_t*> sObjects;
77 static Mutex sLock;
78
79 volatile int32_t terminated;
80 mutable volatile int32_t count;
81
82public:
83 egl_object_t() : terminated(0), count(1) {
84 Mutex::Autolock _l(sLock);
85 sObjects.add(this);
86 }
87
88 inline bool isAlive() const { return !terminated; }
89
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090private:
Mathias Agopian9429e9c2009-08-21 02:18:25 -070091 bool get() {
92 Mutex::Autolock _l(sLock);
93 if (egl_object_t::sObjects.indexOf(this) >= 0) {
94 android_atomic_inc(&count);
95 return true;
96 }
97 return false;
98 }
99
100 bool put() {
101 Mutex::Autolock _l(sLock);
102 if (android_atomic_dec(&count) == 1) {
103 sObjects.remove(this);
104 return true;
105 }
106 return false;
107 }
108
109public:
110 template <typename N, typename T>
111 struct LocalRef {
112 N* ref;
113 LocalRef(T o) : ref(0) {
114 N* native = reinterpret_cast<N*>(o);
115 if (o && native->get()) {
116 ref = native;
117 }
118 }
119 ~LocalRef() {
120 if (ref && ref->put()) {
121 delete ref;
122 }
123 }
124 inline N* get() {
125 return ref;
126 }
127 void acquire() const {
128 if (ref) {
129 android_atomic_inc(&ref->count);
130 }
131 }
132 void release() const {
133 if (ref) {
134 int32_t c = android_atomic_dec(&ref->count);
135 // ref->count cannot be 1 prior atomic_dec because we have
136 // a reference, and if we have one, it means there was
137 // already one before us.
138 LOGE_IF(c==1, "refcount is now 0 in release()");
139 }
140 }
141 void terminate() {
142 if (ref) {
143 ref->terminated = 1;
144 release();
145 }
146 }
147 };
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148};
149
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700150SortedVector<egl_object_t*> egl_object_t::sObjects;
151Mutex egl_object_t::sLock;
152
Mathias Agopiancee79392010-07-26 21:14:59 -0700153
154struct egl_config_t {
155 egl_config_t() {}
156 egl_config_t(int impl, EGLConfig config)
157 : impl(impl), config(config), configId(0), implConfigId(0) { }
158 int impl; // the implementation this config is for
159 EGLConfig config; // the implementation's EGLConfig
160 EGLint configId; // our CONFIG_ID
161 EGLint implConfigId; // the implementation's CONFIG_ID
162 inline bool operator < (const egl_config_t& rhs) const {
163 if (impl < rhs.impl) return true;
164 if (impl > rhs.impl) return false;
165 return config < rhs.config;
166 }
167};
168
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700169struct egl_display_t {
170 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
171
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 struct strings_t {
173 char const * vendor;
174 char const * version;
175 char const * clientApi;
176 char const * extensions;
177 };
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700178
179 struct DisplayImpl {
180 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
181 state(NOT_INITIALIZED), numConfigs(0) { }
182 EGLDisplay dpy;
183 EGLConfig* config;
184 EGLint state;
185 EGLint numConfigs;
186 strings_t queryString;
187 };
188
Mathias Agopiancee79392010-07-26 21:14:59 -0700189 uint32_t magic;
190 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
191 EGLint numTotalConfigs;
192 egl_config_t* configs;
193 uint32_t refs;
194 Mutex lock;
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700195
Mathias Agopiancee79392010-07-26 21:14:59 -0700196 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700197 ~egl_display_t() { magic = 0; }
Eric Hassold3ede7c12011-03-23 15:59:00 -0700198 inline bool isReady() const { return (refs > 0); }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700199 inline bool isValid() const { return magic == '_dpy'; }
200 inline bool isAlive() const { return isValid(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201};
202
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700203struct egl_surface_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700205 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
206
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800207 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win,
208 EGLSurface surface, int impl, egl_connection_t const* cnx)
209 : dpy(dpy), surface(surface), config(config), win(win), impl(impl), cnx(cnx) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 }
211 ~egl_surface_t() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 }
213 EGLDisplay dpy;
214 EGLSurface surface;
Mathias Agopiancee79392010-07-26 21:14:59 -0700215 EGLConfig config;
Mathias Agopian644bb2a2010-11-24 15:59:35 -0800216 sp<ANativeWindow> win;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 int impl;
218 egl_connection_t const* cnx;
219};
220
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700221struct egl_context_t : public egl_object_t
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700223 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
224
Mathias Agopiancee79392010-07-26 21:14:59 -0700225 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian618fa102009-10-14 02:06:37 -0700226 int impl, egl_connection_t const* cnx, int version)
Mathias Agopianc3ce8802010-12-08 15:34:02 -0800227 : dpy(dpy), context(context), config(config), read(0), draw(0), impl(impl),
David Li65948aa2011-03-10 16:40:37 -0800228 cnx(cnx), version(version), dbg(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 {
230 }
David Li65948aa2011-03-10 16:40:37 -0800231 ~egl_context_t()
232 {
233 if (dbg)
234 DestroyDbgContext(dbg);
235 dbg = NULL;
236 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 EGLDisplay dpy;
238 EGLContext context;
Mathias Agopiancee79392010-07-26 21:14:59 -0700239 EGLConfig config;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 EGLSurface read;
241 EGLSurface draw;
242 int impl;
243 egl_connection_t const* cnx;
Mathias Agopian618fa102009-10-14 02:06:37 -0700244 int version;
David Li65948aa2011-03-10 16:40:37 -0800245 DbgContext * dbg;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246};
247
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700248struct egl_image_t : public egl_object_t
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700249{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700250 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
251
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700252 egl_image_t(EGLDisplay dpy, EGLContext context)
253 : dpy(dpy), context(context)
254 {
255 memset(images, 0, sizeof(images));
256 }
257 EGLDisplay dpy;
Mathias Agopian77fbf8d2010-09-09 11:12:54 -0700258 EGLContext context;
Mathias Agopian618fa102009-10-14 02:06:37 -0700259 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700260};
261
Mathias Agopianc291f5852010-08-27 16:48:56 -0700262struct egl_sync_t : public egl_object_t
263{
264 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
265
266 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync)
267 : dpy(dpy), context(context), sync(sync)
268 {
269 }
270 EGLDisplay dpy;
271 EGLContext context;
272 EGLSyncKHR sync;
273};
274
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700275typedef egl_surface_t::Ref SurfaceRef;
276typedef egl_context_t::Ref ContextRef;
277typedef egl_image_t::Ref ImageRef;
Mathias Agopianc291f5852010-08-27 16:48:56 -0700278typedef egl_sync_t::Ref SyncRef;
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280struct tls_t
281{
Mathias Agopiand274eae2009-07-31 16:21:17 -0700282 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 EGLint error;
284 EGLContext ctx;
Mathias Agopiand274eae2009-07-31 16:21:17 -0700285 EGLBoolean logCallWithNoContext;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286};
287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
289// ----------------------------------------------------------------------------
290
Mathias Agopianbf41b112010-04-09 13:37:34 -0700291static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292static egl_display_t gDisplay[NUM_DISPLAYS];
293static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
294static pthread_key_t gEGLThreadLocalStorageKey = -1;
295
296// ----------------------------------------------------------------------------
297
Mathias Agopian618fa102009-10-14 02:06:37 -0700298EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
299EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopianeccc8cf2009-05-13 00:19:22 -0700300EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700302#if EGL_TRACE
303
304EGLAPI pthread_key_t gGLTraceKey = -1;
305
306// ----------------------------------------------------------------------------
307
David Li2f5a6552011-03-01 16:08:10 -0800308static int gEGLTraceLevel, gEGLDebugLevel;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700309static int gEGLApplicationTraceLevel;
David Li2f5a6552011-03-01 16:08:10 -0800310extern EGLAPI gl_hooks_t gHooksTrace, gHooksDebug;
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700311
312static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
313 pthread_setspecific(gGLTraceKey, value);
314}
315
316gl_hooks_t const* getGLTraceThreadSpecific() {
317 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
318}
319
320static void initEglTraceLevel() {
321 char value[PROPERTY_VALUE_MAX];
322 property_get("debug.egl.trace", value, "0");
323 int propertyLevel = atoi(value);
324 int applicationLevel = gEGLApplicationTraceLevel;
325 gEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
David Li2f5a6552011-03-01 16:08:10 -0800326
327 property_get("debug.egl.debug_proc", value, "");
328 long pid = getpid();
329 char procPath[128] = {};
330 sprintf(procPath, "/proc/%ld/cmdline", pid);
331 FILE * file = fopen(procPath, "r");
332 if (file)
333 {
334 char cmdline[256] = {};
335 if (fgets(cmdline, sizeof(cmdline) - 1, file))
336 {
David Li2f5a6552011-03-01 16:08:10 -0800337 if (!strcmp(value, cmdline))
338 gEGLDebugLevel = 1;
339 }
340 fclose(file);
341 }
342
David Li2f5a6552011-03-01 16:08:10 -0800343 if (gEGLDebugLevel > 0)
David Li85f33a72011-03-10 19:07:42 -0800344 {
345 property_get("debug.egl.debug_port", value, "5039");
346 StartDebugServer(atoi(value));
347 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700348}
349
350static void setGLHooksThreadSpecific(gl_hooks_t const *value) {
351 if (gEGLTraceLevel > 0) {
352 setGlTraceThreadSpecific(value);
353 setGlThreadSpecific(&gHooksTrace);
David Li65948aa2011-03-10 16:40:37 -0800354 } else if (gEGLDebugLevel > 0 && value != &gHooksNoContext) {
David Li2f5a6552011-03-01 16:08:10 -0800355 setGlTraceThreadSpecific(value);
356 setGlThreadSpecific(&gHooksDebug);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700357 } else {
358 setGlThreadSpecific(value);
359 }
360}
361
362/*
363 * Global entry point to allow applications to modify their own trace level.
364 * The effective trace level is the max of this level and the value of debug.egl.trace.
365 */
366extern "C"
367void setGLTraceLevel(int level) {
368 gEGLApplicationTraceLevel = level;
369}
370
371#else
372
373static inline void setGLHooksThreadSpecific(gl_hooks_t const *value) {
374 setGlThreadSpecific(value);
375}
376
377#endif
378
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379// ----------------------------------------------------------------------------
380
381static __attribute__((noinline))
382const char *egl_strerror(EGLint err)
383{
384 switch (err){
385 case EGL_SUCCESS: return "EGL_SUCCESS";
386 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
387 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
388 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
389 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
390 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
391 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
392 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
393 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
394 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
395 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
396 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
397 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
398 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
399 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
400 default: return "UNKNOWN";
401 }
402}
403
404static __attribute__((noinline))
405void clearTLS() {
406 if (gEGLThreadLocalStorageKey != -1) {
407 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
408 if (tls) {
409 delete tls;
410 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
411 }
412 }
413}
414
415static tls_t* getTLS()
416{
417 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
418 if (tls == 0) {
419 tls = new tls_t;
420 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
421 }
422 return tls;
423}
424
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800425static inline void clearError() {
Jamie Gennisf1cde8e2011-01-30 16:50:04 -0800426 // This must clear the error from all the underlying EGL implementations as
427 // well as the EGL wrapper layer.
428 eglGetError();
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800429}
430
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800431template<typename T>
432static __attribute__((noinline))
433T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
434 if (gEGLThreadLocalStorageKey == -1) {
435 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
436 if (gEGLThreadLocalStorageKey == -1)
437 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
438 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
439 }
440 tls_t* tls = getTLS();
441 if (tls->error != error) {
442 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
443 tls->error = error;
444 }
445 return returnValue;
446}
447
448static __attribute__((noinline))
449GLint getError() {
450 if (gEGLThreadLocalStorageKey == -1)
451 return EGL_SUCCESS;
452 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
453 if (!tls) return EGL_SUCCESS;
454 GLint error = tls->error;
455 tls->error = EGL_SUCCESS;
456 return error;
457}
458
459static __attribute__((noinline))
460void setContext(EGLContext ctx) {
461 if (gEGLThreadLocalStorageKey == -1) {
462 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
463 if (gEGLThreadLocalStorageKey == -1)
464 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
465 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
466 }
467 tls_t* tls = getTLS();
468 tls->ctx = ctx;
469}
470
471static __attribute__((noinline))
472EGLContext getContext() {
473 if (gEGLThreadLocalStorageKey == -1)
474 return EGL_NO_CONTEXT;
475 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
476 if (!tls) return EGL_NO_CONTEXT;
477 return tls->ctx;
478}
479
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480/*****************************************************************************/
481
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800482template<typename T>
483static __attribute__((noinline))
484int binarySearch(
485 T const sortedArray[], int first, int last, T key)
486{
487 while (first <= last) {
488 int mid = (first + last) / 2;
Mathias Agopiancee79392010-07-26 21:14:59 -0700489 if (sortedArray[mid] < key) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490 first = mid + 1;
491 } else if (key < sortedArray[mid]) {
492 last = mid - 1;
493 } else {
494 return mid;
495 }
496 }
497 return -1;
498}
499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500static int cmp_configs(const void* a, const void *b)
501{
Mathias Agopiancee79392010-07-26 21:14:59 -0700502 const egl_config_t& c0 = *(egl_config_t const *)a;
503 const egl_config_t& c1 = *(egl_config_t const *)b;
504 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505}
506
507struct extention_map_t {
508 const char* name;
509 __eglMustCastToProperFunctionPointerType address;
510};
511
512static const extention_map_t gExtentionMap[] = {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700513 { "eglLockSurfaceKHR",
514 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
515 { "eglUnlockSurfaceKHR",
516 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
517 { "eglCreateImageKHR",
518 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
519 { "eglDestroyImageKHR",
520 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopian8d2e83b2009-06-24 22:37:39 -0700521 { "eglSetSwapRectangleANDROID",
522 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523};
524
Mathias Agopian24035332010-08-02 17:34:32 -0700525extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
526
527// accesses protected by gInitDriverMutex
528static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
529static int gGLExtentionSlot = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800530
531static void(*findProcAddress(const char* name,
532 const extention_map_t* map, size_t n))()
533{
534 for (uint32_t i=0 ; i<n ; i++) {
535 if (!strcmp(name, map[i].name)) {
536 return map[i].address;
537 }
538 }
539 return NULL;
540}
541
542// ----------------------------------------------------------------------------
543
Mathias Agopian6f087122010-09-23 16:38:38 -0700544static int gl_no_context() {
Mathias Agopiand274eae2009-07-31 16:21:17 -0700545 tls_t* tls = getTLS();
546 if (tls->logCallWithNoContext == EGL_TRUE) {
547 tls->logCallWithNoContext = EGL_FALSE;
548 LOGE("call to OpenGL ES API with no current context "
549 "(logged once per thread)");
550 }
Mathias Agopian6f087122010-09-23 16:38:38 -0700551 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -0700552}
553
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800554static void early_egl_init(void)
555{
556#if !USE_FAST_TLS_KEY
557 pthread_key_create(&gGLWrapperKey, NULL);
558#endif
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700559#if EGL_TRACE
560 pthread_key_create(&gGLTraceKey, NULL);
561 initEglTraceLevel();
562#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800563 uint32_t addr = (uint32_t)((void*)gl_no_context);
564 android_memset32(
Mathias Agopian618fa102009-10-14 02:06:37 -0700565 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 addr,
Mathias Agopian618fa102009-10-14 02:06:37 -0700567 sizeof(gHooksNoContext));
Mathias Agopian05c53112010-09-23 11:32:52 -0700568
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700569 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800570}
571
572static pthread_once_t once_control = PTHREAD_ONCE_INIT;
573static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
574
575
576static inline
577egl_display_t* get_display(EGLDisplay dpy)
578{
579 uintptr_t index = uintptr_t(dpy)-1U;
580 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
581}
582
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700583template<typename NATIVE, typename EGL>
584static inline NATIVE* egl_to_native_cast(EGL arg) {
585 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586}
587
588static inline
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700589egl_surface_t* get_surface(EGLSurface surface) {
590 return egl_to_native_cast<egl_surface_t>(surface);
591}
592
593static inline
594egl_context_t* get_context(EGLContext context) {
595 return egl_to_native_cast<egl_context_t>(context);
596}
597
David Li65948aa2011-03-10 16:40:37 -0800598DbgContext * getDbgContextThreadSpecific()
599{
600 return get_context(getContext())->dbg;
601}
602
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603static inline
604egl_image_t* get_image(EGLImageKHR image) {
605 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606}
607
Mathias Agopianc291f5852010-08-27 16:48:56 -0700608static inline
609egl_sync_t* get_sync(EGLSyncKHR sync) {
610 return egl_to_native_cast<egl_sync_t>(sync);
611}
612
Eric Hassold3ede7c12011-03-23 15:59:00 -0700613static inline
614egl_display_t* validate_display(EGLDisplay dpy)
615{
616 egl_display_t * const dp = get_display(dpy);
617 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_display_t*)NULL);
618 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (egl_display_t*)NULL);
619
620 return dp;
621}
622
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623static egl_connection_t* validate_display_config(
624 EGLDisplay dpy, EGLConfig config,
Mathias Agopiancee79392010-07-26 21:14:59 -0700625 egl_display_t const*& dp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700627 dp = validate_display(dpy);
628 if (!dp) return (egl_connection_t*) NULL;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629
Mathias Agopiancee79392010-07-26 21:14:59 -0700630 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
632 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700633 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 if (cnx->dso == 0) {
635 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
636 }
637 return cnx;
638}
639
640static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
641{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700642 egl_display_t const * const dp = validate_display(dpy);
643 if (!dp) return EGL_FALSE;
644 if (!dp->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800645 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700646 if (!get_context(ctx)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
648 return EGL_TRUE;
649}
650
651static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
652{
Eric Hassold3ede7c12011-03-23 15:59:00 -0700653 egl_display_t const * const dp = validate_display(dpy);
654 if (!dp) return EGL_FALSE;
655 if (!dp->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700657 if (!get_surface(surface)->isAlive())
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658 return setError(EGL_BAD_SURFACE, EGL_FALSE);
659 return EGL_TRUE;
660}
661
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700662EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
663{
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700664 ImageRef _i(image);
665 if (!_i.get()) return EGL_NO_IMAGE_KHR;
666
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700667 EGLContext context = getContext();
668 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
669 return EGL_NO_IMAGE_KHR;
670
671 egl_context_t const * const c = get_context(context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700672 if (!c->isAlive())
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700673 return EGL_NO_IMAGE_KHR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700675 egl_image_t const * const i = get_image(image);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700676 return i->images[c->impl];
677}
678
Mathias Agopian923c6612009-08-17 18:07:06 -0700679// ----------------------------------------------------------------------------
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700680
Mathias Agopian923c6612009-08-17 18:07:06 -0700681// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700682// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700683// egl_init_drivers_locked()
684//
685static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
686
687EGLBoolean egl_init_drivers_locked()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800688{
689 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700690 // initialized by static ctor. should be set here.
691 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692 }
693
Mathias Agopiande586972009-05-28 17:39:03 -0700694 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700695 Loader& loader(Loader::getInstance());
Mathias Agopiande586972009-05-28 17:39:03 -0700696
Mathias Agopian923c6612009-08-17 18:07:06 -0700697 // dynamically load all our EGL implementations for all displays
698 // and retrieve the corresponding EGLDisplay
699 // if that fails, don't use this driver.
700 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
701 egl_connection_t* cnx;
702 egl_display_t* d = &gDisplay[0];
703
704 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705 if (cnx->dso == 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700706 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
707 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
708 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700709 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700710 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700711 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700712 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700713 if (dpy == EGL_NO_DISPLAY) {
714 loader.close(cnx->dso);
715 cnx->dso = NULL;
716 }
717 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718 }
719
720 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopian923c6612009-08-17 18:07:06 -0700721 if (cnx->dso == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 char value[PROPERTY_VALUE_MAX];
723 property_get("debug.egl.hw", value, "1");
724 if (atoi(value) != 0) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700725 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
726 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
727 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopian923c6612009-08-17 18:07:06 -0700728 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700729 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian923c6612009-08-17 18:07:06 -0700730 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700731 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopian923c6612009-08-17 18:07:06 -0700732 if (dpy == EGL_NO_DISPLAY) {
733 loader.close(cnx->dso);
734 cnx->dso = NULL;
735 }
736 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 } else {
738 LOGD("3D hardware acceleration is disabled");
739 }
740 }
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700741
Mathias Agopian923c6612009-08-17 18:07:06 -0700742 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
743 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800744 }
745
Mathias Agopian923c6612009-08-17 18:07:06 -0700746 return EGL_TRUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747}
748
Mathias Agopian923c6612009-08-17 18:07:06 -0700749EGLBoolean egl_init_drivers()
750{
751 EGLBoolean res;
752 pthread_mutex_lock(&gInitDriverMutex);
753 res = egl_init_drivers_locked();
754 pthread_mutex_unlock(&gInitDriverMutex);
755 return res;
756}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700757
758// ----------------------------------------------------------------------------
759}; // namespace android
760// ----------------------------------------------------------------------------
761
762using namespace android;
763
764EGLDisplay eglGetDisplay(NativeDisplayType display)
765{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800766 clearError();
767
Mathias Agopian923c6612009-08-17 18:07:06 -0700768 uint32_t index = uint32_t(display);
769 if (index >= NUM_DISPLAYS) {
770 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
771 }
772
773 if (egl_init_drivers() == EGL_FALSE) {
774 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
775 }
776
777 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
778 return dpy;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700779}
780
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781// ----------------------------------------------------------------------------
782// Initialization
783// ----------------------------------------------------------------------------
784
785EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
786{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800787 clearError();
788
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 egl_display_t * const dp = get_display(dpy);
790 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
791
Mathias Agopian75bc2782010-02-05 16:17:01 -0800792 Mutex::Autolock _l(dp->lock);
793
794 if (dp->refs > 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800795 if (major != NULL) *major = VERSION_MAJOR;
796 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich81cd0842010-03-15 20:45:21 -0700797 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 return EGL_TRUE;
799 }
Jack Palevicha2dd6cf2010-10-26 15:21:24 -0700800
801#if EGL_TRACE
802
803 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
804 // the information from that call may be stale.)
805 initEglTraceLevel();
806
807#endif
808
809 setGLHooksThreadSpecific(&gHooksNoContext);
810
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800811 // initialize each EGL and
812 // build our own extension string first, based on the extension we know
813 // and the extension supported by our client implementation
Mathias Agopian618fa102009-10-14 02:06:37 -0700814 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800815 egl_connection_t* const cnx = &gEGLImpl[i];
816 cnx->major = -1;
817 cnx->minor = -1;
818 if (!cnx->dso)
819 continue;
820
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700821#if defined(ADRENO130)
822#warning "Adreno-130 eglInitialize() workaround"
823 /*
824 * The ADRENO 130 driver returns a different EGLDisplay each time
825 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
826 * after eglTerminate() has been called, so that eglInitialize()
827 * cannot be called again. Therefore, we need to make sure to call
828 * eglGetDisplay() before calling eglInitialize();
829 */
830 if (i == IMPL_HARDWARE) {
831 dp->disp[i].dpy =
Mathias Agopian618fa102009-10-14 02:06:37 -0700832 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700833 }
834#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700836
837 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian618fa102009-10-14 02:06:37 -0700838 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800839 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700840 // i, idpy, cnx->major, cnx->minor, cnx);
841
842 // display is now initialized
843 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800844
845 // get the query-strings for this display for each implementation
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700846 dp->disp[i].queryString.vendor =
Mathias Agopian618fa102009-10-14 02:06:37 -0700847 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700848 dp->disp[i].queryString.version =
Mathias Agopian618fa102009-10-14 02:06:37 -0700849 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700850 dp->disp[i].queryString.extensions =
Mathias Agopian618fa102009-10-14 02:06:37 -0700851 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700852 dp->disp[i].queryString.clientApi =
Mathias Agopian618fa102009-10-14 02:06:37 -0700853 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854
855 } else {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700856 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700857 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858 }
859 }
860
861 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700862 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800863 egl_connection_t* const cnx = &gEGLImpl[i];
864 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
865 EGLint n;
Mathias Agopian618fa102009-10-14 02:06:37 -0700866 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700867 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
868 if (dp->disp[i].config) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700869 if (cnx->egl.eglGetConfigs(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700870 dp->disp[i].dpy, dp->disp[i].config, n,
871 &dp->disp[i].numConfigs))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872 {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873 dp->numTotalConfigs += n;
874 res = EGL_TRUE;
875 }
876 }
877 }
878 }
879 }
880
881 if (res == EGL_TRUE) {
Mathias Agopiancee79392010-07-26 21:14:59 -0700882 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
883 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
884 egl_connection_t* const cnx = &gEGLImpl[i];
885 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
886 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
887 dp->configs[k].impl = i;
888 dp->configs[k].config = dp->disp[i].config[j];
889 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
890 // store the implementation's CONFIG_ID
891 cnx->egl.eglGetConfigAttrib(
892 dp->disp[i].dpy,
893 dp->disp[i].config[j],
894 EGL_CONFIG_ID,
895 &dp->configs[k].implConfigId);
896 k++;
897 }
898 }
899 }
900
901 // sort our configurations so we can do binary-searches
902 qsort( dp->configs,
903 dp->numTotalConfigs,
904 sizeof(egl_config_t), cmp_configs);
905
Mathias Agopian75bc2782010-02-05 16:17:01 -0800906 dp->refs++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 if (major != NULL) *major = VERSION_MAJOR;
908 if (minor != NULL) *minor = VERSION_MINOR;
909 return EGL_TRUE;
910 }
911 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
912}
913
914EGLBoolean eglTerminate(EGLDisplay dpy)
915{
Mathias Agopian923c6612009-08-17 18:07:06 -0700916 // NOTE: don't unload the drivers b/c some APIs can be called
917 // after eglTerminate() has been called. eglTerminate() only
918 // terminates an EGLDisplay, not a EGL itself.
919
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800920 clearError();
921
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922 egl_display_t* const dp = get_display(dpy);
923 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian75bc2782010-02-05 16:17:01 -0800924
925 Mutex::Autolock _l(dp->lock);
926
927 if (dp->refs == 0) {
928 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
929 }
930
931 // this is specific to Android, display termination is ref-counted.
Jack Palevich81cd0842010-03-15 20:45:21 -0700932 if (dp->refs > 1) {
933 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 return EGL_TRUE;
Jack Palevich81cd0842010-03-15 20:45:21 -0700935 }
Mathias Agopiande586972009-05-28 17:39:03 -0700936
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 EGLBoolean res = EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -0700938 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800939 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700940 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700941 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700942 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian618fa102009-10-14 02:06:37 -0700943 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700944 }
Mathias Agopian923c6612009-08-17 18:07:06 -0700945 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700946 free(dp->disp[i].config);
947
948 dp->disp[i].numConfigs = 0;
949 dp->disp[i].config = 0;
950 dp->disp[i].state = egl_display_t::TERMINATED;
951
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952 res = EGL_TRUE;
953 }
954 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -0700955
956 // TODO: all egl_object_t should be marked for termination
957
Mathias Agopian75bc2782010-02-05 16:17:01 -0800958 dp->refs--;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800959 dp->numTotalConfigs = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700960 delete [] dp->configs;
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -0800961
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800962 return res;
963}
964
965// ----------------------------------------------------------------------------
966// configuration
967// ----------------------------------------------------------------------------
968
969EGLBoolean eglGetConfigs( EGLDisplay dpy,
970 EGLConfig *configs,
971 EGLint config_size, EGLint *num_config)
972{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800973 clearError();
974
Eric Hassold3ede7c12011-03-23 15:59:00 -0700975 egl_display_t const * const dp = validate_display(dpy);
976 if (!dp) return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977
978 GLint numConfigs = dp->numTotalConfigs;
979 if (!configs) {
980 *num_config = numConfigs;
981 return EGL_TRUE;
982 }
Mathias Agopiancee79392010-07-26 21:14:59 -0700983
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984 GLint n = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -0700985 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
986 *configs++ = EGLConfig(i);
987 config_size--;
988 n++;
989 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990
991 *num_config = n;
992 return EGL_TRUE;
993}
994
995EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
996 EGLConfig *configs, EGLint config_size,
997 EGLint *num_config)
998{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -0800999 clearError();
1000
Eric Hassold3ede7c12011-03-23 15:59:00 -07001001 egl_display_t const * const dp = validate_display(dpy);
1002 if (!dp) return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003
Jack Palevich749c63d2009-03-25 15:12:17 -07001004 if (num_config==0) {
1005 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001006 }
1007
1008 EGLint n;
1009 EGLBoolean res = EGL_FALSE;
1010 *num_config = 0;
1011
1012
1013 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopiancee79392010-07-26 21:14:59 -07001014 // to do this, we have to go through the attrib_list array once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 // to figure out both its size and if it contains an EGL_CONFIG_ID
1016 // key. If so, the full array is copied and patched.
1017 // NOTE: we assume that there can be only one occurrence
1018 // of EGL_CONFIG_ID.
1019
1020 EGLint patch_index = -1;
1021 GLint attr;
1022 size_t size = 0;
Mathias Agopian04aed212010-05-17 14:45:43 -07001023 if (attrib_list) {
1024 while ((attr=attrib_list[size]) != EGL_NONE) {
1025 if (attr == EGL_CONFIG_ID)
1026 patch_index = size;
1027 size += 2;
1028 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 }
1030 if (patch_index >= 0) {
1031 size += 2; // we need copy the sentinel as well
1032 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
1033 if (new_list == 0)
1034 return setError(EGL_BAD_ALLOC, EGL_FALSE);
1035 memcpy(new_list, attrib_list, size*sizeof(EGLint));
1036
1037 // patch the requested EGL_CONFIG_ID
Mathias Agopiancee79392010-07-26 21:14:59 -07001038 bool found = false;
1039 EGLConfig ourConfig(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 EGLint& configId(new_list[patch_index+1]);
Mathias Agopiancee79392010-07-26 21:14:59 -07001041 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
1042 if (dp->configs[i].configId == configId) {
1043 ourConfig = EGLConfig(i);
1044 configId = dp->configs[i].implConfigId;
1045 found = true;
1046 break;
1047 }
1048 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001049
Mathias Agopiancee79392010-07-26 21:14:59 -07001050 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
1051 if (found && cnx->dso) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 // and switch to the new list
1053 attrib_list = const_cast<const EGLint *>(new_list);
1054
1055 // At this point, the only configuration that can match is
1056 // dp->configs[i][index], however, we don't know if it would be
1057 // rejected because of the other attributes, so we do have to call
Mathias Agopian618fa102009-10-14 02:06:37 -07001058 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 // through all the EGLimpl[].
1060 // We also know we can only get a single config back, and we know
1061 // which one.
1062
Mathias Agopian618fa102009-10-14 02:06:37 -07001063 res = cnx->egl.eglChooseConfig(
Mathias Agopiancee79392010-07-26 21:14:59 -07001064 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
1065 attrib_list, configs, config_size, &n);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 if (res && n>0) {
1067 // n has to be 0 or 1, by construction, and we already know
1068 // which config it will return (since there can be only one).
Jack Palevich749c63d2009-03-25 15:12:17 -07001069 if (configs) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001070 configs[0] = ourConfig;
Jack Palevich749c63d2009-03-25 15:12:17 -07001071 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 *num_config = 1;
1073 }
1074 }
1075
1076 free(const_cast<EGLint *>(attrib_list));
1077 return res;
1078 }
1079
Mathias Agopiancee79392010-07-26 21:14:59 -07001080
Mathias Agopian618fa102009-10-14 02:06:37 -07001081 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 egl_connection_t* const cnx = &gEGLImpl[i];
1083 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001084 if (cnx->egl.eglChooseConfig(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001085 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich749c63d2009-03-25 15:12:17 -07001086 if (configs) {
1087 // now we need to convert these client EGLConfig to our
Mathias Agopiancee79392010-07-26 21:14:59 -07001088 // internal EGLConfig format.
1089 // This is done in O(n Log(n)) time.
Jack Palevich749c63d2009-03-25 15:12:17 -07001090 for (int j=0 ; j<n ; j++) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001091 egl_config_t key(i, configs[j]);
1092 intptr_t index = binarySearch<egl_config_t>(
1093 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich749c63d2009-03-25 15:12:17 -07001094 if (index >= 0) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001095 configs[j] = EGLConfig(index);
Jack Palevich749c63d2009-03-25 15:12:17 -07001096 } else {
1097 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1098 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 }
Jack Palevich749c63d2009-03-25 15:12:17 -07001100 configs += n;
1101 config_size -= n;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 *num_config += n;
1104 res = EGL_TRUE;
1105 }
1106 }
1107 }
1108 return res;
1109}
1110
1111EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1112 EGLint attribute, EGLint *value)
1113{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001114 clearError();
1115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001117 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 if (!cnx) return EGL_FALSE;
1119
1120 if (attribute == EGL_CONFIG_ID) {
Mathias Agopiancee79392010-07-26 21:14:59 -07001121 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 return EGL_TRUE;
1123 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001124 return cnx->egl.eglGetConfigAttrib(
Mathias Agopiancee79392010-07-26 21:14:59 -07001125 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1126 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127}
1128
1129// ----------------------------------------------------------------------------
1130// surfaces
1131// ----------------------------------------------------------------------------
1132
1133EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1134 NativeWindowType window,
1135 const EGLint *attrib_list)
1136{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001137 clearError();
1138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001140 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001141 if (cnx) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001142 EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy;
1143 EGLConfig iConfig = dp->configs[intptr_t(config)].config;
1144 EGLint format;
1145
Jamie Gennis5c0c93a2011-03-14 15:34:04 -07001146 // for now fail if the window is not a Surface.
1147 int type = -1;
1148 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
1149 if ((anw->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &type) != 0) ||
1150 (type == NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT)) {
1151 LOGE("native window is a SurfaceTextureClient (currently "
1152 "unsupported)");
1153 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
1154 }
1155
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001156 // set the native window's buffers format to match this config
1157 if (cnx->egl.eglGetConfigAttrib(iDpy,
1158 iConfig, EGL_NATIVE_VISUAL_ID, &format)) {
1159 if (format != 0) {
1160 native_window_set_buffers_geometry(window, 0, 0, format);
1161 }
1162 }
1163
Mathias Agopian618fa102009-10-14 02:06:37 -07001164 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001165 iDpy, iConfig, window, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001166 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001167 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001168 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 return s;
1170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171 }
1172 return EGL_NO_SURFACE;
1173}
1174
1175EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1176 NativePixmapType pixmap,
1177 const EGLint *attrib_list)
1178{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001179 clearError();
1180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001182 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001184 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001185 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1186 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001188 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001189 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 return s;
1191 }
1192 }
1193 return EGL_NO_SURFACE;
1194}
1195
1196EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1197 const EGLint *attrib_list)
1198{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001199 clearError();
1200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001202 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 if (cnx) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001204 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopiancee79392010-07-26 21:14:59 -07001205 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1206 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001207 if (surface != EGL_NO_SURFACE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001208 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
Mathias Agopiancee79392010-07-26 21:14:59 -07001209 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210 return s;
1211 }
1212 }
1213 return EGL_NO_SURFACE;
1214}
1215
1216EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1217{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001218 clearError();
1219
Eric Hassold3ede7c12011-03-23 15:59:00 -07001220 egl_display_t const * const dp = validate_display(dpy);
1221 if (!dp) return EGL_FALSE;
1222
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001223 SurfaceRef _s(surface);
1224 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 if (!validate_display_surface(dpy, surface))
1227 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001229 egl_surface_t * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001230 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001231 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001232 if (result == EGL_TRUE) {
Mathias Agopian644bb2a2010-11-24 15:59:35 -08001233 if (s->win != NULL) {
1234 native_window_set_buffers_geometry(s->win.get(), 0, 0, 0);
1235 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001236 _s.terminate();
1237 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238 return result;
1239}
1240
1241EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1242 EGLint attribute, EGLint *value)
1243{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001244 clearError();
1245
Eric Hassold3ede7c12011-03-23 15:59:00 -07001246 egl_display_t const * const dp = validate_display(dpy);
1247 if (!dp) return EGL_FALSE;
1248
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001249 SurfaceRef _s(surface);
1250 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 if (!validate_display_surface(dpy, surface))
1253 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001254 egl_surface_t const * const s = get_surface(surface);
1255
Mathias Agopiancee79392010-07-26 21:14:59 -07001256 EGLBoolean result(EGL_TRUE);
1257 if (attribute == EGL_CONFIG_ID) {
1258 // We need to remap EGL_CONFIG_IDs
1259 *value = dp->configs[intptr_t(s->config)].configId;
1260 } else {
1261 result = s->cnx->egl.eglQuerySurface(
1262 dp->disp[s->impl].dpy, s->surface, attribute, value);
1263 }
1264
1265 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266}
1267
1268// ----------------------------------------------------------------------------
Mathias Agopiancee79392010-07-26 21:14:59 -07001269// Contexts
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270// ----------------------------------------------------------------------------
1271
1272EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1273 EGLContext share_list, const EGLint *attrib_list)
1274{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001275 clearError();
1276
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001278 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 if (cnx) {
Jamie Gennis4c39f8f2010-07-02 11:39:12 -07001280 if (share_list != EGL_NO_CONTEXT) {
1281 egl_context_t* const c = get_context(share_list);
1282 share_list = c->context;
1283 }
Mathias Agopian618fa102009-10-14 02:06:37 -07001284 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopiancee79392010-07-26 21:14:59 -07001285 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1286 dp->configs[intptr_t(config)].config,
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001287 share_list, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 if (context != EGL_NO_CONTEXT) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001289 // figure out if it's a GLESv1 or GLESv2
1290 int version = 0;
1291 if (attrib_list) {
1292 while (*attrib_list != EGL_NONE) {
1293 GLint attr = *attrib_list++;
1294 GLint value = *attrib_list++;
1295 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1296 if (value == 1) {
1297 version = GLESv1_INDEX;
1298 } else if (value == 2) {
1299 version = GLESv2_INDEX;
1300 }
1301 }
1302 };
1303 }
Mathias Agopiancee79392010-07-26 21:14:59 -07001304 egl_context_t* c = new egl_context_t(dpy, context, config,
1305 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 return c;
1307 }
1308 }
1309 return EGL_NO_CONTEXT;
1310}
1311
1312EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1313{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001314 clearError();
1315
Eric Hassold3ede7c12011-03-23 15:59:00 -07001316 egl_display_t const * const dp = validate_display(dpy);
1317 if (!dp) return EGL_FALSE;
1318
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001319 ContextRef _c(ctx);
1320 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 if (!validate_display_context(dpy, ctx))
1323 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 egl_context_t * const c = get_context(ctx);
Mathias Agopian618fa102009-10-14 02:06:37 -07001325 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001326 dp->disp[c->impl].dpy, c->context);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001327 if (result == EGL_TRUE) {
1328 _c.terminate();
1329 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 return result;
1331}
1332
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001333static void loseCurrent(egl_context_t * cur_c)
1334{
1335 if (cur_c) {
1336 egl_surface_t * cur_r = get_surface(cur_c->read);
1337 egl_surface_t * cur_d = get_surface(cur_c->draw);
1338
1339 // by construction, these are either 0 or valid (possibly terminated)
1340 // it should be impossible for these to be invalid
1341 ContextRef _cur_c(cur_c);
1342 SurfaceRef _cur_r(cur_r);
1343 SurfaceRef _cur_d(cur_d);
1344
1345 cur_c->read = NULL;
1346 cur_c->draw = NULL;
1347
1348 _cur_c.release();
1349 _cur_r.release();
1350 _cur_d.release();
1351 }
1352}
1353
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1355 EGLSurface read, EGLContext ctx)
1356{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001357 clearError();
1358
Eric Hassold3ede7c12011-03-23 15:59:00 -07001359 egl_display_t const * const dp = get_display(dpy);
1360 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1361
1362 /* If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
1363 EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
1364 a valid but uninitialized display. */
1365 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
1366 (draw != EGL_NO_SURFACE) ) {
1367 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
1368 }
1369
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001370 // get a reference to the object passed in
1371 ContextRef _c(ctx);
1372 SurfaceRef _d(draw);
1373 SurfaceRef _r(read);
1374
Eric Hassold3ede7c12011-03-23 15:59:00 -07001375 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001376 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1377 // EGL_NO_CONTEXT is valid
1378 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 }
1380
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001381 // these are the underlying implementation's object
1382 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopianaf742132009-06-25 00:01:11 -07001383 EGLSurface impl_draw = EGL_NO_SURFACE;
1384 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001385
1386 // these are our objects structs passed in
1387 egl_context_t * c = NULL;
1388 egl_surface_t const * d = NULL;
1389 egl_surface_t const * r = NULL;
1390
1391 // these are the current objects structs
1392 egl_context_t * cur_c = get_context(getContext());
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001393
1394 if (ctx != EGL_NO_CONTEXT) {
1395 c = get_context(ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001396 impl_ctx = c->context;
1397 } else {
1398 // no context given, use the implementation of the current context
1399 if (cur_c == NULL) {
1400 // no current context
1401 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001402 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1403 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001404 }
Mathias Agopian8063c3a2010-01-25 11:30:11 -08001405 // not an error, there is just no current context.
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001406 return EGL_TRUE;
1407 }
1408 }
1409
1410 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 if (draw != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001412 d = get_surface(draw);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001413 // make sure the EGLContext and EGLSurface passed in are for
1414 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001415 if (c && d->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001417 impl_draw = d->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001418 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001419
1420 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 if (read != EGL_NO_SURFACE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001422 r = get_surface(read);
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001423 // make sure the EGLContext and EGLSurface passed in are for
1424 // the same driver
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001425 if (c && r->impl != c->impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopianaf742132009-06-25 00:01:11 -07001427 impl_read = r->surface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 }
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001429
1430 EGLBoolean result;
1431
1432 if (c) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001433 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001434 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001435 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001436 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001437 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001438 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439
1440 if (result == EGL_TRUE) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001441
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001442 loseCurrent(cur_c);
1443
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001444 if (ctx != EGL_NO_CONTEXT) {
David Li65948aa2011-03-10 16:40:37 -08001445 if (!c->dbg && gEGLDebugLevel > 0)
1446 c->dbg = CreateDbgContext(c->version, c->cnx->hooks[c->version]);
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001447 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001448 setContext(ctx);
1449 _c.acquire();
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001450 _r.acquire();
1451 _d.acquire();
1452 c->read = read;
1453 c->draw = draw;
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001454 } else {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001455 setGLHooksThreadSpecific(&gHooksNoContext);
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001456 setContext(EGL_NO_CONTEXT);
1457 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 }
1459 return result;
1460}
1461
1462
1463EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1464 EGLint attribute, EGLint *value)
1465{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001466 clearError();
1467
Eric Hassold3ede7c12011-03-23 15:59:00 -07001468 egl_display_t const * const dp = validate_display(dpy);
1469 if (!dp) return EGL_FALSE;
1470
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001471 ContextRef _c(ctx);
1472 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1473
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 if (!validate_display_context(dpy, ctx))
1475 return EGL_FALSE;
1476
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 egl_context_t * const c = get_context(ctx);
1478
Mathias Agopiancee79392010-07-26 21:14:59 -07001479 EGLBoolean result(EGL_TRUE);
1480 if (attribute == EGL_CONFIG_ID) {
1481 *value = dp->configs[intptr_t(c->config)].configId;
1482 } else {
1483 // We need to remap EGL_CONFIG_IDs
1484 result = c->cnx->egl.eglQueryContext(
1485 dp->disp[c->impl].dpy, c->context, attribute, value);
1486 }
1487
1488 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489}
1490
1491EGLContext eglGetCurrentContext(void)
1492{
Mathias Agopian923c6612009-08-17 18:07:06 -07001493 // could be called before eglInitialize(), but we wouldn't have a context
1494 // then, and this function would correctly return EGL_NO_CONTEXT.
1495
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001496 clearError();
1497
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001498 EGLContext ctx = getContext();
1499 return ctx;
1500}
1501
1502EGLSurface eglGetCurrentSurface(EGLint readdraw)
1503{
Mathias Agopian923c6612009-08-17 18:07:06 -07001504 // could be called before eglInitialize(), but we wouldn't have a context
1505 // then, and this function would correctly return EGL_NO_SURFACE.
1506
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001507 clearError();
1508
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 EGLContext ctx = getContext();
1510 if (ctx) {
1511 egl_context_t const * const c = get_context(ctx);
1512 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1513 switch (readdraw) {
1514 case EGL_READ: return c->read;
1515 case EGL_DRAW: return c->draw;
1516 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1517 }
1518 }
1519 return EGL_NO_SURFACE;
1520}
1521
1522EGLDisplay eglGetCurrentDisplay(void)
1523{
Mathias Agopian923c6612009-08-17 18:07:06 -07001524 // could be called before eglInitialize(), but we wouldn't have a context
1525 // then, and this function would correctly return EGL_NO_DISPLAY.
1526
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001527 clearError();
1528
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529 EGLContext ctx = getContext();
1530 if (ctx) {
1531 egl_context_t const * const c = get_context(ctx);
1532 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1533 return c->dpy;
1534 }
1535 return EGL_NO_DISPLAY;
1536}
1537
1538EGLBoolean eglWaitGL(void)
1539{
Mathias Agopian923c6612009-08-17 18:07:06 -07001540 // could be called before eglInitialize(), but we wouldn't have a context
1541 // then, and this function would return GL_TRUE, which isn't wrong.
1542
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001543 clearError();
1544
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 EGLBoolean res = EGL_TRUE;
1546 EGLContext ctx = getContext();
1547 if (ctx) {
1548 egl_context_t const * const c = get_context(ctx);
1549 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1550 if (uint32_t(c->impl)>=2)
1551 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1552 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1553 if (!cnx->dso)
1554 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001555 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001556 }
1557 return res;
1558}
1559
1560EGLBoolean eglWaitNative(EGLint engine)
1561{
Mathias Agopian923c6612009-08-17 18:07:06 -07001562 // could be called before eglInitialize(), but we wouldn't have a context
1563 // then, and this function would return GL_TRUE, which isn't wrong.
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001564
1565 clearError();
1566
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 EGLBoolean res = EGL_TRUE;
1568 EGLContext ctx = getContext();
1569 if (ctx) {
1570 egl_context_t const * const c = get_context(ctx);
1571 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1572 if (uint32_t(c->impl)>=2)
1573 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1574 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1575 if (!cnx->dso)
1576 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001577 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 }
1579 return res;
1580}
1581
1582EGLint eglGetError(void)
1583{
1584 EGLint result = EGL_SUCCESS;
Mathias Agopian02dafb52010-09-21 15:43:59 -07001585 EGLint err;
Mathias Agopian618fa102009-10-14 02:06:37 -07001586 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian02dafb52010-09-21 15:43:59 -07001587 err = EGL_SUCCESS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001588 egl_connection_t* const cnx = &gEGLImpl[i];
1589 if (cnx->dso)
Mathias Agopian618fa102009-10-14 02:06:37 -07001590 err = cnx->egl.eglGetError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1592 result = err;
1593 }
Mathias Agopian02dafb52010-09-21 15:43:59 -07001594 err = getError();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595 if (result == EGL_SUCCESS)
Mathias Agopian02dafb52010-09-21 15:43:59 -07001596 result = err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 return result;
1598}
1599
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001600// Note: Similar implementations of these functions also exist in
1601// gl2.cpp and gl.cpp, and are used by applications that call the
1602// exported entry points directly.
1603typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
1604typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
1605
1606static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL;
1607static PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL;
1608
1609static void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image)
1610{
1611 GLeglImageOES implImage =
1612 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1613 glEGLImageTargetTexture2DOES_impl(target, implImage);
1614}
1615
1616static void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image)
1617{
1618 GLeglImageOES implImage =
1619 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
1620 glEGLImageTargetRenderbufferStorageOES_impl(target, implImage);
1621}
1622
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001623__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624{
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001625 // eglGetProcAddress() could be the very first function called
1626 // in which case we must make sure we've initialized ourselves, this
1627 // happens the first time egl_get_display() is called.
Mathias Agopian923c6612009-08-17 18:07:06 -07001628
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001629 clearError();
1630
Mathias Agopian923c6612009-08-17 18:07:06 -07001631 if (egl_init_drivers() == EGL_FALSE) {
1632 setError(EGL_BAD_PARAMETER, NULL);
1633 return NULL;
1634 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001635
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 __eglMustCastToProperFunctionPointerType addr;
1637 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1638 if (addr) return addr;
1639
Mathias Agopian24035332010-08-02 17:34:32 -07001640 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1641 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642
Mathias Agopian24035332010-08-02 17:34:32 -07001643 /*
1644 * Since eglGetProcAddress() is not associated to anything, it needs
1645 * to return a function pointer that "works" regardless of what
1646 * the current context is.
1647 *
1648 * For this reason, we return a "forwarder", a small stub that takes
1649 * care of calling the function associated with the context
1650 * currently bound.
1651 *
1652 * We first look for extensions we've already resolved, if we're seeing
1653 * this extension for the first time, we go through all our
1654 * implementations and call eglGetProcAddress() and record the
1655 * result in the appropriate implementation hooks and return the
1656 * address of the forwarder corresponding to that hook set.
1657 *
1658 */
1659
1660 const String8 name(procname);
1661 addr = gGLExtentionMap.valueFor(name);
1662 const int slot = gGLExtentionSlot;
1663
1664 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1665 "no more slots for eglGetProcAddress(\"%s\")",
1666 procname);
1667
1668 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1669 bool found = false;
1670 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1671 egl_connection_t* const cnx = &gEGLImpl[i];
1672 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1673 found = true;
Mathias Agopian4a88b522010-08-13 12:19:04 -07001674 // Extensions are independent of the bound context
1675 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1676 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001677#if EGL_TRACE
David Li2f5a6552011-03-01 16:08:10 -08001678 gHooksDebug.ext.extensions[slot] = gHooksTrace.ext.extensions[slot] =
Jack Palevicha2dd6cf2010-10-26 15:21:24 -07001679#endif
Mathias Agopian24035332010-08-02 17:34:32 -07001680 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 }
1682 }
Mathias Agopian24035332010-08-02 17:34:32 -07001683 if (found) {
1684 addr = gExtensionForwarders[slot];
Michael I. Gold609bb4d2011-01-04 01:16:59 -08001685
1686 if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) {
1687 glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr;
1688 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper;
1689 }
1690 if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) {
1691 glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr;
1692 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper;
1693 }
1694
Mathias Agopian24035332010-08-02 17:34:32 -07001695 gGLExtentionMap.add(name, addr);
1696 gGLExtentionSlot++;
1697 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699
Mathias Agopian24035332010-08-02 17:34:32 -07001700 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian24035332010-08-02 17:34:32 -07001701 return addr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001702}
1703
1704EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1705{
David Lib33d5cf2011-03-04 17:50:48 -08001706 EGLBoolean Debug_eglSwapBuffers(EGLDisplay dpy, EGLSurface draw);
1707 if (gEGLDebugLevel > 0)
1708 Debug_eglSwapBuffers(dpy, draw);
1709
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001710 clearError();
1711
Eric Hassold3ede7c12011-03-23 15:59:00 -07001712 egl_display_t const * const dp = validate_display(dpy);
1713 if (!dp) return EGL_FALSE;
1714
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001715 SurfaceRef _s(draw);
1716 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1717
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 if (!validate_display_surface(dpy, draw))
1719 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07001721 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001722}
1723
1724EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1725 NativePixmapType target)
1726{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001727 clearError();
1728
Eric Hassold3ede7c12011-03-23 15:59:00 -07001729 egl_display_t const * const dp = validate_display(dpy);
1730 if (!dp) return EGL_FALSE;
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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001738 return s->cnx->egl.eglCopyBuffers(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001739 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740}
1741
1742const char* eglQueryString(EGLDisplay dpy, EGLint name)
1743{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001744 clearError();
1745
Eric Hassold3ede7c12011-03-23 15:59:00 -07001746 egl_display_t const * const dp = validate_display(dpy);
1747 if (!dp) return (const char *) NULL;
1748
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749 switch (name) {
1750 case EGL_VENDOR:
1751 return gVendorString;
1752 case EGL_VERSION:
1753 return gVersionString;
1754 case EGL_EXTENSIONS:
1755 return gExtensionString;
1756 case EGL_CLIENT_APIS:
1757 return gClientApiString;
1758 }
1759 return setError(EGL_BAD_PARAMETER, (const char *)0);
1760}
1761
1762
1763// ----------------------------------------------------------------------------
1764// EGL 1.1
1765// ----------------------------------------------------------------------------
1766
1767EGLBoolean eglSurfaceAttrib(
1768 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1769{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001770 clearError();
1771
Eric Hassold3ede7c12011-03-23 15:59:00 -07001772 egl_display_t const * const dp = validate_display(dpy);
1773 if (!dp) return EGL_FALSE;
1774
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001775 SurfaceRef _s(surface);
1776 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001778 if (!validate_display_surface(dpy, surface))
1779 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001780 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001781 if (s->cnx->egl.eglSurfaceAttrib) {
1782 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001783 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001784 }
1785 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1786}
1787
1788EGLBoolean eglBindTexImage(
1789 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1790{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001791 clearError();
1792
Eric Hassold3ede7c12011-03-23 15:59:00 -07001793 egl_display_t const * const dp = validate_display(dpy);
1794 if (!dp) return EGL_FALSE;
1795
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001796 SurfaceRef _s(surface);
1797 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1798
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799 if (!validate_display_surface(dpy, surface))
1800 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001802 if (s->cnx->egl.eglBindTexImage) {
1803 return s->cnx->egl.eglBindTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001804 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001805 }
1806 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1807}
1808
1809EGLBoolean eglReleaseTexImage(
1810 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1811{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001812 clearError();
1813
Eric Hassold3ede7c12011-03-23 15:59:00 -07001814 egl_display_t const * const dp = validate_display(dpy);
1815 if (!dp) return EGL_FALSE;
1816
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001817 SurfaceRef _s(surface);
1818 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1819
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001820 if (!validate_display_surface(dpy, surface))
1821 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001822 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian618fa102009-10-14 02:06:37 -07001823 if (s->cnx->egl.eglReleaseTexImage) {
1824 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001825 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826 }
1827 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1828}
1829
1830EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1831{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001832 clearError();
1833
Eric Hassold3ede7c12011-03-23 15:59:00 -07001834 egl_display_t const * const dp = validate_display(dpy);
1835 if (!dp) return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836
1837 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001838 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001839 egl_connection_t* const cnx = &gEGLImpl[i];
1840 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001841 if (cnx->egl.eglSwapInterval) {
1842 if (cnx->egl.eglSwapInterval(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001843 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001844 res = EGL_FALSE;
1845 }
1846 }
1847 }
1848 }
1849 return res;
1850}
1851
1852
1853// ----------------------------------------------------------------------------
1854// EGL 1.2
1855// ----------------------------------------------------------------------------
1856
1857EGLBoolean eglWaitClient(void)
1858{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001859 clearError();
1860
Mathias Agopian923c6612009-08-17 18:07:06 -07001861 // could be called before eglInitialize(), but we wouldn't have a context
1862 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863 EGLBoolean res = EGL_TRUE;
1864 EGLContext ctx = getContext();
1865 if (ctx) {
1866 egl_context_t const * const c = get_context(ctx);
1867 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1868 if (uint32_t(c->impl)>=2)
1869 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1870 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1871 if (!cnx->dso)
1872 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian618fa102009-10-14 02:06:37 -07001873 if (cnx->egl.eglWaitClient) {
1874 res = cnx->egl.eglWaitClient();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875 } else {
Mathias Agopian618fa102009-10-14 02:06:37 -07001876 res = cnx->egl.eglWaitGL();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877 }
1878 }
1879 return res;
1880}
1881
1882EGLBoolean eglBindAPI(EGLenum api)
1883{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001884 clearError();
1885
Mathias Agopian923c6612009-08-17 18:07:06 -07001886 if (egl_init_drivers() == EGL_FALSE) {
1887 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1888 }
1889
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890 // bind this API on all EGLs
1891 EGLBoolean res = EGL_TRUE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001892 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 egl_connection_t* const cnx = &gEGLImpl[i];
1894 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001895 if (cnx->egl.eglBindAPI) {
1896 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897 res = EGL_FALSE;
1898 }
1899 }
1900 }
1901 }
1902 return res;
1903}
1904
1905EGLenum eglQueryAPI(void)
1906{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001907 clearError();
1908
Mathias Agopian923c6612009-08-17 18:07:06 -07001909 if (egl_init_drivers() == EGL_FALSE) {
1910 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1911 }
1912
Mathias Agopian618fa102009-10-14 02:06:37 -07001913 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914 egl_connection_t* const cnx = &gEGLImpl[i];
1915 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001916 if (cnx->egl.eglQueryAPI) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917 // the first one we find is okay, because they all
1918 // should be the same
Mathias Agopian618fa102009-10-14 02:06:37 -07001919 return cnx->egl.eglQueryAPI();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001920 }
1921 }
1922 }
1923 // or, it can only be OpenGL ES
1924 return EGL_OPENGL_ES_API;
1925}
1926
1927EGLBoolean eglReleaseThread(void)
1928{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001929 clearError();
1930
Michael I. Gold0c3ce2a2010-12-23 13:51:36 -08001931 // If there is context bound to the thread, release it
1932 loseCurrent(get_context(getContext()));
1933
Mathias Agopian618fa102009-10-14 02:06:37 -07001934 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 egl_connection_t* const cnx = &gEGLImpl[i];
1936 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07001937 if (cnx->egl.eglReleaseThread) {
1938 cnx->egl.eglReleaseThread();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939 }
1940 }
1941 }
1942 clearTLS();
1943 return EGL_TRUE;
1944}
1945
1946EGLSurface eglCreatePbufferFromClientBuffer(
1947 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1948 EGLConfig config, const EGLint *attrib_list)
1949{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001950 clearError();
1951
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952 egl_display_t const* dp = 0;
Mathias Agopiancee79392010-07-26 21:14:59 -07001953 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001954 if (!cnx) return EGL_FALSE;
Mathias Agopian618fa102009-10-14 02:06:37 -07001955 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1956 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopiancee79392010-07-26 21:14:59 -07001957 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1958 buftype, buffer,
1959 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001960 }
1961 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1962}
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001963
1964// ----------------------------------------------------------------------------
1965// EGL_EGLEXT_VERSION 3
1966// ----------------------------------------------------------------------------
1967
1968EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1969 const EGLint *attrib_list)
1970{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001971 clearError();
1972
Eric Hassold3ede7c12011-03-23 15:59:00 -07001973 egl_display_t const * const dp = validate_display(dpy);
1974 if (!dp) return EGL_FALSE;
1975
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001976 SurfaceRef _s(surface);
1977 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1978
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001979 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07001980 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001981
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001982 egl_surface_t const * const s = get_surface(surface);
1983
Mathias Agopian618fa102009-10-14 02:06:37 -07001984 if (s->cnx->egl.eglLockSurfaceKHR) {
1985 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07001986 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001987 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07001988 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001989}
1990
1991EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1992{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08001993 clearError();
1994
Eric Hassold3ede7c12011-03-23 15:59:00 -07001995 egl_display_t const * const dp = validate_display(dpy);
1996 if (!dp) return EGL_FALSE;
1997
Mathias Agopian9429e9c2009-08-21 02:18:25 -07001998 SurfaceRef _s(surface);
1999 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
2000
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002001 if (!validate_display_surface(dpy, surface))
Mathias Agopian24e5f522009-08-12 21:18:15 -07002002 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002003
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002004 egl_surface_t const * const s = get_surface(surface);
2005
Mathias Agopian618fa102009-10-14 02:06:37 -07002006 if (s->cnx->egl.eglUnlockSurfaceKHR) {
2007 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002008 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002009 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07002010 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002011}
2012
2013EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
2014 EGLClientBuffer buffer, const EGLint *attrib_list)
2015{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002016 clearError();
2017
Eric Hassold3ede7c12011-03-23 15:59:00 -07002018 egl_display_t const * const dp = validate_display(dpy);
2019 if (!dp) return EGL_NO_IMAGE_KHR;
2020
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002021 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002022 ContextRef _c(ctx);
2023 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002024 if (!validate_display_context(dpy, ctx))
2025 return EGL_NO_IMAGE_KHR;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002026 egl_context_t * const c = get_context(ctx);
2027 // since we have an EGLContext, we know which implementation to use
Mathias Agopian618fa102009-10-14 02:06:37 -07002028 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002029 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002030 if (image == EGL_NO_IMAGE_KHR)
2031 return image;
2032
2033 egl_image_t* result = new egl_image_t(dpy, ctx);
2034 result->images[c->impl] = image;
2035 return (EGLImageKHR)result;
2036 } else {
2037 // EGL_NO_CONTEXT is a valid parameter
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002038
2039 /* Since we don't have a way to know which implementation to call,
2040 * we're calling all of them. If at least one of the implementation
2041 * succeeded, this is a success.
2042 */
2043
2044 EGLint currentError = eglGetError();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002045
Mathias Agopian618fa102009-10-14 02:06:37 -07002046 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002047 bool success = false;
Mathias Agopian618fa102009-10-14 02:06:37 -07002048 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002049 egl_connection_t* const cnx = &gEGLImpl[i];
2050 implImages[i] = EGL_NO_IMAGE_KHR;
2051 if (cnx->dso) {
Mathias Agopian618fa102009-10-14 02:06:37 -07002052 if (cnx->egl.eglCreateImageKHR) {
2053 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002054 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002055 if (implImages[i] != EGL_NO_IMAGE_KHR) {
2056 success = true;
2057 }
2058 }
2059 }
2060 }
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002061
2062 if (!success) {
2063 // failure, if there was an error when we entered this function,
2064 // the error flag must not be updated.
2065 // Otherwise, the error is whatever happened in the implementation
2066 // that faulted.
2067 if (currentError != EGL_SUCCESS) {
2068 setError(currentError, EGL_NO_IMAGE_KHR);
2069 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002070 return EGL_NO_IMAGE_KHR;
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002071 } else {
2072 // In case of success, we need to clear all error flags
2073 // (especially those caused by the implementation that didn't
Mathias Agopian863e5fd2009-10-29 18:29:30 -07002074 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopiandf2d9292009-10-28 21:00:29 -07002075 // a "full" success (all implementation succeeded).
2076 eglGetError();
2077 }
2078
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002079 egl_image_t* result = new egl_image_t(dpy, ctx);
2080 memcpy(result->images, implImages, sizeof(implImages));
2081 return (EGLImageKHR)result;
2082 }
2083}
2084
2085EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
2086{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002087 clearError();
2088
Eric Hassold3ede7c12011-03-23 15:59:00 -07002089 egl_display_t const * const dp = validate_display(dpy);
2090 if (!dp) return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002091
Eric Hassold3ede7c12011-03-23 15:59:00 -07002092 ImageRef _i(img);
2093 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002094
Eric Hassold3ede7c12011-03-23 15:59:00 -07002095 egl_image_t* image = get_image(img);
2096 bool success = false;
2097 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
2098 egl_connection_t* const cnx = &gEGLImpl[i];
2099 if (image->images[i] != EGL_NO_IMAGE_KHR) {
2100 if (cnx->dso) {
2101 if (cnx->egl.eglDestroyImageKHR) {
2102 if (cnx->egl.eglDestroyImageKHR(
2103 dp->disp[i].dpy, image->images[i])) {
2104 success = true;
2105 }
2106 }
2107 }
2108 }
2109 }
2110 if (!success)
2111 return EGL_FALSE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002112
Eric Hassold3ede7c12011-03-23 15:59:00 -07002113 _i.terminate();
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002114
Eric Hassold3ede7c12011-03-23 15:59:00 -07002115 return EGL_TRUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002116}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002117
Mathias Agopianc291f5852010-08-27 16:48:56 -07002118// ----------------------------------------------------------------------------
2119// EGL_EGLEXT_VERSION 5
2120// ----------------------------------------------------------------------------
2121
2122
2123EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
2124{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002125 clearError();
2126
Eric Hassold3ede7c12011-03-23 15:59:00 -07002127 egl_display_t const * const dp = validate_display(dpy);
2128 if (!dp) return EGL_NO_SYNC_KHR;
2129
Mathias Agopianc291f5852010-08-27 16:48:56 -07002130 EGLContext ctx = eglGetCurrentContext();
2131 ContextRef _c(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002132 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
Mathias Agopianc291f5852010-08-27 16:48:56 -07002133 if (!validate_display_context(dpy, ctx))
Mathias Agopiana93b9572010-09-21 16:22:10 -07002134 return EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002135 egl_context_t * const c = get_context(ctx);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002136 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002137 if (c->cnx->egl.eglCreateSyncKHR) {
2138 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
2139 dp->disp[c->impl].dpy, type, attrib_list);
Mathias Agopiana93b9572010-09-21 16:22:10 -07002140 if (sync == EGL_NO_SYNC_KHR)
Mathias Agopianc291f5852010-08-27 16:48:56 -07002141 return sync;
2142 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
2143 }
2144 return (EGLSyncKHR)result;
2145}
2146
2147EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
2148{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002149 clearError();
2150
Eric Hassold3ede7c12011-03-23 15:59:00 -07002151 egl_display_t const * const dp = validate_display(dpy);
2152 if (!dp) return EGL_FALSE;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002153
2154 SyncRef _s(sync);
2155 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2156 egl_sync_t* syncObject = get_sync(sync);
2157
2158 EGLContext ctx = syncObject->context;
2159 ContextRef _c(ctx);
2160 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2161 if (!validate_display_context(dpy, ctx))
2162 return EGL_FALSE;
2163
Mathias Agopian36bdf142011-03-16 14:19:03 -07002164 EGLBoolean result = EGL_FALSE;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002165 egl_context_t * const c = get_context(ctx);
Mathias Agopianc291f5852010-08-27 16:48:56 -07002166 if (c->cnx->egl.eglDestroySyncKHR) {
Mathias Agopian36bdf142011-03-16 14:19:03 -07002167 result = c->cnx->egl.eglDestroySyncKHR(
Mathias Agopianc291f5852010-08-27 16:48:56 -07002168 dp->disp[c->impl].dpy, syncObject->sync);
Mathias Agopian36bdf142011-03-16 14:19:03 -07002169 if (result)
2170 _s.terminate();
Mathias Agopianc291f5852010-08-27 16:48:56 -07002171 }
Mathias Agopian36bdf142011-03-16 14:19:03 -07002172 return result;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002173}
2174
2175EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
2176{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002177 clearError();
2178
Eric Hassold3ede7c12011-03-23 15:59:00 -07002179 egl_display_t const * const dp = validate_display(dpy);
2180 if (!dp) return EGL_FALSE;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002181
2182 SyncRef _s(sync);
2183 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2184 egl_sync_t* syncObject = get_sync(sync);
2185
2186 EGLContext ctx = syncObject->context;
2187 ContextRef _c(ctx);
2188 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2189 if (!validate_display_context(dpy, ctx))
2190 return EGL_FALSE;
2191
2192 egl_context_t * const c = get_context(ctx);
2193
2194 if (c->cnx->egl.eglClientWaitSyncKHR) {
2195 return c->cnx->egl.eglClientWaitSyncKHR(
2196 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
2197 }
2198
2199 return EGL_FALSE;
2200}
2201
2202EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
2203{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002204 clearError();
2205
Eric Hassold3ede7c12011-03-23 15:59:00 -07002206 egl_display_t const * const dp = validate_display(dpy);
2207 if (!dp) return EGL_FALSE;
Mathias Agopianc291f5852010-08-27 16:48:56 -07002208
2209 SyncRef _s(sync);
2210 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2211 egl_sync_t* syncObject = get_sync(sync);
2212
2213 EGLContext ctx = syncObject->context;
2214 ContextRef _c(ctx);
2215 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
2216 if (!validate_display_context(dpy, ctx))
2217 return EGL_FALSE;
2218
2219 egl_context_t * const c = get_context(ctx);
2220
2221 if (c->cnx->egl.eglGetSyncAttribKHR) {
2222 return c->cnx->egl.eglGetSyncAttribKHR(
2223 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
2224 }
2225
2226 return EGL_FALSE;
2227}
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002228
2229// ----------------------------------------------------------------------------
2230// ANDROID extensions
2231// ----------------------------------------------------------------------------
2232
2233EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
2234 EGLint left, EGLint top, EGLint width, EGLint height)
2235{
Michael I. Gold4aea6bf2011-01-21 15:39:38 -08002236 clearError();
2237
Eric Hassold3ede7c12011-03-23 15:59:00 -07002238 egl_display_t const * const dp = validate_display(dpy);
2239 if (!dp) return EGL_FALSE;
2240
Mathias Agopian9429e9c2009-08-21 02:18:25 -07002241 SurfaceRef _s(draw);
2242 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
2243
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002244 if (!validate_display_surface(dpy, draw))
2245 return EGL_FALSE;
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002246 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian618fa102009-10-14 02:06:37 -07002247 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
2248 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopiana69e0ed2009-08-24 21:47:13 -07002249 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002250 }
Mathias Agopian24e5f522009-08-12 21:18:15 -07002251 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopiandf3ca302009-05-04 19:29:25 -07002252}