blob: c2fc6bd3947b73774548cce0bc344c2542a602dd [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
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#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
Mathias Agopian518ec112011-05-13 16:21:08 -070030#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070031#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070032#include <cutils/memory.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
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
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070059#define ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS 0
60
Mathias Agopian518ec112011-05-13 16:21:08 -070061// ----------------------------------------------------------------------------
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063namespace android {
64
Mathias Agopian518ec112011-05-13 16:21:08 -070065struct extention_map_t {
66 const char* name;
67 __eglMustCastToProperFunctionPointerType address;
68};
69
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070070/*
Jesse Hall21558da2013-08-06 15:31:22 -070071 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070072 *
Jesse Hall21558da2013-08-06 15:31:22 -070073 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
74 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070075 *
Jesse Hall21558da2013-08-06 15:31:22 -070076 * The rest (gExtensionString) depend on support in the EGL driver, and are
77 * only available if the driver supports them. However, some of these must be
78 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080079 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070080 * the mandatory extensions are present and may not function properly if some
81 * are missing.
82 *
83 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070084 */
Jesse Hall21558da2013-08-06 15:31:22 -070085extern char const * const gBuiltinExtensionString =
86 "EGL_KHR_get_all_proc_addresses "
87 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080088 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080089 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080090 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070091#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -070092 "EGL_ANDROID_get_frame_timestamps "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070093#endif
Jesse Hall21558da2013-08-06 15:31:22 -070094 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095extern char const * const gExtensionString =
96 "EGL_KHR_image " // mandatory
97 "EGL_KHR_image_base " // mandatory
98 "EGL_KHR_image_pixmap "
99 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700100 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700101 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700102 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_cubemap_image "
104 "EGL_KHR_gl_renderbuffer_image "
105 "EGL_KHR_reusable_sync "
106 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700107 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700108 "EGL_KHR_config_attribs "
109 "EGL_KHR_surfaceless_context "
110 "EGL_KHR_stream "
111 "EGL_KHR_stream_fifo "
112 "EGL_KHR_stream_producer_eglsurface "
113 "EGL_KHR_stream_consumer_gltexture "
114 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700115 "EGL_EXT_create_context_robustness "
116 "EGL_NV_system_time "
117 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700118 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700119 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800120 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700121 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800122 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700123 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700124 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700125 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700126 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000127 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700128 ;
129
130// extensions not exposed to applications but used by the ANDROID system
131// "EGL_ANDROID_blob_cache " // strongly recommended
132// "EGL_IMG_hibernate_process " // optional
133// "EGL_ANDROID_native_fence_sync " // strongly recommended
134// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700135// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700136
137/*
138 * EGL Extensions entry-points exposed to 3rd party applications
139 * (keep in sync with gExtensionString above)
140 *
141 */
142static const extention_map_t sExtensionMap[] = {
143 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700144 { "eglLockSurfaceKHR",
145 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
146 { "eglUnlockSurfaceKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700148
149 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700150 { "eglCreateImageKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
152 { "eglDestroyImageKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700154
155 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
156 { "eglCreateSyncKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
158 { "eglDestroySyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
160 { "eglClientWaitSyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
162 { "eglSignalSyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
164 { "eglGetSyncAttribKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
166
167 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800168 { "eglGetSystemTimeFrequencyNV",
169 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
170 { "eglGetSystemTimeNV",
171 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700172
Mathias Agopian2bb71682013-03-27 17:32:41 -0700173 // EGL_KHR_wait_sync
174 { "eglWaitSyncKHR",
175 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700176
177 // EGL_ANDROID_presentation_time
178 { "eglPresentationTimeANDROID",
179 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800180
181 // EGL_KHR_swap_buffers_with_damage
182 { "eglSwapBuffersWithDamageKHR",
183 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
184
Craig Donner05249fc2016-01-15 19:33:55 -0800185 // EGL_ANDROID_native_client_buffer
186 { "eglCreateNativeClientBufferANDROID",
187 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
188
Dan Stozaa894d082015-02-19 15:27:36 -0800189 // EGL_KHR_partial_update
190 { "eglSetDamageRegionKHR",
191 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700192
193 { "eglCreateStreamKHR",
194 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
195 { "eglDestroyStreamKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
197 { "eglStreamAttribKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
199 { "eglQueryStreamKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
201 { "eglQueryStreamu64KHR",
202 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
203 { "eglQueryStreamTimeKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
205 { "eglCreateStreamProducerSurfaceKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
207 { "eglStreamConsumerGLTextureExternalKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
209 { "eglStreamConsumerAcquireKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
211 { "eglStreamConsumerReleaseKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
213 { "eglGetStreamFileDescriptorKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
215 { "eglCreateStreamFromFileDescriptorKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700217
218 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800219 { "eglGetNextFrameIdANDROID",
220 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700221 { "eglGetFrameTimestampsANDROID",
222 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
223 { "eglQueryTimestampSupportedANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglQueryTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700225};
226
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700227/*
228 * These extensions entry-points should not be exposed to applications.
229 * They're used internally by the Android EGL layer.
230 */
231#define FILTER_EXTENSIONS(procname) \
232 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
233 !strcmp((procname), "eglHibernateProcessIMG") || \
234 !strcmp((procname), "eglAwakenProcessIMG") || \
235 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
236
237
238
Mathias Agopian518ec112011-05-13 16:21:08 -0700239// accesses protected by sExtensionMapMutex
240static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
241static int sGLExtentionSlot = 0;
242static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
243
244static void(*findProcAddress(const char* name,
245 const extention_map_t* map, size_t n))() {
246 for (uint32_t i=0 ; i<n ; i++) {
247 if (!strcmp(name, map[i].name)) {
248 return map[i].address;
249 }
250 }
251 return NULL;
252}
253
254// ----------------------------------------------------------------------------
255
Mathias Agopian518ec112011-05-13 16:21:08 -0700256extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
257extern EGLBoolean egl_init_drivers();
258extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700259extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700260
Mathias Agopian518ec112011-05-13 16:21:08 -0700261} // namespace android;
262
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700263
Mathias Agopian518ec112011-05-13 16:21:08 -0700264// ----------------------------------------------------------------------------
265
266static inline void clearError() { egl_tls_t::clearError(); }
267static inline EGLContext getContext() { return egl_tls_t::getContext(); }
268
269// ----------------------------------------------------------------------------
270
271EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
272{
Jesse Hall1508ae62017-01-19 17:43:26 -0800273 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700274 clearError();
275
Dan Stozac3289c42014-01-17 11:38:34 -0800276 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700277 if (index >= NUM_DISPLAYS) {
278 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
279 }
280
281 if (egl_init_drivers() == EGL_FALSE) {
282 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
283 }
284
285 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
286 return dpy;
287}
288
289// ----------------------------------------------------------------------------
290// Initialization
291// ----------------------------------------------------------------------------
292
293EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
294{
295 clearError();
296
Jesse Hallb29e5e82012-04-04 16:53:42 -0700297 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700298 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
299
300 EGLBoolean res = dp->initialize(major, minor);
301
302 return res;
303}
304
305EGLBoolean eglTerminate(EGLDisplay dpy)
306{
307 // NOTE: don't unload the drivers b/c some APIs can be called
308 // after eglTerminate() has been called. eglTerminate() only
309 // terminates an EGLDisplay, not a EGL itself.
310
311 clearError();
312
Jesse Hallb29e5e82012-04-04 16:53:42 -0700313 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700314 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
315
316 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800317
Mathias Agopian518ec112011-05-13 16:21:08 -0700318 return res;
319}
320
321// ----------------------------------------------------------------------------
322// configuration
323// ----------------------------------------------------------------------------
324
325EGLBoolean eglGetConfigs( EGLDisplay dpy,
326 EGLConfig *configs,
327 EGLint config_size, EGLint *num_config)
328{
329 clearError();
330
Jesse Hallb29e5e82012-04-04 16:53:42 -0700331 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700332 if (!dp) return EGL_FALSE;
333
Mathias Agopian7773c432012-02-13 20:06:08 -0800334 if (num_config==0) {
335 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700336 }
337
Mathias Agopian7773c432012-02-13 20:06:08 -0800338 EGLBoolean res = EGL_FALSE;
339 *num_config = 0;
340
341 egl_connection_t* const cnx = &gEGLImpl;
342 if (cnx->dso) {
343 res = cnx->egl.eglGetConfigs(
344 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700345 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800346
347 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700348}
349
350EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
351 EGLConfig *configs, EGLint config_size,
352 EGLint *num_config)
353{
354 clearError();
355
Jesse Hallb29e5e82012-04-04 16:53:42 -0700356 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700357 if (!dp) return EGL_FALSE;
358
359 if (num_config==0) {
360 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
361 }
362
Mathias Agopian518ec112011-05-13 16:21:08 -0700363 EGLBoolean res = EGL_FALSE;
364 *num_config = 0;
365
Mathias Agopianada798b2012-02-13 17:09:30 -0800366 egl_connection_t* const cnx = &gEGLImpl;
367 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700368 if (attrib_list) {
369 char value[PROPERTY_VALUE_MAX];
370 property_get("debug.egl.force_msaa", value, "false");
371
372 if (!strcmp(value, "true")) {
373 size_t attribCount = 0;
374 EGLint attrib = attrib_list[0];
375
376 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700377 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700378 const EGLint *attribRendererable = NULL;
379 const EGLint *attribCaveat = NULL;
380
381 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700382 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700383 while (attrib != EGL_NONE) {
384 attrib = attrib_list[attribCount];
385 switch (attrib) {
386 case EGL_RENDERABLE_TYPE:
387 attribRendererable = &attrib_list[attribCount];
388 break;
389 case EGL_CONFIG_CAVEAT:
390 attribCaveat = &attrib_list[attribCount];
391 break;
392 }
393 attribCount++;
394 }
395
396 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
397 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800398
Romain Guy1cffc802012-10-15 18:13:05 -0700399 // Insert 2 extra attributes to force-enable MSAA 4x
400 EGLint aaAttribs[attribCount + 4];
401 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
402 aaAttribs[1] = 1;
403 aaAttribs[2] = EGL_SAMPLES;
404 aaAttribs[3] = 4;
405
406 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
407
408 EGLint numConfigAA;
409 EGLBoolean resAA = cnx->egl.eglChooseConfig(
410 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
411
412 if (resAA == EGL_TRUE && numConfigAA > 0) {
413 ALOGD("Enabling MSAA 4x");
414 *num_config = numConfigAA;
415 return resAA;
416 }
417 }
418 }
419 }
420
Mathias Agopian7773c432012-02-13 20:06:08 -0800421 res = cnx->egl.eglChooseConfig(
422 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700423 }
424 return res;
425}
426
427EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
428 EGLint attribute, EGLint *value)
429{
430 clearError();
431
Jesse Hallb29e5e82012-04-04 16:53:42 -0700432 egl_connection_t* cnx = NULL;
433 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
434 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800435
Mathias Agopian518ec112011-05-13 16:21:08 -0700436 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800437 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700438}
439
440// ----------------------------------------------------------------------------
441// surfaces
442// ----------------------------------------------------------------------------
443
Jesse Hallc2e41222013-08-08 13:40:22 -0700444// Turn linear formats into corresponding sRGB formats when colorspace is
445// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
446// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800447// the modification isn't possible, the original dataSpace is returned.
448static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
449 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700450 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800451 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700452 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800453 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700454 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800455 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700456}
457
Mathias Agopian518ec112011-05-13 16:21:08 -0700458EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
459 NativeWindowType window,
460 const EGLint *attrib_list)
461{
462 clearError();
463
Jesse Hallb29e5e82012-04-04 16:53:42 -0700464 egl_connection_t* cnx = NULL;
465 egl_display_ptr dp = validate_display_connection(dpy, cnx);
466 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800467 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700468
Andy McFaddend566ce32014-01-07 15:54:17 -0800469 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
470 if (result != OK) {
471 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
472 "failed (%#x) (already connected to another API?)",
473 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700474 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700475 }
476
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700477 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700478 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
479 // of our native format. So if sRGB gamma is requested, we have to
480 // modify the EGLconfig's format before setting the native window's
481 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800482
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700483 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
484 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
485 &componentType);
486
487 // by default, just pick appropriate RGBA
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700488 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700489 if (dp->haveExtension("EGL_EXT_pixel_format_float") &&
490 (componentType == EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT)) {
491 format = HAL_PIXEL_FORMAT_RGBA_FP16;
492 }
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;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700496 EGLint r, g, b;
497 r = g = b = 0;
498 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
499 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
500 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700501 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700502 EGLint colorDepth = r + g + b;
503
504 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700505 if (colorDepth <= 16) {
506 format = HAL_PIXEL_FORMAT_RGB_565;
507 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700508 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
509 if (colorDepth > 24) {
510 format = HAL_PIXEL_FORMAT_RGBA_1010102;
511 } else {
512 format = HAL_PIXEL_FORMAT_RGBX_8888;
513 }
514 } else {
515 format = HAL_PIXEL_FORMAT_RGBA_FP16;
516 }
517 }
518 } else {
519 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
520 if (colorDepth > 24) {
521 format = HAL_PIXEL_FORMAT_RGBA_1010102;
522 } else {
523 format = HAL_PIXEL_FORMAT_RGBA_8888;
524 }
525 } else {
526 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700527 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700528 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700529
530 // now select a corresponding sRGB format if needed
531 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
532 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
533 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530534 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700535 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700536 }
537 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800538
Jesse Hallc2e41222013-08-08 13:40:22 -0700539 if (format != 0) {
540 int err = native_window_set_buffers_format(window, format);
541 if (err != 0) {
542 ALOGE("error setting native window pixel format: %s (%d)",
543 strerror(-err), err);
544 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
545 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
546 }
547 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700548
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800549 if (dataSpace != 0) {
550 int err = native_window_set_buffers_data_space(window, dataSpace);
551 if (err != 0) {
552 ALOGE("error setting native window pixel dataSpace: %s (%d)",
553 strerror(-err), err);
554 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
555 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
556 }
557 }
558
Jamie Gennis59769462011-11-19 18:04:43 -0800559 // the EGL spec requires that a new EGLSurface default to swap interval
560 // 1, so explicitly set that on the window here.
561 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
562 anw->setSwapInterval(anw, 1);
563
Mathias Agopian518ec112011-05-13 16:21:08 -0700564 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800565 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700566 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700567 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
568 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700569 return s;
570 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700571
572 // EGLSurface creation failed
573 native_window_set_buffers_format(window, 0);
574 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 }
576 return EGL_NO_SURFACE;
577}
578
579EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
580 NativePixmapType pixmap,
581 const EGLint *attrib_list)
582{
583 clearError();
584
Jesse Hallb29e5e82012-04-04 16:53:42 -0700585 egl_connection_t* cnx = NULL;
586 egl_display_ptr dp = validate_display_connection(dpy, cnx);
587 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700588 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800589 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700590 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700591 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
592 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700593 return s;
594 }
595 }
596 return EGL_NO_SURFACE;
597}
598
599EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
600 const EGLint *attrib_list)
601{
602 clearError();
603
Jesse Hallb29e5e82012-04-04 16:53:42 -0700604 egl_connection_t* cnx = NULL;
605 egl_display_ptr dp = validate_display_connection(dpy, cnx);
606 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700607 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800608 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700609 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700610 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
611 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700612 return s;
613 }
614 }
615 return EGL_NO_SURFACE;
616}
Jesse Hall47743382013-02-08 11:13:46 -0800617
Mathias Agopian518ec112011-05-13 16:21:08 -0700618EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
619{
620 clearError();
621
Jesse Hallb29e5e82012-04-04 16:53:42 -0700622 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700623 if (!dp) return EGL_FALSE;
624
Jesse Hallb29e5e82012-04-04 16:53:42 -0700625 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700626 if (!_s.get())
627 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700628
629 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800630 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700631 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700632 _s.terminate();
633 }
634 return result;
635}
636
637EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
638 EGLint attribute, EGLint *value)
639{
640 clearError();
641
Jesse Hallb29e5e82012-04-04 16:53:42 -0700642 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700643 if (!dp) return EGL_FALSE;
644
Jesse Hallb29e5e82012-04-04 16:53:42 -0700645 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700646 if (!_s.get())
647 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700648
Mathias Agopian518ec112011-05-13 16:21:08 -0700649 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800650 return s->cnx->egl.eglQuerySurface(
651 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700652}
653
Jamie Gennise8696a42012-01-15 18:54:57 -0800654void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800655 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800656 clearError();
657
Jesse Hallb29e5e82012-04-04 16:53:42 -0700658 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800659 if (!dp) {
660 return;
661 }
662
Jesse Hallb29e5e82012-04-04 16:53:42 -0700663 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800664 if (!_s.get()) {
665 setError(EGL_BAD_SURFACE, EGL_FALSE);
666 return;
667 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800668}
669
Mathias Agopian518ec112011-05-13 16:21:08 -0700670// ----------------------------------------------------------------------------
671// Contexts
672// ----------------------------------------------------------------------------
673
674EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
675 EGLContext share_list, const EGLint *attrib_list)
676{
677 clearError();
678
Jesse Hallb29e5e82012-04-04 16:53:42 -0700679 egl_connection_t* cnx = NULL;
680 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700681 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700682 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700683 if (!ContextRef(dp.get(), share_list).get()) {
684 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
685 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700686 egl_context_t* const c = get_context(share_list);
687 share_list = c->context;
688 }
689 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800690 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700691 if (context != EGL_NO_CONTEXT) {
692 // figure out if it's a GLESv1 or GLESv2
693 int version = 0;
694 if (attrib_list) {
695 while (*attrib_list != EGL_NONE) {
696 GLint attr = *attrib_list++;
697 GLint value = *attrib_list++;
698 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
699 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800700 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800701 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800702 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700703 }
704 }
705 };
706 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700707 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
708 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700709 return c;
710 }
711 }
712 return EGL_NO_CONTEXT;
713}
714
715EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
716{
717 clearError();
718
Jesse Hallb29e5e82012-04-04 16:53:42 -0700719 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700720 if (!dp)
721 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700722
Jesse Hallb29e5e82012-04-04 16:53:42 -0700723 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700724 if (!_c.get())
725 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800726
Mathias Agopian518ec112011-05-13 16:21:08 -0700727 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800728 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700729 if (result == EGL_TRUE) {
730 _c.terminate();
731 }
732 return result;
733}
734
Mathias Agopian518ec112011-05-13 16:21:08 -0700735EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
736 EGLSurface read, EGLContext ctx)
737{
738 clearError();
739
Jesse Hallb29e5e82012-04-04 16:53:42 -0700740 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700741 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
742
Mathias Agopian5b287a62011-05-16 18:58:55 -0700743 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
744 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
745 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700746 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
747 (draw != EGL_NO_SURFACE) ) {
748 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
749 }
750
751 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700752 ContextRef _c(dp.get(), ctx);
753 SurfaceRef _d(dp.get(), draw);
754 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700755
756 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700757 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700759 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700760 }
761
762 // these are the underlying implementation's object
763 EGLContext impl_ctx = EGL_NO_CONTEXT;
764 EGLSurface impl_draw = EGL_NO_SURFACE;
765 EGLSurface impl_read = EGL_NO_SURFACE;
766
767 // these are our objects structs passed in
768 egl_context_t * c = NULL;
769 egl_surface_t const * d = NULL;
770 egl_surface_t const * r = NULL;
771
772 // these are the current objects structs
773 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800774
Mathias Agopian518ec112011-05-13 16:21:08 -0700775 if (ctx != EGL_NO_CONTEXT) {
776 c = get_context(ctx);
777 impl_ctx = c->context;
778 } else {
779 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700780 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
781 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
782 return setError(EGL_BAD_MATCH, EGL_FALSE);
783 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700784 if (cur_c == NULL) {
785 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700786 // not an error, there is just no current context.
787 return EGL_TRUE;
788 }
789 }
790
791 // retrieve the underlying implementation's draw EGLSurface
792 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700793 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700794 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700795 impl_draw = d->surface;
796 }
797
798 // retrieve the underlying implementation's read EGLSurface
799 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700800 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700801 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700802 impl_read = r->surface;
803 }
804
Mathias Agopian518ec112011-05-13 16:21:08 -0700805
Jesse Hallb29e5e82012-04-04 16:53:42 -0700806 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800807 draw, read, ctx,
808 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700809
810 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800811 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700812 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
813 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700814 _c.acquire();
815 _r.acquire();
816 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700817 } else {
818 setGLHooksThreadSpecific(&gHooksNoContext);
819 egl_tls_t::setContext(EGL_NO_CONTEXT);
820 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700821 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000822 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700823 egl_connection_t* const cnx = &gEGLImpl;
824 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700825 }
826 return result;
827}
828
829
830EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
831 EGLint attribute, EGLint *value)
832{
833 clearError();
834
Jesse Hallb29e5e82012-04-04 16:53:42 -0700835 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700836 if (!dp) return EGL_FALSE;
837
Jesse Hallb29e5e82012-04-04 16:53:42 -0700838 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700839 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
840
Mathias Agopian518ec112011-05-13 16:21:08 -0700841 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800842 return c->cnx->egl.eglQueryContext(
843 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700844
Mathias Agopian518ec112011-05-13 16:21:08 -0700845}
846
847EGLContext eglGetCurrentContext(void)
848{
849 // could be called before eglInitialize(), but we wouldn't have a context
850 // then, and this function would correctly return EGL_NO_CONTEXT.
851
852 clearError();
853
854 EGLContext ctx = getContext();
855 return ctx;
856}
857
858EGLSurface eglGetCurrentSurface(EGLint readdraw)
859{
860 // could be called before eglInitialize(), but we wouldn't have a context
861 // then, and this function would correctly return EGL_NO_SURFACE.
862
863 clearError();
864
865 EGLContext ctx = getContext();
866 if (ctx) {
867 egl_context_t const * const c = get_context(ctx);
868 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
869 switch (readdraw) {
870 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800871 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700872 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
873 }
874 }
875 return EGL_NO_SURFACE;
876}
877
878EGLDisplay eglGetCurrentDisplay(void)
879{
880 // could be called before eglInitialize(), but we wouldn't have a context
881 // then, and this function would correctly return EGL_NO_DISPLAY.
882
883 clearError();
884
885 EGLContext ctx = getContext();
886 if (ctx) {
887 egl_context_t const * const c = get_context(ctx);
888 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
889 return c->dpy;
890 }
891 return EGL_NO_DISPLAY;
892}
893
894EGLBoolean eglWaitGL(void)
895{
Mathias Agopian518ec112011-05-13 16:21:08 -0700896 clearError();
897
Mathias Agopianada798b2012-02-13 17:09:30 -0800898 egl_connection_t* const cnx = &gEGLImpl;
899 if (!cnx->dso)
900 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
901
902 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700903}
904
905EGLBoolean eglWaitNative(EGLint engine)
906{
Mathias Agopian518ec112011-05-13 16:21:08 -0700907 clearError();
908
Mathias Agopianada798b2012-02-13 17:09:30 -0800909 egl_connection_t* const cnx = &gEGLImpl;
910 if (!cnx->dso)
911 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
912
913 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700914}
915
916EGLint eglGetError(void)
917{
Mathias Agopianada798b2012-02-13 17:09:30 -0800918 EGLint err = EGL_SUCCESS;
919 egl_connection_t* const cnx = &gEGLImpl;
920 if (cnx->dso) {
921 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700922 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800923 if (err == EGL_SUCCESS) {
924 err = egl_tls_t::getError();
925 }
926 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700927}
928
Michael Chockc0ec5e22014-01-27 08:14:33 -0800929static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700930 const char* procname) {
931 const egl_connection_t* cnx = &gEGLImpl;
932 void* proc = NULL;
933
Michael Chockc0ec5e22014-01-27 08:14:33 -0800934 proc = dlsym(cnx->libEgl, procname);
935 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
936
Jesse Hallc07b5202013-07-04 12:08:16 -0700937 proc = dlsym(cnx->libGles2, procname);
938 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
939
940 proc = dlsym(cnx->libGles1, procname);
941 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
942
943 return NULL;
944}
945
Mathias Agopian518ec112011-05-13 16:21:08 -0700946__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
947{
948 // eglGetProcAddress() could be the very first function called
949 // in which case we must make sure we've initialized ourselves, this
950 // happens the first time egl_get_display() is called.
951
952 clearError();
953
954 if (egl_init_drivers() == EGL_FALSE) {
955 setError(EGL_BAD_PARAMETER, NULL);
956 return NULL;
957 }
958
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700959 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700960 return NULL;
961 }
962
Mathias Agopian518ec112011-05-13 16:21:08 -0700963 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700964 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700965 if (addr) return addr;
966
Michael Chockc0ec5e22014-01-27 08:14:33 -0800967 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700968 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700969
Mathias Agopian518ec112011-05-13 16:21:08 -0700970 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
971 pthread_mutex_lock(&sExtensionMapMutex);
972
973 /*
974 * Since eglGetProcAddress() is not associated to anything, it needs
975 * to return a function pointer that "works" regardless of what
976 * the current context is.
977 *
978 * For this reason, we return a "forwarder", a small stub that takes
979 * care of calling the function associated with the context
980 * currently bound.
981 *
982 * We first look for extensions we've already resolved, if we're seeing
983 * this extension for the first time, we go through all our
984 * implementations and call eglGetProcAddress() and record the
985 * result in the appropriate implementation hooks and return the
986 * address of the forwarder corresponding to that hook set.
987 *
988 */
989
990 const String8 name(procname);
991 addr = sGLExtentionMap.valueFor(name);
992 const int slot = sGLExtentionSlot;
993
Steve Blocke6f43dd2012-01-06 19:20:56 +0000994 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700995 "no more slots for eglGetProcAddress(\"%s\")",
996 procname);
997
998 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
999 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001000
1001 egl_connection_t* const cnx = &gEGLImpl;
1002 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001003 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001004 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001005 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1006 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001007 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001008 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001009 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001010
Mathias Agopian518ec112011-05-13 16:21:08 -07001011 if (found) {
1012 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001013 sGLExtentionMap.add(name, addr);
1014 sGLExtentionSlot++;
1015 }
1016 }
1017
1018 pthread_mutex_unlock(&sExtensionMapMutex);
1019 return addr;
1020}
1021
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001022class FrameCompletionThread : public Thread {
1023public:
1024
1025 static void queueSync(EGLSyncKHR sync) {
1026 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1027 static bool running = false;
1028 if (!running) {
1029 thread->run("GPUFrameCompletion");
1030 running = true;
1031 }
1032 {
1033 Mutex::Autolock lock(thread->mMutex);
1034 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1035 thread->mFramesQueued).string());
1036 thread->mQueue.push_back(sync);
1037 thread->mCondition.signal();
1038 thread->mFramesQueued++;
1039 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1040 }
1041 }
1042
1043private:
1044 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1045
1046 virtual bool threadLoop() {
1047 EGLSyncKHR sync;
1048 uint32_t frameNum;
1049 {
1050 Mutex::Autolock lock(mMutex);
1051 while (mQueue.isEmpty()) {
1052 mCondition.wait(mMutex);
1053 }
1054 sync = mQueue[0];
1055 frameNum = mFramesCompleted;
1056 }
1057 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1058 {
1059 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1060 frameNum).string());
1061 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1062 if (result == EGL_FALSE) {
1063 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1064 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1065 ALOGE("FrameCompletion: timeout waiting for fence");
1066 }
1067 eglDestroySyncKHR(dpy, sync);
1068 }
1069 {
1070 Mutex::Autolock lock(mMutex);
1071 mQueue.removeAt(0);
1072 mFramesCompleted++;
1073 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1074 }
1075 return true;
1076 }
1077
1078 uint32_t mFramesQueued;
1079 uint32_t mFramesCompleted;
1080 Vector<EGLSyncKHR> mQueue;
1081 Condition mCondition;
1082 Mutex mMutex;
1083};
1084
Dan Stozaa894d082015-02-19 15:27:36 -08001085EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1086 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001087{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001088 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001089 clearError();
1090
Jesse Hallb29e5e82012-04-04 16:53:42 -07001091 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001092 if (!dp) return EGL_FALSE;
1093
Jesse Hallb29e5e82012-04-04 16:53:42 -07001094 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001095 if (!_s.get())
1096 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001097
Mathias Agopian518ec112011-05-13 16:21:08 -07001098 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001099
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001100 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1101 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1102 if (sync != EGL_NO_SYNC_KHR) {
1103 FrameCompletionThread::queueSync(sync);
1104 }
1105 }
1106
Mathias Agopian7db993a2012-03-25 00:49:46 -07001107 if (CC_UNLIKELY(dp->finishOnSwap)) {
1108 uint32_t pixel;
1109 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1110 if (c) {
1111 // glReadPixels() ensures that the frame is complete
1112 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1113 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1114 }
1115 }
1116
Dan Stozaa894d082015-02-19 15:27:36 -08001117 if (n_rects == 0) {
1118 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1119 }
1120
1121 Vector<android_native_rect_t> androidRects;
1122 for (int r = 0; r < n_rects; ++r) {
1123 int offset = r * 4;
1124 int x = rects[offset];
1125 int y = rects[offset + 1];
1126 int width = rects[offset + 2];
1127 int height = rects[offset + 3];
1128 android_native_rect_t androidRect;
1129 androidRect.left = x;
1130 androidRect.top = y + height;
1131 androidRect.right = x + width;
1132 androidRect.bottom = y;
1133 androidRects.push_back(androidRect);
1134 }
1135 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1136 androidRects.size());
1137
1138 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1139 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1140 rects, n_rects);
1141 } else {
1142 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1143 }
1144}
1145
1146EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1147{
1148 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001149}
1150
1151EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1152 NativePixmapType target)
1153{
1154 clearError();
1155
Jesse Hallb29e5e82012-04-04 16:53:42 -07001156 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001157 if (!dp) return EGL_FALSE;
1158
Jesse Hallb29e5e82012-04-04 16:53:42 -07001159 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001160 if (!_s.get())
1161 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001162
Mathias Agopian518ec112011-05-13 16:21:08 -07001163 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001164 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001165}
1166
1167const char* eglQueryString(EGLDisplay dpy, EGLint name)
1168{
1169 clearError();
1170
Chia-I Wue57d1352016-08-15 16:10:02 +08001171 // Generate an error quietly when client extensions (as defined by
1172 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1173 // validate_display to generate the error as validate_display would log
1174 // the error, which can be misleading.
1175 //
1176 // If we want to support EGL_EXT_client_extensions later, we can return
1177 // the client extension string here instead.
1178 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1179 return setErrorQuiet(EGL_BAD_DISPLAY, nullptr);
1180
Jesse Hallb29e5e82012-04-04 16:53:42 -07001181 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001182 if (!dp) return (const char *) NULL;
1183
1184 switch (name) {
1185 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001186 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001187 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001188 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001189 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001190 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001191 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001192 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001193 }
1194 return setError(EGL_BAD_PARAMETER, (const char *)0);
1195}
1196
Mathias Agopianca088332013-03-28 17:44:13 -07001197EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1198{
1199 clearError();
1200
1201 const egl_display_ptr dp = validate_display(dpy);
1202 if (!dp) return (const char *) NULL;
1203
1204 switch (name) {
1205 case EGL_VENDOR:
1206 return dp->disp.queryString.vendor;
1207 case EGL_VERSION:
1208 return dp->disp.queryString.version;
1209 case EGL_EXTENSIONS:
1210 return dp->disp.queryString.extensions;
1211 case EGL_CLIENT_APIS:
1212 return dp->disp.queryString.clientApi;
1213 }
1214 return setError(EGL_BAD_PARAMETER, (const char *)0);
1215}
Mathias Agopian518ec112011-05-13 16:21:08 -07001216
1217// ----------------------------------------------------------------------------
1218// EGL 1.1
1219// ----------------------------------------------------------------------------
1220
1221EGLBoolean eglSurfaceAttrib(
1222 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1223{
1224 clearError();
1225
Jesse Hallb29e5e82012-04-04 16:53:42 -07001226 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001227 if (!dp) return EGL_FALSE;
1228
Jesse Hallb29e5e82012-04-04 16:53:42 -07001229 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001230 if (!_s.get())
1231 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001232
Pablo Ceballosc18be292016-05-31 14:55:42 -07001233 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001234
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001235 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001236 if (!s->win.get()) {
1237 setError(EGL_BAD_SURFACE, EGL_FALSE);
1238 }
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001239 int err = native_window_set_auto_refresh(s->win.get(),
1240 value ? true : false);
1241 return (err == NO_ERROR) ? EGL_TRUE :
1242 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001243 }
1244
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001245#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001246 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001247 if (!s->win.get()) {
1248 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1249 }
Brian Anderson069b3652016-07-22 10:32:47 -07001250 int err = native_window_enable_frame_timestamps(
1251 s->win.get(), value ? true : false);
1252 return (err == NO_ERROR) ? EGL_TRUE :
1253 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001254 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001255#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001256
Mathias Agopian518ec112011-05-13 16:21:08 -07001257 if (s->cnx->egl.eglSurfaceAttrib) {
1258 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001259 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001260 }
1261 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1262}
1263
1264EGLBoolean eglBindTexImage(
1265 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1266{
1267 clearError();
1268
Jesse Hallb29e5e82012-04-04 16:53:42 -07001269 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001270 if (!dp) return EGL_FALSE;
1271
Jesse Hallb29e5e82012-04-04 16:53:42 -07001272 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001273 if (!_s.get())
1274 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001275
Mathias Agopian518ec112011-05-13 16:21:08 -07001276 egl_surface_t const * const s = get_surface(surface);
1277 if (s->cnx->egl.eglBindTexImage) {
1278 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001279 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001280 }
1281 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1282}
1283
1284EGLBoolean eglReleaseTexImage(
1285 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1286{
1287 clearError();
1288
Jesse Hallb29e5e82012-04-04 16:53:42 -07001289 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001290 if (!dp) return EGL_FALSE;
1291
Jesse Hallb29e5e82012-04-04 16:53:42 -07001292 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001293 if (!_s.get())
1294 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001295
Mathias Agopian518ec112011-05-13 16:21:08 -07001296 egl_surface_t const * const s = get_surface(surface);
1297 if (s->cnx->egl.eglReleaseTexImage) {
1298 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001299 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001300 }
1301 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1302}
1303
1304EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1305{
1306 clearError();
1307
Jesse Hallb29e5e82012-04-04 16:53:42 -07001308 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001309 if (!dp) return EGL_FALSE;
1310
1311 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001312 egl_connection_t* const cnx = &gEGLImpl;
1313 if (cnx->dso && cnx->egl.eglSwapInterval) {
1314 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001315 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001316
Mathias Agopian518ec112011-05-13 16:21:08 -07001317 return res;
1318}
1319
1320
1321// ----------------------------------------------------------------------------
1322// EGL 1.2
1323// ----------------------------------------------------------------------------
1324
1325EGLBoolean eglWaitClient(void)
1326{
1327 clearError();
1328
Mathias Agopianada798b2012-02-13 17:09:30 -08001329 egl_connection_t* const cnx = &gEGLImpl;
1330 if (!cnx->dso)
1331 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1332
1333 EGLBoolean res;
1334 if (cnx->egl.eglWaitClient) {
1335 res = cnx->egl.eglWaitClient();
1336 } else {
1337 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001338 }
1339 return res;
1340}
1341
1342EGLBoolean eglBindAPI(EGLenum api)
1343{
1344 clearError();
1345
1346 if (egl_init_drivers() == EGL_FALSE) {
1347 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1348 }
1349
1350 // bind this API on all EGLs
1351 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001352 egl_connection_t* const cnx = &gEGLImpl;
1353 if (cnx->dso && cnx->egl.eglBindAPI) {
1354 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001355 }
1356 return res;
1357}
1358
1359EGLenum eglQueryAPI(void)
1360{
1361 clearError();
1362
1363 if (egl_init_drivers() == EGL_FALSE) {
1364 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1365 }
1366
Mathias Agopianada798b2012-02-13 17:09:30 -08001367 egl_connection_t* const cnx = &gEGLImpl;
1368 if (cnx->dso && cnx->egl.eglQueryAPI) {
1369 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001370 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001371
Mathias Agopian518ec112011-05-13 16:21:08 -07001372 // or, it can only be OpenGL ES
1373 return EGL_OPENGL_ES_API;
1374}
1375
1376EGLBoolean eglReleaseThread(void)
1377{
1378 clearError();
1379
Mathias Agopianada798b2012-02-13 17:09:30 -08001380 egl_connection_t* const cnx = &gEGLImpl;
1381 if (cnx->dso && cnx->egl.eglReleaseThread) {
1382 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001383 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301384
1385 // If there is context bound to the thread, release it
1386 egl_display_t::loseCurrent(get_context(getContext()));
1387
Mathias Agopian518ec112011-05-13 16:21:08 -07001388 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001389 return EGL_TRUE;
1390}
1391
1392EGLSurface eglCreatePbufferFromClientBuffer(
1393 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1394 EGLConfig config, const EGLint *attrib_list)
1395{
1396 clearError();
1397
Jesse Hallb29e5e82012-04-04 16:53:42 -07001398 egl_connection_t* cnx = NULL;
1399 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1400 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001401 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1402 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001403 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001404 }
1405 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1406}
1407
1408// ----------------------------------------------------------------------------
1409// EGL_EGLEXT_VERSION 3
1410// ----------------------------------------------------------------------------
1411
1412EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1413 const EGLint *attrib_list)
1414{
1415 clearError();
1416
Jesse Hallb29e5e82012-04-04 16:53:42 -07001417 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001418 if (!dp) return EGL_FALSE;
1419
Jesse Hallb29e5e82012-04-04 16:53:42 -07001420 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001421 if (!_s.get())
1422 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001423
1424 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 if (s->cnx->egl.eglLockSurfaceKHR) {
1426 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001427 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001428 }
1429 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1430}
1431
1432EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1433{
1434 clearError();
1435
Jesse Hallb29e5e82012-04-04 16:53:42 -07001436 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001437 if (!dp) return EGL_FALSE;
1438
Jesse Hallb29e5e82012-04-04 16:53:42 -07001439 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001440 if (!_s.get())
1441 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001442
1443 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001444 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001445 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001446 }
1447 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1448}
1449
1450EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1451 EGLClientBuffer buffer, const EGLint *attrib_list)
1452{
1453 clearError();
1454
Jesse Hallb29e5e82012-04-04 16:53:42 -07001455 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001456 if (!dp) return EGL_NO_IMAGE_KHR;
1457
Jesse Hallb29e5e82012-04-04 16:53:42 -07001458 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001459 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001460
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001461 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1462 egl_connection_t* const cnx = &gEGLImpl;
1463 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1464 result = cnx->egl.eglCreateImageKHR(
1465 dp->disp.dpy,
1466 c ? c->context : EGL_NO_CONTEXT,
1467 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001468 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001469 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001470}
1471
1472EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1473{
1474 clearError();
1475
Jesse Hallb29e5e82012-04-04 16:53:42 -07001476 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001477 if (!dp) return EGL_FALSE;
1478
Steven Holte646a5c52012-06-04 20:02:11 -07001479 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001480 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001481 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001482 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001483 }
Steven Holte646a5c52012-06-04 20:02:11 -07001484 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001485}
1486
1487// ----------------------------------------------------------------------------
1488// EGL_EGLEXT_VERSION 5
1489// ----------------------------------------------------------------------------
1490
1491
1492EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1493{
1494 clearError();
1495
Jesse Hallb29e5e82012-04-04 16:53:42 -07001496 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001497 if (!dp) return EGL_NO_SYNC_KHR;
1498
Mathias Agopian518ec112011-05-13 16:21:08 -07001499 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001500 egl_connection_t* const cnx = &gEGLImpl;
1501 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1502 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001504 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001505}
1506
1507EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1508{
1509 clearError();
1510
Jesse Hallb29e5e82012-04-04 16:53:42 -07001511 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001512 if (!dp) return EGL_FALSE;
1513
Mathias Agopian518ec112011-05-13 16:21:08 -07001514 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001515 egl_connection_t* const cnx = &gEGLImpl;
1516 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1517 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001518 }
1519 return result;
1520}
1521
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001522EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1523 clearError();
1524
1525 const egl_display_ptr dp = validate_display(dpy);
1526 if (!dp) return EGL_FALSE;
1527
1528 EGLBoolean result = EGL_FALSE;
1529 egl_connection_t* const cnx = &gEGLImpl;
1530 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1531 result = cnx->egl.eglSignalSyncKHR(
1532 dp->disp.dpy, sync, mode);
1533 }
1534 return result;
1535}
1536
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001537EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1538 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001539{
1540 clearError();
1541
Jesse Hallb29e5e82012-04-04 16:53:42 -07001542 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001543 if (!dp) return EGL_FALSE;
1544
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001545 EGLBoolean result = EGL_FALSE;
1546 egl_connection_t* const cnx = &gEGLImpl;
1547 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1548 result = cnx->egl.eglClientWaitSyncKHR(
1549 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001550 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001551 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001552}
1553
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001554EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1555 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001556{
1557 clearError();
1558
Jesse Hallb29e5e82012-04-04 16:53:42 -07001559 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001560 if (!dp) return EGL_FALSE;
1561
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001562 EGLBoolean result = EGL_FALSE;
1563 egl_connection_t* const cnx = &gEGLImpl;
1564 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1565 result = cnx->egl.eglGetSyncAttribKHR(
1566 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001567 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001568 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001569}
1570
Season Li000d88f2015-07-01 11:39:40 -07001571EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1572{
1573 clearError();
1574
1575 const egl_display_ptr dp = validate_display(dpy);
1576 if (!dp) return EGL_NO_STREAM_KHR;
1577
1578 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1579 egl_connection_t* const cnx = &gEGLImpl;
1580 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1581 result = cnx->egl.eglCreateStreamKHR(
1582 dp->disp.dpy, attrib_list);
1583 }
1584 return result;
1585}
1586
1587EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1588{
1589 clearError();
1590
1591 const egl_display_ptr dp = validate_display(dpy);
1592 if (!dp) return EGL_FALSE;
1593
1594 EGLBoolean result = EGL_FALSE;
1595 egl_connection_t* const cnx = &gEGLImpl;
1596 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1597 result = cnx->egl.eglDestroyStreamKHR(
1598 dp->disp.dpy, stream);
1599 }
1600 return result;
1601}
1602
1603EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1604 EGLenum attribute, EGLint value)
1605{
1606 clearError();
1607
1608 const egl_display_ptr dp = validate_display(dpy);
1609 if (!dp) return EGL_FALSE;
1610
1611 EGLBoolean result = EGL_FALSE;
1612 egl_connection_t* const cnx = &gEGLImpl;
1613 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1614 result = cnx->egl.eglStreamAttribKHR(
1615 dp->disp.dpy, stream, attribute, value);
1616 }
1617 return result;
1618}
1619
1620EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1621 EGLenum attribute, EGLint *value)
1622{
1623 clearError();
1624
1625 const egl_display_ptr dp = validate_display(dpy);
1626 if (!dp) return EGL_FALSE;
1627
1628 EGLBoolean result = EGL_FALSE;
1629 egl_connection_t* const cnx = &gEGLImpl;
1630 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1631 result = cnx->egl.eglQueryStreamKHR(
1632 dp->disp.dpy, stream, attribute, value);
1633 }
1634 return result;
1635}
1636
1637EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1638 EGLenum attribute, EGLuint64KHR *value)
1639{
1640 clearError();
1641
1642 const egl_display_ptr dp = validate_display(dpy);
1643 if (!dp) return EGL_FALSE;
1644
1645 EGLBoolean result = EGL_FALSE;
1646 egl_connection_t* const cnx = &gEGLImpl;
1647 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1648 result = cnx->egl.eglQueryStreamu64KHR(
1649 dp->disp.dpy, stream, attribute, value);
1650 }
1651 return result;
1652}
1653
1654EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1655 EGLenum attribute, EGLTimeKHR *value)
1656{
1657 clearError();
1658
1659 const egl_display_ptr dp = validate_display(dpy);
1660 if (!dp) return EGL_FALSE;
1661
1662 EGLBoolean result = EGL_FALSE;
1663 egl_connection_t* const cnx = &gEGLImpl;
1664 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1665 result = cnx->egl.eglQueryStreamTimeKHR(
1666 dp->disp.dpy, stream, attribute, value);
1667 }
1668 return result;
1669}
1670
1671EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1672 EGLStreamKHR stream, const EGLint *attrib_list)
1673{
1674 clearError();
1675
1676 egl_display_ptr dp = validate_display(dpy);
1677 if (!dp) return EGL_NO_SURFACE;
1678
1679 egl_connection_t* const cnx = &gEGLImpl;
1680 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1681 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1682 dp->disp.dpy, config, stream, attrib_list);
1683 if (surface != EGL_NO_SURFACE) {
1684 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1685 surface, cnx);
1686 return s;
1687 }
1688 }
1689 return EGL_NO_SURFACE;
1690}
1691
1692EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1693 EGLStreamKHR stream)
1694{
1695 clearError();
1696
1697 const egl_display_ptr dp = validate_display(dpy);
1698 if (!dp) return EGL_FALSE;
1699
1700 EGLBoolean result = EGL_FALSE;
1701 egl_connection_t* const cnx = &gEGLImpl;
1702 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1703 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1704 dp->disp.dpy, stream);
1705 }
1706 return result;
1707}
1708
1709EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1710 EGLStreamKHR stream)
1711{
1712 clearError();
1713
1714 const egl_display_ptr dp = validate_display(dpy);
1715 if (!dp) return EGL_FALSE;
1716
1717 EGLBoolean result = EGL_FALSE;
1718 egl_connection_t* const cnx = &gEGLImpl;
1719 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1720 result = cnx->egl.eglStreamConsumerAcquireKHR(
1721 dp->disp.dpy, stream);
1722 }
1723 return result;
1724}
1725
1726EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1727 EGLStreamKHR stream)
1728{
1729 clearError();
1730
1731 const egl_display_ptr dp = validate_display(dpy);
1732 if (!dp) return EGL_FALSE;
1733
1734 EGLBoolean result = EGL_FALSE;
1735 egl_connection_t* const cnx = &gEGLImpl;
1736 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1737 result = cnx->egl.eglStreamConsumerReleaseKHR(
1738 dp->disp.dpy, stream);
1739 }
1740 return result;
1741}
1742
1743EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1744 EGLDisplay dpy, EGLStreamKHR stream)
1745{
1746 clearError();
1747
1748 const egl_display_ptr dp = validate_display(dpy);
1749 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1750
1751 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1752 egl_connection_t* const cnx = &gEGLImpl;
1753 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1754 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1755 dp->disp.dpy, stream);
1756 }
1757 return result;
1758}
1759
1760EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1761 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1762{
1763 clearError();
1764
1765 const egl_display_ptr dp = validate_display(dpy);
1766 if (!dp) return EGL_NO_STREAM_KHR;
1767
1768 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1769 egl_connection_t* const cnx = &gEGLImpl;
1770 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1771 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1772 dp->disp.dpy, file_descriptor);
1773 }
1774 return result;
1775}
1776
Mathias Agopian518ec112011-05-13 16:21:08 -07001777// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001778// EGL_EGLEXT_VERSION 15
1779// ----------------------------------------------------------------------------
1780
1781EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1782 clearError();
1783 const egl_display_ptr dp = validate_display(dpy);
1784 if (!dp) return EGL_FALSE;
1785 EGLint result = EGL_FALSE;
1786 egl_connection_t* const cnx = &gEGLImpl;
1787 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1788 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1789 }
1790 return result;
1791}
1792
1793// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001794// ANDROID extensions
1795// ----------------------------------------------------------------------------
1796
Jamie Gennis331841b2012-09-06 14:52:00 -07001797EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1798{
1799 clearError();
1800
1801 const egl_display_ptr dp = validate_display(dpy);
1802 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1803
1804 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1805 egl_connection_t* const cnx = &gEGLImpl;
1806 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1807 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1808 }
1809 return result;
1810}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001811
Andy McFadden72841452013-03-01 16:25:32 -08001812EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1813 EGLnsecsANDROID time)
1814{
1815 clearError();
1816
1817 const egl_display_ptr dp = validate_display(dpy);
1818 if (!dp) {
1819 return EGL_FALSE;
1820 }
1821
1822 SurfaceRef _s(dp.get(), surface);
1823 if (!_s.get()) {
1824 setError(EGL_BAD_SURFACE, EGL_FALSE);
1825 return EGL_FALSE;
1826 }
1827
1828 egl_surface_t const * const s = get_surface(surface);
1829 native_window_set_buffers_timestamp(s->win.get(), time);
1830
1831 return EGL_TRUE;
1832}
1833
Craig Donner05249fc2016-01-15 19:33:55 -08001834EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1835{
1836 clearError();
1837
1838 int usage = 0;
1839 uint32_t width = 0;
1840 uint32_t height = 0;
1841 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001842 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001843 uint32_t red_size = 0;
1844 uint32_t green_size = 0;
1845 uint32_t blue_size = 0;
1846 uint32_t alpha_size = 0;
1847
Craig Donnerb40504a2016-06-03 17:54:25 -07001848#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001849 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001850 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001851 target = value; \
1852 } else { \
1853 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1854 } \
1855 break
1856
1857 if (attrib_list) {
1858 while (*attrib_list != EGL_NONE) {
1859 GLint attr = *attrib_list++;
1860 GLint value = *attrib_list++;
1861 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001862 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1863 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1864 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1865 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1866 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1867 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001868 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001869 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1870 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1871 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001872 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001873 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001874 usage |= GRALLOC_USAGE_HW_RENDER;
1875 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001876 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001877 usage |= GRALLOC_USAGE_HW_TEXTURE;
1878 }
Craig Donner05249fc2016-01-15 19:33:55 -08001879 break;
1880 default:
1881 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1882 }
1883 }
1884 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001885#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001886
1887 // Validate format.
1888 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1889 if (alpha_size == 8) {
1890 format = HAL_PIXEL_FORMAT_RGBA_8888;
1891 } else {
1892 format = HAL_PIXEL_FORMAT_RGB_888;
1893 }
1894 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1895 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001896 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001897 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001898 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001899 red_size, green_size, blue_size, alpha_size);
1900 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1901 }
1902
Craig Donner68671532016-07-18 10:19:54 -07001903#define CHECK_ERROR_CONDITION(message) \
1904 if (err != NO_ERROR) { \
1905 ALOGE(message); \
1906 goto error_condition; \
1907 }
1908
1909 // The holder is used to destroy the buffer if an error occurs.
1910 GraphicBuffer* gBuffer = new GraphicBuffer();
1911 sp<IServiceManager> sm = defaultServiceManager();
1912 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1913 sp<IBinder> allocator;
1914 Parcel sc_data, sc_reply, data, reply;
1915 status_t err = NO_ERROR;
1916 if (sm == NULL) {
1917 ALOGE("Unable to connect to ServiceManager");
1918 goto error_condition;
1919 }
1920
1921 // Obtain an allocator.
1922 if (surfaceFlinger == NULL) {
1923 ALOGE("Unable to connect to SurfaceFlinger");
1924 goto error_condition;
1925 }
1926 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1927 err = surfaceFlinger->transact(
1928 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1929 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1930 allocator = sc_reply.readStrongBinder();
1931
1932 if (allocator == NULL) {
1933 ALOGE("Unable to obtain an ISurfaceComposer");
1934 goto error_condition;
1935 }
1936 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1937 err = data.writeUint32(width);
1938 CHECK_ERROR_CONDITION("Unable to write width");
1939 err = data.writeUint32(height);
1940 CHECK_ERROR_CONDITION("Unable to write height");
1941 err = data.writeInt32(static_cast<int32_t>(format));
1942 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001943 err = data.writeUint32(layer_count);
1944 CHECK_ERROR_CONDITION("Unable to write layer count");
1945 err = data.writeUint32(usage);
Craig Donner68671532016-07-18 10:19:54 -07001946 CHECK_ERROR_CONDITION("Unable to write usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001947 err = data.writeUtf8AsUtf16(
1948 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1949 std::to_string(getpid()) + ']');
1950 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001951 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1952 &reply);
1953 CHECK_ERROR_CONDITION(
1954 "Unable to request buffer allocation from surface composer");
1955 err = reply.readInt32();
1956 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1957 err = reply.read(*gBuffer);
1958 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1959
1960 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001961 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001962 ALOGE("Unable to create native buffer "
1963 "{ w=%u, h=%u, f=%u, u=%#x, lc=%u}: %#x", width, height, format,
1964 usage, layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001965 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001966 }
Craig Donner6ebc46a2016-10-21 15:23:44 -07001967 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, u=%#x, lc=%u}",
1968 gBuffer, width, height, format, usage, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001969 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001970
1971#undef CHECK_ERROR_CONDITION
1972
1973error_condition:
1974 // Delete the buffer.
1975 sp<GraphicBuffer> holder(gBuffer);
1976 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001977}
1978
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001979// ----------------------------------------------------------------------------
1980// NVIDIA extensions
1981// ----------------------------------------------------------------------------
1982EGLuint64NV eglGetSystemTimeFrequencyNV()
1983{
1984 clearError();
1985
1986 if (egl_init_drivers() == EGL_FALSE) {
1987 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1988 }
1989
1990 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001991 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001992
Mathias Agopianada798b2012-02-13 17:09:30 -08001993 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1994 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001995 }
1996
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001997 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001998}
1999
2000EGLuint64NV eglGetSystemTimeNV()
2001{
2002 clearError();
2003
2004 if (egl_init_drivers() == EGL_FALSE) {
2005 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2006 }
2007
2008 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002009 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002010
Mathias Agopianada798b2012-02-13 17:09:30 -08002011 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2012 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002013 }
2014
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07002015 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002016}
Dan Stozaa894d082015-02-19 15:27:36 -08002017
2018// ----------------------------------------------------------------------------
2019// Partial update extension
2020// ----------------------------------------------------------------------------
2021EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2022 EGLint *rects, EGLint n_rects)
2023{
2024 clearError();
2025
2026 const egl_display_ptr dp = validate_display(dpy);
2027 if (!dp) {
2028 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2029 return EGL_FALSE;
2030 }
2031
2032 SurfaceRef _s(dp.get(), surface);
2033 if (!_s.get()) {
2034 setError(EGL_BAD_SURFACE, EGL_FALSE);
2035 return EGL_FALSE;
2036 }
2037
2038 egl_surface_t const * const s = get_surface(surface);
2039 if (s->cnx->egl.eglSetDamageRegionKHR) {
2040 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2041 rects, n_rects);
2042 }
2043
2044 return EGL_FALSE;
2045}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002046
Brian Anderson1049d1d2016-12-16 17:25:57 -08002047EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2048 EGLuint64KHR *frameId) {
2049 clearError();
2050
2051 const egl_display_ptr dp = validate_display(dpy);
2052 if (!dp) {
2053 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2054 }
2055
2056 SurfaceRef _s(dp.get(), surface);
2057 if (!_s.get()) {
2058 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2059 }
2060
2061 egl_surface_t const * const s = get_surface(surface);
2062
2063 if (!s->win.get()) {
2064 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2065 }
2066
2067 uint64_t nextFrameId = 0;
2068 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2069
2070 if (ret != NO_ERROR) {
2071 // This should not happen. Return an error that is not in the spec
2072 // so it's obvious something is very wrong.
2073 ALOGE("eglGetNextFrameId: Unexpected error.");
2074 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
2075 }
2076
2077 *frameId = nextFrameId;
2078 return EGL_TRUE;
2079}
2080
Pablo Ceballosc18be292016-05-31 14:55:42 -07002081EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002082 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002083 EGLnsecsANDROID *values)
2084{
2085 clearError();
2086
2087 const egl_display_ptr dp = validate_display(dpy);
2088 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002089 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002090 }
2091
2092 SurfaceRef _s(dp.get(), surface);
2093 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002094 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002095 }
2096
2097 egl_surface_t const * const s = get_surface(surface);
2098
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002099 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002100 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002101 }
2102
Brian Andersondbd0ea82016-07-22 09:38:59 -07002103 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002104 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002105 nsecs_t* latchTime = nullptr;
2106 nsecs_t* firstRefreshStartTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002107 nsecs_t* GLCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002108 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002109 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002110 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002111 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002112 nsecs_t* releaseTime = nullptr;
2113
2114 for (int i = 0; i < numTimestamps; i++) {
2115 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002116 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2117 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002118 break;
2119 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2120 acquireTime = &values[i];
2121 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002122 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2123 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002124 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002125 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2126 firstRefreshStartTime = &values[i];
2127 break;
2128 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2129 lastRefreshStartTime = &values[i];
2130 break;
2131 case EGL_FIRST_COMPOSITION_FINISHED_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002132 GLCompositionDoneTime = &values[i];
2133 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002134 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2135 displayPresentTime = &values[i];
2136 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002137 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2138 displayRetireTime = &values[i];
2139 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002140 case EGL_DEQUEUE_READY_TIME_ANDROID:
2141 dequeueReadyTime = &values[i];
2142 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002143 case EGL_READS_DONE_TIME_ANDROID:
2144 releaseTime = &values[i];
2145 break;
2146 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002147 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002148 }
2149 }
2150
Brian Anderson1049d1d2016-12-16 17:25:57 -08002151 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002152 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
2153 lastRefreshStartTime, GLCompositionDoneTime, displayPresentTime,
2154 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002155
Brian Anderson069b3652016-07-22 10:32:47 -07002156 switch (ret) {
2157 case NO_ERROR:
2158 return EGL_TRUE;
2159 case NAME_NOT_FOUND:
2160 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002161 case INVALID_OPERATION:
2162 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002163 case BAD_VALUE:
2164 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2165 default:
2166 // This should not happen. Return an error that is not in the spec
2167 // so it's obvious something is very wrong.
Brian Anderson1049d1d2016-12-16 17:25:57 -08002168 ALOGE("eglGetFrameTimestamps: Unexpected error.");
Brian Anderson069b3652016-07-22 10:32:47 -07002169 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002170 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002171}
2172
2173EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2174 EGLint timestamp)
2175{
2176 clearError();
2177
2178 const egl_display_ptr dp = validate_display(dpy);
2179 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002180 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002181 }
2182
2183 SurfaceRef _s(dp.get(), surface);
2184 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002185 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2186 }
2187
2188 egl_surface_t const * const s = get_surface(surface);
2189
2190 ANativeWindow* window = s->win.get();
2191 if (!window) {
2192 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002193 }
2194
2195 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002196#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Brian Andersondbd0ea82016-07-22 09:38:59 -07002197 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002198 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002199 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2200 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2201 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2202 case EGL_FIRST_COMPOSITION_FINISHED_TIME_ANDROID:
2203 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002204 case EGL_READS_DONE_TIME_ANDROID:
2205 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002206 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2207 int value = 0;
2208 window->query(window,
2209 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2210 return value == 0 ? EGL_FALSE : EGL_TRUE;
2211 }
2212 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2213 int value = 0;
2214 window->query(window,
2215 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2216 return value == 0 ? EGL_FALSE : EGL_TRUE;
2217 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002218#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002219 default:
2220 return EGL_FALSE;
2221 }
2222}