blob: 42d01b210373a3713fcb750a62e644f71ad47016 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
24#include <hardware/gralloc.h>
25#include <system/window.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
Mathias Agopian518ec112011-05-13 16:21:08 -070030#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070031#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070032#include <cutils/memory.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Craig Donner68671532016-07-18 10:19:54 -070036#include <gui/ISurfaceComposer.h>
37
Craig Donner05249fc2016-01-15 19:33:55 -080038#include <ui/GraphicBuffer.h>
39
Mathias Agopian518ec112011-05-13 16:21:08 -070040#include <utils/KeyedVector.h>
41#include <utils/SortedVector.h>
42#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070044
Craig Donner68671532016-07-18 10:19:54 -070045#include "binder/Binder.h"
46#include "binder/Parcel.h"
47#include "binder/IServiceManager.h"
48
Mathias Agopian39c24a22013-04-04 23:17:56 -070049#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070050#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070051
52#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070053#include "egl_object.h"
54#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080055#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070056
57using namespace android;
58
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070059#define ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS 0
60
Mathias Agopian518ec112011-05-13 16:21:08 -070061// ----------------------------------------------------------------------------
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063namespace android {
64
Mathias Agopian518ec112011-05-13 16:21:08 -070065struct extention_map_t {
66 const char* name;
67 __eglMustCastToProperFunctionPointerType address;
68};
69
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070070/*
Jesse Hall21558da2013-08-06 15:31:22 -070071 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070072 *
Jesse Hall21558da2013-08-06 15:31:22 -070073 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
74 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070075 *
Jesse Hall21558da2013-08-06 15:31:22 -070076 * The rest (gExtensionString) depend on support in the EGL driver, and are
77 * only available if the driver supports them. However, some of these must be
78 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080079 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070080 * the mandatory extensions are present and may not function properly if some
81 * are missing.
82 *
83 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070084 */
Jesse Hall21558da2013-08-06 15:31:22 -070085extern char const * const gBuiltinExtensionString =
86 "EGL_KHR_get_all_proc_addresses "
87 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080088 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080089 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080090 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070091#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -070092 "EGL_ANDROID_get_frame_timestamps "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070093#endif
Jesse Hall21558da2013-08-06 15:31:22 -070094 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095extern char const * const gExtensionString =
96 "EGL_KHR_image " // mandatory
97 "EGL_KHR_image_base " // mandatory
98 "EGL_KHR_image_pixmap "
99 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700100 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700101 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700102 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_cubemap_image "
104 "EGL_KHR_gl_renderbuffer_image "
105 "EGL_KHR_reusable_sync "
106 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700107 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700108 "EGL_KHR_config_attribs "
109 "EGL_KHR_surfaceless_context "
110 "EGL_KHR_stream "
111 "EGL_KHR_stream_fifo "
112 "EGL_KHR_stream_producer_eglsurface "
113 "EGL_KHR_stream_consumer_gltexture "
114 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700115 "EGL_EXT_create_context_robustness "
116 "EGL_NV_system_time "
117 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700118 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700119 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800120 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700121 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800122 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700123 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700124 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700125 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700126 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000127 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700128 ;
129
130// extensions not exposed to applications but used by the ANDROID system
131// "EGL_ANDROID_blob_cache " // strongly recommended
132// "EGL_IMG_hibernate_process " // optional
133// "EGL_ANDROID_native_fence_sync " // strongly recommended
134// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700135// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700136
137/*
138 * EGL Extensions entry-points exposed to 3rd party applications
139 * (keep in sync with gExtensionString above)
140 *
141 */
142static const extention_map_t sExtensionMap[] = {
143 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700144 { "eglLockSurfaceKHR",
145 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
146 { "eglUnlockSurfaceKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700148
149 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700150 { "eglCreateImageKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
152 { "eglDestroyImageKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700154
155 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
156 { "eglCreateSyncKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
158 { "eglDestroySyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
160 { "eglClientWaitSyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
162 { "eglSignalSyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
164 { "eglGetSyncAttribKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
166
167 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800168 { "eglGetSystemTimeFrequencyNV",
169 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
170 { "eglGetSystemTimeNV",
171 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700172
Mathias Agopian2bb71682013-03-27 17:32:41 -0700173 // EGL_KHR_wait_sync
174 { "eglWaitSyncKHR",
175 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700176
177 // EGL_ANDROID_presentation_time
178 { "eglPresentationTimeANDROID",
179 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800180
181 // EGL_KHR_swap_buffers_with_damage
182 { "eglSwapBuffersWithDamageKHR",
183 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
184
Craig Donner05249fc2016-01-15 19:33:55 -0800185 // EGL_ANDROID_native_client_buffer
186 { "eglCreateNativeClientBufferANDROID",
187 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
188
Dan Stozaa894d082015-02-19 15:27:36 -0800189 // EGL_KHR_partial_update
190 { "eglSetDamageRegionKHR",
191 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700192
193 { "eglCreateStreamKHR",
194 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
195 { "eglDestroyStreamKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
197 { "eglStreamAttribKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
199 { "eglQueryStreamKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
201 { "eglQueryStreamu64KHR",
202 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
203 { "eglQueryStreamTimeKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
205 { "eglCreateStreamProducerSurfaceKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
207 { "eglStreamConsumerGLTextureExternalKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
209 { "eglStreamConsumerAcquireKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
211 { "eglStreamConsumerReleaseKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
213 { "eglGetStreamFileDescriptorKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
215 { "eglCreateStreamFromFileDescriptorKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700217
218 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800219 { "eglGetNextFrameIdANDROID",
220 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800221 { "eglGetCompositorTimingANDROID",
222 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
223 { "eglGetCompositorTimingSupportedANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700225 { "eglGetFrameTimestampsANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800227 { "eglGetFrameTimestampSupportedANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700229};
230
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700231/*
232 * These extensions entry-points should not be exposed to applications.
233 * They're used internally by the Android EGL layer.
234 */
235#define FILTER_EXTENSIONS(procname) \
236 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
237 !strcmp((procname), "eglHibernateProcessIMG") || \
238 !strcmp((procname), "eglAwakenProcessIMG") || \
239 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
240
241
242
Mathias Agopian518ec112011-05-13 16:21:08 -0700243// accesses protected by sExtensionMapMutex
244static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
245static int sGLExtentionSlot = 0;
246static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
247
248static void(*findProcAddress(const char* name,
249 const extention_map_t* map, size_t n))() {
250 for (uint32_t i=0 ; i<n ; i++) {
251 if (!strcmp(name, map[i].name)) {
252 return map[i].address;
253 }
254 }
255 return NULL;
256}
257
258// ----------------------------------------------------------------------------
259
Mathias Agopian518ec112011-05-13 16:21:08 -0700260extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
261extern EGLBoolean egl_init_drivers();
262extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700263extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700264
Mathias Agopian518ec112011-05-13 16:21:08 -0700265} // namespace android;
266
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700267
Mathias Agopian518ec112011-05-13 16:21:08 -0700268// ----------------------------------------------------------------------------
269
270static inline void clearError() { egl_tls_t::clearError(); }
271static inline EGLContext getContext() { return egl_tls_t::getContext(); }
272
273// ----------------------------------------------------------------------------
274
275EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
276{
Jesse Hall1508ae62017-01-19 17:43:26 -0800277 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700278 clearError();
279
Dan Stozac3289c42014-01-17 11:38:34 -0800280 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700281 if (index >= NUM_DISPLAYS) {
282 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
283 }
284
285 if (egl_init_drivers() == EGL_FALSE) {
286 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
287 }
288
289 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
290 return dpy;
291}
292
293// ----------------------------------------------------------------------------
294// Initialization
295// ----------------------------------------------------------------------------
296
297EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
298{
299 clearError();
300
Jesse Hallb29e5e82012-04-04 16:53:42 -0700301 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700302 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
303
304 EGLBoolean res = dp->initialize(major, minor);
305
306 return res;
307}
308
309EGLBoolean eglTerminate(EGLDisplay dpy)
310{
311 // NOTE: don't unload the drivers b/c some APIs can be called
312 // after eglTerminate() has been called. eglTerminate() only
313 // terminates an EGLDisplay, not a EGL itself.
314
315 clearError();
316
Jesse Hallb29e5e82012-04-04 16:53:42 -0700317 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700318 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
319
320 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800321
Mathias Agopian518ec112011-05-13 16:21:08 -0700322 return res;
323}
324
325// ----------------------------------------------------------------------------
326// configuration
327// ----------------------------------------------------------------------------
328
329EGLBoolean eglGetConfigs( EGLDisplay dpy,
330 EGLConfig *configs,
331 EGLint config_size, EGLint *num_config)
332{
333 clearError();
334
Jesse Hallb29e5e82012-04-04 16:53:42 -0700335 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700336 if (!dp) return EGL_FALSE;
337
Mathias Agopian7773c432012-02-13 20:06:08 -0800338 if (num_config==0) {
339 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700340 }
341
Mathias Agopian7773c432012-02-13 20:06:08 -0800342 EGLBoolean res = EGL_FALSE;
343 *num_config = 0;
344
345 egl_connection_t* const cnx = &gEGLImpl;
346 if (cnx->dso) {
347 res = cnx->egl.eglGetConfigs(
348 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700349 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800350
351 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700352}
353
354EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
355 EGLConfig *configs, EGLint config_size,
356 EGLint *num_config)
357{
358 clearError();
359
Jesse Hallb29e5e82012-04-04 16:53:42 -0700360 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700361 if (!dp) return EGL_FALSE;
362
363 if (num_config==0) {
364 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
365 }
366
Mathias Agopian518ec112011-05-13 16:21:08 -0700367 EGLBoolean res = EGL_FALSE;
368 *num_config = 0;
369
Mathias Agopianada798b2012-02-13 17:09:30 -0800370 egl_connection_t* const cnx = &gEGLImpl;
371 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700372 if (attrib_list) {
373 char value[PROPERTY_VALUE_MAX];
374 property_get("debug.egl.force_msaa", value, "false");
375
376 if (!strcmp(value, "true")) {
377 size_t attribCount = 0;
378 EGLint attrib = attrib_list[0];
379
380 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700381 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700382 const EGLint *attribRendererable = NULL;
383 const EGLint *attribCaveat = NULL;
384
385 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700386 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700387 while (attrib != EGL_NONE) {
388 attrib = attrib_list[attribCount];
389 switch (attrib) {
390 case EGL_RENDERABLE_TYPE:
391 attribRendererable = &attrib_list[attribCount];
392 break;
393 case EGL_CONFIG_CAVEAT:
394 attribCaveat = &attrib_list[attribCount];
395 break;
396 }
397 attribCount++;
398 }
399
400 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
401 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800402
Romain Guy1cffc802012-10-15 18:13:05 -0700403 // Insert 2 extra attributes to force-enable MSAA 4x
404 EGLint aaAttribs[attribCount + 4];
405 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
406 aaAttribs[1] = 1;
407 aaAttribs[2] = EGL_SAMPLES;
408 aaAttribs[3] = 4;
409
410 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
411
412 EGLint numConfigAA;
413 EGLBoolean resAA = cnx->egl.eglChooseConfig(
414 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
415
416 if (resAA == EGL_TRUE && numConfigAA > 0) {
417 ALOGD("Enabling MSAA 4x");
418 *num_config = numConfigAA;
419 return resAA;
420 }
421 }
422 }
423 }
424
Mathias Agopian7773c432012-02-13 20:06:08 -0800425 res = cnx->egl.eglChooseConfig(
426 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700427 }
428 return res;
429}
430
431EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
432 EGLint attribute, EGLint *value)
433{
434 clearError();
435
Jesse Hallb29e5e82012-04-04 16:53:42 -0700436 egl_connection_t* cnx = NULL;
437 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
438 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800439
Mathias Agopian518ec112011-05-13 16:21:08 -0700440 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800441 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700442}
443
444// ----------------------------------------------------------------------------
445// surfaces
446// ----------------------------------------------------------------------------
447
Jesse Hallc2e41222013-08-08 13:40:22 -0700448// Turn linear formats into corresponding sRGB formats when colorspace is
449// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
450// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800451// the modification isn't possible, the original dataSpace is returned.
452static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
453 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700454 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800455 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700456 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800457 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700458 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800459 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700460}
461
Mathias Agopian518ec112011-05-13 16:21:08 -0700462EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
463 NativeWindowType window,
464 const EGLint *attrib_list)
465{
466 clearError();
467
Jesse Hallb29e5e82012-04-04 16:53:42 -0700468 egl_connection_t* cnx = NULL;
469 egl_display_ptr dp = validate_display_connection(dpy, cnx);
470 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800471 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700472
Andy McFaddend566ce32014-01-07 15:54:17 -0800473 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
474 if (result != OK) {
475 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
476 "failed (%#x) (already connected to another API?)",
477 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700478 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700479 }
480
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700481 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700482 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
483 // of our native format. So if sRGB gamma is requested, we have to
484 // modify the EGLconfig's format before setting the native window's
485 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800486
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700487 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
488 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
489 &componentType);
490
491 // by default, just pick appropriate RGBA
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700492 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700493 if (dp->haveExtension("EGL_EXT_pixel_format_float") &&
494 (componentType == EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT)) {
495 format = HAL_PIXEL_FORMAT_RGBA_FP16;
496 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800497 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700498
499 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700500 EGLint r, g, b;
501 r = g = b = 0;
502 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
503 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
504 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700505 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700506 EGLint colorDepth = r + g + b;
507
508 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700509 if (colorDepth <= 16) {
510 format = HAL_PIXEL_FORMAT_RGB_565;
511 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700512 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
513 if (colorDepth > 24) {
514 format = HAL_PIXEL_FORMAT_RGBA_1010102;
515 } else {
516 format = HAL_PIXEL_FORMAT_RGBX_8888;
517 }
518 } else {
519 format = HAL_PIXEL_FORMAT_RGBA_FP16;
520 }
521 }
522 } else {
523 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
524 if (colorDepth > 24) {
525 format = HAL_PIXEL_FORMAT_RGBA_1010102;
526 } else {
527 format = HAL_PIXEL_FORMAT_RGBA_8888;
528 }
529 } else {
530 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700531 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700532 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700533
534 // now select a corresponding sRGB format if needed
535 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
536 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
537 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530538 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700539 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700540 }
541 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800542
Jesse Hallc2e41222013-08-08 13:40:22 -0700543 if (format != 0) {
544 int err = native_window_set_buffers_format(window, format);
545 if (err != 0) {
546 ALOGE("error setting native window pixel format: %s (%d)",
547 strerror(-err), err);
548 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
549 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
550 }
551 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700552
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800553 if (dataSpace != 0) {
554 int err = native_window_set_buffers_data_space(window, dataSpace);
555 if (err != 0) {
556 ALOGE("error setting native window pixel dataSpace: %s (%d)",
557 strerror(-err), err);
558 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
559 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
560 }
561 }
562
Jamie Gennis59769462011-11-19 18:04:43 -0800563 // the EGL spec requires that a new EGLSurface default to swap interval
564 // 1, so explicitly set that on the window here.
565 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
566 anw->setSwapInterval(anw, 1);
567
Mathias Agopian518ec112011-05-13 16:21:08 -0700568 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800569 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700570 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700571 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
572 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700573 return s;
574 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700575
576 // EGLSurface creation failed
577 native_window_set_buffers_format(window, 0);
578 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700579 }
580 return EGL_NO_SURFACE;
581}
582
583EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
584 NativePixmapType pixmap,
585 const EGLint *attrib_list)
586{
587 clearError();
588
Jesse Hallb29e5e82012-04-04 16:53:42 -0700589 egl_connection_t* cnx = NULL;
590 egl_display_ptr dp = validate_display_connection(dpy, cnx);
591 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700592 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800593 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700594 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700595 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
596 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700597 return s;
598 }
599 }
600 return EGL_NO_SURFACE;
601}
602
603EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
604 const EGLint *attrib_list)
605{
606 clearError();
607
Jesse Hallb29e5e82012-04-04 16:53:42 -0700608 egl_connection_t* cnx = NULL;
609 egl_display_ptr dp = validate_display_connection(dpy, cnx);
610 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700611 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800612 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700613 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700614 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
615 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700616 return s;
617 }
618 }
619 return EGL_NO_SURFACE;
620}
Jesse Hall47743382013-02-08 11:13:46 -0800621
Mathias Agopian518ec112011-05-13 16:21:08 -0700622EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
623{
624 clearError();
625
Jesse Hallb29e5e82012-04-04 16:53:42 -0700626 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700627 if (!dp) return EGL_FALSE;
628
Jesse Hallb29e5e82012-04-04 16:53:42 -0700629 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700630 if (!_s.get())
631 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700632
633 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800634 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700635 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700636 _s.terminate();
637 }
638 return result;
639}
640
641EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
642 EGLint attribute, EGLint *value)
643{
644 clearError();
645
Jesse Hallb29e5e82012-04-04 16:53:42 -0700646 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700647 if (!dp) return EGL_FALSE;
648
Jesse Hallb29e5e82012-04-04 16:53:42 -0700649 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700650 if (!_s.get())
651 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700652
Mathias Agopian518ec112011-05-13 16:21:08 -0700653 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800654 return s->cnx->egl.eglQuerySurface(
655 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700656}
657
Jamie Gennise8696a42012-01-15 18:54:57 -0800658void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800659 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800660 clearError();
661
Jesse Hallb29e5e82012-04-04 16:53:42 -0700662 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800663 if (!dp) {
664 return;
665 }
666
Jesse Hallb29e5e82012-04-04 16:53:42 -0700667 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800668 if (!_s.get()) {
669 setError(EGL_BAD_SURFACE, EGL_FALSE);
670 return;
671 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800672}
673
Mathias Agopian518ec112011-05-13 16:21:08 -0700674// ----------------------------------------------------------------------------
675// Contexts
676// ----------------------------------------------------------------------------
677
678EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
679 EGLContext share_list, const EGLint *attrib_list)
680{
681 clearError();
682
Jesse Hallb29e5e82012-04-04 16:53:42 -0700683 egl_connection_t* cnx = NULL;
684 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700685 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700686 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700687 if (!ContextRef(dp.get(), share_list).get()) {
688 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
689 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700690 egl_context_t* const c = get_context(share_list);
691 share_list = c->context;
692 }
693 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800694 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700695 if (context != EGL_NO_CONTEXT) {
696 // figure out if it's a GLESv1 or GLESv2
697 int version = 0;
698 if (attrib_list) {
699 while (*attrib_list != EGL_NONE) {
700 GLint attr = *attrib_list++;
701 GLint value = *attrib_list++;
702 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
703 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800704 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800705 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800706 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700707 }
708 }
709 };
710 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700711 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
712 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 return c;
714 }
715 }
716 return EGL_NO_CONTEXT;
717}
718
719EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
720{
721 clearError();
722
Jesse Hallb29e5e82012-04-04 16:53:42 -0700723 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700724 if (!dp)
725 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700726
Jesse Hallb29e5e82012-04-04 16:53:42 -0700727 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700728 if (!_c.get())
729 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800730
Mathias Agopian518ec112011-05-13 16:21:08 -0700731 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800732 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700733 if (result == EGL_TRUE) {
734 _c.terminate();
735 }
736 return result;
737}
738
Mathias Agopian518ec112011-05-13 16:21:08 -0700739EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
740 EGLSurface read, EGLContext ctx)
741{
742 clearError();
743
Jesse Hallb29e5e82012-04-04 16:53:42 -0700744 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700745 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
746
Mathias Agopian5b287a62011-05-16 18:58:55 -0700747 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
748 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
749 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700750 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
751 (draw != EGL_NO_SURFACE) ) {
752 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
753 }
754
755 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700756 ContextRef _c(dp.get(), ctx);
757 SurfaceRef _d(dp.get(), draw);
758 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700759
760 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700761 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700762 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700763 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700764 }
765
766 // these are the underlying implementation's object
767 EGLContext impl_ctx = EGL_NO_CONTEXT;
768 EGLSurface impl_draw = EGL_NO_SURFACE;
769 EGLSurface impl_read = EGL_NO_SURFACE;
770
771 // these are our objects structs passed in
772 egl_context_t * c = NULL;
773 egl_surface_t const * d = NULL;
774 egl_surface_t const * r = NULL;
775
776 // these are the current objects structs
777 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800778
Mathias Agopian518ec112011-05-13 16:21:08 -0700779 if (ctx != EGL_NO_CONTEXT) {
780 c = get_context(ctx);
781 impl_ctx = c->context;
782 } else {
783 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700784 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
785 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
786 return setError(EGL_BAD_MATCH, EGL_FALSE);
787 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700788 if (cur_c == NULL) {
789 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700790 // not an error, there is just no current context.
791 return EGL_TRUE;
792 }
793 }
794
795 // retrieve the underlying implementation's draw EGLSurface
796 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700797 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 impl_draw = d->surface;
800 }
801
802 // retrieve the underlying implementation's read EGLSurface
803 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700804 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700805 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700806 impl_read = r->surface;
807 }
808
Mathias Agopian518ec112011-05-13 16:21:08 -0700809
Jesse Hallb29e5e82012-04-04 16:53:42 -0700810 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800811 draw, read, ctx,
812 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700813
814 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800815 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700816 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
817 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700818 _c.acquire();
819 _r.acquire();
820 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700821 } else {
822 setGLHooksThreadSpecific(&gHooksNoContext);
823 egl_tls_t::setContext(EGL_NO_CONTEXT);
824 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700825 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000826 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700827 egl_connection_t* const cnx = &gEGLImpl;
828 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700829 }
830 return result;
831}
832
833
834EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
835 EGLint attribute, EGLint *value)
836{
837 clearError();
838
Jesse Hallb29e5e82012-04-04 16:53:42 -0700839 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700840 if (!dp) return EGL_FALSE;
841
Jesse Hallb29e5e82012-04-04 16:53:42 -0700842 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700843 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
844
Mathias Agopian518ec112011-05-13 16:21:08 -0700845 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800846 return c->cnx->egl.eglQueryContext(
847 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700848
Mathias Agopian518ec112011-05-13 16:21:08 -0700849}
850
851EGLContext eglGetCurrentContext(void)
852{
853 // could be called before eglInitialize(), but we wouldn't have a context
854 // then, and this function would correctly return EGL_NO_CONTEXT.
855
856 clearError();
857
858 EGLContext ctx = getContext();
859 return ctx;
860}
861
862EGLSurface eglGetCurrentSurface(EGLint readdraw)
863{
864 // could be called before eglInitialize(), but we wouldn't have a context
865 // then, and this function would correctly return EGL_NO_SURFACE.
866
867 clearError();
868
869 EGLContext ctx = getContext();
870 if (ctx) {
871 egl_context_t const * const c = get_context(ctx);
872 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
873 switch (readdraw) {
874 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800875 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700876 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
877 }
878 }
879 return EGL_NO_SURFACE;
880}
881
882EGLDisplay eglGetCurrentDisplay(void)
883{
884 // could be called before eglInitialize(), but we wouldn't have a context
885 // then, and this function would correctly return EGL_NO_DISPLAY.
886
887 clearError();
888
889 EGLContext ctx = getContext();
890 if (ctx) {
891 egl_context_t const * const c = get_context(ctx);
892 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
893 return c->dpy;
894 }
895 return EGL_NO_DISPLAY;
896}
897
898EGLBoolean eglWaitGL(void)
899{
Mathias Agopian518ec112011-05-13 16:21:08 -0700900 clearError();
901
Mathias Agopianada798b2012-02-13 17:09:30 -0800902 egl_connection_t* const cnx = &gEGLImpl;
903 if (!cnx->dso)
904 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
905
906 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700907}
908
909EGLBoolean eglWaitNative(EGLint engine)
910{
Mathias Agopian518ec112011-05-13 16:21:08 -0700911 clearError();
912
Mathias Agopianada798b2012-02-13 17:09:30 -0800913 egl_connection_t* const cnx = &gEGLImpl;
914 if (!cnx->dso)
915 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
916
917 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700918}
919
920EGLint eglGetError(void)
921{
Mathias Agopianada798b2012-02-13 17:09:30 -0800922 EGLint err = EGL_SUCCESS;
923 egl_connection_t* const cnx = &gEGLImpl;
924 if (cnx->dso) {
925 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700926 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800927 if (err == EGL_SUCCESS) {
928 err = egl_tls_t::getError();
929 }
930 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700931}
932
Michael Chockc0ec5e22014-01-27 08:14:33 -0800933static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700934 const char* procname) {
935 const egl_connection_t* cnx = &gEGLImpl;
936 void* proc = NULL;
937
Michael Chockc0ec5e22014-01-27 08:14:33 -0800938 proc = dlsym(cnx->libEgl, procname);
939 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
940
Jesse Hallc07b5202013-07-04 12:08:16 -0700941 proc = dlsym(cnx->libGles2, procname);
942 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
943
944 proc = dlsym(cnx->libGles1, procname);
945 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
946
947 return NULL;
948}
949
Mathias Agopian518ec112011-05-13 16:21:08 -0700950__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
951{
952 // eglGetProcAddress() could be the very first function called
953 // in which case we must make sure we've initialized ourselves, this
954 // happens the first time egl_get_display() is called.
955
956 clearError();
957
958 if (egl_init_drivers() == EGL_FALSE) {
959 setError(EGL_BAD_PARAMETER, NULL);
960 return NULL;
961 }
962
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700963 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700964 return NULL;
965 }
966
Mathias Agopian518ec112011-05-13 16:21:08 -0700967 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700968 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 if (addr) return addr;
970
Michael Chockc0ec5e22014-01-27 08:14:33 -0800971 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700972 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700973
Mathias Agopian518ec112011-05-13 16:21:08 -0700974 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
975 pthread_mutex_lock(&sExtensionMapMutex);
976
977 /*
978 * Since eglGetProcAddress() is not associated to anything, it needs
979 * to return a function pointer that "works" regardless of what
980 * the current context is.
981 *
982 * For this reason, we return a "forwarder", a small stub that takes
983 * care of calling the function associated with the context
984 * currently bound.
985 *
986 * We first look for extensions we've already resolved, if we're seeing
987 * this extension for the first time, we go through all our
988 * implementations and call eglGetProcAddress() and record the
989 * result in the appropriate implementation hooks and return the
990 * address of the forwarder corresponding to that hook set.
991 *
992 */
993
994 const String8 name(procname);
995 addr = sGLExtentionMap.valueFor(name);
996 const int slot = sGLExtentionSlot;
997
Steve Blocke6f43dd2012-01-06 19:20:56 +0000998 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700999 "no more slots for eglGetProcAddress(\"%s\")",
1000 procname);
1001
1002 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1003 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001004
1005 egl_connection_t* const cnx = &gEGLImpl;
1006 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001007 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001008 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001009 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1010 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001011 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001012 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001013 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001014
Mathias Agopian518ec112011-05-13 16:21:08 -07001015 if (found) {
1016 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 sGLExtentionMap.add(name, addr);
1018 sGLExtentionSlot++;
1019 }
1020 }
1021
1022 pthread_mutex_unlock(&sExtensionMapMutex);
1023 return addr;
1024}
1025
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001026class FrameCompletionThread : public Thread {
1027public:
1028
1029 static void queueSync(EGLSyncKHR sync) {
1030 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1031 static bool running = false;
1032 if (!running) {
1033 thread->run("GPUFrameCompletion");
1034 running = true;
1035 }
1036 {
1037 Mutex::Autolock lock(thread->mMutex);
1038 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1039 thread->mFramesQueued).string());
1040 thread->mQueue.push_back(sync);
1041 thread->mCondition.signal();
1042 thread->mFramesQueued++;
1043 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1044 }
1045 }
1046
1047private:
1048 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1049
1050 virtual bool threadLoop() {
1051 EGLSyncKHR sync;
1052 uint32_t frameNum;
1053 {
1054 Mutex::Autolock lock(mMutex);
1055 while (mQueue.isEmpty()) {
1056 mCondition.wait(mMutex);
1057 }
1058 sync = mQueue[0];
1059 frameNum = mFramesCompleted;
1060 }
1061 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1062 {
1063 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1064 frameNum).string());
1065 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1066 if (result == EGL_FALSE) {
1067 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1068 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1069 ALOGE("FrameCompletion: timeout waiting for fence");
1070 }
1071 eglDestroySyncKHR(dpy, sync);
1072 }
1073 {
1074 Mutex::Autolock lock(mMutex);
1075 mQueue.removeAt(0);
1076 mFramesCompleted++;
1077 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1078 }
1079 return true;
1080 }
1081
1082 uint32_t mFramesQueued;
1083 uint32_t mFramesCompleted;
1084 Vector<EGLSyncKHR> mQueue;
1085 Condition mCondition;
1086 Mutex mMutex;
1087};
1088
Dan Stozaa894d082015-02-19 15:27:36 -08001089EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1090 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001091{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001092 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001093 clearError();
1094
Jesse Hallb29e5e82012-04-04 16:53:42 -07001095 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001096 if (!dp) return EGL_FALSE;
1097
Jesse Hallb29e5e82012-04-04 16:53:42 -07001098 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001099 if (!_s.get())
1100 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001101
Mathias Agopian518ec112011-05-13 16:21:08 -07001102 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001103
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001104 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1105 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1106 if (sync != EGL_NO_SYNC_KHR) {
1107 FrameCompletionThread::queueSync(sync);
1108 }
1109 }
1110
Mathias Agopian7db993a2012-03-25 00:49:46 -07001111 if (CC_UNLIKELY(dp->finishOnSwap)) {
1112 uint32_t pixel;
1113 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1114 if (c) {
1115 // glReadPixels() ensures that the frame is complete
1116 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1117 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1118 }
1119 }
1120
Dan Stozaa894d082015-02-19 15:27:36 -08001121 if (n_rects == 0) {
1122 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1123 }
1124
1125 Vector<android_native_rect_t> androidRects;
1126 for (int r = 0; r < n_rects; ++r) {
1127 int offset = r * 4;
1128 int x = rects[offset];
1129 int y = rects[offset + 1];
1130 int width = rects[offset + 2];
1131 int height = rects[offset + 3];
1132 android_native_rect_t androidRect;
1133 androidRect.left = x;
1134 androidRect.top = y + height;
1135 androidRect.right = x + width;
1136 androidRect.bottom = y;
1137 androidRects.push_back(androidRect);
1138 }
1139 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1140 androidRects.size());
1141
1142 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1143 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1144 rects, n_rects);
1145 } else {
1146 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1147 }
1148}
1149
1150EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1151{
1152 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001153}
1154
1155EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1156 NativePixmapType target)
1157{
1158 clearError();
1159
Jesse Hallb29e5e82012-04-04 16:53:42 -07001160 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001161 if (!dp) return EGL_FALSE;
1162
Jesse Hallb29e5e82012-04-04 16:53:42 -07001163 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001164 if (!_s.get())
1165 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001166
Mathias Agopian518ec112011-05-13 16:21:08 -07001167 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001168 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001169}
1170
1171const char* eglQueryString(EGLDisplay dpy, EGLint name)
1172{
1173 clearError();
1174
Chia-I Wue57d1352016-08-15 16:10:02 +08001175 // Generate an error quietly when client extensions (as defined by
1176 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1177 // validate_display to generate the error as validate_display would log
1178 // the error, which can be misleading.
1179 //
1180 // If we want to support EGL_EXT_client_extensions later, we can return
1181 // the client extension string here instead.
1182 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1183 return setErrorQuiet(EGL_BAD_DISPLAY, nullptr);
1184
Jesse Hallb29e5e82012-04-04 16:53:42 -07001185 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001186 if (!dp) return (const char *) NULL;
1187
1188 switch (name) {
1189 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001190 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001191 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001192 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001193 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001194 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001195 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001196 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001197 }
1198 return setError(EGL_BAD_PARAMETER, (const char *)0);
1199}
1200
Mathias Agopianca088332013-03-28 17:44:13 -07001201EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1202{
1203 clearError();
1204
1205 const egl_display_ptr dp = validate_display(dpy);
1206 if (!dp) return (const char *) NULL;
1207
1208 switch (name) {
1209 case EGL_VENDOR:
1210 return dp->disp.queryString.vendor;
1211 case EGL_VERSION:
1212 return dp->disp.queryString.version;
1213 case EGL_EXTENSIONS:
1214 return dp->disp.queryString.extensions;
1215 case EGL_CLIENT_APIS:
1216 return dp->disp.queryString.clientApi;
1217 }
1218 return setError(EGL_BAD_PARAMETER, (const char *)0);
1219}
Mathias Agopian518ec112011-05-13 16:21:08 -07001220
1221// ----------------------------------------------------------------------------
1222// EGL 1.1
1223// ----------------------------------------------------------------------------
1224
1225EGLBoolean eglSurfaceAttrib(
1226 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1227{
1228 clearError();
1229
Jesse Hallb29e5e82012-04-04 16:53:42 -07001230 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001231 if (!dp) return EGL_FALSE;
1232
Jesse Hallb29e5e82012-04-04 16:53:42 -07001233 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001234 if (!_s.get())
1235 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001236
Pablo Ceballosc18be292016-05-31 14:55:42 -07001237 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001238
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001239 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001240 if (!s->win.get()) {
1241 setError(EGL_BAD_SURFACE, EGL_FALSE);
1242 }
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001243 int err = native_window_set_auto_refresh(s->win.get(),
1244 value ? true : false);
1245 return (err == NO_ERROR) ? EGL_TRUE :
1246 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001247 }
1248
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001249#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001250 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001251 if (!s->win.get()) {
1252 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1253 }
Brian Anderson069b3652016-07-22 10:32:47 -07001254 int err = native_window_enable_frame_timestamps(
1255 s->win.get(), value ? true : false);
1256 return (err == NO_ERROR) ? EGL_TRUE :
1257 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001258 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001259#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001260
Mathias Agopian518ec112011-05-13 16:21:08 -07001261 if (s->cnx->egl.eglSurfaceAttrib) {
1262 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001263 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001264 }
1265 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1266}
1267
1268EGLBoolean eglBindTexImage(
1269 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1270{
1271 clearError();
1272
Jesse Hallb29e5e82012-04-04 16:53:42 -07001273 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001274 if (!dp) return EGL_FALSE;
1275
Jesse Hallb29e5e82012-04-04 16:53:42 -07001276 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001277 if (!_s.get())
1278 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001279
Mathias Agopian518ec112011-05-13 16:21:08 -07001280 egl_surface_t const * const s = get_surface(surface);
1281 if (s->cnx->egl.eglBindTexImage) {
1282 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001283 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001284 }
1285 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1286}
1287
1288EGLBoolean eglReleaseTexImage(
1289 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1290{
1291 clearError();
1292
Jesse Hallb29e5e82012-04-04 16:53:42 -07001293 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001294 if (!dp) return EGL_FALSE;
1295
Jesse Hallb29e5e82012-04-04 16:53:42 -07001296 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001297 if (!_s.get())
1298 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001299
Mathias Agopian518ec112011-05-13 16:21:08 -07001300 egl_surface_t const * const s = get_surface(surface);
1301 if (s->cnx->egl.eglReleaseTexImage) {
1302 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001303 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001304 }
1305 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1306}
1307
1308EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1309{
1310 clearError();
1311
Jesse Hallb29e5e82012-04-04 16:53:42 -07001312 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001313 if (!dp) return EGL_FALSE;
1314
1315 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001316 egl_connection_t* const cnx = &gEGLImpl;
1317 if (cnx->dso && cnx->egl.eglSwapInterval) {
1318 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001319 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001320
Mathias Agopian518ec112011-05-13 16:21:08 -07001321 return res;
1322}
1323
1324
1325// ----------------------------------------------------------------------------
1326// EGL 1.2
1327// ----------------------------------------------------------------------------
1328
1329EGLBoolean eglWaitClient(void)
1330{
1331 clearError();
1332
Mathias Agopianada798b2012-02-13 17:09:30 -08001333 egl_connection_t* const cnx = &gEGLImpl;
1334 if (!cnx->dso)
1335 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1336
1337 EGLBoolean res;
1338 if (cnx->egl.eglWaitClient) {
1339 res = cnx->egl.eglWaitClient();
1340 } else {
1341 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001342 }
1343 return res;
1344}
1345
1346EGLBoolean eglBindAPI(EGLenum api)
1347{
1348 clearError();
1349
1350 if (egl_init_drivers() == EGL_FALSE) {
1351 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1352 }
1353
1354 // bind this API on all EGLs
1355 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001356 egl_connection_t* const cnx = &gEGLImpl;
1357 if (cnx->dso && cnx->egl.eglBindAPI) {
1358 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001359 }
1360 return res;
1361}
1362
1363EGLenum eglQueryAPI(void)
1364{
1365 clearError();
1366
1367 if (egl_init_drivers() == EGL_FALSE) {
1368 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1369 }
1370
Mathias Agopianada798b2012-02-13 17:09:30 -08001371 egl_connection_t* const cnx = &gEGLImpl;
1372 if (cnx->dso && cnx->egl.eglQueryAPI) {
1373 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001374 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001375
Mathias Agopian518ec112011-05-13 16:21:08 -07001376 // or, it can only be OpenGL ES
1377 return EGL_OPENGL_ES_API;
1378}
1379
1380EGLBoolean eglReleaseThread(void)
1381{
1382 clearError();
1383
Mathias Agopianada798b2012-02-13 17:09:30 -08001384 egl_connection_t* const cnx = &gEGLImpl;
1385 if (cnx->dso && cnx->egl.eglReleaseThread) {
1386 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001387 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301388
1389 // If there is context bound to the thread, release it
1390 egl_display_t::loseCurrent(get_context(getContext()));
1391
Mathias Agopian518ec112011-05-13 16:21:08 -07001392 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001393 return EGL_TRUE;
1394}
1395
1396EGLSurface eglCreatePbufferFromClientBuffer(
1397 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1398 EGLConfig config, const EGLint *attrib_list)
1399{
1400 clearError();
1401
Jesse Hallb29e5e82012-04-04 16:53:42 -07001402 egl_connection_t* cnx = NULL;
1403 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1404 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001405 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1406 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001407 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001408 }
1409 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1410}
1411
1412// ----------------------------------------------------------------------------
1413// EGL_EGLEXT_VERSION 3
1414// ----------------------------------------------------------------------------
1415
1416EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1417 const EGLint *attrib_list)
1418{
1419 clearError();
1420
Jesse Hallb29e5e82012-04-04 16:53:42 -07001421 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001422 if (!dp) return EGL_FALSE;
1423
Jesse Hallb29e5e82012-04-04 16:53:42 -07001424 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001425 if (!_s.get())
1426 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001427
1428 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001429 if (s->cnx->egl.eglLockSurfaceKHR) {
1430 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001431 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001432 }
1433 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1434}
1435
1436EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1437{
1438 clearError();
1439
Jesse Hallb29e5e82012-04-04 16:53:42 -07001440 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001441 if (!dp) return EGL_FALSE;
1442
Jesse Hallb29e5e82012-04-04 16:53:42 -07001443 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001444 if (!_s.get())
1445 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001446
1447 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001448 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001449 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450 }
1451 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1452}
1453
1454EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1455 EGLClientBuffer buffer, const EGLint *attrib_list)
1456{
1457 clearError();
1458
Jesse Hallb29e5e82012-04-04 16:53:42 -07001459 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001460 if (!dp) return EGL_NO_IMAGE_KHR;
1461
Jesse Hallb29e5e82012-04-04 16:53:42 -07001462 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001463 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001464
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001465 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1466 egl_connection_t* const cnx = &gEGLImpl;
1467 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1468 result = cnx->egl.eglCreateImageKHR(
1469 dp->disp.dpy,
1470 c ? c->context : EGL_NO_CONTEXT,
1471 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001472 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001473 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001474}
1475
1476EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1477{
1478 clearError();
1479
Jesse Hallb29e5e82012-04-04 16:53:42 -07001480 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001481 if (!dp) return EGL_FALSE;
1482
Steven Holte646a5c52012-06-04 20:02:11 -07001483 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001484 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001485 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001486 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001487 }
Steven Holte646a5c52012-06-04 20:02:11 -07001488 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001489}
1490
1491// ----------------------------------------------------------------------------
1492// EGL_EGLEXT_VERSION 5
1493// ----------------------------------------------------------------------------
1494
1495
1496EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1497{
1498 clearError();
1499
Jesse Hallb29e5e82012-04-04 16:53:42 -07001500 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001501 if (!dp) return EGL_NO_SYNC_KHR;
1502
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001504 egl_connection_t* const cnx = &gEGLImpl;
1505 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1506 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001507 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001508 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001509}
1510
1511EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1512{
1513 clearError();
1514
Jesse Hallb29e5e82012-04-04 16:53:42 -07001515 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 if (!dp) return EGL_FALSE;
1517
Mathias Agopian518ec112011-05-13 16:21:08 -07001518 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001519 egl_connection_t* const cnx = &gEGLImpl;
1520 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1521 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001522 }
1523 return result;
1524}
1525
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001526EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1527 clearError();
1528
1529 const egl_display_ptr dp = validate_display(dpy);
1530 if (!dp) return EGL_FALSE;
1531
1532 EGLBoolean result = EGL_FALSE;
1533 egl_connection_t* const cnx = &gEGLImpl;
1534 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1535 result = cnx->egl.eglSignalSyncKHR(
1536 dp->disp.dpy, sync, mode);
1537 }
1538 return result;
1539}
1540
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001541EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1542 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001543{
1544 clearError();
1545
Jesse Hallb29e5e82012-04-04 16:53:42 -07001546 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001547 if (!dp) return EGL_FALSE;
1548
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001549 EGLBoolean result = EGL_FALSE;
1550 egl_connection_t* const cnx = &gEGLImpl;
1551 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1552 result = cnx->egl.eglClientWaitSyncKHR(
1553 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001554 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001555 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001556}
1557
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001558EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1559 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001560{
1561 clearError();
1562
Jesse Hallb29e5e82012-04-04 16:53:42 -07001563 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001564 if (!dp) return EGL_FALSE;
1565
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001566 EGLBoolean result = EGL_FALSE;
1567 egl_connection_t* const cnx = &gEGLImpl;
1568 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1569 result = cnx->egl.eglGetSyncAttribKHR(
1570 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001571 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001572 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001573}
1574
Season Li000d88f2015-07-01 11:39:40 -07001575EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1576{
1577 clearError();
1578
1579 const egl_display_ptr dp = validate_display(dpy);
1580 if (!dp) return EGL_NO_STREAM_KHR;
1581
1582 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1583 egl_connection_t* const cnx = &gEGLImpl;
1584 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1585 result = cnx->egl.eglCreateStreamKHR(
1586 dp->disp.dpy, attrib_list);
1587 }
1588 return result;
1589}
1590
1591EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1592{
1593 clearError();
1594
1595 const egl_display_ptr dp = validate_display(dpy);
1596 if (!dp) return EGL_FALSE;
1597
1598 EGLBoolean result = EGL_FALSE;
1599 egl_connection_t* const cnx = &gEGLImpl;
1600 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1601 result = cnx->egl.eglDestroyStreamKHR(
1602 dp->disp.dpy, stream);
1603 }
1604 return result;
1605}
1606
1607EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1608 EGLenum attribute, EGLint value)
1609{
1610 clearError();
1611
1612 const egl_display_ptr dp = validate_display(dpy);
1613 if (!dp) return EGL_FALSE;
1614
1615 EGLBoolean result = EGL_FALSE;
1616 egl_connection_t* const cnx = &gEGLImpl;
1617 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1618 result = cnx->egl.eglStreamAttribKHR(
1619 dp->disp.dpy, stream, attribute, value);
1620 }
1621 return result;
1622}
1623
1624EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1625 EGLenum attribute, EGLint *value)
1626{
1627 clearError();
1628
1629 const egl_display_ptr dp = validate_display(dpy);
1630 if (!dp) return EGL_FALSE;
1631
1632 EGLBoolean result = EGL_FALSE;
1633 egl_connection_t* const cnx = &gEGLImpl;
1634 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1635 result = cnx->egl.eglQueryStreamKHR(
1636 dp->disp.dpy, stream, attribute, value);
1637 }
1638 return result;
1639}
1640
1641EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1642 EGLenum attribute, EGLuint64KHR *value)
1643{
1644 clearError();
1645
1646 const egl_display_ptr dp = validate_display(dpy);
1647 if (!dp) return EGL_FALSE;
1648
1649 EGLBoolean result = EGL_FALSE;
1650 egl_connection_t* const cnx = &gEGLImpl;
1651 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1652 result = cnx->egl.eglQueryStreamu64KHR(
1653 dp->disp.dpy, stream, attribute, value);
1654 }
1655 return result;
1656}
1657
1658EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1659 EGLenum attribute, EGLTimeKHR *value)
1660{
1661 clearError();
1662
1663 const egl_display_ptr dp = validate_display(dpy);
1664 if (!dp) return EGL_FALSE;
1665
1666 EGLBoolean result = EGL_FALSE;
1667 egl_connection_t* const cnx = &gEGLImpl;
1668 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1669 result = cnx->egl.eglQueryStreamTimeKHR(
1670 dp->disp.dpy, stream, attribute, value);
1671 }
1672 return result;
1673}
1674
1675EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1676 EGLStreamKHR stream, const EGLint *attrib_list)
1677{
1678 clearError();
1679
1680 egl_display_ptr dp = validate_display(dpy);
1681 if (!dp) return EGL_NO_SURFACE;
1682
1683 egl_connection_t* const cnx = &gEGLImpl;
1684 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1685 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1686 dp->disp.dpy, config, stream, attrib_list);
1687 if (surface != EGL_NO_SURFACE) {
1688 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1689 surface, cnx);
1690 return s;
1691 }
1692 }
1693 return EGL_NO_SURFACE;
1694}
1695
1696EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1697 EGLStreamKHR stream)
1698{
1699 clearError();
1700
1701 const egl_display_ptr dp = validate_display(dpy);
1702 if (!dp) return EGL_FALSE;
1703
1704 EGLBoolean result = EGL_FALSE;
1705 egl_connection_t* const cnx = &gEGLImpl;
1706 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1707 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1708 dp->disp.dpy, stream);
1709 }
1710 return result;
1711}
1712
1713EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1714 EGLStreamKHR stream)
1715{
1716 clearError();
1717
1718 const egl_display_ptr dp = validate_display(dpy);
1719 if (!dp) return EGL_FALSE;
1720
1721 EGLBoolean result = EGL_FALSE;
1722 egl_connection_t* const cnx = &gEGLImpl;
1723 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1724 result = cnx->egl.eglStreamConsumerAcquireKHR(
1725 dp->disp.dpy, stream);
1726 }
1727 return result;
1728}
1729
1730EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1731 EGLStreamKHR stream)
1732{
1733 clearError();
1734
1735 const egl_display_ptr dp = validate_display(dpy);
1736 if (!dp) return EGL_FALSE;
1737
1738 EGLBoolean result = EGL_FALSE;
1739 egl_connection_t* const cnx = &gEGLImpl;
1740 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1741 result = cnx->egl.eglStreamConsumerReleaseKHR(
1742 dp->disp.dpy, stream);
1743 }
1744 return result;
1745}
1746
1747EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1748 EGLDisplay dpy, EGLStreamKHR stream)
1749{
1750 clearError();
1751
1752 const egl_display_ptr dp = validate_display(dpy);
1753 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1754
1755 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1756 egl_connection_t* const cnx = &gEGLImpl;
1757 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1758 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1759 dp->disp.dpy, stream);
1760 }
1761 return result;
1762}
1763
1764EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1765 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1766{
1767 clearError();
1768
1769 const egl_display_ptr dp = validate_display(dpy);
1770 if (!dp) return EGL_NO_STREAM_KHR;
1771
1772 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1773 egl_connection_t* const cnx = &gEGLImpl;
1774 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1775 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1776 dp->disp.dpy, file_descriptor);
1777 }
1778 return result;
1779}
1780
Mathias Agopian518ec112011-05-13 16:21:08 -07001781// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001782// EGL_EGLEXT_VERSION 15
1783// ----------------------------------------------------------------------------
1784
1785EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1786 clearError();
1787 const egl_display_ptr dp = validate_display(dpy);
1788 if (!dp) return EGL_FALSE;
1789 EGLint result = EGL_FALSE;
1790 egl_connection_t* const cnx = &gEGLImpl;
1791 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1792 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1793 }
1794 return result;
1795}
1796
1797// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001798// ANDROID extensions
1799// ----------------------------------------------------------------------------
1800
Jamie Gennis331841b2012-09-06 14:52:00 -07001801EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1802{
1803 clearError();
1804
1805 const egl_display_ptr dp = validate_display(dpy);
1806 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1807
1808 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1809 egl_connection_t* const cnx = &gEGLImpl;
1810 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1811 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1812 }
1813 return result;
1814}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001815
Andy McFadden72841452013-03-01 16:25:32 -08001816EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1817 EGLnsecsANDROID time)
1818{
1819 clearError();
1820
1821 const egl_display_ptr dp = validate_display(dpy);
1822 if (!dp) {
1823 return EGL_FALSE;
1824 }
1825
1826 SurfaceRef _s(dp.get(), surface);
1827 if (!_s.get()) {
1828 setError(EGL_BAD_SURFACE, EGL_FALSE);
1829 return EGL_FALSE;
1830 }
1831
1832 egl_surface_t const * const s = get_surface(surface);
1833 native_window_set_buffers_timestamp(s->win.get(), time);
1834
1835 return EGL_TRUE;
1836}
1837
Craig Donner05249fc2016-01-15 19:33:55 -08001838EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1839{
1840 clearError();
1841
1842 int usage = 0;
1843 uint32_t width = 0;
1844 uint32_t height = 0;
1845 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001846 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001847 uint32_t red_size = 0;
1848 uint32_t green_size = 0;
1849 uint32_t blue_size = 0;
1850 uint32_t alpha_size = 0;
1851
Craig Donnerb40504a2016-06-03 17:54:25 -07001852#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001853 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001854 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001855 target = value; \
1856 } else { \
1857 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1858 } \
1859 break
1860
1861 if (attrib_list) {
1862 while (*attrib_list != EGL_NONE) {
1863 GLint attr = *attrib_list++;
1864 GLint value = *attrib_list++;
1865 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001866 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1867 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1868 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1869 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1870 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1871 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001872 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001873 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1874 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1875 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001876 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001877 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001878 usage |= GRALLOC_USAGE_HW_RENDER;
1879 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001880 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001881 usage |= GRALLOC_USAGE_HW_TEXTURE;
1882 }
Craig Donner05249fc2016-01-15 19:33:55 -08001883 break;
1884 default:
1885 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1886 }
1887 }
1888 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001889#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001890
1891 // Validate format.
1892 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1893 if (alpha_size == 8) {
1894 format = HAL_PIXEL_FORMAT_RGBA_8888;
1895 } else {
1896 format = HAL_PIXEL_FORMAT_RGB_888;
1897 }
1898 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1899 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001900 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001901 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001902 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001903 red_size, green_size, blue_size, alpha_size);
1904 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1905 }
1906
Craig Donner68671532016-07-18 10:19:54 -07001907#define CHECK_ERROR_CONDITION(message) \
1908 if (err != NO_ERROR) { \
1909 ALOGE(message); \
1910 goto error_condition; \
1911 }
1912
1913 // The holder is used to destroy the buffer if an error occurs.
1914 GraphicBuffer* gBuffer = new GraphicBuffer();
1915 sp<IServiceManager> sm = defaultServiceManager();
1916 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1917 sp<IBinder> allocator;
1918 Parcel sc_data, sc_reply, data, reply;
1919 status_t err = NO_ERROR;
1920 if (sm == NULL) {
1921 ALOGE("Unable to connect to ServiceManager");
1922 goto error_condition;
1923 }
1924
1925 // Obtain an allocator.
1926 if (surfaceFlinger == NULL) {
1927 ALOGE("Unable to connect to SurfaceFlinger");
1928 goto error_condition;
1929 }
1930 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1931 err = surfaceFlinger->transact(
1932 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1933 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1934 allocator = sc_reply.readStrongBinder();
1935
1936 if (allocator == NULL) {
1937 ALOGE("Unable to obtain an ISurfaceComposer");
1938 goto error_condition;
1939 }
1940 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1941 err = data.writeUint32(width);
1942 CHECK_ERROR_CONDITION("Unable to write width");
1943 err = data.writeUint32(height);
1944 CHECK_ERROR_CONDITION("Unable to write height");
1945 err = data.writeInt32(static_cast<int32_t>(format));
1946 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001947 err = data.writeUint32(layer_count);
1948 CHECK_ERROR_CONDITION("Unable to write layer count");
1949 err = data.writeUint32(usage);
Craig Donner68671532016-07-18 10:19:54 -07001950 CHECK_ERROR_CONDITION("Unable to write usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001951 err = data.writeUtf8AsUtf16(
1952 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1953 std::to_string(getpid()) + ']');
1954 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001955 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1956 &reply);
1957 CHECK_ERROR_CONDITION(
1958 "Unable to request buffer allocation from surface composer");
1959 err = reply.readInt32();
1960 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1961 err = reply.read(*gBuffer);
1962 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1963
1964 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001965 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001966 ALOGE("Unable to create native buffer "
1967 "{ w=%u, h=%u, f=%u, u=%#x, lc=%u}: %#x", width, height, format,
1968 usage, layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001969 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001970 }
Craig Donner6ebc46a2016-10-21 15:23:44 -07001971 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, u=%#x, lc=%u}",
1972 gBuffer, width, height, format, usage, 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) {
2165#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
2166 case EGL_COMPOSITE_DEADLINE_ANDROID:
2167 case EGL_COMPOSITE_INTERVAL_ANDROID:
2168 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2169 return EGL_TRUE;
2170#endif
2171 default:
2172 return EGL_FALSE;
2173 }
2174}
2175
Pablo Ceballosc18be292016-05-31 14:55:42 -07002176EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002177 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002178 EGLnsecsANDROID *values)
2179{
2180 clearError();
2181
2182 const egl_display_ptr dp = validate_display(dpy);
2183 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002184 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002185 }
2186
2187 SurfaceRef _s(dp.get(), surface);
2188 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002189 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002190 }
2191
2192 egl_surface_t const * const s = get_surface(surface);
2193
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002194 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002195 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002196 }
2197
Brian Andersondbd0ea82016-07-22 09:38:59 -07002198 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002199 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002200 nsecs_t* latchTime = nullptr;
2201 nsecs_t* firstRefreshStartTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002202 nsecs_t* GLCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002203 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002204 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002205 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002206 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002207 nsecs_t* releaseTime = nullptr;
2208
2209 for (int i = 0; i < numTimestamps; i++) {
2210 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002211 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2212 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002213 break;
2214 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2215 acquireTime = &values[i];
2216 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002217 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2218 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002219 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002220 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2221 firstRefreshStartTime = &values[i];
2222 break;
2223 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2224 lastRefreshStartTime = &values[i];
2225 break;
2226 case EGL_FIRST_COMPOSITION_FINISHED_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002227 GLCompositionDoneTime = &values[i];
2228 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002229 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2230 displayPresentTime = &values[i];
2231 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002232 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2233 displayRetireTime = &values[i];
2234 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002235 case EGL_DEQUEUE_READY_TIME_ANDROID:
2236 dequeueReadyTime = &values[i];
2237 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002238 case EGL_READS_DONE_TIME_ANDROID:
2239 releaseTime = &values[i];
2240 break;
2241 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002242 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002243 }
2244 }
2245
Brian Anderson1049d1d2016-12-16 17:25:57 -08002246 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002247 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
2248 lastRefreshStartTime, GLCompositionDoneTime, displayPresentTime,
2249 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002250
Brian Anderson069b3652016-07-22 10:32:47 -07002251 switch (ret) {
2252 case NO_ERROR:
2253 return EGL_TRUE;
2254 case NAME_NOT_FOUND:
2255 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002256 case INVALID_OPERATION:
2257 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002258 case BAD_VALUE:
2259 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2260 default:
2261 // This should not happen. Return an error that is not in the spec
2262 // so it's obvious something is very wrong.
Brian Anderson1049d1d2016-12-16 17:25:57 -08002263 ALOGE("eglGetFrameTimestamps: Unexpected error.");
Brian Anderson069b3652016-07-22 10:32:47 -07002264 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002265 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002266}
2267
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002268EGLBoolean eglGetFrameTimestampSupportedANDROID(
2269 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002270{
2271 clearError();
2272
2273 const egl_display_ptr dp = validate_display(dpy);
2274 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002275 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002276 }
2277
2278 SurfaceRef _s(dp.get(), surface);
2279 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002280 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2281 }
2282
2283 egl_surface_t const * const s = get_surface(surface);
2284
2285 ANativeWindow* window = s->win.get();
2286 if (!window) {
2287 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002288 }
2289
2290 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002291#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002292 case EGL_COMPOSITE_DEADLINE_ANDROID:
2293 case EGL_COMPOSITE_INTERVAL_ANDROID:
2294 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002295 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002296 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002297 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2298 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2299 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2300 case EGL_FIRST_COMPOSITION_FINISHED_TIME_ANDROID:
2301 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002302 case EGL_READS_DONE_TIME_ANDROID:
2303 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002304 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2305 int value = 0;
2306 window->query(window,
2307 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2308 return value == 0 ? EGL_FALSE : EGL_TRUE;
2309 }
2310 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2311 int value = 0;
2312 window->query(window,
2313 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2314 return value == 0 ? EGL_FALSE : EGL_TRUE;
2315 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002316#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002317 default:
2318 return EGL_FALSE;
2319 }
2320}