blob: b99757442c4e51eab953cb19a0d4f244ec817d05 [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
Craig Donner05249fc2016-01-15 19:33:55 -080036#include <ui/GraphicBuffer.h>
37
Mathias Agopian518ec112011-05-13 16:21:08 -070038#include <utils/KeyedVector.h>
39#include <utils/SortedVector.h>
40#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080041#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070042
Mathias Agopian39c24a22013-04-04 23:17:56 -070043#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070044#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070045
46#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070047#include "egl_object.h"
48#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080049#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070050
51using namespace android;
52
Jesse Halla2ba4282013-09-14 21:00:14 -070053// This extension has not been ratified yet, so can't be shipped.
54// Implementation is incomplete and untested.
55#define ENABLE_EGL_KHR_GL_COLORSPACE 0
56
Mathias Agopian518ec112011-05-13 16:21:08 -070057// ----------------------------------------------------------------------------
58
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070059namespace android {
60
Mathias Agopian518ec112011-05-13 16:21:08 -070061struct extention_map_t {
62 const char* name;
63 __eglMustCastToProperFunctionPointerType address;
64};
65
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070066/*
Jesse Hall21558da2013-08-06 15:31:22 -070067 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070068 *
Jesse Hall21558da2013-08-06 15:31:22 -070069 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
70 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070071 *
Jesse Hall21558da2013-08-06 15:31:22 -070072 * The rest (gExtensionString) depend on support in the EGL driver, and are
73 * only available if the driver supports them. However, some of these must be
74 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080075 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070076 * the mandatory extensions are present and may not function properly if some
77 * are missing.
78 *
79 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070080 */
Jesse Hall21558da2013-08-06 15:31:22 -070081extern char const * const gBuiltinExtensionString =
82 "EGL_KHR_get_all_proc_addresses "
83 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080084 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080085 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080086 "EGL_ANDROID_front_buffer_auto_refresh "
Jesse Hall21558da2013-08-06 15:31:22 -070087 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070088extern char const * const gExtensionString =
89 "EGL_KHR_image " // mandatory
90 "EGL_KHR_image_base " // mandatory
91 "EGL_KHR_image_pixmap "
92 "EGL_KHR_lock_surface "
Jesse Halla2ba4282013-09-14 21:00:14 -070093#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
Jesse Hallc2e41222013-08-08 13:40:22 -070094 "EGL_KHR_gl_colorspace "
Jesse Halla2ba4282013-09-14 21:00:14 -070095#endif
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070096 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070097 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070098 "EGL_KHR_gl_texture_cubemap_image "
99 "EGL_KHR_gl_renderbuffer_image "
100 "EGL_KHR_reusable_sync "
101 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700102 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700103 "EGL_KHR_config_attribs "
104 "EGL_KHR_surfaceless_context "
105 "EGL_KHR_stream "
106 "EGL_KHR_stream_fifo "
107 "EGL_KHR_stream_producer_eglsurface "
108 "EGL_KHR_stream_consumer_gltexture "
109 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700110 "EGL_EXT_create_context_robustness "
111 "EGL_NV_system_time "
112 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700113 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700114 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800115 "EGL_KHR_partial_update " // strongly recommended
116 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700117 "EGL_KHR_create_context_no_error "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700118 ;
119
120// extensions not exposed to applications but used by the ANDROID system
121// "EGL_ANDROID_blob_cache " // strongly recommended
122// "EGL_IMG_hibernate_process " // optional
123// "EGL_ANDROID_native_fence_sync " // strongly recommended
124// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700125// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700126
127/*
128 * EGL Extensions entry-points exposed to 3rd party applications
129 * (keep in sync with gExtensionString above)
130 *
131 */
132static const extention_map_t sExtensionMap[] = {
133 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700134 { "eglLockSurfaceKHR",
135 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
136 { "eglUnlockSurfaceKHR",
137 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700138
139 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700140 { "eglCreateImageKHR",
141 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
142 { "eglDestroyImageKHR",
143 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700144
145 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
146 { "eglCreateSyncKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
148 { "eglDestroySyncKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
150 { "eglClientWaitSyncKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
152 { "eglSignalSyncKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
154 { "eglGetSyncAttribKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
156
157 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800158 { "eglGetSystemTimeFrequencyNV",
159 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
160 { "eglGetSystemTimeNV",
161 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700162
Mathias Agopian2bb71682013-03-27 17:32:41 -0700163 // EGL_KHR_wait_sync
164 { "eglWaitSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700166
167 // EGL_ANDROID_presentation_time
168 { "eglPresentationTimeANDROID",
169 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800170
171 // EGL_KHR_swap_buffers_with_damage
172 { "eglSwapBuffersWithDamageKHR",
173 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
174
Craig Donner05249fc2016-01-15 19:33:55 -0800175 // EGL_ANDROID_native_client_buffer
176 { "eglCreateNativeClientBufferANDROID",
177 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
178
Dan Stozaa894d082015-02-19 15:27:36 -0800179 // EGL_KHR_partial_update
180 { "eglSetDamageRegionKHR",
181 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700182
183 { "eglCreateStreamKHR",
184 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
185 { "eglDestroyStreamKHR",
186 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
187 { "eglStreamAttribKHR",
188 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
189 { "eglQueryStreamKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
191 { "eglQueryStreamu64KHR",
192 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
193 { "eglQueryStreamTimeKHR",
194 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
195 { "eglCreateStreamProducerSurfaceKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
197 { "eglStreamConsumerGLTextureExternalKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
199 { "eglStreamConsumerAcquireKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
201 { "eglStreamConsumerReleaseKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
203 { "eglGetStreamFileDescriptorKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
205 { "eglCreateStreamFromFileDescriptorKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Mathias Agopian518ec112011-05-13 16:21:08 -0700207};
208
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700209/*
210 * These extensions entry-points should not be exposed to applications.
211 * They're used internally by the Android EGL layer.
212 */
213#define FILTER_EXTENSIONS(procname) \
214 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
215 !strcmp((procname), "eglHibernateProcessIMG") || \
216 !strcmp((procname), "eglAwakenProcessIMG") || \
217 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
218
219
220
Mathias Agopian518ec112011-05-13 16:21:08 -0700221// accesses protected by sExtensionMapMutex
222static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
223static int sGLExtentionSlot = 0;
224static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
225
226static void(*findProcAddress(const char* name,
227 const extention_map_t* map, size_t n))() {
228 for (uint32_t i=0 ; i<n ; i++) {
229 if (!strcmp(name, map[i].name)) {
230 return map[i].address;
231 }
232 }
233 return NULL;
234}
235
236// ----------------------------------------------------------------------------
237
Mathias Agopian518ec112011-05-13 16:21:08 -0700238extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
239extern EGLBoolean egl_init_drivers();
240extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700241extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700242
Mathias Agopian518ec112011-05-13 16:21:08 -0700243} // namespace android;
244
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700245
Mathias Agopian518ec112011-05-13 16:21:08 -0700246// ----------------------------------------------------------------------------
247
248static inline void clearError() { egl_tls_t::clearError(); }
249static inline EGLContext getContext() { return egl_tls_t::getContext(); }
250
251// ----------------------------------------------------------------------------
252
253EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
254{
255 clearError();
256
Dan Stozac3289c42014-01-17 11:38:34 -0800257 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700258 if (index >= NUM_DISPLAYS) {
259 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
260 }
261
262 if (egl_init_drivers() == EGL_FALSE) {
263 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
264 }
265
266 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
267 return dpy;
268}
269
270// ----------------------------------------------------------------------------
271// Initialization
272// ----------------------------------------------------------------------------
273
274EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
275{
276 clearError();
277
Jesse Hallb29e5e82012-04-04 16:53:42 -0700278 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700279 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
280
281 EGLBoolean res = dp->initialize(major, minor);
282
283 return res;
284}
285
286EGLBoolean eglTerminate(EGLDisplay dpy)
287{
288 // NOTE: don't unload the drivers b/c some APIs can be called
289 // after eglTerminate() has been called. eglTerminate() only
290 // terminates an EGLDisplay, not a EGL itself.
291
292 clearError();
293
Jesse Hallb29e5e82012-04-04 16:53:42 -0700294 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700295 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
296
297 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800298
Mathias Agopian518ec112011-05-13 16:21:08 -0700299 return res;
300}
301
302// ----------------------------------------------------------------------------
303// configuration
304// ----------------------------------------------------------------------------
305
306EGLBoolean eglGetConfigs( EGLDisplay dpy,
307 EGLConfig *configs,
308 EGLint config_size, EGLint *num_config)
309{
310 clearError();
311
Jesse Hallb29e5e82012-04-04 16:53:42 -0700312 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700313 if (!dp) return EGL_FALSE;
314
Mathias Agopian7773c432012-02-13 20:06:08 -0800315 if (num_config==0) {
316 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700317 }
318
Mathias Agopian7773c432012-02-13 20:06:08 -0800319 EGLBoolean res = EGL_FALSE;
320 *num_config = 0;
321
322 egl_connection_t* const cnx = &gEGLImpl;
323 if (cnx->dso) {
324 res = cnx->egl.eglGetConfigs(
325 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700326 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800327
328 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700329}
330
331EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
332 EGLConfig *configs, EGLint config_size,
333 EGLint *num_config)
334{
335 clearError();
336
Jesse Hallb29e5e82012-04-04 16:53:42 -0700337 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700338 if (!dp) return EGL_FALSE;
339
340 if (num_config==0) {
341 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
342 }
343
Mathias Agopian518ec112011-05-13 16:21:08 -0700344 EGLBoolean res = EGL_FALSE;
345 *num_config = 0;
346
Mathias Agopianada798b2012-02-13 17:09:30 -0800347 egl_connection_t* const cnx = &gEGLImpl;
348 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700349 if (attrib_list) {
350 char value[PROPERTY_VALUE_MAX];
351 property_get("debug.egl.force_msaa", value, "false");
352
353 if (!strcmp(value, "true")) {
354 size_t attribCount = 0;
355 EGLint attrib = attrib_list[0];
356
357 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700358 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700359 const EGLint *attribRendererable = NULL;
360 const EGLint *attribCaveat = NULL;
361
362 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700363 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700364 while (attrib != EGL_NONE) {
365 attrib = attrib_list[attribCount];
366 switch (attrib) {
367 case EGL_RENDERABLE_TYPE:
368 attribRendererable = &attrib_list[attribCount];
369 break;
370 case EGL_CONFIG_CAVEAT:
371 attribCaveat = &attrib_list[attribCount];
372 break;
373 }
374 attribCount++;
375 }
376
377 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
378 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800379
Romain Guy1cffc802012-10-15 18:13:05 -0700380 // Insert 2 extra attributes to force-enable MSAA 4x
381 EGLint aaAttribs[attribCount + 4];
382 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
383 aaAttribs[1] = 1;
384 aaAttribs[2] = EGL_SAMPLES;
385 aaAttribs[3] = 4;
386
387 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
388
389 EGLint numConfigAA;
390 EGLBoolean resAA = cnx->egl.eglChooseConfig(
391 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
392
393 if (resAA == EGL_TRUE && numConfigAA > 0) {
394 ALOGD("Enabling MSAA 4x");
395 *num_config = numConfigAA;
396 return resAA;
397 }
398 }
399 }
400 }
401
Mathias Agopian7773c432012-02-13 20:06:08 -0800402 res = cnx->egl.eglChooseConfig(
403 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700404 }
405 return res;
406}
407
408EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
409 EGLint attribute, EGLint *value)
410{
411 clearError();
412
Jesse Hallb29e5e82012-04-04 16:53:42 -0700413 egl_connection_t* cnx = NULL;
414 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
415 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800416
Mathias Agopian518ec112011-05-13 16:21:08 -0700417 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800418 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700419}
420
421// ----------------------------------------------------------------------------
422// surfaces
423// ----------------------------------------------------------------------------
424
Jesse Halla2ba4282013-09-14 21:00:14 -0700425// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
Jesse Hallc2e41222013-08-08 13:40:22 -0700426// been added to the Khronos egl.h.
427#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE
428#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB
429#define EGL_GL_COLORSPACE_LINEAR_KHR EGL_VG_COLORSPACE_LINEAR
430
431// Turn linear formats into corresponding sRGB formats when colorspace is
432// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
433// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800434// the modification isn't possible, the original dataSpace is returned.
435static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
436 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700437 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800438 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700439 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800440 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700441 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800442 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700443}
444
Mathias Agopian518ec112011-05-13 16:21:08 -0700445EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
446 NativeWindowType window,
447 const EGLint *attrib_list)
448{
449 clearError();
450
Jesse Hallb29e5e82012-04-04 16:53:42 -0700451 egl_connection_t* cnx = NULL;
452 egl_display_ptr dp = validate_display_connection(dpy, cnx);
453 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800454 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700455
Andy McFaddend566ce32014-01-07 15:54:17 -0800456 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
457 if (result != OK) {
458 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
459 "failed (%#x) (already connected to another API?)",
460 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700461 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700462 }
463
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700464 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700465 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
466 // of our native format. So if sRGB gamma is requested, we have to
467 // modify the EGLconfig's format before setting the native window's
468 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800469
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700470 // by default, just pick RGBA_8888
471 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800472 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700473
474 EGLint a = 0;
475 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
476 if (a > 0) {
477 // alpha-channel requested, there's really only one suitable format
478 format = HAL_PIXEL_FORMAT_RGBA_8888;
479 } else {
480 EGLint r, g, b;
481 r = g = b = 0;
482 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
483 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
484 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
485 EGLint colorDepth = r + g + b;
486 if (colorDepth <= 16) {
487 format = HAL_PIXEL_FORMAT_RGB_565;
488 } else {
489 format = HAL_PIXEL_FORMAT_RGBX_8888;
490 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700491 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700492
493 // now select a corresponding sRGB format if needed
494 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
495 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
496 if (*attr == EGL_GL_COLORSPACE_KHR) {
Jesse Halla2ba4282013-09-14 21:00:14 -0700497 if (ENABLE_EGL_KHR_GL_COLORSPACE) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800498 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jesse Halla2ba4282013-09-14 21:00:14 -0700499 } else {
500 // Normally we'd pass through unhandled attributes to
501 // the driver. But in case the driver implements this
502 // extension but we're disabling it, we want to prevent
503 // it getting through -- support will be broken without
504 // our help.
505 ALOGE("sRGB window surfaces not supported");
506 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
507 }
Jamie Gennisbee205f2011-07-01 13:12:07 -0700508 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700509 }
510 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800511
Jesse Hallc2e41222013-08-08 13:40:22 -0700512 if (format != 0) {
513 int err = native_window_set_buffers_format(window, format);
514 if (err != 0) {
515 ALOGE("error setting native window pixel format: %s (%d)",
516 strerror(-err), err);
517 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
518 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
519 }
520 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700521
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800522 if (dataSpace != 0) {
523 int err = native_window_set_buffers_data_space(window, dataSpace);
524 if (err != 0) {
525 ALOGE("error setting native window pixel dataSpace: %s (%d)",
526 strerror(-err), err);
527 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
528 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
529 }
530 }
531
Jamie Gennis59769462011-11-19 18:04:43 -0800532 // the EGL spec requires that a new EGLSurface default to swap interval
533 // 1, so explicitly set that on the window here.
534 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
535 anw->setSwapInterval(anw, 1);
536
Mathias Agopian518ec112011-05-13 16:21:08 -0700537 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800538 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700539 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700540 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
541 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700542 return s;
543 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700544
545 // EGLSurface creation failed
546 native_window_set_buffers_format(window, 0);
547 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700548 }
549 return EGL_NO_SURFACE;
550}
551
552EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
553 NativePixmapType pixmap,
554 const EGLint *attrib_list)
555{
556 clearError();
557
Jesse Hallb29e5e82012-04-04 16:53:42 -0700558 egl_connection_t* cnx = NULL;
559 egl_display_ptr dp = validate_display_connection(dpy, cnx);
560 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700561 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800562 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700563 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700564 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
565 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700566 return s;
567 }
568 }
569 return EGL_NO_SURFACE;
570}
571
572EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
573 const EGLint *attrib_list)
574{
575 clearError();
576
Jesse Hallb29e5e82012-04-04 16:53:42 -0700577 egl_connection_t* cnx = NULL;
578 egl_display_ptr dp = validate_display_connection(dpy, cnx);
579 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800581 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700582 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700583 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
584 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700585 return s;
586 }
587 }
588 return EGL_NO_SURFACE;
589}
Jesse Hall47743382013-02-08 11:13:46 -0800590
Mathias Agopian518ec112011-05-13 16:21:08 -0700591EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
592{
593 clearError();
594
Jesse Hallb29e5e82012-04-04 16:53:42 -0700595 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700596 if (!dp) return EGL_FALSE;
597
Jesse Hallb29e5e82012-04-04 16:53:42 -0700598 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700599 if (!_s.get())
600 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700601
602 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800603 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700604 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700605 _s.terminate();
Pablo Ceballos541de492016-04-09 15:56:12 -0700606 dp->removeSurface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700607 }
608 return result;
609}
610
611EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
612 EGLint attribute, EGLint *value)
613{
614 clearError();
615
Jesse Hallb29e5e82012-04-04 16:53:42 -0700616 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700617 if (!dp) return EGL_FALSE;
618
Jesse Hallb29e5e82012-04-04 16:53:42 -0700619 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700620 if (!_s.get())
621 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700622
Mathias Agopian518ec112011-05-13 16:21:08 -0700623 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800624 return s->cnx->egl.eglQuerySurface(
625 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700626}
627
Jamie Gennise8696a42012-01-15 18:54:57 -0800628void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800629 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800630 clearError();
631
Jesse Hallb29e5e82012-04-04 16:53:42 -0700632 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800633 if (!dp) {
634 return;
635 }
636
Jesse Hallb29e5e82012-04-04 16:53:42 -0700637 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800638 if (!_s.get()) {
639 setError(EGL_BAD_SURFACE, EGL_FALSE);
640 return;
641 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800642}
643
Mathias Agopian518ec112011-05-13 16:21:08 -0700644// ----------------------------------------------------------------------------
645// Contexts
646// ----------------------------------------------------------------------------
647
648EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
649 EGLContext share_list, const EGLint *attrib_list)
650{
651 clearError();
652
Jesse Hallb29e5e82012-04-04 16:53:42 -0700653 egl_connection_t* cnx = NULL;
654 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700655 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700656 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700657 if (!ContextRef(dp.get(), share_list).get()) {
658 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
659 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700660 egl_context_t* const c = get_context(share_list);
661 share_list = c->context;
662 }
663 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800664 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700665 if (context != EGL_NO_CONTEXT) {
666 // figure out if it's a GLESv1 or GLESv2
667 int version = 0;
668 if (attrib_list) {
669 while (*attrib_list != EGL_NONE) {
670 GLint attr = *attrib_list++;
671 GLint value = *attrib_list++;
672 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
673 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800674 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800675 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800676 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700677 }
678 }
679 };
680 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700681 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
682 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700683 return c;
684 }
685 }
686 return EGL_NO_CONTEXT;
687}
688
689EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
690{
691 clearError();
692
Jesse Hallb29e5e82012-04-04 16:53:42 -0700693 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700694 if (!dp)
695 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700696
Jesse Hallb29e5e82012-04-04 16:53:42 -0700697 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700698 if (!_c.get())
699 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800700
Mathias Agopian518ec112011-05-13 16:21:08 -0700701 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800702 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700703 if (result == EGL_TRUE) {
704 _c.terminate();
705 }
706 return result;
707}
708
Mathias Agopian518ec112011-05-13 16:21:08 -0700709EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
710 EGLSurface read, EGLContext ctx)
711{
712 clearError();
713
Jesse Hallb29e5e82012-04-04 16:53:42 -0700714 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700715 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
716
Mathias Agopian5b287a62011-05-16 18:58:55 -0700717 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
718 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
719 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700720 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
721 (draw != EGL_NO_SURFACE) ) {
722 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
723 }
724
725 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700726 ContextRef _c(dp.get(), ctx);
727 SurfaceRef _d(dp.get(), draw);
728 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700729
730 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700731 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700732 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700733 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700734 }
735
736 // these are the underlying implementation's object
737 EGLContext impl_ctx = EGL_NO_CONTEXT;
738 EGLSurface impl_draw = EGL_NO_SURFACE;
739 EGLSurface impl_read = EGL_NO_SURFACE;
740
741 // these are our objects structs passed in
742 egl_context_t * c = NULL;
743 egl_surface_t const * d = NULL;
744 egl_surface_t const * r = NULL;
745
746 // these are the current objects structs
747 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800748
Mathias Agopian518ec112011-05-13 16:21:08 -0700749 if (ctx != EGL_NO_CONTEXT) {
750 c = get_context(ctx);
751 impl_ctx = c->context;
752 } else {
753 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700754 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
755 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
756 return setError(EGL_BAD_MATCH, EGL_FALSE);
757 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 if (cur_c == NULL) {
759 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700760 // not an error, there is just no current context.
761 return EGL_TRUE;
762 }
763 }
764
765 // retrieve the underlying implementation's draw EGLSurface
766 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700767 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700768 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 impl_draw = d->surface;
770 }
771
772 // retrieve the underlying implementation's read EGLSurface
773 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700774 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700775 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700776 impl_read = r->surface;
777 }
778
Mathias Agopian518ec112011-05-13 16:21:08 -0700779
Jesse Hallb29e5e82012-04-04 16:53:42 -0700780 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800781 draw, read, ctx,
782 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700783
784 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800785 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700786 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
787 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700788 _c.acquire();
789 _r.acquire();
790 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700791 } else {
792 setGLHooksThreadSpecific(&gHooksNoContext);
793 egl_tls_t::setContext(EGL_NO_CONTEXT);
794 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700795 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000796 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700797 egl_connection_t* const cnx = &gEGLImpl;
798 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 }
800 return result;
801}
802
803
804EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
805 EGLint attribute, EGLint *value)
806{
807 clearError();
808
Jesse Hallb29e5e82012-04-04 16:53:42 -0700809 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 if (!dp) return EGL_FALSE;
811
Jesse Hallb29e5e82012-04-04 16:53:42 -0700812 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700813 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
814
Mathias Agopian518ec112011-05-13 16:21:08 -0700815 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800816 return c->cnx->egl.eglQueryContext(
817 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700818
Mathias Agopian518ec112011-05-13 16:21:08 -0700819}
820
821EGLContext eglGetCurrentContext(void)
822{
823 // could be called before eglInitialize(), but we wouldn't have a context
824 // then, and this function would correctly return EGL_NO_CONTEXT.
825
826 clearError();
827
828 EGLContext ctx = getContext();
829 return ctx;
830}
831
832EGLSurface eglGetCurrentSurface(EGLint readdraw)
833{
834 // could be called before eglInitialize(), but we wouldn't have a context
835 // then, and this function would correctly return EGL_NO_SURFACE.
836
837 clearError();
838
839 EGLContext ctx = getContext();
840 if (ctx) {
841 egl_context_t const * const c = get_context(ctx);
842 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
843 switch (readdraw) {
844 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800845 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700846 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
847 }
848 }
849 return EGL_NO_SURFACE;
850}
851
852EGLDisplay eglGetCurrentDisplay(void)
853{
854 // could be called before eglInitialize(), but we wouldn't have a context
855 // then, and this function would correctly return EGL_NO_DISPLAY.
856
857 clearError();
858
859 EGLContext ctx = getContext();
860 if (ctx) {
861 egl_context_t const * const c = get_context(ctx);
862 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
863 return c->dpy;
864 }
865 return EGL_NO_DISPLAY;
866}
867
868EGLBoolean eglWaitGL(void)
869{
Mathias Agopian518ec112011-05-13 16:21:08 -0700870 clearError();
871
Mathias Agopianada798b2012-02-13 17:09:30 -0800872 egl_connection_t* const cnx = &gEGLImpl;
873 if (!cnx->dso)
874 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
875
876 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700877}
878
879EGLBoolean eglWaitNative(EGLint engine)
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.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700888}
889
890EGLint eglGetError(void)
891{
Mathias Agopianada798b2012-02-13 17:09:30 -0800892 EGLint err = EGL_SUCCESS;
893 egl_connection_t* const cnx = &gEGLImpl;
894 if (cnx->dso) {
895 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700896 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800897 if (err == EGL_SUCCESS) {
898 err = egl_tls_t::getError();
899 }
900 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700901}
902
Michael Chockc0ec5e22014-01-27 08:14:33 -0800903static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700904 const char* procname) {
905 const egl_connection_t* cnx = &gEGLImpl;
906 void* proc = NULL;
907
Michael Chockc0ec5e22014-01-27 08:14:33 -0800908 proc = dlsym(cnx->libEgl, procname);
909 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
910
Jesse Hallc07b5202013-07-04 12:08:16 -0700911 proc = dlsym(cnx->libGles2, procname);
912 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
913
914 proc = dlsym(cnx->libGles1, procname);
915 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
916
917 return NULL;
918}
919
Mathias Agopian518ec112011-05-13 16:21:08 -0700920__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
921{
922 // eglGetProcAddress() could be the very first function called
923 // in which case we must make sure we've initialized ourselves, this
924 // happens the first time egl_get_display() is called.
925
926 clearError();
927
928 if (egl_init_drivers() == EGL_FALSE) {
929 setError(EGL_BAD_PARAMETER, NULL);
930 return NULL;
931 }
932
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700933 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700934 return NULL;
935 }
936
Mathias Agopian518ec112011-05-13 16:21:08 -0700937 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700938 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700939 if (addr) return addr;
940
Michael Chockc0ec5e22014-01-27 08:14:33 -0800941 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700942 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700943
Mathias Agopian518ec112011-05-13 16:21:08 -0700944 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
945 pthread_mutex_lock(&sExtensionMapMutex);
946
947 /*
948 * Since eglGetProcAddress() is not associated to anything, it needs
949 * to return a function pointer that "works" regardless of what
950 * the current context is.
951 *
952 * For this reason, we return a "forwarder", a small stub that takes
953 * care of calling the function associated with the context
954 * currently bound.
955 *
956 * We first look for extensions we've already resolved, if we're seeing
957 * this extension for the first time, we go through all our
958 * implementations and call eglGetProcAddress() and record the
959 * result in the appropriate implementation hooks and return the
960 * address of the forwarder corresponding to that hook set.
961 *
962 */
963
964 const String8 name(procname);
965 addr = sGLExtentionMap.valueFor(name);
966 const int slot = sGLExtentionSlot;
967
Steve Blocke6f43dd2012-01-06 19:20:56 +0000968 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 "no more slots for eglGetProcAddress(\"%s\")",
970 procname);
971
972 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
973 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800974
975 egl_connection_t* const cnx = &gEGLImpl;
976 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800977 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800978 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800979 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
980 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800981 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800982 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800984
Mathias Agopian518ec112011-05-13 16:21:08 -0700985 if (found) {
986 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700987 sGLExtentionMap.add(name, addr);
988 sGLExtentionSlot++;
989 }
990 }
991
992 pthread_mutex_unlock(&sExtensionMapMutex);
993 return addr;
994}
995
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700996class FrameCompletionThread : public Thread {
997public:
998
999 static void queueSync(EGLSyncKHR sync) {
1000 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1001 static bool running = false;
1002 if (!running) {
1003 thread->run("GPUFrameCompletion");
1004 running = true;
1005 }
1006 {
1007 Mutex::Autolock lock(thread->mMutex);
1008 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1009 thread->mFramesQueued).string());
1010 thread->mQueue.push_back(sync);
1011 thread->mCondition.signal();
1012 thread->mFramesQueued++;
1013 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1014 }
1015 }
1016
1017private:
1018 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1019
1020 virtual bool threadLoop() {
1021 EGLSyncKHR sync;
1022 uint32_t frameNum;
1023 {
1024 Mutex::Autolock lock(mMutex);
1025 while (mQueue.isEmpty()) {
1026 mCondition.wait(mMutex);
1027 }
1028 sync = mQueue[0];
1029 frameNum = mFramesCompleted;
1030 }
1031 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1032 {
1033 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1034 frameNum).string());
1035 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1036 if (result == EGL_FALSE) {
1037 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1038 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1039 ALOGE("FrameCompletion: timeout waiting for fence");
1040 }
1041 eglDestroySyncKHR(dpy, sync);
1042 }
1043 {
1044 Mutex::Autolock lock(mMutex);
1045 mQueue.removeAt(0);
1046 mFramesCompleted++;
1047 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1048 }
1049 return true;
1050 }
1051
1052 uint32_t mFramesQueued;
1053 uint32_t mFramesCompleted;
1054 Vector<EGLSyncKHR> mQueue;
1055 Condition mCondition;
1056 Mutex mMutex;
1057};
1058
Dan Stozaa894d082015-02-19 15:27:36 -08001059EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1060 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001061{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001062 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001063 clearError();
1064
Jesse Hallb29e5e82012-04-04 16:53:42 -07001065 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001066 if (!dp) return EGL_FALSE;
1067
Jesse Hallb29e5e82012-04-04 16:53:42 -07001068 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001069 if (!_s.get())
1070 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001071
Mathias Agopian518ec112011-05-13 16:21:08 -07001072 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001073
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001074 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1075 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1076 if (sync != EGL_NO_SYNC_KHR) {
1077 FrameCompletionThread::queueSync(sync);
1078 }
1079 }
1080
Mathias Agopian7db993a2012-03-25 00:49:46 -07001081 if (CC_UNLIKELY(dp->finishOnSwap)) {
1082 uint32_t pixel;
1083 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1084 if (c) {
1085 // glReadPixels() ensures that the frame is complete
1086 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1087 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1088 }
1089 }
1090
Dan Stozaa894d082015-02-19 15:27:36 -08001091 if (n_rects == 0) {
1092 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1093 }
1094
1095 Vector<android_native_rect_t> androidRects;
1096 for (int r = 0; r < n_rects; ++r) {
1097 int offset = r * 4;
1098 int x = rects[offset];
1099 int y = rects[offset + 1];
1100 int width = rects[offset + 2];
1101 int height = rects[offset + 3];
1102 android_native_rect_t androidRect;
1103 androidRect.left = x;
1104 androidRect.top = y + height;
1105 androidRect.right = x + width;
1106 androidRect.bottom = y;
1107 androidRects.push_back(androidRect);
1108 }
1109 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1110 androidRects.size());
1111
1112 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1113 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1114 rects, n_rects);
1115 } else {
1116 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1117 }
1118}
1119
1120EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1121{
1122 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001123}
1124
1125EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1126 NativePixmapType target)
1127{
1128 clearError();
1129
Jesse Hallb29e5e82012-04-04 16:53:42 -07001130 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001131 if (!dp) return EGL_FALSE;
1132
Jesse Hallb29e5e82012-04-04 16:53:42 -07001133 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001134 if (!_s.get())
1135 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001136
Mathias Agopian518ec112011-05-13 16:21:08 -07001137 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001138 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001139}
1140
1141const char* eglQueryString(EGLDisplay dpy, EGLint name)
1142{
1143 clearError();
1144
Jesse Hallb29e5e82012-04-04 16:53:42 -07001145 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001146 if (!dp) return (const char *) NULL;
1147
1148 switch (name) {
1149 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001150 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001151 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001152 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001153 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001154 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001155 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001156 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001157 }
1158 return setError(EGL_BAD_PARAMETER, (const char *)0);
1159}
1160
Mathias Agopianca088332013-03-28 17:44:13 -07001161EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1162{
1163 clearError();
1164
1165 const egl_display_ptr dp = validate_display(dpy);
1166 if (!dp) return (const char *) NULL;
1167
1168 switch (name) {
1169 case EGL_VENDOR:
1170 return dp->disp.queryString.vendor;
1171 case EGL_VERSION:
1172 return dp->disp.queryString.version;
1173 case EGL_EXTENSIONS:
1174 return dp->disp.queryString.extensions;
1175 case EGL_CLIENT_APIS:
1176 return dp->disp.queryString.clientApi;
1177 }
1178 return setError(EGL_BAD_PARAMETER, (const char *)0);
1179}
Mathias Agopian518ec112011-05-13 16:21:08 -07001180
1181// ----------------------------------------------------------------------------
1182// EGL 1.1
1183// ----------------------------------------------------------------------------
1184
1185EGLBoolean eglSurfaceAttrib(
1186 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1187{
1188 clearError();
1189
Jesse Hallb29e5e82012-04-04 16:53:42 -07001190 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001191 if (!dp) return EGL_FALSE;
1192
Jesse Hallb29e5e82012-04-04 16:53:42 -07001193 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001194 if (!_s.get())
1195 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001196
Mathias Agopian518ec112011-05-13 16:21:08 -07001197 egl_surface_t const * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001198
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001199 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001200 int err = native_window_set_auto_refresh(s->win.get(),
1201 value ? true : false);
1202 return (err == NO_ERROR) ? EGL_TRUE :
1203 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001204 }
1205
Mathias Agopian518ec112011-05-13 16:21:08 -07001206 if (s->cnx->egl.eglSurfaceAttrib) {
1207 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001208 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001209 }
1210 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1211}
1212
1213EGLBoolean eglBindTexImage(
1214 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1215{
1216 clearError();
1217
Jesse Hallb29e5e82012-04-04 16:53:42 -07001218 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001219 if (!dp) return EGL_FALSE;
1220
Jesse Hallb29e5e82012-04-04 16:53:42 -07001221 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001222 if (!_s.get())
1223 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001224
Mathias Agopian518ec112011-05-13 16:21:08 -07001225 egl_surface_t const * const s = get_surface(surface);
1226 if (s->cnx->egl.eglBindTexImage) {
1227 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001228 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001229 }
1230 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1231}
1232
1233EGLBoolean eglReleaseTexImage(
1234 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1235{
1236 clearError();
1237
Jesse Hallb29e5e82012-04-04 16:53:42 -07001238 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001239 if (!dp) return EGL_FALSE;
1240
Jesse Hallb29e5e82012-04-04 16:53:42 -07001241 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001242 if (!_s.get())
1243 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001244
Mathias Agopian518ec112011-05-13 16:21:08 -07001245 egl_surface_t const * const s = get_surface(surface);
1246 if (s->cnx->egl.eglReleaseTexImage) {
1247 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001248 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001249 }
1250 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1251}
1252
1253EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
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
1260 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001261 egl_connection_t* const cnx = &gEGLImpl;
1262 if (cnx->dso && cnx->egl.eglSwapInterval) {
1263 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001264 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001265
Mathias Agopian518ec112011-05-13 16:21:08 -07001266 return res;
1267}
1268
1269
1270// ----------------------------------------------------------------------------
1271// EGL 1.2
1272// ----------------------------------------------------------------------------
1273
1274EGLBoolean eglWaitClient(void)
1275{
1276 clearError();
1277
Mathias Agopianada798b2012-02-13 17:09:30 -08001278 egl_connection_t* const cnx = &gEGLImpl;
1279 if (!cnx->dso)
1280 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1281
1282 EGLBoolean res;
1283 if (cnx->egl.eglWaitClient) {
1284 res = cnx->egl.eglWaitClient();
1285 } else {
1286 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001287 }
1288 return res;
1289}
1290
1291EGLBoolean eglBindAPI(EGLenum api)
1292{
1293 clearError();
1294
1295 if (egl_init_drivers() == EGL_FALSE) {
1296 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1297 }
1298
1299 // bind this API on all EGLs
1300 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001301 egl_connection_t* const cnx = &gEGLImpl;
1302 if (cnx->dso && cnx->egl.eglBindAPI) {
1303 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001304 }
1305 return res;
1306}
1307
1308EGLenum eglQueryAPI(void)
1309{
1310 clearError();
1311
1312 if (egl_init_drivers() == EGL_FALSE) {
1313 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1314 }
1315
Mathias Agopianada798b2012-02-13 17:09:30 -08001316 egl_connection_t* const cnx = &gEGLImpl;
1317 if (cnx->dso && cnx->egl.eglQueryAPI) {
1318 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001319 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001320
Mathias Agopian518ec112011-05-13 16:21:08 -07001321 // or, it can only be OpenGL ES
1322 return EGL_OPENGL_ES_API;
1323}
1324
1325EGLBoolean eglReleaseThread(void)
1326{
1327 clearError();
1328
1329 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001330 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001331
Mathias Agopianada798b2012-02-13 17:09:30 -08001332 egl_connection_t* const cnx = &gEGLImpl;
1333 if (cnx->dso && cnx->egl.eglReleaseThread) {
1334 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001335 }
1336 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001337 return EGL_TRUE;
1338}
1339
1340EGLSurface eglCreatePbufferFromClientBuffer(
1341 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1342 EGLConfig config, const EGLint *attrib_list)
1343{
1344 clearError();
1345
Jesse Hallb29e5e82012-04-04 16:53:42 -07001346 egl_connection_t* cnx = NULL;
1347 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1348 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001349 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1350 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001351 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001352 }
1353 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1354}
1355
1356// ----------------------------------------------------------------------------
1357// EGL_EGLEXT_VERSION 3
1358// ----------------------------------------------------------------------------
1359
1360EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1361 const EGLint *attrib_list)
1362{
1363 clearError();
1364
Jesse Hallb29e5e82012-04-04 16:53:42 -07001365 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001366 if (!dp) return EGL_FALSE;
1367
Jesse Hallb29e5e82012-04-04 16:53:42 -07001368 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001369 if (!_s.get())
1370 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001371
1372 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001373 if (s->cnx->egl.eglLockSurfaceKHR) {
1374 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001375 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001376 }
1377 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1378}
1379
1380EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1381{
1382 clearError();
1383
Jesse Hallb29e5e82012-04-04 16:53:42 -07001384 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001385 if (!dp) return EGL_FALSE;
1386
Jesse Hallb29e5e82012-04-04 16:53:42 -07001387 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001388 if (!_s.get())
1389 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001390
1391 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001392 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001393 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001394 }
1395 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1396}
1397
1398EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1399 EGLClientBuffer buffer, const EGLint *attrib_list)
1400{
1401 clearError();
1402
Jesse Hallb29e5e82012-04-04 16:53:42 -07001403 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001404 if (!dp) return EGL_NO_IMAGE_KHR;
1405
Jesse Hallb29e5e82012-04-04 16:53:42 -07001406 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001407 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001408
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001409 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1410 egl_connection_t* const cnx = &gEGLImpl;
1411 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1412 result = cnx->egl.eglCreateImageKHR(
1413 dp->disp.dpy,
1414 c ? c->context : EGL_NO_CONTEXT,
1415 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001416 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001417 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001418}
1419
1420EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1421{
1422 clearError();
1423
Jesse Hallb29e5e82012-04-04 16:53:42 -07001424 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 if (!dp) return EGL_FALSE;
1426
Steven Holte646a5c52012-06-04 20:02:11 -07001427 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001428 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001429 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001430 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001431 }
Steven Holte646a5c52012-06-04 20:02:11 -07001432 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001433}
1434
1435// ----------------------------------------------------------------------------
1436// EGL_EGLEXT_VERSION 5
1437// ----------------------------------------------------------------------------
1438
1439
1440EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1441{
1442 clearError();
1443
Jesse Hallb29e5e82012-04-04 16:53:42 -07001444 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001445 if (!dp) return EGL_NO_SYNC_KHR;
1446
Mathias Agopian518ec112011-05-13 16:21:08 -07001447 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001448 egl_connection_t* const cnx = &gEGLImpl;
1449 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1450 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001451 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001452 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001453}
1454
1455EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1456{
1457 clearError();
1458
Jesse Hallb29e5e82012-04-04 16:53:42 -07001459 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001460 if (!dp) return EGL_FALSE;
1461
Mathias Agopian518ec112011-05-13 16:21:08 -07001462 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001463 egl_connection_t* const cnx = &gEGLImpl;
1464 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1465 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001466 }
1467 return result;
1468}
1469
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001470EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1471 clearError();
1472
1473 const egl_display_ptr dp = validate_display(dpy);
1474 if (!dp) return EGL_FALSE;
1475
1476 EGLBoolean result = EGL_FALSE;
1477 egl_connection_t* const cnx = &gEGLImpl;
1478 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1479 result = cnx->egl.eglSignalSyncKHR(
1480 dp->disp.dpy, sync, mode);
1481 }
1482 return result;
1483}
1484
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001485EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1486 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001487{
1488 clearError();
1489
Jesse Hallb29e5e82012-04-04 16:53:42 -07001490 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001491 if (!dp) return EGL_FALSE;
1492
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001493 EGLBoolean result = EGL_FALSE;
1494 egl_connection_t* const cnx = &gEGLImpl;
1495 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1496 result = cnx->egl.eglClientWaitSyncKHR(
1497 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001498 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001499 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001500}
1501
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001502EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1503 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001504{
1505 clearError();
1506
Jesse Hallb29e5e82012-04-04 16:53:42 -07001507 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001508 if (!dp) return EGL_FALSE;
1509
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001510 EGLBoolean result = EGL_FALSE;
1511 egl_connection_t* const cnx = &gEGLImpl;
1512 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1513 result = cnx->egl.eglGetSyncAttribKHR(
1514 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001515 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001516 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001517}
1518
Season Li000d88f2015-07-01 11:39:40 -07001519EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1520{
1521 clearError();
1522
1523 const egl_display_ptr dp = validate_display(dpy);
1524 if (!dp) return EGL_NO_STREAM_KHR;
1525
1526 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1527 egl_connection_t* const cnx = &gEGLImpl;
1528 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1529 result = cnx->egl.eglCreateStreamKHR(
1530 dp->disp.dpy, attrib_list);
1531 }
1532 return result;
1533}
1534
1535EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1536{
1537 clearError();
1538
1539 const egl_display_ptr dp = validate_display(dpy);
1540 if (!dp) return EGL_FALSE;
1541
1542 EGLBoolean result = EGL_FALSE;
1543 egl_connection_t* const cnx = &gEGLImpl;
1544 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1545 result = cnx->egl.eglDestroyStreamKHR(
1546 dp->disp.dpy, stream);
1547 }
1548 return result;
1549}
1550
1551EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1552 EGLenum attribute, EGLint value)
1553{
1554 clearError();
1555
1556 const egl_display_ptr dp = validate_display(dpy);
1557 if (!dp) return EGL_FALSE;
1558
1559 EGLBoolean result = EGL_FALSE;
1560 egl_connection_t* const cnx = &gEGLImpl;
1561 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1562 result = cnx->egl.eglStreamAttribKHR(
1563 dp->disp.dpy, stream, attribute, value);
1564 }
1565 return result;
1566}
1567
1568EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1569 EGLenum attribute, EGLint *value)
1570{
1571 clearError();
1572
1573 const egl_display_ptr dp = validate_display(dpy);
1574 if (!dp) return EGL_FALSE;
1575
1576 EGLBoolean result = EGL_FALSE;
1577 egl_connection_t* const cnx = &gEGLImpl;
1578 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1579 result = cnx->egl.eglQueryStreamKHR(
1580 dp->disp.dpy, stream, attribute, value);
1581 }
1582 return result;
1583}
1584
1585EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1586 EGLenum attribute, EGLuint64KHR *value)
1587{
1588 clearError();
1589
1590 const egl_display_ptr dp = validate_display(dpy);
1591 if (!dp) return EGL_FALSE;
1592
1593 EGLBoolean result = EGL_FALSE;
1594 egl_connection_t* const cnx = &gEGLImpl;
1595 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1596 result = cnx->egl.eglQueryStreamu64KHR(
1597 dp->disp.dpy, stream, attribute, value);
1598 }
1599 return result;
1600}
1601
1602EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1603 EGLenum attribute, EGLTimeKHR *value)
1604{
1605 clearError();
1606
1607 const egl_display_ptr dp = validate_display(dpy);
1608 if (!dp) return EGL_FALSE;
1609
1610 EGLBoolean result = EGL_FALSE;
1611 egl_connection_t* const cnx = &gEGLImpl;
1612 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1613 result = cnx->egl.eglQueryStreamTimeKHR(
1614 dp->disp.dpy, stream, attribute, value);
1615 }
1616 return result;
1617}
1618
1619EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1620 EGLStreamKHR stream, const EGLint *attrib_list)
1621{
1622 clearError();
1623
1624 egl_display_ptr dp = validate_display(dpy);
1625 if (!dp) return EGL_NO_SURFACE;
1626
1627 egl_connection_t* const cnx = &gEGLImpl;
1628 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1629 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1630 dp->disp.dpy, config, stream, attrib_list);
1631 if (surface != EGL_NO_SURFACE) {
1632 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1633 surface, cnx);
1634 return s;
1635 }
1636 }
1637 return EGL_NO_SURFACE;
1638}
1639
1640EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1641 EGLStreamKHR stream)
1642{
1643 clearError();
1644
1645 const egl_display_ptr dp = validate_display(dpy);
1646 if (!dp) return EGL_FALSE;
1647
1648 EGLBoolean result = EGL_FALSE;
1649 egl_connection_t* const cnx = &gEGLImpl;
1650 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1651 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1652 dp->disp.dpy, stream);
1653 }
1654 return result;
1655}
1656
1657EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1658 EGLStreamKHR stream)
1659{
1660 clearError();
1661
1662 const egl_display_ptr dp = validate_display(dpy);
1663 if (!dp) return EGL_FALSE;
1664
1665 EGLBoolean result = EGL_FALSE;
1666 egl_connection_t* const cnx = &gEGLImpl;
1667 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1668 result = cnx->egl.eglStreamConsumerAcquireKHR(
1669 dp->disp.dpy, stream);
1670 }
1671 return result;
1672}
1673
1674EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1675 EGLStreamKHR stream)
1676{
1677 clearError();
1678
1679 const egl_display_ptr dp = validate_display(dpy);
1680 if (!dp) return EGL_FALSE;
1681
1682 EGLBoolean result = EGL_FALSE;
1683 egl_connection_t* const cnx = &gEGLImpl;
1684 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1685 result = cnx->egl.eglStreamConsumerReleaseKHR(
1686 dp->disp.dpy, stream);
1687 }
1688 return result;
1689}
1690
1691EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1692 EGLDisplay dpy, EGLStreamKHR stream)
1693{
1694 clearError();
1695
1696 const egl_display_ptr dp = validate_display(dpy);
1697 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1698
1699 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1700 egl_connection_t* const cnx = &gEGLImpl;
1701 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1702 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1703 dp->disp.dpy, stream);
1704 }
1705 return result;
1706}
1707
1708EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1709 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1710{
1711 clearError();
1712
1713 const egl_display_ptr dp = validate_display(dpy);
1714 if (!dp) return EGL_NO_STREAM_KHR;
1715
1716 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1717 egl_connection_t* const cnx = &gEGLImpl;
1718 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1719 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1720 dp->disp.dpy, file_descriptor);
1721 }
1722 return result;
1723}
1724
Mathias Agopian518ec112011-05-13 16:21:08 -07001725// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001726// EGL_EGLEXT_VERSION 15
1727// ----------------------------------------------------------------------------
1728
1729EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1730 clearError();
1731 const egl_display_ptr dp = validate_display(dpy);
1732 if (!dp) return EGL_FALSE;
1733 EGLint result = EGL_FALSE;
1734 egl_connection_t* const cnx = &gEGLImpl;
1735 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1736 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1737 }
1738 return result;
1739}
1740
1741// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001742// ANDROID extensions
1743// ----------------------------------------------------------------------------
1744
Jamie Gennis331841b2012-09-06 14:52:00 -07001745EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1746{
1747 clearError();
1748
1749 const egl_display_ptr dp = validate_display(dpy);
1750 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1751
1752 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1753 egl_connection_t* const cnx = &gEGLImpl;
1754 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1755 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1756 }
1757 return result;
1758}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001759
Andy McFadden72841452013-03-01 16:25:32 -08001760EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1761 EGLnsecsANDROID time)
1762{
1763 clearError();
1764
1765 const egl_display_ptr dp = validate_display(dpy);
1766 if (!dp) {
1767 return EGL_FALSE;
1768 }
1769
1770 SurfaceRef _s(dp.get(), surface);
1771 if (!_s.get()) {
1772 setError(EGL_BAD_SURFACE, EGL_FALSE);
1773 return EGL_FALSE;
1774 }
1775
1776 egl_surface_t const * const s = get_surface(surface);
1777 native_window_set_buffers_timestamp(s->win.get(), time);
1778
1779 return EGL_TRUE;
1780}
1781
Craig Donner05249fc2016-01-15 19:33:55 -08001782EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1783{
1784 clearError();
1785
1786 int usage = 0;
1787 uint32_t width = 0;
1788 uint32_t height = 0;
1789 uint32_t format = 0;
1790 uint32_t red_size = 0;
1791 uint32_t green_size = 0;
1792 uint32_t blue_size = 0;
1793 uint32_t alpha_size = 0;
1794
1795#define GET_POSITIVE_VALUE(case_name, target) \
1796 case case_name: \
1797 if (value > 0) { \
1798 target = value; \
1799 } else { \
1800 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1801 } \
1802 break
1803
1804 if (attrib_list) {
1805 while (*attrib_list != EGL_NONE) {
1806 GLint attr = *attrib_list++;
1807 GLint value = *attrib_list++;
1808 switch (attr) {
1809 GET_POSITIVE_VALUE(EGL_WIDTH, width);
1810 GET_POSITIVE_VALUE(EGL_HEIGHT, height);
1811 GET_POSITIVE_VALUE(EGL_RED_SIZE, red_size);
1812 GET_POSITIVE_VALUE(EGL_GREEN_SIZE, green_size);
1813 GET_POSITIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1814 GET_POSITIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
1815 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1816 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1817 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001818 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001819 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001820 usage |= GRALLOC_USAGE_HW_RENDER;
1821 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001822 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001823 usage |= GRALLOC_USAGE_HW_TEXTURE;
1824 }
1825 // The buffer must be used for either a texture or a
1826 // renderbuffer.
Craig Donner8cfae6d2016-04-12 16:54:03 -07001827 if ((value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) &&
1828 (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID)) {
Craig Donner05249fc2016-01-15 19:33:55 -08001829 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1830 }
1831 break;
1832 default:
1833 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1834 }
1835 }
1836 }
1837#undef GET_POSITIVE_VALUE
1838
1839 // Validate format.
1840 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1841 if (alpha_size == 8) {
1842 format = HAL_PIXEL_FORMAT_RGBA_8888;
1843 } else {
1844 format = HAL_PIXEL_FORMAT_RGB_888;
1845 }
1846 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1847 alpha_size == 0) {
1848 format == HAL_PIXEL_FORMAT_RGB_565;
1849 } else {
1850 ALOGE("Invalid native pixel format { r=%d, g=%d, b=%d, a=%d }",
1851 red_size, green_size, blue_size, alpha_size);
1852 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1853 }
1854
1855 GraphicBuffer* gBuffer = new GraphicBuffer(width, height, format, usage);
1856 const status_t err = gBuffer->initCheck();
1857 if (err != NO_ERROR) {
1858 ALOGE("Unable to create native buffer { w=%d, h=%d, f=%d, u=%#x }: %#x",
1859 width, height, format, usage, err);
1860 // Destroy the buffer.
1861 sp<GraphicBuffer> holder(gBuffer);
1862 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
1863 }
1864 ALOGD("Created new native buffer %p { w=%d, h=%d, f=%d, u=%#x }",
1865 gBuffer, width, height, format, usage);
1866 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
1867}
1868
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001869// ----------------------------------------------------------------------------
1870// NVIDIA extensions
1871// ----------------------------------------------------------------------------
1872EGLuint64NV eglGetSystemTimeFrequencyNV()
1873{
1874 clearError();
1875
1876 if (egl_init_drivers() == EGL_FALSE) {
1877 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1878 }
1879
1880 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001881 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001882
Mathias Agopianada798b2012-02-13 17:09:30 -08001883 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1884 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001885 }
1886
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001887 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001888}
1889
1890EGLuint64NV eglGetSystemTimeNV()
1891{
1892 clearError();
1893
1894 if (egl_init_drivers() == EGL_FALSE) {
1895 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1896 }
1897
1898 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001899 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001900
Mathias Agopianada798b2012-02-13 17:09:30 -08001901 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1902 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001903 }
1904
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001905 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001906}
Dan Stozaa894d082015-02-19 15:27:36 -08001907
1908// ----------------------------------------------------------------------------
1909// Partial update extension
1910// ----------------------------------------------------------------------------
1911EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1912 EGLint *rects, EGLint n_rects)
1913{
1914 clearError();
1915
1916 const egl_display_ptr dp = validate_display(dpy);
1917 if (!dp) {
1918 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1919 return EGL_FALSE;
1920 }
1921
1922 SurfaceRef _s(dp.get(), surface);
1923 if (!_s.get()) {
1924 setError(EGL_BAD_SURFACE, EGL_FALSE);
1925 return EGL_FALSE;
1926 }
1927
1928 egl_surface_t const * const s = get_surface(surface);
1929 if (s->cnx->egl.eglSetDamageRegionKHR) {
1930 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1931 rects, n_rects);
1932 }
1933
1934 return EGL_FALSE;
1935}