blob: 8b37da56c9e7429b6f3975675efdc258da1c802b [file] [log] [blame]
Mathias Agopian518ec112011-05-13 16:21:08 -07001/*
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
17#include <ctype.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include <hardware/gralloc.h>
22#include <system/window.h>
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28
29#include <cutils/log.h>
30#include <cutils/atomic.h>
31#include <cutils/properties.h>
32#include <cutils/memory.h>
33
34#include <utils/KeyedVector.h>
35#include <utils/SortedVector.h>
36#include <utils/String8.h>
37
38#include "egl_impl.h"
39#include "egl_tls.h"
Siva Velusamy0469dd62011-11-30 15:05:37 -080040#include "glestrace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070041#include "hooks.h"
42
43#include "egl_display.h"
44#include "egl_impl.h"
45#include "egl_object.h"
46#include "egl_tls.h"
47
48using namespace android;
49
50// ----------------------------------------------------------------------------
51
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080052#define EGL_VERSION_HW_ANDROID 0x3143
53
Mathias Agopian518ec112011-05-13 16:21:08 -070054struct extention_map_t {
55 const char* name;
56 __eglMustCastToProperFunctionPointerType address;
57};
58
59static const extention_map_t sExtentionMap[] = {
60 { "eglLockSurfaceKHR",
61 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
62 { "eglUnlockSurfaceKHR",
63 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
64 { "eglCreateImageKHR",
65 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
66 { "eglDestroyImageKHR",
67 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Jonas Yang1c3d72a2011-08-26 20:04:39 +080068 { "eglGetSystemTimeFrequencyNV",
69 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
70 { "eglGetSystemTimeNV",
71 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopian518ec112011-05-13 16:21:08 -070072};
73
74// accesses protected by sExtensionMapMutex
75static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
76static int sGLExtentionSlot = 0;
77static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
78
79static void(*findProcAddress(const char* name,
80 const extention_map_t* map, size_t n))() {
81 for (uint32_t i=0 ; i<n ; i++) {
82 if (!strcmp(name, map[i].name)) {
83 return map[i].address;
84 }
85 }
86 return NULL;
87}
88
89// ----------------------------------------------------------------------------
90
91template<typename T>
92static __attribute__((noinline))
93int binarySearch(T const sortedArray[], int first, int last, T key) {
94 while (first <= last) {
95 int mid = (first + last) / 2;
96 if (sortedArray[mid] < key) {
97 first = mid + 1;
98 } else if (key < sortedArray[mid]) {
99 last = mid - 1;
100 } else {
101 return mid;
102 }
103 }
104 return -1;
105}
106
107// ----------------------------------------------------------------------------
108
109namespace android {
110extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
111extern EGLBoolean egl_init_drivers();
112extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
113extern int gEGLDebugLevel;
114extern gl_hooks_t gHooksTrace;
Mathias Agopian518ec112011-05-13 16:21:08 -0700115} // namespace android;
116
117// ----------------------------------------------------------------------------
118
119static inline void clearError() { egl_tls_t::clearError(); }
120static inline EGLContext getContext() { return egl_tls_t::getContext(); }
121
122// ----------------------------------------------------------------------------
123
124EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
125{
126 clearError();
127
128 uint32_t index = uint32_t(display);
129 if (index >= NUM_DISPLAYS) {
130 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
131 }
132
133 if (egl_init_drivers() == EGL_FALSE) {
134 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
135 }
136
137 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
138 return dpy;
139}
140
141// ----------------------------------------------------------------------------
142// Initialization
143// ----------------------------------------------------------------------------
144
145EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
146{
147 clearError();
148
149 egl_display_t * const dp = get_display(dpy);
150 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
151
152 EGLBoolean res = dp->initialize(major, minor);
153
154 return res;
155}
156
157EGLBoolean eglTerminate(EGLDisplay dpy)
158{
159 // NOTE: don't unload the drivers b/c some APIs can be called
160 // after eglTerminate() has been called. eglTerminate() only
161 // terminates an EGLDisplay, not a EGL itself.
162
163 clearError();
164
165 egl_display_t* const dp = get_display(dpy);
166 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
167
168 EGLBoolean res = dp->terminate();
169
170 return res;
171}
172
173// ----------------------------------------------------------------------------
174// configuration
175// ----------------------------------------------------------------------------
176
177EGLBoolean eglGetConfigs( EGLDisplay dpy,
178 EGLConfig *configs,
179 EGLint config_size, EGLint *num_config)
180{
181 clearError();
182
183 egl_display_t const * const dp = validate_display(dpy);
184 if (!dp) return EGL_FALSE;
185
186 GLint numConfigs = dp->numTotalConfigs;
187 if (!configs) {
188 *num_config = numConfigs;
189 return EGL_TRUE;
190 }
191
192 GLint n = 0;
193 for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) {
194 *configs++ = EGLConfig(i);
195 config_size--;
196 n++;
197 }
198
199 *num_config = n;
200 return EGL_TRUE;
201}
202
203EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
204 EGLConfig *configs, EGLint config_size,
205 EGLint *num_config)
206{
207 clearError();
208
209 egl_display_t const * const dp = validate_display(dpy);
210 if (!dp) return EGL_FALSE;
211
212 if (num_config==0) {
213 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
214 }
215
216 EGLint n;
217 EGLBoolean res = EGL_FALSE;
218 *num_config = 0;
219
220
221 // It is unfortunate, but we need to remap the EGL_CONFIG_IDs,
222 // to do this, we have to go through the attrib_list array once
223 // to figure out both its size and if it contains an EGL_CONFIG_ID
224 // key. If so, the full array is copied and patched.
225 // NOTE: we assume that there can be only one occurrence
226 // of EGL_CONFIG_ID.
227
228 EGLint patch_index = -1;
229 GLint attr;
230 size_t size = 0;
231 if (attrib_list) {
232 while ((attr=attrib_list[size]) != EGL_NONE) {
233 if (attr == EGL_CONFIG_ID)
234 patch_index = size;
235 size += 2;
236 }
237 }
238 if (patch_index >= 0) {
239 size += 2; // we need copy the sentinel as well
240 EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint));
241 if (new_list == 0)
242 return setError(EGL_BAD_ALLOC, EGL_FALSE);
243 memcpy(new_list, attrib_list, size*sizeof(EGLint));
244
245 // patch the requested EGL_CONFIG_ID
246 bool found = false;
247 EGLConfig ourConfig(0);
248 EGLint& configId(new_list[patch_index+1]);
249 for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) {
250 if (dp->configs[i].configId == configId) {
251 ourConfig = EGLConfig(i);
252 configId = dp->configs[i].implConfigId;
253 found = true;
254 break;
255 }
256 }
257
258 egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl];
259 if (found && cnx->dso) {
260 // and switch to the new list
261 attrib_list = const_cast<const EGLint *>(new_list);
262
263 // At this point, the only configuration that can match is
264 // dp->configs[i][index], however, we don't know if it would be
265 // rejected because of the other attributes, so we do have to call
266 // cnx->egl.eglChooseConfig() -- but we don't have to loop
267 // through all the EGLimpl[].
268 // We also know we can only get a single config back, and we know
269 // which one.
270
271 res = cnx->egl.eglChooseConfig(
272 dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy,
273 attrib_list, configs, config_size, &n);
274 if (res && n>0) {
275 // n has to be 0 or 1, by construction, and we already know
276 // which config it will return (since there can be only one).
277 if (configs) {
278 configs[0] = ourConfig;
279 }
280 *num_config = 1;
281 }
282 }
283
284 free(const_cast<EGLint *>(attrib_list));
285 return res;
286 }
287
288
289 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
290 egl_connection_t* const cnx = &gEGLImpl[i];
291 if (cnx->dso) {
292 if (cnx->egl.eglChooseConfig(
293 dp->disp[i].dpy, attrib_list, configs, config_size, &n)) {
294 if (configs) {
295 // now we need to convert these client EGLConfig to our
296 // internal EGLConfig format.
297 // This is done in O(n Log(n)) time.
298 for (int j=0 ; j<n ; j++) {
299 egl_config_t key(i, configs[j]);
300 intptr_t index = binarySearch<egl_config_t>(
301 dp->configs, 0, dp->numTotalConfigs, key);
302 if (index >= 0) {
303 configs[j] = EGLConfig(index);
304 } else {
305 return setError(EGL_BAD_CONFIG, EGL_FALSE);
306 }
307 }
308 configs += n;
309 config_size -= n;
310 }
311 *num_config += n;
312 res = EGL_TRUE;
313 }
314 }
315 }
316 return res;
317}
318
319EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
320 EGLint attribute, EGLint *value)
321{
322 clearError();
323
324 egl_display_t const* dp = 0;
325 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
326 if (!cnx) return EGL_FALSE;
327
328 if (attribute == EGL_CONFIG_ID) {
329 *value = dp->configs[intptr_t(config)].configId;
330 return EGL_TRUE;
331 }
332 return cnx->egl.eglGetConfigAttrib(
333 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
334 dp->configs[intptr_t(config)].config, attribute, value);
335}
336
337// ----------------------------------------------------------------------------
338// surfaces
339// ----------------------------------------------------------------------------
340
341EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
342 NativeWindowType window,
343 const EGLint *attrib_list)
344{
345 clearError();
346
347 egl_display_t const* dp = 0;
348 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
349 if (cnx) {
350 EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy;
351 EGLConfig iConfig = dp->configs[intptr_t(config)].config;
352 EGLint format;
353
Mathias Agopian81a63352011-07-29 17:55:48 -0700354 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000355 ALOGE("EGLNativeWindowType %p already connected to another API",
Mathias Agopian81a63352011-07-29 17:55:48 -0700356 window);
357 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
358 }
359
Mathias Agopian518ec112011-05-13 16:21:08 -0700360 // set the native window's buffers format to match this config
361 if (cnx->egl.eglGetConfigAttrib(iDpy,
362 iConfig, EGL_NATIVE_VISUAL_ID, &format)) {
363 if (format != 0) {
Jamie Gennisbee205f2011-07-01 13:12:07 -0700364 int err = native_window_set_buffers_format(window, format);
365 if (err != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000366 ALOGE("error setting native window pixel format: %s (%d)",
Jamie Gennisbee205f2011-07-01 13:12:07 -0700367 strerror(-err), err);
Mathias Agopian81a63352011-07-29 17:55:48 -0700368 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700369 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
370 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700371 }
372 }
373
Jamie Gennis59769462011-11-19 18:04:43 -0800374 // the EGL spec requires that a new EGLSurface default to swap interval
375 // 1, so explicitly set that on the window here.
376 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
377 anw->setSwapInterval(anw, 1);
378
Mathias Agopian518ec112011-05-13 16:21:08 -0700379 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
380 iDpy, iConfig, window, attrib_list);
381 if (surface != EGL_NO_SURFACE) {
382 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface,
383 dp->configs[intptr_t(config)].impl, cnx);
384 return s;
385 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700386
387 // EGLSurface creation failed
388 native_window_set_buffers_format(window, 0);
389 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700390 }
391 return EGL_NO_SURFACE;
392}
393
394EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
395 NativePixmapType pixmap,
396 const EGLint *attrib_list)
397{
398 clearError();
399
400 egl_display_t const* dp = 0;
401 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
402 if (cnx) {
403 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
404 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
405 dp->configs[intptr_t(config)].config, pixmap, attrib_list);
406 if (surface != EGL_NO_SURFACE) {
407 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
408 dp->configs[intptr_t(config)].impl, cnx);
409 return s;
410 }
411 }
412 return EGL_NO_SURFACE;
413}
414
415EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
416 const EGLint *attrib_list)
417{
418 clearError();
419
420 egl_display_t const* dp = 0;
421 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
422 if (cnx) {
423 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
424 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
425 dp->configs[intptr_t(config)].config, attrib_list);
426 if (surface != EGL_NO_SURFACE) {
427 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface,
428 dp->configs[intptr_t(config)].impl, cnx);
429 return s;
430 }
431 }
432 return EGL_NO_SURFACE;
433}
434
435EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
436{
437 clearError();
438
439 egl_display_t const * const dp = validate_display(dpy);
440 if (!dp) return EGL_FALSE;
441
Mathias Agopianf0480de2011-11-13 20:50:07 -0800442 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700443 if (!_s.get())
444 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700445
446 egl_surface_t * const s = get_surface(surface);
447 EGLBoolean result = s->cnx->egl.eglDestroySurface(
448 dp->disp[s->impl].dpy, s->surface);
449 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700450 _s.terminate();
451 }
452 return result;
453}
454
455EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
456 EGLint attribute, EGLint *value)
457{
458 clearError();
459
460 egl_display_t const * const dp = validate_display(dpy);
461 if (!dp) return EGL_FALSE;
462
Mathias Agopianf0480de2011-11-13 20:50:07 -0800463 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700464 if (!_s.get())
465 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700466
Mathias Agopian518ec112011-05-13 16:21:08 -0700467 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700468 EGLBoolean result(EGL_TRUE);
469 if (attribute == EGL_CONFIG_ID) {
470 // We need to remap EGL_CONFIG_IDs
471 *value = dp->configs[intptr_t(s->config)].configId;
472 } else {
473 result = s->cnx->egl.eglQuerySurface(
474 dp->disp[s->impl].dpy, s->surface, attribute, value);
475 }
476
477 return result;
478}
479
Jamie Gennise8696a42012-01-15 18:54:57 -0800480void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
481 clearError();
482
483 egl_display_t const * const dp = validate_display(dpy);
484 if (!dp) {
485 return;
486 }
487
488 SurfaceRef _s(dp, surface);
489 if (!_s.get()) {
490 setError(EGL_BAD_SURFACE, EGL_FALSE);
491 return;
492 }
493
494 int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
495
496 egl_surface_t const * const s = get_surface(surface);
497 native_window_set_buffers_timestamp(s->win.get(), timestamp);
498}
499
Mathias Agopian518ec112011-05-13 16:21:08 -0700500// ----------------------------------------------------------------------------
501// Contexts
502// ----------------------------------------------------------------------------
503
504EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
505 EGLContext share_list, const EGLint *attrib_list)
506{
507 clearError();
508
509 egl_display_t const* dp = 0;
510 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
511 if (cnx) {
512 if (share_list != EGL_NO_CONTEXT) {
513 egl_context_t* const c = get_context(share_list);
514 share_list = c->context;
515 }
516 EGLContext context = cnx->egl.eglCreateContext(
517 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
518 dp->configs[intptr_t(config)].config,
519 share_list, attrib_list);
520 if (context != EGL_NO_CONTEXT) {
521 // figure out if it's a GLESv1 or GLESv2
522 int version = 0;
523 if (attrib_list) {
524 while (*attrib_list != EGL_NONE) {
525 GLint attr = *attrib_list++;
526 GLint value = *attrib_list++;
527 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
528 if (value == 1) {
529 version = GLESv1_INDEX;
530 } else if (value == 2) {
531 version = GLESv2_INDEX;
532 }
533 }
534 };
535 }
536 egl_context_t* c = new egl_context_t(dpy, context, config,
537 dp->configs[intptr_t(config)].impl, cnx, version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800538#if EGL_TRACE
539 if (gEGLDebugLevel > 0)
540 GLTrace_eglCreateContext(version, c);
541#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700542 return c;
543 }
544 }
545 return EGL_NO_CONTEXT;
546}
547
548EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
549{
550 clearError();
551
552 egl_display_t const * const dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700553 if (!dp)
554 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700555
Mathias Agopianf0480de2011-11-13 20:50:07 -0800556 ContextRef _c(dp, ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700557 if (!_c.get())
558 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700559
Mathias Agopian518ec112011-05-13 16:21:08 -0700560 egl_context_t * const c = get_context(ctx);
561 EGLBoolean result = c->cnx->egl.eglDestroyContext(
562 dp->disp[c->impl].dpy, c->context);
563 if (result == EGL_TRUE) {
564 _c.terminate();
565 }
566 return result;
567}
568
569static void loseCurrent(egl_context_t * cur_c)
570{
571 if (cur_c) {
572 egl_surface_t * cur_r = get_surface(cur_c->read);
573 egl_surface_t * cur_d = get_surface(cur_c->draw);
574
575 // by construction, these are either 0 or valid (possibly terminated)
576 // it should be impossible for these to be invalid
577 ContextRef _cur_c(cur_c);
578 SurfaceRef _cur_r(cur_r);
579 SurfaceRef _cur_d(cur_d);
580
581 cur_c->read = NULL;
582 cur_c->draw = NULL;
583
584 _cur_c.release();
585 _cur_r.release();
586 _cur_d.release();
587 }
588}
589
590EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
591 EGLSurface read, EGLContext ctx)
592{
593 clearError();
594
595 egl_display_t const * const dp = get_display(dpy);
596 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
597
Mathias Agopian5b287a62011-05-16 18:58:55 -0700598 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
599 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
600 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700601 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
602 (draw != EGL_NO_SURFACE) ) {
603 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
604 }
605
606 // get a reference to the object passed in
Mathias Agopianf0480de2011-11-13 20:50:07 -0800607 ContextRef _c(dp, ctx);
608 SurfaceRef _d(dp, draw);
609 SurfaceRef _r(dp, read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700610
611 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700612 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700613 // EGL_NO_CONTEXT is valid
614 return EGL_FALSE;
615 }
616
617 // these are the underlying implementation's object
618 EGLContext impl_ctx = EGL_NO_CONTEXT;
619 EGLSurface impl_draw = EGL_NO_SURFACE;
620 EGLSurface impl_read = EGL_NO_SURFACE;
621
622 // these are our objects structs passed in
623 egl_context_t * c = NULL;
624 egl_surface_t const * d = NULL;
625 egl_surface_t const * r = NULL;
626
627 // these are the current objects structs
628 egl_context_t * cur_c = get_context(getContext());
629
630 if (ctx != EGL_NO_CONTEXT) {
631 c = get_context(ctx);
632 impl_ctx = c->context;
633 } else {
634 // no context given, use the implementation of the current context
635 if (cur_c == NULL) {
636 // no current context
637 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
638 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
639 return setError(EGL_BAD_MATCH, EGL_FALSE);
640 }
641 // not an error, there is just no current context.
642 return EGL_TRUE;
643 }
644 }
645
646 // retrieve the underlying implementation's draw EGLSurface
647 if (draw != EGL_NO_SURFACE) {
648 d = get_surface(draw);
649 // make sure the EGLContext and EGLSurface passed in are for
650 // the same driver
651 if (c && d->impl != c->impl)
652 return setError(EGL_BAD_MATCH, EGL_FALSE);
653 impl_draw = d->surface;
654 }
655
656 // retrieve the underlying implementation's read EGLSurface
657 if (read != EGL_NO_SURFACE) {
658 r = get_surface(read);
659 // make sure the EGLContext and EGLSurface passed in are for
660 // the same driver
661 if (c && r->impl != c->impl)
662 return setError(EGL_BAD_MATCH, EGL_FALSE);
663 impl_read = r->surface;
664 }
665
666 EGLBoolean result;
667
668 if (c) {
669 result = c->cnx->egl.eglMakeCurrent(
670 dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx);
671 } else {
672 result = cur_c->cnx->egl.eglMakeCurrent(
673 dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx);
674 }
675
676 if (result == EGL_TRUE) {
677
678 loseCurrent(cur_c);
679
680 if (ctx != EGL_NO_CONTEXT) {
681 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
682 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800683#if EGL_TRACE
684 if (gEGLDebugLevel > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800685 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800686#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700687 _c.acquire();
688 _r.acquire();
689 _d.acquire();
690 c->read = read;
691 c->draw = draw;
692 } else {
693 setGLHooksThreadSpecific(&gHooksNoContext);
694 egl_tls_t::setContext(EGL_NO_CONTEXT);
695 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700696 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000697 // this will ALOGE the error
Mathias Agopian5fecea72011-08-25 18:38:24 -0700698 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700699 }
700 return result;
701}
702
703
704EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
705 EGLint attribute, EGLint *value)
706{
707 clearError();
708
709 egl_display_t const * const dp = validate_display(dpy);
710 if (!dp) return EGL_FALSE;
711
Mathias Agopianf0480de2011-11-13 20:50:07 -0800712 ContextRef _c(dp, ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
714
Mathias Agopian518ec112011-05-13 16:21:08 -0700715 egl_context_t * const c = get_context(ctx);
716
717 EGLBoolean result(EGL_TRUE);
718 if (attribute == EGL_CONFIG_ID) {
719 *value = dp->configs[intptr_t(c->config)].configId;
720 } else {
721 // We need to remap EGL_CONFIG_IDs
722 result = c->cnx->egl.eglQueryContext(
723 dp->disp[c->impl].dpy, c->context, attribute, value);
724 }
725
726 return result;
727}
728
729EGLContext eglGetCurrentContext(void)
730{
731 // could be called before eglInitialize(), but we wouldn't have a context
732 // then, and this function would correctly return EGL_NO_CONTEXT.
733
734 clearError();
735
736 EGLContext ctx = getContext();
737 return ctx;
738}
739
740EGLSurface eglGetCurrentSurface(EGLint readdraw)
741{
742 // could be called before eglInitialize(), but we wouldn't have a context
743 // then, and this function would correctly return EGL_NO_SURFACE.
744
745 clearError();
746
747 EGLContext ctx = getContext();
748 if (ctx) {
749 egl_context_t const * const c = get_context(ctx);
750 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
751 switch (readdraw) {
752 case EGL_READ: return c->read;
753 case EGL_DRAW: return c->draw;
754 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
755 }
756 }
757 return EGL_NO_SURFACE;
758}
759
760EGLDisplay eglGetCurrentDisplay(void)
761{
762 // could be called before eglInitialize(), but we wouldn't have a context
763 // then, and this function would correctly return EGL_NO_DISPLAY.
764
765 clearError();
766
767 EGLContext ctx = getContext();
768 if (ctx) {
769 egl_context_t const * const c = get_context(ctx);
770 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
771 return c->dpy;
772 }
773 return EGL_NO_DISPLAY;
774}
775
776EGLBoolean eglWaitGL(void)
777{
778 // could be called before eglInitialize(), but we wouldn't have a context
779 // then, and this function would return GL_TRUE, which isn't wrong.
780
781 clearError();
782
783 EGLBoolean res = EGL_TRUE;
784 EGLContext ctx = getContext();
785 if (ctx) {
786 egl_context_t const * const c = get_context(ctx);
787 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
788 if (uint32_t(c->impl)>=2)
789 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
790 egl_connection_t* const cnx = &gEGLImpl[c->impl];
791 if (!cnx->dso)
792 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
793 res = cnx->egl.eglWaitGL();
794 }
795 return res;
796}
797
798EGLBoolean eglWaitNative(EGLint engine)
799{
800 // could be called before eglInitialize(), but we wouldn't have a context
801 // then, and this function would return GL_TRUE, which isn't wrong.
802
803 clearError();
804
805 EGLBoolean res = EGL_TRUE;
806 EGLContext ctx = getContext();
807 if (ctx) {
808 egl_context_t const * const c = get_context(ctx);
809 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
810 if (uint32_t(c->impl)>=2)
811 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
812 egl_connection_t* const cnx = &gEGLImpl[c->impl];
813 if (!cnx->dso)
814 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
815 res = cnx->egl.eglWaitNative(engine);
816 }
817 return res;
818}
819
820EGLint eglGetError(void)
821{
822 EGLint result = EGL_SUCCESS;
823 EGLint err;
824 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
825 err = EGL_SUCCESS;
826 egl_connection_t* const cnx = &gEGLImpl[i];
827 if (cnx->dso)
828 err = cnx->egl.eglGetError();
829 if (err!=EGL_SUCCESS && result==EGL_SUCCESS)
830 result = err;
831 }
832 err = egl_tls_t::getError();
833 if (result == EGL_SUCCESS)
834 result = err;
835 return result;
836}
837
838// Note: Similar implementations of these functions also exist in
839// gl2.cpp and gl.cpp, and are used by applications that call the
840// exported entry points directly.
841typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image);
842typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image);
843
844static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL;
845static PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL;
846
847static void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image)
848{
849 GLeglImageOES implImage =
850 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
851 glEGLImageTargetTexture2DOES_impl(target, implImage);
852}
853
854static void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image)
855{
856 GLeglImageOES implImage =
857 (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
858 glEGLImageTargetRenderbufferStorageOES_impl(target, implImage);
859}
860
861__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
862{
863 // eglGetProcAddress() could be the very first function called
864 // in which case we must make sure we've initialized ourselves, this
865 // happens the first time egl_get_display() is called.
866
867 clearError();
868
869 if (egl_init_drivers() == EGL_FALSE) {
870 setError(EGL_BAD_PARAMETER, NULL);
871 return NULL;
872 }
873
Jamie Gennisaca51c02011-11-03 17:42:43 -0700874 // The EGL_ANDROID_blob_cache extension should not be exposed to
875 // applications. It is used internally by the Android EGL layer.
Jamie Gennisc42fcf02011-11-09 15:35:34 -0800876 if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID")) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700877 return NULL;
878 }
879
Mathias Agopian518ec112011-05-13 16:21:08 -0700880 __eglMustCastToProperFunctionPointerType addr;
881 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
882 if (addr) return addr;
883
Jamie Gennisaca51c02011-11-03 17:42:43 -0700884
Mathias Agopian518ec112011-05-13 16:21:08 -0700885 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
886 pthread_mutex_lock(&sExtensionMapMutex);
887
888 /*
889 * Since eglGetProcAddress() is not associated to anything, it needs
890 * to return a function pointer that "works" regardless of what
891 * the current context is.
892 *
893 * For this reason, we return a "forwarder", a small stub that takes
894 * care of calling the function associated with the context
895 * currently bound.
896 *
897 * We first look for extensions we've already resolved, if we're seeing
898 * this extension for the first time, we go through all our
899 * implementations and call eglGetProcAddress() and record the
900 * result in the appropriate implementation hooks and return the
901 * address of the forwarder corresponding to that hook set.
902 *
903 */
904
905 const String8 name(procname);
906 addr = sGLExtentionMap.valueFor(name);
907 const int slot = sGLExtentionSlot;
908
Steve Blocke6f43dd2012-01-06 19:20:56 +0000909 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700910 "no more slots for eglGetProcAddress(\"%s\")",
911 procname);
912
Siva Velusamy0469dd62011-11-30 15:05:37 -0800913#if EGL_TRACE
914 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
915#endif
916
Mathias Agopian518ec112011-05-13 16:21:08 -0700917 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
918 bool found = false;
919 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
920 egl_connection_t* const cnx = &gEGLImpl[i];
921 if (cnx->dso && cnx->egl.eglGetProcAddress) {
922 found = true;
923 // Extensions are independent of the bound context
924 cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] =
925 cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] =
926#if EGL_TRACE
Siva Velusamy0469dd62011-11-30 15:05:37 -0800927 debugHooks->ext.extensions[slot] = gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700928#endif
929 cnx->egl.eglGetProcAddress(procname);
930 }
931 }
932 if (found) {
933 addr = gExtensionForwarders[slot];
934
935 if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) {
936 glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr;
937 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper;
938 }
939 if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) {
940 glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr;
941 addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper;
942 }
943
944 sGLExtentionMap.add(name, addr);
945 sGLExtentionSlot++;
946 }
947 }
948
949 pthread_mutex_unlock(&sExtensionMapMutex);
950 return addr;
951}
952
953EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
954{
Mathias Agopian518ec112011-05-13 16:21:08 -0700955 clearError();
956
957 egl_display_t const * const dp = validate_display(dpy);
958 if (!dp) return EGL_FALSE;
959
Mathias Agopianf0480de2011-11-13 20:50:07 -0800960 SurfaceRef _s(dp, draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700961 if (!_s.get())
962 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700963
Siva Velusamy0469dd62011-11-30 15:05:37 -0800964#if EGL_TRACE
965 if (gEGLDebugLevel > 0)
966 GLTrace_eglSwapBuffers(dpy, draw);
967#endif
968
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 egl_surface_t const * const s = get_surface(draw);
970 return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface);
971}
972
973EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
974 NativePixmapType target)
975{
976 clearError();
977
978 egl_display_t const * const dp = validate_display(dpy);
979 if (!dp) return EGL_FALSE;
980
Mathias Agopianf0480de2011-11-13 20:50:07 -0800981 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700982 if (!_s.get())
983 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700984
Mathias Agopian518ec112011-05-13 16:21:08 -0700985 egl_surface_t const * const s = get_surface(surface);
986 return s->cnx->egl.eglCopyBuffers(
987 dp->disp[s->impl].dpy, s->surface, target);
988}
989
990const char* eglQueryString(EGLDisplay dpy, EGLint name)
991{
992 clearError();
993
994 egl_display_t const * const dp = validate_display(dpy);
995 if (!dp) return (const char *) NULL;
996
997 switch (name) {
998 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800999 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001000 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001001 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001002 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001003 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001004 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001005 return dp->getClientApiString();
Mathias Agopianbc2d79e2011-11-29 17:55:46 -08001006 case EGL_VERSION_HW_ANDROID: {
1007 if (gEGLImpl[IMPL_HARDWARE].dso) {
1008 return dp->disp[IMPL_HARDWARE].queryString.version;
1009 }
1010 return dp->disp[IMPL_SOFTWARE].queryString.version;
1011 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001012 }
1013 return setError(EGL_BAD_PARAMETER, (const char *)0);
1014}
1015
1016
1017// ----------------------------------------------------------------------------
1018// EGL 1.1
1019// ----------------------------------------------------------------------------
1020
1021EGLBoolean eglSurfaceAttrib(
1022 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1023{
1024 clearError();
1025
1026 egl_display_t const * const dp = validate_display(dpy);
1027 if (!dp) return EGL_FALSE;
1028
Mathias Agopianf0480de2011-11-13 20:50:07 -08001029 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001030 if (!_s.get())
1031 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001032
Mathias Agopian518ec112011-05-13 16:21:08 -07001033 egl_surface_t const * const s = get_surface(surface);
1034 if (s->cnx->egl.eglSurfaceAttrib) {
1035 return s->cnx->egl.eglSurfaceAttrib(
1036 dp->disp[s->impl].dpy, s->surface, attribute, value);
1037 }
1038 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1039}
1040
1041EGLBoolean eglBindTexImage(
1042 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1043{
1044 clearError();
1045
1046 egl_display_t const * const dp = validate_display(dpy);
1047 if (!dp) return EGL_FALSE;
1048
Mathias Agopianf0480de2011-11-13 20:50:07 -08001049 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001050 if (!_s.get())
1051 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001052
Mathias Agopian518ec112011-05-13 16:21:08 -07001053 egl_surface_t const * const s = get_surface(surface);
1054 if (s->cnx->egl.eglBindTexImage) {
1055 return s->cnx->egl.eglBindTexImage(
1056 dp->disp[s->impl].dpy, s->surface, buffer);
1057 }
1058 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1059}
1060
1061EGLBoolean eglReleaseTexImage(
1062 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1063{
1064 clearError();
1065
1066 egl_display_t const * const dp = validate_display(dpy);
1067 if (!dp) return EGL_FALSE;
1068
Mathias Agopianf0480de2011-11-13 20:50:07 -08001069 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001070 if (!_s.get())
1071 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001072
Mathias Agopian518ec112011-05-13 16:21:08 -07001073 egl_surface_t const * const s = get_surface(surface);
1074 if (s->cnx->egl.eglReleaseTexImage) {
1075 return s->cnx->egl.eglReleaseTexImage(
1076 dp->disp[s->impl].dpy, s->surface, buffer);
1077 }
1078 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1079}
1080
1081EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1082{
1083 clearError();
1084
1085 egl_display_t const * const dp = validate_display(dpy);
1086 if (!dp) return EGL_FALSE;
1087
1088 EGLBoolean res = EGL_TRUE;
1089 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1090 egl_connection_t* const cnx = &gEGLImpl[i];
1091 if (cnx->dso) {
1092 if (cnx->egl.eglSwapInterval) {
1093 if (cnx->egl.eglSwapInterval(
1094 dp->disp[i].dpy, interval) == EGL_FALSE) {
1095 res = EGL_FALSE;
1096 }
1097 }
1098 }
1099 }
1100 return res;
1101}
1102
1103
1104// ----------------------------------------------------------------------------
1105// EGL 1.2
1106// ----------------------------------------------------------------------------
1107
1108EGLBoolean eglWaitClient(void)
1109{
1110 clearError();
1111
1112 // could be called before eglInitialize(), but we wouldn't have a context
1113 // then, and this function would return GL_TRUE, which isn't wrong.
1114 EGLBoolean res = EGL_TRUE;
1115 EGLContext ctx = getContext();
1116 if (ctx) {
1117 egl_context_t const * const c = get_context(ctx);
1118 if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1119 if (uint32_t(c->impl)>=2)
1120 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1121 egl_connection_t* const cnx = &gEGLImpl[c->impl];
1122 if (!cnx->dso)
1123 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1124 if (cnx->egl.eglWaitClient) {
1125 res = cnx->egl.eglWaitClient();
1126 } else {
1127 res = cnx->egl.eglWaitGL();
1128 }
1129 }
1130 return res;
1131}
1132
1133EGLBoolean eglBindAPI(EGLenum api)
1134{
1135 clearError();
1136
1137 if (egl_init_drivers() == EGL_FALSE) {
1138 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1139 }
1140
1141 // bind this API on all EGLs
1142 EGLBoolean res = EGL_TRUE;
1143 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1144 egl_connection_t* const cnx = &gEGLImpl[i];
1145 if (cnx->dso) {
1146 if (cnx->egl.eglBindAPI) {
1147 if (cnx->egl.eglBindAPI(api) == EGL_FALSE) {
1148 res = EGL_FALSE;
1149 }
1150 }
1151 }
1152 }
1153 return res;
1154}
1155
1156EGLenum eglQueryAPI(void)
1157{
1158 clearError();
1159
1160 if (egl_init_drivers() == EGL_FALSE) {
1161 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1162 }
1163
1164 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1165 egl_connection_t* const cnx = &gEGLImpl[i];
1166 if (cnx->dso) {
1167 if (cnx->egl.eglQueryAPI) {
1168 // the first one we find is okay, because they all
1169 // should be the same
1170 return cnx->egl.eglQueryAPI();
1171 }
1172 }
1173 }
1174 // or, it can only be OpenGL ES
1175 return EGL_OPENGL_ES_API;
1176}
1177
1178EGLBoolean eglReleaseThread(void)
1179{
1180 clearError();
1181
1182 // If there is context bound to the thread, release it
1183 loseCurrent(get_context(getContext()));
1184
1185 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1186 egl_connection_t* const cnx = &gEGLImpl[i];
1187 if (cnx->dso) {
1188 if (cnx->egl.eglReleaseThread) {
1189 cnx->egl.eglReleaseThread();
1190 }
1191 }
1192 }
1193 egl_tls_t::clearTLS();
Siva Velusamy0469dd62011-11-30 15:05:37 -08001194#if EGL_TRACE
1195 if (gEGLDebugLevel > 0)
1196 GLTrace_eglReleaseThread();
1197#endif
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 return EGL_TRUE;
1199}
1200
1201EGLSurface eglCreatePbufferFromClientBuffer(
1202 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1203 EGLConfig config, const EGLint *attrib_list)
1204{
1205 clearError();
1206
1207 egl_display_t const* dp = 0;
1208 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
1209 if (!cnx) return EGL_FALSE;
1210 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1211 return cnx->egl.eglCreatePbufferFromClientBuffer(
1212 dp->disp[ dp->configs[intptr_t(config)].impl ].dpy,
1213 buftype, buffer,
1214 dp->configs[intptr_t(config)].config, attrib_list);
1215 }
1216 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1217}
1218
1219// ----------------------------------------------------------------------------
1220// EGL_EGLEXT_VERSION 3
1221// ----------------------------------------------------------------------------
1222
1223EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1224 const EGLint *attrib_list)
1225{
1226 clearError();
1227
1228 egl_display_t const * const dp = validate_display(dpy);
1229 if (!dp) return EGL_FALSE;
1230
Mathias Agopianf0480de2011-11-13 20:50:07 -08001231 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001232 if (!_s.get())
1233 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001234
1235 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001236 if (s->cnx->egl.eglLockSurfaceKHR) {
1237 return s->cnx->egl.eglLockSurfaceKHR(
1238 dp->disp[s->impl].dpy, s->surface, attrib_list);
1239 }
1240 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1241}
1242
1243EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1244{
1245 clearError();
1246
1247 egl_display_t const * const dp = validate_display(dpy);
1248 if (!dp) return EGL_FALSE;
1249
Mathias Agopianf0480de2011-11-13 20:50:07 -08001250 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001251 if (!_s.get())
1252 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001253
1254 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001255 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1256 return s->cnx->egl.eglUnlockSurfaceKHR(
1257 dp->disp[s->impl].dpy, s->surface);
1258 }
1259 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1260}
1261
1262EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1263 EGLClientBuffer buffer, const EGLint *attrib_list)
1264{
1265 clearError();
1266
1267 egl_display_t const * const dp = validate_display(dpy);
1268 if (!dp) return EGL_NO_IMAGE_KHR;
1269
1270 if (ctx != EGL_NO_CONTEXT) {
Mathias Agopianf0480de2011-11-13 20:50:07 -08001271 ContextRef _c(dp, ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001272 if (!_c.get())
1273 return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
Mathias Agopian518ec112011-05-13 16:21:08 -07001274 egl_context_t * const c = get_context(ctx);
1275 // since we have an EGLContext, we know which implementation to use
1276 EGLImageKHR image = c->cnx->egl.eglCreateImageKHR(
1277 dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list);
1278 if (image == EGL_NO_IMAGE_KHR)
1279 return image;
1280
1281 egl_image_t* result = new egl_image_t(dpy, ctx);
1282 result->images[c->impl] = image;
1283 return (EGLImageKHR)result;
1284 } else {
1285 // EGL_NO_CONTEXT is a valid parameter
1286
1287 /* Since we don't have a way to know which implementation to call,
1288 * we're calling all of them. If at least one of the implementation
1289 * succeeded, this is a success.
1290 */
1291
1292 EGLint currentError = eglGetError();
1293
1294 EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS];
1295 bool success = false;
1296 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1297 egl_connection_t* const cnx = &gEGLImpl[i];
1298 implImages[i] = EGL_NO_IMAGE_KHR;
1299 if (cnx->dso) {
1300 if (cnx->egl.eglCreateImageKHR) {
1301 implImages[i] = cnx->egl.eglCreateImageKHR(
1302 dp->disp[i].dpy, ctx, target, buffer, attrib_list);
1303 if (implImages[i] != EGL_NO_IMAGE_KHR) {
1304 success = true;
1305 }
1306 }
1307 }
1308 }
1309
1310 if (!success) {
1311 // failure, if there was an error when we entered this function,
1312 // the error flag must not be updated.
1313 // Otherwise, the error is whatever happened in the implementation
1314 // that faulted.
1315 if (currentError != EGL_SUCCESS) {
1316 setError(currentError, EGL_NO_IMAGE_KHR);
1317 }
1318 return EGL_NO_IMAGE_KHR;
1319 } else {
1320 // In case of success, we need to clear all error flags
1321 // (especially those caused by the implementation that didn't
1322 // succeed). TODO: we could avoid this if we knew this was
1323 // a "full" success (all implementation succeeded).
1324 eglGetError();
1325 }
1326
1327 egl_image_t* result = new egl_image_t(dpy, ctx);
1328 memcpy(result->images, implImages, sizeof(implImages));
1329 return (EGLImageKHR)result;
1330 }
1331}
1332
1333EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1334{
1335 clearError();
1336
1337 egl_display_t const * const dp = validate_display(dpy);
1338 if (!dp) return EGL_FALSE;
1339
Mathias Agopianf0480de2011-11-13 20:50:07 -08001340 ImageRef _i(dp, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001341 if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1342
1343 egl_image_t* image = get_image(img);
1344 bool success = false;
1345 for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) {
1346 egl_connection_t* const cnx = &gEGLImpl[i];
1347 if (image->images[i] != EGL_NO_IMAGE_KHR) {
1348 if (cnx->dso) {
1349 if (cnx->egl.eglDestroyImageKHR) {
1350 if (cnx->egl.eglDestroyImageKHR(
1351 dp->disp[i].dpy, image->images[i])) {
1352 success = true;
1353 }
1354 }
1355 }
1356 }
1357 }
1358 if (!success)
1359 return EGL_FALSE;
1360
1361 _i.terminate();
1362
1363 return EGL_TRUE;
1364}
1365
1366// ----------------------------------------------------------------------------
1367// EGL_EGLEXT_VERSION 5
1368// ----------------------------------------------------------------------------
1369
1370
1371EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1372{
1373 clearError();
1374
1375 egl_display_t const * const dp = validate_display(dpy);
1376 if (!dp) return EGL_NO_SYNC_KHR;
1377
1378 EGLContext ctx = eglGetCurrentContext();
Mathias Agopianf0480de2011-11-13 20:50:07 -08001379 ContextRef _c(dp, ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001380 if (!_c.get())
1381 return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
1382
Mathias Agopian518ec112011-05-13 16:21:08 -07001383 egl_context_t * const c = get_context(ctx);
1384 EGLSyncKHR result = EGL_NO_SYNC_KHR;
1385 if (c->cnx->egl.eglCreateSyncKHR) {
1386 EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR(
1387 dp->disp[c->impl].dpy, type, attrib_list);
1388 if (sync == EGL_NO_SYNC_KHR)
1389 return sync;
1390 result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync);
1391 }
1392 return (EGLSyncKHR)result;
1393}
1394
1395EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1396{
1397 clearError();
1398
1399 egl_display_t const * const dp = validate_display(dpy);
1400 if (!dp) return EGL_FALSE;
1401
Mathias Agopianf0480de2011-11-13 20:50:07 -08001402 SyncRef _s(dp, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001403 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1404 egl_sync_t* syncObject = get_sync(sync);
1405
1406 EGLContext ctx = syncObject->context;
Mathias Agopianf0480de2011-11-13 20:50:07 -08001407 ContextRef _c(dp, ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001408 if (!_c.get())
1409 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001410
1411 EGLBoolean result = EGL_FALSE;
1412 egl_context_t * const c = get_context(ctx);
1413 if (c->cnx->egl.eglDestroySyncKHR) {
1414 result = c->cnx->egl.eglDestroySyncKHR(
1415 dp->disp[c->impl].dpy, syncObject->sync);
1416 if (result)
1417 _s.terminate();
1418 }
1419 return result;
1420}
1421
1422EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout)
1423{
1424 clearError();
1425
1426 egl_display_t const * const dp = validate_display(dpy);
1427 if (!dp) return EGL_FALSE;
1428
Mathias Agopianf0480de2011-11-13 20:50:07 -08001429 SyncRef _s(dp, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1431 egl_sync_t* syncObject = get_sync(sync);
1432
1433 EGLContext ctx = syncObject->context;
Mathias Agopianf0480de2011-11-13 20:50:07 -08001434 ContextRef _c(dp, ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001435 if (!_c.get())
1436 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001437
1438 egl_context_t * const c = get_context(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001439 if (c->cnx->egl.eglClientWaitSyncKHR) {
1440 return c->cnx->egl.eglClientWaitSyncKHR(
1441 dp->disp[c->impl].dpy, syncObject->sync, flags, timeout);
1442 }
1443
1444 return EGL_FALSE;
1445}
1446
1447EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value)
1448{
1449 clearError();
1450
1451 egl_display_t const * const dp = validate_display(dpy);
1452 if (!dp) return EGL_FALSE;
1453
Mathias Agopianf0480de2011-11-13 20:50:07 -08001454 SyncRef _s(dp, sync);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001455 if (!_s.get())
1456 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001457
Mathias Agopian5b287a62011-05-16 18:58:55 -07001458 egl_sync_t* syncObject = get_sync(sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001459 EGLContext ctx = syncObject->context;
Mathias Agopianf0480de2011-11-13 20:50:07 -08001460 ContextRef _c(dp, ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001461 if (!_c.get())
1462 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001463
1464 egl_context_t * const c = get_context(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001465 if (c->cnx->egl.eglGetSyncAttribKHR) {
1466 return c->cnx->egl.eglGetSyncAttribKHR(
1467 dp->disp[c->impl].dpy, syncObject->sync, attribute, value);
1468 }
1469
1470 return EGL_FALSE;
1471}
1472
1473// ----------------------------------------------------------------------------
1474// ANDROID extensions
1475// ----------------------------------------------------------------------------
1476
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001477/* ANDROID extensions entry-point go here */
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001478
1479// ----------------------------------------------------------------------------
1480// NVIDIA extensions
1481// ----------------------------------------------------------------------------
1482EGLuint64NV eglGetSystemTimeFrequencyNV()
1483{
1484 clearError();
1485
1486 if (egl_init_drivers() == EGL_FALSE) {
1487 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1488 }
1489
1490 EGLuint64NV ret = 0;
1491 egl_connection_t* const cnx = &gEGLImpl[IMPL_HARDWARE];
1492
1493 if (cnx->dso) {
1494 if (cnx->egl.eglGetSystemTimeFrequencyNV) {
1495 return cnx->egl.eglGetSystemTimeFrequencyNV();
1496 }
1497 }
1498
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001499 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001500}
1501
1502EGLuint64NV eglGetSystemTimeNV()
1503{
1504 clearError();
1505
1506 if (egl_init_drivers() == EGL_FALSE) {
1507 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1508 }
1509
1510 EGLuint64NV ret = 0;
1511 egl_connection_t* const cnx = &gEGLImpl[IMPL_HARDWARE];
1512
1513 if (cnx->dso) {
1514 if (cnx->egl.eglGetSystemTimeNV) {
1515 return cnx->egl.eglGetSystemTimeNV();
1516 }
1517 }
1518
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001519 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001520}