blob: 32818f0f93e2e21661b17bef55a87d6a285f978b [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
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025#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
59// ----------------------------------------------------------------------------
60
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070061namespace android {
62
Mathias Agopian518ec112011-05-13 16:21:08 -070063struct extention_map_t {
64 const char* name;
65 __eglMustCastToProperFunctionPointerType address;
66};
67
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070068/*
Jesse Hall21558da2013-08-06 15:31:22 -070069 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070070 *
Jesse Hall21558da2013-08-06 15:31:22 -070071 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
72 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070073 *
Jesse Hall21558da2013-08-06 15:31:22 -070074 * The rest (gExtensionString) depend on support in the EGL driver, and are
75 * only available if the driver supports them. However, some of these must be
76 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080077 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070078 * the mandatory extensions are present and may not function properly if some
79 * are missing.
80 *
81 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070082 */
Jesse Hall21558da2013-08-06 15:31:22 -070083extern char const * const gBuiltinExtensionString =
84 "EGL_KHR_get_all_proc_addresses "
85 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080086 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080087 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080088 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070089 "EGL_ANDROID_get_frame_timestamps "
Jesse Hall21558da2013-08-06 15:31:22 -070090 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070091extern char const * const gExtensionString =
92 "EGL_KHR_image " // mandatory
93 "EGL_KHR_image_base " // mandatory
94 "EGL_KHR_image_pixmap "
95 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -070096 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070098 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070099 "EGL_KHR_gl_texture_cubemap_image "
100 "EGL_KHR_gl_renderbuffer_image "
101 "EGL_KHR_reusable_sync "
102 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700103 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700104 "EGL_KHR_config_attribs "
105 "EGL_KHR_surfaceless_context "
106 "EGL_KHR_stream "
107 "EGL_KHR_stream_fifo "
108 "EGL_KHR_stream_producer_eglsurface "
109 "EGL_KHR_stream_consumer_gltexture "
110 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700111 "EGL_EXT_create_context_robustness "
112 "EGL_NV_system_time "
113 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700114 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700115 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800116 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700117 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800118 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700119 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700120 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700121 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700122 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000123 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700124 ;
125
126// extensions not exposed to applications but used by the ANDROID system
127// "EGL_ANDROID_blob_cache " // strongly recommended
128// "EGL_IMG_hibernate_process " // optional
129// "EGL_ANDROID_native_fence_sync " // strongly recommended
130// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700131// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700132
133/*
134 * EGL Extensions entry-points exposed to 3rd party applications
135 * (keep in sync with gExtensionString above)
136 *
137 */
138static const extention_map_t sExtensionMap[] = {
139 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700140 { "eglLockSurfaceKHR",
141 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
142 { "eglUnlockSurfaceKHR",
143 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700144
145 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700146 { "eglCreateImageKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
148 { "eglDestroyImageKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700150
151 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
152 { "eglCreateSyncKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
154 { "eglDestroySyncKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
156 { "eglClientWaitSyncKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
158 { "eglSignalSyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
160 { "eglGetSyncAttribKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
162
163 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800164 { "eglGetSystemTimeFrequencyNV",
165 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
166 { "eglGetSystemTimeNV",
167 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700168
Mathias Agopian2bb71682013-03-27 17:32:41 -0700169 // EGL_KHR_wait_sync
170 { "eglWaitSyncKHR",
171 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700172
173 // EGL_ANDROID_presentation_time
174 { "eglPresentationTimeANDROID",
175 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800176
177 // EGL_KHR_swap_buffers_with_damage
178 { "eglSwapBuffersWithDamageKHR",
179 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
180
Craig Donner05249fc2016-01-15 19:33:55 -0800181 // EGL_ANDROID_native_client_buffer
182 { "eglCreateNativeClientBufferANDROID",
183 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
184
Dan Stozaa894d082015-02-19 15:27:36 -0800185 // EGL_KHR_partial_update
186 { "eglSetDamageRegionKHR",
187 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700188
189 { "eglCreateStreamKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
191 { "eglDestroyStreamKHR",
192 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
193 { "eglStreamAttribKHR",
194 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
195 { "eglQueryStreamKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
197 { "eglQueryStreamu64KHR",
198 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
199 { "eglQueryStreamTimeKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
201 { "eglCreateStreamProducerSurfaceKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
203 { "eglStreamConsumerGLTextureExternalKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
205 { "eglStreamConsumerAcquireKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
207 { "eglStreamConsumerReleaseKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
209 { "eglGetStreamFileDescriptorKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
211 { "eglCreateStreamFromFileDescriptorKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700213
214 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800215 { "eglGetNextFrameIdANDROID",
216 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800217 { "eglGetCompositorTimingANDROID",
218 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
219 { "eglGetCompositorTimingSupportedANDROID",
220 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700221 { "eglGetFrameTimestampsANDROID",
222 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800223 { "eglGetFrameTimestampSupportedANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
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 Ceballosc18be292016-05-31 14:55:42 -07001245 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001246 if (!s->win.get()) {
1247 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1248 }
Brian Anderson069b3652016-07-22 10:32:47 -07001249 int err = native_window_enable_frame_timestamps(
1250 s->win.get(), value ? true : false);
1251 return (err == NO_ERROR) ? EGL_TRUE :
1252 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001253 }
1254
Mathias Agopian518ec112011-05-13 16:21:08 -07001255 if (s->cnx->egl.eglSurfaceAttrib) {
1256 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001257 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001258 }
1259 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1260}
1261
1262EGLBoolean eglBindTexImage(
1263 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1264{
1265 clearError();
1266
Jesse Hallb29e5e82012-04-04 16:53:42 -07001267 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001268 if (!dp) return EGL_FALSE;
1269
Jesse Hallb29e5e82012-04-04 16:53:42 -07001270 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001271 if (!_s.get())
1272 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001273
Mathias Agopian518ec112011-05-13 16:21:08 -07001274 egl_surface_t const * const s = get_surface(surface);
1275 if (s->cnx->egl.eglBindTexImage) {
1276 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001277 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 }
1279 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1280}
1281
1282EGLBoolean eglReleaseTexImage(
1283 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1284{
1285 clearError();
1286
Jesse Hallb29e5e82012-04-04 16:53:42 -07001287 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001288 if (!dp) return EGL_FALSE;
1289
Jesse Hallb29e5e82012-04-04 16:53:42 -07001290 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001291 if (!_s.get())
1292 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001293
Mathias Agopian518ec112011-05-13 16:21:08 -07001294 egl_surface_t const * const s = get_surface(surface);
1295 if (s->cnx->egl.eglReleaseTexImage) {
1296 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001297 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001298 }
1299 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1300}
1301
1302EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1303{
1304 clearError();
1305
Jesse Hallb29e5e82012-04-04 16:53:42 -07001306 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001307 if (!dp) return EGL_FALSE;
1308
1309 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001310 egl_connection_t* const cnx = &gEGLImpl;
1311 if (cnx->dso && cnx->egl.eglSwapInterval) {
1312 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001313 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001314
Mathias Agopian518ec112011-05-13 16:21:08 -07001315 return res;
1316}
1317
1318
1319// ----------------------------------------------------------------------------
1320// EGL 1.2
1321// ----------------------------------------------------------------------------
1322
1323EGLBoolean eglWaitClient(void)
1324{
1325 clearError();
1326
Mathias Agopianada798b2012-02-13 17:09:30 -08001327 egl_connection_t* const cnx = &gEGLImpl;
1328 if (!cnx->dso)
1329 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1330
1331 EGLBoolean res;
1332 if (cnx->egl.eglWaitClient) {
1333 res = cnx->egl.eglWaitClient();
1334 } else {
1335 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001336 }
1337 return res;
1338}
1339
1340EGLBoolean eglBindAPI(EGLenum api)
1341{
1342 clearError();
1343
1344 if (egl_init_drivers() == EGL_FALSE) {
1345 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1346 }
1347
1348 // bind this API on all EGLs
1349 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001350 egl_connection_t* const cnx = &gEGLImpl;
1351 if (cnx->dso && cnx->egl.eglBindAPI) {
1352 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001353 }
1354 return res;
1355}
1356
1357EGLenum eglQueryAPI(void)
1358{
1359 clearError();
1360
1361 if (egl_init_drivers() == EGL_FALSE) {
1362 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1363 }
1364
Mathias Agopianada798b2012-02-13 17:09:30 -08001365 egl_connection_t* const cnx = &gEGLImpl;
1366 if (cnx->dso && cnx->egl.eglQueryAPI) {
1367 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001368 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001369
Mathias Agopian518ec112011-05-13 16:21:08 -07001370 // or, it can only be OpenGL ES
1371 return EGL_OPENGL_ES_API;
1372}
1373
1374EGLBoolean eglReleaseThread(void)
1375{
1376 clearError();
1377
Mathias Agopianada798b2012-02-13 17:09:30 -08001378 egl_connection_t* const cnx = &gEGLImpl;
1379 if (cnx->dso && cnx->egl.eglReleaseThread) {
1380 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001381 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301382
1383 // If there is context bound to the thread, release it
1384 egl_display_t::loseCurrent(get_context(getContext()));
1385
Mathias Agopian518ec112011-05-13 16:21:08 -07001386 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001387 return EGL_TRUE;
1388}
1389
1390EGLSurface eglCreatePbufferFromClientBuffer(
1391 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1392 EGLConfig config, const EGLint *attrib_list)
1393{
1394 clearError();
1395
Jesse Hallb29e5e82012-04-04 16:53:42 -07001396 egl_connection_t* cnx = NULL;
1397 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1398 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001399 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1400 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001401 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001402 }
1403 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1404}
1405
1406// ----------------------------------------------------------------------------
1407// EGL_EGLEXT_VERSION 3
1408// ----------------------------------------------------------------------------
1409
1410EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1411 const EGLint *attrib_list)
1412{
1413 clearError();
1414
Jesse Hallb29e5e82012-04-04 16:53:42 -07001415 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001416 if (!dp) return EGL_FALSE;
1417
Jesse Hallb29e5e82012-04-04 16:53:42 -07001418 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001419 if (!_s.get())
1420 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001421
1422 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001423 if (s->cnx->egl.eglLockSurfaceKHR) {
1424 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001425 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001426 }
1427 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1428}
1429
1430EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1431{
1432 clearError();
1433
Jesse Hallb29e5e82012-04-04 16:53:42 -07001434 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001435 if (!dp) return EGL_FALSE;
1436
Jesse Hallb29e5e82012-04-04 16:53:42 -07001437 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001438 if (!_s.get())
1439 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001440
1441 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001442 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001443 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001444 }
1445 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1446}
1447
1448EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1449 EGLClientBuffer buffer, const EGLint *attrib_list)
1450{
1451 clearError();
1452
Jesse Hallb29e5e82012-04-04 16:53:42 -07001453 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001454 if (!dp) return EGL_NO_IMAGE_KHR;
1455
Jesse Hallb29e5e82012-04-04 16:53:42 -07001456 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001457 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001458
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001459 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1460 egl_connection_t* const cnx = &gEGLImpl;
1461 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1462 result = cnx->egl.eglCreateImageKHR(
1463 dp->disp.dpy,
1464 c ? c->context : EGL_NO_CONTEXT,
1465 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001466 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001467 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001468}
1469
1470EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1471{
1472 clearError();
1473
Jesse Hallb29e5e82012-04-04 16:53:42 -07001474 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001475 if (!dp) return EGL_FALSE;
1476
Steven Holte646a5c52012-06-04 20:02:11 -07001477 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001478 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001479 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001480 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001481 }
Steven Holte646a5c52012-06-04 20:02:11 -07001482 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001483}
1484
1485// ----------------------------------------------------------------------------
1486// EGL_EGLEXT_VERSION 5
1487// ----------------------------------------------------------------------------
1488
1489
1490EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1491{
1492 clearError();
1493
Jesse Hallb29e5e82012-04-04 16:53:42 -07001494 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001495 if (!dp) return EGL_NO_SYNC_KHR;
1496
Mathias Agopian518ec112011-05-13 16:21:08 -07001497 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001498 egl_connection_t* const cnx = &gEGLImpl;
1499 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1500 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001501 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001502 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001503}
1504
1505EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1506{
1507 clearError();
1508
Jesse Hallb29e5e82012-04-04 16:53:42 -07001509 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001510 if (!dp) return EGL_FALSE;
1511
Mathias Agopian518ec112011-05-13 16:21:08 -07001512 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001513 egl_connection_t* const cnx = &gEGLImpl;
1514 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1515 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 }
1517 return result;
1518}
1519
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001520EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1521 clearError();
1522
1523 const egl_display_ptr dp = validate_display(dpy);
1524 if (!dp) return EGL_FALSE;
1525
1526 EGLBoolean result = EGL_FALSE;
1527 egl_connection_t* const cnx = &gEGLImpl;
1528 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1529 result = cnx->egl.eglSignalSyncKHR(
1530 dp->disp.dpy, sync, mode);
1531 }
1532 return result;
1533}
1534
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001535EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1536 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001537{
1538 clearError();
1539
Jesse Hallb29e5e82012-04-04 16:53:42 -07001540 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001541 if (!dp) return EGL_FALSE;
1542
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001543 EGLBoolean result = EGL_FALSE;
1544 egl_connection_t* const cnx = &gEGLImpl;
1545 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1546 result = cnx->egl.eglClientWaitSyncKHR(
1547 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001548 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001549 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001550}
1551
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001552EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1553 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001554{
1555 clearError();
1556
Jesse Hallb29e5e82012-04-04 16:53:42 -07001557 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001558 if (!dp) return EGL_FALSE;
1559
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001560 EGLBoolean result = EGL_FALSE;
1561 egl_connection_t* const cnx = &gEGLImpl;
1562 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1563 result = cnx->egl.eglGetSyncAttribKHR(
1564 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001565 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001566 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001567}
1568
Season Li000d88f2015-07-01 11:39:40 -07001569EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1570{
1571 clearError();
1572
1573 const egl_display_ptr dp = validate_display(dpy);
1574 if (!dp) return EGL_NO_STREAM_KHR;
1575
1576 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1577 egl_connection_t* const cnx = &gEGLImpl;
1578 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1579 result = cnx->egl.eglCreateStreamKHR(
1580 dp->disp.dpy, attrib_list);
1581 }
1582 return result;
1583}
1584
1585EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1586{
1587 clearError();
1588
1589 const egl_display_ptr dp = validate_display(dpy);
1590 if (!dp) return EGL_FALSE;
1591
1592 EGLBoolean result = EGL_FALSE;
1593 egl_connection_t* const cnx = &gEGLImpl;
1594 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1595 result = cnx->egl.eglDestroyStreamKHR(
1596 dp->disp.dpy, stream);
1597 }
1598 return result;
1599}
1600
1601EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1602 EGLenum attribute, EGLint value)
1603{
1604 clearError();
1605
1606 const egl_display_ptr dp = validate_display(dpy);
1607 if (!dp) return EGL_FALSE;
1608
1609 EGLBoolean result = EGL_FALSE;
1610 egl_connection_t* const cnx = &gEGLImpl;
1611 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1612 result = cnx->egl.eglStreamAttribKHR(
1613 dp->disp.dpy, stream, attribute, value);
1614 }
1615 return result;
1616}
1617
1618EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1619 EGLenum attribute, EGLint *value)
1620{
1621 clearError();
1622
1623 const egl_display_ptr dp = validate_display(dpy);
1624 if (!dp) return EGL_FALSE;
1625
1626 EGLBoolean result = EGL_FALSE;
1627 egl_connection_t* const cnx = &gEGLImpl;
1628 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1629 result = cnx->egl.eglQueryStreamKHR(
1630 dp->disp.dpy, stream, attribute, value);
1631 }
1632 return result;
1633}
1634
1635EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1636 EGLenum attribute, EGLuint64KHR *value)
1637{
1638 clearError();
1639
1640 const egl_display_ptr dp = validate_display(dpy);
1641 if (!dp) return EGL_FALSE;
1642
1643 EGLBoolean result = EGL_FALSE;
1644 egl_connection_t* const cnx = &gEGLImpl;
1645 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1646 result = cnx->egl.eglQueryStreamu64KHR(
1647 dp->disp.dpy, stream, attribute, value);
1648 }
1649 return result;
1650}
1651
1652EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1653 EGLenum attribute, EGLTimeKHR *value)
1654{
1655 clearError();
1656
1657 const egl_display_ptr dp = validate_display(dpy);
1658 if (!dp) return EGL_FALSE;
1659
1660 EGLBoolean result = EGL_FALSE;
1661 egl_connection_t* const cnx = &gEGLImpl;
1662 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1663 result = cnx->egl.eglQueryStreamTimeKHR(
1664 dp->disp.dpy, stream, attribute, value);
1665 }
1666 return result;
1667}
1668
1669EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1670 EGLStreamKHR stream, const EGLint *attrib_list)
1671{
1672 clearError();
1673
1674 egl_display_ptr dp = validate_display(dpy);
1675 if (!dp) return EGL_NO_SURFACE;
1676
1677 egl_connection_t* const cnx = &gEGLImpl;
1678 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1679 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1680 dp->disp.dpy, config, stream, attrib_list);
1681 if (surface != EGL_NO_SURFACE) {
1682 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1683 surface, cnx);
1684 return s;
1685 }
1686 }
1687 return EGL_NO_SURFACE;
1688}
1689
1690EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1691 EGLStreamKHR stream)
1692{
1693 clearError();
1694
1695 const egl_display_ptr dp = validate_display(dpy);
1696 if (!dp) return EGL_FALSE;
1697
1698 EGLBoolean result = EGL_FALSE;
1699 egl_connection_t* const cnx = &gEGLImpl;
1700 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1701 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1702 dp->disp.dpy, stream);
1703 }
1704 return result;
1705}
1706
1707EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1708 EGLStreamKHR stream)
1709{
1710 clearError();
1711
1712 const egl_display_ptr dp = validate_display(dpy);
1713 if (!dp) return EGL_FALSE;
1714
1715 EGLBoolean result = EGL_FALSE;
1716 egl_connection_t* const cnx = &gEGLImpl;
1717 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1718 result = cnx->egl.eglStreamConsumerAcquireKHR(
1719 dp->disp.dpy, stream);
1720 }
1721 return result;
1722}
1723
1724EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1725 EGLStreamKHR stream)
1726{
1727 clearError();
1728
1729 const egl_display_ptr dp = validate_display(dpy);
1730 if (!dp) return EGL_FALSE;
1731
1732 EGLBoolean result = EGL_FALSE;
1733 egl_connection_t* const cnx = &gEGLImpl;
1734 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1735 result = cnx->egl.eglStreamConsumerReleaseKHR(
1736 dp->disp.dpy, stream);
1737 }
1738 return result;
1739}
1740
1741EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1742 EGLDisplay dpy, EGLStreamKHR stream)
1743{
1744 clearError();
1745
1746 const egl_display_ptr dp = validate_display(dpy);
1747 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1748
1749 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1750 egl_connection_t* const cnx = &gEGLImpl;
1751 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1752 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1753 dp->disp.dpy, stream);
1754 }
1755 return result;
1756}
1757
1758EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1759 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1760{
1761 clearError();
1762
1763 const egl_display_ptr dp = validate_display(dpy);
1764 if (!dp) return EGL_NO_STREAM_KHR;
1765
1766 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1767 egl_connection_t* const cnx = &gEGLImpl;
1768 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1769 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1770 dp->disp.dpy, file_descriptor);
1771 }
1772 return result;
1773}
1774
Mathias Agopian518ec112011-05-13 16:21:08 -07001775// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001776// EGL_EGLEXT_VERSION 15
1777// ----------------------------------------------------------------------------
1778
1779EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1780 clearError();
1781 const egl_display_ptr dp = validate_display(dpy);
1782 if (!dp) return EGL_FALSE;
1783 EGLint result = EGL_FALSE;
1784 egl_connection_t* const cnx = &gEGLImpl;
1785 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1786 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1787 }
1788 return result;
1789}
1790
1791// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001792// ANDROID extensions
1793// ----------------------------------------------------------------------------
1794
Jamie Gennis331841b2012-09-06 14:52:00 -07001795EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1796{
1797 clearError();
1798
1799 const egl_display_ptr dp = validate_display(dpy);
1800 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1801
1802 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1803 egl_connection_t* const cnx = &gEGLImpl;
1804 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1805 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1806 }
1807 return result;
1808}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001809
Andy McFadden72841452013-03-01 16:25:32 -08001810EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1811 EGLnsecsANDROID time)
1812{
1813 clearError();
1814
1815 const egl_display_ptr dp = validate_display(dpy);
1816 if (!dp) {
1817 return EGL_FALSE;
1818 }
1819
1820 SurfaceRef _s(dp.get(), surface);
1821 if (!_s.get()) {
1822 setError(EGL_BAD_SURFACE, EGL_FALSE);
1823 return EGL_FALSE;
1824 }
1825
1826 egl_surface_t const * const s = get_surface(surface);
1827 native_window_set_buffers_timestamp(s->win.get(), time);
1828
1829 return EGL_TRUE;
1830}
1831
Craig Donner05249fc2016-01-15 19:33:55 -08001832EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1833{
1834 clearError();
1835
Craig Donnere96a3252017-02-02 12:13:34 -08001836 uint64_t producerUsage = 0;
1837 uint64_t consumerUsage = 0;
Craig Donner05249fc2016-01-15 19:33:55 -08001838 uint32_t width = 0;
1839 uint32_t height = 0;
1840 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001841 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001842 uint32_t red_size = 0;
1843 uint32_t green_size = 0;
1844 uint32_t blue_size = 0;
1845 uint32_t alpha_size = 0;
1846
Craig Donnerb40504a2016-06-03 17:54:25 -07001847#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001848 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001849 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001850 target = value; \
1851 } else { \
1852 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1853 } \
1854 break
1855
1856 if (attrib_list) {
1857 while (*attrib_list != EGL_NONE) {
1858 GLint attr = *attrib_list++;
1859 GLint value = *attrib_list++;
1860 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001861 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1862 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1863 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1864 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1865 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1866 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001867 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001868 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1869 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001870 producerUsage |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001871 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001872 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001873 producerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
Craig Donner05249fc2016-01-15 19:33:55 -08001874 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001875 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001876 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
Craig Donner05249fc2016-01-15 19:33:55 -08001877 }
Craig Donner05249fc2016-01-15 19:33:55 -08001878 break;
1879 default:
1880 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1881 }
1882 }
1883 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001884#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001885
1886 // Validate format.
1887 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1888 if (alpha_size == 8) {
1889 format = HAL_PIXEL_FORMAT_RGBA_8888;
1890 } else {
1891 format = HAL_PIXEL_FORMAT_RGB_888;
1892 }
1893 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1894 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001895 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001896 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001897 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001898 red_size, green_size, blue_size, alpha_size);
1899 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1900 }
1901
Craig Donner68671532016-07-18 10:19:54 -07001902#define CHECK_ERROR_CONDITION(message) \
1903 if (err != NO_ERROR) { \
1904 ALOGE(message); \
1905 goto error_condition; \
1906 }
1907
1908 // The holder is used to destroy the buffer if an error occurs.
1909 GraphicBuffer* gBuffer = new GraphicBuffer();
1910 sp<IServiceManager> sm = defaultServiceManager();
1911 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1912 sp<IBinder> allocator;
1913 Parcel sc_data, sc_reply, data, reply;
1914 status_t err = NO_ERROR;
1915 if (sm == NULL) {
1916 ALOGE("Unable to connect to ServiceManager");
1917 goto error_condition;
1918 }
1919
1920 // Obtain an allocator.
1921 if (surfaceFlinger == NULL) {
1922 ALOGE("Unable to connect to SurfaceFlinger");
1923 goto error_condition;
1924 }
1925 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1926 err = surfaceFlinger->transact(
1927 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1928 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1929 allocator = sc_reply.readStrongBinder();
1930
1931 if (allocator == NULL) {
1932 ALOGE("Unable to obtain an ISurfaceComposer");
1933 goto error_condition;
1934 }
1935 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1936 err = data.writeUint32(width);
1937 CHECK_ERROR_CONDITION("Unable to write width");
1938 err = data.writeUint32(height);
1939 CHECK_ERROR_CONDITION("Unable to write height");
1940 err = data.writeInt32(static_cast<int32_t>(format));
1941 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001942 err = data.writeUint32(layer_count);
1943 CHECK_ERROR_CONDITION("Unable to write layer count");
Craig Donnere96a3252017-02-02 12:13:34 -08001944 err = data.writeUint64(producerUsage);
1945 CHECK_ERROR_CONDITION("Unable to write producer usage");
1946 err = data.writeUint64(consumerUsage);
1947 CHECK_ERROR_CONDITION("Unable to write consumer usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001948 err = data.writeUtf8AsUtf16(
1949 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1950 std::to_string(getpid()) + ']');
1951 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001952 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1953 &reply);
1954 CHECK_ERROR_CONDITION(
1955 "Unable to request buffer allocation from surface composer");
1956 err = reply.readInt32();
1957 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1958 err = reply.read(*gBuffer);
1959 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1960
1961 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001962 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001963 ALOGE("Unable to create native buffer "
Craig Donnere96a3252017-02-02 12:13:34 -08001964 "{ w=%u, h=%u, f=%u, pu=%" PRIx64 " cu=%" PRIx64 ", lc=%u} %#x",
1965 width, height, format, producerUsage, consumerUsage,
1966 layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001967 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001968 }
Craig Donnere96a3252017-02-02 12:13:34 -08001969 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, pu=%" PRIx64
1970 " cu=%" PRIx64 ", lc=%u}",
1971 gBuffer, width, height, format, producerUsage, consumerUsage,
1972 layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001973 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001974
1975#undef CHECK_ERROR_CONDITION
1976
1977error_condition:
1978 // Delete the buffer.
1979 sp<GraphicBuffer> holder(gBuffer);
1980 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001981}
1982
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001983// ----------------------------------------------------------------------------
1984// NVIDIA extensions
1985// ----------------------------------------------------------------------------
1986EGLuint64NV eglGetSystemTimeFrequencyNV()
1987{
1988 clearError();
1989
1990 if (egl_init_drivers() == EGL_FALSE) {
1991 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1992 }
1993
1994 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001995 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001996
Mathias Agopianada798b2012-02-13 17:09:30 -08001997 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1998 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001999 }
2000
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07002001 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002002}
2003
2004EGLuint64NV eglGetSystemTimeNV()
2005{
2006 clearError();
2007
2008 if (egl_init_drivers() == EGL_FALSE) {
2009 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2010 }
2011
2012 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002013 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002014
Mathias Agopianada798b2012-02-13 17:09:30 -08002015 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2016 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002017 }
2018
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07002019 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002020}
Dan Stozaa894d082015-02-19 15:27:36 -08002021
2022// ----------------------------------------------------------------------------
2023// Partial update extension
2024// ----------------------------------------------------------------------------
2025EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2026 EGLint *rects, EGLint n_rects)
2027{
2028 clearError();
2029
2030 const egl_display_ptr dp = validate_display(dpy);
2031 if (!dp) {
2032 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2033 return EGL_FALSE;
2034 }
2035
2036 SurfaceRef _s(dp.get(), surface);
2037 if (!_s.get()) {
2038 setError(EGL_BAD_SURFACE, EGL_FALSE);
2039 return EGL_FALSE;
2040 }
2041
2042 egl_surface_t const * const s = get_surface(surface);
2043 if (s->cnx->egl.eglSetDamageRegionKHR) {
2044 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2045 rects, n_rects);
2046 }
2047
2048 return EGL_FALSE;
2049}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002050
Brian Anderson1049d1d2016-12-16 17:25:57 -08002051EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2052 EGLuint64KHR *frameId) {
2053 clearError();
2054
2055 const egl_display_ptr dp = validate_display(dpy);
2056 if (!dp) {
2057 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2058 }
2059
2060 SurfaceRef _s(dp.get(), surface);
2061 if (!_s.get()) {
2062 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2063 }
2064
2065 egl_surface_t const * const s = get_surface(surface);
2066
2067 if (!s->win.get()) {
2068 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2069 }
2070
2071 uint64_t nextFrameId = 0;
2072 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2073
2074 if (ret != NO_ERROR) {
2075 // This should not happen. Return an error that is not in the spec
2076 // so it's obvious something is very wrong.
2077 ALOGE("eglGetNextFrameId: Unexpected error.");
2078 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
2079 }
2080
2081 *frameId = nextFrameId;
2082 return EGL_TRUE;
2083}
2084
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002085EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2086 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2087{
2088 clearError();
2089
2090 const egl_display_ptr dp = validate_display(dpy);
2091 if (!dp) {
2092 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2093 }
2094
2095 SurfaceRef _s(dp.get(), surface);
2096 if (!_s.get()) {
2097 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2098 }
2099
2100 egl_surface_t const * const s = get_surface(surface);
2101
2102 if (!s->win.get()) {
2103 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2104 }
2105
2106 nsecs_t* compositeDeadline = nullptr;
2107 nsecs_t* compositeInterval = nullptr;
2108 nsecs_t* compositeToPresentLatency = nullptr;
2109
2110 for (int i = 0; i < numTimestamps; i++) {
2111 switch (names[i]) {
2112 case EGL_COMPOSITE_DEADLINE_ANDROID:
2113 compositeDeadline = &values[i];
2114 break;
2115 case EGL_COMPOSITE_INTERVAL_ANDROID:
2116 compositeInterval = &values[i];
2117 break;
2118 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2119 compositeToPresentLatency = &values[i];
2120 break;
2121 default:
2122 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2123 }
2124 }
2125
2126 status_t ret = native_window_get_compositor_timing(s->win.get(),
2127 compositeDeadline, compositeInterval, compositeToPresentLatency);
2128
2129 switch (ret) {
2130 case NO_ERROR:
2131 return EGL_TRUE;
2132 case INVALID_OPERATION:
2133 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2134 default:
2135 // This should not happen. Return an error that is not in the spec
2136 // so it's obvious something is very wrong.
2137 ALOGE("eglGetCompositorTiming: Unexpected error.");
2138 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
2139 }
2140}
2141
2142EGLBoolean eglGetCompositorTimingSupportedANDROID(
2143 EGLDisplay dpy, EGLSurface surface, EGLint name)
2144{
2145 clearError();
2146
2147 const egl_display_ptr dp = validate_display(dpy);
2148 if (!dp) {
2149 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2150 }
2151
2152 SurfaceRef _s(dp.get(), surface);
2153 if (!_s.get()) {
2154 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2155 }
2156
2157 egl_surface_t const * const s = get_surface(surface);
2158
2159 ANativeWindow* window = s->win.get();
2160 if (!window) {
2161 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2162 }
2163
2164 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002165 case EGL_COMPOSITE_DEADLINE_ANDROID:
2166 case EGL_COMPOSITE_INTERVAL_ANDROID:
2167 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2168 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002169 default:
2170 return EGL_FALSE;
2171 }
2172}
2173
Pablo Ceballosc18be292016-05-31 14:55:42 -07002174EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002175 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002176 EGLnsecsANDROID *values)
2177{
2178 clearError();
2179
2180 const egl_display_ptr dp = validate_display(dpy);
2181 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002182 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002183 }
2184
2185 SurfaceRef _s(dp.get(), surface);
2186 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002187 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002188 }
2189
2190 egl_surface_t const * const s = get_surface(surface);
2191
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002192 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002193 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002194 }
2195
Brian Andersondbd0ea82016-07-22 09:38:59 -07002196 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002197 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002198 nsecs_t* latchTime = nullptr;
2199 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002200 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002201 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002202 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002203 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002204 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002205 nsecs_t* releaseTime = nullptr;
2206
2207 for (int i = 0; i < numTimestamps; i++) {
2208 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002209 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2210 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002211 break;
2212 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2213 acquireTime = &values[i];
2214 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002215 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2216 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002217 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002218 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2219 firstRefreshStartTime = &values[i];
2220 break;
2221 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2222 lastRefreshStartTime = &values[i];
2223 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002224 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2225 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002226 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002227 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2228 displayPresentTime = &values[i];
2229 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002230 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2231 displayRetireTime = &values[i];
2232 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002233 case EGL_DEQUEUE_READY_TIME_ANDROID:
2234 dequeueReadyTime = &values[i];
2235 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002236 case EGL_READS_DONE_TIME_ANDROID:
2237 releaseTime = &values[i];
2238 break;
2239 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002240 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002241 }
2242 }
2243
Brian Anderson1049d1d2016-12-16 17:25:57 -08002244 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002245 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002246 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002247 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002248
Brian Anderson069b3652016-07-22 10:32:47 -07002249 switch (ret) {
2250 case NO_ERROR:
2251 return EGL_TRUE;
2252 case NAME_NOT_FOUND:
2253 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002254 case INVALID_OPERATION:
2255 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002256 case BAD_VALUE:
2257 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2258 default:
2259 // This should not happen. Return an error that is not in the spec
2260 // so it's obvious something is very wrong.
Brian Anderson1049d1d2016-12-16 17:25:57 -08002261 ALOGE("eglGetFrameTimestamps: Unexpected error.");
Brian Anderson069b3652016-07-22 10:32:47 -07002262 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002263 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002264}
2265
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002266EGLBoolean eglGetFrameTimestampSupportedANDROID(
2267 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002268{
2269 clearError();
2270
2271 const egl_display_ptr dp = validate_display(dpy);
2272 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002273 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002274 }
2275
2276 SurfaceRef _s(dp.get(), surface);
2277 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002278 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2279 }
2280
2281 egl_surface_t const * const s = get_surface(surface);
2282
2283 ANativeWindow* window = s->win.get();
2284 if (!window) {
2285 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002286 }
2287
2288 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002289 case EGL_COMPOSITE_DEADLINE_ANDROID:
2290 case EGL_COMPOSITE_INTERVAL_ANDROID:
2291 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002292 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002293 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002294 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2295 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2296 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002297 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002298 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002299 case EGL_READS_DONE_TIME_ANDROID:
2300 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002301 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2302 int value = 0;
2303 window->query(window,
2304 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2305 return value == 0 ? EGL_FALSE : EGL_TRUE;
2306 }
2307 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2308 int value = 0;
2309 window->query(window,
2310 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2311 return value == 0 ? EGL_FALSE : EGL_TRUE;
2312 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002313 default:
2314 return EGL_FALSE;
2315 }
2316}