blob: f744b72df26fa546347bda30af841e1deb63182b [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080017#include <ctype.h>
Mathias Agopian11be99d2009-05-17 18:50:16 -070018#include <stdlib.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <string.h>
20#include <errno.h>
21#include <dlfcn.h>
22
23#include <sys/ioctl.h>
24
25#if HAVE_ANDROID_OS
26#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 Agopian4a34e882009-08-21 02:18:25 -070039#include <utils/SortedVector.h>
Mathias Agopian3944eab2010-08-02 17:34:32 -070040#include <utils/KeyedVector.h>
41#include <utils/String8.h>
Mathias Agopian4a34e882009-08-21 02:18:25 -070042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include "hooks.h"
44#include "egl_impl.h"
Mathias Agopian9d17c052009-05-28 17:39:03 -070045#include "Loader.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047#define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
48
49// ----------------------------------------------------------------------------
50namespace android {
51// ----------------------------------------------------------------------------
52
53#define VERSION_MAJOR 1
54#define VERSION_MINOR 4
55static char const * const gVendorString = "Android";
Mathias Agopiandcebf6f2009-08-17 18:07:06 -070056static char const * const gVersionString = "1.4 Android META-EGL";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057static char const * const gClientApiString = "OpenGL ES";
Mathias Agopian1473f462009-04-10 14:24:30 -070058static char const * const gExtensionString =
59 "EGL_KHR_image "
Mathias Agopian927d37c2009-05-06 23:47:08 -070060 "EGL_KHR_image_base "
61 "EGL_KHR_image_pixmap "
Mathias Agopianb20a4b92010-08-27 16:08:03 -070062 "EGL_KHR_gl_texture_2D_image "
Mathias Agopian9ca8a842010-08-27 16:48:56 -070063 "EGL_KHR_fence_sync "
Mathias Agopian1473f462009-04-10 14:24:30 -070064 "EGL_ANDROID_image_native_buffer "
Mathias Agopian2e20bff2009-05-04 19:29:25 -070065 "EGL_ANDROID_swap_rectangle "
Mathias Agopian1473f462009-04-10 14:24:30 -070066 ;
67
68// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Mathias Agopian4a34e882009-08-21 02:18:25 -070070class egl_object_t {
71 static SortedVector<egl_object_t*> sObjects;
72 static Mutex sLock;
73
74 volatile int32_t terminated;
75 mutable volatile int32_t count;
76
77public:
78 egl_object_t() : terminated(0), count(1) {
79 Mutex::Autolock _l(sLock);
80 sObjects.add(this);
81 }
82
83 inline bool isAlive() const { return !terminated; }
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085private:
Mathias Agopian4a34e882009-08-21 02:18:25 -070086 bool get() {
87 Mutex::Autolock _l(sLock);
88 if (egl_object_t::sObjects.indexOf(this) >= 0) {
89 android_atomic_inc(&count);
90 return true;
91 }
92 return false;
93 }
94
95 bool put() {
96 Mutex::Autolock _l(sLock);
97 if (android_atomic_dec(&count) == 1) {
98 sObjects.remove(this);
99 return true;
100 }
101 return false;
102 }
103
104public:
105 template <typename N, typename T>
106 struct LocalRef {
107 N* ref;
108 LocalRef(T o) : ref(0) {
109 N* native = reinterpret_cast<N*>(o);
110 if (o && native->get()) {
111 ref = native;
112 }
113 }
114 ~LocalRef() {
115 if (ref && ref->put()) {
116 delete ref;
117 }
118 }
119 inline N* get() {
120 return ref;
121 }
122 void acquire() const {
123 if (ref) {
124 android_atomic_inc(&ref->count);
125 }
126 }
127 void release() const {
128 if (ref) {
129 int32_t c = android_atomic_dec(&ref->count);
130 // ref->count cannot be 1 prior atomic_dec because we have
131 // a reference, and if we have one, it means there was
132 // already one before us.
133 LOGE_IF(c==1, "refcount is now 0 in release()");
134 }
135 }
136 void terminate() {
137 if (ref) {
138 ref->terminated = 1;
139 release();
140 }
141 }
142 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143};
144
Mathias Agopian4a34e882009-08-21 02:18:25 -0700145SortedVector<egl_object_t*> egl_object_t::sObjects;
146Mutex egl_object_t::sLock;
147
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700148
149struct egl_config_t {
150 egl_config_t() {}
151 egl_config_t(int impl, EGLConfig config)
152 : impl(impl), config(config), configId(0), implConfigId(0) { }
153 int impl; // the implementation this config is for
154 EGLConfig config; // the implementation's EGLConfig
155 EGLint configId; // our CONFIG_ID
156 EGLint implConfigId; // the implementation's CONFIG_ID
157 inline bool operator < (const egl_config_t& rhs) const {
158 if (impl < rhs.impl) return true;
159 if (impl > rhs.impl) return false;
160 return config < rhs.config;
161 }
162};
163
Mathias Agopian94263d72009-08-24 21:47:13 -0700164struct egl_display_t {
165 enum { NOT_INITIALIZED, INITIALIZED, TERMINATED };
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 struct strings_t {
168 char const * vendor;
169 char const * version;
170 char const * clientApi;
171 char const * extensions;
172 };
Mathias Agopian94263d72009-08-24 21:47:13 -0700173
174 struct DisplayImpl {
175 DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
176 state(NOT_INITIALIZED), numConfigs(0) { }
177 EGLDisplay dpy;
178 EGLConfig* config;
179 EGLint state;
180 EGLint numConfigs;
181 strings_t queryString;
182 };
183
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700184 uint32_t magic;
185 DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
186 EGLint numTotalConfigs;
187 egl_config_t* configs;
188 uint32_t refs;
189 Mutex lock;
Mathias Agopian94263d72009-08-24 21:47:13 -0700190
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700191 egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { }
Mathias Agopian4a34e882009-08-21 02:18:25 -0700192 ~egl_display_t() { magic = 0; }
193 inline bool isValid() const { return magic == '_dpy'; }
194 inline bool isAlive() const { return isValid(); }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195};
196
Mathias Agopian4a34e882009-08-21 02:18:25 -0700197struct egl_surface_t : public egl_object_t
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198{
Mathias Agopian4a34e882009-08-21 02:18:25 -0700199 typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
200
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700201 egl_surface_t(EGLDisplay dpy, EGLSurface surface, EGLConfig config,
Mathias Agopian1473f462009-04-10 14:24:30 -0700202 int impl, egl_connection_t const* cnx)
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700203 : dpy(dpy), surface(surface), config(config), impl(impl), cnx(cnx) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
205 ~egl_surface_t() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 }
207 EGLDisplay dpy;
208 EGLSurface surface;
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700209 EGLConfig config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 int impl;
211 egl_connection_t const* cnx;
212};
213
Mathias Agopian4a34e882009-08-21 02:18:25 -0700214struct egl_context_t : public egl_object_t
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215{
Mathias Agopian4a34e882009-08-21 02:18:25 -0700216 typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
217
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700218 egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
Mathias Agopian6fc56992009-10-14 02:06:37 -0700219 int impl, egl_connection_t const* cnx, int version)
220 : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx),
221 version(version)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 {
223 }
224 EGLDisplay dpy;
225 EGLContext context;
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700226 EGLConfig config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 EGLSurface read;
228 EGLSurface draw;
229 int impl;
230 egl_connection_t const* cnx;
Mathias Agopian6fc56992009-10-14 02:06:37 -0700231 int version;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232};
233
Mathias Agopian4a34e882009-08-21 02:18:25 -0700234struct egl_image_t : public egl_object_t
Mathias Agopian1473f462009-04-10 14:24:30 -0700235{
Mathias Agopian4a34e882009-08-21 02:18:25 -0700236 typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
237
Mathias Agopian1473f462009-04-10 14:24:30 -0700238 egl_image_t(EGLDisplay dpy, EGLContext context)
239 : dpy(dpy), context(context)
240 {
241 memset(images, 0, sizeof(images));
242 }
243 EGLDisplay dpy;
Mathias Agopian97961db2010-09-09 11:12:54 -0700244 EGLContext context;
Mathias Agopian6fc56992009-10-14 02:06:37 -0700245 EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian1473f462009-04-10 14:24:30 -0700246};
247
Mathias Agopian9ca8a842010-08-27 16:48:56 -0700248struct egl_sync_t : public egl_object_t
249{
250 typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
251
252 egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync)
253 : dpy(dpy), context(context), sync(sync)
254 {
255 }
256 EGLDisplay dpy;
257 EGLContext context;
258 EGLSyncKHR sync;
259};
260
Mathias Agopian4a34e882009-08-21 02:18:25 -0700261typedef egl_surface_t::Ref SurfaceRef;
262typedef egl_context_t::Ref ContextRef;
263typedef egl_image_t::Ref ImageRef;
Mathias Agopian9ca8a842010-08-27 16:48:56 -0700264typedef egl_sync_t::Ref SyncRef;
Mathias Agopian4a34e882009-08-21 02:18:25 -0700265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266struct tls_t
267{
Mathias Agopian997d1072009-07-31 16:21:17 -0700268 tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 EGLint error;
270 EGLContext ctx;
Mathias Agopian997d1072009-07-31 16:21:17 -0700271 EGLBoolean logCallWithNoContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272};
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
275// ----------------------------------------------------------------------------
276
Mathias Agopiane5478352010-04-09 13:37:34 -0700277static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278static egl_display_t gDisplay[NUM_DISPLAYS];
279static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
280static pthread_key_t gEGLThreadLocalStorageKey = -1;
281
282// ----------------------------------------------------------------------------
283
Mathias Agopian6fc56992009-10-14 02:06:37 -0700284EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS];
285EGLAPI gl_hooks_t gHooksNoContext;
Mathias Agopian3cc68d22009-05-13 00:19:22 -0700286EGLAPI pthread_key_t gGLWrapperKey = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700288#if EGL_TRACE
289
290EGLAPI pthread_key_t gGLTraceKey = -1;
291
292// ----------------------------------------------------------------------------
293
294static int gEGLTraceLevel;
295static int gEGLApplicationTraceLevel;
296extern EGLAPI gl_hooks_t gHooksTrace;
297
298static inline void setGlTraceThreadSpecific(gl_hooks_t const *value) {
299 pthread_setspecific(gGLTraceKey, value);
300}
301
302gl_hooks_t const* getGLTraceThreadSpecific() {
303 return static_cast<gl_hooks_t*>(pthread_getspecific(gGLTraceKey));
304}
305
306static void initEglTraceLevel() {
307 char value[PROPERTY_VALUE_MAX];
308 property_get("debug.egl.trace", value, "0");
309 int propertyLevel = atoi(value);
310 int applicationLevel = gEGLApplicationTraceLevel;
311 gEGLTraceLevel = propertyLevel > applicationLevel ? propertyLevel : applicationLevel;
312}
313
314static void setGLHooksThreadSpecific(gl_hooks_t const *value) {
315 if (gEGLTraceLevel > 0) {
316 setGlTraceThreadSpecific(value);
317 setGlThreadSpecific(&gHooksTrace);
318 } else {
319 setGlThreadSpecific(value);
320 }
321}
322
323/*
324 * Global entry point to allow applications to modify their own trace level.
325 * The effective trace level is the max of this level and the value of debug.egl.trace.
326 */
327extern "C"
328void setGLTraceLevel(int level) {
329 gEGLApplicationTraceLevel = level;
330}
331
332#else
333
334static inline void setGLHooksThreadSpecific(gl_hooks_t const *value) {
335 setGlThreadSpecific(value);
336}
337
338#endif
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340// ----------------------------------------------------------------------------
341
342static __attribute__((noinline))
343const char *egl_strerror(EGLint err)
344{
345 switch (err){
346 case EGL_SUCCESS: return "EGL_SUCCESS";
347 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
348 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
349 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
350 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
351 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
352 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
353 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
354 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
355 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
356 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
357 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
358 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
359 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
360 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
361 default: return "UNKNOWN";
362 }
363}
364
365static __attribute__((noinline))
366void clearTLS() {
367 if (gEGLThreadLocalStorageKey != -1) {
368 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
369 if (tls) {
370 delete tls;
371 pthread_setspecific(gEGLThreadLocalStorageKey, 0);
372 }
373 }
374}
375
376static tls_t* getTLS()
377{
378 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
379 if (tls == 0) {
380 tls = new tls_t;
381 pthread_setspecific(gEGLThreadLocalStorageKey, tls);
382 }
383 return tls;
384}
385
386template<typename T>
387static __attribute__((noinline))
388T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) {
389 if (gEGLThreadLocalStorageKey == -1) {
390 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
391 if (gEGLThreadLocalStorageKey == -1)
392 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
393 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
394 }
395 tls_t* tls = getTLS();
396 if (tls->error != error) {
397 LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
398 tls->error = error;
399 }
400 return returnValue;
401}
402
403static __attribute__((noinline))
404GLint getError() {
405 if (gEGLThreadLocalStorageKey == -1)
406 return EGL_SUCCESS;
407 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
408 if (!tls) return EGL_SUCCESS;
409 GLint error = tls->error;
410 tls->error = EGL_SUCCESS;
411 return error;
412}
413
414static __attribute__((noinline))
415void setContext(EGLContext ctx) {
416 if (gEGLThreadLocalStorageKey == -1) {
417 pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
418 if (gEGLThreadLocalStorageKey == -1)
419 pthread_key_create(&gEGLThreadLocalStorageKey, NULL);
420 pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
421 }
422 tls_t* tls = getTLS();
423 tls->ctx = ctx;
424}
425
426static __attribute__((noinline))
427EGLContext getContext() {
428 if (gEGLThreadLocalStorageKey == -1)
429 return EGL_NO_CONTEXT;
430 tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey);
431 if (!tls) return EGL_NO_CONTEXT;
432 return tls->ctx;
433}
434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435/*****************************************************************************/
436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437template<typename T>
438static __attribute__((noinline))
439int binarySearch(
440 T const sortedArray[], int first, int last, T key)
441{
442 while (first <= last) {
443 int mid = (first + last) / 2;
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700444 if (sortedArray[mid] < key) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 first = mid + 1;
446 } else if (key < sortedArray[mid]) {
447 last = mid - 1;
448 } else {
449 return mid;
450 }
451 }
452 return -1;
453}
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455static int cmp_configs(const void* a, const void *b)
456{
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700457 const egl_config_t& c0 = *(egl_config_t const *)a;
458 const egl_config_t& c1 = *(egl_config_t const *)b;
459 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460}
461
462struct extention_map_t {
463 const char* name;
464 __eglMustCastToProperFunctionPointerType address;
465};
466
467static const extention_map_t gExtentionMap[] = {
Mathias Agopian1473f462009-04-10 14:24:30 -0700468 { "eglLockSurfaceKHR",
469 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
470 { "eglUnlockSurfaceKHR",
471 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
472 { "eglCreateImageKHR",
473 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
474 { "eglDestroyImageKHR",
475 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopianc1e3ec52009-06-24 22:37:39 -0700476 { "eglSetSwapRectangleANDROID",
477 (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478};
479
Mathias Agopian3944eab2010-08-02 17:34:32 -0700480extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
481
482// accesses protected by gInitDriverMutex
483static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap;
484static int gGLExtentionSlot = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485
486static void(*findProcAddress(const char* name,
487 const extention_map_t* map, size_t n))()
488{
489 for (uint32_t i=0 ; i<n ; i++) {
490 if (!strcmp(name, map[i].name)) {
491 return map[i].address;
492 }
493 }
494 return NULL;
495}
496
497// ----------------------------------------------------------------------------
498
Mathias Agopian25b388c2010-09-23 16:38:38 -0700499static int gl_no_context() {
Mathias Agopian997d1072009-07-31 16:21:17 -0700500 tls_t* tls = getTLS();
501 if (tls->logCallWithNoContext == EGL_TRUE) {
502 tls->logCallWithNoContext = EGL_FALSE;
503 LOGE("call to OpenGL ES API with no current context "
504 "(logged once per thread)");
505 }
Mathias Agopian25b388c2010-09-23 16:38:38 -0700506 return 0;
Mathias Agopian5c6c5c72010-09-23 11:32:52 -0700507}
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509static void early_egl_init(void)
510{
511#if !USE_FAST_TLS_KEY
512 pthread_key_create(&gGLWrapperKey, NULL);
513#endif
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700514#if EGL_TRACE
515 pthread_key_create(&gGLTraceKey, NULL);
516 initEglTraceLevel();
517#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 uint32_t addr = (uint32_t)((void*)gl_no_context);
519 android_memset32(
Mathias Agopian6fc56992009-10-14 02:06:37 -0700520 (uint32_t*)(void*)&gHooksNoContext,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 addr,
Mathias Agopian6fc56992009-10-14 02:06:37 -0700522 sizeof(gHooksNoContext));
Mathias Agopian5c6c5c72010-09-23 11:32:52 -0700523
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700524 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525}
526
527static pthread_once_t once_control = PTHREAD_ONCE_INIT;
528static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
529
530
531static inline
532egl_display_t* get_display(EGLDisplay dpy)
533{
534 uintptr_t index = uintptr_t(dpy)-1U;
535 return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index];
536}
537
Mathias Agopian1473f462009-04-10 14:24:30 -0700538template<typename NATIVE, typename EGL>
539static inline NATIVE* egl_to_native_cast(EGL arg) {
540 return reinterpret_cast<NATIVE*>(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541}
542
543static inline
Mathias Agopian1473f462009-04-10 14:24:30 -0700544egl_surface_t* get_surface(EGLSurface surface) {
545 return egl_to_native_cast<egl_surface_t>(surface);
546}
547
548static inline
549egl_context_t* get_context(EGLContext context) {
550 return egl_to_native_cast<egl_context_t>(context);
551}
552
553static inline
554egl_image_t* get_image(EGLImageKHR image) {
555 return egl_to_native_cast<egl_image_t>(image);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556}
557
Mathias Agopian9ca8a842010-08-27 16:48:56 -0700558static inline
559egl_sync_t* get_sync(EGLSyncKHR sync) {
560 return egl_to_native_cast<egl_sync_t>(sync);
561}
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563static egl_connection_t* validate_display_config(
564 EGLDisplay dpy, EGLConfig config,
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700565 egl_display_t const*& dp)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566{
567 dp = get_display(dpy);
568 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL);
569
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700570 if (intptr_t(config) >= dp->numTotalConfigs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
572 }
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700573 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 if (cnx->dso == 0) {
575 return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL);
576 }
577 return cnx;
578}
579
580static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx)
581{
582 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
583 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian4a34e882009-08-21 02:18:25 -0700584 if (!get_display(dpy)->isAlive())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian4a34e882009-08-21 02:18:25 -0700586 if (!get_context(ctx)->isAlive())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
588 return EGL_TRUE;
589}
590
591static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface)
592{
593 if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS)
594 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian4a34e882009-08-21 02:18:25 -0700595 if (!get_display(dpy)->isAlive())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian4a34e882009-08-21 02:18:25 -0700597 if (!get_surface(surface)->isAlive())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 return setError(EGL_BAD_SURFACE, EGL_FALSE);
599 return EGL_TRUE;
600}
601
Mathias Agopian1473f462009-04-10 14:24:30 -0700602EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
603{
Mathias Agopian4a34e882009-08-21 02:18:25 -0700604 ImageRef _i(image);
605 if (!_i.get()) return EGL_NO_IMAGE_KHR;
606
Mathias Agopian1473f462009-04-10 14:24:30 -0700607 EGLContext context = getContext();
608 if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
609 return EGL_NO_IMAGE_KHR;
610
611 egl_context_t const * const c = get_context(context);
Mathias Agopian4a34e882009-08-21 02:18:25 -0700612 if (!c->isAlive())
Mathias Agopian1473f462009-04-10 14:24:30 -0700613 return EGL_NO_IMAGE_KHR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
Mathias Agopian1473f462009-04-10 14:24:30 -0700615 egl_image_t const * const i = get_image(image);
Mathias Agopian1473f462009-04-10 14:24:30 -0700616 return i->images[c->impl];
617}
618
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700619// ----------------------------------------------------------------------------
Mathias Agopian1473f462009-04-10 14:24:30 -0700620
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700621// this mutex protects:
Mathias Agopian94263d72009-08-24 21:47:13 -0700622// d->disp[]
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700623// egl_init_drivers_locked()
624//
625static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
626
627EGLBoolean egl_init_drivers_locked()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628{
629 if (sEarlyInitState) {
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700630 // initialized by static ctor. should be set here.
631 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633
Mathias Agopian9d17c052009-05-28 17:39:03 -0700634 // get our driver loader
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700635 Loader& loader(Loader::getInstance());
Mathias Agopian9d17c052009-05-28 17:39:03 -0700636
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700637 // dynamically load all our EGL implementations for all displays
638 // and retrieve the corresponding EGLDisplay
639 // if that fails, don't use this driver.
640 // TODO: currently we only deal with EGL_DEFAULT_DISPLAY
641 egl_connection_t* cnx;
642 egl_display_t* d = &gDisplay[0];
643
644 cnx = &gEGLImpl[IMPL_SOFTWARE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 if (cnx->dso == 0) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700646 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE];
647 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE];
648 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700649 if (cnx->dso) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700650 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700651 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!");
Mathias Agopian94263d72009-08-24 21:47:13 -0700652 d->disp[IMPL_SOFTWARE].dpy = dpy;
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700653 if (dpy == EGL_NO_DISPLAY) {
654 loader.close(cnx->dso);
655 cnx->dso = NULL;
656 }
657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659
660 cnx = &gEGLImpl[IMPL_HARDWARE];
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700661 if (cnx->dso == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 char value[PROPERTY_VALUE_MAX];
663 property_get("debug.egl.hw", value, "1");
664 if (atoi(value) != 0) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700665 cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE];
666 cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE];
667 cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700668 if (cnx->dso) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700669 EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700670 LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!");
Mathias Agopian94263d72009-08-24 21:47:13 -0700671 d->disp[IMPL_HARDWARE].dpy = dpy;
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700672 if (dpy == EGL_NO_DISPLAY) {
673 loader.close(cnx->dso);
674 cnx->dso = NULL;
675 }
676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 } else {
678 LOGD("3D hardware acceleration is disabled");
679 }
680 }
Mathias Agopian94263d72009-08-24 21:47:13 -0700681
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700682 if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) {
683 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 }
685
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700686 return EGL_TRUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687}
688
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700689EGLBoolean egl_init_drivers()
690{
691 EGLBoolean res;
692 pthread_mutex_lock(&gInitDriverMutex);
693 res = egl_init_drivers_locked();
694 pthread_mutex_unlock(&gInitDriverMutex);
695 return res;
696}
Mathias Agopian1473f462009-04-10 14:24:30 -0700697
698// ----------------------------------------------------------------------------
699}; // namespace android
700// ----------------------------------------------------------------------------
701
702using namespace android;
703
704EGLDisplay eglGetDisplay(NativeDisplayType display)
705{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700706 uint32_t index = uint32_t(display);
707 if (index >= NUM_DISPLAYS) {
708 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
709 }
710
711 if (egl_init_drivers() == EGL_FALSE) {
712 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
713 }
714
715 EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU);
716 return dpy;
Mathias Agopian1473f462009-04-10 14:24:30 -0700717}
718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719// ----------------------------------------------------------------------------
720// Initialization
721// ----------------------------------------------------------------------------
722
723EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
724{
725 egl_display_t * const dp = get_display(dpy);
726 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
727
Mathias Agopian6099ab72010-02-05 16:17:01 -0800728 Mutex::Autolock _l(dp->lock);
729
730 if (dp->refs > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 if (major != NULL) *major = VERSION_MAJOR;
732 if (minor != NULL) *minor = VERSION_MINOR;
Jack Palevich0a41c3c2010-03-15 20:45:21 -0700733 dp->refs++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 return EGL_TRUE;
735 }
Jack Palevichd4d0fb92010-10-26 15:21:24 -0700736
737#if EGL_TRACE
738
739 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
740 // the information from that call may be stale.)
741 initEglTraceLevel();
742
743#endif
744
745 setGLHooksThreadSpecific(&gHooksNoContext);
746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 // initialize each EGL and
748 // build our own extension string first, based on the extension we know
749 // and the extension supported by our client implementation
Mathias Agopian6fc56992009-10-14 02:06:37 -0700750 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 egl_connection_t* const cnx = &gEGLImpl[i];
752 cnx->major = -1;
753 cnx->minor = -1;
754 if (!cnx->dso)
755 continue;
756
Mathias Agopian94263d72009-08-24 21:47:13 -0700757#if defined(ADRENO130)
758#warning "Adreno-130 eglInitialize() workaround"
759 /*
760 * The ADRENO 130 driver returns a different EGLDisplay each time
761 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
762 * after eglTerminate() has been called, so that eglInitialize()
763 * cannot be called again. Therefore, we need to make sure to call
764 * eglGetDisplay() before calling eglInitialize();
765 */
766 if (i == IMPL_HARDWARE) {
767 dp->disp[i].dpy =
Mathias Agopian6fc56992009-10-14 02:06:37 -0700768 cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian94263d72009-08-24 21:47:13 -0700769 }
770#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771
Mathias Agopian94263d72009-08-24 21:47:13 -0700772
773 EGLDisplay idpy = dp->disp[i].dpy;
Mathias Agopian6fc56992009-10-14 02:06:37 -0700774 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
Mathias Agopian94263d72009-08-24 21:47:13 -0700776 // i, idpy, cnx->major, cnx->minor, cnx);
777
778 // display is now initialized
779 dp->disp[i].state = egl_display_t::INITIALIZED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780
781 // get the query-strings for this display for each implementation
Mathias Agopian94263d72009-08-24 21:47:13 -0700782 dp->disp[i].queryString.vendor =
Mathias Agopian6fc56992009-10-14 02:06:37 -0700783 cnx->egl.eglQueryString(idpy, EGL_VENDOR);
Mathias Agopian94263d72009-08-24 21:47:13 -0700784 dp->disp[i].queryString.version =
Mathias Agopian6fc56992009-10-14 02:06:37 -0700785 cnx->egl.eglQueryString(idpy, EGL_VERSION);
Mathias Agopian94263d72009-08-24 21:47:13 -0700786 dp->disp[i].queryString.extensions =
Mathias Agopian6fc56992009-10-14 02:06:37 -0700787 cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS);
Mathias Agopian94263d72009-08-24 21:47:13 -0700788 dp->disp[i].queryString.clientApi =
Mathias Agopian6fc56992009-10-14 02:06:37 -0700789 cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790
791 } else {
Mathias Agopian94263d72009-08-24 21:47:13 -0700792 LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
Mathias Agopian6fc56992009-10-14 02:06:37 -0700793 egl_strerror(cnx->egl.eglGetError()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
795 }
796
797 EGLBoolean res = EGL_FALSE;
Mathias Agopian6fc56992009-10-14 02:06:37 -0700798 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 egl_connection_t* const cnx = &gEGLImpl[i];
800 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
801 EGLint n;
Mathias Agopian6fc56992009-10-14 02:06:37 -0700802 if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) {
Mathias Agopian94263d72009-08-24 21:47:13 -0700803 dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n);
804 if (dp->disp[i].config) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700805 if (cnx->egl.eglGetConfigs(
Mathias Agopian94263d72009-08-24 21:47:13 -0700806 dp->disp[i].dpy, dp->disp[i].config, n,
807 &dp->disp[i].numConfigs))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 dp->numTotalConfigs += n;
810 res = EGL_TRUE;
811 }
812 }
813 }
814 }
815 }
816
817 if (res == EGL_TRUE) {
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700818 dp->configs = new egl_config_t[ dp->numTotalConfigs ];
819 for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
820 egl_connection_t* const cnx = &gEGLImpl[i];
821 if (cnx->dso && cnx->major>=0 && cnx->minor>=0) {
822 for (int j=0 ; j<dp->disp[i].numConfigs ; j++) {
823 dp->configs[k].impl = i;
824 dp->configs[k].config = dp->disp[i].config[j];
825 dp->configs[k].configId = k + 1; // CONFIG_ID start at 1
826 // store the implementation's CONFIG_ID
827 cnx->egl.eglGetConfigAttrib(
828 dp->disp[i].dpy,
829 dp->disp[i].config[j],
830 EGL_CONFIG_ID,
831 &dp->configs[k].implConfigId);
832 k++;
833 }
834 }
835 }
836
837 // sort our configurations so we can do binary-searches
838 qsort( dp->configs,
839 dp->numTotalConfigs,
840 sizeof(egl_config_t), cmp_configs);
841
Mathias Agopian6099ab72010-02-05 16:17:01 -0800842 dp->refs++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 if (major != NULL) *major = VERSION_MAJOR;
844 if (minor != NULL) *minor = VERSION_MINOR;
845 return EGL_TRUE;
846 }
847 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
848}
849
850EGLBoolean eglTerminate(EGLDisplay dpy)
851{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700852 // NOTE: don't unload the drivers b/c some APIs can be called
853 // after eglTerminate() has been called. eglTerminate() only
854 // terminates an EGLDisplay, not a EGL itself.
855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 egl_display_t* const dp = get_display(dpy);
857 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian6099ab72010-02-05 16:17:01 -0800858
859 Mutex::Autolock _l(dp->lock);
860
861 if (dp->refs == 0) {
862 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
863 }
864
865 // this is specific to Android, display termination is ref-counted.
Jack Palevich0a41c3c2010-03-15 20:45:21 -0700866 if (dp->refs > 1) {
867 dp->refs--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 return EGL_TRUE;
Jack Palevich0a41c3c2010-03-15 20:45:21 -0700869 }
Mathias Agopian9d17c052009-05-28 17:39:03 -0700870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 EGLBoolean res = EGL_FALSE;
Mathias Agopian6fc56992009-10-14 02:06:37 -0700872 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 egl_connection_t* const cnx = &gEGLImpl[i];
Mathias Agopian94263d72009-08-24 21:47:13 -0700874 if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) {
Mathias Agopian6fc56992009-10-14 02:06:37 -0700875 if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) {
Mathias Agopian94263d72009-08-24 21:47:13 -0700876 LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy,
Mathias Agopian6fc56992009-10-14 02:06:37 -0700877 egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian94263d72009-08-24 21:47:13 -0700878 }
Mathias Agopiandcebf6f2009-08-17 18:07:06 -0700879 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopian94263d72009-08-24 21:47:13 -0700880 free(dp->disp[i].config);
881
882 dp->disp[i].numConfigs = 0;
883 dp->disp[i].config = 0;
884 dp->disp[i].state = egl_display_t::TERMINATED;
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 res = EGL_TRUE;
887 }
888 }
Mathias Agopian4a34e882009-08-21 02:18:25 -0700889
890 // TODO: all egl_object_t should be marked for termination
891
Mathias Agopian6099ab72010-02-05 16:17:01 -0800892 dp->refs--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 dp->numTotalConfigs = 0;
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700894 delete [] dp->configs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 clearTLS();
896 return res;
897}
898
899// ----------------------------------------------------------------------------
900// configuration
901// ----------------------------------------------------------------------------
902
903EGLBoolean eglGetConfigs( EGLDisplay dpy,
904 EGLConfig *configs,
905 EGLint config_size, EGLint *num_config)
906{
907 egl_display_t const * const dp = get_display(dpy);
908 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
909
910 GLint numConfigs = dp->numTotalConfigs;
911 if (!configs) {
912 *num_config = numConfigs;
913 return EGL_TRUE;
914 }
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 GLint n = 0;
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700917 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
918 *configs++ = EGLConfig(i);
919 config_size--;
920 n++;
921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922
923 *num_config = n;
924 return EGL_TRUE;
925}
926
927EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
928 EGLConfig *configs, EGLint config_size,
929 EGLint *num_config)
930{
931 egl_display_t const * const dp = get_display(dpy);
932 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
933
Jack Palevich1badb712009-03-25 15:12:17 -0700934 if (num_config==0) {
935 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
937
938 EGLint n;
939 EGLBoolean res = EGL_FALSE;
940 *num_config = 0;
941
942
943 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700944 // to do this, we have to go through the attrib_list array once
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 // to figure out both its size and if it contains an EGL_CONFIG_ID
946 // key. If so, the full array is copied and patched.
947 // NOTE: we assume that there can be only one occurrence
948 // of EGL_CONFIG_ID.
949
950 EGLint patch_index = -1;
951 GLint attr;
952 size_t size = 0;
Mathias Agopian7e71fcf2010-05-17 14:45:43 -0700953 if (attrib_list) {
954 while ((attr=attrib_list[size]) != EGL_NONE) {
955 if (attr == EGL_CONFIG_ID)
956 patch_index = size;
957 size += 2;
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960 if (patch_index >= 0) {
961 size += 2; // we need copy the sentinel as well
962 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
963 if (new_list == 0)
964 return setError(EGL_BAD_ALLOC, EGL_FALSE);
965 memcpy(new_list, attrib_list, size*sizeof(EGLint));
966
967 // patch the requested EGL_CONFIG_ID
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700968 bool found = false;
969 EGLConfig ourConfig(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 EGLint& configId(new_list[patch_index+1]);
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700971 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
972 if (dp->configs[i].configId == configId) {
973 ourConfig = EGLConfig(i);
974 configId = dp->configs[i].implConfigId;
975 found = true;
976 break;
977 }
978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700980 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
981 if (found && cnx->dso) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 // and switch to the new list
983 attrib_list = const_cast<const EGLint *>(new_list);
984
985 // At this point, the only configuration that can match is
986 // dp->configs[i][index], however, we don't know if it would be
987 // rejected because of the other attributes, so we do have to call
Mathias Agopian6fc56992009-10-14 02:06:37 -0700988 // cnx->egl.eglChooseConfig() -- but we don't have to loop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 // through all the EGLimpl[].
990 // We also know we can only get a single config back, and we know
991 // which one.
992
Mathias Agopian6fc56992009-10-14 02:06:37 -0700993 res = cnx->egl.eglChooseConfig(
Mathias Agopian3ad6c442010-07-26 21:14:59 -0700994 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
995 attrib_list, configs, config_size, &n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 if (res && n>0) {
997 // n has to be 0 or 1, by construction, and we already know
998 // which config it will return (since there can be only one).
Jack Palevich1badb712009-03-25 15:12:17 -0700999 if (configs) {
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001000 configs[0] = ourConfig;
Jack Palevich1badb712009-03-25 15:12:17 -07001001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 *num_config = 1;
1003 }
1004 }
1005
1006 free(const_cast<EGLint *>(attrib_list));
1007 return res;
1008 }
1009
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001010
Mathias Agopian6fc56992009-10-14 02:06:37 -07001011 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 egl_connection_t* const cnx = &gEGLImpl[i];
1013 if (cnx->dso) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001014 if (cnx->egl.eglChooseConfig(
Mathias Agopian94263d72009-08-24 21:47:13 -07001015 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
Jack Palevich1badb712009-03-25 15:12:17 -07001016 if (configs) {
1017 // now we need to convert these client EGLConfig to our
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001018 // internal EGLConfig format.
1019 // This is done in O(n Log(n)) time.
Jack Palevich1badb712009-03-25 15:12:17 -07001020 for (int j=0 ; j<n ; j++) {
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001021 egl_config_t key(i, configs[j]);
1022 intptr_t index = binarySearch<egl_config_t>(
1023 dp->configs, 0, dp->numTotalConfigs, key);
Jack Palevich1badb712009-03-25 15:12:17 -07001024 if (index >= 0) {
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001025 configs[j] = EGLConfig(index);
Jack Palevich1badb712009-03-25 15:12:17 -07001026 } else {
1027 return setError(EGL_BAD_CONFIG, EGL_FALSE);
1028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 }
Jack Palevich1badb712009-03-25 15:12:17 -07001030 configs += n;
1031 config_size -= n;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 *num_config += n;
1034 res = EGL_TRUE;
1035 }
1036 }
1037 }
1038 return res;
1039}
1040
1041EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
1042 EGLint attribute, EGLint *value)
1043{
1044 egl_display_t const* dp = 0;
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001045 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 if (!cnx) return EGL_FALSE;
1047
1048 if (attribute == EGL_CONFIG_ID) {
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001049 *value = dp->configs[intptr_t(config)].configId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 return EGL_TRUE;
1051 }
Mathias Agopian6fc56992009-10-14 02:06:37 -07001052 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001053 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1054 dp->configs[intptr_t(config)].config, attribute, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055}
1056
1057// ----------------------------------------------------------------------------
1058// surfaces
1059// ----------------------------------------------------------------------------
1060
1061EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
1062 NativeWindowType window,
1063 const EGLint *attrib_list)
1064{
1065 egl_display_t const* dp = 0;
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001066 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 if (cnx) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001068 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001069 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1070 dp->configs[intptr_t(config)].config, window, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 if (surface != EGL_NO_SURFACE) {
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001072 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1073 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 return s;
1075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077 return EGL_NO_SURFACE;
1078}
1079
1080EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
1081 NativePixmapType pixmap,
1082 const EGLint *attrib_list)
1083{
1084 egl_display_t const* dp = 0;
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001085 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 if (cnx) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001087 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001088 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1089 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 if (surface != EGL_NO_SURFACE) {
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001091 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1092 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 return s;
1094 }
1095 }
1096 return EGL_NO_SURFACE;
1097}
1098
1099EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
1100 const EGLint *attrib_list)
1101{
1102 egl_display_t const* dp = 0;
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001103 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 if (cnx) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001105 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001106 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1107 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 if (surface != EGL_NO_SURFACE) {
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001109 egl_surface_t* s = new egl_surface_t(dpy, surface, config,
1110 dp->configs[intptr_t(config)].impl, cnx);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 return s;
1112 }
1113 }
1114 return EGL_NO_SURFACE;
1115}
1116
1117EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
1118{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001119 SurfaceRef _s(surface);
1120 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 if (!validate_display_surface(dpy, surface))
1123 return EGL_FALSE;
1124 egl_display_t const * const dp = get_display(dpy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125
Mathias Agopian4a34e882009-08-21 02:18:25 -07001126 egl_surface_t * const s = get_surface(surface);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001127 EGLBoolean result = s->cnx->egl.eglDestroySurface(
Mathias Agopian94263d72009-08-24 21:47:13 -07001128 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian4a34e882009-08-21 02:18:25 -07001129 if (result == EGL_TRUE) {
1130 _s.terminate();
1131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 return result;
1133}
1134
1135EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
1136 EGLint attribute, EGLint *value)
1137{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001138 SurfaceRef _s(surface);
1139 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 if (!validate_display_surface(dpy, surface))
1142 return EGL_FALSE;
1143 egl_display_t const * const dp = get_display(dpy);
1144 egl_surface_t const * const s = get_surface(surface);
1145
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001146 EGLBoolean result(EGL_TRUE);
1147 if (attribute == EGL_CONFIG_ID) {
1148 // We need to remap EGL_CONFIG_IDs
1149 *value = dp->configs[intptr_t(s->config)].configId;
1150 } else {
1151 result = s->cnx->egl.eglQuerySurface(
1152 dp->disp[s->impl].dpy, s->surface, attribute, value);
1153 }
1154
1155 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156}
1157
1158// ----------------------------------------------------------------------------
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001159// Contexts
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160// ----------------------------------------------------------------------------
1161
1162EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
1163 EGLContext share_list, const EGLint *attrib_list)
1164{
1165 egl_display_t const* dp = 0;
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001166 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 if (cnx) {
Jamie Gennis5149f912010-07-02 11:39:12 -07001168 if (share_list != EGL_NO_CONTEXT) {
1169 egl_context_t* const c = get_context(share_list);
1170 share_list = c->context;
1171 }
Mathias Agopian6fc56992009-10-14 02:06:37 -07001172 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001173 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1174 dp->configs[intptr_t(config)].config,
Mathias Agopian94263d72009-08-24 21:47:13 -07001175 share_list, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 if (context != EGL_NO_CONTEXT) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001177 // figure out if it's a GLESv1 or GLESv2
1178 int version = 0;
1179 if (attrib_list) {
1180 while (*attrib_list != EGL_NONE) {
1181 GLint attr = *attrib_list++;
1182 GLint value = *attrib_list++;
1183 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
1184 if (value == 1) {
1185 version = GLESv1_INDEX;
1186 } else if (value == 2) {
1187 version = GLESv2_INDEX;
1188 }
1189 }
1190 };
1191 }
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001192 egl_context_t* c = new egl_context_t(dpy, context, config,
1193 dp->configs[intptr_t(config)].impl, cnx, version);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 return c;
1195 }
1196 }
1197 return EGL_NO_CONTEXT;
1198}
1199
1200EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1201{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001202 ContextRef _c(ctx);
1203 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 if (!validate_display_context(dpy, ctx))
1206 return EGL_FALSE;
1207 egl_display_t const * const dp = get_display(dpy);
1208 egl_context_t * const c = get_context(ctx);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001209 EGLBoolean result = c->cnx->egl.eglDestroyContext(
Mathias Agopian94263d72009-08-24 21:47:13 -07001210 dp->disp[c->impl].dpy, c->context);
Mathias Agopian4a34e882009-08-21 02:18:25 -07001211 if (result == EGL_TRUE) {
1212 _c.terminate();
1213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 return result;
1215}
1216
1217EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1218 EGLSurface read, EGLContext ctx)
1219{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001220 // get a reference to the object passed in
1221 ContextRef _c(ctx);
1222 SurfaceRef _d(draw);
1223 SurfaceRef _r(read);
1224
1225 // validate the display and the context (if not EGL_NO_CONTEXT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 egl_display_t const * const dp = get_display(dpy);
1227 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian4a34e882009-08-21 02:18:25 -07001228 if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) {
1229 // EGL_NO_CONTEXT is valid
1230 return EGL_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232
Mathias Agopian4a34e882009-08-21 02:18:25 -07001233 // these are the underlying implementation's object
1234 EGLContext impl_ctx = EGL_NO_CONTEXT;
Mathias Agopian3a7e1832009-06-25 00:01:11 -07001235 EGLSurface impl_draw = EGL_NO_SURFACE;
1236 EGLSurface impl_read = EGL_NO_SURFACE;
Mathias Agopian4a34e882009-08-21 02:18:25 -07001237
1238 // these are our objects structs passed in
1239 egl_context_t * c = NULL;
1240 egl_surface_t const * d = NULL;
1241 egl_surface_t const * r = NULL;
1242
1243 // these are the current objects structs
1244 egl_context_t * cur_c = get_context(getContext());
1245 egl_surface_t * cur_r = NULL;
1246 egl_surface_t * cur_d = NULL;
1247
1248 if (ctx != EGL_NO_CONTEXT) {
1249 c = get_context(ctx);
1250 cur_r = get_surface(c->read);
1251 cur_d = get_surface(c->draw);
1252 impl_ctx = c->context;
1253 } else {
1254 // no context given, use the implementation of the current context
1255 if (cur_c == NULL) {
1256 // no current context
1257 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
Mathias Agopian7552dcf2010-01-25 11:30:11 -08001258 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
1259 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian4a34e882009-08-21 02:18:25 -07001260 }
Mathias Agopian7552dcf2010-01-25 11:30:11 -08001261 // not an error, there is just no current context.
Mathias Agopian4a34e882009-08-21 02:18:25 -07001262 return EGL_TRUE;
1263 }
1264 }
1265
1266 // retrieve the underlying implementation's draw EGLSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (draw != EGL_NO_SURFACE) {
Mathias Agopian4a34e882009-08-21 02:18:25 -07001268 d = get_surface(draw);
Mathias Agopian94263d72009-08-24 21:47:13 -07001269 // make sure the EGLContext and EGLSurface passed in are for
1270 // the same driver
Mathias Agopian4a34e882009-08-21 02:18:25 -07001271 if (c && d->impl != c->impl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian3a7e1832009-06-25 00:01:11 -07001273 impl_draw = d->surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
Mathias Agopian4a34e882009-08-21 02:18:25 -07001275
1276 // retrieve the underlying implementation's read EGLSurface
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 if (read != EGL_NO_SURFACE) {
Mathias Agopian4a34e882009-08-21 02:18:25 -07001278 r = get_surface(read);
Mathias Agopian94263d72009-08-24 21:47:13 -07001279 // make sure the EGLContext and EGLSurface passed in are for
1280 // the same driver
Mathias Agopian4a34e882009-08-21 02:18:25 -07001281 if (c && r->impl != c->impl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 return setError(EGL_BAD_MATCH, EGL_FALSE);
Mathias Agopian3a7e1832009-06-25 00:01:11 -07001283 impl_read = r->surface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
Mathias Agopian4a34e882009-08-21 02:18:25 -07001285
1286 EGLBoolean result;
1287
1288 if (c) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001289 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopian94263d72009-08-24 21:47:13 -07001290 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian4a34e882009-08-21 02:18:25 -07001291 } else {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001292 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopian94263d72009-08-24 21:47:13 -07001293 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopian4a34e882009-08-21 02:18:25 -07001294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295
1296 if (result == EGL_TRUE) {
Mathias Agopian4a34e882009-08-21 02:18:25 -07001297 // by construction, these are either 0 or valid (possibly terminated)
1298 // it should be impossible for these to be invalid
1299 ContextRef _cur_c(cur_c);
1300 SurfaceRef _cur_r(cur_r);
1301 SurfaceRef _cur_d(cur_d);
1302
1303 // cur_c has to be valid here (but could be terminated)
1304 if (ctx != EGL_NO_CONTEXT) {
Jack Palevichd4d0fb92010-10-26 15:21:24 -07001305 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
Mathias Agopian4a34e882009-08-21 02:18:25 -07001306 setContext(ctx);
1307 _c.acquire();
1308 } else {
Jack Palevichd4d0fb92010-10-26 15:21:24 -07001309 setGLHooksThreadSpecific(&gHooksNoContext);
Mathias Agopian4a34e882009-08-21 02:18:25 -07001310 setContext(EGL_NO_CONTEXT);
1311 }
1312 _cur_c.release();
1313
1314 _r.acquire();
1315 _cur_r.release();
1316 if (c) c->read = read;
1317
1318 _d.acquire();
1319 _cur_d.release();
1320 if (c) c->draw = draw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322 return result;
1323}
1324
1325
1326EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1327 EGLint attribute, EGLint *value)
1328{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001329 ContextRef _c(ctx);
1330 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 if (!validate_display_context(dpy, ctx))
1333 return EGL_FALSE;
1334
1335 egl_display_t const * const dp = get_display(dpy);
1336 egl_context_t * const c = get_context(ctx);
1337
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001338 EGLBoolean result(EGL_TRUE);
1339 if (attribute == EGL_CONFIG_ID) {
1340 *value = dp->configs[intptr_t(c->config)].configId;
1341 } else {
1342 // We need to remap EGL_CONFIG_IDs
1343 result = c->cnx->egl.eglQueryContext(
1344 dp->disp[c->impl].dpy, c->context, attribute, value);
1345 }
1346
1347 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348}
1349
1350EGLContext eglGetCurrentContext(void)
1351{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001352 // could be called before eglInitialize(), but we wouldn't have a context
1353 // then, and this function would correctly return EGL_NO_CONTEXT.
1354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 EGLContext ctx = getContext();
1356 return ctx;
1357}
1358
1359EGLSurface eglGetCurrentSurface(EGLint readdraw)
1360{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001361 // could be called before eglInitialize(), but we wouldn't have a context
1362 // then, and this function would correctly return EGL_NO_SURFACE.
1363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 EGLContext ctx = getContext();
1365 if (ctx) {
1366 egl_context_t const * const c = get_context(ctx);
1367 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1368 switch (readdraw) {
1369 case EGL_READ: return c->read;
1370 case EGL_DRAW: return c->draw;
1371 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1372 }
1373 }
1374 return EGL_NO_SURFACE;
1375}
1376
1377EGLDisplay eglGetCurrentDisplay(void)
1378{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001379 // could be called before eglInitialize(), but we wouldn't have a context
1380 // then, and this function would correctly return EGL_NO_DISPLAY.
1381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 EGLContext ctx = getContext();
1383 if (ctx) {
1384 egl_context_t const * const c = get_context(ctx);
1385 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1386 return c->dpy;
1387 }
1388 return EGL_NO_DISPLAY;
1389}
1390
1391EGLBoolean eglWaitGL(void)
1392{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001393 // could be called before eglInitialize(), but we wouldn't have a context
1394 // then, and this function would return GL_TRUE, which isn't wrong.
1395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 EGLBoolean res = EGL_TRUE;
1397 EGLContext ctx = getContext();
1398 if (ctx) {
1399 egl_context_t const * const c = get_context(ctx);
1400 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1401 if (uint32_t(c->impl)>=2)
1402 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1403 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1404 if (!cnx->dso)
1405 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001406 res = cnx->egl.eglWaitGL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
1408 return res;
1409}
1410
1411EGLBoolean eglWaitNative(EGLint engine)
1412{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001413 // could be called before eglInitialize(), but we wouldn't have a context
1414 // then, and this function would return GL_TRUE, which isn't wrong.
1415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 EGLBoolean res = EGL_TRUE;
1417 EGLContext ctx = getContext();
1418 if (ctx) {
1419 egl_context_t const * const c = get_context(ctx);
1420 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1421 if (uint32_t(c->impl)>=2)
1422 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1423 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1424 if (!cnx->dso)
1425 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001426 res = cnx->egl.eglWaitNative(engine);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
1428 return res;
1429}
1430
1431EGLint eglGetError(void)
1432{
1433 EGLint result = EGL_SUCCESS;
Mathias Agopianf65630a2010-09-21 15:43:59 -07001434 EGLint err;
Mathias Agopian6fc56992009-10-14 02:06:37 -07001435 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopianf65630a2010-09-21 15:43:59 -07001436 err = EGL_SUCCESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 egl_connection_t* const cnx = &gEGLImpl[i];
1438 if (cnx->dso)
Mathias Agopian6fc56992009-10-14 02:06:37 -07001439 err = cnx->egl.eglGetError();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
1441 result = err;
1442 }
Mathias Agopianf65630a2010-09-21 15:43:59 -07001443 err = getError();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 if (result == EGL_SUCCESS)
Mathias Agopianf65630a2010-09-21 15:43:59 -07001445 result = err;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 return result;
1447}
1448
Mathias Agopian1473f462009-04-10 14:24:30 -07001449__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450{
Mathias Agopian1473f462009-04-10 14:24:30 -07001451 // eglGetProcAddress() could be the very first function called
1452 // in which case we must make sure we've initialized ourselves, this
1453 // happens the first time egl_get_display() is called.
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001454
1455 if (egl_init_drivers() == EGL_FALSE) {
1456 setError(EGL_BAD_PARAMETER, NULL);
1457 return NULL;
1458 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 __eglMustCastToProperFunctionPointerType addr;
1461 addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap));
1462 if (addr) return addr;
1463
Mathias Agopian3944eab2010-08-02 17:34:32 -07001464 // this protects accesses to gGLExtentionMap and gGLExtentionSlot
1465 pthread_mutex_lock(&gInitDriverMutex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466
Mathias Agopian3944eab2010-08-02 17:34:32 -07001467 /*
1468 * Since eglGetProcAddress() is not associated to anything, it needs
1469 * to return a function pointer that "works" regardless of what
1470 * the current context is.
1471 *
1472 * For this reason, we return a "forwarder", a small stub that takes
1473 * care of calling the function associated with the context
1474 * currently bound.
1475 *
1476 * We first look for extensions we've already resolved, if we're seeing
1477 * this extension for the first time, we go through all our
1478 * implementations and call eglGetProcAddress() and record the
1479 * result in the appropriate implementation hooks and return the
1480 * address of the forwarder corresponding to that hook set.
1481 *
1482 */
1483
1484 const String8 name(procname);
1485 addr = gGLExtentionMap.valueFor(name);
1486 const int slot = gGLExtentionSlot;
1487
1488 LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1489 "no more slots for eglGetProcAddress(\"%s\")",
1490 procname);
1491
1492 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1493 bool found = false;
1494 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1495 egl_connection_t* const cnx = &gEGLImpl[i];
1496 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1497 found = true;
Mathias Agopianab575012010-08-13 12:19:04 -07001498 // Extensions are independent of the bound context
1499 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
1500 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
Jack Palevichd4d0fb92010-10-26 15:21:24 -07001501#if EGL_TRACE
1502 gHooksTrace.ext.extensions[slot] =
1503#endif
Mathias Agopian3944eab2010-08-02 17:34:32 -07001504 cnx->egl.eglGetProcAddress(procname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506 }
Mathias Agopian3944eab2010-08-02 17:34:32 -07001507 if (found) {
1508 addr = gExtensionForwarders[slot];
1509 gGLExtentionMap.add(name, addr);
1510 gGLExtentionSlot++;
1511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513
Mathias Agopian3944eab2010-08-02 17:34:32 -07001514 pthread_mutex_unlock(&gInitDriverMutex);
Mathias Agopian3944eab2010-08-02 17:34:32 -07001515 return addr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516}
1517
1518EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
1519{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001520 SurfaceRef _s(draw);
1521 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 if (!validate_display_surface(dpy, draw))
1524 return EGL_FALSE;
1525 egl_display_t const * const dp = get_display(dpy);
1526 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001527 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528}
1529
1530EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1531 NativePixmapType target)
1532{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001533 SurfaceRef _s(surface);
1534 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 if (!validate_display_surface(dpy, surface))
1537 return EGL_FALSE;
1538 egl_display_t const * const dp = get_display(dpy);
1539 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001540 return s->cnx->egl.eglCopyBuffers(
Mathias Agopian94263d72009-08-24 21:47:13 -07001541 dp->disp[s->impl].dpy, s->surface, target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542}
1543
1544const char* eglQueryString(EGLDisplay dpy, EGLint name)
1545{
1546 egl_display_t const * const dp = get_display(dpy);
1547 switch (name) {
1548 case EGL_VENDOR:
1549 return gVendorString;
1550 case EGL_VERSION:
1551 return gVersionString;
1552 case EGL_EXTENSIONS:
1553 return gExtensionString;
1554 case EGL_CLIENT_APIS:
1555 return gClientApiString;
1556 }
1557 return setError(EGL_BAD_PARAMETER, (const char *)0);
1558}
1559
1560
1561// ----------------------------------------------------------------------------
1562// EGL 1.1
1563// ----------------------------------------------------------------------------
1564
1565EGLBoolean eglSurfaceAttrib(
1566 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1567{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001568 SurfaceRef _s(surface);
1569 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 if (!validate_display_surface(dpy, surface))
1572 return EGL_FALSE;
1573 egl_display_t const * const dp = get_display(dpy);
1574 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001575 if (s->cnx->egl.eglSurfaceAttrib) {
1576 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopian94263d72009-08-24 21:47:13 -07001577 dp->disp[s->impl].dpy, s->surface, attribute, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 }
1579 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1580}
1581
1582EGLBoolean eglBindTexImage(
1583 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1584{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001585 SurfaceRef _s(surface);
1586 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 if (!validate_display_surface(dpy, surface))
1589 return EGL_FALSE;
1590 egl_display_t const * const dp = get_display(dpy);
1591 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001592 if (s->cnx->egl.eglBindTexImage) {
1593 return s->cnx->egl.eglBindTexImage(
Mathias Agopian94263d72009-08-24 21:47:13 -07001594 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
1596 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1597}
1598
1599EGLBoolean eglReleaseTexImage(
1600 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1601{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001602 SurfaceRef _s(surface);
1603 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 if (!validate_display_surface(dpy, surface))
1606 return EGL_FALSE;
1607 egl_display_t const * const dp = get_display(dpy);
1608 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001609 if (s->cnx->egl.eglReleaseTexImage) {
1610 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopian94263d72009-08-24 21:47:13 -07001611 dp->disp[s->impl].dpy, s->surface, buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 }
1613 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1614}
1615
1616EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1617{
1618 egl_display_t * const dp = get_display(dpy);
1619 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1620
1621 EGLBoolean res = EGL_TRUE;
Mathias Agopian6fc56992009-10-14 02:06:37 -07001622 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 egl_connection_t* const cnx = &gEGLImpl[i];
1624 if (cnx->dso) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001625 if (cnx->egl.eglSwapInterval) {
1626 if (cnx->egl.eglSwapInterval(
Mathias Agopian94263d72009-08-24 21:47:13 -07001627 dp->disp[i].dpy, interval) == EGL_FALSE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 res = EGL_FALSE;
1629 }
1630 }
1631 }
1632 }
1633 return res;
1634}
1635
1636
1637// ----------------------------------------------------------------------------
1638// EGL 1.2
1639// ----------------------------------------------------------------------------
1640
1641EGLBoolean eglWaitClient(void)
1642{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001643 // could be called before eglInitialize(), but we wouldn't have a context
1644 // then, and this function would return GL_TRUE, which isn't wrong.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 EGLBoolean res = EGL_TRUE;
1646 EGLContext ctx = getContext();
1647 if (ctx) {
1648 egl_context_t const * const c = get_context(ctx);
1649 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1650 if (uint32_t(c->impl)>=2)
1651 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1652 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1653 if (!cnx->dso)
1654 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian6fc56992009-10-14 02:06:37 -07001655 if (cnx->egl.eglWaitClient) {
1656 res = cnx->egl.eglWaitClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 } else {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001658 res = cnx->egl.eglWaitGL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660 }
1661 return res;
1662}
1663
1664EGLBoolean eglBindAPI(EGLenum api)
1665{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001666 if (egl_init_drivers() == EGL_FALSE) {
1667 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1668 }
1669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 // bind this API on all EGLs
1671 EGLBoolean res = EGL_TRUE;
Mathias Agopian6fc56992009-10-14 02:06:37 -07001672 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 egl_connection_t* const cnx = &gEGLImpl[i];
1674 if (cnx->dso) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001675 if (cnx->egl.eglBindAPI) {
1676 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 res = EGL_FALSE;
1678 }
1679 }
1680 }
1681 }
1682 return res;
1683}
1684
1685EGLenum eglQueryAPI(void)
1686{
Mathias Agopiandcebf6f2009-08-17 18:07:06 -07001687 if (egl_init_drivers() == EGL_FALSE) {
1688 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1689 }
1690
Mathias Agopian6fc56992009-10-14 02:06:37 -07001691 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 egl_connection_t* const cnx = &gEGLImpl[i];
1693 if (cnx->dso) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001694 if (cnx->egl.eglQueryAPI) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 // the first one we find is okay, because they all
1696 // should be the same
Mathias Agopian6fc56992009-10-14 02:06:37 -07001697 return cnx->egl.eglQueryAPI();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
1699 }
1700 }
1701 // or, it can only be OpenGL ES
1702 return EGL_OPENGL_ES_API;
1703}
1704
1705EGLBoolean eglReleaseThread(void)
1706{
Mathias Agopian6fc56992009-10-14 02:06:37 -07001707 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 egl_connection_t* const cnx = &gEGLImpl[i];
1709 if (cnx->dso) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001710 if (cnx->egl.eglReleaseThread) {
1711 cnx->egl.eglReleaseThread();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
1713 }
1714 }
1715 clearTLS();
1716 return EGL_TRUE;
1717}
1718
1719EGLSurface eglCreatePbufferFromClientBuffer(
1720 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1721 EGLConfig config, const EGLint *attrib_list)
1722{
1723 egl_display_t const* dp = 0;
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001724 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 if (!cnx) return EGL_FALSE;
Mathias Agopian6fc56992009-10-14 02:06:37 -07001726 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1727 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian3ad6c442010-07-26 21:14:59 -07001728 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1729 buftype, buffer,
1730 dp->configs[intptr_t(config)].config, attrib_list);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
1732 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1733}
Mathias Agopian1473f462009-04-10 14:24:30 -07001734
1735// ----------------------------------------------------------------------------
1736// EGL_EGLEXT_VERSION 3
1737// ----------------------------------------------------------------------------
1738
1739EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1740 const EGLint *attrib_list)
1741{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001742 SurfaceRef _s(surface);
1743 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1744
Mathias Agopian1473f462009-04-10 14:24:30 -07001745 if (!validate_display_surface(dpy, surface))
Mathias Agopian88e3e6b2009-08-12 21:18:15 -07001746 return EGL_FALSE;
Mathias Agopian1473f462009-04-10 14:24:30 -07001747
1748 egl_display_t const * const dp = get_display(dpy);
1749 egl_surface_t const * const s = get_surface(surface);
1750
Mathias Agopian6fc56992009-10-14 02:06:37 -07001751 if (s->cnx->egl.eglLockSurfaceKHR) {
1752 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopian94263d72009-08-24 21:47:13 -07001753 dp->disp[s->impl].dpy, s->surface, attrib_list);
Mathias Agopian1473f462009-04-10 14:24:30 -07001754 }
Mathias Agopian88e3e6b2009-08-12 21:18:15 -07001755 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001756}
1757
1758EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1759{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001760 SurfaceRef _s(surface);
1761 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1762
Mathias Agopian1473f462009-04-10 14:24:30 -07001763 if (!validate_display_surface(dpy, surface))
Mathias Agopian88e3e6b2009-08-12 21:18:15 -07001764 return EGL_FALSE;
Mathias Agopian1473f462009-04-10 14:24:30 -07001765
1766 egl_display_t const * const dp = get_display(dpy);
1767 egl_surface_t const * const s = get_surface(surface);
1768
Mathias Agopian6fc56992009-10-14 02:06:37 -07001769 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1770 return s->cnx->egl.eglUnlockSurfaceKHR(
Mathias Agopian94263d72009-08-24 21:47:13 -07001771 dp->disp[s->impl].dpy, s->surface);
Mathias Agopian1473f462009-04-10 14:24:30 -07001772 }
Mathias Agopian88e3e6b2009-08-12 21:18:15 -07001773 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001774}
1775
1776EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1777 EGLClientBuffer buffer, const EGLint *attrib_list)
1778{
1779 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopian4a34e882009-08-21 02:18:25 -07001780 ContextRef _c(ctx);
1781 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian1473f462009-04-10 14:24:30 -07001782 if (!validate_display_context(dpy, ctx))
1783 return EGL_NO_IMAGE_KHR;
1784 egl_display_t const * const dp = get_display(dpy);
1785 egl_context_t * const c = get_context(ctx);
1786 // since we have an EGLContext, we know which implementation to use
Mathias Agopian6fc56992009-10-14 02:06:37 -07001787 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
Mathias Agopian94263d72009-08-24 21:47:13 -07001788 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
Mathias Agopian1473f462009-04-10 14:24:30 -07001789 if (image == EGL_NO_IMAGE_KHR)
1790 return image;
1791
1792 egl_image_t* result = new egl_image_t(dpy, ctx);
1793 result->images[c->impl] = image;
1794 return (EGLImageKHR)result;
1795 } else {
1796 // EGL_NO_CONTEXT is a valid parameter
1797 egl_display_t const * const dp = get_display(dpy);
1798 if (dp == 0) {
1799 return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR);
1800 }
Mathias Agopianf007a2f2009-10-28 21:00:29 -07001801
1802 /* Since we don't have a way to know which implementation to call,
1803 * we're calling all of them. If at least one of the implementation
1804 * succeeded, this is a success.
1805 */
1806
1807 EGLint currentError = eglGetError();
Mathias Agopian1473f462009-04-10 14:24:30 -07001808
Mathias Agopian6fc56992009-10-14 02:06:37 -07001809 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
Mathias Agopian1473f462009-04-10 14:24:30 -07001810 bool success = false;
Mathias Agopian6fc56992009-10-14 02:06:37 -07001811 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001812 egl_connection_t* const cnx = &gEGLImpl[i];
1813 implImages[i] = EGL_NO_IMAGE_KHR;
1814 if (cnx->dso) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001815 if (cnx->egl.eglCreateImageKHR) {
1816 implImages[i] = cnx->egl.eglCreateImageKHR(
Mathias Agopian94263d72009-08-24 21:47:13 -07001817 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
Mathias Agopian1473f462009-04-10 14:24:30 -07001818 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1819 success = true;
1820 }
1821 }
1822 }
1823 }
Mathias Agopianf007a2f2009-10-28 21:00:29 -07001824
1825 if (!success) {
1826 // failure, if there was an error when we entered this function,
1827 // the error flag must not be updated.
1828 // Otherwise, the error is whatever happened in the implementation
1829 // that faulted.
1830 if (currentError != EGL_SUCCESS) {
1831 setError(currentError, EGL_NO_IMAGE_KHR);
1832 }
Mathias Agopian1473f462009-04-10 14:24:30 -07001833 return EGL_NO_IMAGE_KHR;
Mathias Agopianf007a2f2009-10-28 21:00:29 -07001834 } else {
1835 // In case of success, we need to clear all error flags
1836 // (especially those caused by the implementation that didn't
Mathias Agopian0b0722f2009-10-29 18:29:30 -07001837 // succeed). TODO: we could avoid this if we knew this was
Mathias Agopianf007a2f2009-10-28 21:00:29 -07001838 // a "full" success (all implementation succeeded).
1839 eglGetError();
1840 }
1841
Mathias Agopian1473f462009-04-10 14:24:30 -07001842 egl_image_t* result = new egl_image_t(dpy, ctx);
1843 memcpy(result->images, implImages, sizeof(implImages));
1844 return (EGLImageKHR)result;
1845 }
1846}
1847
1848EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1849{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001850 egl_display_t const * const dp = get_display(dpy);
Mathias Agopian1473f462009-04-10 14:24:30 -07001851 if (dp == 0) {
1852 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1853 }
1854
Mathias Agopian4a34e882009-08-21 02:18:25 -07001855 ImageRef _i(img);
1856 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian1473f462009-04-10 14:24:30 -07001857
Mathias Agopian4a34e882009-08-21 02:18:25 -07001858 egl_image_t* image = get_image(img);
Mathias Agopian1473f462009-04-10 14:24:30 -07001859 bool success = false;
Mathias Agopian6fc56992009-10-14 02:06:37 -07001860 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001861 egl_connection_t* const cnx = &gEGLImpl[i];
1862 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1863 if (cnx->dso) {
Mathias Agopian97961db2010-09-09 11:12:54 -07001864 if (cnx->egl.eglDestroyImageKHR) {
Mathias Agopian6fc56992009-10-14 02:06:37 -07001865 if (cnx->egl.eglDestroyImageKHR(
Mathias Agopian94263d72009-08-24 21:47:13 -07001866 dp->disp[i].dpy, image->images[i])) {
Mathias Agopian1473f462009-04-10 14:24:30 -07001867 success = true;
1868 }
1869 }
1870 }
1871 }
1872 }
1873 if (!success)
1874 return EGL_FALSE;
1875
Mathias Agopian4a34e882009-08-21 02:18:25 -07001876 _i.terminate();
Mathias Agopian1473f462009-04-10 14:24:30 -07001877
Mathias Agopian88e3e6b2009-08-12 21:18:15 -07001878 return EGL_TRUE;
Mathias Agopian1473f462009-04-10 14:24:30 -07001879}
Mathias Agopian2e20bff2009-05-04 19:29:25 -07001880
Mathias Agopian9ca8a842010-08-27 16:48:56 -07001881// ----------------------------------------------------------------------------
1882// EGL_EGLEXT_VERSION 5
1883// ----------------------------------------------------------------------------
1884
1885
1886EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1887{
1888 EGLContext ctx = eglGetCurrentContext();
1889 ContextRef _c(ctx);
Mathias Agopian4644ea72010-09-21 16:22:10 -07001890 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
Mathias Agopian9ca8a842010-08-27 16:48:56 -07001891 if (!validate_display_context(dpy, ctx))
Mathias Agopian4644ea72010-09-21 16:22:10 -07001892 return EGL_NO_SYNC_KHR;
Mathias Agopian9ca8a842010-08-27 16:48:56 -07001893 egl_display_t const * const dp = get_display(dpy);
1894 egl_context_t * const c = get_context(ctx);
Mathias Agopian4644ea72010-09-21 16:22:10 -07001895 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian9ca8a842010-08-27 16:48:56 -07001896 if (c->cnx->egl.eglCreateSyncKHR) {
1897 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
1898 dp->disp[c->impl].dpy, type, attrib_list);
Mathias Agopian4644ea72010-09-21 16:22:10 -07001899 if (sync == EGL_NO_SYNC_KHR)
Mathias Agopian9ca8a842010-08-27 16:48:56 -07001900 return sync;
1901 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
1902 }
1903 return (EGLSyncKHR)result;
1904}
1905
1906EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1907{
1908 egl_display_t const * const dp = get_display(dpy);
1909 if (dp == 0) {
1910 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1911 }
1912
1913 SyncRef _s(sync);
1914 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1915 egl_sync_t* syncObject = get_sync(sync);
1916
1917 EGLContext ctx = syncObject->context;
1918 ContextRef _c(ctx);
1919 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1920 if (!validate_display_context(dpy, ctx))
1921 return EGL_FALSE;
1922
1923 egl_context_t * const c = get_context(ctx);
1924
1925 if (c->cnx->egl.eglDestroySyncKHR) {
1926 return c->cnx->egl.eglDestroySyncKHR(
1927 dp->disp[c->impl].dpy, syncObject->sync);
1928 }
1929
1930 return EGL_FALSE;
1931}
1932
1933EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
1934{
1935 egl_display_t const * const dp = get_display(dpy);
1936 if (dp == 0) {
1937 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1938 }
1939
1940 SyncRef _s(sync);
1941 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1942 egl_sync_t* syncObject = get_sync(sync);
1943
1944 EGLContext ctx = syncObject->context;
1945 ContextRef _c(ctx);
1946 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1947 if (!validate_display_context(dpy, ctx))
1948 return EGL_FALSE;
1949
1950 egl_context_t * const c = get_context(ctx);
1951
1952 if (c->cnx->egl.eglClientWaitSyncKHR) {
1953 return c->cnx->egl.eglClientWaitSyncKHR(
1954 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
1955 }
1956
1957 return EGL_FALSE;
1958}
1959
1960EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
1961{
1962 egl_display_t const * const dp = get_display(dpy);
1963 if (dp == 0) {
1964 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1965 }
1966
1967 SyncRef _s(sync);
1968 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1969 egl_sync_t* syncObject = get_sync(sync);
1970
1971 EGLContext ctx = syncObject->context;
1972 ContextRef _c(ctx);
1973 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1974 if (!validate_display_context(dpy, ctx))
1975 return EGL_FALSE;
1976
1977 egl_context_t * const c = get_context(ctx);
1978
1979 if (c->cnx->egl.eglGetSyncAttribKHR) {
1980 return c->cnx->egl.eglGetSyncAttribKHR(
1981 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
1982 }
1983
1984 return EGL_FALSE;
1985}
Mathias Agopian2e20bff2009-05-04 19:29:25 -07001986
1987// ----------------------------------------------------------------------------
1988// ANDROID extensions
1989// ----------------------------------------------------------------------------
1990
1991EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
1992 EGLint left, EGLint top, EGLint width, EGLint height)
1993{
Mathias Agopian4a34e882009-08-21 02:18:25 -07001994 SurfaceRef _s(draw);
1995 if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
1996
Mathias Agopian2e20bff2009-05-04 19:29:25 -07001997 if (!validate_display_surface(dpy, draw))
1998 return EGL_FALSE;
1999 egl_display_t const * const dp = get_display(dpy);
2000 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian6fc56992009-10-14 02:06:37 -07002001 if (s->cnx->egl.eglSetSwapRectangleANDROID) {
2002 return s->cnx->egl.eglSetSwapRectangleANDROID(
Mathias Agopian94263d72009-08-24 21:47:13 -07002003 dp->disp[s->impl].dpy, s->surface, left, top, width, height);
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002004 }
Mathias Agopian88e3e6b2009-08-12 21:18:15 -07002005 return setError(EGL_BAD_DISPLAY, NULL);
Mathias Agopian2e20bff2009-05-04 19:29:25 -07002006}