blob: 837890759bb4c419f52094ac1e10a7d630e8a467 [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 "
Season Li000d88f2015-07-01 11:39:40 -070094 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095 "EGL_KHR_gl_texture_cubemap_image "
96 "EGL_KHR_gl_renderbuffer_image "
97 "EGL_KHR_reusable_sync "
98 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -070099 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700100 "EGL_KHR_config_attribs "
101 "EGL_KHR_surfaceless_context "
102 "EGL_KHR_stream "
103 "EGL_KHR_stream_fifo "
104 "EGL_KHR_stream_producer_eglsurface "
105 "EGL_KHR_stream_consumer_gltexture "
106 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700107 "EGL_EXT_create_context_robustness "
108 "EGL_NV_system_time "
109 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700110 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700111 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800112 "EGL_KHR_partial_update " // strongly recommended
113 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700114 "EGL_KHR_create_context_no_error "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700115 ;
116
117// extensions not exposed to applications but used by the ANDROID system
118// "EGL_ANDROID_blob_cache " // strongly recommended
119// "EGL_IMG_hibernate_process " // optional
120// "EGL_ANDROID_native_fence_sync " // strongly recommended
121// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700122// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700123
124/*
125 * EGL Extensions entry-points exposed to 3rd party applications
126 * (keep in sync with gExtensionString above)
127 *
128 */
129static const extention_map_t sExtensionMap[] = {
130 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700131 { "eglLockSurfaceKHR",
132 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
133 { "eglUnlockSurfaceKHR",
134 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700135
136 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700137 { "eglCreateImageKHR",
138 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
139 { "eglDestroyImageKHR",
140 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700141
142 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
143 { "eglCreateSyncKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
145 { "eglDestroySyncKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
147 { "eglClientWaitSyncKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
149 { "eglSignalSyncKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
151 { "eglGetSyncAttribKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
153
154 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800155 { "eglGetSystemTimeFrequencyNV",
156 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
157 { "eglGetSystemTimeNV",
158 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700159
Mathias Agopian2bb71682013-03-27 17:32:41 -0700160 // EGL_KHR_wait_sync
161 { "eglWaitSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700163
164 // EGL_ANDROID_presentation_time
165 { "eglPresentationTimeANDROID",
166 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800167
168 // EGL_KHR_swap_buffers_with_damage
169 { "eglSwapBuffersWithDamageKHR",
170 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
171
172 // EGL_KHR_partial_update
173 { "eglSetDamageRegionKHR",
174 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700175
176 { "eglCreateStreamKHR",
177 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
178 { "eglDestroyStreamKHR",
179 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
180 { "eglStreamAttribKHR",
181 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
182 { "eglQueryStreamKHR",
183 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
184 { "eglQueryStreamu64KHR",
185 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
186 { "eglQueryStreamTimeKHR",
187 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
188 { "eglCreateStreamProducerSurfaceKHR",
189 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
190 { "eglStreamConsumerGLTextureExternalKHR",
191 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
192 { "eglStreamConsumerAcquireKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
194 { "eglStreamConsumerReleaseKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
196 { "eglGetStreamFileDescriptorKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
198 { "eglCreateStreamFromFileDescriptorKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Mathias Agopian518ec112011-05-13 16:21:08 -0700200};
201
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700202/*
203 * These extensions entry-points should not be exposed to applications.
204 * They're used internally by the Android EGL layer.
205 */
206#define FILTER_EXTENSIONS(procname) \
207 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
208 !strcmp((procname), "eglHibernateProcessIMG") || \
209 !strcmp((procname), "eglAwakenProcessIMG") || \
210 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
211
212
213
Mathias Agopian518ec112011-05-13 16:21:08 -0700214// accesses protected by sExtensionMapMutex
215static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
216static int sGLExtentionSlot = 0;
217static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
218
219static void(*findProcAddress(const char* name,
220 const extention_map_t* map, size_t n))() {
221 for (uint32_t i=0 ; i<n ; i++) {
222 if (!strcmp(name, map[i].name)) {
223 return map[i].address;
224 }
225 }
226 return NULL;
227}
228
229// ----------------------------------------------------------------------------
230
Mathias Agopian518ec112011-05-13 16:21:08 -0700231extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
232extern EGLBoolean egl_init_drivers();
233extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Siva Velusamya73a9772012-12-18 14:56:55 -0800234extern int getEGLDebugLevel();
235extern void setEGLDebugLevel(int level);
Mathias Agopian518ec112011-05-13 16:21:08 -0700236extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700237
Mathias Agopian518ec112011-05-13 16:21:08 -0700238} // namespace android;
239
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700240
Mathias Agopian518ec112011-05-13 16:21:08 -0700241// ----------------------------------------------------------------------------
242
243static inline void clearError() { egl_tls_t::clearError(); }
244static inline EGLContext getContext() { return egl_tls_t::getContext(); }
245
246// ----------------------------------------------------------------------------
247
248EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
249{
250 clearError();
251
Dan Stozac3289c42014-01-17 11:38:34 -0800252 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700253 if (index >= NUM_DISPLAYS) {
254 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
255 }
256
257 if (egl_init_drivers() == EGL_FALSE) {
258 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
259 }
260
261 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
262 return dpy;
263}
264
265// ----------------------------------------------------------------------------
266// Initialization
267// ----------------------------------------------------------------------------
268
269EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
270{
271 clearError();
272
Jesse Hallb29e5e82012-04-04 16:53:42 -0700273 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700274 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
275
276 EGLBoolean res = dp->initialize(major, minor);
277
278 return res;
279}
280
281EGLBoolean eglTerminate(EGLDisplay dpy)
282{
283 // NOTE: don't unload the drivers b/c some APIs can be called
284 // after eglTerminate() has been called. eglTerminate() only
285 // terminates an EGLDisplay, not a EGL itself.
286
287 clearError();
288
Jesse Hallb29e5e82012-04-04 16:53:42 -0700289 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700290 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
291
292 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800293
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 return res;
295}
296
297// ----------------------------------------------------------------------------
298// configuration
299// ----------------------------------------------------------------------------
300
301EGLBoolean eglGetConfigs( EGLDisplay dpy,
302 EGLConfig *configs,
303 EGLint config_size, EGLint *num_config)
304{
305 clearError();
306
Jesse Hallb29e5e82012-04-04 16:53:42 -0700307 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700308 if (!dp) return EGL_FALSE;
309
Mathias Agopian7773c432012-02-13 20:06:08 -0800310 if (num_config==0) {
311 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700312 }
313
Mathias Agopian7773c432012-02-13 20:06:08 -0800314 EGLBoolean res = EGL_FALSE;
315 *num_config = 0;
316
317 egl_connection_t* const cnx = &gEGLImpl;
318 if (cnx->dso) {
319 res = cnx->egl.eglGetConfigs(
320 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700321 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800322
323 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700324}
325
326EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
327 EGLConfig *configs, EGLint config_size,
328 EGLint *num_config)
329{
330 clearError();
331
Jesse Hallb29e5e82012-04-04 16:53:42 -0700332 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700333 if (!dp) return EGL_FALSE;
334
335 if (num_config==0) {
336 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
337 }
338
Mathias Agopian518ec112011-05-13 16:21:08 -0700339 EGLBoolean res = EGL_FALSE;
340 *num_config = 0;
341
Mathias Agopianada798b2012-02-13 17:09:30 -0800342 egl_connection_t* const cnx = &gEGLImpl;
343 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700344 if (attrib_list) {
345 char value[PROPERTY_VALUE_MAX];
346 property_get("debug.egl.force_msaa", value, "false");
347
348 if (!strcmp(value, "true")) {
349 size_t attribCount = 0;
350 EGLint attrib = attrib_list[0];
351
352 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700353 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700354 const EGLint *attribRendererable = NULL;
355 const EGLint *attribCaveat = NULL;
356
357 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700358 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700359 while (attrib != EGL_NONE) {
360 attrib = attrib_list[attribCount];
361 switch (attrib) {
362 case EGL_RENDERABLE_TYPE:
363 attribRendererable = &attrib_list[attribCount];
364 break;
365 case EGL_CONFIG_CAVEAT:
366 attribCaveat = &attrib_list[attribCount];
367 break;
368 }
369 attribCount++;
370 }
371
372 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
373 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800374
Romain Guy1cffc802012-10-15 18:13:05 -0700375 // Insert 2 extra attributes to force-enable MSAA 4x
376 EGLint aaAttribs[attribCount + 4];
377 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
378 aaAttribs[1] = 1;
379 aaAttribs[2] = EGL_SAMPLES;
380 aaAttribs[3] = 4;
381
382 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
383
384 EGLint numConfigAA;
385 EGLBoolean resAA = cnx->egl.eglChooseConfig(
386 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
387
388 if (resAA == EGL_TRUE && numConfigAA > 0) {
389 ALOGD("Enabling MSAA 4x");
390 *num_config = numConfigAA;
391 return resAA;
392 }
393 }
394 }
395 }
396
Mathias Agopian7773c432012-02-13 20:06:08 -0800397 res = cnx->egl.eglChooseConfig(
398 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700399 }
400 return res;
401}
402
403EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
404 EGLint attribute, EGLint *value)
405{
406 clearError();
407
Jesse Hallb29e5e82012-04-04 16:53:42 -0700408 egl_connection_t* cnx = NULL;
409 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
410 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800411
Mathias Agopian518ec112011-05-13 16:21:08 -0700412 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800413 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700414}
415
416// ----------------------------------------------------------------------------
417// surfaces
418// ----------------------------------------------------------------------------
419
Jesse Halla2ba4282013-09-14 21:00:14 -0700420// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
Jesse Hallc2e41222013-08-08 13:40:22 -0700421// been added to the Khronos egl.h.
422#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE
423#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB
424#define EGL_GL_COLORSPACE_LINEAR_KHR EGL_VG_COLORSPACE_LINEAR
425
426// Turn linear formats into corresponding sRGB formats when colorspace is
427// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
428// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800429// the modification isn't possible, the original dataSpace is returned.
430static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
431 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700432 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800433 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700434 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800435 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700436 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800437 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700438}
439
Mathias Agopian518ec112011-05-13 16:21:08 -0700440EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
441 NativeWindowType window,
442 const EGLint *attrib_list)
443{
444 clearError();
445
Jesse Hallb29e5e82012-04-04 16:53:42 -0700446 egl_connection_t* cnx = NULL;
447 egl_display_ptr dp = validate_display_connection(dpy, cnx);
448 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800449 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700450
Andy McFaddend566ce32014-01-07 15:54:17 -0800451 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
452 if (result != OK) {
453 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
454 "failed (%#x) (already connected to another API?)",
455 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700456 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700457 }
458
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700459 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
461 // of our native format. So if sRGB gamma is requested, we have to
462 // modify the EGLconfig's format before setting the native window's
463 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800464
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700465 // by default, just pick RGBA_8888
466 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800467 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700468
469 EGLint a = 0;
470 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
471 if (a > 0) {
472 // alpha-channel requested, there's really only one suitable format
473 format = HAL_PIXEL_FORMAT_RGBA_8888;
474 } else {
475 EGLint r, g, b;
476 r = g = b = 0;
477 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
478 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
479 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
480 EGLint colorDepth = r + g + b;
481 if (colorDepth <= 16) {
482 format = HAL_PIXEL_FORMAT_RGB_565;
483 } else {
484 format = HAL_PIXEL_FORMAT_RGBX_8888;
485 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700486 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700487
488 // now select a corresponding sRGB format if needed
489 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
490 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
491 if (*attr == EGL_GL_COLORSPACE_KHR) {
Jesse Halla2ba4282013-09-14 21:00:14 -0700492 if (ENABLE_EGL_KHR_GL_COLORSPACE) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800493 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jesse Halla2ba4282013-09-14 21:00:14 -0700494 } else {
495 // Normally we'd pass through unhandled attributes to
496 // the driver. But in case the driver implements this
497 // extension but we're disabling it, we want to prevent
498 // it getting through -- support will be broken without
499 // our help.
500 ALOGE("sRGB window surfaces not supported");
501 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
502 }
Jamie Gennisbee205f2011-07-01 13:12:07 -0700503 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700504 }
505 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800506
Jesse Hallc2e41222013-08-08 13:40:22 -0700507 if (format != 0) {
508 int err = native_window_set_buffers_format(window, format);
509 if (err != 0) {
510 ALOGE("error setting native window pixel format: %s (%d)",
511 strerror(-err), err);
512 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
513 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
514 }
515 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700516
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800517 if (dataSpace != 0) {
518 int err = native_window_set_buffers_data_space(window, dataSpace);
519 if (err != 0) {
520 ALOGE("error setting native window pixel dataSpace: %s (%d)",
521 strerror(-err), err);
522 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
523 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
524 }
525 }
526
Jamie Gennis59769462011-11-19 18:04:43 -0800527 // the EGL spec requires that a new EGLSurface default to swap interval
528 // 1, so explicitly set that on the window here.
529 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
530 anw->setSwapInterval(anw, 1);
531
Mathias Agopian518ec112011-05-13 16:21:08 -0700532 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800533 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700534 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700535 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
536 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700537 return s;
538 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700539
540 // EGLSurface creation failed
541 native_window_set_buffers_format(window, 0);
542 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700543 }
544 return EGL_NO_SURFACE;
545}
546
547EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
548 NativePixmapType pixmap,
549 const EGLint *attrib_list)
550{
551 clearError();
552
Jesse Hallb29e5e82012-04-04 16:53:42 -0700553 egl_connection_t* cnx = NULL;
554 egl_display_ptr dp = validate_display_connection(dpy, cnx);
555 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700556 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800557 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700558 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700559 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
560 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700561 return s;
562 }
563 }
564 return EGL_NO_SURFACE;
565}
566
567EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
568 const EGLint *attrib_list)
569{
570 clearError();
571
Jesse Hallb29e5e82012-04-04 16:53:42 -0700572 egl_connection_t* cnx = NULL;
573 egl_display_ptr dp = validate_display_connection(dpy, cnx);
574 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800576 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700577 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700578 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
579 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 return s;
581 }
582 }
583 return EGL_NO_SURFACE;
584}
Jesse Hall47743382013-02-08 11:13:46 -0800585
Mathias Agopian518ec112011-05-13 16:21:08 -0700586EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
587{
588 clearError();
589
Jesse Hallb29e5e82012-04-04 16:53:42 -0700590 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700591 if (!dp) return EGL_FALSE;
592
Jesse Hallb29e5e82012-04-04 16:53:42 -0700593 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700594 if (!_s.get())
595 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700596
597 egl_surface_t * const s = get_surface(surface);
Amith Dsouza4f21a4c2015-06-30 22:54:16 -0700598 ANativeWindow* window = s->win.get();
599 if (window) {
600 int result = native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
601 if (result != OK) {
602 ALOGE("eglDestroySurface: native_window_api_disconnect (win=%p) "
603 "failed (%#x)",
604 window, result);
605 }
606 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800607 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700608 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700609 _s.terminate();
610 }
611 return result;
612}
613
614EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
615 EGLint attribute, EGLint *value)
616{
617 clearError();
618
Jesse Hallb29e5e82012-04-04 16:53:42 -0700619 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700620 if (!dp) return EGL_FALSE;
621
Jesse Hallb29e5e82012-04-04 16:53:42 -0700622 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700623 if (!_s.get())
624 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700625
Mathias Agopian518ec112011-05-13 16:21:08 -0700626 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800627 return s->cnx->egl.eglQuerySurface(
628 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700629}
630
Jamie Gennise8696a42012-01-15 18:54:57 -0800631void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800632 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800633 clearError();
634
Jesse Hallb29e5e82012-04-04 16:53:42 -0700635 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800636 if (!dp) {
637 return;
638 }
639
Jesse Hallb29e5e82012-04-04 16:53:42 -0700640 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800641 if (!_s.get()) {
642 setError(EGL_BAD_SURFACE, EGL_FALSE);
643 return;
644 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800645}
646
Mathias Agopian518ec112011-05-13 16:21:08 -0700647// ----------------------------------------------------------------------------
648// Contexts
649// ----------------------------------------------------------------------------
650
651EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
652 EGLContext share_list, const EGLint *attrib_list)
653{
654 clearError();
655
Jesse Hallb29e5e82012-04-04 16:53:42 -0700656 egl_connection_t* cnx = NULL;
657 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700658 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700659 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700660 if (!ContextRef(dp.get(), share_list).get()) {
661 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
662 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700663 egl_context_t* const c = get_context(share_list);
664 share_list = c->context;
665 }
666 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800667 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700668 if (context != EGL_NO_CONTEXT) {
669 // figure out if it's a GLESv1 or GLESv2
670 int version = 0;
671 if (attrib_list) {
672 while (*attrib_list != EGL_NONE) {
673 GLint attr = *attrib_list++;
674 GLint value = *attrib_list++;
675 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
676 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800677 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800678 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800679 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700680 }
681 }
682 };
683 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700684 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
685 version);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800686#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800687 if (getEGLDebugLevel() > 0)
Siva Velusamy0469dd62011-11-30 15:05:37 -0800688 GLTrace_eglCreateContext(version, c);
689#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700690 return c;
691 }
692 }
693 return EGL_NO_CONTEXT;
694}
695
696EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
697{
698 clearError();
699
Jesse Hallb29e5e82012-04-04 16:53:42 -0700700 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700701 if (!dp)
702 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700703
Jesse Hallb29e5e82012-04-04 16:53:42 -0700704 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700705 if (!_c.get())
706 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800707
Mathias Agopian518ec112011-05-13 16:21:08 -0700708 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800709 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700710 if (result == EGL_TRUE) {
711 _c.terminate();
712 }
713 return result;
714}
715
Mathias Agopian518ec112011-05-13 16:21:08 -0700716EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
717 EGLSurface read, EGLContext ctx)
718{
719 clearError();
720
Jesse Hallb29e5e82012-04-04 16:53:42 -0700721 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700722 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
723
Mathias Agopian5b287a62011-05-16 18:58:55 -0700724 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
725 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
726 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700727 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
728 (draw != EGL_NO_SURFACE) ) {
729 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
730 }
731
732 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700733 ContextRef _c(dp.get(), ctx);
734 SurfaceRef _d(dp.get(), draw);
735 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700736
737 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700738 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700739 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700740 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700741 }
742
743 // these are the underlying implementation's object
744 EGLContext impl_ctx = EGL_NO_CONTEXT;
745 EGLSurface impl_draw = EGL_NO_SURFACE;
746 EGLSurface impl_read = EGL_NO_SURFACE;
747
748 // these are our objects structs passed in
749 egl_context_t * c = NULL;
750 egl_surface_t const * d = NULL;
751 egl_surface_t const * r = NULL;
752
753 // these are the current objects structs
754 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800755
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 if (ctx != EGL_NO_CONTEXT) {
757 c = get_context(ctx);
758 impl_ctx = c->context;
759 } else {
760 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700761 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
762 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
763 return setError(EGL_BAD_MATCH, EGL_FALSE);
764 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700765 if (cur_c == NULL) {
766 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 // not an error, there is just no current context.
768 return EGL_TRUE;
769 }
770 }
771
772 // retrieve the underlying implementation's draw EGLSurface
773 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700774 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700775 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700776 impl_draw = d->surface;
777 }
778
779 // retrieve the underlying implementation's read EGLSurface
780 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700781 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700782 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700783 impl_read = r->surface;
784 }
785
Mathias Agopian518ec112011-05-13 16:21:08 -0700786
Jesse Hallb29e5e82012-04-04 16:53:42 -0700787 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800788 draw, read, ctx,
789 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700790
791 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800792 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700793 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
794 egl_tls_t::setContext(ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800795#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -0800796 if (getEGLDebugLevel() > 0)
Siva Velusamy93a826f2011-12-14 12:19:56 -0800797 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
Siva Velusamy0469dd62011-11-30 15:05:37 -0800798#endif
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 _c.acquire();
800 _r.acquire();
801 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700802 } else {
803 setGLHooksThreadSpecific(&gHooksNoContext);
804 egl_tls_t::setContext(EGL_NO_CONTEXT);
805 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700806 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000807 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700808 egl_connection_t* const cnx = &gEGLImpl;
809 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 }
811 return result;
812}
813
814
815EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
816 EGLint attribute, EGLint *value)
817{
818 clearError();
819
Jesse Hallb29e5e82012-04-04 16:53:42 -0700820 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700821 if (!dp) return EGL_FALSE;
822
Jesse Hallb29e5e82012-04-04 16:53:42 -0700823 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700824 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
825
Mathias Agopian518ec112011-05-13 16:21:08 -0700826 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800827 return c->cnx->egl.eglQueryContext(
828 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700829
Mathias Agopian518ec112011-05-13 16:21:08 -0700830}
831
832EGLContext eglGetCurrentContext(void)
833{
834 // could be called before eglInitialize(), but we wouldn't have a context
835 // then, and this function would correctly return EGL_NO_CONTEXT.
836
837 clearError();
838
839 EGLContext ctx = getContext();
840 return ctx;
841}
842
843EGLSurface eglGetCurrentSurface(EGLint readdraw)
844{
845 // could be called before eglInitialize(), but we wouldn't have a context
846 // then, and this function would correctly return EGL_NO_SURFACE.
847
848 clearError();
849
850 EGLContext ctx = getContext();
851 if (ctx) {
852 egl_context_t const * const c = get_context(ctx);
853 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
854 switch (readdraw) {
855 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800856 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700857 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
858 }
859 }
860 return EGL_NO_SURFACE;
861}
862
863EGLDisplay eglGetCurrentDisplay(void)
864{
865 // could be called before eglInitialize(), but we wouldn't have a context
866 // then, and this function would correctly return EGL_NO_DISPLAY.
867
868 clearError();
869
870 EGLContext ctx = getContext();
871 if (ctx) {
872 egl_context_t const * const c = get_context(ctx);
873 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
874 return c->dpy;
875 }
876 return EGL_NO_DISPLAY;
877}
878
879EGLBoolean eglWaitGL(void)
880{
Mathias Agopian518ec112011-05-13 16:21:08 -0700881 clearError();
882
Mathias Agopianada798b2012-02-13 17:09:30 -0800883 egl_connection_t* const cnx = &gEGLImpl;
884 if (!cnx->dso)
885 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
886
887 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700888}
889
890EGLBoolean eglWaitNative(EGLint engine)
891{
Mathias Agopian518ec112011-05-13 16:21:08 -0700892 clearError();
893
Mathias Agopianada798b2012-02-13 17:09:30 -0800894 egl_connection_t* const cnx = &gEGLImpl;
895 if (!cnx->dso)
896 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
897
898 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700899}
900
901EGLint eglGetError(void)
902{
Mathias Agopianada798b2012-02-13 17:09:30 -0800903 EGLint err = EGL_SUCCESS;
904 egl_connection_t* const cnx = &gEGLImpl;
905 if (cnx->dso) {
906 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700907 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800908 if (err == EGL_SUCCESS) {
909 err = egl_tls_t::getError();
910 }
911 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700912}
913
Michael Chockc0ec5e22014-01-27 08:14:33 -0800914static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700915 const char* procname) {
916 const egl_connection_t* cnx = &gEGLImpl;
917 void* proc = NULL;
918
Michael Chockc0ec5e22014-01-27 08:14:33 -0800919 proc = dlsym(cnx->libEgl, procname);
920 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
921
Jesse Hallc07b5202013-07-04 12:08:16 -0700922 proc = dlsym(cnx->libGles2, procname);
923 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
924
925 proc = dlsym(cnx->libGles1, procname);
926 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
927
928 return NULL;
929}
930
Mathias Agopian518ec112011-05-13 16:21:08 -0700931__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
932{
933 // eglGetProcAddress() could be the very first function called
934 // in which case we must make sure we've initialized ourselves, this
935 // happens the first time egl_get_display() is called.
936
937 clearError();
938
939 if (egl_init_drivers() == EGL_FALSE) {
940 setError(EGL_BAD_PARAMETER, NULL);
941 return NULL;
942 }
943
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700944 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700945 return NULL;
946 }
947
Mathias Agopian518ec112011-05-13 16:21:08 -0700948 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700949 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700950 if (addr) return addr;
951
Michael Chockc0ec5e22014-01-27 08:14:33 -0800952 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700953 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700954
Mathias Agopian518ec112011-05-13 16:21:08 -0700955 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
956 pthread_mutex_lock(&sExtensionMapMutex);
957
958 /*
959 * Since eglGetProcAddress() is not associated to anything, it needs
960 * to return a function pointer that "works" regardless of what
961 * the current context is.
962 *
963 * For this reason, we return a "forwarder", a small stub that takes
964 * care of calling the function associated with the context
965 * currently bound.
966 *
967 * We first look for extensions we've already resolved, if we're seeing
968 * this extension for the first time, we go through all our
969 * implementations and call eglGetProcAddress() and record the
970 * result in the appropriate implementation hooks and return the
971 * address of the forwarder corresponding to that hook set.
972 *
973 */
974
975 const String8 name(procname);
976 addr = sGLExtentionMap.valueFor(name);
977 const int slot = sGLExtentionSlot;
978
Steve Blocke6f43dd2012-01-06 19:20:56 +0000979 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700980 "no more slots for eglGetProcAddress(\"%s\")",
981 procname);
982
Siva Velusamy0469dd62011-11-30 15:05:37 -0800983#if EGL_TRACE
984 gl_hooks_t *debugHooks = GLTrace_getGLHooks();
985#endif
986
Mathias Agopian518ec112011-05-13 16:21:08 -0700987 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
988 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800989
990 egl_connection_t* const cnx = &gEGLImpl;
991 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800992 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800993 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800994 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
995 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700996#if EGL_TRACE
Mathias Agopianada798b2012-02-13 17:09:30 -0800997 debugHooks->ext.extensions[slot] =
998 gHooksTrace.ext.extensions[slot] =
Mathias Agopian518ec112011-05-13 16:21:08 -0700999#endif
Mathias Agopianada798b2012-02-13 17:09:30 -08001000 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001001 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001002 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001003
Mathias Agopian518ec112011-05-13 16:21:08 -07001004 if (found) {
1005 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001006 sGLExtentionMap.add(name, addr);
1007 sGLExtentionSlot++;
1008 }
1009 }
1010
1011 pthread_mutex_unlock(&sExtensionMapMutex);
1012 return addr;
1013}
1014
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001015class FrameCompletionThread : public Thread {
1016public:
1017
1018 static void queueSync(EGLSyncKHR sync) {
1019 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1020 static bool running = false;
1021 if (!running) {
1022 thread->run("GPUFrameCompletion");
1023 running = true;
1024 }
1025 {
1026 Mutex::Autolock lock(thread->mMutex);
1027 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1028 thread->mFramesQueued).string());
1029 thread->mQueue.push_back(sync);
1030 thread->mCondition.signal();
1031 thread->mFramesQueued++;
1032 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1033 }
1034 }
1035
1036private:
1037 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1038
1039 virtual bool threadLoop() {
1040 EGLSyncKHR sync;
1041 uint32_t frameNum;
1042 {
1043 Mutex::Autolock lock(mMutex);
1044 while (mQueue.isEmpty()) {
1045 mCondition.wait(mMutex);
1046 }
1047 sync = mQueue[0];
1048 frameNum = mFramesCompleted;
1049 }
1050 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1051 {
1052 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1053 frameNum).string());
1054 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1055 if (result == EGL_FALSE) {
1056 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1057 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1058 ALOGE("FrameCompletion: timeout waiting for fence");
1059 }
1060 eglDestroySyncKHR(dpy, sync);
1061 }
1062 {
1063 Mutex::Autolock lock(mMutex);
1064 mQueue.removeAt(0);
1065 mFramesCompleted++;
1066 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1067 }
1068 return true;
1069 }
1070
1071 uint32_t mFramesQueued;
1072 uint32_t mFramesCompleted;
1073 Vector<EGLSyncKHR> mQueue;
1074 Condition mCondition;
1075 Mutex mMutex;
1076};
1077
Dan Stozaa894d082015-02-19 15:27:36 -08001078EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1079 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001080{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001081 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001082 clearError();
1083
Jesse Hallb29e5e82012-04-04 16:53:42 -07001084 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001085 if (!dp) return EGL_FALSE;
1086
Jesse Hallb29e5e82012-04-04 16:53:42 -07001087 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001088 if (!_s.get())
1089 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001090
Siva Velusamy0469dd62011-11-30 15:05:37 -08001091#if EGL_TRACE
Siva Velusamya73a9772012-12-18 14:56:55 -08001092 gl_hooks_t const *trace_hooks = getGLTraceThreadSpecific();
1093 if (getEGLDebugLevel() > 0) {
1094 if (trace_hooks == NULL) {
1095 if (GLTrace_start() < 0) {
1096 ALOGE("Disabling Tracer for OpenGL ES");
1097 setEGLDebugLevel(0);
1098 } else {
1099 // switch over to the trace version of hooks
1100 EGLContext ctx = egl_tls_t::getContext();
1101 egl_context_t * const c = get_context(ctx);
1102 if (c) {
1103 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1104 GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx);
1105 }
1106 }
1107 }
1108
Siva Velusamy0469dd62011-11-30 15:05:37 -08001109 GLTrace_eglSwapBuffers(dpy, draw);
Siva Velusamya73a9772012-12-18 14:56:55 -08001110 } else if (trace_hooks != NULL) {
1111 // tracing is now disabled, so switch back to the non trace version
1112 EGLContext ctx = egl_tls_t::getContext();
1113 egl_context_t * const c = get_context(ctx);
1114 if (c) setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1115 GLTrace_stop();
1116 }
Siva Velusamy0469dd62011-11-30 15:05:37 -08001117#endif
1118
Mathias Agopian518ec112011-05-13 16:21:08 -07001119 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001120
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001121 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1122 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1123 if (sync != EGL_NO_SYNC_KHR) {
1124 FrameCompletionThread::queueSync(sync);
1125 }
1126 }
1127
Mathias Agopian7db993a2012-03-25 00:49:46 -07001128 if (CC_UNLIKELY(dp->finishOnSwap)) {
1129 uint32_t pixel;
1130 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1131 if (c) {
1132 // glReadPixels() ensures that the frame is complete
1133 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1134 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1135 }
1136 }
1137
Dan Stozaa894d082015-02-19 15:27:36 -08001138 if (n_rects == 0) {
1139 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1140 }
1141
1142 Vector<android_native_rect_t> androidRects;
1143 for (int r = 0; r < n_rects; ++r) {
1144 int offset = r * 4;
1145 int x = rects[offset];
1146 int y = rects[offset + 1];
1147 int width = rects[offset + 2];
1148 int height = rects[offset + 3];
1149 android_native_rect_t androidRect;
1150 androidRect.left = x;
1151 androidRect.top = y + height;
1152 androidRect.right = x + width;
1153 androidRect.bottom = y;
1154 androidRects.push_back(androidRect);
1155 }
1156 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1157 androidRects.size());
1158
1159 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1160 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1161 rects, n_rects);
1162 } else {
1163 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1164 }
1165}
1166
1167EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1168{
1169 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001170}
1171
1172EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1173 NativePixmapType target)
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_FALSE;
1179
Jesse Hallb29e5e82012-04-04 16:53:42 -07001180 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001181 if (!_s.get())
1182 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001183
Mathias Agopian518ec112011-05-13 16:21:08 -07001184 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001185 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001186}
1187
1188const char* eglQueryString(EGLDisplay dpy, EGLint name)
1189{
1190 clearError();
1191
Jesse Hallb29e5e82012-04-04 16:53:42 -07001192 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001193 if (!dp) return (const char *) NULL;
1194
1195 switch (name) {
1196 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001197 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001199 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001200 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001201 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001202 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001203 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001204 }
1205 return setError(EGL_BAD_PARAMETER, (const char *)0);
1206}
1207
Mathias Agopianca088332013-03-28 17:44:13 -07001208EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1209{
1210 clearError();
1211
1212 const egl_display_ptr dp = validate_display(dpy);
1213 if (!dp) return (const char *) NULL;
1214
1215 switch (name) {
1216 case EGL_VENDOR:
1217 return dp->disp.queryString.vendor;
1218 case EGL_VERSION:
1219 return dp->disp.queryString.version;
1220 case EGL_EXTENSIONS:
1221 return dp->disp.queryString.extensions;
1222 case EGL_CLIENT_APIS:
1223 return dp->disp.queryString.clientApi;
1224 }
1225 return setError(EGL_BAD_PARAMETER, (const char *)0);
1226}
Mathias Agopian518ec112011-05-13 16:21:08 -07001227
1228// ----------------------------------------------------------------------------
1229// EGL 1.1
1230// ----------------------------------------------------------------------------
1231
1232EGLBoolean eglSurfaceAttrib(
1233 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1234{
1235 clearError();
1236
Jesse Hallb29e5e82012-04-04 16:53:42 -07001237 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001238 if (!dp) return EGL_FALSE;
1239
Jesse Hallb29e5e82012-04-04 16:53:42 -07001240 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001241 if (!_s.get())
1242 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001243
Mathias Agopian518ec112011-05-13 16:21:08 -07001244 egl_surface_t const * const s = get_surface(surface);
1245 if (s->cnx->egl.eglSurfaceAttrib) {
1246 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001247 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001248 }
1249 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1250}
1251
1252EGLBoolean eglBindTexImage(
1253 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1254{
1255 clearError();
1256
Jesse Hallb29e5e82012-04-04 16:53:42 -07001257 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001258 if (!dp) return EGL_FALSE;
1259
Jesse Hallb29e5e82012-04-04 16:53:42 -07001260 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001261 if (!_s.get())
1262 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001263
Mathias Agopian518ec112011-05-13 16:21:08 -07001264 egl_surface_t const * const s = get_surface(surface);
1265 if (s->cnx->egl.eglBindTexImage) {
1266 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001267 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001268 }
1269 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1270}
1271
1272EGLBoolean eglReleaseTexImage(
1273 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1274{
1275 clearError();
1276
Jesse Hallb29e5e82012-04-04 16:53:42 -07001277 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 if (!dp) return EGL_FALSE;
1279
Jesse Hallb29e5e82012-04-04 16:53:42 -07001280 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001281 if (!_s.get())
1282 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001283
Mathias Agopian518ec112011-05-13 16:21:08 -07001284 egl_surface_t const * const s = get_surface(surface);
1285 if (s->cnx->egl.eglReleaseTexImage) {
1286 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001287 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001288 }
1289 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1290}
1291
1292EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1293{
1294 clearError();
1295
Jesse Hallb29e5e82012-04-04 16:53:42 -07001296 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001297 if (!dp) return EGL_FALSE;
1298
1299 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001300 egl_connection_t* const cnx = &gEGLImpl;
1301 if (cnx->dso && cnx->egl.eglSwapInterval) {
1302 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001303 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001304
Mathias Agopian518ec112011-05-13 16:21:08 -07001305 return res;
1306}
1307
1308
1309// ----------------------------------------------------------------------------
1310// EGL 1.2
1311// ----------------------------------------------------------------------------
1312
1313EGLBoolean eglWaitClient(void)
1314{
1315 clearError();
1316
Mathias Agopianada798b2012-02-13 17:09:30 -08001317 egl_connection_t* const cnx = &gEGLImpl;
1318 if (!cnx->dso)
1319 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1320
1321 EGLBoolean res;
1322 if (cnx->egl.eglWaitClient) {
1323 res = cnx->egl.eglWaitClient();
1324 } else {
1325 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001326 }
1327 return res;
1328}
1329
1330EGLBoolean eglBindAPI(EGLenum api)
1331{
1332 clearError();
1333
1334 if (egl_init_drivers() == EGL_FALSE) {
1335 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1336 }
1337
1338 // bind this API on all EGLs
1339 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001340 egl_connection_t* const cnx = &gEGLImpl;
1341 if (cnx->dso && cnx->egl.eglBindAPI) {
1342 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001343 }
1344 return res;
1345}
1346
1347EGLenum eglQueryAPI(void)
1348{
1349 clearError();
1350
1351 if (egl_init_drivers() == EGL_FALSE) {
1352 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1353 }
1354
Mathias Agopianada798b2012-02-13 17:09:30 -08001355 egl_connection_t* const cnx = &gEGLImpl;
1356 if (cnx->dso && cnx->egl.eglQueryAPI) {
1357 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001358 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001359
Mathias Agopian518ec112011-05-13 16:21:08 -07001360 // or, it can only be OpenGL ES
1361 return EGL_OPENGL_ES_API;
1362}
1363
1364EGLBoolean eglReleaseThread(void)
1365{
1366 clearError();
1367
Mathias Agopian4e620dd2013-05-30 16:07:36 -07001368#if EGL_TRACE
1369 if (getEGLDebugLevel() > 0)
1370 GLTrace_eglReleaseThread();
1371#endif
1372
Mathias Agopian518ec112011-05-13 16:21:08 -07001373 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001374 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001375
Mathias Agopianada798b2012-02-13 17:09:30 -08001376 egl_connection_t* const cnx = &gEGLImpl;
1377 if (cnx->dso && cnx->egl.eglReleaseThread) {
1378 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001379 }
1380 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001381 return EGL_TRUE;
1382}
1383
1384EGLSurface eglCreatePbufferFromClientBuffer(
1385 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1386 EGLConfig config, const EGLint *attrib_list)
1387{
1388 clearError();
1389
Jesse Hallb29e5e82012-04-04 16:53:42 -07001390 egl_connection_t* cnx = NULL;
1391 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1392 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001393 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1394 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001395 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001396 }
1397 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1398}
1399
1400// ----------------------------------------------------------------------------
1401// EGL_EGLEXT_VERSION 3
1402// ----------------------------------------------------------------------------
1403
1404EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1405 const EGLint *attrib_list)
1406{
1407 clearError();
1408
Jesse Hallb29e5e82012-04-04 16:53:42 -07001409 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001410 if (!dp) return EGL_FALSE;
1411
Jesse Hallb29e5e82012-04-04 16:53:42 -07001412 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001413 if (!_s.get())
1414 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001415
1416 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001417 if (s->cnx->egl.eglLockSurfaceKHR) {
1418 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001419 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001420 }
1421 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1422}
1423
1424EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1425{
1426 clearError();
1427
Jesse Hallb29e5e82012-04-04 16:53:42 -07001428 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001429 if (!dp) return EGL_FALSE;
1430
Jesse Hallb29e5e82012-04-04 16:53:42 -07001431 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001432 if (!_s.get())
1433 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001434
1435 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001436 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001437 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001438 }
1439 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1440}
1441
1442EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1443 EGLClientBuffer buffer, const EGLint *attrib_list)
1444{
1445 clearError();
1446
Jesse Hallb29e5e82012-04-04 16:53:42 -07001447 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001448 if (!dp) return EGL_NO_IMAGE_KHR;
1449
Jesse Hallb29e5e82012-04-04 16:53:42 -07001450 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001451 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001452
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001453 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1454 egl_connection_t* const cnx = &gEGLImpl;
1455 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1456 result = cnx->egl.eglCreateImageKHR(
1457 dp->disp.dpy,
1458 c ? c->context : EGL_NO_CONTEXT,
1459 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001460 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001461 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001462}
1463
1464EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1465{
1466 clearError();
1467
Jesse Hallb29e5e82012-04-04 16:53:42 -07001468 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001469 if (!dp) return EGL_FALSE;
1470
Steven Holte646a5c52012-06-04 20:02:11 -07001471 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001472 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001473 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001474 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001475 }
Steven Holte646a5c52012-06-04 20:02:11 -07001476 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001477}
1478
1479// ----------------------------------------------------------------------------
1480// EGL_EGLEXT_VERSION 5
1481// ----------------------------------------------------------------------------
1482
1483
1484EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1485{
1486 clearError();
1487
Jesse Hallb29e5e82012-04-04 16:53:42 -07001488 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001489 if (!dp) return EGL_NO_SYNC_KHR;
1490
Mathias Agopian518ec112011-05-13 16:21:08 -07001491 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001492 egl_connection_t* const cnx = &gEGLImpl;
1493 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1494 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001495 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001496 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001497}
1498
1499EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1500{
1501 clearError();
1502
Jesse Hallb29e5e82012-04-04 16:53:42 -07001503 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001504 if (!dp) return EGL_FALSE;
1505
Mathias Agopian518ec112011-05-13 16:21:08 -07001506 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001507 egl_connection_t* const cnx = &gEGLImpl;
1508 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1509 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001510 }
1511 return result;
1512}
1513
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001514EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1515 clearError();
1516
1517 const egl_display_ptr dp = validate_display(dpy);
1518 if (!dp) return EGL_FALSE;
1519
1520 EGLBoolean result = EGL_FALSE;
1521 egl_connection_t* const cnx = &gEGLImpl;
1522 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1523 result = cnx->egl.eglSignalSyncKHR(
1524 dp->disp.dpy, sync, mode);
1525 }
1526 return result;
1527}
1528
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001529EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1530 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001531{
1532 clearError();
1533
Jesse Hallb29e5e82012-04-04 16:53:42 -07001534 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001535 if (!dp) return EGL_FALSE;
1536
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001537 EGLBoolean result = EGL_FALSE;
1538 egl_connection_t* const cnx = &gEGLImpl;
1539 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1540 result = cnx->egl.eglClientWaitSyncKHR(
1541 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001542 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001543 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001544}
1545
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001546EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1547 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001548{
1549 clearError();
1550
Jesse Hallb29e5e82012-04-04 16:53:42 -07001551 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001552 if (!dp) return EGL_FALSE;
1553
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001554 EGLBoolean result = EGL_FALSE;
1555 egl_connection_t* const cnx = &gEGLImpl;
1556 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1557 result = cnx->egl.eglGetSyncAttribKHR(
1558 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001559 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001560 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001561}
1562
Season Li000d88f2015-07-01 11:39:40 -07001563EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1564{
1565 clearError();
1566
1567 const egl_display_ptr dp = validate_display(dpy);
1568 if (!dp) return EGL_NO_STREAM_KHR;
1569
1570 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1571 egl_connection_t* const cnx = &gEGLImpl;
1572 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1573 result = cnx->egl.eglCreateStreamKHR(
1574 dp->disp.dpy, attrib_list);
1575 }
1576 return result;
1577}
1578
1579EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1580{
1581 clearError();
1582
1583 const egl_display_ptr dp = validate_display(dpy);
1584 if (!dp) return EGL_FALSE;
1585
1586 EGLBoolean result = EGL_FALSE;
1587 egl_connection_t* const cnx = &gEGLImpl;
1588 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1589 result = cnx->egl.eglDestroyStreamKHR(
1590 dp->disp.dpy, stream);
1591 }
1592 return result;
1593}
1594
1595EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1596 EGLenum attribute, EGLint value)
1597{
1598 clearError();
1599
1600 const egl_display_ptr dp = validate_display(dpy);
1601 if (!dp) return EGL_FALSE;
1602
1603 EGLBoolean result = EGL_FALSE;
1604 egl_connection_t* const cnx = &gEGLImpl;
1605 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1606 result = cnx->egl.eglStreamAttribKHR(
1607 dp->disp.dpy, stream, attribute, value);
1608 }
1609 return result;
1610}
1611
1612EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1613 EGLenum attribute, EGLint *value)
1614{
1615 clearError();
1616
1617 const egl_display_ptr dp = validate_display(dpy);
1618 if (!dp) return EGL_FALSE;
1619
1620 EGLBoolean result = EGL_FALSE;
1621 egl_connection_t* const cnx = &gEGLImpl;
1622 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1623 result = cnx->egl.eglQueryStreamKHR(
1624 dp->disp.dpy, stream, attribute, value);
1625 }
1626 return result;
1627}
1628
1629EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1630 EGLenum attribute, EGLuint64KHR *value)
1631{
1632 clearError();
1633
1634 const egl_display_ptr dp = validate_display(dpy);
1635 if (!dp) return EGL_FALSE;
1636
1637 EGLBoolean result = EGL_FALSE;
1638 egl_connection_t* const cnx = &gEGLImpl;
1639 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1640 result = cnx->egl.eglQueryStreamu64KHR(
1641 dp->disp.dpy, stream, attribute, value);
1642 }
1643 return result;
1644}
1645
1646EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1647 EGLenum attribute, EGLTimeKHR *value)
1648{
1649 clearError();
1650
1651 const egl_display_ptr dp = validate_display(dpy);
1652 if (!dp) return EGL_FALSE;
1653
1654 EGLBoolean result = EGL_FALSE;
1655 egl_connection_t* const cnx = &gEGLImpl;
1656 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1657 result = cnx->egl.eglQueryStreamTimeKHR(
1658 dp->disp.dpy, stream, attribute, value);
1659 }
1660 return result;
1661}
1662
1663EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1664 EGLStreamKHR stream, const EGLint *attrib_list)
1665{
1666 clearError();
1667
1668 egl_display_ptr dp = validate_display(dpy);
1669 if (!dp) return EGL_NO_SURFACE;
1670
1671 egl_connection_t* const cnx = &gEGLImpl;
1672 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1673 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1674 dp->disp.dpy, config, stream, attrib_list);
1675 if (surface != EGL_NO_SURFACE) {
1676 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1677 surface, cnx);
1678 return s;
1679 }
1680 }
1681 return EGL_NO_SURFACE;
1682}
1683
1684EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1685 EGLStreamKHR stream)
1686{
1687 clearError();
1688
1689 const egl_display_ptr dp = validate_display(dpy);
1690 if (!dp) return EGL_FALSE;
1691
1692 EGLBoolean result = EGL_FALSE;
1693 egl_connection_t* const cnx = &gEGLImpl;
1694 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1695 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1696 dp->disp.dpy, stream);
1697 }
1698 return result;
1699}
1700
1701EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1702 EGLStreamKHR stream)
1703{
1704 clearError();
1705
1706 const egl_display_ptr dp = validate_display(dpy);
1707 if (!dp) return EGL_FALSE;
1708
1709 EGLBoolean result = EGL_FALSE;
1710 egl_connection_t* const cnx = &gEGLImpl;
1711 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1712 result = cnx->egl.eglStreamConsumerAcquireKHR(
1713 dp->disp.dpy, stream);
1714 }
1715 return result;
1716}
1717
1718EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1719 EGLStreamKHR stream)
1720{
1721 clearError();
1722
1723 const egl_display_ptr dp = validate_display(dpy);
1724 if (!dp) return EGL_FALSE;
1725
1726 EGLBoolean result = EGL_FALSE;
1727 egl_connection_t* const cnx = &gEGLImpl;
1728 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1729 result = cnx->egl.eglStreamConsumerReleaseKHR(
1730 dp->disp.dpy, stream);
1731 }
1732 return result;
1733}
1734
1735EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1736 EGLDisplay dpy, EGLStreamKHR stream)
1737{
1738 clearError();
1739
1740 const egl_display_ptr dp = validate_display(dpy);
1741 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1742
1743 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1744 egl_connection_t* const cnx = &gEGLImpl;
1745 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1746 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1747 dp->disp.dpy, stream);
1748 }
1749 return result;
1750}
1751
1752EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1753 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1754{
1755 clearError();
1756
1757 const egl_display_ptr dp = validate_display(dpy);
1758 if (!dp) return EGL_NO_STREAM_KHR;
1759
1760 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1761 egl_connection_t* const cnx = &gEGLImpl;
1762 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1763 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1764 dp->disp.dpy, file_descriptor);
1765 }
1766 return result;
1767}
1768
Mathias Agopian518ec112011-05-13 16:21:08 -07001769// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001770// EGL_EGLEXT_VERSION 15
1771// ----------------------------------------------------------------------------
1772
1773EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1774 clearError();
1775 const egl_display_ptr dp = validate_display(dpy);
1776 if (!dp) return EGL_FALSE;
1777 EGLint result = EGL_FALSE;
1778 egl_connection_t* const cnx = &gEGLImpl;
1779 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1780 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1781 }
1782 return result;
1783}
1784
1785// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001786// ANDROID extensions
1787// ----------------------------------------------------------------------------
1788
Jamie Gennis331841b2012-09-06 14:52:00 -07001789EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1790{
1791 clearError();
1792
1793 const egl_display_ptr dp = validate_display(dpy);
1794 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1795
1796 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1797 egl_connection_t* const cnx = &gEGLImpl;
1798 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1799 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1800 }
1801 return result;
1802}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001803
Andy McFadden72841452013-03-01 16:25:32 -08001804EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1805 EGLnsecsANDROID time)
1806{
1807 clearError();
1808
1809 const egl_display_ptr dp = validate_display(dpy);
1810 if (!dp) {
1811 return EGL_FALSE;
1812 }
1813
1814 SurfaceRef _s(dp.get(), surface);
1815 if (!_s.get()) {
1816 setError(EGL_BAD_SURFACE, EGL_FALSE);
1817 return EGL_FALSE;
1818 }
1819
1820 egl_surface_t const * const s = get_surface(surface);
1821 native_window_set_buffers_timestamp(s->win.get(), time);
1822
1823 return EGL_TRUE;
1824}
1825
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001826// ----------------------------------------------------------------------------
1827// NVIDIA extensions
1828// ----------------------------------------------------------------------------
1829EGLuint64NV eglGetSystemTimeFrequencyNV()
1830{
1831 clearError();
1832
1833 if (egl_init_drivers() == EGL_FALSE) {
1834 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1835 }
1836
1837 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001838 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001839
Mathias Agopianada798b2012-02-13 17:09:30 -08001840 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1841 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001842 }
1843
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001844 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001845}
1846
1847EGLuint64NV eglGetSystemTimeNV()
1848{
1849 clearError();
1850
1851 if (egl_init_drivers() == EGL_FALSE) {
1852 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1853 }
1854
1855 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001856 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001857
Mathias Agopianada798b2012-02-13 17:09:30 -08001858 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1859 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001860 }
1861
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001862 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001863}
Dan Stozaa894d082015-02-19 15:27:36 -08001864
1865// ----------------------------------------------------------------------------
1866// Partial update extension
1867// ----------------------------------------------------------------------------
1868EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1869 EGLint *rects, EGLint n_rects)
1870{
1871 clearError();
1872
1873 const egl_display_ptr dp = validate_display(dpy);
1874 if (!dp) {
1875 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1876 return EGL_FALSE;
1877 }
1878
1879 SurfaceRef _s(dp.get(), surface);
1880 if (!_s.get()) {
1881 setError(EGL_BAD_SURFACE, EGL_FALSE);
1882 return EGL_FALSE;
1883 }
1884
1885 egl_surface_t const * const s = get_surface(surface);
1886 if (s->cnx->egl.eglSetDamageRegionKHR) {
1887 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1888 rects, n_rects);
1889 }
1890
1891 return EGL_FALSE;
1892}