blob: 8dd052cb686be5945841f5765add33d897d65300 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** 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
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** 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
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Jesse Hallc07b5202013-07-04 12:08:16 -070019#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070020#include <ctype.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include <hardware/gralloc.h>
25#include <system/window.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
30#include <cutils/log.h>
31#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070033#include <cutils/properties.h>
34#include <cutils/memory.h>
35
36#include <utils/KeyedVector.h>
37#include <utils/SortedVector.h>
38#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080039#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070040
Mathias Agopian39c24a22013-04-04 23:17:56 -070041#include "../egl_impl.h"
42#include "../glestrace.h"
43#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070044
45#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070046#include "egl_object.h"
47#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080048#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070049
50using namespace android;
51
Jesse Halla2ba4282013-09-14 21:00:14 -070052// This extension has not been ratified yet, so can't be shipped.
53// Implementation is incomplete and untested.
54#define ENABLE_EGL_KHR_GL_COLORSPACE 0
55
Mathias Agopian518ec112011-05-13 16:21:08 -070056// ----------------------------------------------------------------------------
57
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070058namespace android {
59
Mathias Agopian518ec112011-05-13 16:21:08 -070060struct extention_map_t {
61 const char* name;
62 __eglMustCastToProperFunctionPointerType address;
63};
64
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070065/*
Jesse Hall21558da2013-08-06 15:31:22 -070066 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070067 *
Jesse Hall21558da2013-08-06 15:31:22 -070068 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
69 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070070 *
Jesse Hall21558da2013-08-06 15:31:22 -070071 * The rest (gExtensionString) depend on support in the EGL driver, and are
72 * only available if the driver supports them. However, some of these must be
73 * supported because they are used by the Android system itself; these are
74 * listd as mandatory below and are required by the CDD. The system *assumes*
75 * the mandatory extensions are present and may not function properly if some
76 * are missing.
77 *
78 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070079 */
Jesse Hall21558da2013-08-06 15:31:22 -070080extern char const * const gBuiltinExtensionString =
81 "EGL_KHR_get_all_proc_addresses "
82 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080083 "EGL_KHR_swap_buffers_with_damage "
Jesse Hall21558da2013-08-06 15:31:22 -070084 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070085extern char const * const gExtensionString =
86 "EGL_KHR_image " // mandatory
87 "EGL_KHR_image_base " // mandatory
88 "EGL_KHR_image_pixmap "
89 "EGL_KHR_lock_surface "
Jesse Halla2ba4282013-09-14 21:00:14 -070090#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
Jesse Hallc2e41222013-08-08 13:40:22 -070091 "EGL_KHR_gl_colorspace "
Jesse Halla2ba4282013-09-14 21:00:14 -070092#endif
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070093 "EGL_KHR_gl_texture_2D_image "
94 "EGL_KHR_gl_texture_cubemap_image "
95 "EGL_KHR_gl_renderbuffer_image "
96 "EGL_KHR_reusable_sync "
97 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -070098 "EGL_KHR_create_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070099 "EGL_EXT_create_context_robustness "
100 "EGL_NV_system_time "
101 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700102 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700103 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800104 "EGL_KHR_partial_update " // strongly recommended
105 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700106 "EGL_KHR_create_context_no_error "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700107 ;
108
109// extensions not exposed to applications but used by the ANDROID system
110// "EGL_ANDROID_blob_cache " // strongly recommended
111// "EGL_IMG_hibernate_process " // optional
112// "EGL_ANDROID_native_fence_sync " // strongly recommended
113// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700114// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700115
116/*
117 * EGL Extensions entry-points exposed to 3rd party applications
118 * (keep in sync with gExtensionString above)
119 *
120 */
121static const extention_map_t sExtensionMap[] = {
122 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700123 { "eglLockSurfaceKHR",
124 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
125 { "eglUnlockSurfaceKHR",
126 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700127
128 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700129 { "eglCreateImageKHR",
130 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
131 { "eglDestroyImageKHR",
132 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700133
134 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
135 { "eglCreateSyncKHR",
136 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
137 { "eglDestroySyncKHR",
138 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
139 { "eglClientWaitSyncKHR",
140 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
141 { "eglSignalSyncKHR",
142 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
143 { "eglGetSyncAttribKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
145
146 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800147 { "eglGetSystemTimeFrequencyNV",
148 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
149 { "eglGetSystemTimeNV",
150 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700151
Mathias Agopian2bb71682013-03-27 17:32:41 -0700152 // EGL_KHR_wait_sync
153 { "eglWaitSyncKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700155
156 // EGL_ANDROID_presentation_time
157 { "eglPresentationTimeANDROID",
158 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800159
160 // EGL_KHR_swap_buffers_with_damage
161 { "eglSwapBuffersWithDamageKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
163
164 // EGL_KHR_partial_update
165 { "eglSetDamageRegionKHR",
166 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Mathias Agopian518ec112011-05-13 16:21:08 -0700167};
168
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700169/*
170 * These extensions entry-points should not be exposed to applications.
171 * They're used internally by the Android EGL layer.
172 */
173#define FILTER_EXTENSIONS(procname) \
174 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
175 !strcmp((procname), "eglHibernateProcessIMG") || \
176 !strcmp((procname), "eglAwakenProcessIMG") || \
177 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
178
179
180
Mathias Agopian518ec112011-05-13 16:21:08 -0700181// accesses protected by sExtensionMapMutex
182static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
183static int sGLExtentionSlot = 0;
184static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
185
186static void(*findProcAddress(const char* name,
187 const extention_map_t* map, size_t n))() {
188 for (uint32_t i=0 ; i<n ; i++) {
189 if (!strcmp(name, map[i].name)) {
190 return map[i].address;
191 }
192 }
193 return NULL;
194}
195
196// ----------------------------------------------------------------------------
197
Mathias Agopian518ec112011-05-13 16:21:08 -0700198extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
199extern EGLBoolean egl_init_drivers();
200extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Siva Velusamya73a9772012-12-18 14:56:55 -0800201extern int getEGLDebugLevel();
202extern void setEGLDebugLevel(int level);
Mathias Agopian518ec112011-05-13 16:21:08 -0700203extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700204
Mathias Agopian518ec112011-05-13 16:21:08 -0700205} // namespace android;
206
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700207
Mathias Agopian518ec112011-05-13 16:21:08 -0700208// ----------------------------------------------------------------------------
209
210static inline void clearError() { egl_tls_t::clearError(); }
211static inline EGLContext getContext() { return egl_tls_t::getContext(); }
212
213// ----------------------------------------------------------------------------
214
215EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
216{
217 clearError();
218
Dan Stozac3289c42014-01-17 11:38:34 -0800219 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700220 if (index >= NUM_DISPLAYS) {
221 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
222 }
223
224 if (egl_init_drivers() == EGL_FALSE) {
225 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
226 }
227
228 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
229 return dpy;
230}
231
232// ----------------------------------------------------------------------------
233// Initialization
234// ----------------------------------------------------------------------------
235
236EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
237{
238 clearError();
239
Jesse Hallb29e5e82012-04-04 16:53:42 -0700240 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700241 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
242
243 EGLBoolean res = dp->initialize(major, minor);
244
245 return res;
246}
247
248EGLBoolean eglTerminate(EGLDisplay dpy)
249{
250 // NOTE: don't unload the drivers b/c some APIs can be called
251 // after eglTerminate() has been called. eglTerminate() only
252 // terminates an EGLDisplay, not a EGL itself.
253
254 clearError();
255
Jesse Hallb29e5e82012-04-04 16:53:42 -0700256 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700257 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
258
259 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800260
Mathias Agopian518ec112011-05-13 16:21:08 -0700261 return res;
262}
263
264// ----------------------------------------------------------------------------
265// configuration
266// ----------------------------------------------------------------------------
267
268EGLBoolean eglGetConfigs( EGLDisplay dpy,
269 EGLConfig *configs,
270 EGLint config_size, EGLint *num_config)
271{
272 clearError();
273
Jesse Hallb29e5e82012-04-04 16:53:42 -0700274 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700275 if (!dp) return EGL_FALSE;
276
Mathias Agopian7773c432012-02-13 20:06:08 -0800277 if (num_config==0) {
278 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700279 }
280
Mathias Agopian7773c432012-02-13 20:06:08 -0800281 EGLBoolean res = EGL_FALSE;
282 *num_config = 0;
283
284 egl_connection_t* const cnx = &gEGLImpl;
285 if (cnx->dso) {
286 res = cnx->egl.eglGetConfigs(
287 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700288 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800289
290 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700291}
292
293EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
294 EGLConfig *configs, EGLint config_size,
295 EGLint *num_config)
296{
297 clearError();
298
Jesse Hallb29e5e82012-04-04 16:53:42 -0700299 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700300 if (!dp) return EGL_FALSE;
301
302 if (num_config==0) {
303 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
304 }
305
Mathias Agopian518ec112011-05-13 16:21:08 -0700306 EGLBoolean res = EGL_FALSE;
307 *num_config = 0;
308
Mathias Agopianada798b2012-02-13 17:09:30 -0800309 egl_connection_t* const cnx = &gEGLImpl;
310 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700311 if (attrib_list) {
312 char value[PROPERTY_VALUE_MAX];
313 property_get("debug.egl.force_msaa", value, "false");
314
315 if (!strcmp(value, "true")) {
316 size_t attribCount = 0;
317 EGLint attrib = attrib_list[0];
318
319 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700320 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700321 const EGLint *attribRendererable = NULL;
322 const EGLint *attribCaveat = NULL;
323
324 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700325 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700326 while (attrib != EGL_NONE) {
327 attrib = attrib_list[attribCount];
328 switch (attrib) {
329 case EGL_RENDERABLE_TYPE:
330 attribRendererable = &attrib_list[attribCount];
331 break;
332 case EGL_CONFIG_CAVEAT:
333 attribCaveat = &attrib_list[attribCount];
334 break;
335 }
336 attribCount++;
337 }
338
339 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
340 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800341
Romain Guy1cffc802012-10-15 18:13:05 -0700342 // Insert 2 extra attributes to force-enable MSAA 4x
343 EGLint aaAttribs[attribCount + 4];
344 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
345 aaAttribs[1] = 1;
346 aaAttribs[2] = EGL_SAMPLES;
347 aaAttribs[3] = 4;
348
349 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
350
351 EGLint numConfigAA;
352 EGLBoolean resAA = cnx->egl.eglChooseConfig(
353 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
354
355 if (resAA == EGL_TRUE && numConfigAA > 0) {
356 ALOGD("Enabling MSAA 4x");
357 *num_config = numConfigAA;
358 return resAA;
359 }
360 }
361 }
362 }
363
Mathias Agopian7773c432012-02-13 20:06:08 -0800364 res = cnx->egl.eglChooseConfig(
365 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700366 }
367 return res;
368}
369
370EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
371 EGLint attribute, EGLint *value)
372{
373 clearError();
374
Jesse Hallb29e5e82012-04-04 16:53:42 -0700375 egl_connection_t* cnx = NULL;
376 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
377 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800378
Mathias Agopian518ec112011-05-13 16:21:08 -0700379 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800380 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700381}
382
383// ----------------------------------------------------------------------------
384// surfaces
385// ----------------------------------------------------------------------------
386
Jesse Halla2ba4282013-09-14 21:00:14 -0700387// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
Jesse Hallc2e41222013-08-08 13:40:22 -0700388// been added to the Khronos egl.h.
389#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE
390#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB
391#define EGL_GL_COLORSPACE_LINEAR_KHR EGL_VG_COLORSPACE_LINEAR
392
393// Turn linear formats into corresponding sRGB formats when colorspace is
394// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
395// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800396// the modification isn't possible, the original dataSpace is returned.
397static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
398 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700399 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800400 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700401 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800402 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700403 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800404 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700405}
406
Mathias Agopian518ec112011-05-13 16:21:08 -0700407EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
408 NativeWindowType window,
409 const EGLint *attrib_list)
410{
411 clearError();
412
Jesse Hallb29e5e82012-04-04 16:53:42 -0700413 egl_connection_t* cnx = NULL;
414 egl_display_ptr dp = validate_display_connection(dpy, cnx);
415 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800416 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700417
Andy McFaddend566ce32014-01-07 15:54:17 -0800418 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
419 if (result != OK) {
420 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
421 "failed (%#x) (already connected to another API?)",
422 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700423 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700424 }
425
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700426 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700427 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
428 // of our native format. So if sRGB gamma is requested, we have to
429 // modify the EGLconfig's format before setting the native window's
430 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800431
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700432 // by default, just pick RGBA_8888
433 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800434 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700435
436 EGLint a = 0;
437 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
438 if (a > 0) {
439 // alpha-channel requested, there's really only one suitable format
440 format = HAL_PIXEL_FORMAT_RGBA_8888;
441 } else {
442 EGLint r, g, b;
443 r = g = b = 0;
444 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
445 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
446 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
447 EGLint colorDepth = r + g + b;
448 if (colorDepth <= 16) {
449 format = HAL_PIXEL_FORMAT_RGB_565;
450 } else {
451 format = HAL_PIXEL_FORMAT_RGBX_8888;
452 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700453 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700454
455 // now select a corresponding sRGB format if needed
456 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
457 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
458 if (*attr == EGL_GL_COLORSPACE_KHR) {
Jesse Halla2ba4282013-09-14 21:00:14 -0700459 if (ENABLE_EGL_KHR_GL_COLORSPACE) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800460 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jesse Halla2ba4282013-09-14 21:00:14 -0700461 } else {
462 // Normally we'd pass through unhandled attributes to
463 // the driver. But in case the driver implements this
464 // extension but we're disabling it, we want to prevent
465 // it getting through -- support will be broken without
466 // our help.
467 ALOGE("sRGB window surfaces not supported");
468 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
469 }
Jamie Gennisbee205f2011-07-01 13:12:07 -0700470 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700471 }
472 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800473
Jesse Hallc2e41222013-08-08 13:40:22 -0700474 if (format != 0) {
475 int err = native_window_set_buffers_format(window, format);
476 if (err != 0) {
477 ALOGE("error setting native window pixel format: %s (%d)",
478 strerror(-err), err);
479 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
480 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
481 }
482 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700483
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800484 if (dataSpace != 0) {
485 int err = native_window_set_buffers_data_space(window, dataSpace);
486 if (err != 0) {
487 ALOGE("error setting native window pixel dataSpace: %s (%d)",
488 strerror(-err), err);
489 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
490 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
491 }
492 }
493
Jamie Gennis59769462011-11-19 18:04:43 -0800494 // the EGL spec requires that a new EGLSurface default to swap interval
495 // 1, so explicitly set that on the window here.
496 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
497 anw->setSwapInterval(anw, 1);
498
Mathias Agopian518ec112011-05-13 16:21:08 -0700499 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800500 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700501 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700502 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
503 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700504 return s;
505 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700506
507 // EGLSurface creation failed
508 native_window_set_buffers_format(window, 0);
509 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700510 }
511 return EGL_NO_SURFACE;
512}
513
514EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
515 NativePixmapType pixmap,
516 const EGLint *attrib_list)
517{
518 clearError();
519
Jesse Hallb29e5e82012-04-04 16:53:42 -0700520 egl_connection_t* cnx = NULL;
521 egl_display_ptr dp = validate_display_connection(dpy, cnx);
522 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700523 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800524 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700525 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700526 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
527 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700528 return s;
529 }
530 }
531 return EGL_NO_SURFACE;
532}
533
534EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
535 const EGLint *attrib_list)
536{
537 clearError();
538
Jesse Hallb29e5e82012-04-04 16:53:42 -0700539 egl_connection_t* cnx = NULL;
540 egl_display_ptr dp = validate_display_connection(dpy, cnx);
541 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700542 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800543 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700544 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700545 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
546 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700547 return s;
548 }
549 }
550 return EGL_NO_SURFACE;
551}
Jesse Hall47743382013-02-08 11:13:46 -0800552
Mathias Agopian518ec112011-05-13 16:21:08 -0700553EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
554{
555 clearError();
556
Jesse Hallb29e5e82012-04-04 16:53:42 -0700557 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700558 if (!dp) return EGL_FALSE;
559
Jesse Hallb29e5e82012-04-04 16:53:42 -0700560 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700561 if (!_s.get())
562 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700563
564 egl_surface_t * const s = get_surface(surface);
Amith Dsouza4f21a4c2015-06-30 22:54:16 -0700565 ANativeWindow* window = s->win.get();
566 if (window) {
567 int result = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
568 if (result != OK) {
569 ALOGE("eglDestroySurface: native_window_api_disconnect (win=%p) "
570 "failed (%#x)",
571 window, result);
572 }
573 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800574 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700576 _s.terminate();
577 }
578 return result;
579}
580
581EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
582 EGLint attribute, EGLint *value)
583{
584 clearError();
585
Jesse Hallb29e5e82012-04-04 16:53:42 -0700586 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700587 if (!dp) return EGL_FALSE;
588
Jesse Hallb29e5e82012-04-04 16:53:42 -0700589 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700590 if (!_s.get())
591 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700592
Mathias Agopian518ec112011-05-13 16:21:08 -0700593 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800594 return s->cnx->egl.eglQuerySurface(
595 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700596}
597
Jamie Gennise8696a42012-01-15 18:54:57 -0800598void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800599 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800600 clearError();
601
Jesse Hallb29e5e82012-04-04 16:53:42 -0700602 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800603 if (!dp) {
604 return;
605 }
606
Jesse Hallb29e5e82012-04-04 16:53:42 -0700607 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800608 if (!_s.get()) {
609 setError(EGL_BAD_SURFACE, EGL_FALSE);
610 return;
611 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800612}
613
Mathias Agopian518ec112011-05-13 16:21:08 -0700614// ----------------------------------------------------------------------------
615// Contexts
616// ----------------------------------------------------------------------------
617
618EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
619 EGLContext share_list, const EGLint *attrib_list)
620{
621 clearError();
622
Jesse Hallb29e5e82012-04-04 16:53:42 -0700623 egl_connection_t* cnx = NULL;
624 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700625 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700626 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700627 if (!ContextRef(dp.get(), share_list).get()) {
628 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
629 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700630 egl_context_t* const c = get_context(share_list);
631 share_list = c->context;
632 }
633 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800634 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700635 if (context != EGL_NO_CONTEXT) {
636 // figure out if it's a GLESv1 or GLESv2
637 int version = 0;
638 if (attrib_list) {
639 while (*attrib_list != EGL_NONE) {
640 GLint attr = *attrib_list++;
641 GLint value = *attrib_list++;
642 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
643 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800644 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800645 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800646 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700647 }
648 }
649 };
650 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700651 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
652 version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800653#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800654 if (getEGLDebugLevel() > 0)
Siva Velusamy0469dd62011-11-30 15:05:37 -0800655 GLTrace_eglCreateContext(version, c);
656#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700657 return c;
658 }
659 }
660 return EGL_NO_CONTEXT;
661}
662
663EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
664{
665 clearError();
666
Jesse Hallb29e5e82012-04-04 16:53:42 -0700667 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700668 if (!dp)
669 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700670
Jesse Hallb29e5e82012-04-04 16:53:42 -0700671 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700672 if (!_c.get())
673 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800674
Mathias Agopian518ec112011-05-13 16:21:08 -0700675 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800676 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700677 if (result == EGL_TRUE) {
678 _c.terminate();
679 }
680 return result;
681}
682
Mathias Agopian518ec112011-05-13 16:21:08 -0700683EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
684 EGLSurface read, EGLContext ctx)
685{
686 clearError();
687
Jesse Hallb29e5e82012-04-04 16:53:42 -0700688 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700689 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
690
Mathias Agopian5b287a62011-05-16 18:58:55 -0700691 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
692 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
693 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700694 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
695 (draw != EGL_NO_SURFACE) ) {
696 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
697 }
698
699 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700700 ContextRef _c(dp.get(), ctx);
701 SurfaceRef _d(dp.get(), draw);
702 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700703
704 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700705 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700706 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700707 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700708 }
709
710 // these are the underlying implementation's object
711 EGLContext impl_ctx = EGL_NO_CONTEXT;
712 EGLSurface impl_draw = EGL_NO_SURFACE;
713 EGLSurface impl_read = EGL_NO_SURFACE;
714
715 // these are our objects structs passed in
716 egl_context_t * c = NULL;
717 egl_surface_t const * d = NULL;
718 egl_surface_t const * r = NULL;
719
720 // these are the current objects structs
721 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800722
Mathias Agopian518ec112011-05-13 16:21:08 -0700723 if (ctx != EGL_NO_CONTEXT) {
724 c = get_context(ctx);
725 impl_ctx = c->context;
726 } else {
727 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700728 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
729 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
730 return setError(EGL_BAD_MATCH, EGL_FALSE);
731 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700732 if (cur_c == NULL) {
733 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700734 // not an error, there is just no current context.
735 return EGL_TRUE;
736 }
737 }
738
739 // retrieve the underlying implementation's draw EGLSurface
740 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700741 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700742 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700743 impl_draw = d->surface;
744 }
745
746 // retrieve the underlying implementation's read EGLSurface
747 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700748 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700749 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700750 impl_read = r->surface;
751 }
752
Mathias Agopian518ec112011-05-13 16:21:08 -0700753
Jesse Hallb29e5e82012-04-04 16:53:42 -0700754 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800755 draw, read, ctx,
756 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700757
758 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800759 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700760 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
761 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800762#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800763 if (getEGLDebugLevel() > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800764 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800765#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700766 _c.acquire();
767 _r.acquire();
768 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 } else {
770 setGLHooksThreadSpecific(&gHooksNoContext);
771 egl_tls_t::setContext(EGL_NO_CONTEXT);
772 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700773 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000774 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700775 egl_connection_t* const cnx = &gEGLImpl;
776 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700777 }
778 return result;
779}
780
781
782EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
783 EGLint attribute, EGLint *value)
784{
785 clearError();
786
Jesse Hallb29e5e82012-04-04 16:53:42 -0700787 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700788 if (!dp) return EGL_FALSE;
789
Jesse Hallb29e5e82012-04-04 16:53:42 -0700790 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700791 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
792
Mathias Agopian518ec112011-05-13 16:21:08 -0700793 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800794 return c->cnx->egl.eglQueryContext(
795 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700796
Mathias Agopian518ec112011-05-13 16:21:08 -0700797}
798
799EGLContext eglGetCurrentContext(void)
800{
801 // could be called before eglInitialize(), but we wouldn't have a context
802 // then, and this function would correctly return EGL_NO_CONTEXT.
803
804 clearError();
805
806 EGLContext ctx = getContext();
807 return ctx;
808}
809
810EGLSurface eglGetCurrentSurface(EGLint readdraw)
811{
812 // could be called before eglInitialize(), but we wouldn't have a context
813 // then, and this function would correctly return EGL_NO_SURFACE.
814
815 clearError();
816
817 EGLContext ctx = getContext();
818 if (ctx) {
819 egl_context_t const * const c = get_context(ctx);
820 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
821 switch (readdraw) {
822 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800823 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700824 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
825 }
826 }
827 return EGL_NO_SURFACE;
828}
829
830EGLDisplay eglGetCurrentDisplay(void)
831{
832 // could be called before eglInitialize(), but we wouldn't have a context
833 // then, and this function would correctly return EGL_NO_DISPLAY.
834
835 clearError();
836
837 EGLContext ctx = getContext();
838 if (ctx) {
839 egl_context_t const * const c = get_context(ctx);
840 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
841 return c->dpy;
842 }
843 return EGL_NO_DISPLAY;
844}
845
846EGLBoolean eglWaitGL(void)
847{
Mathias Agopian518ec112011-05-13 16:21:08 -0700848 clearError();
849
Mathias Agopianada798b2012-02-13 17:09:30 -0800850 egl_connection_t* const cnx = &gEGLImpl;
851 if (!cnx->dso)
852 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
853
854 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700855}
856
857EGLBoolean eglWaitNative(EGLint engine)
858{
Mathias Agopian518ec112011-05-13 16:21:08 -0700859 clearError();
860
Mathias Agopianada798b2012-02-13 17:09:30 -0800861 egl_connection_t* const cnx = &gEGLImpl;
862 if (!cnx->dso)
863 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
864
865 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700866}
867
868EGLint eglGetError(void)
869{
Mathias Agopianada798b2012-02-13 17:09:30 -0800870 EGLint err = EGL_SUCCESS;
871 egl_connection_t* const cnx = &gEGLImpl;
872 if (cnx->dso) {
873 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700874 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800875 if (err == EGL_SUCCESS) {
876 err = egl_tls_t::getError();
877 }
878 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700879}
880
Michael Chockc0ec5e22014-01-27 08:14:33 -0800881static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700882 const char* procname) {
883 const egl_connection_t* cnx = &gEGLImpl;
884 void* proc = NULL;
885
Michael Chockc0ec5e22014-01-27 08:14:33 -0800886 proc = dlsym(cnx->libEgl, procname);
887 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
888
Jesse Hallc07b5202013-07-04 12:08:16 -0700889 proc = dlsym(cnx->libGles2, procname);
890 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
891
892 proc = dlsym(cnx->libGles1, procname);
893 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
894
895 return NULL;
896}
897
Mathias Agopian518ec112011-05-13 16:21:08 -0700898__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
899{
900 // eglGetProcAddress() could be the very first function called
901 // in which case we must make sure we've initialized ourselves, this
902 // happens the first time egl_get_display() is called.
903
904 clearError();
905
906 if (egl_init_drivers() == EGL_FALSE) {
907 setError(EGL_BAD_PARAMETER, NULL);
908 return NULL;
909 }
910
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700911 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700912 return NULL;
913 }
914
Mathias Agopian518ec112011-05-13 16:21:08 -0700915 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700916 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700917 if (addr) return addr;
918
Michael Chockc0ec5e22014-01-27 08:14:33 -0800919 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700920 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700921
Mathias Agopian518ec112011-05-13 16:21:08 -0700922 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
923 pthread_mutex_lock(&sExtensionMapMutex);
924
925 /*
926 * Since eglGetProcAddress() is not associated to anything, it needs
927 * to return a function pointer that "works" regardless of what
928 * the current context is.
929 *
930 * For this reason, we return a "forwarder", a small stub that takes
931 * care of calling the function associated with the context
932 * currently bound.
933 *
934 * We first look for extensions we've already resolved, if we're seeing
935 * this extension for the first time, we go through all our
936 * implementations and call eglGetProcAddress() and record the
937 * result in the appropriate implementation hooks and return the
938 * address of the forwarder corresponding to that hook set.
939 *
940 */
941
942 const String8 name(procname);
943 addr = sGLExtentionMap.valueFor(name);
944 const int slot = sGLExtentionSlot;
945
Steve Blocke6f43dd2012-01-06 19:20:56 +0000946 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700947 "no more slots for eglGetProcAddress(\"%s\")",
948 procname);
949
Siva Velusamy0469dd62011-11-30 15:05:37 -0800950#if EGL_TRACE
951 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
952#endif
953
Mathias Agopian518ec112011-05-13 16:21:08 -0700954 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
955 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800956
957 egl_connection_t* const cnx = &gEGLImpl;
958 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800959 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800960 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800961 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
962 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700963#if EGL_TRACE
Mathias Agopianada798b2012-02-13 17:09:30 -0800964 debugHooks->ext.extensions[slot] =
965 gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700966#endif
Mathias Agopianada798b2012-02-13 17:09:30 -0800967 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800968 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800970
Mathias Agopian518ec112011-05-13 16:21:08 -0700971 if (found) {
972 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700973 sGLExtentionMap.add(name, addr);
974 sGLExtentionSlot++;
975 }
976 }
977
978 pthread_mutex_unlock(&sExtensionMapMutex);
979 return addr;
980}
981
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700982class FrameCompletionThread : public Thread {
983public:
984
985 static void queueSync(EGLSyncKHR sync) {
986 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
987 static bool running = false;
988 if (!running) {
989 thread->run("GPUFrameCompletion");
990 running = true;
991 }
992 {
993 Mutex::Autolock lock(thread->mMutex);
994 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
995 thread->mFramesQueued).string());
996 thread->mQueue.push_back(sync);
997 thread->mCondition.signal();
998 thread->mFramesQueued++;
999 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1000 }
1001 }
1002
1003private:
1004 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1005
1006 virtual bool threadLoop() {
1007 EGLSyncKHR sync;
1008 uint32_t frameNum;
1009 {
1010 Mutex::Autolock lock(mMutex);
1011 while (mQueue.isEmpty()) {
1012 mCondition.wait(mMutex);
1013 }
1014 sync = mQueue[0];
1015 frameNum = mFramesCompleted;
1016 }
1017 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1018 {
1019 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1020 frameNum).string());
1021 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1022 if (result == EGL_FALSE) {
1023 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1024 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1025 ALOGE("FrameCompletion: timeout waiting for fence");
1026 }
1027 eglDestroySyncKHR(dpy, sync);
1028 }
1029 {
1030 Mutex::Autolock lock(mMutex);
1031 mQueue.removeAt(0);
1032 mFramesCompleted++;
1033 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1034 }
1035 return true;
1036 }
1037
1038 uint32_t mFramesQueued;
1039 uint32_t mFramesCompleted;
1040 Vector<EGLSyncKHR> mQueue;
1041 Condition mCondition;
1042 Mutex mMutex;
1043};
1044
Dan Stozaa894d082015-02-19 15:27:36 -08001045EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1046 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001047{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001048 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001049 clearError();
1050
Jesse Hallb29e5e82012-04-04 16:53:42 -07001051 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001052 if (!dp) return EGL_FALSE;
1053
Jesse Hallb29e5e82012-04-04 16:53:42 -07001054 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001055 if (!_s.get())
1056 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001057
Siva Velusamy0469dd62011-11-30 15:05:37 -08001058#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -08001059 gl_hooks_t const *trace_hooks = getGLTraceThreadSpecific();
1060 if (getEGLDebugLevel() > 0) {
1061 if (trace_hooks == NULL) {
1062 if (GLTrace_start() < 0) {
1063 ALOGE("Disabling Tracer for OpenGL ES");
1064 setEGLDebugLevel(0);
1065 } else {
1066 // switch over to the trace version of hooks
1067 EGLContext ctx = egl_tls_t::getContext();
1068 egl_context_t * const c = get_context(ctx);
1069 if (c) {
1070 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1071 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
1072 }
1073 }
1074 }
1075
Siva Velusamy0469dd62011-11-30 15:05:37 -08001076 GLTrace_eglSwapBuffers(dpy, draw);
Siva Velusamya73a9772012-12-18 14:56:55 -08001077 } else if (trace_hooks != NULL) {
1078 // tracing is now disabled, so switch back to the non trace version
1079 EGLContext ctx = egl_tls_t::getContext();
1080 egl_context_t * const c = get_context(ctx);
1081 if (c) setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1082 GLTrace_stop();
1083 }
Siva Velusamy0469dd62011-11-30 15:05:37 -08001084#endif
1085
Mathias Agopian518ec112011-05-13 16:21:08 -07001086 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001087
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001088 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1089 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1090 if (sync != EGL_NO_SYNC_KHR) {
1091 FrameCompletionThread::queueSync(sync);
1092 }
1093 }
1094
Mathias Agopian7db993a2012-03-25 00:49:46 -07001095 if (CC_UNLIKELY(dp->finishOnSwap)) {
1096 uint32_t pixel;
1097 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1098 if (c) {
1099 // glReadPixels() ensures that the frame is complete
1100 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1101 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1102 }
1103 }
1104
Dan Stozaa894d082015-02-19 15:27:36 -08001105 if (n_rects == 0) {
1106 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1107 }
1108
1109 Vector<android_native_rect_t> androidRects;
1110 for (int r = 0; r < n_rects; ++r) {
1111 int offset = r * 4;
1112 int x = rects[offset];
1113 int y = rects[offset + 1];
1114 int width = rects[offset + 2];
1115 int height = rects[offset + 3];
1116 android_native_rect_t androidRect;
1117 androidRect.left = x;
1118 androidRect.top = y + height;
1119 androidRect.right = x + width;
1120 androidRect.bottom = y;
1121 androidRects.push_back(androidRect);
1122 }
1123 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1124 androidRects.size());
1125
1126 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1127 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1128 rects, n_rects);
1129 } else {
1130 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1131 }
1132}
1133
1134EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1135{
1136 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001137}
1138
1139EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1140 NativePixmapType target)
1141{
1142 clearError();
1143
Jesse Hallb29e5e82012-04-04 16:53:42 -07001144 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001145 if (!dp) return EGL_FALSE;
1146
Jesse Hallb29e5e82012-04-04 16:53:42 -07001147 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001148 if (!_s.get())
1149 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001150
Mathias Agopian518ec112011-05-13 16:21:08 -07001151 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001152 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001153}
1154
1155const char* eglQueryString(EGLDisplay dpy, EGLint name)
1156{
1157 clearError();
1158
Jesse Hallb29e5e82012-04-04 16:53:42 -07001159 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001160 if (!dp) return (const char *) NULL;
1161
1162 switch (name) {
1163 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001164 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001165 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001166 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001167 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001168 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001169 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001170 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001171 }
1172 return setError(EGL_BAD_PARAMETER, (const char *)0);
1173}
1174
Mathias Agopianca088332013-03-28 17:44:13 -07001175EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1176{
1177 clearError();
1178
1179 const egl_display_ptr dp = validate_display(dpy);
1180 if (!dp) return (const char *) NULL;
1181
1182 switch (name) {
1183 case EGL_VENDOR:
1184 return dp->disp.queryString.vendor;
1185 case EGL_VERSION:
1186 return dp->disp.queryString.version;
1187 case EGL_EXTENSIONS:
1188 return dp->disp.queryString.extensions;
1189 case EGL_CLIENT_APIS:
1190 return dp->disp.queryString.clientApi;
1191 }
1192 return setError(EGL_BAD_PARAMETER, (const char *)0);
1193}
Mathias Agopian518ec112011-05-13 16:21:08 -07001194
1195// ----------------------------------------------------------------------------
1196// EGL 1.1
1197// ----------------------------------------------------------------------------
1198
1199EGLBoolean eglSurfaceAttrib(
1200 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1201{
1202 clearError();
1203
Jesse Hallb29e5e82012-04-04 16:53:42 -07001204 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001205 if (!dp) return EGL_FALSE;
1206
Jesse Hallb29e5e82012-04-04 16:53:42 -07001207 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001208 if (!_s.get())
1209 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001210
Mathias Agopian518ec112011-05-13 16:21:08 -07001211 egl_surface_t const * const s = get_surface(surface);
1212 if (s->cnx->egl.eglSurfaceAttrib) {
1213 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001214 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001215 }
1216 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1217}
1218
1219EGLBoolean eglBindTexImage(
1220 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1221{
1222 clearError();
1223
Jesse Hallb29e5e82012-04-04 16:53:42 -07001224 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001225 if (!dp) return EGL_FALSE;
1226
Jesse Hallb29e5e82012-04-04 16:53:42 -07001227 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001228 if (!_s.get())
1229 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001230
Mathias Agopian518ec112011-05-13 16:21:08 -07001231 egl_surface_t const * const s = get_surface(surface);
1232 if (s->cnx->egl.eglBindTexImage) {
1233 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001234 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001235 }
1236 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1237}
1238
1239EGLBoolean eglReleaseTexImage(
1240 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1241{
1242 clearError();
1243
Jesse Hallb29e5e82012-04-04 16:53:42 -07001244 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001245 if (!dp) return EGL_FALSE;
1246
Jesse Hallb29e5e82012-04-04 16:53:42 -07001247 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001248 if (!_s.get())
1249 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001250
Mathias Agopian518ec112011-05-13 16:21:08 -07001251 egl_surface_t const * const s = get_surface(surface);
1252 if (s->cnx->egl.eglReleaseTexImage) {
1253 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001254 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001255 }
1256 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1257}
1258
1259EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1260{
1261 clearError();
1262
Jesse Hallb29e5e82012-04-04 16:53:42 -07001263 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001264 if (!dp) return EGL_FALSE;
1265
1266 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001267 egl_connection_t* const cnx = &gEGLImpl;
1268 if (cnx->dso && cnx->egl.eglSwapInterval) {
1269 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001270 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001271
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 return res;
1273}
1274
1275
1276// ----------------------------------------------------------------------------
1277// EGL 1.2
1278// ----------------------------------------------------------------------------
1279
1280EGLBoolean eglWaitClient(void)
1281{
1282 clearError();
1283
Mathias Agopianada798b2012-02-13 17:09:30 -08001284 egl_connection_t* const cnx = &gEGLImpl;
1285 if (!cnx->dso)
1286 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1287
1288 EGLBoolean res;
1289 if (cnx->egl.eglWaitClient) {
1290 res = cnx->egl.eglWaitClient();
1291 } else {
1292 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001293 }
1294 return res;
1295}
1296
1297EGLBoolean eglBindAPI(EGLenum api)
1298{
1299 clearError();
1300
1301 if (egl_init_drivers() == EGL_FALSE) {
1302 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1303 }
1304
1305 // bind this API on all EGLs
1306 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001307 egl_connection_t* const cnx = &gEGLImpl;
1308 if (cnx->dso && cnx->egl.eglBindAPI) {
1309 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001310 }
1311 return res;
1312}
1313
1314EGLenum eglQueryAPI(void)
1315{
1316 clearError();
1317
1318 if (egl_init_drivers() == EGL_FALSE) {
1319 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1320 }
1321
Mathias Agopianada798b2012-02-13 17:09:30 -08001322 egl_connection_t* const cnx = &gEGLImpl;
1323 if (cnx->dso && cnx->egl.eglQueryAPI) {
1324 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001325 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001326
Mathias Agopian518ec112011-05-13 16:21:08 -07001327 // or, it can only be OpenGL ES
1328 return EGL_OPENGL_ES_API;
1329}
1330
1331EGLBoolean eglReleaseThread(void)
1332{
1333 clearError();
1334
Mathias Agopian4e620dd2013-05-30 16:07:36 -07001335#if EGL_TRACE
1336 if (getEGLDebugLevel() > 0)
1337 GLTrace_eglReleaseThread();
1338#endif
1339
Mathias Agopian518ec112011-05-13 16:21:08 -07001340 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001341 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001342
Mathias Agopianada798b2012-02-13 17:09:30 -08001343 egl_connection_t* const cnx = &gEGLImpl;
1344 if (cnx->dso && cnx->egl.eglReleaseThread) {
1345 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001346 }
1347 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001348 return EGL_TRUE;
1349}
1350
1351EGLSurface eglCreatePbufferFromClientBuffer(
1352 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1353 EGLConfig config, const EGLint *attrib_list)
1354{
1355 clearError();
1356
Jesse Hallb29e5e82012-04-04 16:53:42 -07001357 egl_connection_t* cnx = NULL;
1358 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1359 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001360 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1361 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001362 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001363 }
1364 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1365}
1366
1367// ----------------------------------------------------------------------------
1368// EGL_EGLEXT_VERSION 3
1369// ----------------------------------------------------------------------------
1370
1371EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1372 const EGLint *attrib_list)
1373{
1374 clearError();
1375
Jesse Hallb29e5e82012-04-04 16:53:42 -07001376 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001377 if (!dp) return EGL_FALSE;
1378
Jesse Hallb29e5e82012-04-04 16:53:42 -07001379 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001380 if (!_s.get())
1381 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001382
1383 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001384 if (s->cnx->egl.eglLockSurfaceKHR) {
1385 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001386 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001387 }
1388 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1389}
1390
1391EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1392{
1393 clearError();
1394
Jesse Hallb29e5e82012-04-04 16:53:42 -07001395 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001396 if (!dp) return EGL_FALSE;
1397
Jesse Hallb29e5e82012-04-04 16:53:42 -07001398 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001399 if (!_s.get())
1400 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001401
1402 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001403 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001404 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001405 }
1406 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1407}
1408
1409EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1410 EGLClientBuffer buffer, const EGLint *attrib_list)
1411{
1412 clearError();
1413
Jesse Hallb29e5e82012-04-04 16:53:42 -07001414 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001415 if (!dp) return EGL_NO_IMAGE_KHR;
1416
Jesse Hallb29e5e82012-04-04 16:53:42 -07001417 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001418 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001419
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001420 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1421 egl_connection_t* const cnx = &gEGLImpl;
1422 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1423 result = cnx->egl.eglCreateImageKHR(
1424 dp->disp.dpy,
1425 c ? c->context : EGL_NO_CONTEXT,
1426 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001427 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001428 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001429}
1430
1431EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1432{
1433 clearError();
1434
Jesse Hallb29e5e82012-04-04 16:53:42 -07001435 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001436 if (!dp) return EGL_FALSE;
1437
Steven Holte646a5c52012-06-04 20:02:11 -07001438 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001439 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001440 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001441 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001442 }
Steven Holte646a5c52012-06-04 20:02:11 -07001443 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001444}
1445
1446// ----------------------------------------------------------------------------
1447// EGL_EGLEXT_VERSION 5
1448// ----------------------------------------------------------------------------
1449
1450
1451EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1452{
1453 clearError();
1454
Jesse Hallb29e5e82012-04-04 16:53:42 -07001455 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001456 if (!dp) return EGL_NO_SYNC_KHR;
1457
Mathias Agopian518ec112011-05-13 16:21:08 -07001458 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001459 egl_connection_t* const cnx = &gEGLImpl;
1460 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1461 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001462 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001463 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001464}
1465
1466EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1467{
1468 clearError();
1469
Jesse Hallb29e5e82012-04-04 16:53:42 -07001470 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001471 if (!dp) return EGL_FALSE;
1472
Mathias Agopian518ec112011-05-13 16:21:08 -07001473 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001474 egl_connection_t* const cnx = &gEGLImpl;
1475 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1476 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001477 }
1478 return result;
1479}
1480
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001481EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1482 clearError();
1483
1484 const egl_display_ptr dp = validate_display(dpy);
1485 if (!dp) return EGL_FALSE;
1486
1487 EGLBoolean result = EGL_FALSE;
1488 egl_connection_t* const cnx = &gEGLImpl;
1489 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1490 result = cnx->egl.eglSignalSyncKHR(
1491 dp->disp.dpy, sync, mode);
1492 }
1493 return result;
1494}
1495
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001496EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1497 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001498{
1499 clearError();
1500
Jesse Hallb29e5e82012-04-04 16:53:42 -07001501 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001502 if (!dp) return EGL_FALSE;
1503
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001504 EGLBoolean result = EGL_FALSE;
1505 egl_connection_t* const cnx = &gEGLImpl;
1506 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1507 result = cnx->egl.eglClientWaitSyncKHR(
1508 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001509 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001510 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001511}
1512
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001513EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1514 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001515{
1516 clearError();
1517
Jesse Hallb29e5e82012-04-04 16:53:42 -07001518 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001519 if (!dp) return EGL_FALSE;
1520
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001521 EGLBoolean result = EGL_FALSE;
1522 egl_connection_t* const cnx = &gEGLImpl;
1523 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1524 result = cnx->egl.eglGetSyncAttribKHR(
1525 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001526 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001527 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001528}
1529
1530// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001531// EGL_EGLEXT_VERSION 15
1532// ----------------------------------------------------------------------------
1533
1534EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1535 clearError();
1536 const egl_display_ptr dp = validate_display(dpy);
1537 if (!dp) return EGL_FALSE;
1538 EGLint result = EGL_FALSE;
1539 egl_connection_t* const cnx = &gEGLImpl;
1540 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1541 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1542 }
1543 return result;
1544}
1545
1546// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001547// ANDROID extensions
1548// ----------------------------------------------------------------------------
1549
Jamie Gennis331841b2012-09-06 14:52:00 -07001550EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1551{
1552 clearError();
1553
1554 const egl_display_ptr dp = validate_display(dpy);
1555 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1556
1557 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1558 egl_connection_t* const cnx = &gEGLImpl;
1559 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1560 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1561 }
1562 return result;
1563}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001564
Andy McFadden72841452013-03-01 16:25:32 -08001565EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1566 EGLnsecsANDROID time)
1567{
1568 clearError();
1569
1570 const egl_display_ptr dp = validate_display(dpy);
1571 if (!dp) {
1572 return EGL_FALSE;
1573 }
1574
1575 SurfaceRef _s(dp.get(), surface);
1576 if (!_s.get()) {
1577 setError(EGL_BAD_SURFACE, EGL_FALSE);
1578 return EGL_FALSE;
1579 }
1580
1581 egl_surface_t const * const s = get_surface(surface);
1582 native_window_set_buffers_timestamp(s->win.get(), time);
1583
1584 return EGL_TRUE;
1585}
1586
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001587// ----------------------------------------------------------------------------
1588// NVIDIA extensions
1589// ----------------------------------------------------------------------------
1590EGLuint64NV eglGetSystemTimeFrequencyNV()
1591{
1592 clearError();
1593
1594 if (egl_init_drivers() == EGL_FALSE) {
1595 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1596 }
1597
1598 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001599 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001600
Mathias Agopianada798b2012-02-13 17:09:30 -08001601 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1602 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001603 }
1604
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001605 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001606}
1607
1608EGLuint64NV eglGetSystemTimeNV()
1609{
1610 clearError();
1611
1612 if (egl_init_drivers() == EGL_FALSE) {
1613 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1614 }
1615
1616 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001617 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001618
Mathias Agopianada798b2012-02-13 17:09:30 -08001619 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1620 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001621 }
1622
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001623 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001624}
Dan Stozaa894d082015-02-19 15:27:36 -08001625
1626// ----------------------------------------------------------------------------
1627// Partial update extension
1628// ----------------------------------------------------------------------------
1629EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1630 EGLint *rects, EGLint n_rects)
1631{
1632 clearError();
1633
1634 const egl_display_ptr dp = validate_display(dpy);
1635 if (!dp) {
1636 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1637 return EGL_FALSE;
1638 }
1639
1640 SurfaceRef _s(dp.get(), surface);
1641 if (!_s.get()) {
1642 setError(EGL_BAD_SURFACE, EGL_FALSE);
1643 return EGL_FALSE;
1644 }
1645
1646 egl_surface_t const * const s = get_surface(surface);
1647 if (s->cnx->egl.eglSetDamageRegionKHR) {
1648 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1649 rects, n_rects);
1650 }
1651
1652 return EGL_FALSE;
1653}