blob: 7a84651f63658046b03bb98cd74814bf323f8ff3 [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];
Siva Velusamya73a9772012-12-18 14:56:55 -0800100extern int getEGLDebugLevel();
101extern void setEGLDebugLevel(int level);
Mathias Agopian518ec112011-05-13 16:21:08 -0700102extern gl_hooks_t gHooksTrace;
Mathias Agopian518ec112011-05-13 16:21:08 -0700103} // namespace android;
104
105// ----------------------------------------------------------------------------
106
107static inline void clearError() { egl_tls_t::clearError(); }
108static inline EGLContext getContext() { return egl_tls_t::getContext(); }
109
110// ----------------------------------------------------------------------------
111
112EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
113{
114 clearError();
115
116 uint32_t index = uint32_t(display);
117 if (index >= NUM_DISPLAYS) {
118 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
119 }
120
121 if (egl_init_drivers() == EGL_FALSE) {
122 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
123 }
124
125 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
126 return dpy;
127}
128
129// ----------------------------------------------------------------------------
130// Initialization
131// ----------------------------------------------------------------------------
132
133EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
134{
135 clearError();
136
Jesse Hallb29e5e82012-04-04 16:53:42 -0700137 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700138 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
139
140 EGLBoolean res = dp->initialize(major, minor);
141
142 return res;
143}
144
145EGLBoolean eglTerminate(EGLDisplay dpy)
146{
147 // NOTE: don't unload the drivers b/c some APIs can be called
148 // after eglTerminate() has been called. eglTerminate() only
149 // terminates an EGLDisplay, not a EGL itself.
150
151 clearError();
152
Jesse Hallb29e5e82012-04-04 16:53:42 -0700153 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
155
156 EGLBoolean res = dp->terminate();
157
158 return res;
159}
160
161// ----------------------------------------------------------------------------
162// configuration
163// ----------------------------------------------------------------------------
164
165EGLBoolean eglGetConfigs( EGLDisplay dpy,
166 EGLConfig *configs,
167 EGLint config_size, EGLint *num_config)
168{
169 clearError();
170
Jesse Hallb29e5e82012-04-04 16:53:42 -0700171 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700172 if (!dp) return EGL_FALSE;
173
Mathias Agopian7773c432012-02-13 20:06:08 -0800174 if (num_config==0) {
175 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700176 }
177
Mathias Agopian7773c432012-02-13 20:06:08 -0800178 EGLBoolean res = EGL_FALSE;
179 *num_config = 0;
180
181 egl_connection_t* const cnx = &gEGLImpl;
182 if (cnx->dso) {
183 res = cnx->egl.eglGetConfigs(
184 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700185 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800186
187 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700188}
189
190EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
191 EGLConfig *configs, EGLint config_size,
192 EGLint *num_config)
193{
194 clearError();
195
Jesse Hallb29e5e82012-04-04 16:53:42 -0700196 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700197 if (!dp) return EGL_FALSE;
198
199 if (num_config==0) {
200 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
201 }
202
Mathias Agopian518ec112011-05-13 16:21:08 -0700203 EGLBoolean res = EGL_FALSE;
204 *num_config = 0;
205
Mathias Agopianada798b2012-02-13 17:09:30 -0800206 egl_connection_t* const cnx = &gEGLImpl;
207 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700208 if (attrib_list) {
209 char value[PROPERTY_VALUE_MAX];
210 property_get("debug.egl.force_msaa", value, "false");
211
212 if (!strcmp(value, "true")) {
213 size_t attribCount = 0;
214 EGLint attrib = attrib_list[0];
215
216 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700217 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700218 const EGLint *attribRendererable = NULL;
219 const EGLint *attribCaveat = NULL;
220
221 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700222 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700223 while (attrib != EGL_NONE) {
224 attrib = attrib_list[attribCount];
225 switch (attrib) {
226 case EGL_RENDERABLE_TYPE:
227 attribRendererable = &attrib_list[attribCount];
228 break;
229 case EGL_CONFIG_CAVEAT:
230 attribCaveat = &attrib_list[attribCount];
231 break;
232 }
233 attribCount++;
234 }
235
236 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
237 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
238
239 // Insert 2 extra attributes to force-enable MSAA 4x
240 EGLint aaAttribs[attribCount + 4];
241 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
242 aaAttribs[1] = 1;
243 aaAttribs[2] = EGL_SAMPLES;
244 aaAttribs[3] = 4;
245
246 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
247
248 EGLint numConfigAA;
249 EGLBoolean resAA = cnx->egl.eglChooseConfig(
250 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
251
252 if (resAA == EGL_TRUE && numConfigAA > 0) {
253 ALOGD("Enabling MSAA 4x");
254 *num_config = numConfigAA;
255 return resAA;
256 }
257 }
258 }
259 }
260
Mathias Agopian7773c432012-02-13 20:06:08 -0800261 res = cnx->egl.eglChooseConfig(
262 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700263 }
264 return res;
265}
266
267EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
268 EGLint attribute, EGLint *value)
269{
270 clearError();
271
Jesse Hallb29e5e82012-04-04 16:53:42 -0700272 egl_connection_t* cnx = NULL;
273 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
274 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700275
Mathias Agopian518ec112011-05-13 16:21:08 -0700276 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800277 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700278}
279
280// ----------------------------------------------------------------------------
281// surfaces
282// ----------------------------------------------------------------------------
283
284EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
285 NativeWindowType window,
286 const EGLint *attrib_list)
287{
288 clearError();
289
Jesse Hallb29e5e82012-04-04 16:53:42 -0700290 egl_connection_t* cnx = NULL;
291 egl_display_ptr dp = validate_display_connection(dpy, cnx);
292 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800293 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 EGLint format;
295
Mathias Agopian81a63352011-07-29 17:55:48 -0700296 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000297 ALOGE("EGLNativeWindowType %p already connected to another API",
Mathias Agopian81a63352011-07-29 17:55:48 -0700298 window);
299 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
300 }
301
Mathias Agopian518ec112011-05-13 16:21:08 -0700302 // set the native window's buffers format to match this config
303 if (cnx->egl.eglGetConfigAttrib(iDpy,
Mathias Agopian7773c432012-02-13 20:06:08 -0800304 config, EGL_NATIVE_VISUAL_ID, &format)) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700305 if (format != 0) {
Jamie Gennisbee205f2011-07-01 13:12:07 -0700306 int err = native_window_set_buffers_format(window, format);
307 if (err != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000308 ALOGE("error setting native window pixel format: %s (%d)",
Jamie Gennisbee205f2011-07-01 13:12:07 -0700309 strerror(-err), err);
Mathias Agopian81a63352011-07-29 17:55:48 -0700310 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Jamie Gennisbee205f2011-07-01 13:12:07 -0700311 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
312 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700313 }
314 }
315
Jamie Gennis59769462011-11-19 18:04:43 -0800316 // the EGL spec requires that a new EGLSurface default to swap interval
317 // 1, so explicitly set that on the window here.
318 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
319 anw->setSwapInterval(anw, 1);
320
Mathias Agopian518ec112011-05-13 16:21:08 -0700321 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800322 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700323 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700324 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
325 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700326 return s;
327 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700328
329 // EGLSurface creation failed
330 native_window_set_buffers_format(window, 0);
331 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700332 }
333 return EGL_NO_SURFACE;
334}
335
336EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
337 NativePixmapType pixmap,
338 const EGLint *attrib_list)
339{
340 clearError();
341
Jesse Hallb29e5e82012-04-04 16:53:42 -0700342 egl_connection_t* cnx = NULL;
343 egl_display_ptr dp = validate_display_connection(dpy, cnx);
344 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700345 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800346 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700347 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700348 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
349 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700350 return s;
351 }
352 }
353 return EGL_NO_SURFACE;
354}
355
356EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
357 const EGLint *attrib_list)
358{
359 clearError();
360
Jesse Hallb29e5e82012-04-04 16:53:42 -0700361 egl_connection_t* cnx = NULL;
362 egl_display_ptr dp = validate_display_connection(dpy, cnx);
363 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700364 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800365 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700366 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700367 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
368 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700369 return s;
370 }
371 }
372 return EGL_NO_SURFACE;
373}
374
375EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
376{
377 clearError();
378
Jesse Hallb29e5e82012-04-04 16:53:42 -0700379 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700380 if (!dp) return EGL_FALSE;
381
Jesse Hallb29e5e82012-04-04 16:53:42 -0700382 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700383 if (!_s.get())
384 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700385
386 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800387 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700388 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700389 _s.terminate();
390 }
391 return result;
392}
393
394EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
395 EGLint attribute, EGLint *value)
396{
397 clearError();
398
Jesse Hallb29e5e82012-04-04 16:53:42 -0700399 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700400 if (!dp) return EGL_FALSE;
401
Jesse Hallb29e5e82012-04-04 16:53:42 -0700402 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700403 if (!_s.get())
404 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700405
Mathias Agopian518ec112011-05-13 16:21:08 -0700406 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800407 return s->cnx->egl.eglQuerySurface(
408 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700409}
410
Jamie Gennise8696a42012-01-15 18:54:57 -0800411void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800412 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800413 clearError();
414
Jesse Hallb29e5e82012-04-04 16:53:42 -0700415 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800416 if (!dp) {
417 return;
418 }
419
Jesse Hallb29e5e82012-04-04 16:53:42 -0700420 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800421 if (!_s.get()) {
422 setError(EGL_BAD_SURFACE, EGL_FALSE);
423 return;
424 }
425
426 int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
427
428 egl_surface_t const * const s = get_surface(surface);
429 native_window_set_buffers_timestamp(s->win.get(), timestamp);
430}
431
Mathias Agopian518ec112011-05-13 16:21:08 -0700432// ----------------------------------------------------------------------------
433// Contexts
434// ----------------------------------------------------------------------------
435
436EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
437 EGLContext share_list, const EGLint *attrib_list)
438{
439 clearError();
440
Jesse Hallb29e5e82012-04-04 16:53:42 -0700441 egl_connection_t* cnx = NULL;
442 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
443 if (dpy) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700444 if (share_list != EGL_NO_CONTEXT) {
445 egl_context_t* const c = get_context(share_list);
446 share_list = c->context;
447 }
448 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800449 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700450 if (context != EGL_NO_CONTEXT) {
451 // figure out if it's a GLESv1 or GLESv2
452 int version = 0;
453 if (attrib_list) {
454 while (*attrib_list != EGL_NONE) {
455 GLint attr = *attrib_list++;
456 GLint value = *attrib_list++;
457 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
458 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800459 version = egl_connection_t::GLESv1_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700460 } else if (value == 2) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800461 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700462 }
463 }
464 };
465 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700466 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
467 version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800468#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800469 if (getEGLDebugLevel() > 0)
Siva Velusamy0469dd62011-11-30 15:05:37 -0800470 GLTrace_eglCreateContext(version, c);
471#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700472 return c;
Mathias Agopian500407a2012-09-24 17:57:48 -0700473 } else {
474 EGLint error = eglGetError();
475 ALOGE_IF(error == EGL_SUCCESS,
476 "eglCreateContext(%p, %p, %p, %p) returned EGL_NO_CONTEXT "
477 "but no EGL error!",
478 dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700479 }
480 }
481 return EGL_NO_CONTEXT;
482}
483
484EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
485{
486 clearError();
487
Jesse Hallb29e5e82012-04-04 16:53:42 -0700488 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700489 if (!dp)
490 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700491
Jesse Hallb29e5e82012-04-04 16:53:42 -0700492 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700493 if (!_c.get())
494 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700495
Mathias Agopian518ec112011-05-13 16:21:08 -0700496 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800497 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700498 if (result == EGL_TRUE) {
499 _c.terminate();
500 }
501 return result;
502}
503
Mathias Agopian518ec112011-05-13 16:21:08 -0700504EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
505 EGLSurface read, EGLContext ctx)
506{
507 clearError();
508
Jesse Hallb29e5e82012-04-04 16:53:42 -0700509 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700510 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
511
Mathias Agopian5b287a62011-05-16 18:58:55 -0700512 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
513 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
514 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700515 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
516 (draw != EGL_NO_SURFACE) ) {
517 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
518 }
519
520 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700521 ContextRef _c(dp.get(), ctx);
522 SurfaceRef _d(dp.get(), draw);
523 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700524
525 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700526 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700527 // EGL_NO_CONTEXT is valid
528 return EGL_FALSE;
529 }
530
531 // these are the underlying implementation's object
532 EGLContext impl_ctx = EGL_NO_CONTEXT;
533 EGLSurface impl_draw = EGL_NO_SURFACE;
534 EGLSurface impl_read = EGL_NO_SURFACE;
535
536 // these are our objects structs passed in
537 egl_context_t * c = NULL;
538 egl_surface_t const * d = NULL;
539 egl_surface_t const * r = NULL;
540
541 // these are the current objects structs
542 egl_context_t * cur_c = get_context(getContext());
543
544 if (ctx != EGL_NO_CONTEXT) {
545 c = get_context(ctx);
546 impl_ctx = c->context;
547 } else {
548 // no context given, use the implementation of the current context
549 if (cur_c == NULL) {
550 // no current context
551 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
552 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
553 return setError(EGL_BAD_MATCH, EGL_FALSE);
554 }
555 // not an error, there is just no current context.
556 return EGL_TRUE;
557 }
558 }
559
560 // retrieve the underlying implementation's draw EGLSurface
561 if (draw != EGL_NO_SURFACE) {
562 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700563 impl_draw = d->surface;
564 }
565
566 // retrieve the underlying implementation's read EGLSurface
567 if (read != EGL_NO_SURFACE) {
568 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700569 impl_read = r->surface;
570 }
571
Mathias Agopian518ec112011-05-13 16:21:08 -0700572
Jesse Hallb29e5e82012-04-04 16:53:42 -0700573 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800574 draw, read, ctx,
575 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700576
577 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800578 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700579 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
580 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800581#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800582 if (getEGLDebugLevel() > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800583 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800584#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700585 _c.acquire();
586 _r.acquire();
587 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700588 } else {
589 setGLHooksThreadSpecific(&gHooksNoContext);
590 egl_tls_t::setContext(EGL_NO_CONTEXT);
591 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700592 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000593 // this will ALOGE the error
Mathias Agopian5fecea72011-08-25 18:38:24 -0700594 result = setError(c->cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700595 }
596 return result;
597}
598
599
600EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
601 EGLint attribute, EGLint *value)
602{
603 clearError();
604
Jesse Hallb29e5e82012-04-04 16:53:42 -0700605 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700606 if (!dp) return EGL_FALSE;
607
Jesse Hallb29e5e82012-04-04 16:53:42 -0700608 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700609 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
610
Mathias Agopian518ec112011-05-13 16:21:08 -0700611 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800612 return c->cnx->egl.eglQueryContext(
613 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700614
Mathias Agopian518ec112011-05-13 16:21:08 -0700615}
616
617EGLContext eglGetCurrentContext(void)
618{
619 // could be called before eglInitialize(), but we wouldn't have a context
620 // then, and this function would correctly return EGL_NO_CONTEXT.
621
622 clearError();
623
624 EGLContext ctx = getContext();
625 return ctx;
626}
627
628EGLSurface eglGetCurrentSurface(EGLint readdraw)
629{
630 // could be called before eglInitialize(), but we wouldn't have a context
631 // then, and this function would correctly return EGL_NO_SURFACE.
632
633 clearError();
634
635 EGLContext ctx = getContext();
636 if (ctx) {
637 egl_context_t const * const c = get_context(ctx);
638 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
639 switch (readdraw) {
640 case EGL_READ: return c->read;
641 case EGL_DRAW: return c->draw;
642 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
643 }
644 }
645 return EGL_NO_SURFACE;
646}
647
648EGLDisplay eglGetCurrentDisplay(void)
649{
650 // could be called before eglInitialize(), but we wouldn't have a context
651 // then, and this function would correctly return EGL_NO_DISPLAY.
652
653 clearError();
654
655 EGLContext ctx = getContext();
656 if (ctx) {
657 egl_context_t const * const c = get_context(ctx);
658 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
659 return c->dpy;
660 }
661 return EGL_NO_DISPLAY;
662}
663
664EGLBoolean eglWaitGL(void)
665{
Mathias Agopian518ec112011-05-13 16:21:08 -0700666 clearError();
667
Mathias Agopianada798b2012-02-13 17:09:30 -0800668 egl_connection_t* const cnx = &gEGLImpl;
669 if (!cnx->dso)
670 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
671
672 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700673}
674
675EGLBoolean eglWaitNative(EGLint engine)
676{
Mathias Agopian518ec112011-05-13 16:21:08 -0700677 clearError();
678
Mathias Agopianada798b2012-02-13 17:09:30 -0800679 egl_connection_t* const cnx = &gEGLImpl;
680 if (!cnx->dso)
681 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
682
683 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700684}
685
686EGLint eglGetError(void)
687{
Mathias Agopianada798b2012-02-13 17:09:30 -0800688 EGLint err = EGL_SUCCESS;
689 egl_connection_t* const cnx = &gEGLImpl;
690 if (cnx->dso) {
691 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700692 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800693 if (err == EGL_SUCCESS) {
694 err = egl_tls_t::getError();
695 }
696 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700697}
698
Mathias Agopian518ec112011-05-13 16:21:08 -0700699__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
700{
701 // eglGetProcAddress() could be the very first function called
702 // in which case we must make sure we've initialized ourselves, this
703 // happens the first time egl_get_display() is called.
704
705 clearError();
706
707 if (egl_init_drivers() == EGL_FALSE) {
708 setError(EGL_BAD_PARAMETER, NULL);
709 return NULL;
710 }
711
Jesse Hall25838592012-04-05 15:53:28 -0700712 // These extensions should not be exposed to applications. They're used
713 // internally by the Android EGL layer.
714 if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID") ||
Jamie Gennis331841b2012-09-06 14:52:00 -0700715 !strcmp(procname, "eglDupNativeFenceFDANDROID") ||
Jamie Gennis010dd4f2012-09-09 17:46:17 -0700716 !strcmp(procname, "eglWaitSyncANDROID") ||
Jesse Hall25838592012-04-05 15:53:28 -0700717 !strcmp(procname, "eglHibernateProcessIMG") ||
718 !strcmp(procname, "eglAwakenProcessIMG")) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700719 return NULL;
720 }
721
Mathias Agopian518ec112011-05-13 16:21:08 -0700722 __eglMustCastToProperFunctionPointerType addr;
723 addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
724 if (addr) return addr;
725
Jamie Gennisaca51c02011-11-03 17:42:43 -0700726
Mathias Agopian518ec112011-05-13 16:21:08 -0700727 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
728 pthread_mutex_lock(&sExtensionMapMutex);
729
730 /*
731 * Since eglGetProcAddress() is not associated to anything, it needs
732 * to return a function pointer that "works" regardless of what
733 * the current context is.
734 *
735 * For this reason, we return a "forwarder", a small stub that takes
736 * care of calling the function associated with the context
737 * currently bound.
738 *
739 * We first look for extensions we've already resolved, if we're seeing
740 * this extension for the first time, we go through all our
741 * implementations and call eglGetProcAddress() and record the
742 * result in the appropriate implementation hooks and return the
743 * address of the forwarder corresponding to that hook set.
744 *
745 */
746
747 const String8 name(procname);
748 addr = sGLExtentionMap.valueFor(name);
749 const int slot = sGLExtentionSlot;
750
Steve Blocke6f43dd2012-01-06 19:20:56 +0000751 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700752 "no more slots for eglGetProcAddress(\"%s\")",
753 procname);
754
Siva Velusamy0469dd62011-11-30 15:05:37 -0800755#if EGL_TRACE
756 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
757#endif
758
Mathias Agopian518ec112011-05-13 16:21:08 -0700759 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
760 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800761
762 egl_connection_t* const cnx = &gEGLImpl;
763 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800764 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800765 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800766 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
767 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700768#if EGL_TRACE
Mathias Agopianada798b2012-02-13 17:09:30 -0800769 debugHooks->ext.extensions[slot] =
770 gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700771#endif
Mathias Agopianada798b2012-02-13 17:09:30 -0800772 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800773 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700774 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800775
Mathias Agopian518ec112011-05-13 16:21:08 -0700776 if (found) {
luliuhui69d10072012-08-30 11:15:36 +0800777#if USE_FAST_TLS_KEY
Mathias Agopian518ec112011-05-13 16:21:08 -0700778 addr = gExtensionForwarders[slot];
luliuhui69d10072012-08-30 11:15:36 +0800779#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700780 sGLExtentionMap.add(name, addr);
781 sGLExtentionSlot++;
782 }
783 }
784
785 pthread_mutex_unlock(&sExtensionMapMutex);
786 return addr;
787}
788
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700789class FrameCompletionThread : public Thread {
790public:
791
792 static void queueSync(EGLSyncKHR sync) {
793 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
794 static bool running = false;
795 if (!running) {
796 thread->run("GPUFrameCompletion");
797 running = true;
798 }
799 {
800 Mutex::Autolock lock(thread->mMutex);
801 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
802 thread->mFramesQueued).string());
803 thread->mQueue.push_back(sync);
804 thread->mCondition.signal();
805 thread->mFramesQueued++;
806 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
807 }
808 }
809
810private:
811 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
812
813 virtual bool threadLoop() {
814 EGLSyncKHR sync;
815 uint32_t frameNum;
816 {
817 Mutex::Autolock lock(mMutex);
818 while (mQueue.isEmpty()) {
819 mCondition.wait(mMutex);
820 }
821 sync = mQueue[0];
822 frameNum = mFramesCompleted;
823 }
824 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
825 {
826 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
827 frameNum).string());
828 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
829 if (result == EGL_FALSE) {
830 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
831 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
832 ALOGE("FrameCompletion: timeout waiting for fence");
833 }
834 eglDestroySyncKHR(dpy, sync);
835 }
836 {
837 Mutex::Autolock lock(mMutex);
838 mQueue.removeAt(0);
839 mFramesCompleted++;
840 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
841 }
842 return true;
843 }
844
845 uint32_t mFramesQueued;
846 uint32_t mFramesCompleted;
847 Vector<EGLSyncKHR> mQueue;
848 Condition mCondition;
849 Mutex mMutex;
850};
851
Mathias Agopian518ec112011-05-13 16:21:08 -0700852EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
853{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800854 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700855 clearError();
856
Jesse Hallb29e5e82012-04-04 16:53:42 -0700857 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700858 if (!dp) return EGL_FALSE;
859
Jesse Hallb29e5e82012-04-04 16:53:42 -0700860 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700861 if (!_s.get())
862 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700863
Siva Velusamy0469dd62011-11-30 15:05:37 -0800864#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800865 gl_hooks_t const *trace_hooks = getGLTraceThreadSpecific();
866 if (getEGLDebugLevel() > 0) {
867 if (trace_hooks == NULL) {
868 if (GLTrace_start() < 0) {
869 ALOGE("Disabling Tracer for OpenGL ES");
870 setEGLDebugLevel(0);
871 } else {
872 // switch over to the trace version of hooks
873 EGLContext ctx = egl_tls_t::getContext();
874 egl_context_t * const c = get_context(ctx);
875 if (c) {
876 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
877 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
878 }
879 }
880 }
881
Siva Velusamy0469dd62011-11-30 15:05:37 -0800882 GLTrace_eglSwapBuffers(dpy, draw);
Siva Velusamya73a9772012-12-18 14:56:55 -0800883 } else if (trace_hooks != NULL) {
884 // tracing is now disabled, so switch back to the non trace version
885 EGLContext ctx = egl_tls_t::getContext();
886 egl_context_t * const c = get_context(ctx);
887 if (c) setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
888 GLTrace_stop();
889 }
Siva Velusamy0469dd62011-11-30 15:05:37 -0800890#endif
891
Mathias Agopian518ec112011-05-13 16:21:08 -0700892 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -0700893
894 if (CC_UNLIKELY(dp->finishOnSwap)) {
895 uint32_t pixel;
896 egl_context_t * const c = get_context( egl_tls_t::getContext() );
897 if (c) {
898 // glReadPixels() ensures that the frame is complete
899 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
900 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
901 }
902 }
903
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700904 EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
905
906 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
907 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
908 {
909 sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
910 }
911 if (sync != EGL_NO_SYNC_KHR) {
912 FrameCompletionThread::queueSync(sync);
913 }
914 }
915
916 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -0700917}
918
919EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
920 NativePixmapType target)
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);
Mathias Agopianada798b2012-02-13 17:09:30 -0800932 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -0700933}
934
935const char* eglQueryString(EGLDisplay dpy, EGLint name)
936{
937 clearError();
938
Jesse Hallb29e5e82012-04-04 16:53:42 -0700939 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700940 if (!dp) return (const char *) NULL;
941
942 switch (name) {
943 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800944 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700945 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800946 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700947 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800948 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -0700949 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800950 return dp->getClientApiString();
Mathias Agopianada798b2012-02-13 17:09:30 -0800951 case EGL_VERSION_HW_ANDROID:
952 return dp->disp.queryString.version;
Mathias Agopian518ec112011-05-13 16:21:08 -0700953 }
954 return setError(EGL_BAD_PARAMETER, (const char *)0);
955}
956
957
958// ----------------------------------------------------------------------------
959// EGL 1.1
960// ----------------------------------------------------------------------------
961
962EGLBoolean eglSurfaceAttrib(
963 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
964{
965 clearError();
966
Jesse Hallb29e5e82012-04-04 16:53:42 -0700967 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700968 if (!dp) return EGL_FALSE;
969
Jesse Hallb29e5e82012-04-04 16:53:42 -0700970 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700971 if (!_s.get())
972 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700973
Mathias Agopian518ec112011-05-13 16:21:08 -0700974 egl_surface_t const * const s = get_surface(surface);
975 if (s->cnx->egl.eglSurfaceAttrib) {
976 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -0800977 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700978 }
979 return setError(EGL_BAD_SURFACE, EGL_FALSE);
980}
981
982EGLBoolean eglBindTexImage(
983 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
984{
985 clearError();
986
Jesse Hallb29e5e82012-04-04 16:53:42 -0700987 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700988 if (!dp) return EGL_FALSE;
989
Jesse Hallb29e5e82012-04-04 16:53:42 -0700990 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700991 if (!_s.get())
992 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700993
Mathias Agopian518ec112011-05-13 16:21:08 -0700994 egl_surface_t const * const s = get_surface(surface);
995 if (s->cnx->egl.eglBindTexImage) {
996 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -0800997 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -0700998 }
999 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1000}
1001
1002EGLBoolean eglReleaseTexImage(
1003 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1004{
1005 clearError();
1006
Jesse Hallb29e5e82012-04-04 16:53:42 -07001007 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001008 if (!dp) return EGL_FALSE;
1009
Jesse Hallb29e5e82012-04-04 16:53:42 -07001010 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001011 if (!_s.get())
1012 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001013
Mathias Agopian518ec112011-05-13 16:21:08 -07001014 egl_surface_t const * const s = get_surface(surface);
1015 if (s->cnx->egl.eglReleaseTexImage) {
1016 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001017 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001018 }
1019 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1020}
1021
1022EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1023{
1024 clearError();
1025
Jesse Hallb29e5e82012-04-04 16:53:42 -07001026 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001027 if (!dp) return EGL_FALSE;
1028
1029 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001030 egl_connection_t* const cnx = &gEGLImpl;
1031 if (cnx->dso && cnx->egl.eglSwapInterval) {
1032 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001033 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001034
Mathias Agopian518ec112011-05-13 16:21:08 -07001035 return res;
1036}
1037
1038
1039// ----------------------------------------------------------------------------
1040// EGL 1.2
1041// ----------------------------------------------------------------------------
1042
1043EGLBoolean eglWaitClient(void)
1044{
1045 clearError();
1046
Mathias Agopianada798b2012-02-13 17:09:30 -08001047 egl_connection_t* const cnx = &gEGLImpl;
1048 if (!cnx->dso)
1049 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1050
1051 EGLBoolean res;
1052 if (cnx->egl.eglWaitClient) {
1053 res = cnx->egl.eglWaitClient();
1054 } else {
1055 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001056 }
1057 return res;
1058}
1059
1060EGLBoolean eglBindAPI(EGLenum api)
1061{
1062 clearError();
1063
1064 if (egl_init_drivers() == EGL_FALSE) {
1065 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1066 }
1067
1068 // bind this API on all EGLs
1069 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001070 egl_connection_t* const cnx = &gEGLImpl;
1071 if (cnx->dso && cnx->egl.eglBindAPI) {
1072 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001073 }
1074 return res;
1075}
1076
1077EGLenum eglQueryAPI(void)
1078{
1079 clearError();
1080
1081 if (egl_init_drivers() == EGL_FALSE) {
1082 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1083 }
1084
Mathias Agopianada798b2012-02-13 17:09:30 -08001085 egl_connection_t* const cnx = &gEGLImpl;
1086 if (cnx->dso && cnx->egl.eglQueryAPI) {
1087 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001088 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001089
Mathias Agopian518ec112011-05-13 16:21:08 -07001090 // or, it can only be OpenGL ES
1091 return EGL_OPENGL_ES_API;
1092}
1093
1094EGLBoolean eglReleaseThread(void)
1095{
1096 clearError();
1097
1098 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001099 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001100
Mathias Agopianada798b2012-02-13 17:09:30 -08001101 egl_connection_t* const cnx = &gEGLImpl;
1102 if (cnx->dso && cnx->egl.eglReleaseThread) {
1103 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001104 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001105
Mathias Agopian518ec112011-05-13 16:21:08 -07001106 egl_tls_t::clearTLS();
Siva Velusamy0469dd62011-11-30 15:05:37 -08001107#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -08001108 if (getEGLDebugLevel() > 0)
Siva Velusamy0469dd62011-11-30 15:05:37 -08001109 GLTrace_eglReleaseThread();
1110#endif
Mathias Agopian518ec112011-05-13 16:21:08 -07001111 return EGL_TRUE;
1112}
1113
1114EGLSurface eglCreatePbufferFromClientBuffer(
1115 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1116 EGLConfig config, const EGLint *attrib_list)
1117{
1118 clearError();
1119
Jesse Hallb29e5e82012-04-04 16:53:42 -07001120 egl_connection_t* cnx = NULL;
1121 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1122 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001123 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1124 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001125 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001126 }
1127 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1128}
1129
1130// ----------------------------------------------------------------------------
1131// EGL_EGLEXT_VERSION 3
1132// ----------------------------------------------------------------------------
1133
1134EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1135 const EGLint *attrib_list)
1136{
1137 clearError();
1138
Jesse Hallb29e5e82012-04-04 16:53:42 -07001139 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001140 if (!dp) return EGL_FALSE;
1141
Jesse Hallb29e5e82012-04-04 16:53:42 -07001142 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001143 if (!_s.get())
1144 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001145
1146 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001147 if (s->cnx->egl.eglLockSurfaceKHR) {
1148 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001149 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001150 }
1151 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1152}
1153
1154EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1155{
1156 clearError();
1157
Jesse Hallb29e5e82012-04-04 16:53:42 -07001158 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001159 if (!dp) return EGL_FALSE;
1160
Jesse Hallb29e5e82012-04-04 16:53:42 -07001161 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001162 if (!_s.get())
1163 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001164
1165 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001166 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001167 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001168 }
1169 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1170}
1171
1172EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1173 EGLClientBuffer buffer, const EGLint *attrib_list)
1174{
1175 clearError();
1176
Jesse Hallb29e5e82012-04-04 16:53:42 -07001177 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001178 if (!dp) return EGL_NO_IMAGE_KHR;
1179
Jesse Hallb29e5e82012-04-04 16:53:42 -07001180 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001181 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001182
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001183 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1184 egl_connection_t* const cnx = &gEGLImpl;
1185 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1186 result = cnx->egl.eglCreateImageKHR(
1187 dp->disp.dpy,
1188 c ? c->context : EGL_NO_CONTEXT,
1189 target, buffer, attrib_list);
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
1194EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1195{
1196 clearError();
1197
Jesse Hallb29e5e82012-04-04 16:53:42 -07001198 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001199 if (!dp) return EGL_FALSE;
1200
Steven Holte646a5c52012-06-04 20:02:11 -07001201 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001202 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001203 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001204 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001205 }
Steven Holte646a5c52012-06-04 20:02:11 -07001206 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001207}
1208
1209// ----------------------------------------------------------------------------
1210// EGL_EGLEXT_VERSION 5
1211// ----------------------------------------------------------------------------
1212
1213
1214EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1215{
1216 clearError();
1217
Jesse Hallb29e5e82012-04-04 16:53:42 -07001218 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001219 if (!dp) return EGL_NO_SYNC_KHR;
1220
Mathias Agopian518ec112011-05-13 16:21:08 -07001221 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001222 egl_connection_t* const cnx = &gEGLImpl;
1223 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1224 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001225 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001226 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001227}
1228
1229EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1230{
1231 clearError();
1232
Jesse Hallb29e5e82012-04-04 16:53:42 -07001233 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001234 if (!dp) return EGL_FALSE;
1235
Mathias Agopian518ec112011-05-13 16:21:08 -07001236 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001237 egl_connection_t* const cnx = &gEGLImpl;
1238 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1239 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001240 }
1241 return result;
1242}
1243
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001244EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1245 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001246{
1247 clearError();
1248
Jesse Hallb29e5e82012-04-04 16:53:42 -07001249 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001250 if (!dp) return EGL_FALSE;
1251
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001252 EGLBoolean result = EGL_FALSE;
1253 egl_connection_t* const cnx = &gEGLImpl;
1254 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1255 result = cnx->egl.eglClientWaitSyncKHR(
1256 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001257 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001258 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001259}
1260
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001261EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1262 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001263{
1264 clearError();
1265
Jesse Hallb29e5e82012-04-04 16:53:42 -07001266 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001267 if (!dp) return EGL_FALSE;
1268
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001269 EGLBoolean result = EGL_FALSE;
1270 egl_connection_t* const cnx = &gEGLImpl;
1271 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1272 result = cnx->egl.eglGetSyncAttribKHR(
1273 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001274 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001275 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001276}
1277
1278// ----------------------------------------------------------------------------
1279// ANDROID extensions
1280// ----------------------------------------------------------------------------
1281
Jamie Gennis331841b2012-09-06 14:52:00 -07001282EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1283{
1284 clearError();
1285
1286 const egl_display_ptr dp = validate_display(dpy);
1287 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1288
1289 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1290 egl_connection_t* const cnx = &gEGLImpl;
1291 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1292 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1293 }
1294 return result;
1295}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001296
Jamie Gennis010dd4f2012-09-09 17:46:17 -07001297EGLint eglWaitSyncANDROID(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags)
1298{
1299 clearError();
1300
1301 const egl_display_ptr dp = validate_display(dpy);
1302 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1303
1304 EGLint result = EGL_FALSE;
1305 egl_connection_t* const cnx = &gEGLImpl;
1306 if (cnx->dso && cnx->egl.eglWaitSyncANDROID) {
1307 result = cnx->egl.eglWaitSyncANDROID(dp->disp.dpy, sync, flags);
1308 }
1309 return result;
1310}
1311
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001312// ----------------------------------------------------------------------------
1313// NVIDIA extensions
1314// ----------------------------------------------------------------------------
1315EGLuint64NV eglGetSystemTimeFrequencyNV()
1316{
1317 clearError();
1318
1319 if (egl_init_drivers() == EGL_FALSE) {
1320 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1321 }
1322
1323 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001324 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001325
Mathias Agopianada798b2012-02-13 17:09:30 -08001326 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1327 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001328 }
1329
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001330 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001331}
1332
1333EGLuint64NV eglGetSystemTimeNV()
1334{
1335 clearError();
1336
1337 if (egl_init_drivers() == EGL_FALSE) {
1338 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1339 }
1340
1341 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001342 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001343
Mathias Agopianada798b2012-02-13 17:09:30 -08001344 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1345 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001346 }
1347
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001348 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001349}