blob: 8df7d3937e70941d8a953ef21b34199f218c4bea [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 },
Jonas Yang1c3d72a2011-08-26 20:04:39 +080073 { "eglGetSystemTimeFrequencyNV",
74 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
75 { "eglGetSystemTimeNV",
76 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopian518ec112011-05-13 16:21:08 -070077};
78
79// accesses protected by sExtensionMapMutex
80static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
81static int sGLExtentionSlot = 0;
82static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
83
84static void(*findProcAddress(const char* name,
85 const extention_map_t* map, size_t n))() {
86 for (uint32_t i=0 ; i<n ; i++) {
87 if (!strcmp(name, map[i].name)) {
88 return map[i].address;
89 }
90 }
91 return NULL;
92}
93
94// ----------------------------------------------------------------------------
95
Mathias Agopian518ec112011-05-13 16:21:08 -070096namespace android {
97extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
98extern EGLBoolean egl_init_drivers();
99extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
100extern int gEGLDebugLevel;
101extern gl_hooks_t gHooksTrace;
Mathias Agopian518ec112011-05-13 16:21:08 -0700102} // namespace android;
103
104// ----------------------------------------------------------------------------
105
106static inline void clearError() { egl_tls_t::clearError(); }
107static inline EGLContext getContext() { return egl_tls_t::getContext(); }
108
109// ----------------------------------------------------------------------------
110
111EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
112{
113 clearError();
114
115 uint32_t index = uint32_t(display);
116 if (index >= NUM_DISPLAYS) {
117 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
118 }
119
120 if (egl_init_drivers() == EGL_FALSE) {
121 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
122 }
123
124 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
125 return dpy;
126}
127
128// ----------------------------------------------------------------------------
129// Initialization
130// ----------------------------------------------------------------------------
131
132EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
133{
134 clearError();
135
136 egl_display_t * const dp = get_display(dpy);
137 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
138
139 EGLBoolean res = dp->initialize(major, minor);
140
141 return res;
142}
143
144EGLBoolean eglTerminate(EGLDisplay dpy)
145{
146 // NOTE: don't unload the drivers b/c some APIs can be called
147 // after eglTerminate() has been called. eglTerminate() only
148 // terminates an EGLDisplay, not a EGL itself.
149
150 clearError();
151
152 egl_display_t* const dp = get_display(dpy);
153 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
154
155 EGLBoolean res = dp->terminate();
156
157 return res;
158}
159
160// ----------------------------------------------------------------------------
161// configuration
162// ----------------------------------------------------------------------------
163
164EGLBoolean eglGetConfigs( EGLDisplay dpy,
165 EGLConfig *configs,
166 EGLint config_size, EGLint *num_config)
167{
168 clearError();
169
170 egl_display_t const * const dp = validate_display(dpy);
171 if (!dp) return EGL_FALSE;
172
Mathias Agopian7773c432012-02-13 20:06:08 -0800173 if (num_config==0) {
174 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700175 }
176
Mathias Agopian7773c432012-02-13 20:06:08 -0800177 EGLBoolean res = EGL_FALSE;
178 *num_config = 0;
179
180 egl_connection_t* const cnx = &gEGLImpl;
181 if (cnx->dso) {
182 res = cnx->egl.eglGetConfigs(
183 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700184 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800185
186 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700187}
188
189EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
190 EGLConfig *configs, EGLint config_size,
191 EGLint *num_config)
192{
193 clearError();
194
195 egl_display_t const * const dp = validate_display(dpy);
196 if (!dp) return EGL_FALSE;
197
198 if (num_config==0) {
199 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
200 }
201
Mathias Agopian518ec112011-05-13 16:21:08 -0700202 EGLBoolean res = EGL_FALSE;
203 *num_config = 0;
204
Mathias Agopianada798b2012-02-13 17:09:30 -0800205 egl_connection_t* const cnx = &gEGLImpl;
206 if (cnx->dso) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800207 res = cnx->egl.eglChooseConfig(
208 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700209 }
210 return res;
211}
212
213EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
214 EGLint attribute, EGLint *value)
215{
216 clearError();
217
218 egl_display_t const* dp = 0;
219 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
220 if (!cnx) return EGL_FALSE;
221
Mathias Agopian518ec112011-05-13 16:21:08 -0700222 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800223 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700224}
225
226// ----------------------------------------------------------------------------
227// surfaces
228// ----------------------------------------------------------------------------
229
230EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
231 NativeWindowType window,
232 const EGLint *attrib_list)
233{
234 clearError();
235
236 egl_display_t const* dp = 0;
237 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
238 if (cnx) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800239 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700240 EGLint format;
241
Mathias Agopian81a63352011-07-29 17:55:48 -0700242 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000243 ALOGE("EGLNativeWindowType %p already connected to another API",
Mathias Agopian81a63352011-07-29 17:55:48 -0700244 window);
245 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
246 }
247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248 // set the native window's buffers format to match this config
249 if (cnx->egl.eglGetConfigAttrib(iDpy,
Mathias Agopian7773c432012-02-13 20:06:08 -0800250 config, EGL_NATIVE_VISUAL_ID, &format)) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700251 if (format != 0) {
Jamie Gennisbee205f2011-07-01 13:12:07 -0700252 int err = native_window_set_buffers_format(window, format);
253 if (err != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000254 ALOGE("error setting native window pixel format: %s (%d)",
Jamie Gennisbee205f2011-07-01 13:12:07 -0700255 strerror(-err), err);
Mathias Agopian81a63352011-07-29 17:55:48 -0700256 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700257 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
258 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700259 }
260 }
261
Jamie Gennis59769462011-11-19 18:04:43 -0800262 // the EGL spec requires that a new EGLSurface default to swap interval
263 // 1, so explicitly set that on the window here.
264 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
265 anw->setSwapInterval(anw, 1);
266
Mathias Agopian518ec112011-05-13 16:21:08 -0700267 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800268 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700269 if (surface != EGL_NO_SURFACE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800270 egl_surface_t* s = new egl_surface_t(dpy, config, window, surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700271 return s;
272 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700273
274 // EGLSurface creation failed
275 native_window_set_buffers_format(window, 0);
276 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700277 }
278 return EGL_NO_SURFACE;
279}
280
281EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
282 NativePixmapType pixmap,
283 const EGLint *attrib_list)
284{
285 clearError();
286
287 egl_display_t const* dp = 0;
288 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
289 if (cnx) {
290 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800291 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700292 if (surface != EGL_NO_SURFACE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800293 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 return s;
295 }
296 }
297 return EGL_NO_SURFACE;
298}
299
300EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
301 const EGLint *attrib_list)
302{
303 clearError();
304
305 egl_display_t const* dp = 0;
306 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
307 if (cnx) {
308 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800309 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700310 if (surface != EGL_NO_SURFACE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800311 egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700312 return s;
313 }
314 }
315 return EGL_NO_SURFACE;
316}
317
318EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
319{
320 clearError();
321
322 egl_display_t const * const dp = validate_display(dpy);
323 if (!dp) return EGL_FALSE;
324
Mathias Agopianf0480de2011-11-13 20:50:07 -0800325 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700326 if (!_s.get())
327 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700328
329 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800330 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700331 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700332 _s.terminate();
333 }
334 return result;
335}
336
337EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
338 EGLint attribute, EGLint *value)
339{
340 clearError();
341
342 egl_display_t const * const dp = validate_display(dpy);
343 if (!dp) return EGL_FALSE;
344
Mathias Agopianf0480de2011-11-13 20:50:07 -0800345 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700346 if (!_s.get())
347 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700348
Mathias Agopian518ec112011-05-13 16:21:08 -0700349 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800350 return s->cnx->egl.eglQuerySurface(
351 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700352}
353
Jamie Gennise8696a42012-01-15 18:54:57 -0800354void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800355 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800356 clearError();
357
358 egl_display_t const * const dp = validate_display(dpy);
359 if (!dp) {
360 return;
361 }
362
363 SurfaceRef _s(dp, surface);
364 if (!_s.get()) {
365 setError(EGL_BAD_SURFACE, EGL_FALSE);
366 return;
367 }
368
369 int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
370
371 egl_surface_t const * const s = get_surface(surface);
372 native_window_set_buffers_timestamp(s->win.get(), timestamp);
373}
374
Mathias Agopian518ec112011-05-13 16:21:08 -0700375// ----------------------------------------------------------------------------
376// Contexts
377// ----------------------------------------------------------------------------
378
379EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
380 EGLContext share_list, const EGLint *attrib_list)
381{
382 clearError();
383
384 egl_display_t const* dp = 0;
385 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
386 if (cnx) {
387 if (share_list != EGL_NO_CONTEXT) {
388 egl_context_t* const c = get_context(share_list);
389 share_list = c->context;
390 }
391 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800392 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700393 if (context != EGL_NO_CONTEXT) {
394 // figure out if it's a GLESv1 or GLESv2
395 int version = 0;
396 if (attrib_list) {
397 while (*attrib_list != EGL_NONE) {
398 GLint attr = *attrib_list++;
399 GLint value = *attrib_list++;
400 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
401 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800402 version = egl_connection_t::GLESv1_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700403 } else if (value == 2) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800404 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700405 }
406 }
407 };
408 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800409 egl_context_t* c = new egl_context_t(dpy, context, config, cnx, version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800410#if EGL_TRACE
411 if (gEGLDebugLevel > 0)
412 GLTrace_eglCreateContext(version, c);
413#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700414 return c;
415 }
416 }
417 return EGL_NO_CONTEXT;
418}
419
420EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
421{
422 clearError();
423
424 egl_display_t const * const dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700425 if (!dp)
426 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700427
Mathias Agopianf0480de2011-11-13 20:50:07 -0800428 ContextRef _c(dp, ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700429 if (!_c.get())
430 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700431
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800433 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700434 if (result == EGL_TRUE) {
435 _c.terminate();
436 }
437 return result;
438}
439
Mathias Agopian518ec112011-05-13 16:21:08 -0700440EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
441 EGLSurface read, EGLContext ctx)
442{
443 clearError();
444
445 egl_display_t const * const dp = get_display(dpy);
446 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
447
Mathias Agopian5b287a62011-05-16 18:58:55 -0700448 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
449 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
450 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700451 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
452 (draw != EGL_NO_SURFACE) ) {
453 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
454 }
455
456 // get a reference to the object passed in
Mathias Agopianf0480de2011-11-13 20:50:07 -0800457 ContextRef _c(dp, ctx);
458 SurfaceRef _d(dp, draw);
459 SurfaceRef _r(dp, read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700460
461 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700462 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700463 // EGL_NO_CONTEXT is valid
464 return EGL_FALSE;
465 }
466
467 // these are the underlying implementation's object
468 EGLContext impl_ctx = EGL_NO_CONTEXT;
469 EGLSurface impl_draw = EGL_NO_SURFACE;
470 EGLSurface impl_read = EGL_NO_SURFACE;
471
472 // these are our objects structs passed in
473 egl_context_t * c = NULL;
474 egl_surface_t const * d = NULL;
475 egl_surface_t const * r = NULL;
476
477 // these are the current objects structs
478 egl_context_t * cur_c = get_context(getContext());
479
480 if (ctx != EGL_NO_CONTEXT) {
481 c = get_context(ctx);
482 impl_ctx = c->context;
483 } else {
484 // no context given, use the implementation of the current context
485 if (cur_c == NULL) {
486 // no current context
487 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
488 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
489 return setError(EGL_BAD_MATCH, EGL_FALSE);
490 }
491 // not an error, there is just no current context.
492 return EGL_TRUE;
493 }
494 }
495
496 // retrieve the underlying implementation's draw EGLSurface
497 if (draw != EGL_NO_SURFACE) {
498 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700499 impl_draw = d->surface;
500 }
501
502 // retrieve the underlying implementation's read EGLSurface
503 if (read != EGL_NO_SURFACE) {
504 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700505 impl_read = r->surface;
506 }
507
Mathias Agopian518ec112011-05-13 16:21:08 -0700508
Mathias Agopianfb87e542012-01-30 18:20:52 -0800509 EGLBoolean result = const_cast<egl_display_t*>(dp)->makeCurrent(c, cur_c,
510 draw, read, ctx,
511 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700512
513 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800514 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700515 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
516 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800517#if EGL_TRACE
518 if (gEGLDebugLevel > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800519 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800520#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700521 _c.acquire();
522 _r.acquire();
523 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700524 } else {
525 setGLHooksThreadSpecific(&gHooksNoContext);
526 egl_tls_t::setContext(EGL_NO_CONTEXT);
527 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700528 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000529 // this will ALOGE the error
Mathias Agopian5fecea72011-08-25 18:38:24 -0700530 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700531 }
532 return result;
533}
534
535
536EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
537 EGLint attribute, EGLint *value)
538{
539 clearError();
540
541 egl_display_t const * const dp = validate_display(dpy);
542 if (!dp) return EGL_FALSE;
543
Mathias Agopianf0480de2011-11-13 20:50:07 -0800544 ContextRef _c(dp, ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700545 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
546
Mathias Agopian518ec112011-05-13 16:21:08 -0700547 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800548 return c->cnx->egl.eglQueryContext(
549 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700550
Mathias Agopian518ec112011-05-13 16:21:08 -0700551}
552
553EGLContext eglGetCurrentContext(void)
554{
555 // could be called before eglInitialize(), but we wouldn't have a context
556 // then, and this function would correctly return EGL_NO_CONTEXT.
557
558 clearError();
559
560 EGLContext ctx = getContext();
561 return ctx;
562}
563
564EGLSurface eglGetCurrentSurface(EGLint readdraw)
565{
566 // could be called before eglInitialize(), but we wouldn't have a context
567 // then, and this function would correctly return EGL_NO_SURFACE.
568
569 clearError();
570
571 EGLContext ctx = getContext();
572 if (ctx) {
573 egl_context_t const * const c = get_context(ctx);
574 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
575 switch (readdraw) {
576 case EGL_READ: return c->read;
577 case EGL_DRAW: return c->draw;
578 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
579 }
580 }
581 return EGL_NO_SURFACE;
582}
583
584EGLDisplay eglGetCurrentDisplay(void)
585{
586 // could be called before eglInitialize(), but we wouldn't have a context
587 // then, and this function would correctly return EGL_NO_DISPLAY.
588
589 clearError();
590
591 EGLContext ctx = getContext();
592 if (ctx) {
593 egl_context_t const * const c = get_context(ctx);
594 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
595 return c->dpy;
596 }
597 return EGL_NO_DISPLAY;
598}
599
600EGLBoolean eglWaitGL(void)
601{
Mathias Agopian518ec112011-05-13 16:21:08 -0700602 clearError();
603
Mathias Agopianada798b2012-02-13 17:09:30 -0800604 egl_connection_t* const cnx = &gEGLImpl;
605 if (!cnx->dso)
606 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
607
608 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700609}
610
611EGLBoolean eglWaitNative(EGLint engine)
612{
Mathias Agopian518ec112011-05-13 16:21:08 -0700613 clearError();
614
Mathias Agopianada798b2012-02-13 17:09:30 -0800615 egl_connection_t* const cnx = &gEGLImpl;
616 if (!cnx->dso)
617 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
618
619 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700620}
621
622EGLint eglGetError(void)
623{
Mathias Agopianada798b2012-02-13 17:09:30 -0800624 EGLint err = EGL_SUCCESS;
625 egl_connection_t* const cnx = &gEGLImpl;
626 if (cnx->dso) {
627 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700628 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800629 if (err == EGL_SUCCESS) {
630 err = egl_tls_t::getError();
631 }
632 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700633}
634
Mathias Agopian518ec112011-05-13 16:21:08 -0700635__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
636{
637 // eglGetProcAddress() could be the very first function called
638 // in which case we must make sure we've initialized ourselves, this
639 // happens the first time egl_get_display() is called.
640
641 clearError();
642
643 if (egl_init_drivers() == EGL_FALSE) {
644 setError(EGL_BAD_PARAMETER, NULL);
645 return NULL;
646 }
647
Jamie Gennisaca51c02011-11-03 17:42:43 -0700648 // The EGL_ANDROID_blob_cache extension should not be exposed to
649 // applications. It is used internally by the Android EGL layer.
Jamie Gennisc42fcf02011-11-09 15:35:34 -0800650 if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID")) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700651 return NULL;
652 }
653
Mathias Agopian518ec112011-05-13 16:21:08 -0700654 __eglMustCastToProperFunctionPointerType addr;
655 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
656 if (addr) return addr;
657
Jamie Gennisaca51c02011-11-03 17:42:43 -0700658
Mathias Agopian518ec112011-05-13 16:21:08 -0700659 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
660 pthread_mutex_lock(&sExtensionMapMutex);
661
662 /*
663 * Since eglGetProcAddress() is not associated to anything, it needs
664 * to return a function pointer that "works" regardless of what
665 * the current context is.
666 *
667 * For this reason, we return a "forwarder", a small stub that takes
668 * care of calling the function associated with the context
669 * currently bound.
670 *
671 * We first look for extensions we've already resolved, if we're seeing
672 * this extension for the first time, we go through all our
673 * implementations and call eglGetProcAddress() and record the
674 * result in the appropriate implementation hooks and return the
675 * address of the forwarder corresponding to that hook set.
676 *
677 */
678
679 const String8 name(procname);
680 addr = sGLExtentionMap.valueFor(name);
681 const int slot = sGLExtentionSlot;
682
Steve Blocke6f43dd2012-01-06 19:20:56 +0000683 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700684 "no more slots for eglGetProcAddress(\"%s\")",
685 procname);
686
Siva Velusamy0469dd62011-11-30 15:05:37 -0800687#if EGL_TRACE
688 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
689#endif
690
Mathias Agopian518ec112011-05-13 16:21:08 -0700691 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
692 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800693
694 egl_connection_t* const cnx = &gEGLImpl;
695 if (cnx->dso && cnx->egl.eglGetProcAddress) {
696 found = true;
697 // Extensions are independent of the bound context
Mathias Agopian7773c432012-02-13 20:06:08 -0800698 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
699 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700700#if EGL_TRACE
Mathias Agopianada798b2012-02-13 17:09:30 -0800701 debugHooks->ext.extensions[slot] =
702 gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700703#endif
Mathias Agopianada798b2012-02-13 17:09:30 -0800704 cnx->egl.eglGetProcAddress(procname);
Mathias Agopian518ec112011-05-13 16:21:08 -0700705 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800706
Mathias Agopian518ec112011-05-13 16:21:08 -0700707 if (found) {
708 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700709 sGLExtentionMap.add(name, addr);
710 sGLExtentionSlot++;
711 }
712 }
713
714 pthread_mutex_unlock(&sExtensionMapMutex);
715 return addr;
716}
717
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700718class FrameCompletionThread : public Thread {
719public:
720
721 static void queueSync(EGLSyncKHR sync) {
722 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
723 static bool running = false;
724 if (!running) {
725 thread->run("GPUFrameCompletion");
726 running = true;
727 }
728 {
729 Mutex::Autolock lock(thread->mMutex);
730 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
731 thread->mFramesQueued).string());
732 thread->mQueue.push_back(sync);
733 thread->mCondition.signal();
734 thread->mFramesQueued++;
735 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
736 }
737 }
738
739private:
740 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
741
742 virtual bool threadLoop() {
743 EGLSyncKHR sync;
744 uint32_t frameNum;
745 {
746 Mutex::Autolock lock(mMutex);
747 while (mQueue.isEmpty()) {
748 mCondition.wait(mMutex);
749 }
750 sync = mQueue[0];
751 frameNum = mFramesCompleted;
752 }
753 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
754 {
755 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
756 frameNum).string());
757 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
758 if (result == EGL_FALSE) {
759 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
760 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
761 ALOGE("FrameCompletion: timeout waiting for fence");
762 }
763 eglDestroySyncKHR(dpy, sync);
764 }
765 {
766 Mutex::Autolock lock(mMutex);
767 mQueue.removeAt(0);
768 mFramesCompleted++;
769 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
770 }
771 return true;
772 }
773
774 uint32_t mFramesQueued;
775 uint32_t mFramesCompleted;
776 Vector<EGLSyncKHR> mQueue;
777 Condition mCondition;
778 Mutex mMutex;
779};
780
Mathias Agopian518ec112011-05-13 16:21:08 -0700781EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
782{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800783 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700784 clearError();
785
786 egl_display_t const * const dp = validate_display(dpy);
787 if (!dp) return EGL_FALSE;
788
Mathias Agopianf0480de2011-11-13 20:50:07 -0800789 SurfaceRef _s(dp, draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700790 if (!_s.get())
791 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700792
Siva Velusamy0469dd62011-11-30 15:05:37 -0800793#if EGL_TRACE
794 if (gEGLDebugLevel > 0)
795 GLTrace_eglSwapBuffers(dpy, draw);
796#endif
797
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -0700799
800 if (CC_UNLIKELY(dp->finishOnSwap)) {
801 uint32_t pixel;
802 egl_context_t * const c = get_context( egl_tls_t::getContext() );
803 if (c) {
804 // glReadPixels() ensures that the frame is complete
805 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
806 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
807 }
808 }
809
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700810 EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
811
812 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
813 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
814 {
815 sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
816 }
817 if (sync != EGL_NO_SYNC_KHR) {
818 FrameCompletionThread::queueSync(sync);
819 }
820 }
821
822 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -0700823}
824
825EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
826 NativePixmapType target)
827{
828 clearError();
829
830 egl_display_t const * const dp = validate_display(dpy);
831 if (!dp) return EGL_FALSE;
832
Mathias Agopianf0480de2011-11-13 20:50:07 -0800833 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700834 if (!_s.get())
835 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700836
Mathias Agopian518ec112011-05-13 16:21:08 -0700837 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800838 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -0700839}
840
841const char* eglQueryString(EGLDisplay dpy, EGLint name)
842{
843 clearError();
844
845 egl_display_t const * const dp = validate_display(dpy);
846 if (!dp) return (const char *) NULL;
847
848 switch (name) {
849 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800850 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700851 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800852 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800854 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700855 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800856 return dp->getClientApiString();
Mathias Agopianada798b2012-02-13 17:09:30 -0800857 case EGL_VERSION_HW_ANDROID:
858 return dp->disp.queryString.version;
Mathias Agopian518ec112011-05-13 16:21:08 -0700859 }
860 return setError(EGL_BAD_PARAMETER, (const char *)0);
861}
862
863
864// ----------------------------------------------------------------------------
865// EGL 1.1
866// ----------------------------------------------------------------------------
867
868EGLBoolean eglSurfaceAttrib(
869 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
870{
871 clearError();
872
873 egl_display_t const * const dp = validate_display(dpy);
874 if (!dp) return EGL_FALSE;
875
Mathias Agopianf0480de2011-11-13 20:50:07 -0800876 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700877 if (!_s.get())
878 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700879
Mathias Agopian518ec112011-05-13 16:21:08 -0700880 egl_surface_t const * const s = get_surface(surface);
881 if (s->cnx->egl.eglSurfaceAttrib) {
882 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -0800883 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700884 }
885 return setError(EGL_BAD_SURFACE, EGL_FALSE);
886}
887
888EGLBoolean eglBindTexImage(
889 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
890{
891 clearError();
892
893 egl_display_t const * const dp = validate_display(dpy);
894 if (!dp) return EGL_FALSE;
895
Mathias Agopianf0480de2011-11-13 20:50:07 -0800896 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700897 if (!_s.get())
898 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700899
Mathias Agopian518ec112011-05-13 16:21:08 -0700900 egl_surface_t const * const s = get_surface(surface);
901 if (s->cnx->egl.eglBindTexImage) {
902 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -0800903 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -0700904 }
905 return setError(EGL_BAD_SURFACE, EGL_FALSE);
906}
907
908EGLBoolean eglReleaseTexImage(
909 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
910{
911 clearError();
912
913 egl_display_t const * const dp = validate_display(dpy);
914 if (!dp) return EGL_FALSE;
915
Mathias Agopianf0480de2011-11-13 20:50:07 -0800916 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700917 if (!_s.get())
918 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700919
Mathias Agopian518ec112011-05-13 16:21:08 -0700920 egl_surface_t const * const s = get_surface(surface);
921 if (s->cnx->egl.eglReleaseTexImage) {
922 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -0800923 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -0700924 }
925 return setError(EGL_BAD_SURFACE, EGL_FALSE);
926}
927
928EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
929{
930 clearError();
931
932 egl_display_t const * const dp = validate_display(dpy);
933 if (!dp) return EGL_FALSE;
934
935 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800936 egl_connection_t* const cnx = &gEGLImpl;
937 if (cnx->dso && cnx->egl.eglSwapInterval) {
938 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -0700939 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800940
Mathias Agopian518ec112011-05-13 16:21:08 -0700941 return res;
942}
943
944
945// ----------------------------------------------------------------------------
946// EGL 1.2
947// ----------------------------------------------------------------------------
948
949EGLBoolean eglWaitClient(void)
950{
951 clearError();
952
Mathias Agopianada798b2012-02-13 17:09:30 -0800953 egl_connection_t* const cnx = &gEGLImpl;
954 if (!cnx->dso)
955 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
956
957 EGLBoolean res;
958 if (cnx->egl.eglWaitClient) {
959 res = cnx->egl.eglWaitClient();
960 } else {
961 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700962 }
963 return res;
964}
965
966EGLBoolean eglBindAPI(EGLenum api)
967{
968 clearError();
969
970 if (egl_init_drivers() == EGL_FALSE) {
971 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
972 }
973
974 // bind this API on all EGLs
975 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800976 egl_connection_t* const cnx = &gEGLImpl;
977 if (cnx->dso && cnx->egl.eglBindAPI) {
978 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -0700979 }
980 return res;
981}
982
983EGLenum eglQueryAPI(void)
984{
985 clearError();
986
987 if (egl_init_drivers() == EGL_FALSE) {
988 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
989 }
990
Mathias Agopianada798b2012-02-13 17:09:30 -0800991 egl_connection_t* const cnx = &gEGLImpl;
992 if (cnx->dso && cnx->egl.eglQueryAPI) {
993 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -0700994 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800995
Mathias Agopian518ec112011-05-13 16:21:08 -0700996 // or, it can only be OpenGL ES
997 return EGL_OPENGL_ES_API;
998}
999
1000EGLBoolean eglReleaseThread(void)
1001{
1002 clearError();
1003
1004 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001005 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001006
Mathias Agopianada798b2012-02-13 17:09:30 -08001007 egl_connection_t* const cnx = &gEGLImpl;
1008 if (cnx->dso && cnx->egl.eglReleaseThread) {
1009 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001011
Mathias Agopian518ec112011-05-13 16:21:08 -07001012 egl_tls_t::clearTLS();
Siva Velusamy0469dd62011-11-30 15:05:37 -08001013#if EGL_TRACE
1014 if (gEGLDebugLevel > 0)
1015 GLTrace_eglReleaseThread();
1016#endif
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 return EGL_TRUE;
1018}
1019
1020EGLSurface eglCreatePbufferFromClientBuffer(
1021 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1022 EGLConfig config, const EGLint *attrib_list)
1023{
1024 clearError();
1025
1026 egl_display_t const* dp = 0;
1027 egl_connection_t* cnx = validate_display_config(dpy, config, dp);
1028 if (!cnx) return EGL_FALSE;
1029 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1030 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001031 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001032 }
1033 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1034}
1035
1036// ----------------------------------------------------------------------------
1037// EGL_EGLEXT_VERSION 3
1038// ----------------------------------------------------------------------------
1039
1040EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1041 const EGLint *attrib_list)
1042{
1043 clearError();
1044
1045 egl_display_t const * const dp = validate_display(dpy);
1046 if (!dp) return EGL_FALSE;
1047
Mathias Agopianf0480de2011-11-13 20:50:07 -08001048 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001049 if (!_s.get())
1050 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001051
1052 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001053 if (s->cnx->egl.eglLockSurfaceKHR) {
1054 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001055 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001056 }
1057 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1058}
1059
1060EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1061{
1062 clearError();
1063
1064 egl_display_t const * const dp = validate_display(dpy);
1065 if (!dp) return EGL_FALSE;
1066
Mathias Agopianf0480de2011-11-13 20:50:07 -08001067 SurfaceRef _s(dp, surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001068 if (!_s.get())
1069 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001070
1071 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001072 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001073 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001074 }
1075 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1076}
1077
1078EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1079 EGLClientBuffer buffer, const EGLint *attrib_list)
1080{
1081 clearError();
1082
1083 egl_display_t const * const dp = validate_display(dpy);
1084 if (!dp) return EGL_NO_IMAGE_KHR;
1085
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001086 ContextRef _c(dp, ctx);
1087 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001088
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001089 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1090 egl_connection_t* const cnx = &gEGLImpl;
1091 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1092 result = cnx->egl.eglCreateImageKHR(
1093 dp->disp.dpy,
1094 c ? c->context : EGL_NO_CONTEXT,
1095 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001096 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001097 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001098}
1099
1100EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1101{
1102 clearError();
1103
1104 egl_display_t const * const dp = validate_display(dpy);
1105 if (!dp) return EGL_FALSE;
1106
Mathias Agopianada798b2012-02-13 17:09:30 -08001107 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001108 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1109 cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001110 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001111 return EGL_TRUE;
1112}
1113
1114// ----------------------------------------------------------------------------
1115// EGL_EGLEXT_VERSION 5
1116// ----------------------------------------------------------------------------
1117
1118
1119EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1120{
1121 clearError();
1122
1123 egl_display_t const * const dp = validate_display(dpy);
1124 if (!dp) return EGL_NO_SYNC_KHR;
1125
Mathias Agopian518ec112011-05-13 16:21:08 -07001126 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001127 egl_connection_t* const cnx = &gEGLImpl;
1128 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1129 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001130 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001131 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001132}
1133
1134EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1135{
1136 clearError();
1137
1138 egl_display_t const * const dp = validate_display(dpy);
1139 if (!dp) return EGL_FALSE;
1140
Mathias Agopian518ec112011-05-13 16:21:08 -07001141 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001142 egl_connection_t* const cnx = &gEGLImpl;
1143 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1144 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001145 }
1146 return result;
1147}
1148
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001149EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1150 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001151{
1152 clearError();
1153
1154 egl_display_t const * const dp = validate_display(dpy);
1155 if (!dp) return EGL_FALSE;
1156
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001157 EGLBoolean result = EGL_FALSE;
1158 egl_connection_t* const cnx = &gEGLImpl;
1159 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1160 result = cnx->egl.eglClientWaitSyncKHR(
1161 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001162 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001163 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001164}
1165
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001166EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1167 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001168{
1169 clearError();
1170
1171 egl_display_t const * const dp = validate_display(dpy);
1172 if (!dp) return EGL_FALSE;
1173
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001174 EGLBoolean result = EGL_FALSE;
1175 egl_connection_t* const cnx = &gEGLImpl;
1176 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1177 result = cnx->egl.eglGetSyncAttribKHR(
1178 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001179 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001180 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001181}
1182
1183// ----------------------------------------------------------------------------
1184// ANDROID extensions
1185// ----------------------------------------------------------------------------
1186
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001187/* ANDROID extensions entry-point go here */
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001188
1189// ----------------------------------------------------------------------------
1190// NVIDIA extensions
1191// ----------------------------------------------------------------------------
1192EGLuint64NV eglGetSystemTimeFrequencyNV()
1193{
1194 clearError();
1195
1196 if (egl_init_drivers() == EGL_FALSE) {
1197 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1198 }
1199
1200 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001201 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001202
Mathias Agopianada798b2012-02-13 17:09:30 -08001203 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1204 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001205 }
1206
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001207 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001208}
1209
1210EGLuint64NV eglGetSystemTimeNV()
1211{
1212 clearError();
1213
1214 if (egl_init_drivers() == EGL_FALSE) {
1215 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1216 }
1217
1218 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001219 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001220
Mathias Agopianada798b2012-02-13 17:09:30 -08001221 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1222 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001223 }
1224
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001225 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001226}