blob: d0943a9062749b8ab966ba2e133e0cc59dc6f940 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025#include <system/window.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
Craig Donner60761072017-01-27 12:30:44 -080030#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080031#include <private/android/AHardwareBufferHelpers.h>
32
Mathias Agopian518ec112011-05-13 16:21:08 -070033#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070034#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035#include <cutils/memory.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070036#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070037#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070038
Craig Donner68671532016-07-18 10:19:54 -070039#include <gui/ISurfaceComposer.h>
40
Craig Donner05249fc2016-01-15 19:33:55 -080041#include <ui/GraphicBuffer.h>
42
Mathias Agopian89ed4c82017-02-09 18:48:34 -080043
Mathias Agopian518ec112011-05-13 16:21:08 -070044#include <utils/KeyedVector.h>
45#include <utils/SortedVector.h>
46#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080047#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070048
Craig Donner68671532016-07-18 10:19:54 -070049#include "binder/Binder.h"
50#include "binder/Parcel.h"
51#include "binder/IServiceManager.h"
52
Mathias Agopian39c24a22013-04-04 23:17:56 -070053#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070054#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070055
56#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070057#include "egl_object.h"
58#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080059#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070060
61using namespace android;
62
63// ----------------------------------------------------------------------------
64
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070065namespace android {
66
Mathias Agopian518ec112011-05-13 16:21:08 -070067struct extention_map_t {
68 const char* name;
69 __eglMustCastToProperFunctionPointerType address;
70};
71
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070072/*
Jesse Hall21558da2013-08-06 15:31:22 -070073 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070074 *
Jesse Hall21558da2013-08-06 15:31:22 -070075 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
76 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070077 *
Jesse Hall21558da2013-08-06 15:31:22 -070078 * The rest (gExtensionString) depend on support in the EGL driver, and are
79 * only available if the driver supports them. However, some of these must be
80 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080081 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070082 * the mandatory extensions are present and may not function properly if some
83 * are missing.
84 *
85 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070086 */
Jesse Hall21558da2013-08-06 15:31:22 -070087extern char const * const gBuiltinExtensionString =
88 "EGL_KHR_get_all_proc_addresses "
89 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080090 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080091 "EGL_ANDROID_create_native_client_buffer "
Craig Donner60761072017-01-27 12:30:44 -080092 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080093 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070094 "EGL_ANDROID_get_frame_timestamps "
Jesse Hall21558da2013-08-06 15:31:22 -070095 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070096extern char const * const gExtensionString =
97 "EGL_KHR_image " // mandatory
98 "EGL_KHR_image_base " // mandatory
99 "EGL_KHR_image_pixmap "
100 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700101 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700102 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700103 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700104 "EGL_KHR_gl_texture_cubemap_image "
105 "EGL_KHR_gl_renderbuffer_image "
106 "EGL_KHR_reusable_sync "
107 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700108 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700109 "EGL_KHR_config_attribs "
110 "EGL_KHR_surfaceless_context "
111 "EGL_KHR_stream "
112 "EGL_KHR_stream_fifo "
113 "EGL_KHR_stream_producer_eglsurface "
114 "EGL_KHR_stream_consumer_gltexture "
115 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700116 "EGL_EXT_create_context_robustness "
117 "EGL_NV_system_time "
118 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700119 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700120 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800121 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700122 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800123 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700124 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700125 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700126 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700127 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000128 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700129 ;
130
131// extensions not exposed to applications but used by the ANDROID system
132// "EGL_ANDROID_blob_cache " // strongly recommended
133// "EGL_IMG_hibernate_process " // optional
134// "EGL_ANDROID_native_fence_sync " // strongly recommended
135// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700136// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700137
138/*
139 * EGL Extensions entry-points exposed to 3rd party applications
140 * (keep in sync with gExtensionString above)
141 *
142 */
143static const extention_map_t sExtensionMap[] = {
144 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700145 { "eglLockSurfaceKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
147 { "eglUnlockSurfaceKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700149
150 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700151 { "eglCreateImageKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
153 { "eglDestroyImageKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700155
156 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
157 { "eglCreateSyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
159 { "eglDestroySyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
161 { "eglClientWaitSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
163 { "eglSignalSyncKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
165 { "eglGetSyncAttribKHR",
166 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
167
168 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800169 { "eglGetSystemTimeFrequencyNV",
170 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
171 { "eglGetSystemTimeNV",
172 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700173
Mathias Agopian2bb71682013-03-27 17:32:41 -0700174 // EGL_KHR_wait_sync
175 { "eglWaitSyncKHR",
176 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700177
178 // EGL_ANDROID_presentation_time
179 { "eglPresentationTimeANDROID",
180 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800181
182 // EGL_KHR_swap_buffers_with_damage
183 { "eglSwapBuffersWithDamageKHR",
184 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
185
Craig Donner60761072017-01-27 12:30:44 -0800186 // EGL_ANDROID_create_native_client_buffer
Craig Donner05249fc2016-01-15 19:33:55 -0800187 { "eglCreateNativeClientBufferANDROID",
188 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
189
Craig Donner60761072017-01-27 12:30:44 -0800190 // EGL_ANDROID_get_native_client_buffer
191 { "eglGetNativeClientBufferANDROID",
192 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
193
Dan Stozaa894d082015-02-19 15:27:36 -0800194 // EGL_KHR_partial_update
195 { "eglSetDamageRegionKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700197
198 { "eglCreateStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
200 { "eglDestroyStreamKHR",
201 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
202 { "eglStreamAttribKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
204 { "eglQueryStreamKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
206 { "eglQueryStreamu64KHR",
207 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
208 { "eglQueryStreamTimeKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
210 { "eglCreateStreamProducerSurfaceKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
212 { "eglStreamConsumerGLTextureExternalKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
214 { "eglStreamConsumerAcquireKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
216 { "eglStreamConsumerReleaseKHR",
217 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
218 { "eglGetStreamFileDescriptorKHR",
219 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
220 { "eglCreateStreamFromFileDescriptorKHR",
221 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700222
223 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800224 { "eglGetNextFrameIdANDROID",
225 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800226 { "eglGetCompositorTimingANDROID",
227 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
228 { "eglGetCompositorTimingSupportedANDROID",
229 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700230 { "eglGetFrameTimestampsANDROID",
231 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800232 { "eglGetFrameTimestampSupportedANDROID",
233 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700234};
235
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700236/*
237 * These extensions entry-points should not be exposed to applications.
238 * They're used internally by the Android EGL layer.
239 */
240#define FILTER_EXTENSIONS(procname) \
241 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
242 !strcmp((procname), "eglHibernateProcessIMG") || \
243 !strcmp((procname), "eglAwakenProcessIMG") || \
244 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
245
246
247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248// accesses protected by sExtensionMapMutex
249static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
250static int sGLExtentionSlot = 0;
251static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
252
253static void(*findProcAddress(const char* name,
254 const extention_map_t* map, size_t n))() {
255 for (uint32_t i=0 ; i<n ; i++) {
256 if (!strcmp(name, map[i].name)) {
257 return map[i].address;
258 }
259 }
260 return NULL;
261}
262
263// ----------------------------------------------------------------------------
264
Mathias Agopian518ec112011-05-13 16:21:08 -0700265extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
266extern EGLBoolean egl_init_drivers();
267extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700268extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700269
Mathias Agopian518ec112011-05-13 16:21:08 -0700270} // namespace android;
271
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700272
Mathias Agopian518ec112011-05-13 16:21:08 -0700273// ----------------------------------------------------------------------------
274
275static inline void clearError() { egl_tls_t::clearError(); }
276static inline EGLContext getContext() { return egl_tls_t::getContext(); }
277
278// ----------------------------------------------------------------------------
279
280EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
281{
Jesse Hall1508ae62017-01-19 17:43:26 -0800282 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700283 clearError();
284
Dan Stozac3289c42014-01-17 11:38:34 -0800285 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700286 if (index >= NUM_DISPLAYS) {
287 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
288 }
289
290 if (egl_init_drivers() == EGL_FALSE) {
291 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
292 }
293
294 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
295 return dpy;
296}
297
298// ----------------------------------------------------------------------------
299// Initialization
300// ----------------------------------------------------------------------------
301
302EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
303{
304 clearError();
305
Jesse Hallb29e5e82012-04-04 16:53:42 -0700306 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700307 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
308
309 EGLBoolean res = dp->initialize(major, minor);
310
311 return res;
312}
313
314EGLBoolean eglTerminate(EGLDisplay dpy)
315{
316 // NOTE: don't unload the drivers b/c some APIs can be called
317 // after eglTerminate() has been called. eglTerminate() only
318 // terminates an EGLDisplay, not a EGL itself.
319
320 clearError();
321
Jesse Hallb29e5e82012-04-04 16:53:42 -0700322 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700323 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
324
325 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800326
Mathias Agopian518ec112011-05-13 16:21:08 -0700327 return res;
328}
329
330// ----------------------------------------------------------------------------
331// configuration
332// ----------------------------------------------------------------------------
333
334EGLBoolean eglGetConfigs( EGLDisplay dpy,
335 EGLConfig *configs,
336 EGLint config_size, EGLint *num_config)
337{
338 clearError();
339
Jesse Hallb29e5e82012-04-04 16:53:42 -0700340 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700341 if (!dp) return EGL_FALSE;
342
Mathias Agopian7773c432012-02-13 20:06:08 -0800343 if (num_config==0) {
344 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700345 }
346
Mathias Agopian7773c432012-02-13 20:06:08 -0800347 EGLBoolean res = EGL_FALSE;
348 *num_config = 0;
349
350 egl_connection_t* const cnx = &gEGLImpl;
351 if (cnx->dso) {
352 res = cnx->egl.eglGetConfigs(
353 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700354 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800355
356 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700357}
358
359EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
360 EGLConfig *configs, EGLint config_size,
361 EGLint *num_config)
362{
363 clearError();
364
Jesse Hallb29e5e82012-04-04 16:53:42 -0700365 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700366 if (!dp) return EGL_FALSE;
367
368 if (num_config==0) {
369 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
370 }
371
Mathias Agopian518ec112011-05-13 16:21:08 -0700372 EGLBoolean res = EGL_FALSE;
373 *num_config = 0;
374
Mathias Agopianada798b2012-02-13 17:09:30 -0800375 egl_connection_t* const cnx = &gEGLImpl;
376 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700377 if (attrib_list) {
378 char value[PROPERTY_VALUE_MAX];
379 property_get("debug.egl.force_msaa", value, "false");
380
381 if (!strcmp(value, "true")) {
382 size_t attribCount = 0;
383 EGLint attrib = attrib_list[0];
384
385 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700386 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700387 const EGLint *attribRendererable = NULL;
388 const EGLint *attribCaveat = NULL;
389
390 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700391 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700392 while (attrib != EGL_NONE) {
393 attrib = attrib_list[attribCount];
394 switch (attrib) {
395 case EGL_RENDERABLE_TYPE:
396 attribRendererable = &attrib_list[attribCount];
397 break;
398 case EGL_CONFIG_CAVEAT:
399 attribCaveat = &attrib_list[attribCount];
400 break;
401 }
402 attribCount++;
403 }
404
405 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
406 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800407
Romain Guy1cffc802012-10-15 18:13:05 -0700408 // Insert 2 extra attributes to force-enable MSAA 4x
409 EGLint aaAttribs[attribCount + 4];
410 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
411 aaAttribs[1] = 1;
412 aaAttribs[2] = EGL_SAMPLES;
413 aaAttribs[3] = 4;
414
415 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
416
417 EGLint numConfigAA;
418 EGLBoolean resAA = cnx->egl.eglChooseConfig(
419 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
420
421 if (resAA == EGL_TRUE && numConfigAA > 0) {
422 ALOGD("Enabling MSAA 4x");
423 *num_config = numConfigAA;
424 return resAA;
425 }
426 }
427 }
428 }
429
Mathias Agopian7773c432012-02-13 20:06:08 -0800430 res = cnx->egl.eglChooseConfig(
431 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 }
433 return res;
434}
435
436EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
437 EGLint attribute, EGLint *value)
438{
439 clearError();
440
Jesse Hallb29e5e82012-04-04 16:53:42 -0700441 egl_connection_t* cnx = NULL;
442 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
443 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800444
Mathias Agopian518ec112011-05-13 16:21:08 -0700445 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800446 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700447}
448
449// ----------------------------------------------------------------------------
450// surfaces
451// ----------------------------------------------------------------------------
452
Jesse Hallc2e41222013-08-08 13:40:22 -0700453// Turn linear formats into corresponding sRGB formats when colorspace is
454// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
455// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800456// the modification isn't possible, the original dataSpace is returned.
457static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
458 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700459 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800460 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700461 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800462 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700463 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800464 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700465}
466
Mathias Agopian518ec112011-05-13 16:21:08 -0700467EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
468 NativeWindowType window,
469 const EGLint *attrib_list)
470{
471 clearError();
472
Jesse Hallb29e5e82012-04-04 16:53:42 -0700473 egl_connection_t* cnx = NULL;
474 egl_display_ptr dp = validate_display_connection(dpy, cnx);
475 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800476 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700477
Andy McFaddend566ce32014-01-07 15:54:17 -0800478 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
479 if (result != OK) {
480 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
481 "failed (%#x) (already connected to another API?)",
482 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700483 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700484 }
485
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700486 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700487 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
488 // of our native format. So if sRGB gamma is requested, we have to
489 // modify the EGLconfig's format before setting the native window's
490 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800491
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700492 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
493 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
494 &componentType);
495
496 // by default, just pick appropriate RGBA
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700497 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700498 if (dp->haveExtension("EGL_EXT_pixel_format_float") &&
499 (componentType == EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT)) {
500 format = HAL_PIXEL_FORMAT_RGBA_FP16;
501 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800502 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700503
504 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700505 EGLint r, g, b;
506 r = g = b = 0;
507 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
508 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
509 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700510 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700511 EGLint colorDepth = r + g + b;
512
513 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700514 if (colorDepth <= 16) {
515 format = HAL_PIXEL_FORMAT_RGB_565;
516 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700517 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
518 if (colorDepth > 24) {
519 format = HAL_PIXEL_FORMAT_RGBA_1010102;
520 } else {
521 format = HAL_PIXEL_FORMAT_RGBX_8888;
522 }
523 } else {
524 format = HAL_PIXEL_FORMAT_RGBA_FP16;
525 }
526 }
527 } else {
528 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
529 if (colorDepth > 24) {
530 format = HAL_PIXEL_FORMAT_RGBA_1010102;
531 } else {
532 format = HAL_PIXEL_FORMAT_RGBA_8888;
533 }
534 } else {
535 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700536 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700537 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700538
539 // now select a corresponding sRGB format if needed
540 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
541 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
542 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530543 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700544 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700545 }
546 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800547
Jesse Hallc2e41222013-08-08 13:40:22 -0700548 if (format != 0) {
549 int err = native_window_set_buffers_format(window, format);
550 if (err != 0) {
551 ALOGE("error setting native window pixel format: %s (%d)",
552 strerror(-err), err);
553 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
554 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
555 }
556 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700557
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800558 if (dataSpace != 0) {
559 int err = native_window_set_buffers_data_space(window, dataSpace);
560 if (err != 0) {
561 ALOGE("error setting native window pixel dataSpace: %s (%d)",
562 strerror(-err), err);
563 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
564 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
565 }
566 }
567
Jamie Gennis59769462011-11-19 18:04:43 -0800568 // the EGL spec requires that a new EGLSurface default to swap interval
569 // 1, so explicitly set that on the window here.
570 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
571 anw->setSwapInterval(anw, 1);
572
Mathias Agopian518ec112011-05-13 16:21:08 -0700573 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800574 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700576 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
577 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700578 return s;
579 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700580
581 // EGLSurface creation failed
582 native_window_set_buffers_format(window, 0);
583 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700584 }
585 return EGL_NO_SURFACE;
586}
587
588EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
589 NativePixmapType pixmap,
590 const EGLint *attrib_list)
591{
592 clearError();
593
Jesse Hallb29e5e82012-04-04 16:53:42 -0700594 egl_connection_t* cnx = NULL;
595 egl_display_ptr dp = validate_display_connection(dpy, cnx);
596 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700597 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800598 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700599 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700600 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
601 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700602 return s;
603 }
604 }
605 return EGL_NO_SURFACE;
606}
607
608EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
609 const EGLint *attrib_list)
610{
611 clearError();
612
Jesse Hallb29e5e82012-04-04 16:53:42 -0700613 egl_connection_t* cnx = NULL;
614 egl_display_ptr dp = validate_display_connection(dpy, cnx);
615 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700616 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800617 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700618 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700619 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
620 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700621 return s;
622 }
623 }
624 return EGL_NO_SURFACE;
625}
Jesse Hall47743382013-02-08 11:13:46 -0800626
Mathias Agopian518ec112011-05-13 16:21:08 -0700627EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
628{
629 clearError();
630
Jesse Hallb29e5e82012-04-04 16:53:42 -0700631 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700632 if (!dp) return EGL_FALSE;
633
Jesse Hallb29e5e82012-04-04 16:53:42 -0700634 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700635 if (!_s.get())
636 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700637
638 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800639 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700640 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700641 _s.terminate();
642 }
643 return result;
644}
645
646EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
647 EGLint attribute, EGLint *value)
648{
649 clearError();
650
Jesse Hallb29e5e82012-04-04 16:53:42 -0700651 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700652 if (!dp) return EGL_FALSE;
653
Jesse Hallb29e5e82012-04-04 16:53:42 -0700654 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700655 if (!_s.get())
656 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700657
Mathias Agopian518ec112011-05-13 16:21:08 -0700658 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800659 return s->cnx->egl.eglQuerySurface(
660 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700661}
662
Jamie Gennise8696a42012-01-15 18:54:57 -0800663void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800664 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800665 clearError();
666
Jesse Hallb29e5e82012-04-04 16:53:42 -0700667 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800668 if (!dp) {
669 return;
670 }
671
Jesse Hallb29e5e82012-04-04 16:53:42 -0700672 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800673 if (!_s.get()) {
674 setError(EGL_BAD_SURFACE, EGL_FALSE);
675 return;
676 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800677}
678
Mathias Agopian518ec112011-05-13 16:21:08 -0700679// ----------------------------------------------------------------------------
680// Contexts
681// ----------------------------------------------------------------------------
682
683EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
684 EGLContext share_list, const EGLint *attrib_list)
685{
686 clearError();
687
Jesse Hallb29e5e82012-04-04 16:53:42 -0700688 egl_connection_t* cnx = NULL;
689 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700690 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700691 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700692 if (!ContextRef(dp.get(), share_list).get()) {
693 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
694 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700695 egl_context_t* const c = get_context(share_list);
696 share_list = c->context;
697 }
698 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800699 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700700 if (context != EGL_NO_CONTEXT) {
701 // figure out if it's a GLESv1 or GLESv2
702 int version = 0;
703 if (attrib_list) {
704 while (*attrib_list != EGL_NONE) {
705 GLint attr = *attrib_list++;
706 GLint value = *attrib_list++;
707 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
708 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800709 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800710 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800711 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700712 }
713 }
714 };
715 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700716 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
717 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700718 return c;
719 }
720 }
721 return EGL_NO_CONTEXT;
722}
723
724EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
725{
726 clearError();
727
Jesse Hallb29e5e82012-04-04 16:53:42 -0700728 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700729 if (!dp)
730 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700731
Jesse Hallb29e5e82012-04-04 16:53:42 -0700732 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700733 if (!_c.get())
734 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800735
Mathias Agopian518ec112011-05-13 16:21:08 -0700736 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800737 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700738 if (result == EGL_TRUE) {
739 _c.terminate();
740 }
741 return result;
742}
743
Mathias Agopian518ec112011-05-13 16:21:08 -0700744EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
745 EGLSurface read, EGLContext ctx)
746{
747 clearError();
748
Jesse Hallb29e5e82012-04-04 16:53:42 -0700749 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700750 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
751
Mathias Agopian5b287a62011-05-16 18:58:55 -0700752 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
753 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
754 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700755 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
756 (draw != EGL_NO_SURFACE) ) {
757 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
758 }
759
760 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700761 ContextRef _c(dp.get(), ctx);
762 SurfaceRef _d(dp.get(), draw);
763 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700764
765 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700766 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700768 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 }
770
771 // these are the underlying implementation's object
772 EGLContext impl_ctx = EGL_NO_CONTEXT;
773 EGLSurface impl_draw = EGL_NO_SURFACE;
774 EGLSurface impl_read = EGL_NO_SURFACE;
775
776 // these are our objects structs passed in
777 egl_context_t * c = NULL;
778 egl_surface_t const * d = NULL;
779 egl_surface_t const * r = NULL;
780
781 // these are the current objects structs
782 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800783
Mathias Agopian518ec112011-05-13 16:21:08 -0700784 if (ctx != EGL_NO_CONTEXT) {
785 c = get_context(ctx);
786 impl_ctx = c->context;
787 } else {
788 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700789 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
790 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
791 return setError(EGL_BAD_MATCH, EGL_FALSE);
792 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700793 if (cur_c == NULL) {
794 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700795 // not an error, there is just no current context.
796 return EGL_TRUE;
797 }
798 }
799
800 // retrieve the underlying implementation's draw EGLSurface
801 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700802 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700803 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700804 impl_draw = d->surface;
805 }
806
807 // retrieve the underlying implementation's read EGLSurface
808 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700809 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 impl_read = r->surface;
812 }
813
Mathias Agopian518ec112011-05-13 16:21:08 -0700814
Jesse Hallb29e5e82012-04-04 16:53:42 -0700815 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800816 draw, read, ctx,
817 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700818
819 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800820 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700821 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
822 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700823 _c.acquire();
824 _r.acquire();
825 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700826 } else {
827 setGLHooksThreadSpecific(&gHooksNoContext);
828 egl_tls_t::setContext(EGL_NO_CONTEXT);
829 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700830 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000831 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700832 egl_connection_t* const cnx = &gEGLImpl;
833 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700834 }
835 return result;
836}
837
838
839EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
840 EGLint attribute, EGLint *value)
841{
842 clearError();
843
Jesse Hallb29e5e82012-04-04 16:53:42 -0700844 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700845 if (!dp) return EGL_FALSE;
846
Jesse Hallb29e5e82012-04-04 16:53:42 -0700847 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700848 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
849
Mathias Agopian518ec112011-05-13 16:21:08 -0700850 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800851 return c->cnx->egl.eglQueryContext(
852 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700853
Mathias Agopian518ec112011-05-13 16:21:08 -0700854}
855
856EGLContext eglGetCurrentContext(void)
857{
858 // could be called before eglInitialize(), but we wouldn't have a context
859 // then, and this function would correctly return EGL_NO_CONTEXT.
860
861 clearError();
862
863 EGLContext ctx = getContext();
864 return ctx;
865}
866
867EGLSurface eglGetCurrentSurface(EGLint readdraw)
868{
869 // could be called before eglInitialize(), but we wouldn't have a context
870 // then, and this function would correctly return EGL_NO_SURFACE.
871
872 clearError();
873
874 EGLContext ctx = getContext();
875 if (ctx) {
876 egl_context_t const * const c = get_context(ctx);
877 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
878 switch (readdraw) {
879 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800880 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700881 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
882 }
883 }
884 return EGL_NO_SURFACE;
885}
886
887EGLDisplay eglGetCurrentDisplay(void)
888{
889 // could be called before eglInitialize(), but we wouldn't have a context
890 // then, and this function would correctly return EGL_NO_DISPLAY.
891
892 clearError();
893
894 EGLContext ctx = getContext();
895 if (ctx) {
896 egl_context_t const * const c = get_context(ctx);
897 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
898 return c->dpy;
899 }
900 return EGL_NO_DISPLAY;
901}
902
903EGLBoolean eglWaitGL(void)
904{
Mathias Agopian518ec112011-05-13 16:21:08 -0700905 clearError();
906
Mathias Agopianada798b2012-02-13 17:09:30 -0800907 egl_connection_t* const cnx = &gEGLImpl;
908 if (!cnx->dso)
909 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
910
911 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700912}
913
914EGLBoolean eglWaitNative(EGLint engine)
915{
Mathias Agopian518ec112011-05-13 16:21:08 -0700916 clearError();
917
Mathias Agopianada798b2012-02-13 17:09:30 -0800918 egl_connection_t* const cnx = &gEGLImpl;
919 if (!cnx->dso)
920 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
921
922 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700923}
924
925EGLint eglGetError(void)
926{
Mathias Agopianada798b2012-02-13 17:09:30 -0800927 EGLint err = EGL_SUCCESS;
928 egl_connection_t* const cnx = &gEGLImpl;
929 if (cnx->dso) {
930 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700931 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800932 if (err == EGL_SUCCESS) {
933 err = egl_tls_t::getError();
934 }
935 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700936}
937
Michael Chockc0ec5e22014-01-27 08:14:33 -0800938static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700939 const char* procname) {
940 const egl_connection_t* cnx = &gEGLImpl;
941 void* proc = NULL;
942
Michael Chockc0ec5e22014-01-27 08:14:33 -0800943 proc = dlsym(cnx->libEgl, procname);
944 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
945
Jesse Hallc07b5202013-07-04 12:08:16 -0700946 proc = dlsym(cnx->libGles2, procname);
947 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
948
949 proc = dlsym(cnx->libGles1, procname);
950 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
951
952 return NULL;
953}
954
Mathias Agopian518ec112011-05-13 16:21:08 -0700955__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
956{
957 // eglGetProcAddress() could be the very first function called
958 // in which case we must make sure we've initialized ourselves, this
959 // happens the first time egl_get_display() is called.
960
961 clearError();
962
963 if (egl_init_drivers() == EGL_FALSE) {
964 setError(EGL_BAD_PARAMETER, NULL);
965 return NULL;
966 }
967
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700968 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700969 return NULL;
970 }
971
Mathias Agopian518ec112011-05-13 16:21:08 -0700972 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700973 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700974 if (addr) return addr;
975
Michael Chockc0ec5e22014-01-27 08:14:33 -0800976 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700977 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700978
Mathias Agopian518ec112011-05-13 16:21:08 -0700979 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
980 pthread_mutex_lock(&sExtensionMapMutex);
981
982 /*
983 * Since eglGetProcAddress() is not associated to anything, it needs
984 * to return a function pointer that "works" regardless of what
985 * the current context is.
986 *
987 * For this reason, we return a "forwarder", a small stub that takes
988 * care of calling the function associated with the context
989 * currently bound.
990 *
991 * We first look for extensions we've already resolved, if we're seeing
992 * this extension for the first time, we go through all our
993 * implementations and call eglGetProcAddress() and record the
994 * result in the appropriate implementation hooks and return the
995 * address of the forwarder corresponding to that hook set.
996 *
997 */
998
999 const String8 name(procname);
1000 addr = sGLExtentionMap.valueFor(name);
1001 const int slot = sGLExtentionSlot;
1002
Steve Blocke6f43dd2012-01-06 19:20:56 +00001003 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001004 "no more slots for eglGetProcAddress(\"%s\")",
1005 procname);
1006
1007 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1008 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001009
1010 egl_connection_t* const cnx = &gEGLImpl;
1011 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001012 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001013 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001014 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1015 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001016 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001017 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001018 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001019
Mathias Agopian518ec112011-05-13 16:21:08 -07001020 if (found) {
1021 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001022 sGLExtentionMap.add(name, addr);
1023 sGLExtentionSlot++;
1024 }
1025 }
1026
1027 pthread_mutex_unlock(&sExtensionMapMutex);
1028 return addr;
1029}
1030
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001031class FrameCompletionThread : public Thread {
1032public:
1033
1034 static void queueSync(EGLSyncKHR sync) {
1035 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1036 static bool running = false;
1037 if (!running) {
1038 thread->run("GPUFrameCompletion");
1039 running = true;
1040 }
1041 {
1042 Mutex::Autolock lock(thread->mMutex);
1043 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1044 thread->mFramesQueued).string());
1045 thread->mQueue.push_back(sync);
1046 thread->mCondition.signal();
1047 thread->mFramesQueued++;
1048 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1049 }
1050 }
1051
1052private:
1053 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1054
1055 virtual bool threadLoop() {
1056 EGLSyncKHR sync;
1057 uint32_t frameNum;
1058 {
1059 Mutex::Autolock lock(mMutex);
1060 while (mQueue.isEmpty()) {
1061 mCondition.wait(mMutex);
1062 }
1063 sync = mQueue[0];
1064 frameNum = mFramesCompleted;
1065 }
1066 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1067 {
1068 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1069 frameNum).string());
1070 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1071 if (result == EGL_FALSE) {
1072 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1073 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1074 ALOGE("FrameCompletion: timeout waiting for fence");
1075 }
1076 eglDestroySyncKHR(dpy, sync);
1077 }
1078 {
1079 Mutex::Autolock lock(mMutex);
1080 mQueue.removeAt(0);
1081 mFramesCompleted++;
1082 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1083 }
1084 return true;
1085 }
1086
1087 uint32_t mFramesQueued;
1088 uint32_t mFramesCompleted;
1089 Vector<EGLSyncKHR> mQueue;
1090 Condition mCondition;
1091 Mutex mMutex;
1092};
1093
Dan Stozaa894d082015-02-19 15:27:36 -08001094EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1095 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001096{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001097 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001098 clearError();
1099
Jesse Hallb29e5e82012-04-04 16:53:42 -07001100 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001101 if (!dp) return EGL_FALSE;
1102
Jesse Hallb29e5e82012-04-04 16:53:42 -07001103 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001104 if (!_s.get())
1105 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001106
Mathias Agopian518ec112011-05-13 16:21:08 -07001107 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001108
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001109 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1110 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1111 if (sync != EGL_NO_SYNC_KHR) {
1112 FrameCompletionThread::queueSync(sync);
1113 }
1114 }
1115
Mathias Agopian7db993a2012-03-25 00:49:46 -07001116 if (CC_UNLIKELY(dp->finishOnSwap)) {
1117 uint32_t pixel;
1118 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1119 if (c) {
1120 // glReadPixels() ensures that the frame is complete
1121 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1122 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1123 }
1124 }
1125
Dan Stozaa894d082015-02-19 15:27:36 -08001126 if (n_rects == 0) {
1127 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1128 }
1129
1130 Vector<android_native_rect_t> androidRects;
1131 for (int r = 0; r < n_rects; ++r) {
1132 int offset = r * 4;
1133 int x = rects[offset];
1134 int y = rects[offset + 1];
1135 int width = rects[offset + 2];
1136 int height = rects[offset + 3];
1137 android_native_rect_t androidRect;
1138 androidRect.left = x;
1139 androidRect.top = y + height;
1140 androidRect.right = x + width;
1141 androidRect.bottom = y;
1142 androidRects.push_back(androidRect);
1143 }
1144 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1145 androidRects.size());
1146
1147 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1148 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1149 rects, n_rects);
1150 } else {
1151 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1152 }
1153}
1154
1155EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1156{
1157 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001158}
1159
1160EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1161 NativePixmapType target)
1162{
1163 clearError();
1164
Jesse Hallb29e5e82012-04-04 16:53:42 -07001165 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001166 if (!dp) return EGL_FALSE;
1167
Jesse Hallb29e5e82012-04-04 16:53:42 -07001168 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001169 if (!_s.get())
1170 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001171
Mathias Agopian518ec112011-05-13 16:21:08 -07001172 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001173 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001174}
1175
1176const char* eglQueryString(EGLDisplay dpy, EGLint name)
1177{
1178 clearError();
1179
Chia-I Wue57d1352016-08-15 16:10:02 +08001180 // Generate an error quietly when client extensions (as defined by
1181 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1182 // validate_display to generate the error as validate_display would log
1183 // the error, which can be misleading.
1184 //
1185 // If we want to support EGL_EXT_client_extensions later, we can return
1186 // the client extension string here instead.
1187 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1188 return setErrorQuiet(EGL_BAD_DISPLAY, nullptr);
1189
Jesse Hallb29e5e82012-04-04 16:53:42 -07001190 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001191 if (!dp) return (const char *) NULL;
1192
1193 switch (name) {
1194 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001195 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001196 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001197 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001199 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001200 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001201 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001202 }
1203 return setError(EGL_BAD_PARAMETER, (const char *)0);
1204}
1205
Mathias Agopianca088332013-03-28 17:44:13 -07001206EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1207{
1208 clearError();
1209
1210 const egl_display_ptr dp = validate_display(dpy);
1211 if (!dp) return (const char *) NULL;
1212
1213 switch (name) {
1214 case EGL_VENDOR:
1215 return dp->disp.queryString.vendor;
1216 case EGL_VERSION:
1217 return dp->disp.queryString.version;
1218 case EGL_EXTENSIONS:
1219 return dp->disp.queryString.extensions;
1220 case EGL_CLIENT_APIS:
1221 return dp->disp.queryString.clientApi;
1222 }
1223 return setError(EGL_BAD_PARAMETER, (const char *)0);
1224}
Mathias Agopian518ec112011-05-13 16:21:08 -07001225
1226// ----------------------------------------------------------------------------
1227// EGL 1.1
1228// ----------------------------------------------------------------------------
1229
1230EGLBoolean eglSurfaceAttrib(
1231 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1232{
1233 clearError();
1234
Jesse Hallb29e5e82012-04-04 16:53:42 -07001235 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001236 if (!dp) return EGL_FALSE;
1237
Jesse Hallb29e5e82012-04-04 16:53:42 -07001238 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001239 if (!_s.get())
1240 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001241
Pablo Ceballosc18be292016-05-31 14:55:42 -07001242 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001243
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001244 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001245 if (!s->win.get()) {
1246 setError(EGL_BAD_SURFACE, EGL_FALSE);
1247 }
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001248 int err = native_window_set_auto_refresh(s->win.get(),
1249 value ? true : false);
1250 return (err == NO_ERROR) ? EGL_TRUE :
1251 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001252 }
1253
Pablo Ceballosc18be292016-05-31 14:55:42 -07001254 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001255 if (!s->win.get()) {
1256 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1257 }
Brian Anderson069b3652016-07-22 10:32:47 -07001258 int err = native_window_enable_frame_timestamps(
1259 s->win.get(), value ? true : false);
1260 return (err == NO_ERROR) ? EGL_TRUE :
1261 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001262 }
1263
Mathias Agopian518ec112011-05-13 16:21:08 -07001264 if (s->cnx->egl.eglSurfaceAttrib) {
1265 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001266 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001267 }
1268 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1269}
1270
1271EGLBoolean eglBindTexImage(
1272 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1273{
1274 clearError();
1275
Jesse Hallb29e5e82012-04-04 16:53:42 -07001276 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001277 if (!dp) return EGL_FALSE;
1278
Jesse Hallb29e5e82012-04-04 16:53:42 -07001279 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001280 if (!_s.get())
1281 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001282
Mathias Agopian518ec112011-05-13 16:21:08 -07001283 egl_surface_t const * const s = get_surface(surface);
1284 if (s->cnx->egl.eglBindTexImage) {
1285 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001286 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001287 }
1288 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1289}
1290
1291EGLBoolean eglReleaseTexImage(
1292 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1293{
1294 clearError();
1295
Jesse Hallb29e5e82012-04-04 16:53:42 -07001296 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001297 if (!dp) return EGL_FALSE;
1298
Jesse Hallb29e5e82012-04-04 16:53:42 -07001299 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001300 if (!_s.get())
1301 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001302
Mathias Agopian518ec112011-05-13 16:21:08 -07001303 egl_surface_t const * const s = get_surface(surface);
1304 if (s->cnx->egl.eglReleaseTexImage) {
1305 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001306 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001307 }
1308 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1309}
1310
1311EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1312{
1313 clearError();
1314
Jesse Hallb29e5e82012-04-04 16:53:42 -07001315 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001316 if (!dp) return EGL_FALSE;
1317
1318 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001319 egl_connection_t* const cnx = &gEGLImpl;
1320 if (cnx->dso && cnx->egl.eglSwapInterval) {
1321 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001322 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001323
Mathias Agopian518ec112011-05-13 16:21:08 -07001324 return res;
1325}
1326
1327
1328// ----------------------------------------------------------------------------
1329// EGL 1.2
1330// ----------------------------------------------------------------------------
1331
1332EGLBoolean eglWaitClient(void)
1333{
1334 clearError();
1335
Mathias Agopianada798b2012-02-13 17:09:30 -08001336 egl_connection_t* const cnx = &gEGLImpl;
1337 if (!cnx->dso)
1338 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1339
1340 EGLBoolean res;
1341 if (cnx->egl.eglWaitClient) {
1342 res = cnx->egl.eglWaitClient();
1343 } else {
1344 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001345 }
1346 return res;
1347}
1348
1349EGLBoolean eglBindAPI(EGLenum api)
1350{
1351 clearError();
1352
1353 if (egl_init_drivers() == EGL_FALSE) {
1354 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1355 }
1356
1357 // bind this API on all EGLs
1358 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001359 egl_connection_t* const cnx = &gEGLImpl;
1360 if (cnx->dso && cnx->egl.eglBindAPI) {
1361 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001362 }
1363 return res;
1364}
1365
1366EGLenum eglQueryAPI(void)
1367{
1368 clearError();
1369
1370 if (egl_init_drivers() == EGL_FALSE) {
1371 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1372 }
1373
Mathias Agopianada798b2012-02-13 17:09:30 -08001374 egl_connection_t* const cnx = &gEGLImpl;
1375 if (cnx->dso && cnx->egl.eglQueryAPI) {
1376 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001377 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001378
Mathias Agopian518ec112011-05-13 16:21:08 -07001379 // or, it can only be OpenGL ES
1380 return EGL_OPENGL_ES_API;
1381}
1382
1383EGLBoolean eglReleaseThread(void)
1384{
1385 clearError();
1386
Mathias Agopianada798b2012-02-13 17:09:30 -08001387 egl_connection_t* const cnx = &gEGLImpl;
1388 if (cnx->dso && cnx->egl.eglReleaseThread) {
1389 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001390 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301391
1392 // If there is context bound to the thread, release it
1393 egl_display_t::loseCurrent(get_context(getContext()));
1394
Mathias Agopian518ec112011-05-13 16:21:08 -07001395 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001396 return EGL_TRUE;
1397}
1398
1399EGLSurface eglCreatePbufferFromClientBuffer(
1400 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1401 EGLConfig config, const EGLint *attrib_list)
1402{
1403 clearError();
1404
Jesse Hallb29e5e82012-04-04 16:53:42 -07001405 egl_connection_t* cnx = NULL;
1406 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1407 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001408 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1409 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001410 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001411 }
1412 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1413}
1414
1415// ----------------------------------------------------------------------------
1416// EGL_EGLEXT_VERSION 3
1417// ----------------------------------------------------------------------------
1418
1419EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1420 const EGLint *attrib_list)
1421{
1422 clearError();
1423
Jesse Hallb29e5e82012-04-04 16:53:42 -07001424 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 if (!dp) return EGL_FALSE;
1426
Jesse Hallb29e5e82012-04-04 16:53:42 -07001427 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001428 if (!_s.get())
1429 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430
1431 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001432 if (s->cnx->egl.eglLockSurfaceKHR) {
1433 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001434 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001435 }
1436 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1437}
1438
1439EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1440{
1441 clearError();
1442
Jesse Hallb29e5e82012-04-04 16:53:42 -07001443 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001444 if (!dp) return EGL_FALSE;
1445
Jesse Hallb29e5e82012-04-04 16:53:42 -07001446 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001447 if (!_s.get())
1448 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001449
1450 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001451 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001452 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001453 }
1454 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1455}
1456
1457EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1458 EGLClientBuffer buffer, const EGLint *attrib_list)
1459{
1460 clearError();
1461
Jesse Hallb29e5e82012-04-04 16:53:42 -07001462 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001463 if (!dp) return EGL_NO_IMAGE_KHR;
1464
Jesse Hallb29e5e82012-04-04 16:53:42 -07001465 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001466 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001467
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001468 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1469 egl_connection_t* const cnx = &gEGLImpl;
1470 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1471 result = cnx->egl.eglCreateImageKHR(
1472 dp->disp.dpy,
1473 c ? c->context : EGL_NO_CONTEXT,
1474 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001475 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001476 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001477}
1478
1479EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1480{
1481 clearError();
1482
Jesse Hallb29e5e82012-04-04 16:53:42 -07001483 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001484 if (!dp) return EGL_FALSE;
1485
Steven Holte646a5c52012-06-04 20:02:11 -07001486 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001487 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001488 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001489 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001490 }
Steven Holte646a5c52012-06-04 20:02:11 -07001491 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001492}
1493
1494// ----------------------------------------------------------------------------
1495// EGL_EGLEXT_VERSION 5
1496// ----------------------------------------------------------------------------
1497
1498
1499EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1500{
1501 clearError();
1502
Jesse Hallb29e5e82012-04-04 16:53:42 -07001503 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001504 if (!dp) return EGL_NO_SYNC_KHR;
1505
Mathias Agopian518ec112011-05-13 16:21:08 -07001506 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001507 egl_connection_t* const cnx = &gEGLImpl;
1508 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1509 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001510 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001511 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001512}
1513
1514EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1515{
1516 clearError();
1517
Jesse Hallb29e5e82012-04-04 16:53:42 -07001518 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001519 if (!dp) return EGL_FALSE;
1520
Mathias Agopian518ec112011-05-13 16:21:08 -07001521 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001522 egl_connection_t* const cnx = &gEGLImpl;
1523 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1524 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001525 }
1526 return result;
1527}
1528
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001529EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1530 clearError();
1531
1532 const egl_display_ptr dp = validate_display(dpy);
1533 if (!dp) return EGL_FALSE;
1534
1535 EGLBoolean result = EGL_FALSE;
1536 egl_connection_t* const cnx = &gEGLImpl;
1537 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1538 result = cnx->egl.eglSignalSyncKHR(
1539 dp->disp.dpy, sync, mode);
1540 }
1541 return result;
1542}
1543
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001544EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1545 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001546{
1547 clearError();
1548
Jesse Hallb29e5e82012-04-04 16:53:42 -07001549 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001550 if (!dp) return EGL_FALSE;
1551
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001552 EGLBoolean result = EGL_FALSE;
1553 egl_connection_t* const cnx = &gEGLImpl;
1554 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1555 result = cnx->egl.eglClientWaitSyncKHR(
1556 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001557 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001558 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001559}
1560
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001561EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1562 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001563{
1564 clearError();
1565
Jesse Hallb29e5e82012-04-04 16:53:42 -07001566 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001567 if (!dp) return EGL_FALSE;
1568
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001569 EGLBoolean result = EGL_FALSE;
1570 egl_connection_t* const cnx = &gEGLImpl;
1571 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1572 result = cnx->egl.eglGetSyncAttribKHR(
1573 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001574 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001575 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001576}
1577
Season Li000d88f2015-07-01 11:39:40 -07001578EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1579{
1580 clearError();
1581
1582 const egl_display_ptr dp = validate_display(dpy);
1583 if (!dp) return EGL_NO_STREAM_KHR;
1584
1585 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1586 egl_connection_t* const cnx = &gEGLImpl;
1587 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1588 result = cnx->egl.eglCreateStreamKHR(
1589 dp->disp.dpy, attrib_list);
1590 }
1591 return result;
1592}
1593
1594EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1595{
1596 clearError();
1597
1598 const egl_display_ptr dp = validate_display(dpy);
1599 if (!dp) return EGL_FALSE;
1600
1601 EGLBoolean result = EGL_FALSE;
1602 egl_connection_t* const cnx = &gEGLImpl;
1603 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1604 result = cnx->egl.eglDestroyStreamKHR(
1605 dp->disp.dpy, stream);
1606 }
1607 return result;
1608}
1609
1610EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1611 EGLenum attribute, EGLint value)
1612{
1613 clearError();
1614
1615 const egl_display_ptr dp = validate_display(dpy);
1616 if (!dp) return EGL_FALSE;
1617
1618 EGLBoolean result = EGL_FALSE;
1619 egl_connection_t* const cnx = &gEGLImpl;
1620 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1621 result = cnx->egl.eglStreamAttribKHR(
1622 dp->disp.dpy, stream, attribute, value);
1623 }
1624 return result;
1625}
1626
1627EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1628 EGLenum attribute, EGLint *value)
1629{
1630 clearError();
1631
1632 const egl_display_ptr dp = validate_display(dpy);
1633 if (!dp) return EGL_FALSE;
1634
1635 EGLBoolean result = EGL_FALSE;
1636 egl_connection_t* const cnx = &gEGLImpl;
1637 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1638 result = cnx->egl.eglQueryStreamKHR(
1639 dp->disp.dpy, stream, attribute, value);
1640 }
1641 return result;
1642}
1643
1644EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1645 EGLenum attribute, EGLuint64KHR *value)
1646{
1647 clearError();
1648
1649 const egl_display_ptr dp = validate_display(dpy);
1650 if (!dp) return EGL_FALSE;
1651
1652 EGLBoolean result = EGL_FALSE;
1653 egl_connection_t* const cnx = &gEGLImpl;
1654 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1655 result = cnx->egl.eglQueryStreamu64KHR(
1656 dp->disp.dpy, stream, attribute, value);
1657 }
1658 return result;
1659}
1660
1661EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1662 EGLenum attribute, EGLTimeKHR *value)
1663{
1664 clearError();
1665
1666 const egl_display_ptr dp = validate_display(dpy);
1667 if (!dp) return EGL_FALSE;
1668
1669 EGLBoolean result = EGL_FALSE;
1670 egl_connection_t* const cnx = &gEGLImpl;
1671 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1672 result = cnx->egl.eglQueryStreamTimeKHR(
1673 dp->disp.dpy, stream, attribute, value);
1674 }
1675 return result;
1676}
1677
1678EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1679 EGLStreamKHR stream, const EGLint *attrib_list)
1680{
1681 clearError();
1682
1683 egl_display_ptr dp = validate_display(dpy);
1684 if (!dp) return EGL_NO_SURFACE;
1685
1686 egl_connection_t* const cnx = &gEGLImpl;
1687 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1688 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1689 dp->disp.dpy, config, stream, attrib_list);
1690 if (surface != EGL_NO_SURFACE) {
1691 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1692 surface, cnx);
1693 return s;
1694 }
1695 }
1696 return EGL_NO_SURFACE;
1697}
1698
1699EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1700 EGLStreamKHR stream)
1701{
1702 clearError();
1703
1704 const egl_display_ptr dp = validate_display(dpy);
1705 if (!dp) return EGL_FALSE;
1706
1707 EGLBoolean result = EGL_FALSE;
1708 egl_connection_t* const cnx = &gEGLImpl;
1709 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1710 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1711 dp->disp.dpy, stream);
1712 }
1713 return result;
1714}
1715
1716EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1717 EGLStreamKHR stream)
1718{
1719 clearError();
1720
1721 const egl_display_ptr dp = validate_display(dpy);
1722 if (!dp) return EGL_FALSE;
1723
1724 EGLBoolean result = EGL_FALSE;
1725 egl_connection_t* const cnx = &gEGLImpl;
1726 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1727 result = cnx->egl.eglStreamConsumerAcquireKHR(
1728 dp->disp.dpy, stream);
1729 }
1730 return result;
1731}
1732
1733EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1734 EGLStreamKHR stream)
1735{
1736 clearError();
1737
1738 const egl_display_ptr dp = validate_display(dpy);
1739 if (!dp) return EGL_FALSE;
1740
1741 EGLBoolean result = EGL_FALSE;
1742 egl_connection_t* const cnx = &gEGLImpl;
1743 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1744 result = cnx->egl.eglStreamConsumerReleaseKHR(
1745 dp->disp.dpy, stream);
1746 }
1747 return result;
1748}
1749
1750EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1751 EGLDisplay dpy, EGLStreamKHR stream)
1752{
1753 clearError();
1754
1755 const egl_display_ptr dp = validate_display(dpy);
1756 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1757
1758 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1759 egl_connection_t* const cnx = &gEGLImpl;
1760 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1761 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1762 dp->disp.dpy, stream);
1763 }
1764 return result;
1765}
1766
1767EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1768 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1769{
1770 clearError();
1771
1772 const egl_display_ptr dp = validate_display(dpy);
1773 if (!dp) return EGL_NO_STREAM_KHR;
1774
1775 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1776 egl_connection_t* const cnx = &gEGLImpl;
1777 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1778 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1779 dp->disp.dpy, file_descriptor);
1780 }
1781 return result;
1782}
1783
Mathias Agopian518ec112011-05-13 16:21:08 -07001784// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001785// EGL_EGLEXT_VERSION 15
1786// ----------------------------------------------------------------------------
1787
1788EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1789 clearError();
1790 const egl_display_ptr dp = validate_display(dpy);
1791 if (!dp) return EGL_FALSE;
1792 EGLint result = EGL_FALSE;
1793 egl_connection_t* const cnx = &gEGLImpl;
1794 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1795 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1796 }
1797 return result;
1798}
1799
1800// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001801// ANDROID extensions
1802// ----------------------------------------------------------------------------
1803
Jamie Gennis331841b2012-09-06 14:52:00 -07001804EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1805{
1806 clearError();
1807
1808 const egl_display_ptr dp = validate_display(dpy);
1809 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1810
1811 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1812 egl_connection_t* const cnx = &gEGLImpl;
1813 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1814 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1815 }
1816 return result;
1817}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001818
Andy McFadden72841452013-03-01 16:25:32 -08001819EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1820 EGLnsecsANDROID time)
1821{
1822 clearError();
1823
1824 const egl_display_ptr dp = validate_display(dpy);
1825 if (!dp) {
1826 return EGL_FALSE;
1827 }
1828
1829 SurfaceRef _s(dp.get(), surface);
1830 if (!_s.get()) {
1831 setError(EGL_BAD_SURFACE, EGL_FALSE);
1832 return EGL_FALSE;
1833 }
1834
1835 egl_surface_t const * const s = get_surface(surface);
1836 native_window_set_buffers_timestamp(s->win.get(), time);
1837
1838 return EGL_TRUE;
1839}
1840
Craig Donner05249fc2016-01-15 19:33:55 -08001841EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1842{
1843 clearError();
1844
Craig Donnere96a3252017-02-02 12:13:34 -08001845 uint64_t producerUsage = 0;
1846 uint64_t consumerUsage = 0;
Craig Donner05249fc2016-01-15 19:33:55 -08001847 uint32_t width = 0;
1848 uint32_t height = 0;
1849 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001850 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001851 uint32_t red_size = 0;
1852 uint32_t green_size = 0;
1853 uint32_t blue_size = 0;
1854 uint32_t alpha_size = 0;
1855
Craig Donnerb40504a2016-06-03 17:54:25 -07001856#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001857 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001858 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001859 target = value; \
1860 } else { \
1861 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1862 } \
1863 break
1864
1865 if (attrib_list) {
1866 while (*attrib_list != EGL_NONE) {
1867 GLint attr = *attrib_list++;
1868 GLint value = *attrib_list++;
1869 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001870 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1871 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1872 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1873 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1874 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1875 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001876 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001877 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1878 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001879 producerUsage |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001880 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001881 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001882 producerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
Craig Donner05249fc2016-01-15 19:33:55 -08001883 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001884 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001885 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
Craig Donner05249fc2016-01-15 19:33:55 -08001886 }
Craig Donner05249fc2016-01-15 19:33:55 -08001887 break;
1888 default:
1889 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1890 }
1891 }
1892 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001893#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001894
1895 // Validate format.
1896 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1897 if (alpha_size == 8) {
1898 format = HAL_PIXEL_FORMAT_RGBA_8888;
1899 } else {
1900 format = HAL_PIXEL_FORMAT_RGB_888;
1901 }
1902 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1903 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001904 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001905 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001906 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001907 red_size, green_size, blue_size, alpha_size);
1908 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1909 }
1910
Craig Donner68671532016-07-18 10:19:54 -07001911#define CHECK_ERROR_CONDITION(message) \
1912 if (err != NO_ERROR) { \
1913 ALOGE(message); \
1914 goto error_condition; \
1915 }
1916
1917 // The holder is used to destroy the buffer if an error occurs.
1918 GraphicBuffer* gBuffer = new GraphicBuffer();
1919 sp<IServiceManager> sm = defaultServiceManager();
1920 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1921 sp<IBinder> allocator;
1922 Parcel sc_data, sc_reply, data, reply;
1923 status_t err = NO_ERROR;
1924 if (sm == NULL) {
1925 ALOGE("Unable to connect to ServiceManager");
1926 goto error_condition;
1927 }
1928
1929 // Obtain an allocator.
1930 if (surfaceFlinger == NULL) {
1931 ALOGE("Unable to connect to SurfaceFlinger");
1932 goto error_condition;
1933 }
1934 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1935 err = surfaceFlinger->transact(
1936 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1937 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1938 allocator = sc_reply.readStrongBinder();
1939
1940 if (allocator == NULL) {
1941 ALOGE("Unable to obtain an ISurfaceComposer");
1942 goto error_condition;
1943 }
1944 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1945 err = data.writeUint32(width);
1946 CHECK_ERROR_CONDITION("Unable to write width");
1947 err = data.writeUint32(height);
1948 CHECK_ERROR_CONDITION("Unable to write height");
1949 err = data.writeInt32(static_cast<int32_t>(format));
1950 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001951 err = data.writeUint32(layer_count);
1952 CHECK_ERROR_CONDITION("Unable to write layer count");
Craig Donnere96a3252017-02-02 12:13:34 -08001953 err = data.writeUint64(producerUsage);
1954 CHECK_ERROR_CONDITION("Unable to write producer usage");
1955 err = data.writeUint64(consumerUsage);
1956 CHECK_ERROR_CONDITION("Unable to write consumer usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001957 err = data.writeUtf8AsUtf16(
1958 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1959 std::to_string(getpid()) + ']');
1960 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001961 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1962 &reply);
1963 CHECK_ERROR_CONDITION(
1964 "Unable to request buffer allocation from surface composer");
1965 err = reply.readInt32();
1966 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1967 err = reply.read(*gBuffer);
1968 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1969
1970 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001971 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001972 ALOGE("Unable to create native buffer "
Craig Donnere96a3252017-02-02 12:13:34 -08001973 "{ w=%u, h=%u, f=%u, pu=%" PRIx64 " cu=%" PRIx64 ", lc=%u} %#x",
1974 width, height, format, producerUsage, consumerUsage,
1975 layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001976 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001977 }
Craig Donnere96a3252017-02-02 12:13:34 -08001978 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, pu=%" PRIx64
1979 " cu=%" PRIx64 ", lc=%u}",
1980 gBuffer, width, height, format, producerUsage, consumerUsage,
1981 layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001982 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001983
1984#undef CHECK_ERROR_CONDITION
1985
1986error_condition:
1987 // Delete the buffer.
1988 sp<GraphicBuffer> holder(gBuffer);
1989 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001990}
1991
Craig Donner60761072017-01-27 12:30:44 -08001992EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1993 clearError();
1994
1995 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1996
Mathias Agopian89ed4c82017-02-09 18:48:34 -08001997 const GraphicBuffer* graphicBuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
Craig Donner60761072017-01-27 12:30:44 -08001998 return static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
1999}
2000
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002001// ----------------------------------------------------------------------------
2002// NVIDIA extensions
2003// ----------------------------------------------------------------------------
2004EGLuint64NV eglGetSystemTimeFrequencyNV()
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.eglGetSystemTimeFrequencyNV) {
2016 return cnx->egl.eglGetSystemTimeFrequencyNV();
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}
2021
2022EGLuint64NV eglGetSystemTimeNV()
2023{
2024 clearError();
2025
2026 if (egl_init_drivers() == EGL_FALSE) {
2027 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2028 }
2029
2030 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002031 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002032
Mathias Agopianada798b2012-02-13 17:09:30 -08002033 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2034 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002035 }
2036
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07002037 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002038}
Dan Stozaa894d082015-02-19 15:27:36 -08002039
2040// ----------------------------------------------------------------------------
2041// Partial update extension
2042// ----------------------------------------------------------------------------
2043EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2044 EGLint *rects, EGLint n_rects)
2045{
2046 clearError();
2047
2048 const egl_display_ptr dp = validate_display(dpy);
2049 if (!dp) {
2050 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2051 return EGL_FALSE;
2052 }
2053
2054 SurfaceRef _s(dp.get(), surface);
2055 if (!_s.get()) {
2056 setError(EGL_BAD_SURFACE, EGL_FALSE);
2057 return EGL_FALSE;
2058 }
2059
2060 egl_surface_t const * const s = get_surface(surface);
2061 if (s->cnx->egl.eglSetDamageRegionKHR) {
2062 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2063 rects, n_rects);
2064 }
2065
2066 return EGL_FALSE;
2067}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002068
Brian Anderson1049d1d2016-12-16 17:25:57 -08002069EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2070 EGLuint64KHR *frameId) {
2071 clearError();
2072
2073 const egl_display_ptr dp = validate_display(dpy);
2074 if (!dp) {
2075 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2076 }
2077
2078 SurfaceRef _s(dp.get(), surface);
2079 if (!_s.get()) {
2080 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2081 }
2082
2083 egl_surface_t const * const s = get_surface(surface);
2084
2085 if (!s->win.get()) {
2086 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2087 }
2088
2089 uint64_t nextFrameId = 0;
2090 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2091
2092 if (ret != NO_ERROR) {
2093 // This should not happen. Return an error that is not in the spec
2094 // so it's obvious something is very wrong.
2095 ALOGE("eglGetNextFrameId: Unexpected error.");
2096 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
2097 }
2098
2099 *frameId = nextFrameId;
2100 return EGL_TRUE;
2101}
2102
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002103EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2104 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2105{
2106 clearError();
2107
2108 const egl_display_ptr dp = validate_display(dpy);
2109 if (!dp) {
2110 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2111 }
2112
2113 SurfaceRef _s(dp.get(), surface);
2114 if (!_s.get()) {
2115 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2116 }
2117
2118 egl_surface_t const * const s = get_surface(surface);
2119
2120 if (!s->win.get()) {
2121 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2122 }
2123
2124 nsecs_t* compositeDeadline = nullptr;
2125 nsecs_t* compositeInterval = nullptr;
2126 nsecs_t* compositeToPresentLatency = nullptr;
2127
2128 for (int i = 0; i < numTimestamps; i++) {
2129 switch (names[i]) {
2130 case EGL_COMPOSITE_DEADLINE_ANDROID:
2131 compositeDeadline = &values[i];
2132 break;
2133 case EGL_COMPOSITE_INTERVAL_ANDROID:
2134 compositeInterval = &values[i];
2135 break;
2136 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2137 compositeToPresentLatency = &values[i];
2138 break;
2139 default:
2140 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2141 }
2142 }
2143
2144 status_t ret = native_window_get_compositor_timing(s->win.get(),
2145 compositeDeadline, compositeInterval, compositeToPresentLatency);
2146
2147 switch (ret) {
2148 case NO_ERROR:
2149 return EGL_TRUE;
2150 case INVALID_OPERATION:
2151 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2152 default:
2153 // This should not happen. Return an error that is not in the spec
2154 // so it's obvious something is very wrong.
2155 ALOGE("eglGetCompositorTiming: Unexpected error.");
2156 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
2157 }
2158}
2159
2160EGLBoolean eglGetCompositorTimingSupportedANDROID(
2161 EGLDisplay dpy, EGLSurface surface, EGLint name)
2162{
2163 clearError();
2164
2165 const egl_display_ptr dp = validate_display(dpy);
2166 if (!dp) {
2167 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2168 }
2169
2170 SurfaceRef _s(dp.get(), surface);
2171 if (!_s.get()) {
2172 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2173 }
2174
2175 egl_surface_t const * const s = get_surface(surface);
2176
2177 ANativeWindow* window = s->win.get();
2178 if (!window) {
2179 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2180 }
2181
2182 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002183 case EGL_COMPOSITE_DEADLINE_ANDROID:
2184 case EGL_COMPOSITE_INTERVAL_ANDROID:
2185 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2186 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002187 default:
2188 return EGL_FALSE;
2189 }
2190}
2191
Pablo Ceballosc18be292016-05-31 14:55:42 -07002192EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002193 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002194 EGLnsecsANDROID *values)
2195{
2196 clearError();
2197
2198 const egl_display_ptr dp = validate_display(dpy);
2199 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002200 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002201 }
2202
2203 SurfaceRef _s(dp.get(), surface);
2204 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002205 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002206 }
2207
2208 egl_surface_t const * const s = get_surface(surface);
2209
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002210 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002211 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002212 }
2213
Brian Andersondbd0ea82016-07-22 09:38:59 -07002214 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002215 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002216 nsecs_t* latchTime = nullptr;
2217 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002218 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002219 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002220 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002221 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002222 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002223 nsecs_t* releaseTime = nullptr;
2224
2225 for (int i = 0; i < numTimestamps; i++) {
2226 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002227 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2228 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002229 break;
2230 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2231 acquireTime = &values[i];
2232 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002233 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2234 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002235 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002236 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2237 firstRefreshStartTime = &values[i];
2238 break;
2239 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2240 lastRefreshStartTime = &values[i];
2241 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002242 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2243 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002244 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002245 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2246 displayPresentTime = &values[i];
2247 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002248 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2249 displayRetireTime = &values[i];
2250 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002251 case EGL_DEQUEUE_READY_TIME_ANDROID:
2252 dequeueReadyTime = &values[i];
2253 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002254 case EGL_READS_DONE_TIME_ANDROID:
2255 releaseTime = &values[i];
2256 break;
2257 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002258 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002259 }
2260 }
2261
Brian Anderson1049d1d2016-12-16 17:25:57 -08002262 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002263 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002264 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002265 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002266
Brian Anderson069b3652016-07-22 10:32:47 -07002267 switch (ret) {
2268 case NO_ERROR:
2269 return EGL_TRUE;
2270 case NAME_NOT_FOUND:
2271 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002272 case INVALID_OPERATION:
2273 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002274 case BAD_VALUE:
2275 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2276 default:
2277 // This should not happen. Return an error that is not in the spec
2278 // so it's obvious something is very wrong.
Brian Anderson1049d1d2016-12-16 17:25:57 -08002279 ALOGE("eglGetFrameTimestamps: Unexpected error.");
Brian Anderson069b3652016-07-22 10:32:47 -07002280 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002281 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002282}
2283
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002284EGLBoolean eglGetFrameTimestampSupportedANDROID(
2285 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002286{
2287 clearError();
2288
2289 const egl_display_ptr dp = validate_display(dpy);
2290 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002291 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002292 }
2293
2294 SurfaceRef _s(dp.get(), surface);
2295 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002296 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2297 }
2298
2299 egl_surface_t const * const s = get_surface(surface);
2300
2301 ANativeWindow* window = s->win.get();
2302 if (!window) {
2303 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002304 }
2305
2306 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002307 case EGL_COMPOSITE_DEADLINE_ANDROID:
2308 case EGL_COMPOSITE_INTERVAL_ANDROID:
2309 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002310 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002311 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002312 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2313 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2314 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002315 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002316 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002317 case EGL_READS_DONE_TIME_ANDROID:
2318 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002319 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2320 int value = 0;
2321 window->query(window,
2322 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2323 return value == 0 ? EGL_FALSE : EGL_TRUE;
2324 }
2325 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2326 int value = 0;
2327 window->query(window,
2328 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2329 return value == 0 ? EGL_FALSE : EGL_TRUE;
2330 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002331 default:
2332 return EGL_FALSE;
2333 }
2334}