blob: 436ea308007567bc2fc5eff608e26bbf21c9b652 [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 Donner68671532016-07-18 10:19:54 -070036#include <gui/ISurfaceComposer.h>
37
Craig Donner05249fc2016-01-15 19:33:55 -080038#include <ui/GraphicBuffer.h>
39
Mathias Agopian518ec112011-05-13 16:21:08 -070040#include <utils/KeyedVector.h>
41#include <utils/SortedVector.h>
42#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070044
Craig Donner68671532016-07-18 10:19:54 -070045#include "binder/Binder.h"
46#include "binder/Parcel.h"
47#include "binder/IServiceManager.h"
48
Mathias Agopian39c24a22013-04-04 23:17:56 -070049#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070050#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070051
52#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070053#include "egl_object.h"
54#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080055#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070056
57using namespace android;
58
Jesse Halla2ba4282013-09-14 21:00:14 -070059// This extension has not been ratified yet, so can't be shipped.
60// Implementation is incomplete and untested.
61#define ENABLE_EGL_KHR_GL_COLORSPACE 0
62
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070063#define ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS 0
64
Mathias Agopian518ec112011-05-13 16:21:08 -070065// ----------------------------------------------------------------------------
66
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070067namespace android {
68
Mathias Agopian518ec112011-05-13 16:21:08 -070069struct extention_map_t {
70 const char* name;
71 __eglMustCastToProperFunctionPointerType address;
72};
73
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070074/*
Jesse Hall21558da2013-08-06 15:31:22 -070075 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070076 *
Jesse Hall21558da2013-08-06 15:31:22 -070077 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
78 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070079 *
Jesse Hall21558da2013-08-06 15:31:22 -070080 * The rest (gExtensionString) depend on support in the EGL driver, and are
81 * only available if the driver supports them. However, some of these must be
82 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080083 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070084 * the mandatory extensions are present and may not function properly if some
85 * are missing.
86 *
87 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070088 */
Jesse Hall21558da2013-08-06 15:31:22 -070089extern char const * const gBuiltinExtensionString =
90 "EGL_KHR_get_all_proc_addresses "
91 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080092 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080093 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080094 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070095#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -070096 "EGL_ANDROID_get_frame_timestamps "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070097#endif
Jesse Hall21558da2013-08-06 15:31:22 -070098 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070099extern char const * const gExtensionString =
100 "EGL_KHR_image " // mandatory
101 "EGL_KHR_image_base " // mandatory
102 "EGL_KHR_image_pixmap "
103 "EGL_KHR_lock_surface "
Jesse Halla2ba4282013-09-14 21:00:14 -0700104#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
Jesse Hallc2e41222013-08-08 13:40:22 -0700105 "EGL_KHR_gl_colorspace "
Jesse Halla2ba4282013-09-14 21:00:14 -0700106#endif
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700107 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700108 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700109 "EGL_KHR_gl_texture_cubemap_image "
110 "EGL_KHR_gl_renderbuffer_image "
111 "EGL_KHR_reusable_sync "
112 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700113 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700114 "EGL_KHR_config_attribs "
115 "EGL_KHR_surfaceless_context "
116 "EGL_KHR_stream "
117 "EGL_KHR_stream_fifo "
118 "EGL_KHR_stream_producer_eglsurface "
119 "EGL_KHR_stream_consumer_gltexture "
120 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700121 "EGL_EXT_create_context_robustness "
122 "EGL_NV_system_time "
123 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700124 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700125 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800126 "EGL_KHR_partial_update " // strongly recommended
127 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700128 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700129 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700130 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700131 "EGL_EXT_protected_content "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700132 ;
133
134// extensions not exposed to applications but used by the ANDROID system
135// "EGL_ANDROID_blob_cache " // strongly recommended
136// "EGL_IMG_hibernate_process " // optional
137// "EGL_ANDROID_native_fence_sync " // strongly recommended
138// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700139// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700140
141/*
142 * EGL Extensions entry-points exposed to 3rd party applications
143 * (keep in sync with gExtensionString above)
144 *
145 */
146static const extention_map_t sExtensionMap[] = {
147 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700148 { "eglLockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
150 { "eglUnlockSurfaceKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700152
153 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 { "eglCreateImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
156 { "eglDestroyImageKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700158
159 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
160 { "eglCreateSyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
162 { "eglDestroySyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
164 { "eglClientWaitSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
166 { "eglSignalSyncKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
168 { "eglGetSyncAttribKHR",
169 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
170
171 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800172 { "eglGetSystemTimeFrequencyNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
174 { "eglGetSystemTimeNV",
175 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700176
Mathias Agopian2bb71682013-03-27 17:32:41 -0700177 // EGL_KHR_wait_sync
178 { "eglWaitSyncKHR",
179 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700180
181 // EGL_ANDROID_presentation_time
182 { "eglPresentationTimeANDROID",
183 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800184
185 // EGL_KHR_swap_buffers_with_damage
186 { "eglSwapBuffersWithDamageKHR",
187 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
188
Craig Donner05249fc2016-01-15 19:33:55 -0800189 // EGL_ANDROID_native_client_buffer
190 { "eglCreateNativeClientBufferANDROID",
191 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
192
Dan Stozaa894d082015-02-19 15:27:36 -0800193 // EGL_KHR_partial_update
194 { "eglSetDamageRegionKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700196
197 { "eglCreateStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
199 { "eglDestroyStreamKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
201 { "eglStreamAttribKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
203 { "eglQueryStreamKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
205 { "eglQueryStreamu64KHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
207 { "eglQueryStreamTimeKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
209 { "eglCreateStreamProducerSurfaceKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
211 { "eglStreamConsumerGLTextureExternalKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
213 { "eglStreamConsumerAcquireKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
215 { "eglStreamConsumerReleaseKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
217 { "eglGetStreamFileDescriptorKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
219 { "eglCreateStreamFromFileDescriptorKHR",
220 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700221
222 // EGL_ANDROID_get_frame_timestamps
223 { "eglGetFrameTimestampsANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
225 { "eglQueryTimestampSupportedANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglQueryTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700227};
228
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700229/*
230 * These extensions entry-points should not be exposed to applications.
231 * They're used internally by the Android EGL layer.
232 */
233#define FILTER_EXTENSIONS(procname) \
234 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
235 !strcmp((procname), "eglHibernateProcessIMG") || \
236 !strcmp((procname), "eglAwakenProcessIMG") || \
237 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
238
239
240
Mathias Agopian518ec112011-05-13 16:21:08 -0700241// accesses protected by sExtensionMapMutex
242static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
243static int sGLExtentionSlot = 0;
244static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
245
246static void(*findProcAddress(const char* name,
247 const extention_map_t* map, size_t n))() {
248 for (uint32_t i=0 ; i<n ; i++) {
249 if (!strcmp(name, map[i].name)) {
250 return map[i].address;
251 }
252 }
253 return NULL;
254}
255
256// ----------------------------------------------------------------------------
257
Mathias Agopian518ec112011-05-13 16:21:08 -0700258extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
259extern EGLBoolean egl_init_drivers();
260extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700261extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700262
Mathias Agopian518ec112011-05-13 16:21:08 -0700263} // namespace android;
264
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700265
Mathias Agopian518ec112011-05-13 16:21:08 -0700266// ----------------------------------------------------------------------------
267
268static inline void clearError() { egl_tls_t::clearError(); }
269static inline EGLContext getContext() { return egl_tls_t::getContext(); }
270
271// ----------------------------------------------------------------------------
272
273EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
274{
Jesse Hallbb5a9212017-01-19 17:43:26 -0800275 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700276 clearError();
277
Dan Stozac3289c42014-01-17 11:38:34 -0800278 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700279 if (index >= NUM_DISPLAYS) {
280 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
281 }
282
283 if (egl_init_drivers() == EGL_FALSE) {
284 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
285 }
286
287 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
288 return dpy;
289}
290
291// ----------------------------------------------------------------------------
292// Initialization
293// ----------------------------------------------------------------------------
294
295EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
296{
297 clearError();
298
Jesse Hallb29e5e82012-04-04 16:53:42 -0700299 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700300 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
301
302 EGLBoolean res = dp->initialize(major, minor);
303
304 return res;
305}
306
307EGLBoolean eglTerminate(EGLDisplay dpy)
308{
309 // NOTE: don't unload the drivers b/c some APIs can be called
310 // after eglTerminate() has been called. eglTerminate() only
311 // terminates an EGLDisplay, not a EGL itself.
312
313 clearError();
314
Jesse Hallb29e5e82012-04-04 16:53:42 -0700315 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700316 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
317
318 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800319
Mathias Agopian518ec112011-05-13 16:21:08 -0700320 return res;
321}
322
323// ----------------------------------------------------------------------------
324// configuration
325// ----------------------------------------------------------------------------
326
327EGLBoolean eglGetConfigs( EGLDisplay dpy,
328 EGLConfig *configs,
329 EGLint config_size, EGLint *num_config)
330{
331 clearError();
332
Jesse Hallb29e5e82012-04-04 16:53:42 -0700333 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700334 if (!dp) return EGL_FALSE;
335
Mathias Agopian7773c432012-02-13 20:06:08 -0800336 if (num_config==0) {
337 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700338 }
339
Mathias Agopian7773c432012-02-13 20:06:08 -0800340 EGLBoolean res = EGL_FALSE;
341 *num_config = 0;
342
343 egl_connection_t* const cnx = &gEGLImpl;
344 if (cnx->dso) {
345 res = cnx->egl.eglGetConfigs(
346 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700347 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800348
349 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700350}
351
352EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
353 EGLConfig *configs, EGLint config_size,
354 EGLint *num_config)
355{
356 clearError();
357
Jesse Hallb29e5e82012-04-04 16:53:42 -0700358 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700359 if (!dp) return EGL_FALSE;
360
361 if (num_config==0) {
362 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
363 }
364
Mathias Agopian518ec112011-05-13 16:21:08 -0700365 EGLBoolean res = EGL_FALSE;
366 *num_config = 0;
367
Mathias Agopianada798b2012-02-13 17:09:30 -0800368 egl_connection_t* const cnx = &gEGLImpl;
369 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700370 if (attrib_list) {
371 char value[PROPERTY_VALUE_MAX];
372 property_get("debug.egl.force_msaa", value, "false");
373
374 if (!strcmp(value, "true")) {
375 size_t attribCount = 0;
376 EGLint attrib = attrib_list[0];
377
378 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700379 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700380 const EGLint *attribRendererable = NULL;
381 const EGLint *attribCaveat = NULL;
382
383 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700384 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700385 while (attrib != EGL_NONE) {
386 attrib = attrib_list[attribCount];
387 switch (attrib) {
388 case EGL_RENDERABLE_TYPE:
389 attribRendererable = &attrib_list[attribCount];
390 break;
391 case EGL_CONFIG_CAVEAT:
392 attribCaveat = &attrib_list[attribCount];
393 break;
394 }
395 attribCount++;
396 }
397
398 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
399 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800400
Romain Guy1cffc802012-10-15 18:13:05 -0700401 // Insert 2 extra attributes to force-enable MSAA 4x
402 EGLint aaAttribs[attribCount + 4];
403 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
404 aaAttribs[1] = 1;
405 aaAttribs[2] = EGL_SAMPLES;
406 aaAttribs[3] = 4;
407
408 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
409
410 EGLint numConfigAA;
411 EGLBoolean resAA = cnx->egl.eglChooseConfig(
412 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
413
414 if (resAA == EGL_TRUE && numConfigAA > 0) {
415 ALOGD("Enabling MSAA 4x");
416 *num_config = numConfigAA;
417 return resAA;
418 }
419 }
420 }
421 }
422
Mathias Agopian7773c432012-02-13 20:06:08 -0800423 res = cnx->egl.eglChooseConfig(
424 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700425 }
426 return res;
427}
428
429EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
430 EGLint attribute, EGLint *value)
431{
432 clearError();
433
Jesse Hallb29e5e82012-04-04 16:53:42 -0700434 egl_connection_t* cnx = NULL;
435 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
436 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800437
Mathias Agopian518ec112011-05-13 16:21:08 -0700438 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800439 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700440}
441
442// ----------------------------------------------------------------------------
443// surfaces
444// ----------------------------------------------------------------------------
445
Jesse Halla2ba4282013-09-14 21:00:14 -0700446// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
Jesse Hallc2e41222013-08-08 13:40:22 -0700447// been added to the Khronos egl.h.
448#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE
449#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB
450#define EGL_GL_COLORSPACE_LINEAR_KHR EGL_VG_COLORSPACE_LINEAR
451
452// Turn linear formats into corresponding sRGB formats when colorspace is
453// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
454// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800455// the modification isn't possible, the original dataSpace is returned.
456static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
457 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700458 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800459 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800461 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700462 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800463 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700464}
465
Mathias Agopian518ec112011-05-13 16:21:08 -0700466EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
467 NativeWindowType window,
468 const EGLint *attrib_list)
469{
470 clearError();
471
Jesse Hallb29e5e82012-04-04 16:53:42 -0700472 egl_connection_t* cnx = NULL;
473 egl_display_ptr dp = validate_display_connection(dpy, cnx);
474 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800475 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700476
Andy McFaddend566ce32014-01-07 15:54:17 -0800477 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
478 if (result != OK) {
479 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
480 "failed (%#x) (already connected to another API?)",
481 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700482 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700483 }
484
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700485 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700486 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
487 // of our native format. So if sRGB gamma is requested, we have to
488 // modify the EGLconfig's format before setting the native window's
489 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800490
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700491 // by default, just pick RGBA_8888
492 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800493 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700494
495 EGLint a = 0;
496 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
497 if (a > 0) {
498 // alpha-channel requested, there's really only one suitable format
499 format = HAL_PIXEL_FORMAT_RGBA_8888;
500 } else {
501 EGLint r, g, b;
502 r = g = b = 0;
503 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
504 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
505 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
506 EGLint colorDepth = r + g + b;
507 if (colorDepth <= 16) {
508 format = HAL_PIXEL_FORMAT_RGB_565;
509 } else {
510 format = HAL_PIXEL_FORMAT_RGBX_8888;
511 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700512 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700513
514 // now select a corresponding sRGB format if needed
515 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
516 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
517 if (*attr == EGL_GL_COLORSPACE_KHR) {
Jesse Halla2ba4282013-09-14 21:00:14 -0700518 if (ENABLE_EGL_KHR_GL_COLORSPACE) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800519 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jesse Halla2ba4282013-09-14 21:00:14 -0700520 } else {
521 // Normally we'd pass through unhandled attributes to
522 // the driver. But in case the driver implements this
523 // extension but we're disabling it, we want to prevent
524 // it getting through -- support will be broken without
525 // our help.
526 ALOGE("sRGB window surfaces not supported");
527 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
528 }
Jamie Gennisbee205f2011-07-01 13:12:07 -0700529 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700530 }
531 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800532
Jesse Hallc2e41222013-08-08 13:40:22 -0700533 if (format != 0) {
534 int err = native_window_set_buffers_format(window, format);
535 if (err != 0) {
536 ALOGE("error setting native window pixel format: %s (%d)",
537 strerror(-err), err);
538 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
539 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
540 }
541 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700542
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800543 if (dataSpace != 0) {
544 int err = native_window_set_buffers_data_space(window, dataSpace);
545 if (err != 0) {
546 ALOGE("error setting native window pixel dataSpace: %s (%d)",
547 strerror(-err), err);
548 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
549 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
550 }
551 }
552
Jamie Gennis59769462011-11-19 18:04:43 -0800553 // the EGL spec requires that a new EGLSurface default to swap interval
554 // 1, so explicitly set that on the window here.
555 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
556 anw->setSwapInterval(anw, 1);
557
Mathias Agopian518ec112011-05-13 16:21:08 -0700558 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800559 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700560 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700561 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
562 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700563 return s;
564 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700565
566 // EGLSurface creation failed
567 native_window_set_buffers_format(window, 0);
568 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700569 }
570 return EGL_NO_SURFACE;
571}
572
573EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
574 NativePixmapType pixmap,
575 const EGLint *attrib_list)
576{
577 clearError();
578
Jesse Hallb29e5e82012-04-04 16:53:42 -0700579 egl_connection_t* cnx = NULL;
580 egl_display_ptr dp = validate_display_connection(dpy, cnx);
581 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700582 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800583 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700584 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700585 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
586 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700587 return s;
588 }
589 }
590 return EGL_NO_SURFACE;
591}
592
593EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
594 const EGLint *attrib_list)
595{
596 clearError();
597
Jesse Hallb29e5e82012-04-04 16:53:42 -0700598 egl_connection_t* cnx = NULL;
599 egl_display_ptr dp = validate_display_connection(dpy, cnx);
600 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700601 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800602 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700603 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700604 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
605 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700606 return s;
607 }
608 }
609 return EGL_NO_SURFACE;
610}
Jesse Hall47743382013-02-08 11:13:46 -0800611
Mathias Agopian518ec112011-05-13 16:21:08 -0700612EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
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
623 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800624 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700625 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700626 _s.terminate();
627 }
628 return result;
629}
630
631EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
632 EGLint attribute, EGLint *value)
633{
634 clearError();
635
Jesse Hallb29e5e82012-04-04 16:53:42 -0700636 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700637 if (!dp) return EGL_FALSE;
638
Jesse Hallb29e5e82012-04-04 16:53:42 -0700639 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700640 if (!_s.get())
641 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700642
Mathias Agopian518ec112011-05-13 16:21:08 -0700643 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800644 return s->cnx->egl.eglQuerySurface(
645 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700646}
647
Jamie Gennise8696a42012-01-15 18:54:57 -0800648void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800649 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800650 clearError();
651
Jesse Hallb29e5e82012-04-04 16:53:42 -0700652 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800653 if (!dp) {
654 return;
655 }
656
Jesse Hallb29e5e82012-04-04 16:53:42 -0700657 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800658 if (!_s.get()) {
659 setError(EGL_BAD_SURFACE, EGL_FALSE);
660 return;
661 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800662}
663
Mathias Agopian518ec112011-05-13 16:21:08 -0700664// ----------------------------------------------------------------------------
665// Contexts
666// ----------------------------------------------------------------------------
667
668EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
669 EGLContext share_list, const EGLint *attrib_list)
670{
671 clearError();
672
Jesse Hallb29e5e82012-04-04 16:53:42 -0700673 egl_connection_t* cnx = NULL;
674 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700675 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700676 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700677 if (!ContextRef(dp.get(), share_list).get()) {
678 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
679 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700680 egl_context_t* const c = get_context(share_list);
681 share_list = c->context;
682 }
683 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800684 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700685 if (context != EGL_NO_CONTEXT) {
686 // figure out if it's a GLESv1 or GLESv2
687 int version = 0;
688 if (attrib_list) {
689 while (*attrib_list != EGL_NONE) {
690 GLint attr = *attrib_list++;
691 GLint value = *attrib_list++;
692 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
693 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800694 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800695 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800696 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700697 }
698 }
699 };
700 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700701 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
702 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700703 return c;
704 }
705 }
706 return EGL_NO_CONTEXT;
707}
708
709EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
710{
711 clearError();
712
Jesse Hallb29e5e82012-04-04 16:53:42 -0700713 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700714 if (!dp)
715 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700716
Jesse Hallb29e5e82012-04-04 16:53:42 -0700717 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700718 if (!_c.get())
719 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800720
Mathias Agopian518ec112011-05-13 16:21:08 -0700721 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800722 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700723 if (result == EGL_TRUE) {
724 _c.terminate();
725 }
726 return result;
727}
728
Mathias Agopian518ec112011-05-13 16:21:08 -0700729EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
730 EGLSurface read, EGLContext ctx)
731{
732 clearError();
733
Jesse Hallb29e5e82012-04-04 16:53:42 -0700734 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700735 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
736
Mathias Agopian5b287a62011-05-16 18:58:55 -0700737 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
738 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
739 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700740 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
741 (draw != EGL_NO_SURFACE) ) {
742 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
743 }
744
745 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700746 ContextRef _c(dp.get(), ctx);
747 SurfaceRef _d(dp.get(), draw);
748 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700749
750 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700751 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700752 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700753 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700754 }
755
756 // these are the underlying implementation's object
757 EGLContext impl_ctx = EGL_NO_CONTEXT;
758 EGLSurface impl_draw = EGL_NO_SURFACE;
759 EGLSurface impl_read = EGL_NO_SURFACE;
760
761 // these are our objects structs passed in
762 egl_context_t * c = NULL;
763 egl_surface_t const * d = NULL;
764 egl_surface_t const * r = NULL;
765
766 // these are the current objects structs
767 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800768
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 if (ctx != EGL_NO_CONTEXT) {
770 c = get_context(ctx);
771 impl_ctx = c->context;
772 } else {
773 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700774 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
775 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
776 return setError(EGL_BAD_MATCH, EGL_FALSE);
777 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700778 if (cur_c == NULL) {
779 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700780 // not an error, there is just no current context.
781 return EGL_TRUE;
782 }
783 }
784
785 // retrieve the underlying implementation's draw EGLSurface
786 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700787 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700788 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700789 impl_draw = d->surface;
790 }
791
792 // retrieve the underlying implementation's read EGLSurface
793 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700794 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700795 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700796 impl_read = r->surface;
797 }
798
Mathias Agopian518ec112011-05-13 16:21:08 -0700799
Jesse Hallb29e5e82012-04-04 16:53:42 -0700800 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800801 draw, read, ctx,
802 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700803
804 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800805 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700806 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
807 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700808 _c.acquire();
809 _r.acquire();
810 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 } else {
812 setGLHooksThreadSpecific(&gHooksNoContext);
813 egl_tls_t::setContext(EGL_NO_CONTEXT);
814 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700815 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000816 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700817 egl_connection_t* const cnx = &gEGLImpl;
818 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700819 }
820 return result;
821}
822
823
824EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
825 EGLint attribute, EGLint *value)
826{
827 clearError();
828
Jesse Hallb29e5e82012-04-04 16:53:42 -0700829 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700830 if (!dp) return EGL_FALSE;
831
Jesse Hallb29e5e82012-04-04 16:53:42 -0700832 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700833 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
834
Mathias Agopian518ec112011-05-13 16:21:08 -0700835 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800836 return c->cnx->egl.eglQueryContext(
837 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700838
Mathias Agopian518ec112011-05-13 16:21:08 -0700839}
840
841EGLContext eglGetCurrentContext(void)
842{
843 // could be called before eglInitialize(), but we wouldn't have a context
844 // then, and this function would correctly return EGL_NO_CONTEXT.
845
846 clearError();
847
848 EGLContext ctx = getContext();
849 return ctx;
850}
851
852EGLSurface eglGetCurrentSurface(EGLint readdraw)
853{
854 // could be called before eglInitialize(), but we wouldn't have a context
855 // then, and this function would correctly return EGL_NO_SURFACE.
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 switch (readdraw) {
864 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800865 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700866 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
867 }
868 }
869 return EGL_NO_SURFACE;
870}
871
872EGLDisplay eglGetCurrentDisplay(void)
873{
874 // could be called before eglInitialize(), but we wouldn't have a context
875 // then, and this function would correctly return EGL_NO_DISPLAY.
876
877 clearError();
878
879 EGLContext ctx = getContext();
880 if (ctx) {
881 egl_context_t const * const c = get_context(ctx);
882 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
883 return c->dpy;
884 }
885 return EGL_NO_DISPLAY;
886}
887
888EGLBoolean eglWaitGL(void)
889{
Mathias Agopian518ec112011-05-13 16:21:08 -0700890 clearError();
891
Mathias Agopianada798b2012-02-13 17:09:30 -0800892 egl_connection_t* const cnx = &gEGLImpl;
893 if (!cnx->dso)
894 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
895
896 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700897}
898
899EGLBoolean eglWaitNative(EGLint engine)
900{
Mathias Agopian518ec112011-05-13 16:21:08 -0700901 clearError();
902
Mathias Agopianada798b2012-02-13 17:09:30 -0800903 egl_connection_t* const cnx = &gEGLImpl;
904 if (!cnx->dso)
905 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
906
907 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700908}
909
910EGLint eglGetError(void)
911{
Mathias Agopianada798b2012-02-13 17:09:30 -0800912 EGLint err = EGL_SUCCESS;
913 egl_connection_t* const cnx = &gEGLImpl;
914 if (cnx->dso) {
915 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700916 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800917 if (err == EGL_SUCCESS) {
918 err = egl_tls_t::getError();
919 }
920 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700921}
922
Michael Chockc0ec5e22014-01-27 08:14:33 -0800923static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700924 const char* procname) {
925 const egl_connection_t* cnx = &gEGLImpl;
926 void* proc = NULL;
927
Michael Chockc0ec5e22014-01-27 08:14:33 -0800928 proc = dlsym(cnx->libEgl, procname);
929 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
930
Jesse Hallc07b5202013-07-04 12:08:16 -0700931 proc = dlsym(cnx->libGles2, procname);
932 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
933
934 proc = dlsym(cnx->libGles1, procname);
935 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
936
937 return NULL;
938}
939
Mathias Agopian518ec112011-05-13 16:21:08 -0700940__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
941{
942 // eglGetProcAddress() could be the very first function called
943 // in which case we must make sure we've initialized ourselves, this
944 // happens the first time egl_get_display() is called.
945
946 clearError();
947
948 if (egl_init_drivers() == EGL_FALSE) {
949 setError(EGL_BAD_PARAMETER, NULL);
950 return NULL;
951 }
952
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700953 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700954 return NULL;
955 }
956
Mathias Agopian518ec112011-05-13 16:21:08 -0700957 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700958 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700959 if (addr) return addr;
960
Michael Chockc0ec5e22014-01-27 08:14:33 -0800961 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700962 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700963
Mathias Agopian518ec112011-05-13 16:21:08 -0700964 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
965 pthread_mutex_lock(&sExtensionMapMutex);
966
967 /*
968 * Since eglGetProcAddress() is not associated to anything, it needs
969 * to return a function pointer that "works" regardless of what
970 * the current context is.
971 *
972 * For this reason, we return a "forwarder", a small stub that takes
973 * care of calling the function associated with the context
974 * currently bound.
975 *
976 * We first look for extensions we've already resolved, if we're seeing
977 * this extension for the first time, we go through all our
978 * implementations and call eglGetProcAddress() and record the
979 * result in the appropriate implementation hooks and return the
980 * address of the forwarder corresponding to that hook set.
981 *
982 */
983
984 const String8 name(procname);
985 addr = sGLExtentionMap.valueFor(name);
986 const int slot = sGLExtentionSlot;
987
Steve Blocke6f43dd2012-01-06 19:20:56 +0000988 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700989 "no more slots for eglGetProcAddress(\"%s\")",
990 procname);
991
992 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
993 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800994
995 egl_connection_t* const cnx = &gEGLImpl;
996 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800997 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800998 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800999 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1000 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001001 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001002 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001003 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001004
Mathias Agopian518ec112011-05-13 16:21:08 -07001005 if (found) {
1006 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001007 sGLExtentionMap.add(name, addr);
1008 sGLExtentionSlot++;
1009 }
1010 }
1011
1012 pthread_mutex_unlock(&sExtensionMapMutex);
1013 return addr;
1014}
1015
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001016class FrameCompletionThread : public Thread {
1017public:
1018
1019 static void queueSync(EGLSyncKHR sync) {
1020 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1021 static bool running = false;
1022 if (!running) {
1023 thread->run("GPUFrameCompletion");
1024 running = true;
1025 }
1026 {
1027 Mutex::Autolock lock(thread->mMutex);
1028 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1029 thread->mFramesQueued).string());
1030 thread->mQueue.push_back(sync);
1031 thread->mCondition.signal();
1032 thread->mFramesQueued++;
1033 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1034 }
1035 }
1036
1037private:
1038 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1039
1040 virtual bool threadLoop() {
1041 EGLSyncKHR sync;
1042 uint32_t frameNum;
1043 {
1044 Mutex::Autolock lock(mMutex);
1045 while (mQueue.isEmpty()) {
1046 mCondition.wait(mMutex);
1047 }
1048 sync = mQueue[0];
1049 frameNum = mFramesCompleted;
1050 }
1051 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1052 {
1053 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1054 frameNum).string());
1055 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1056 if (result == EGL_FALSE) {
1057 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1058 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1059 ALOGE("FrameCompletion: timeout waiting for fence");
1060 }
1061 eglDestroySyncKHR(dpy, sync);
1062 }
1063 {
1064 Mutex::Autolock lock(mMutex);
1065 mQueue.removeAt(0);
1066 mFramesCompleted++;
1067 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1068 }
1069 return true;
1070 }
1071
1072 uint32_t mFramesQueued;
1073 uint32_t mFramesCompleted;
1074 Vector<EGLSyncKHR> mQueue;
1075 Condition mCondition;
1076 Mutex mMutex;
1077};
1078
Dan Stozaa894d082015-02-19 15:27:36 -08001079EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1080 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001081{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001082 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001083 clearError();
1084
Jesse Hallb29e5e82012-04-04 16:53:42 -07001085 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001086 if (!dp) return EGL_FALSE;
1087
Jesse Hallb29e5e82012-04-04 16:53:42 -07001088 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001089 if (!_s.get())
1090 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001091
Mathias Agopian518ec112011-05-13 16:21:08 -07001092 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001093
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001094 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1095 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1096 if (sync != EGL_NO_SYNC_KHR) {
1097 FrameCompletionThread::queueSync(sync);
1098 }
1099 }
1100
Mathias Agopian7db993a2012-03-25 00:49:46 -07001101 if (CC_UNLIKELY(dp->finishOnSwap)) {
1102 uint32_t pixel;
1103 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1104 if (c) {
1105 // glReadPixels() ensures that the frame is complete
1106 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1107 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1108 }
1109 }
1110
Dan Stozaa894d082015-02-19 15:27:36 -08001111 if (n_rects == 0) {
1112 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1113 }
1114
1115 Vector<android_native_rect_t> androidRects;
1116 for (int r = 0; r < n_rects; ++r) {
1117 int offset = r * 4;
1118 int x = rects[offset];
1119 int y = rects[offset + 1];
1120 int width = rects[offset + 2];
1121 int height = rects[offset + 3];
1122 android_native_rect_t androidRect;
1123 androidRect.left = x;
1124 androidRect.top = y + height;
1125 androidRect.right = x + width;
1126 androidRect.bottom = y;
1127 androidRects.push_back(androidRect);
1128 }
1129 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1130 androidRects.size());
1131
1132 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1133 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1134 rects, n_rects);
1135 } else {
1136 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1137 }
1138}
1139
1140EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1141{
1142 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001143}
1144
1145EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1146 NativePixmapType target)
1147{
1148 clearError();
1149
Jesse Hallb29e5e82012-04-04 16:53:42 -07001150 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001151 if (!dp) return EGL_FALSE;
1152
Jesse Hallb29e5e82012-04-04 16:53:42 -07001153 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001154 if (!_s.get())
1155 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001156
Mathias Agopian518ec112011-05-13 16:21:08 -07001157 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001158 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001159}
1160
1161const char* eglQueryString(EGLDisplay dpy, EGLint name)
1162{
1163 clearError();
1164
Jesse Hallb29e5e82012-04-04 16:53:42 -07001165 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001166 if (!dp) return (const char *) NULL;
1167
1168 switch (name) {
1169 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001170 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001171 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001172 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001173 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001174 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001175 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001176 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001177 }
1178 return setError(EGL_BAD_PARAMETER, (const char *)0);
1179}
1180
Mathias Agopianca088332013-03-28 17:44:13 -07001181EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1182{
1183 clearError();
1184
1185 const egl_display_ptr dp = validate_display(dpy);
1186 if (!dp) return (const char *) NULL;
1187
1188 switch (name) {
1189 case EGL_VENDOR:
1190 return dp->disp.queryString.vendor;
1191 case EGL_VERSION:
1192 return dp->disp.queryString.version;
1193 case EGL_EXTENSIONS:
1194 return dp->disp.queryString.extensions;
1195 case EGL_CLIENT_APIS:
1196 return dp->disp.queryString.clientApi;
1197 }
1198 return setError(EGL_BAD_PARAMETER, (const char *)0);
1199}
Mathias Agopian518ec112011-05-13 16:21:08 -07001200
1201// ----------------------------------------------------------------------------
1202// EGL 1.1
1203// ----------------------------------------------------------------------------
1204
1205EGLBoolean eglSurfaceAttrib(
1206 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1207{
1208 clearError();
1209
Jesse Hallb29e5e82012-04-04 16:53:42 -07001210 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001211 if (!dp) return EGL_FALSE;
1212
Jesse Hallb29e5e82012-04-04 16:53:42 -07001213 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001214 if (!_s.get())
1215 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001216
Pablo Ceballosc18be292016-05-31 14:55:42 -07001217 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001218
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001219 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001220 int err = native_window_set_auto_refresh(s->win.get(),
1221 value ? true : false);
1222 return (err == NO_ERROR) ? EGL_TRUE :
1223 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001224 }
1225
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001226#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001227 if (attribute == EGL_TIMESTAMPS_ANDROID) {
1228 s->enableTimestamps = value;
1229 return EGL_TRUE;
1230 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001231#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001232
Mathias Agopian518ec112011-05-13 16:21:08 -07001233 if (s->cnx->egl.eglSurfaceAttrib) {
1234 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001235 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001236 }
1237 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1238}
1239
1240EGLBoolean eglBindTexImage(
1241 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1242{
1243 clearError();
1244
Jesse Hallb29e5e82012-04-04 16:53:42 -07001245 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001246 if (!dp) return EGL_FALSE;
1247
Jesse Hallb29e5e82012-04-04 16:53:42 -07001248 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001249 if (!_s.get())
1250 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001251
Mathias Agopian518ec112011-05-13 16:21:08 -07001252 egl_surface_t const * const s = get_surface(surface);
1253 if (s->cnx->egl.eglBindTexImage) {
1254 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001255 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001256 }
1257 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1258}
1259
1260EGLBoolean eglReleaseTexImage(
1261 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1262{
1263 clearError();
1264
Jesse Hallb29e5e82012-04-04 16:53:42 -07001265 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001266 if (!dp) return EGL_FALSE;
1267
Jesse Hallb29e5e82012-04-04 16:53:42 -07001268 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001269 if (!_s.get())
1270 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001271
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 egl_surface_t const * const s = get_surface(surface);
1273 if (s->cnx->egl.eglReleaseTexImage) {
1274 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001275 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001276 }
1277 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1278}
1279
1280EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1281{
1282 clearError();
1283
Jesse Hallb29e5e82012-04-04 16:53:42 -07001284 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001285 if (!dp) return EGL_FALSE;
1286
1287 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001288 egl_connection_t* const cnx = &gEGLImpl;
1289 if (cnx->dso && cnx->egl.eglSwapInterval) {
1290 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001291 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001292
Mathias Agopian518ec112011-05-13 16:21:08 -07001293 return res;
1294}
1295
1296
1297// ----------------------------------------------------------------------------
1298// EGL 1.2
1299// ----------------------------------------------------------------------------
1300
1301EGLBoolean eglWaitClient(void)
1302{
1303 clearError();
1304
Mathias Agopianada798b2012-02-13 17:09:30 -08001305 egl_connection_t* const cnx = &gEGLImpl;
1306 if (!cnx->dso)
1307 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1308
1309 EGLBoolean res;
1310 if (cnx->egl.eglWaitClient) {
1311 res = cnx->egl.eglWaitClient();
1312 } else {
1313 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001314 }
1315 return res;
1316}
1317
1318EGLBoolean eglBindAPI(EGLenum api)
1319{
1320 clearError();
1321
1322 if (egl_init_drivers() == EGL_FALSE) {
1323 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1324 }
1325
1326 // bind this API on all EGLs
1327 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001328 egl_connection_t* const cnx = &gEGLImpl;
1329 if (cnx->dso && cnx->egl.eglBindAPI) {
1330 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001331 }
1332 return res;
1333}
1334
1335EGLenum eglQueryAPI(void)
1336{
1337 clearError();
1338
1339 if (egl_init_drivers() == EGL_FALSE) {
1340 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1341 }
1342
Mathias Agopianada798b2012-02-13 17:09:30 -08001343 egl_connection_t* const cnx = &gEGLImpl;
1344 if (cnx->dso && cnx->egl.eglQueryAPI) {
1345 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001346 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001347
Mathias Agopian518ec112011-05-13 16:21:08 -07001348 // or, it can only be OpenGL ES
1349 return EGL_OPENGL_ES_API;
1350}
1351
1352EGLBoolean eglReleaseThread(void)
1353{
1354 clearError();
1355
1356 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001357 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001358
Mathias Agopianada798b2012-02-13 17:09:30 -08001359 egl_connection_t* const cnx = &gEGLImpl;
1360 if (cnx->dso && cnx->egl.eglReleaseThread) {
1361 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001362 }
1363 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001364 return EGL_TRUE;
1365}
1366
1367EGLSurface eglCreatePbufferFromClientBuffer(
1368 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1369 EGLConfig config, const EGLint *attrib_list)
1370{
1371 clearError();
1372
Jesse Hallb29e5e82012-04-04 16:53:42 -07001373 egl_connection_t* cnx = NULL;
1374 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1375 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001376 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1377 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001378 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001379 }
1380 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1381}
1382
1383// ----------------------------------------------------------------------------
1384// EGL_EGLEXT_VERSION 3
1385// ----------------------------------------------------------------------------
1386
1387EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1388 const EGLint *attrib_list)
1389{
1390 clearError();
1391
Jesse Hallb29e5e82012-04-04 16:53:42 -07001392 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001393 if (!dp) return EGL_FALSE;
1394
Jesse Hallb29e5e82012-04-04 16:53:42 -07001395 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001396 if (!_s.get())
1397 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001398
1399 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001400 if (s->cnx->egl.eglLockSurfaceKHR) {
1401 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001402 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001403 }
1404 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1405}
1406
1407EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1408{
1409 clearError();
1410
Jesse Hallb29e5e82012-04-04 16:53:42 -07001411 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001412 if (!dp) return EGL_FALSE;
1413
Jesse Hallb29e5e82012-04-04 16:53:42 -07001414 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001415 if (!_s.get())
1416 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001417
1418 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001419 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001420 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001421 }
1422 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1423}
1424
1425EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1426 EGLClientBuffer buffer, const EGLint *attrib_list)
1427{
1428 clearError();
1429
Jesse Hallb29e5e82012-04-04 16:53:42 -07001430 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001431 if (!dp) return EGL_NO_IMAGE_KHR;
1432
Jesse Hallb29e5e82012-04-04 16:53:42 -07001433 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001434 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001435
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001436 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1437 egl_connection_t* const cnx = &gEGLImpl;
1438 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1439 result = cnx->egl.eglCreateImageKHR(
1440 dp->disp.dpy,
1441 c ? c->context : EGL_NO_CONTEXT,
1442 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001443 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001444 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001445}
1446
1447EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1448{
1449 clearError();
1450
Jesse Hallb29e5e82012-04-04 16:53:42 -07001451 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001452 if (!dp) return EGL_FALSE;
1453
Steven Holte646a5c52012-06-04 20:02:11 -07001454 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001455 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001456 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001457 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001458 }
Steven Holte646a5c52012-06-04 20:02:11 -07001459 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001460}
1461
1462// ----------------------------------------------------------------------------
1463// EGL_EGLEXT_VERSION 5
1464// ----------------------------------------------------------------------------
1465
1466
1467EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1468{
1469 clearError();
1470
Jesse Hallb29e5e82012-04-04 16:53:42 -07001471 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001472 if (!dp) return EGL_NO_SYNC_KHR;
1473
Mathias Agopian518ec112011-05-13 16:21:08 -07001474 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001475 egl_connection_t* const cnx = &gEGLImpl;
1476 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1477 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001478 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001479 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001480}
1481
1482EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1483{
1484 clearError();
1485
Jesse Hallb29e5e82012-04-04 16:53:42 -07001486 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001487 if (!dp) return EGL_FALSE;
1488
Mathias Agopian518ec112011-05-13 16:21:08 -07001489 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001490 egl_connection_t* const cnx = &gEGLImpl;
1491 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1492 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001493 }
1494 return result;
1495}
1496
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001497EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1498 clearError();
1499
1500 const egl_display_ptr dp = validate_display(dpy);
1501 if (!dp) return EGL_FALSE;
1502
1503 EGLBoolean result = EGL_FALSE;
1504 egl_connection_t* const cnx = &gEGLImpl;
1505 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1506 result = cnx->egl.eglSignalSyncKHR(
1507 dp->disp.dpy, sync, mode);
1508 }
1509 return result;
1510}
1511
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001512EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1513 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001514{
1515 clearError();
1516
Jesse Hallb29e5e82012-04-04 16:53:42 -07001517 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001518 if (!dp) return EGL_FALSE;
1519
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001520 EGLBoolean result = EGL_FALSE;
1521 egl_connection_t* const cnx = &gEGLImpl;
1522 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1523 result = cnx->egl.eglClientWaitSyncKHR(
1524 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001525 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001526 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001527}
1528
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001529EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1530 EGLint attribute, EGLint *value)
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.eglGetSyncAttribKHR) {
1540 result = cnx->egl.eglGetSyncAttribKHR(
1541 dp->disp.dpy, sync, attribute, value);
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
Season Li000d88f2015-07-01 11:39:40 -07001546EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1547{
1548 clearError();
1549
1550 const egl_display_ptr dp = validate_display(dpy);
1551 if (!dp) return EGL_NO_STREAM_KHR;
1552
1553 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1554 egl_connection_t* const cnx = &gEGLImpl;
1555 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1556 result = cnx->egl.eglCreateStreamKHR(
1557 dp->disp.dpy, attrib_list);
1558 }
1559 return result;
1560}
1561
1562EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1563{
1564 clearError();
1565
1566 const egl_display_ptr dp = validate_display(dpy);
1567 if (!dp) return EGL_FALSE;
1568
1569 EGLBoolean result = EGL_FALSE;
1570 egl_connection_t* const cnx = &gEGLImpl;
1571 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1572 result = cnx->egl.eglDestroyStreamKHR(
1573 dp->disp.dpy, stream);
1574 }
1575 return result;
1576}
1577
1578EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1579 EGLenum attribute, EGLint value)
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.eglStreamAttribKHR) {
1589 result = cnx->egl.eglStreamAttribKHR(
1590 dp->disp.dpy, stream, attribute, value);
1591 }
1592 return result;
1593}
1594
1595EGLBoolean eglQueryStreamKHR(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.eglQueryStreamKHR) {
1606 result = cnx->egl.eglQueryStreamKHR(
1607 dp->disp.dpy, stream, attribute, value);
1608 }
1609 return result;
1610}
1611
1612EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1613 EGLenum attribute, EGLuint64KHR *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.eglQueryStreamu64KHR) {
1623 result = cnx->egl.eglQueryStreamu64KHR(
1624 dp->disp.dpy, stream, attribute, value);
1625 }
1626 return result;
1627}
1628
1629EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1630 EGLenum attribute, EGLTimeKHR *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.eglQueryStreamTimeKHR) {
1640 result = cnx->egl.eglQueryStreamTimeKHR(
1641 dp->disp.dpy, stream, attribute, value);
1642 }
1643 return result;
1644}
1645
1646EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1647 EGLStreamKHR stream, const EGLint *attrib_list)
1648{
1649 clearError();
1650
1651 egl_display_ptr dp = validate_display(dpy);
1652 if (!dp) return EGL_NO_SURFACE;
1653
1654 egl_connection_t* const cnx = &gEGLImpl;
1655 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1656 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1657 dp->disp.dpy, config, stream, attrib_list);
1658 if (surface != EGL_NO_SURFACE) {
1659 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1660 surface, cnx);
1661 return s;
1662 }
1663 }
1664 return EGL_NO_SURFACE;
1665}
1666
1667EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1668 EGLStreamKHR stream)
1669{
1670 clearError();
1671
1672 const egl_display_ptr dp = validate_display(dpy);
1673 if (!dp) return EGL_FALSE;
1674
1675 EGLBoolean result = EGL_FALSE;
1676 egl_connection_t* const cnx = &gEGLImpl;
1677 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1678 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1679 dp->disp.dpy, stream);
1680 }
1681 return result;
1682}
1683
1684EGLBoolean eglStreamConsumerAcquireKHR(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.eglStreamConsumerAcquireKHR) {
1695 result = cnx->egl.eglStreamConsumerAcquireKHR(
1696 dp->disp.dpy, stream);
1697 }
1698 return result;
1699}
1700
1701EGLBoolean eglStreamConsumerReleaseKHR(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.eglStreamConsumerReleaseKHR) {
1712 result = cnx->egl.eglStreamConsumerReleaseKHR(
1713 dp->disp.dpy, stream);
1714 }
1715 return result;
1716}
1717
1718EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1719 EGLDisplay dpy, EGLStreamKHR stream)
1720{
1721 clearError();
1722
1723 const egl_display_ptr dp = validate_display(dpy);
1724 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1725
1726 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1727 egl_connection_t* const cnx = &gEGLImpl;
1728 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1729 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1730 dp->disp.dpy, stream);
1731 }
1732 return result;
1733}
1734
1735EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1736 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1737{
1738 clearError();
1739
1740 const egl_display_ptr dp = validate_display(dpy);
1741 if (!dp) return EGL_NO_STREAM_KHR;
1742
1743 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1744 egl_connection_t* const cnx = &gEGLImpl;
1745 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1746 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1747 dp->disp.dpy, file_descriptor);
1748 }
1749 return result;
1750}
1751
Mathias Agopian518ec112011-05-13 16:21:08 -07001752// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001753// EGL_EGLEXT_VERSION 15
1754// ----------------------------------------------------------------------------
1755
1756EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1757 clearError();
1758 const egl_display_ptr dp = validate_display(dpy);
1759 if (!dp) return EGL_FALSE;
1760 EGLint result = EGL_FALSE;
1761 egl_connection_t* const cnx = &gEGLImpl;
1762 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1763 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1764 }
1765 return result;
1766}
1767
1768// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001769// ANDROID extensions
1770// ----------------------------------------------------------------------------
1771
Jamie Gennis331841b2012-09-06 14:52:00 -07001772EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1773{
1774 clearError();
1775
1776 const egl_display_ptr dp = validate_display(dpy);
1777 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1778
1779 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1780 egl_connection_t* const cnx = &gEGLImpl;
1781 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1782 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1783 }
1784 return result;
1785}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001786
Andy McFadden72841452013-03-01 16:25:32 -08001787EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1788 EGLnsecsANDROID time)
1789{
1790 clearError();
1791
1792 const egl_display_ptr dp = validate_display(dpy);
1793 if (!dp) {
1794 return EGL_FALSE;
1795 }
1796
1797 SurfaceRef _s(dp.get(), surface);
1798 if (!_s.get()) {
1799 setError(EGL_BAD_SURFACE, EGL_FALSE);
1800 return EGL_FALSE;
1801 }
1802
1803 egl_surface_t const * const s = get_surface(surface);
1804 native_window_set_buffers_timestamp(s->win.get(), time);
1805
1806 return EGL_TRUE;
1807}
1808
Craig Donner05249fc2016-01-15 19:33:55 -08001809EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1810{
1811 clearError();
1812
1813 int usage = 0;
1814 uint32_t width = 0;
1815 uint32_t height = 0;
1816 uint32_t format = 0;
1817 uint32_t red_size = 0;
1818 uint32_t green_size = 0;
1819 uint32_t blue_size = 0;
1820 uint32_t alpha_size = 0;
1821
Craig Donnerb40504a2016-06-03 17:54:25 -07001822#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001823 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001824 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001825 target = value; \
1826 } else { \
1827 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1828 } \
1829 break
1830
1831 if (attrib_list) {
1832 while (*attrib_list != EGL_NONE) {
1833 GLint attr = *attrib_list++;
1834 GLint value = *attrib_list++;
1835 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001836 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1837 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1838 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1839 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1840 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1841 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner05249fc2016-01-15 19:33:55 -08001842 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1843 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1844 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001845 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001846 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001847 usage |= GRALLOC_USAGE_HW_RENDER;
1848 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001849 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001850 usage |= GRALLOC_USAGE_HW_TEXTURE;
1851 }
Craig Donner05249fc2016-01-15 19:33:55 -08001852 break;
1853 default:
1854 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1855 }
1856 }
1857 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001858#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001859
1860 // Validate format.
1861 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1862 if (alpha_size == 8) {
1863 format = HAL_PIXEL_FORMAT_RGBA_8888;
1864 } else {
1865 format = HAL_PIXEL_FORMAT_RGB_888;
1866 }
1867 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1868 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001869 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001870 } else {
1871 ALOGE("Invalid native pixel format { r=%d, g=%d, b=%d, a=%d }",
1872 red_size, green_size, blue_size, alpha_size);
1873 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1874 }
1875
Craig Donner68671532016-07-18 10:19:54 -07001876#define CHECK_ERROR_CONDITION(message) \
1877 if (err != NO_ERROR) { \
1878 ALOGE(message); \
1879 goto error_condition; \
1880 }
1881
1882 // The holder is used to destroy the buffer if an error occurs.
1883 GraphicBuffer* gBuffer = new GraphicBuffer();
1884 sp<IServiceManager> sm = defaultServiceManager();
1885 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1886 sp<IBinder> allocator;
1887 Parcel sc_data, sc_reply, data, reply;
1888 status_t err = NO_ERROR;
1889 if (sm == NULL) {
1890 ALOGE("Unable to connect to ServiceManager");
1891 goto error_condition;
1892 }
1893
1894 // Obtain an allocator.
1895 if (surfaceFlinger == NULL) {
1896 ALOGE("Unable to connect to SurfaceFlinger");
1897 goto error_condition;
1898 }
1899 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1900 err = surfaceFlinger->transact(
1901 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1902 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1903 allocator = sc_reply.readStrongBinder();
1904
1905 if (allocator == NULL) {
1906 ALOGE("Unable to obtain an ISurfaceComposer");
1907 goto error_condition;
1908 }
1909 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1910 err = data.writeUint32(width);
1911 CHECK_ERROR_CONDITION("Unable to write width");
1912 err = data.writeUint32(height);
1913 CHECK_ERROR_CONDITION("Unable to write height");
1914 err = data.writeInt32(static_cast<int32_t>(format));
1915 CHECK_ERROR_CONDITION("Unable to write format");
1916 err = data.writeUint32(usage);
1917 CHECK_ERROR_CONDITION("Unable to write usage");
Dan Stozad4079af2016-08-22 17:26:41 -07001918 err = data.writeUtf8AsUtf16(
1919 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1920 std::to_string(getpid()) + ']');
1921 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001922 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1923 &reply);
1924 CHECK_ERROR_CONDITION(
1925 "Unable to request buffer allocation from surface composer");
1926 err = reply.readInt32();
1927 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1928 err = reply.read(*gBuffer);
1929 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1930
1931 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001932 if (err != NO_ERROR) {
1933 ALOGE("Unable to create native buffer { w=%d, h=%d, f=%d, u=%#x }: %#x",
1934 width, height, format, usage, err);
Craig Donner68671532016-07-18 10:19:54 -07001935 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001936 }
1937 ALOGD("Created new native buffer %p { w=%d, h=%d, f=%d, u=%#x }",
1938 gBuffer, width, height, format, usage);
1939 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001940
1941#undef CHECK_ERROR_CONDITION
1942
1943error_condition:
1944 // Delete the buffer.
1945 sp<GraphicBuffer> holder(gBuffer);
1946 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001947}
1948
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001949// ----------------------------------------------------------------------------
1950// NVIDIA extensions
1951// ----------------------------------------------------------------------------
1952EGLuint64NV eglGetSystemTimeFrequencyNV()
1953{
1954 clearError();
1955
1956 if (egl_init_drivers() == EGL_FALSE) {
1957 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1958 }
1959
1960 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001961 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001962
Mathias Agopianada798b2012-02-13 17:09:30 -08001963 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1964 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001965 }
1966
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001967 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001968}
1969
1970EGLuint64NV eglGetSystemTimeNV()
1971{
1972 clearError();
1973
1974 if (egl_init_drivers() == EGL_FALSE) {
1975 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1976 }
1977
1978 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001979 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001980
Mathias Agopianada798b2012-02-13 17:09:30 -08001981 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1982 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001983 }
1984
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001985 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001986}
Dan Stozaa894d082015-02-19 15:27:36 -08001987
1988// ----------------------------------------------------------------------------
1989// Partial update extension
1990// ----------------------------------------------------------------------------
1991EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1992 EGLint *rects, EGLint n_rects)
1993{
1994 clearError();
1995
1996 const egl_display_ptr dp = validate_display(dpy);
1997 if (!dp) {
1998 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1999 return EGL_FALSE;
2000 }
2001
2002 SurfaceRef _s(dp.get(), surface);
2003 if (!_s.get()) {
2004 setError(EGL_BAD_SURFACE, EGL_FALSE);
2005 return EGL_FALSE;
2006 }
2007
2008 egl_surface_t const * const s = get_surface(surface);
2009 if (s->cnx->egl.eglSetDamageRegionKHR) {
2010 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2011 rects, n_rects);
2012 }
2013
2014 return EGL_FALSE;
2015}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002016
2017EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
2018 EGLint framesAgo, EGLint numTimestamps, const EGLint *timestamps,
2019 EGLnsecsANDROID *values)
2020{
2021 clearError();
2022
2023 const egl_display_ptr dp = validate_display(dpy);
2024 if (!dp) {
2025 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2026 return EGL_FALSE;
2027 }
2028
2029 SurfaceRef _s(dp.get(), surface);
2030 if (!_s.get()) {
2031 setError(EGL_BAD_SURFACE, EGL_FALSE);
2032 return EGL_FALSE;
2033 }
2034
2035 egl_surface_t const * const s = get_surface(surface);
2036
2037 if (!s->enableTimestamps) {
2038 setError(EGL_BAD_SURFACE, EGL_FALSE);
2039 return EGL_FALSE;
2040 }
2041
2042 nsecs_t* postedTime = nullptr;
2043 nsecs_t* acquireTime = nullptr;
2044 nsecs_t* refreshStartTime = nullptr;
2045 nsecs_t* GLCompositionDoneTime = nullptr;
2046 nsecs_t* displayRetireTime = nullptr;
2047 nsecs_t* releaseTime = nullptr;
2048
2049 for (int i = 0; i < numTimestamps; i++) {
2050 switch (timestamps[i]) {
2051 case EGL_QUEUE_TIME_ANDROID:
2052 postedTime = &values[i];
2053 break;
2054 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2055 acquireTime = &values[i];
2056 break;
2057 case EGL_COMPOSITION_START_TIME_ANDROID:
2058 refreshStartTime = &values[i];
2059 break;
2060 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
2061 GLCompositionDoneTime = &values[i];
2062 break;
2063 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2064 displayRetireTime = &values[i];
2065 break;
2066 case EGL_READS_DONE_TIME_ANDROID:
2067 releaseTime = &values[i];
2068 break;
2069 default:
2070 setError(EGL_BAD_PARAMETER, EGL_FALSE);
2071 return EGL_FALSE;
2072 }
2073 }
2074
2075 status_t ret = native_window_get_frame_timestamps(s->win.get(), framesAgo,
2076 postedTime, acquireTime, refreshStartTime, GLCompositionDoneTime,
2077 displayRetireTime, releaseTime);
2078
2079 if (ret != NO_ERROR) {
2080 setError(EGL_BAD_ACCESS, EGL_FALSE);
2081 return EGL_FALSE;
2082 }
2083
2084 return EGL_TRUE;
2085}
2086
2087EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2088 EGLint timestamp)
2089{
2090 clearError();
2091
2092 const egl_display_ptr dp = validate_display(dpy);
2093 if (!dp) {
2094 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2095 return EGL_FALSE;
2096 }
2097
2098 SurfaceRef _s(dp.get(), surface);
2099 if (!_s.get()) {
2100 setError(EGL_BAD_SURFACE, EGL_FALSE);
2101 return EGL_FALSE;
2102 }
2103
2104 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002105#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07002106 case EGL_QUEUE_TIME_ANDROID:
2107 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2108 case EGL_COMPOSITION_START_TIME_ANDROID:
2109 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
2110 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2111 case EGL_READS_DONE_TIME_ANDROID:
2112 return EGL_TRUE;
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002113#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002114 default:
2115 return EGL_FALSE;
2116 }
2117}