blob: de6c2c9773e3580c7b10d85aa317a1e9d2818d92 [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
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <hardware/gralloc.h>
24#include <system/window.h>
25
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
28#include <GLES/gl.h>
29#include <GLES/glext.h>
30
31#include <cutils/log.h>
32#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070033#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070034#include <cutils/properties.h>
35#include <cutils/memory.h>
36
37#include <utils/KeyedVector.h>
38#include <utils/SortedVector.h>
39#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080040#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070041
42#include "egl_impl.h"
43#include "egl_tls.h"
Siva Velusamy0469dd62011-11-30 15:05:37 -080044#include "glestrace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070045#include "hooks.h"
46
47#include "egl_display.h"
48#include "egl_impl.h"
49#include "egl_object.h"
50#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080051#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070052
53using namespace android;
54
55// ----------------------------------------------------------------------------
56
Mathias Agopianbc2d79e2011-11-29 17:55:46 -080057#define EGL_VERSION_HW_ANDROID 0x3143
58
Mathias Agopian518ec112011-05-13 16:21:08 -070059struct extention_map_t {
60 const char* name;
61 __eglMustCastToProperFunctionPointerType address;
62};
63
64static const extention_map_t sExtentionMap[] = {
65 { "eglLockSurfaceKHR",
66 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
67 { "eglUnlockSurfaceKHR",
68 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
69 { "eglCreateImageKHR",
70 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
71 { "eglDestroyImageKHR",
72 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -070073 { "eglGetRenderBufferANDROID",
74 (__eglMustCastToProperFunctionPointerType)&eglGetRenderBufferANDROID },
Jonas Yang1c3d72a2011-08-26 20:04:39 +080075 { "eglGetSystemTimeFrequencyNV",
76 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
77 { "eglGetSystemTimeNV",
78 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopian518ec112011-05-13 16:21:08 -070079};
80
81// accesses protected by sExtensionMapMutex
82static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
83static int sGLExtentionSlot = 0;
84static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
85
86static void(*findProcAddress(const char* name,
87 const extention_map_t* map, size_t n))() {
88 for (uint32_t i=0 ; i<n ; i++) {
89 if (!strcmp(name, map[i].name)) {
90 return map[i].address;
91 }
92 }
93 return NULL;
94}
95
96// ----------------------------------------------------------------------------
97
Mathias Agopian518ec112011-05-13 16:21:08 -070098namespace android {
99extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
100extern EGLBoolean egl_init_drivers();
101extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
102extern int gEGLDebugLevel;
103extern gl_hooks_t gHooksTrace;
Mathias Agopian518ec112011-05-13 16:21:08 -0700104} // namespace android;
105
106// ----------------------------------------------------------------------------
107
108static inline void clearError() { egl_tls_t::clearError(); }
109static inline EGLContext getContext() { return egl_tls_t::getContext(); }
110
111// ----------------------------------------------------------------------------
112
113EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
114{
115 clearError();
116
117 uint32_t index = uint32_t(display);
118 if (index >= NUM_DISPLAYS) {
119 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
120 }
121
122 if (egl_init_drivers() == EGL_FALSE) {
123 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
124 }
125
126 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
127 return dpy;
128}
129
130// ----------------------------------------------------------------------------
131// Initialization
132// ----------------------------------------------------------------------------
133
134EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
135{
136 clearError();
137
Jesse Hallb29e5e82012-04-04 16:53:42 -0700138 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700139 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
140
141 EGLBoolean res = dp->initialize(major, minor);
142
143 return res;
144}
145
146EGLBoolean eglTerminate(EGLDisplay dpy)
147{
148 // NOTE: don't unload the drivers b/c some APIs can be called
149 // after eglTerminate() has been called. eglTerminate() only
150 // terminates an EGLDisplay, not a EGL itself.
151
152 clearError();
153
Jesse Hallb29e5e82012-04-04 16:53:42 -0700154 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700155 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
156
157 EGLBoolean res = dp->terminate();
158
159 return res;
160}
161
162// ----------------------------------------------------------------------------
163// configuration
164// ----------------------------------------------------------------------------
165
166EGLBoolean eglGetConfigs( EGLDisplay dpy,
167 EGLConfig *configs,
168 EGLint config_size, EGLint *num_config)
169{
170 clearError();
171
Jesse Hallb29e5e82012-04-04 16:53:42 -0700172 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700173 if (!dp) return EGL_FALSE;
174
Mathias Agopian7773c432012-02-13 20:06:08 -0800175 if (num_config==0) {
176 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700177 }
178
Mathias Agopian7773c432012-02-13 20:06:08 -0800179 EGLBoolean res = EGL_FALSE;
180 *num_config = 0;
181
182 egl_connection_t* const cnx = &gEGLImpl;
183 if (cnx->dso) {
184 res = cnx->egl.eglGetConfigs(
185 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700186 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800187
188 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700189}
190
191EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
192 EGLConfig *configs, EGLint config_size,
193 EGLint *num_config)
194{
195 clearError();
196
Jesse Hallb29e5e82012-04-04 16:53:42 -0700197 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700198 if (!dp) return EGL_FALSE;
199
200 if (num_config==0) {
201 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
202 }
203
Mathias Agopian518ec112011-05-13 16:21:08 -0700204 EGLBoolean res = EGL_FALSE;
205 *num_config = 0;
206
Mathias Agopianada798b2012-02-13 17:09:30 -0800207 egl_connection_t* const cnx = &gEGLImpl;
208 if (cnx->dso) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800209 res = cnx->egl.eglChooseConfig(
210 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700211 }
212 return res;
213}
214
215EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
216 EGLint attribute, EGLint *value)
217{
218 clearError();
219
Jesse Hallb29e5e82012-04-04 16:53:42 -0700220 egl_connection_t* cnx = NULL;
221 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
222 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700223
Mathias Agopian518ec112011-05-13 16:21:08 -0700224 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800225 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700226}
227
228// ----------------------------------------------------------------------------
229// surfaces
230// ----------------------------------------------------------------------------
231
232EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
233 NativeWindowType window,
234 const EGLint *attrib_list)
235{
236 clearError();
237
Jesse Hallb29e5e82012-04-04 16:53:42 -0700238 egl_connection_t* cnx = NULL;
239 egl_display_ptr dp = validate_display_connection(dpy, cnx);
240 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800241 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700242 EGLint format;
243
Mathias Agopian81a63352011-07-29 17:55:48 -0700244 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000245 ALOGE("EGLNativeWindowType %p already connected to another API",
Mathias Agopian81a63352011-07-29 17:55:48 -0700246 window);
247 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
248 }
249
Mathias Agopian518ec112011-05-13 16:21:08 -0700250 // set the native window's buffers format to match this config
251 if (cnx->egl.eglGetConfigAttrib(iDpy,
Mathias Agopian7773c432012-02-13 20:06:08 -0800252 config, EGL_NATIVE_VISUAL_ID, &format)) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700253 if (format != 0) {
Jamie Gennisbee205f2011-07-01 13:12:07 -0700254 int err = native_window_set_buffers_format(window, format);
255 if (err != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000256 ALOGE("error setting native window pixel format: %s (%d)",
Jamie Gennisbee205f2011-07-01 13:12:07 -0700257 strerror(-err), err);
Mathias Agopian81a63352011-07-29 17:55:48 -0700258 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700259 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
260 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700261 }
262 }
263
Jamie Gennis59769462011-11-19 18:04:43 -0800264 // the EGL spec requires that a new EGLSurface default to swap interval
265 // 1, so explicitly set that on the window here.
266 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
267 anw->setSwapInterval(anw, 1);
268
Mathias Agopian518ec112011-05-13 16:21:08 -0700269 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800270 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700271 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700272 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
273 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700274 return s;
275 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700276
277 // EGLSurface creation failed
278 native_window_set_buffers_format(window, 0);
279 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700280 }
281 return EGL_NO_SURFACE;
282}
283
284EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
285 NativePixmapType pixmap,
286 const EGLint *attrib_list)
287{
288 clearError();
289
Jesse Hallb29e5e82012-04-04 16:53:42 -0700290 egl_connection_t* cnx = NULL;
291 egl_display_ptr dp = validate_display_connection(dpy, cnx);
292 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700293 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800294 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700295 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700296 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
297 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700298 return s;
299 }
300 }
301 return EGL_NO_SURFACE;
302}
303
304EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
305 const EGLint *attrib_list)
306{
307 clearError();
308
Jesse Hallb29e5e82012-04-04 16:53:42 -0700309 egl_connection_t* cnx = NULL;
310 egl_display_ptr dp = validate_display_connection(dpy, cnx);
311 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700312 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800313 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700314 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700315 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
316 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700317 return s;
318 }
319 }
320 return EGL_NO_SURFACE;
321}
322
323EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
324{
325 clearError();
326
Jesse Hallb29e5e82012-04-04 16:53:42 -0700327 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700328 if (!dp) return EGL_FALSE;
329
Jesse Hallb29e5e82012-04-04 16:53:42 -0700330 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700331 if (!_s.get())
332 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700333
334 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800335 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700336 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700337 _s.terminate();
338 }
339 return result;
340}
341
342EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
343 EGLint attribute, EGLint *value)
344{
345 clearError();
346
Jesse Hallb29e5e82012-04-04 16:53:42 -0700347 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700348 if (!dp) return EGL_FALSE;
349
Jesse Hallb29e5e82012-04-04 16:53:42 -0700350 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700351 if (!_s.get())
352 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700353
Mathias Agopian518ec112011-05-13 16:21:08 -0700354 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800355 return s->cnx->egl.eglQuerySurface(
356 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700357}
358
Jamie Gennise8696a42012-01-15 18:54:57 -0800359void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800360 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800361 clearError();
362
Jesse Hallb29e5e82012-04-04 16:53:42 -0700363 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800364 if (!dp) {
365 return;
366 }
367
Jesse Hallb29e5e82012-04-04 16:53:42 -0700368 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800369 if (!_s.get()) {
370 setError(EGL_BAD_SURFACE, EGL_FALSE);
371 return;
372 }
373
374 int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
375
376 egl_surface_t const * const s = get_surface(surface);
377 native_window_set_buffers_timestamp(s->win.get(), timestamp);
378}
379
Mathias Agopian518ec112011-05-13 16:21:08 -0700380// ----------------------------------------------------------------------------
381// Contexts
382// ----------------------------------------------------------------------------
383
384EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
385 EGLContext share_list, const EGLint *attrib_list)
386{
387 clearError();
388
Jesse Hallb29e5e82012-04-04 16:53:42 -0700389 egl_connection_t* cnx = NULL;
390 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
391 if (dpy) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700392 if (share_list != EGL_NO_CONTEXT) {
393 egl_context_t* const c = get_context(share_list);
394 share_list = c->context;
395 }
396 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800397 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700398 if (context != EGL_NO_CONTEXT) {
399 // figure out if it's a GLESv1 or GLESv2
400 int version = 0;
401 if (attrib_list) {
402 while (*attrib_list != EGL_NONE) {
403 GLint attr = *attrib_list++;
404 GLint value = *attrib_list++;
405 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
406 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800407 version = egl_connection_t::GLESv1_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700408 } else if (value == 2) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800409 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700410 }
411 }
412 };
413 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700414 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
415 version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800416#if EGL_TRACE
417 if (gEGLDebugLevel > 0)
418 GLTrace_eglCreateContext(version, c);
419#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700420 return c;
421 }
422 }
423 return EGL_NO_CONTEXT;
424}
425
426EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
427{
428 clearError();
429
Jesse Hallb29e5e82012-04-04 16:53:42 -0700430 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700431 if (!dp)
432 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700433
Jesse Hallb29e5e82012-04-04 16:53:42 -0700434 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700435 if (!_c.get())
436 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700437
Mathias Agopian518ec112011-05-13 16:21:08 -0700438 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800439 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700440 if (result == EGL_TRUE) {
441 _c.terminate();
442 }
443 return result;
444}
445
Mathias Agopian518ec112011-05-13 16:21:08 -0700446EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
447 EGLSurface read, EGLContext ctx)
448{
449 clearError();
450
Jesse Hallb29e5e82012-04-04 16:53:42 -0700451 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700452 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
453
Mathias Agopian5b287a62011-05-16 18:58:55 -0700454 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
455 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
456 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700457 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
458 (draw != EGL_NO_SURFACE) ) {
459 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
460 }
461
462 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700463 ContextRef _c(dp.get(), ctx);
464 SurfaceRef _d(dp.get(), draw);
465 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700466
467 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700468 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700469 // EGL_NO_CONTEXT is valid
470 return EGL_FALSE;
471 }
472
473 // these are the underlying implementation's object
474 EGLContext impl_ctx = EGL_NO_CONTEXT;
475 EGLSurface impl_draw = EGL_NO_SURFACE;
476 EGLSurface impl_read = EGL_NO_SURFACE;
477
478 // these are our objects structs passed in
479 egl_context_t * c = NULL;
480 egl_surface_t const * d = NULL;
481 egl_surface_t const * r = NULL;
482
483 // these are the current objects structs
484 egl_context_t * cur_c = get_context(getContext());
485
486 if (ctx != EGL_NO_CONTEXT) {
487 c = get_context(ctx);
488 impl_ctx = c->context;
489 } else {
490 // no context given, use the implementation of the current context
491 if (cur_c == NULL) {
492 // no current context
493 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
494 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
495 return setError(EGL_BAD_MATCH, EGL_FALSE);
496 }
497 // not an error, there is just no current context.
498 return EGL_TRUE;
499 }
500 }
501
502 // retrieve the underlying implementation's draw EGLSurface
503 if (draw != EGL_NO_SURFACE) {
504 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700505 impl_draw = d->surface;
506 }
507
508 // retrieve the underlying implementation's read EGLSurface
509 if (read != EGL_NO_SURFACE) {
510 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700511 impl_read = r->surface;
512 }
513
Mathias Agopian518ec112011-05-13 16:21:08 -0700514
Jesse Hallb29e5e82012-04-04 16:53:42 -0700515 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800516 draw, read, ctx,
517 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700518
519 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800520 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700521 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
522 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800523#if EGL_TRACE
524 if (gEGLDebugLevel > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800525 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800526#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700527 _c.acquire();
528 _r.acquire();
529 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700530 } else {
531 setGLHooksThreadSpecific(&gHooksNoContext);
532 egl_tls_t::setContext(EGL_NO_CONTEXT);
533 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700534 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000535 // this will ALOGE the error
Mathias Agopian5fecea72011-08-25 18:38:24 -0700536 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700537 }
538 return result;
539}
540
541
542EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
543 EGLint attribute, EGLint *value)
544{
545 clearError();
546
Jesse Hallb29e5e82012-04-04 16:53:42 -0700547 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700548 if (!dp) return EGL_FALSE;
549
Jesse Hallb29e5e82012-04-04 16:53:42 -0700550 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700551 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
552
Mathias Agopian518ec112011-05-13 16:21:08 -0700553 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800554 return c->cnx->egl.eglQueryContext(
555 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700556
Mathias Agopian518ec112011-05-13 16:21:08 -0700557}
558
559EGLContext eglGetCurrentContext(void)
560{
561 // could be called before eglInitialize(), but we wouldn't have a context
562 // then, and this function would correctly return EGL_NO_CONTEXT.
563
564 clearError();
565
566 EGLContext ctx = getContext();
567 return ctx;
568}
569
570EGLSurface eglGetCurrentSurface(EGLint readdraw)
571{
572 // could be called before eglInitialize(), but we wouldn't have a context
573 // then, and this function would correctly return EGL_NO_SURFACE.
574
575 clearError();
576
577 EGLContext ctx = getContext();
578 if (ctx) {
579 egl_context_t const * const c = get_context(ctx);
580 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
581 switch (readdraw) {
582 case EGL_READ: return c->read;
583 case EGL_DRAW: return c->draw;
584 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
585 }
586 }
587 return EGL_NO_SURFACE;
588}
589
590EGLDisplay eglGetCurrentDisplay(void)
591{
592 // could be called before eglInitialize(), but we wouldn't have a context
593 // then, and this function would correctly return EGL_NO_DISPLAY.
594
595 clearError();
596
597 EGLContext ctx = getContext();
598 if (ctx) {
599 egl_context_t const * const c = get_context(ctx);
600 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
601 return c->dpy;
602 }
603 return EGL_NO_DISPLAY;
604}
605
606EGLBoolean eglWaitGL(void)
607{
Mathias Agopian518ec112011-05-13 16:21:08 -0700608 clearError();
609
Mathias Agopianada798b2012-02-13 17:09:30 -0800610 egl_connection_t* const cnx = &gEGLImpl;
611 if (!cnx->dso)
612 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
613
614 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700615}
616
617EGLBoolean eglWaitNative(EGLint engine)
618{
Mathias Agopian518ec112011-05-13 16:21:08 -0700619 clearError();
620
Mathias Agopianada798b2012-02-13 17:09:30 -0800621 egl_connection_t* const cnx = &gEGLImpl;
622 if (!cnx->dso)
623 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
624
625 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700626}
627
628EGLint eglGetError(void)
629{
Mathias Agopianada798b2012-02-13 17:09:30 -0800630 EGLint err = EGL_SUCCESS;
631 egl_connection_t* const cnx = &gEGLImpl;
632 if (cnx->dso) {
633 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700634 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800635 if (err == EGL_SUCCESS) {
636 err = egl_tls_t::getError();
637 }
638 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700639}
640
Mathias Agopian518ec112011-05-13 16:21:08 -0700641__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
642{
643 // eglGetProcAddress() could be the very first function called
644 // in which case we must make sure we've initialized ourselves, this
645 // happens the first time egl_get_display() is called.
646
647 clearError();
648
649 if (egl_init_drivers() == EGL_FALSE) {
650 setError(EGL_BAD_PARAMETER, NULL);
651 return NULL;
652 }
653
Jesse Hall25838592012-04-05 15:53:28 -0700654 // These extensions should not be exposed to applications. They're used
655 // internally by the Android EGL layer.
656 if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID") ||
657 !strcmp(procname, "eglHibernateProcessIMG") ||
658 !strcmp(procname, "eglAwakenProcessIMG")) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700659 return NULL;
660 }
661
Mathias Agopian518ec112011-05-13 16:21:08 -0700662 __eglMustCastToProperFunctionPointerType addr;
663 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
664 if (addr) return addr;
665
Jamie Gennisaca51c02011-11-03 17:42:43 -0700666
Mathias Agopian518ec112011-05-13 16:21:08 -0700667 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
668 pthread_mutex_lock(&sExtensionMapMutex);
669
670 /*
671 * Since eglGetProcAddress() is not associated to anything, it needs
672 * to return a function pointer that "works" regardless of what
673 * the current context is.
674 *
675 * For this reason, we return a "forwarder", a small stub that takes
676 * care of calling the function associated with the context
677 * currently bound.
678 *
679 * We first look for extensions we've already resolved, if we're seeing
680 * this extension for the first time, we go through all our
681 * implementations and call eglGetProcAddress() and record the
682 * result in the appropriate implementation hooks and return the
683 * address of the forwarder corresponding to that hook set.
684 *
685 */
686
687 const String8 name(procname);
688 addr = sGLExtentionMap.valueFor(name);
689 const int slot = sGLExtentionSlot;
690
Steve Blocke6f43dd2012-01-06 19:20:56 +0000691 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700692 "no more slots for eglGetProcAddress(\"%s\")",
693 procname);
694
Siva Velusamy0469dd62011-11-30 15:05:37 -0800695#if EGL_TRACE
696 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
697#endif
698
Mathias Agopian518ec112011-05-13 16:21:08 -0700699 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
700 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800701
702 egl_connection_t* const cnx = &gEGLImpl;
703 if (cnx->dso && cnx->egl.eglGetProcAddress) {
704 found = true;
705 // Extensions are independent of the bound context
Mathias Agopian7773c432012-02-13 20:06:08 -0800706 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
707 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700708#if EGL_TRACE
Mathias Agopianada798b2012-02-13 17:09:30 -0800709 debugHooks->ext.extensions[slot] =
710 gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700711#endif
Mathias Agopianada798b2012-02-13 17:09:30 -0800712 cnx->egl.eglGetProcAddress(procname);
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800714
Mathias Agopian518ec112011-05-13 16:21:08 -0700715 if (found) {
716 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700717 sGLExtentionMap.add(name, addr);
718 sGLExtentionSlot++;
719 }
720 }
721
722 pthread_mutex_unlock(&sExtensionMapMutex);
723 return addr;
724}
725
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700726class FrameCompletionThread : public Thread {
727public:
728
729 static void queueSync(EGLSyncKHR sync) {
730 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
731 static bool running = false;
732 if (!running) {
733 thread->run("GPUFrameCompletion");
734 running = true;
735 }
736 {
737 Mutex::Autolock lock(thread->mMutex);
738 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
739 thread->mFramesQueued).string());
740 thread->mQueue.push_back(sync);
741 thread->mCondition.signal();
742 thread->mFramesQueued++;
743 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
744 }
745 }
746
747private:
748 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
749
750 virtual bool threadLoop() {
751 EGLSyncKHR sync;
752 uint32_t frameNum;
753 {
754 Mutex::Autolock lock(mMutex);
755 while (mQueue.isEmpty()) {
756 mCondition.wait(mMutex);
757 }
758 sync = mQueue[0];
759 frameNum = mFramesCompleted;
760 }
761 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
762 {
763 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
764 frameNum).string());
765 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
766 if (result == EGL_FALSE) {
767 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
768 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
769 ALOGE("FrameCompletion: timeout waiting for fence");
770 }
771 eglDestroySyncKHR(dpy, sync);
772 }
773 {
774 Mutex::Autolock lock(mMutex);
775 mQueue.removeAt(0);
776 mFramesCompleted++;
777 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
778 }
779 return true;
780 }
781
782 uint32_t mFramesQueued;
783 uint32_t mFramesCompleted;
784 Vector<EGLSyncKHR> mQueue;
785 Condition mCondition;
786 Mutex mMutex;
787};
788
Mathias Agopian518ec112011-05-13 16:21:08 -0700789EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
790{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800791 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700792 clearError();
793
Jesse Hallb29e5e82012-04-04 16:53:42 -0700794 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700795 if (!dp) return EGL_FALSE;
796
Jesse Hallb29e5e82012-04-04 16:53:42 -0700797 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700798 if (!_s.get())
799 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700800
Siva Velusamy0469dd62011-11-30 15:05:37 -0800801#if EGL_TRACE
802 if (gEGLDebugLevel > 0)
803 GLTrace_eglSwapBuffers(dpy, draw);
804#endif
805
Mathias Agopian518ec112011-05-13 16:21:08 -0700806 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -0700807
808 if (CC_UNLIKELY(dp->finishOnSwap)) {
809 uint32_t pixel;
810 egl_context_t * const c = get_context( egl_tls_t::getContext() );
811 if (c) {
812 // glReadPixels() ensures that the frame is complete
813 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
814 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
815 }
816 }
817
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700818 EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
819
820 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
821 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
822 {
823 sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
824 }
825 if (sync != EGL_NO_SYNC_KHR) {
826 FrameCompletionThread::queueSync(sync);
827 }
828 }
829
830 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -0700831}
832
833EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
834 NativePixmapType target)
835{
836 clearError();
837
Jesse Hallb29e5e82012-04-04 16:53:42 -0700838 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700839 if (!dp) return EGL_FALSE;
840
Jesse Hallb29e5e82012-04-04 16:53:42 -0700841 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700842 if (!_s.get())
843 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700844
Mathias Agopian518ec112011-05-13 16:21:08 -0700845 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800846 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -0700847}
848
849const char* eglQueryString(EGLDisplay dpy, EGLint name)
850{
851 clearError();
852
Jesse Hallb29e5e82012-04-04 16:53:42 -0700853 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700854 if (!dp) return (const char *) NULL;
855
856 switch (name) {
857 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800858 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700859 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800860 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700861 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800862 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700863 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800864 return dp->getClientApiString();
Mathias Agopianada798b2012-02-13 17:09:30 -0800865 case EGL_VERSION_HW_ANDROID:
866 return dp->disp.queryString.version;
Mathias Agopian518ec112011-05-13 16:21:08 -0700867 }
868 return setError(EGL_BAD_PARAMETER, (const char *)0);
869}
870
871
872// ----------------------------------------------------------------------------
873// EGL 1.1
874// ----------------------------------------------------------------------------
875
876EGLBoolean eglSurfaceAttrib(
877 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
878{
879 clearError();
880
Jesse Hallb29e5e82012-04-04 16:53:42 -0700881 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700882 if (!dp) return EGL_FALSE;
883
Jesse Hallb29e5e82012-04-04 16:53:42 -0700884 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700885 if (!_s.get())
886 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700887
Mathias Agopian518ec112011-05-13 16:21:08 -0700888 egl_surface_t const * const s = get_surface(surface);
889 if (s->cnx->egl.eglSurfaceAttrib) {
890 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -0800891 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700892 }
893 return setError(EGL_BAD_SURFACE, EGL_FALSE);
894}
895
896EGLBoolean eglBindTexImage(
897 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
898{
899 clearError();
900
Jesse Hallb29e5e82012-04-04 16:53:42 -0700901 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700902 if (!dp) return EGL_FALSE;
903
Jesse Hallb29e5e82012-04-04 16:53:42 -0700904 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700905 if (!_s.get())
906 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700907
Mathias Agopian518ec112011-05-13 16:21:08 -0700908 egl_surface_t const * const s = get_surface(surface);
909 if (s->cnx->egl.eglBindTexImage) {
910 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -0800911 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -0700912 }
913 return setError(EGL_BAD_SURFACE, EGL_FALSE);
914}
915
916EGLBoolean eglReleaseTexImage(
917 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
918{
919 clearError();
920
Jesse Hallb29e5e82012-04-04 16:53:42 -0700921 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700922 if (!dp) return EGL_FALSE;
923
Jesse Hallb29e5e82012-04-04 16:53:42 -0700924 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700925 if (!_s.get())
926 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700927
Mathias Agopian518ec112011-05-13 16:21:08 -0700928 egl_surface_t const * const s = get_surface(surface);
929 if (s->cnx->egl.eglReleaseTexImage) {
930 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -0800931 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -0700932 }
933 return setError(EGL_BAD_SURFACE, EGL_FALSE);
934}
935
936EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
937{
938 clearError();
939
Jesse Hallb29e5e82012-04-04 16:53:42 -0700940 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700941 if (!dp) return EGL_FALSE;
942
943 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800944 egl_connection_t* const cnx = &gEGLImpl;
945 if (cnx->dso && cnx->egl.eglSwapInterval) {
946 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -0700947 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800948
Mathias Agopian518ec112011-05-13 16:21:08 -0700949 return res;
950}
951
952
953// ----------------------------------------------------------------------------
954// EGL 1.2
955// ----------------------------------------------------------------------------
956
957EGLBoolean eglWaitClient(void)
958{
959 clearError();
960
Mathias Agopianada798b2012-02-13 17:09:30 -0800961 egl_connection_t* const cnx = &gEGLImpl;
962 if (!cnx->dso)
963 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
964
965 EGLBoolean res;
966 if (cnx->egl.eglWaitClient) {
967 res = cnx->egl.eglWaitClient();
968 } else {
969 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700970 }
971 return res;
972}
973
974EGLBoolean eglBindAPI(EGLenum api)
975{
976 clearError();
977
978 if (egl_init_drivers() == EGL_FALSE) {
979 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
980 }
981
982 // bind this API on all EGLs
983 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800984 egl_connection_t* const cnx = &gEGLImpl;
985 if (cnx->dso && cnx->egl.eglBindAPI) {
986 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -0700987 }
988 return res;
989}
990
991EGLenum eglQueryAPI(void)
992{
993 clearError();
994
995 if (egl_init_drivers() == EGL_FALSE) {
996 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
997 }
998
Mathias Agopianada798b2012-02-13 17:09:30 -0800999 egl_connection_t* const cnx = &gEGLImpl;
1000 if (cnx->dso && cnx->egl.eglQueryAPI) {
1001 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001002 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001003
Mathias Agopian518ec112011-05-13 16:21:08 -07001004 // or, it can only be OpenGL ES
1005 return EGL_OPENGL_ES_API;
1006}
1007
1008EGLBoolean eglReleaseThread(void)
1009{
1010 clearError();
1011
1012 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001013 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001014
Mathias Agopianada798b2012-02-13 17:09:30 -08001015 egl_connection_t* const cnx = &gEGLImpl;
1016 if (cnx->dso && cnx->egl.eglReleaseThread) {
1017 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001018 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001019
Mathias Agopian518ec112011-05-13 16:21:08 -07001020 egl_tls_t::clearTLS();
Siva Velusamy0469dd62011-11-30 15:05:37 -08001021#if EGL_TRACE
1022 if (gEGLDebugLevel > 0)
1023 GLTrace_eglReleaseThread();
1024#endif
Mathias Agopian518ec112011-05-13 16:21:08 -07001025 return EGL_TRUE;
1026}
1027
1028EGLSurface eglCreatePbufferFromClientBuffer(
1029 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1030 EGLConfig config, const EGLint *attrib_list)
1031{
1032 clearError();
1033
Jesse Hallb29e5e82012-04-04 16:53:42 -07001034 egl_connection_t* cnx = NULL;
1035 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1036 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001037 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1038 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001039 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001040 }
1041 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1042}
1043
1044// ----------------------------------------------------------------------------
1045// EGL_EGLEXT_VERSION 3
1046// ----------------------------------------------------------------------------
1047
1048EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1049 const EGLint *attrib_list)
1050{
1051 clearError();
1052
Jesse Hallb29e5e82012-04-04 16:53:42 -07001053 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001054 if (!dp) return EGL_FALSE;
1055
Jesse Hallb29e5e82012-04-04 16:53:42 -07001056 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001057 if (!_s.get())
1058 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001059
1060 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001061 if (s->cnx->egl.eglLockSurfaceKHR) {
1062 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001063 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001064 }
1065 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1066}
1067
1068EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1069{
1070 clearError();
1071
Jesse Hallb29e5e82012-04-04 16:53:42 -07001072 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001073 if (!dp) return EGL_FALSE;
1074
Jesse Hallb29e5e82012-04-04 16:53:42 -07001075 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001076 if (!_s.get())
1077 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001078
1079 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001080 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001081 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001082 }
1083 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1084}
1085
1086EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1087 EGLClientBuffer buffer, const EGLint *attrib_list)
1088{
1089 clearError();
1090
Jesse Hallb29e5e82012-04-04 16:53:42 -07001091 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001092 if (!dp) return EGL_NO_IMAGE_KHR;
1093
Jesse Hallb29e5e82012-04-04 16:53:42 -07001094 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001095 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001096
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001097 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1098 egl_connection_t* const cnx = &gEGLImpl;
1099 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1100 result = cnx->egl.eglCreateImageKHR(
1101 dp->disp.dpy,
1102 c ? c->context : EGL_NO_CONTEXT,
1103 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001104 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001105 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001106}
1107
1108EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1109{
1110 clearError();
1111
Jesse Hallb29e5e82012-04-04 16:53:42 -07001112 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001113 if (!dp) return EGL_FALSE;
1114
Mathias Agopianada798b2012-02-13 17:09:30 -08001115 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001116 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1117 cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001118 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001119 return EGL_TRUE;
1120}
1121
1122// ----------------------------------------------------------------------------
1123// EGL_EGLEXT_VERSION 5
1124// ----------------------------------------------------------------------------
1125
1126
1127EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1128{
1129 clearError();
1130
Jesse Hallb29e5e82012-04-04 16:53:42 -07001131 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001132 if (!dp) return EGL_NO_SYNC_KHR;
1133
Mathias Agopian518ec112011-05-13 16:21:08 -07001134 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001135 egl_connection_t* const cnx = &gEGLImpl;
1136 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1137 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001138 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001139 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001140}
1141
1142EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1143{
1144 clearError();
1145
Jesse Hallb29e5e82012-04-04 16:53:42 -07001146 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001147 if (!dp) return EGL_FALSE;
1148
Mathias Agopian518ec112011-05-13 16:21:08 -07001149 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001150 egl_connection_t* const cnx = &gEGLImpl;
1151 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1152 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001153 }
1154 return result;
1155}
1156
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001157EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1158 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001159{
1160 clearError();
1161
Jesse Hallb29e5e82012-04-04 16:53:42 -07001162 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001163 if (!dp) return EGL_FALSE;
1164
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001165 EGLBoolean result = EGL_FALSE;
1166 egl_connection_t* const cnx = &gEGLImpl;
1167 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1168 result = cnx->egl.eglClientWaitSyncKHR(
1169 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001170 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001171 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001172}
1173
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001174EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1175 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001176{
1177 clearError();
1178
Jesse Hallb29e5e82012-04-04 16:53:42 -07001179 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001180 if (!dp) return EGL_FALSE;
1181
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001182 EGLBoolean result = EGL_FALSE;
1183 egl_connection_t* const cnx = &gEGLImpl;
1184 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1185 result = cnx->egl.eglGetSyncAttribKHR(
1186 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001187 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001188 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001189}
1190
1191// ----------------------------------------------------------------------------
1192// ANDROID extensions
1193// ----------------------------------------------------------------------------
1194
Bjorn Anderssonf9752cc2012-08-21 22:22:02 -07001195EGLClientBuffer eglGetRenderBufferANDROID(EGLDisplay dpy, EGLSurface draw)
1196{
1197 clearError();
1198
1199 const egl_display_ptr dp = validate_display(dpy);
1200 if (!dp) return EGL_FALSE;
1201
1202 egl_surface_t const * const s = get_surface(draw);
1203
1204 egl_connection_t* const cnx = &gEGLImpl;
1205 if (cnx->dso && cnx->egl.eglGetRenderBufferANDROID) {
1206 return cnx->egl.eglGetRenderBufferANDROID(dp->disp.dpy, s->surface);
1207 }
1208 return setError(EGL_BAD_DISPLAY, (EGLClientBuffer*)0);
1209}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001210
1211// ----------------------------------------------------------------------------
1212// NVIDIA extensions
1213// ----------------------------------------------------------------------------
1214EGLuint64NV eglGetSystemTimeFrequencyNV()
1215{
1216 clearError();
1217
1218 if (egl_init_drivers() == EGL_FALSE) {
1219 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1220 }
1221
1222 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001223 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001224
Mathias Agopianada798b2012-02-13 17:09:30 -08001225 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1226 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001227 }
1228
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001229 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001230}
1231
1232EGLuint64NV eglGetSystemTimeNV()
1233{
1234 clearError();
1235
1236 if (egl_init_drivers() == EGL_FALSE) {
1237 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1238 }
1239
1240 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001241 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001242
Mathias Agopianada798b2012-02-13 17:09:30 -08001243 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1244 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001245 }
1246
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001247 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001248}