blob: c943ec14cda8c78bc29603945a8f3f86c0371f16 [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
Jesse Hallb29e5e82012-04-04 16:53:42 -0700136 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700137 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
Jesse Hallb29e5e82012-04-04 16:53:42 -0700152 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700153 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
Jesse Hallb29e5e82012-04-04 16:53:42 -0700170 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700171 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
Jesse Hallb29e5e82012-04-04 16:53:42 -0700195 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700196 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
Jesse Hallb29e5e82012-04-04 16:53:42 -0700218 egl_connection_t* cnx = NULL;
219 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
220 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700221
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
Jesse Hallb29e5e82012-04-04 16:53:42 -0700236 egl_connection_t* cnx = NULL;
237 egl_display_ptr dp = validate_display_connection(dpy, cnx);
238 if (dp) {
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) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700270 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
271 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700272 return s;
273 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700274
275 // EGLSurface creation failed
276 native_window_set_buffers_format(window, 0);
277 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700278 }
279 return EGL_NO_SURFACE;
280}
281
282EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
283 NativePixmapType pixmap,
284 const EGLint *attrib_list)
285{
286 clearError();
287
Jesse Hallb29e5e82012-04-04 16:53:42 -0700288 egl_connection_t* cnx = NULL;
289 egl_display_ptr dp = validate_display_connection(dpy, cnx);
290 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700291 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800292 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700293 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700294 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
295 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700296 return s;
297 }
298 }
299 return EGL_NO_SURFACE;
300}
301
302EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
303 const EGLint *attrib_list)
304{
305 clearError();
306
Jesse Hallb29e5e82012-04-04 16:53:42 -0700307 egl_connection_t* cnx = NULL;
308 egl_display_ptr dp = validate_display_connection(dpy, cnx);
309 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700310 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800311 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700312 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700313 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
314 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700315 return s;
316 }
317 }
318 return EGL_NO_SURFACE;
319}
320
321EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
322{
323 clearError();
324
Jesse Hallb29e5e82012-04-04 16:53:42 -0700325 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700326 if (!dp) return EGL_FALSE;
327
Jesse Hallb29e5e82012-04-04 16:53:42 -0700328 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700329 if (!_s.get())
330 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700331
332 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800333 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700334 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700335 _s.terminate();
336 }
337 return result;
338}
339
340EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
341 EGLint attribute, EGLint *value)
342{
343 clearError();
344
Jesse Hallb29e5e82012-04-04 16:53:42 -0700345 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700346 if (!dp) return EGL_FALSE;
347
Jesse Hallb29e5e82012-04-04 16:53:42 -0700348 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700349 if (!_s.get())
350 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700351
Mathias Agopian518ec112011-05-13 16:21:08 -0700352 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800353 return s->cnx->egl.eglQuerySurface(
354 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700355}
356
Jamie Gennise8696a42012-01-15 18:54:57 -0800357void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800358 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800359 clearError();
360
Jesse Hallb29e5e82012-04-04 16:53:42 -0700361 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800362 if (!dp) {
363 return;
364 }
365
Jesse Hallb29e5e82012-04-04 16:53:42 -0700366 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800367 if (!_s.get()) {
368 setError(EGL_BAD_SURFACE, EGL_FALSE);
369 return;
370 }
371
372 int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
373
374 egl_surface_t const * const s = get_surface(surface);
375 native_window_set_buffers_timestamp(s->win.get(), timestamp);
376}
377
Mathias Agopian518ec112011-05-13 16:21:08 -0700378// ----------------------------------------------------------------------------
379// Contexts
380// ----------------------------------------------------------------------------
381
382EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
383 EGLContext share_list, const EGLint *attrib_list)
384{
385 clearError();
386
Jesse Hallb29e5e82012-04-04 16:53:42 -0700387 egl_connection_t* cnx = NULL;
388 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700389 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700390 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700391 if (!ContextRef(dp.get(), share_list).get()) {
392 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
393 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700394 egl_context_t* const c = get_context(share_list);
395 share_list = c->context;
396 }
397 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800398 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700399 if (context != EGL_NO_CONTEXT) {
400 // figure out if it's a GLESv1 or GLESv2
401 int version = 0;
402 if (attrib_list) {
403 while (*attrib_list != EGL_NONE) {
404 GLint attr = *attrib_list++;
405 GLint value = *attrib_list++;
406 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
407 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800408 version = egl_connection_t::GLESv1_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700409 } else if (value == 2) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800410 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700411 }
412 }
413 };
414 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700415 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
416 version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800417#if EGL_TRACE
418 if (gEGLDebugLevel > 0)
419 GLTrace_eglCreateContext(version, c);
420#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700421 return c;
422 }
423 }
424 return EGL_NO_CONTEXT;
425}
426
427EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
428{
429 clearError();
430
Jesse Hallb29e5e82012-04-04 16:53:42 -0700431 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700432 if (!dp)
433 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700434
Jesse Hallb29e5e82012-04-04 16:53:42 -0700435 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700436 if (!_c.get())
437 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700438
Mathias Agopian518ec112011-05-13 16:21:08 -0700439 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800440 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700441 if (result == EGL_TRUE) {
442 _c.terminate();
443 }
444 return result;
445}
446
Mathias Agopian518ec112011-05-13 16:21:08 -0700447EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
448 EGLSurface read, EGLContext ctx)
449{
450 clearError();
451
Jesse Hallb29e5e82012-04-04 16:53:42 -0700452 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700453 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
454
Mathias Agopian5b287a62011-05-16 18:58:55 -0700455 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
456 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
457 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700458 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
459 (draw != EGL_NO_SURFACE) ) {
460 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
461 }
462
463 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700464 ContextRef _c(dp.get(), ctx);
465 SurfaceRef _d(dp.get(), draw);
466 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700467
468 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700469 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700470 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700471 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700472 }
473
474 // these are the underlying implementation's object
475 EGLContext impl_ctx = EGL_NO_CONTEXT;
476 EGLSurface impl_draw = EGL_NO_SURFACE;
477 EGLSurface impl_read = EGL_NO_SURFACE;
478
479 // these are our objects structs passed in
480 egl_context_t * c = NULL;
481 egl_surface_t const * d = NULL;
482 egl_surface_t const * r = NULL;
483
484 // these are the current objects structs
485 egl_context_t * cur_c = get_context(getContext());
486
487 if (ctx != EGL_NO_CONTEXT) {
488 c = get_context(ctx);
489 impl_ctx = c->context;
490 } else {
491 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700492 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
493 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
494 return setError(EGL_BAD_MATCH, EGL_FALSE);
495 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700496 if (cur_c == NULL) {
497 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700498 // not an error, there is just no current context.
499 return EGL_TRUE;
500 }
501 }
502
503 // retrieve the underlying implementation's draw EGLSurface
504 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700505 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700506 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700507 impl_draw = d->surface;
508 }
509
510 // retrieve the underlying implementation's read EGLSurface
511 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700512 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700513 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700514 impl_read = r->surface;
515 }
516
Mathias Agopian518ec112011-05-13 16:21:08 -0700517
Jesse Hallb29e5e82012-04-04 16:53:42 -0700518 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800519 draw, read, ctx,
520 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700521
522 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800523 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700524 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
525 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800526#if EGL_TRACE
527 if (gEGLDebugLevel > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800528 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800529#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700530 _c.acquire();
531 _r.acquire();
532 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700533 } else {
534 setGLHooksThreadSpecific(&gHooksNoContext);
535 egl_tls_t::setContext(EGL_NO_CONTEXT);
536 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700537 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000538 // this will ALOGE the error
Mathias Agopian5fecea72011-08-25 18:38:24 -0700539 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700540 }
541 return result;
542}
543
544
545EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
546 EGLint attribute, EGLint *value)
547{
548 clearError();
549
Jesse Hallb29e5e82012-04-04 16:53:42 -0700550 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700551 if (!dp) return EGL_FALSE;
552
Jesse Hallb29e5e82012-04-04 16:53:42 -0700553 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700554 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
555
Mathias Agopian518ec112011-05-13 16:21:08 -0700556 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800557 return c->cnx->egl.eglQueryContext(
558 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700559
Mathias Agopian518ec112011-05-13 16:21:08 -0700560}
561
562EGLContext eglGetCurrentContext(void)
563{
564 // could be called before eglInitialize(), but we wouldn't have a context
565 // then, and this function would correctly return EGL_NO_CONTEXT.
566
567 clearError();
568
569 EGLContext ctx = getContext();
570 return ctx;
571}
572
573EGLSurface eglGetCurrentSurface(EGLint readdraw)
574{
575 // could be called before eglInitialize(), but we wouldn't have a context
576 // then, and this function would correctly return EGL_NO_SURFACE.
577
578 clearError();
579
580 EGLContext ctx = getContext();
581 if (ctx) {
582 egl_context_t const * const c = get_context(ctx);
583 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
584 switch (readdraw) {
585 case EGL_READ: return c->read;
586 case EGL_DRAW: return c->draw;
587 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
588 }
589 }
590 return EGL_NO_SURFACE;
591}
592
593EGLDisplay eglGetCurrentDisplay(void)
594{
595 // could be called before eglInitialize(), but we wouldn't have a context
596 // then, and this function would correctly return EGL_NO_DISPLAY.
597
598 clearError();
599
600 EGLContext ctx = getContext();
601 if (ctx) {
602 egl_context_t const * const c = get_context(ctx);
603 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
604 return c->dpy;
605 }
606 return EGL_NO_DISPLAY;
607}
608
609EGLBoolean eglWaitGL(void)
610{
Mathias Agopian518ec112011-05-13 16:21:08 -0700611 clearError();
612
Mathias Agopianada798b2012-02-13 17:09:30 -0800613 egl_connection_t* const cnx = &gEGLImpl;
614 if (!cnx->dso)
615 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
616
617 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700618}
619
620EGLBoolean eglWaitNative(EGLint engine)
621{
Mathias Agopian518ec112011-05-13 16:21:08 -0700622 clearError();
623
Mathias Agopianada798b2012-02-13 17:09:30 -0800624 egl_connection_t* const cnx = &gEGLImpl;
625 if (!cnx->dso)
626 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
627
628 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700629}
630
631EGLint eglGetError(void)
632{
Mathias Agopianada798b2012-02-13 17:09:30 -0800633 EGLint err = EGL_SUCCESS;
634 egl_connection_t* const cnx = &gEGLImpl;
635 if (cnx->dso) {
636 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700637 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800638 if (err == EGL_SUCCESS) {
639 err = egl_tls_t::getError();
640 }
641 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700642}
643
Mathias Agopian518ec112011-05-13 16:21:08 -0700644__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
645{
646 // eglGetProcAddress() could be the very first function called
647 // in which case we must make sure we've initialized ourselves, this
648 // happens the first time egl_get_display() is called.
649
650 clearError();
651
652 if (egl_init_drivers() == EGL_FALSE) {
653 setError(EGL_BAD_PARAMETER, NULL);
654 return NULL;
655 }
656
Jesse Hall25838592012-04-05 15:53:28 -0700657 // These extensions should not be exposed to applications. They're used
658 // internally by the Android EGL layer.
659 if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID") ||
660 !strcmp(procname, "eglHibernateProcessIMG") ||
661 !strcmp(procname, "eglAwakenProcessIMG")) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700662 return NULL;
663 }
664
Mathias Agopian518ec112011-05-13 16:21:08 -0700665 __eglMustCastToProperFunctionPointerType addr;
666 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
667 if (addr) return addr;
668
Jamie Gennisaca51c02011-11-03 17:42:43 -0700669
Mathias Agopian518ec112011-05-13 16:21:08 -0700670 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
671 pthread_mutex_lock(&sExtensionMapMutex);
672
673 /*
674 * Since eglGetProcAddress() is not associated to anything, it needs
675 * to return a function pointer that "works" regardless of what
676 * the current context is.
677 *
678 * For this reason, we return a "forwarder", a small stub that takes
679 * care of calling the function associated with the context
680 * currently bound.
681 *
682 * We first look for extensions we've already resolved, if we're seeing
683 * this extension for the first time, we go through all our
684 * implementations and call eglGetProcAddress() and record the
685 * result in the appropriate implementation hooks and return the
686 * address of the forwarder corresponding to that hook set.
687 *
688 */
689
690 const String8 name(procname);
691 addr = sGLExtentionMap.valueFor(name);
692 const int slot = sGLExtentionSlot;
693
Steve Blocke6f43dd2012-01-06 19:20:56 +0000694 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700695 "no more slots for eglGetProcAddress(\"%s\")",
696 procname);
697
Siva Velusamy0469dd62011-11-30 15:05:37 -0800698#if EGL_TRACE
699 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
700#endif
701
Mathias Agopian518ec112011-05-13 16:21:08 -0700702 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
703 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800704
705 egl_connection_t* const cnx = &gEGLImpl;
706 if (cnx->dso && cnx->egl.eglGetProcAddress) {
707 found = true;
708 // Extensions are independent of the bound context
Mathias Agopian7773c432012-02-13 20:06:08 -0800709 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
710 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700711#if EGL_TRACE
Mathias Agopianada798b2012-02-13 17:09:30 -0800712 debugHooks->ext.extensions[slot] =
713 gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700714#endif
Mathias Agopianada798b2012-02-13 17:09:30 -0800715 cnx->egl.eglGetProcAddress(procname);
Mathias Agopian518ec112011-05-13 16:21:08 -0700716 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800717
Mathias Agopian518ec112011-05-13 16:21:08 -0700718 if (found) {
719 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700720 sGLExtentionMap.add(name, addr);
721 sGLExtentionSlot++;
722 }
723 }
724
725 pthread_mutex_unlock(&sExtensionMapMutex);
726 return addr;
727}
728
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700729class FrameCompletionThread : public Thread {
730public:
731
732 static void queueSync(EGLSyncKHR sync) {
733 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
734 static bool running = false;
735 if (!running) {
736 thread->run("GPUFrameCompletion");
737 running = true;
738 }
739 {
740 Mutex::Autolock lock(thread->mMutex);
741 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
742 thread->mFramesQueued).string());
743 thread->mQueue.push_back(sync);
744 thread->mCondition.signal();
745 thread->mFramesQueued++;
746 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
747 }
748 }
749
750private:
751 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
752
753 virtual bool threadLoop() {
754 EGLSyncKHR sync;
755 uint32_t frameNum;
756 {
757 Mutex::Autolock lock(mMutex);
758 while (mQueue.isEmpty()) {
759 mCondition.wait(mMutex);
760 }
761 sync = mQueue[0];
762 frameNum = mFramesCompleted;
763 }
764 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
765 {
766 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
767 frameNum).string());
768 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
769 if (result == EGL_FALSE) {
770 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
771 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
772 ALOGE("FrameCompletion: timeout waiting for fence");
773 }
774 eglDestroySyncKHR(dpy, sync);
775 }
776 {
777 Mutex::Autolock lock(mMutex);
778 mQueue.removeAt(0);
779 mFramesCompleted++;
780 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
781 }
782 return true;
783 }
784
785 uint32_t mFramesQueued;
786 uint32_t mFramesCompleted;
787 Vector<EGLSyncKHR> mQueue;
788 Condition mCondition;
789 Mutex mMutex;
790};
791
Mathias Agopian518ec112011-05-13 16:21:08 -0700792EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
793{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800794 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700795 clearError();
796
Jesse Hallb29e5e82012-04-04 16:53:42 -0700797 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 if (!dp) return EGL_FALSE;
799
Jesse Hallb29e5e82012-04-04 16:53:42 -0700800 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700801 if (!_s.get())
802 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700803
Siva Velusamy0469dd62011-11-30 15:05:37 -0800804#if EGL_TRACE
805 if (gEGLDebugLevel > 0)
806 GLTrace_eglSwapBuffers(dpy, draw);
807#endif
808
Mathias Agopian518ec112011-05-13 16:21:08 -0700809 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -0700810
811 if (CC_UNLIKELY(dp->finishOnSwap)) {
812 uint32_t pixel;
813 egl_context_t * const c = get_context( egl_tls_t::getContext() );
814 if (c) {
815 // glReadPixels() ensures that the frame is complete
816 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
817 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
818 }
819 }
820
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700821 EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
822
823 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
824 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
825 {
826 sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
827 }
828 if (sync != EGL_NO_SYNC_KHR) {
829 FrameCompletionThread::queueSync(sync);
830 }
831 }
832
833 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -0700834}
835
836EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
837 NativePixmapType target)
838{
839 clearError();
840
Jesse Hallb29e5e82012-04-04 16:53:42 -0700841 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700842 if (!dp) return EGL_FALSE;
843
Jesse Hallb29e5e82012-04-04 16:53:42 -0700844 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700845 if (!_s.get())
846 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700847
Mathias Agopian518ec112011-05-13 16:21:08 -0700848 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800849 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -0700850}
851
852const char* eglQueryString(EGLDisplay dpy, EGLint name)
853{
854 clearError();
855
Jesse Hallb29e5e82012-04-04 16:53:42 -0700856 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700857 if (!dp) return (const char *) NULL;
858
859 switch (name) {
860 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800861 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700862 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800863 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700864 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800865 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700866 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800867 return dp->getClientApiString();
Mathias Agopianada798b2012-02-13 17:09:30 -0800868 case EGL_VERSION_HW_ANDROID:
869 return dp->disp.queryString.version;
Mathias Agopian518ec112011-05-13 16:21:08 -0700870 }
871 return setError(EGL_BAD_PARAMETER, (const char *)0);
872}
873
874
875// ----------------------------------------------------------------------------
876// EGL 1.1
877// ----------------------------------------------------------------------------
878
879EGLBoolean eglSurfaceAttrib(
880 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
881{
882 clearError();
883
Jesse Hallb29e5e82012-04-04 16:53:42 -0700884 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700885 if (!dp) return EGL_FALSE;
886
Jesse Hallb29e5e82012-04-04 16:53:42 -0700887 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700888 if (!_s.get())
889 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700890
Mathias Agopian518ec112011-05-13 16:21:08 -0700891 egl_surface_t const * const s = get_surface(surface);
892 if (s->cnx->egl.eglSurfaceAttrib) {
893 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -0800894 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700895 }
896 return setError(EGL_BAD_SURFACE, EGL_FALSE);
897}
898
899EGLBoolean eglBindTexImage(
900 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
901{
902 clearError();
903
Jesse Hallb29e5e82012-04-04 16:53:42 -0700904 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700905 if (!dp) return EGL_FALSE;
906
Jesse Hallb29e5e82012-04-04 16:53:42 -0700907 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700908 if (!_s.get())
909 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700910
Mathias Agopian518ec112011-05-13 16:21:08 -0700911 egl_surface_t const * const s = get_surface(surface);
912 if (s->cnx->egl.eglBindTexImage) {
913 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -0800914 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -0700915 }
916 return setError(EGL_BAD_SURFACE, EGL_FALSE);
917}
918
919EGLBoolean eglReleaseTexImage(
920 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
921{
922 clearError();
923
Jesse Hallb29e5e82012-04-04 16:53:42 -0700924 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700925 if (!dp) return EGL_FALSE;
926
Jesse Hallb29e5e82012-04-04 16:53:42 -0700927 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700928 if (!_s.get())
929 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700930
Mathias Agopian518ec112011-05-13 16:21:08 -0700931 egl_surface_t const * const s = get_surface(surface);
932 if (s->cnx->egl.eglReleaseTexImage) {
933 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -0800934 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -0700935 }
936 return setError(EGL_BAD_SURFACE, EGL_FALSE);
937}
938
939EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
940{
941 clearError();
942
Jesse Hallb29e5e82012-04-04 16:53:42 -0700943 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700944 if (!dp) return EGL_FALSE;
945
946 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800947 egl_connection_t* const cnx = &gEGLImpl;
948 if (cnx->dso && cnx->egl.eglSwapInterval) {
949 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -0700950 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800951
Mathias Agopian518ec112011-05-13 16:21:08 -0700952 return res;
953}
954
955
956// ----------------------------------------------------------------------------
957// EGL 1.2
958// ----------------------------------------------------------------------------
959
960EGLBoolean eglWaitClient(void)
961{
962 clearError();
963
Mathias Agopianada798b2012-02-13 17:09:30 -0800964 egl_connection_t* const cnx = &gEGLImpl;
965 if (!cnx->dso)
966 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
967
968 EGLBoolean res;
969 if (cnx->egl.eglWaitClient) {
970 res = cnx->egl.eglWaitClient();
971 } else {
972 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700973 }
974 return res;
975}
976
977EGLBoolean eglBindAPI(EGLenum api)
978{
979 clearError();
980
981 if (egl_init_drivers() == EGL_FALSE) {
982 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
983 }
984
985 // bind this API on all EGLs
986 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800987 egl_connection_t* const cnx = &gEGLImpl;
988 if (cnx->dso && cnx->egl.eglBindAPI) {
989 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -0700990 }
991 return res;
992}
993
994EGLenum eglQueryAPI(void)
995{
996 clearError();
997
998 if (egl_init_drivers() == EGL_FALSE) {
999 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1000 }
1001
Mathias Agopianada798b2012-02-13 17:09:30 -08001002 egl_connection_t* const cnx = &gEGLImpl;
1003 if (cnx->dso && cnx->egl.eglQueryAPI) {
1004 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001005 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001006
Mathias Agopian518ec112011-05-13 16:21:08 -07001007 // or, it can only be OpenGL ES
1008 return EGL_OPENGL_ES_API;
1009}
1010
1011EGLBoolean eglReleaseThread(void)
1012{
1013 clearError();
1014
1015 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001016 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001017
Mathias Agopianada798b2012-02-13 17:09:30 -08001018 egl_connection_t* const cnx = &gEGLImpl;
1019 if (cnx->dso && cnx->egl.eglReleaseThread) {
1020 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001021 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001022
Mathias Agopian518ec112011-05-13 16:21:08 -07001023 egl_tls_t::clearTLS();
Siva Velusamy0469dd62011-11-30 15:05:37 -08001024#if EGL_TRACE
1025 if (gEGLDebugLevel > 0)
1026 GLTrace_eglReleaseThread();
1027#endif
Mathias Agopian518ec112011-05-13 16:21:08 -07001028 return EGL_TRUE;
1029}
1030
1031EGLSurface eglCreatePbufferFromClientBuffer(
1032 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1033 EGLConfig config, const EGLint *attrib_list)
1034{
1035 clearError();
1036
Jesse Hallb29e5e82012-04-04 16:53:42 -07001037 egl_connection_t* cnx = NULL;
1038 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1039 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001040 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1041 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001042 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001043 }
1044 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1045}
1046
1047// ----------------------------------------------------------------------------
1048// EGL_EGLEXT_VERSION 3
1049// ----------------------------------------------------------------------------
1050
1051EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1052 const EGLint *attrib_list)
1053{
1054 clearError();
1055
Jesse Hallb29e5e82012-04-04 16:53:42 -07001056 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001057 if (!dp) return EGL_FALSE;
1058
Jesse Hallb29e5e82012-04-04 16:53:42 -07001059 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001060 if (!_s.get())
1061 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001062
1063 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001064 if (s->cnx->egl.eglLockSurfaceKHR) {
1065 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001066 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001067 }
1068 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1069}
1070
1071EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1072{
1073 clearError();
1074
Jesse Hallb29e5e82012-04-04 16:53:42 -07001075 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001076 if (!dp) return EGL_FALSE;
1077
Jesse Hallb29e5e82012-04-04 16:53:42 -07001078 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001079 if (!_s.get())
1080 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001081
1082 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001083 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001084 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001085 }
1086 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1087}
1088
1089EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1090 EGLClientBuffer buffer, const EGLint *attrib_list)
1091{
1092 clearError();
1093
Jesse Hallb29e5e82012-04-04 16:53:42 -07001094 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001095 if (!dp) return EGL_NO_IMAGE_KHR;
1096
Jesse Hallb29e5e82012-04-04 16:53:42 -07001097 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001098 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001099
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001100 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1101 egl_connection_t* const cnx = &gEGLImpl;
1102 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1103 result = cnx->egl.eglCreateImageKHR(
1104 dp->disp.dpy,
1105 c ? c->context : EGL_NO_CONTEXT,
1106 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001107 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001108 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001109}
1110
1111EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1112{
1113 clearError();
1114
Jesse Hallb29e5e82012-04-04 16:53:42 -07001115 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001116 if (!dp) return EGL_FALSE;
1117
Mathias Agopianada798b2012-02-13 17:09:30 -08001118 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001119 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1120 cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001121 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001122 return EGL_TRUE;
1123}
1124
1125// ----------------------------------------------------------------------------
1126// EGL_EGLEXT_VERSION 5
1127// ----------------------------------------------------------------------------
1128
1129
1130EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1131{
1132 clearError();
1133
Jesse Hallb29e5e82012-04-04 16:53:42 -07001134 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001135 if (!dp) return EGL_NO_SYNC_KHR;
1136
Mathias Agopian518ec112011-05-13 16:21:08 -07001137 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001138 egl_connection_t* const cnx = &gEGLImpl;
1139 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1140 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001141 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001142 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001143}
1144
1145EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1146{
1147 clearError();
1148
Jesse Hallb29e5e82012-04-04 16:53:42 -07001149 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001150 if (!dp) return EGL_FALSE;
1151
Mathias Agopian518ec112011-05-13 16:21:08 -07001152 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001153 egl_connection_t* const cnx = &gEGLImpl;
1154 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1155 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001156 }
1157 return result;
1158}
1159
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001160EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1161 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001162{
1163 clearError();
1164
Jesse Hallb29e5e82012-04-04 16:53:42 -07001165 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001166 if (!dp) return EGL_FALSE;
1167
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001168 EGLBoolean result = EGL_FALSE;
1169 egl_connection_t* const cnx = &gEGLImpl;
1170 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1171 result = cnx->egl.eglClientWaitSyncKHR(
1172 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001173 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001174 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001175}
1176
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001177EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1178 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001179{
1180 clearError();
1181
Jesse Hallb29e5e82012-04-04 16:53:42 -07001182 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001183 if (!dp) return EGL_FALSE;
1184
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001185 EGLBoolean result = EGL_FALSE;
1186 egl_connection_t* const cnx = &gEGLImpl;
1187 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1188 result = cnx->egl.eglGetSyncAttribKHR(
1189 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001190 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001191 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001192}
1193
1194// ----------------------------------------------------------------------------
1195// ANDROID extensions
1196// ----------------------------------------------------------------------------
1197
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001198/* ANDROID extensions entry-point go here */
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001199
1200// ----------------------------------------------------------------------------
1201// NVIDIA extensions
1202// ----------------------------------------------------------------------------
1203EGLuint64NV eglGetSystemTimeFrequencyNV()
1204{
1205 clearError();
1206
1207 if (egl_init_drivers() == EGL_FALSE) {
1208 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1209 }
1210
1211 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001212 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001213
Mathias Agopianada798b2012-02-13 17:09:30 -08001214 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1215 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001216 }
1217
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001218 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001219}
1220
1221EGLuint64NV eglGetSystemTimeNV()
1222{
1223 clearError();
1224
1225 if (egl_init_drivers() == EGL_FALSE) {
1226 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1227 }
1228
1229 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001230 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001231
Mathias Agopianada798b2012-02-13 17:09:30 -08001232 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1233 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001234 }
1235
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001236 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001237}