blob: d71837b44156c46c51431b4d81b5b18ca3268f88 [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
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
Craig Donner60761072017-01-27 12:30:44 -080029#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080030#include <private/android/AHardwareBufferHelpers.h>
31
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Craig Donner05249fc2016-01-15 19:33:55 -080036#include <ui/GraphicBuffer.h>
37
Mathias Agopian89ed4c82017-02-09 18:48:34 -080038
Mathias Agopian518ec112011-05-13 16:21:08 -070039#include <utils/KeyedVector.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070040#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080041#include <utils/Trace.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080042#include <utils/Thread.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070043
Mathias Agopian39c24a22013-04-04 23:17:56 -070044#include "../egl_impl.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070045
46#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070047#include "egl_object.h"
48#include "egl_tls.h"
49
50using namespace android;
51
52// ----------------------------------------------------------------------------
53
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070054namespace android {
55
Mathias Agopian518ec112011-05-13 16:21:08 -070056struct extention_map_t {
57 const char* name;
58 __eglMustCastToProperFunctionPointerType address;
59};
60
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070061/*
Jesse Hall21558da2013-08-06 15:31:22 -070062 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063 *
Jesse Hall21558da2013-08-06 15:31:22 -070064 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
65 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070066 *
Jesse Hall21558da2013-08-06 15:31:22 -070067 * The rest (gExtensionString) depend on support in the EGL driver, and are
68 * only available if the driver supports them. However, some of these must be
69 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080070 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070071 * the mandatory extensions are present and may not function properly if some
72 * are missing.
73 *
74 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070075 */
Mathias Agopian737b8962017-02-24 14:32:05 -080076
Mathias Agopian311b4792017-02-28 15:00:49 -080077extern char const * const gBuiltinExtensionString;
78extern char const * const gExtensionString;
Mathias Agopian737b8962017-02-24 14:32:05 -080079
Mathias Agopian311b4792017-02-28 15:00:49 -080080char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070081 "EGL_KHR_get_all_proc_addresses "
82 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080083 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080084 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080085 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070086 "EGL_ANDROID_get_frame_timestamps "
Jesse Hall21558da2013-08-06 15:31:22 -070087 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080088
89char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070090 "EGL_KHR_image " // mandatory
91 "EGL_KHR_image_base " // mandatory
92 "EGL_KHR_image_pixmap "
93 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -070094 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070096 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_gl_texture_cubemap_image "
98 "EGL_KHR_gl_renderbuffer_image "
99 "EGL_KHR_reusable_sync "
100 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700101 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700102 "EGL_KHR_config_attribs "
103 "EGL_KHR_surfaceless_context "
104 "EGL_KHR_stream "
105 "EGL_KHR_stream_fifo "
106 "EGL_KHR_stream_producer_eglsurface "
107 "EGL_KHR_stream_consumer_gltexture "
108 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700109 "EGL_EXT_create_context_robustness "
110 "EGL_NV_system_time "
111 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700112 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700113 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800114 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700115 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800116 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700117 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700118 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700119 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700120 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000121 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800122 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700123 ;
124
125// extensions not exposed to applications but used by the ANDROID system
126// "EGL_ANDROID_blob_cache " // strongly recommended
127// "EGL_IMG_hibernate_process " // optional
128// "EGL_ANDROID_native_fence_sync " // strongly recommended
129// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700130// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700131
132/*
133 * EGL Extensions entry-points exposed to 3rd party applications
134 * (keep in sync with gExtensionString above)
135 *
136 */
137static const extention_map_t sExtensionMap[] = {
138 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700139 { "eglLockSurfaceKHR",
140 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
141 { "eglUnlockSurfaceKHR",
142 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700143
144 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700145 { "eglCreateImageKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
147 { "eglDestroyImageKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700149
150 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
151 { "eglCreateSyncKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
153 { "eglDestroySyncKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
155 { "eglClientWaitSyncKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
157 { "eglSignalSyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
159 { "eglGetSyncAttribKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
161
162 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800163 { "eglGetSystemTimeFrequencyNV",
164 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
165 { "eglGetSystemTimeNV",
166 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700167
Mathias Agopian2bb71682013-03-27 17:32:41 -0700168 // EGL_KHR_wait_sync
169 { "eglWaitSyncKHR",
170 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700171
172 // EGL_ANDROID_presentation_time
173 { "eglPresentationTimeANDROID",
174 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800175
176 // EGL_KHR_swap_buffers_with_damage
177 { "eglSwapBuffersWithDamageKHR",
178 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
179
Craig Donner60761072017-01-27 12:30:44 -0800180 // EGL_ANDROID_get_native_client_buffer
181 { "eglGetNativeClientBufferANDROID",
182 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
183
Dan Stozaa894d082015-02-19 15:27:36 -0800184 // EGL_KHR_partial_update
185 { "eglSetDamageRegionKHR",
186 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700187
188 { "eglCreateStreamKHR",
189 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
190 { "eglDestroyStreamKHR",
191 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
192 { "eglStreamAttribKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
194 { "eglQueryStreamKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
196 { "eglQueryStreamu64KHR",
197 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
198 { "eglQueryStreamTimeKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
200 { "eglCreateStreamProducerSurfaceKHR",
201 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
202 { "eglStreamConsumerGLTextureExternalKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
204 { "eglStreamConsumerAcquireKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
206 { "eglStreamConsumerReleaseKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
208 { "eglGetStreamFileDescriptorKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
210 { "eglCreateStreamFromFileDescriptorKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700212
213 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800214 { "eglGetNextFrameIdANDROID",
215 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800216 { "eglGetCompositorTimingANDROID",
217 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
218 { "eglGetCompositorTimingSupportedANDROID",
219 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700220 { "eglGetFrameTimestampsANDROID",
221 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800222 { "eglGetFrameTimestampSupportedANDROID",
223 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700224};
225
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700226/*
227 * These extensions entry-points should not be exposed to applications.
228 * They're used internally by the Android EGL layer.
229 */
230#define FILTER_EXTENSIONS(procname) \
231 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
232 !strcmp((procname), "eglHibernateProcessIMG") || \
233 !strcmp((procname), "eglAwakenProcessIMG") || \
234 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
235
236
237
Mathias Agopian518ec112011-05-13 16:21:08 -0700238// accesses protected by sExtensionMapMutex
239static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
240static int sGLExtentionSlot = 0;
241static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
242
243static void(*findProcAddress(const char* name,
244 const extention_map_t* map, size_t n))() {
245 for (uint32_t i=0 ; i<n ; i++) {
246 if (!strcmp(name, map[i].name)) {
247 return map[i].address;
248 }
249 }
250 return NULL;
251}
252
253// ----------------------------------------------------------------------------
254
Mathias Agopian518ec112011-05-13 16:21:08 -0700255extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
256extern EGLBoolean egl_init_drivers();
257extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700258extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700259
Mathias Agopian518ec112011-05-13 16:21:08 -0700260} // namespace android;
261
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700262
Mathias Agopian518ec112011-05-13 16:21:08 -0700263// ----------------------------------------------------------------------------
264
265static inline void clearError() { egl_tls_t::clearError(); }
266static inline EGLContext getContext() { return egl_tls_t::getContext(); }
267
268// ----------------------------------------------------------------------------
269
270EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
271{
Jesse Hall1508ae62017-01-19 17:43:26 -0800272 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700273 clearError();
274
Dan Stozac3289c42014-01-17 11:38:34 -0800275 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700276 if (index >= NUM_DISPLAYS) {
277 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
278 }
279
280 if (egl_init_drivers() == EGL_FALSE) {
281 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
282 }
283
284 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
285 return dpy;
286}
287
288// ----------------------------------------------------------------------------
289// Initialization
290// ----------------------------------------------------------------------------
291
292EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
293{
294 clearError();
295
Jesse Hallb29e5e82012-04-04 16:53:42 -0700296 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800297 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700298
299 EGLBoolean res = dp->initialize(major, minor);
300
301 return res;
302}
303
304EGLBoolean eglTerminate(EGLDisplay dpy)
305{
306 // NOTE: don't unload the drivers b/c some APIs can be called
307 // after eglTerminate() has been called. eglTerminate() only
308 // terminates an EGLDisplay, not a EGL itself.
309
310 clearError();
311
Jesse Hallb29e5e82012-04-04 16:53:42 -0700312 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800313 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700314
315 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800316
Mathias Agopian518ec112011-05-13 16:21:08 -0700317 return res;
318}
319
320// ----------------------------------------------------------------------------
321// configuration
322// ----------------------------------------------------------------------------
323
324EGLBoolean eglGetConfigs( EGLDisplay dpy,
325 EGLConfig *configs,
326 EGLint config_size, EGLint *num_config)
327{
328 clearError();
329
Jesse Hallb29e5e82012-04-04 16:53:42 -0700330 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700331 if (!dp) return EGL_FALSE;
332
Mathias Agopian7773c432012-02-13 20:06:08 -0800333 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800334 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700335 }
336
Mathias Agopian7773c432012-02-13 20:06:08 -0800337 EGLBoolean res = EGL_FALSE;
338 *num_config = 0;
339
340 egl_connection_t* const cnx = &gEGLImpl;
341 if (cnx->dso) {
342 res = cnx->egl.eglGetConfigs(
343 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700344 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800345
346 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700347}
348
349EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
350 EGLConfig *configs, EGLint config_size,
351 EGLint *num_config)
352{
353 clearError();
354
Jesse Hallb29e5e82012-04-04 16:53:42 -0700355 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700356 if (!dp) return EGL_FALSE;
357
358 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800359 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700360 }
361
Mathias Agopian518ec112011-05-13 16:21:08 -0700362 EGLBoolean res = EGL_FALSE;
363 *num_config = 0;
364
Mathias Agopianada798b2012-02-13 17:09:30 -0800365 egl_connection_t* const cnx = &gEGLImpl;
366 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700367 if (attrib_list) {
368 char value[PROPERTY_VALUE_MAX];
369 property_get("debug.egl.force_msaa", value, "false");
370
371 if (!strcmp(value, "true")) {
372 size_t attribCount = 0;
373 EGLint attrib = attrib_list[0];
374
375 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700376 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700377 const EGLint *attribRendererable = NULL;
378 const EGLint *attribCaveat = NULL;
379
380 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700381 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700382 while (attrib != EGL_NONE) {
383 attrib = attrib_list[attribCount];
384 switch (attrib) {
385 case EGL_RENDERABLE_TYPE:
386 attribRendererable = &attrib_list[attribCount];
387 break;
388 case EGL_CONFIG_CAVEAT:
389 attribCaveat = &attrib_list[attribCount];
390 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800391 default:
392 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700393 }
394 attribCount++;
395 }
396
397 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
398 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800399
Romain Guy1cffc802012-10-15 18:13:05 -0700400 // Insert 2 extra attributes to force-enable MSAA 4x
401 EGLint aaAttribs[attribCount + 4];
402 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
403 aaAttribs[1] = 1;
404 aaAttribs[2] = EGL_SAMPLES;
405 aaAttribs[3] = 4;
406
407 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
408
409 EGLint numConfigAA;
410 EGLBoolean resAA = cnx->egl.eglChooseConfig(
411 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
412
413 if (resAA == EGL_TRUE && numConfigAA > 0) {
414 ALOGD("Enabling MSAA 4x");
415 *num_config = numConfigAA;
416 return resAA;
417 }
418 }
419 }
420 }
421
Mathias Agopian7773c432012-02-13 20:06:08 -0800422 res = cnx->egl.eglChooseConfig(
423 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700424 }
425 return res;
426}
427
428EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
429 EGLint attribute, EGLint *value)
430{
431 clearError();
432
Jesse Hallb29e5e82012-04-04 16:53:42 -0700433 egl_connection_t* cnx = NULL;
434 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
435 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800436
Mathias Agopian518ec112011-05-13 16:21:08 -0700437 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800438 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700439}
440
441// ----------------------------------------------------------------------------
442// surfaces
443// ----------------------------------------------------------------------------
444
Jesse Hallc2e41222013-08-08 13:40:22 -0700445// Turn linear formats into corresponding sRGB formats when colorspace is
446// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
447// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800448// the modification isn't possible, the original dataSpace is returned.
449static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
450 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700451 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800452 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700453 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800454 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700455 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800456 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700457}
458
Mathias Agopian518ec112011-05-13 16:21:08 -0700459EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
460 NativeWindowType window,
461 const EGLint *attrib_list)
462{
463 clearError();
464
Jesse Hallb29e5e82012-04-04 16:53:42 -0700465 egl_connection_t* cnx = NULL;
466 egl_display_ptr dp = validate_display_connection(dpy, cnx);
467 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800468 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700469
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800470 if (!window) {
471 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
472 }
473
474 int value = 0;
475 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
476 if (!value) {
477 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
478 }
479
Andy McFaddend566ce32014-01-07 15:54:17 -0800480 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
481 if (result != OK) {
482 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
483 "failed (%#x) (already connected to another API?)",
484 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700485 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700486 }
487
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700488 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700489 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
490 // of our native format. So if sRGB gamma is requested, we have to
491 // modify the EGLconfig's format before setting the native window's
492 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800493
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700494 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
495 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
496 &componentType);
497
Mathias Agopian95921562017-02-24 14:31:31 -0800498 EGLint format;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800499 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700500 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700501 EGLint r, g, b;
502 r = g = b = 0;
503 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
504 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
505 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700506 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700507 EGLint colorDepth = r + g + b;
508
509 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700510 if (colorDepth <= 16) {
511 format = HAL_PIXEL_FORMAT_RGB_565;
512 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700513 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
514 if (colorDepth > 24) {
515 format = HAL_PIXEL_FORMAT_RGBA_1010102;
516 } else {
517 format = HAL_PIXEL_FORMAT_RGBX_8888;
518 }
519 } else {
520 format = HAL_PIXEL_FORMAT_RGBA_FP16;
521 }
522 }
523 } else {
524 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
525 if (colorDepth > 24) {
526 format = HAL_PIXEL_FORMAT_RGBA_1010102;
527 } else {
528 format = HAL_PIXEL_FORMAT_RGBA_8888;
529 }
530 } else {
531 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700532 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700533 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700534
535 // now select a corresponding sRGB format if needed
536 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
537 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
538 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530539 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700540 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700541 }
542 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800543
Jesse Hallc2e41222013-08-08 13:40:22 -0700544 if (format != 0) {
545 int err = native_window_set_buffers_format(window, format);
546 if (err != 0) {
547 ALOGE("error setting native window pixel format: %s (%d)",
548 strerror(-err), err);
549 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
550 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
551 }
552 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700553
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800554 if (dataSpace != 0) {
555 int err = native_window_set_buffers_data_space(window, dataSpace);
556 if (err != 0) {
557 ALOGE("error setting native window pixel dataSpace: %s (%d)",
558 strerror(-err), err);
559 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
560 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
561 }
562 }
563
Jamie Gennis59769462011-11-19 18:04:43 -0800564 // the EGL spec requires that a new EGLSurface default to swap interval
565 // 1, so explicitly set that on the window here.
566 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
567 anw->setSwapInterval(anw, 1);
568
Mathias Agopian518ec112011-05-13 16:21:08 -0700569 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800570 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700571 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700572 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
573 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700574 return s;
575 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700576
577 // EGLSurface creation failed
578 native_window_set_buffers_format(window, 0);
579 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 }
581 return EGL_NO_SURFACE;
582}
583
584EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
585 NativePixmapType pixmap,
586 const EGLint *attrib_list)
587{
588 clearError();
589
Jesse Hallb29e5e82012-04-04 16:53:42 -0700590 egl_connection_t* cnx = NULL;
591 egl_display_ptr dp = validate_display_connection(dpy, cnx);
592 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700593 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800594 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700595 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700596 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
597 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700598 return s;
599 }
600 }
601 return EGL_NO_SURFACE;
602}
603
604EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
605 const EGLint *attrib_list)
606{
607 clearError();
608
Jesse Hallb29e5e82012-04-04 16:53:42 -0700609 egl_connection_t* cnx = NULL;
610 egl_display_ptr dp = validate_display_connection(dpy, cnx);
611 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700612 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800613 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700614 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700615 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
616 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700617 return s;
618 }
619 }
620 return EGL_NO_SURFACE;
621}
Jesse Hall47743382013-02-08 11:13:46 -0800622
Mathias Agopian518ec112011-05-13 16:21:08 -0700623EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
624{
625 clearError();
626
Jesse Hallb29e5e82012-04-04 16:53:42 -0700627 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700628 if (!dp) return EGL_FALSE;
629
Jesse Hallb29e5e82012-04-04 16:53:42 -0700630 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700631 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800632 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700633
634 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800635 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700636 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700637 _s.terminate();
638 }
639 return result;
640}
641
642EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
643 EGLint attribute, EGLint *value)
644{
645 clearError();
646
Jesse Hallb29e5e82012-04-04 16:53:42 -0700647 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700648 if (!dp) return EGL_FALSE;
649
Jesse Hallb29e5e82012-04-04 16:53:42 -0700650 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700651 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800652 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700653
Mathias Agopian518ec112011-05-13 16:21:08 -0700654 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800655 return s->cnx->egl.eglQuerySurface(
656 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700657}
658
Jamie Gennise8696a42012-01-15 18:54:57 -0800659void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800660 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800661 clearError();
662
Jesse Hallb29e5e82012-04-04 16:53:42 -0700663 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800664 if (!dp) {
665 return;
666 }
667
Jesse Hallb29e5e82012-04-04 16:53:42 -0700668 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800669 if (!_s.get()) {
670 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800671 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800672}
673
Mathias Agopian518ec112011-05-13 16:21:08 -0700674// ----------------------------------------------------------------------------
675// Contexts
676// ----------------------------------------------------------------------------
677
678EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
679 EGLContext share_list, const EGLint *attrib_list)
680{
681 clearError();
682
Jesse Hallb29e5e82012-04-04 16:53:42 -0700683 egl_connection_t* cnx = NULL;
684 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700685 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700686 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700687 if (!ContextRef(dp.get(), share_list).get()) {
688 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
689 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700690 egl_context_t* const c = get_context(share_list);
691 share_list = c->context;
692 }
693 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800694 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700695 if (context != EGL_NO_CONTEXT) {
696 // figure out if it's a GLESv1 or GLESv2
697 int version = 0;
698 if (attrib_list) {
699 while (*attrib_list != EGL_NONE) {
700 GLint attr = *attrib_list++;
701 GLint value = *attrib_list++;
702 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
703 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800704 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800705 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800706 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700707 }
708 }
709 };
710 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700711 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
712 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 return c;
714 }
715 }
716 return EGL_NO_CONTEXT;
717}
718
719EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
720{
721 clearError();
722
Jesse Hallb29e5e82012-04-04 16:53:42 -0700723 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700724 if (!dp)
725 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700726
Jesse Hallb29e5e82012-04-04 16:53:42 -0700727 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700728 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800729 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800730
Mathias Agopian518ec112011-05-13 16:21:08 -0700731 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800732 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700733 if (result == EGL_TRUE) {
734 _c.terminate();
735 }
736 return result;
737}
738
Mathias Agopian518ec112011-05-13 16:21:08 -0700739EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
740 EGLSurface read, EGLContext ctx)
741{
742 clearError();
743
Jesse Hallb29e5e82012-04-04 16:53:42 -0700744 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800745 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700746
Mathias Agopian5b287a62011-05-16 18:58:55 -0700747 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
748 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
749 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700750 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
751 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800752 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700753 }
754
755 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700756 ContextRef _c(dp.get(), ctx);
757 SurfaceRef _d(dp.get(), draw);
758 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700759
760 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700761 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700762 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800763 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700764 }
765
766 // these are the underlying implementation's object
767 EGLContext impl_ctx = EGL_NO_CONTEXT;
768 EGLSurface impl_draw = EGL_NO_SURFACE;
769 EGLSurface impl_read = EGL_NO_SURFACE;
770
771 // these are our objects structs passed in
772 egl_context_t * c = NULL;
773 egl_surface_t const * d = NULL;
774 egl_surface_t const * r = NULL;
775
776 // these are the current objects structs
777 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800778
Mathias Agopian518ec112011-05-13 16:21:08 -0700779 if (ctx != EGL_NO_CONTEXT) {
780 c = get_context(ctx);
781 impl_ctx = c->context;
782 } else {
783 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700784 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
785 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800786 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700787 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700788 if (cur_c == NULL) {
789 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700790 // not an error, there is just no current context.
791 return EGL_TRUE;
792 }
793 }
794
795 // retrieve the underlying implementation's draw EGLSurface
796 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800797 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 impl_draw = d->surface;
800 }
801
802 // retrieve the underlying implementation's read EGLSurface
803 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800804 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700805 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700806 impl_read = r->surface;
807 }
808
Mathias Agopian518ec112011-05-13 16:21:08 -0700809
Jesse Hallb29e5e82012-04-04 16:53:42 -0700810 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800811 draw, read, ctx,
812 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700813
814 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800815 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700816 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
817 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700818 _c.acquire();
819 _r.acquire();
820 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700821 } else {
822 setGLHooksThreadSpecific(&gHooksNoContext);
823 egl_tls_t::setContext(EGL_NO_CONTEXT);
824 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700825 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000826 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700827 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -0800828 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700829 }
830 return result;
831}
832
833
834EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
835 EGLint attribute, EGLint *value)
836{
837 clearError();
838
Jesse Hallb29e5e82012-04-04 16:53:42 -0700839 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700840 if (!dp) return EGL_FALSE;
841
Jesse Hallb29e5e82012-04-04 16:53:42 -0700842 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -0800843 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700844
Mathias Agopian518ec112011-05-13 16:21:08 -0700845 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800846 return c->cnx->egl.eglQueryContext(
847 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700848
Mathias Agopian518ec112011-05-13 16:21:08 -0700849}
850
851EGLContext eglGetCurrentContext(void)
852{
853 // could be called before eglInitialize(), but we wouldn't have a context
854 // then, and this function would correctly return EGL_NO_CONTEXT.
855
856 clearError();
857
858 EGLContext ctx = getContext();
859 return ctx;
860}
861
862EGLSurface eglGetCurrentSurface(EGLint readdraw)
863{
864 // could be called before eglInitialize(), but we wouldn't have a context
865 // then, and this function would correctly return EGL_NO_SURFACE.
866
867 clearError();
868
869 EGLContext ctx = getContext();
870 if (ctx) {
871 egl_context_t const * const c = get_context(ctx);
872 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
873 switch (readdraw) {
874 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800875 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700876 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
877 }
878 }
879 return EGL_NO_SURFACE;
880}
881
882EGLDisplay eglGetCurrentDisplay(void)
883{
884 // could be called before eglInitialize(), but we wouldn't have a context
885 // then, and this function would correctly return EGL_NO_DISPLAY.
886
887 clearError();
888
889 EGLContext ctx = getContext();
890 if (ctx) {
891 egl_context_t const * const c = get_context(ctx);
892 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
893 return c->dpy;
894 }
895 return EGL_NO_DISPLAY;
896}
897
898EGLBoolean eglWaitGL(void)
899{
Mathias Agopian518ec112011-05-13 16:21:08 -0700900 clearError();
901
Mathias Agopianada798b2012-02-13 17:09:30 -0800902 egl_connection_t* const cnx = &gEGLImpl;
903 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800904 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800905
906 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700907}
908
909EGLBoolean eglWaitNative(EGLint engine)
910{
Mathias Agopian518ec112011-05-13 16:21:08 -0700911 clearError();
912
Mathias Agopianada798b2012-02-13 17:09:30 -0800913 egl_connection_t* const cnx = &gEGLImpl;
914 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800915 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800916
917 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700918}
919
920EGLint eglGetError(void)
921{
Mathias Agopianada798b2012-02-13 17:09:30 -0800922 EGLint err = EGL_SUCCESS;
923 egl_connection_t* const cnx = &gEGLImpl;
924 if (cnx->dso) {
925 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700926 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800927 if (err == EGL_SUCCESS) {
928 err = egl_tls_t::getError();
929 }
930 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700931}
932
Michael Chockc0ec5e22014-01-27 08:14:33 -0800933static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700934 const char* procname) {
935 const egl_connection_t* cnx = &gEGLImpl;
936 void* proc = NULL;
937
Michael Chockc0ec5e22014-01-27 08:14:33 -0800938 proc = dlsym(cnx->libEgl, procname);
939 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
940
Jesse Hallc07b5202013-07-04 12:08:16 -0700941 proc = dlsym(cnx->libGles2, procname);
942 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
943
944 proc = dlsym(cnx->libGles1, procname);
945 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
946
947 return NULL;
948}
949
Mathias Agopian518ec112011-05-13 16:21:08 -0700950__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
951{
952 // eglGetProcAddress() could be the very first function called
953 // in which case we must make sure we've initialized ourselves, this
954 // happens the first time egl_get_display() is called.
955
956 clearError();
957
958 if (egl_init_drivers() == EGL_FALSE) {
959 setError(EGL_BAD_PARAMETER, NULL);
960 return NULL;
961 }
962
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700963 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700964 return NULL;
965 }
966
Mathias Agopian518ec112011-05-13 16:21:08 -0700967 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700968 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 if (addr) return addr;
970
Michael Chockc0ec5e22014-01-27 08:14:33 -0800971 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700972 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700973
Mathias Agopian518ec112011-05-13 16:21:08 -0700974 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
975 pthread_mutex_lock(&sExtensionMapMutex);
976
977 /*
978 * Since eglGetProcAddress() is not associated to anything, it needs
979 * to return a function pointer that "works" regardless of what
980 * the current context is.
981 *
982 * For this reason, we return a "forwarder", a small stub that takes
983 * care of calling the function associated with the context
984 * currently bound.
985 *
986 * We first look for extensions we've already resolved, if we're seeing
987 * this extension for the first time, we go through all our
988 * implementations and call eglGetProcAddress() and record the
989 * result in the appropriate implementation hooks and return the
990 * address of the forwarder corresponding to that hook set.
991 *
992 */
993
994 const String8 name(procname);
995 addr = sGLExtentionMap.valueFor(name);
996 const int slot = sGLExtentionSlot;
997
Steve Blocke6f43dd2012-01-06 19:20:56 +0000998 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700999 "no more slots for eglGetProcAddress(\"%s\")",
1000 procname);
1001
1002 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1003 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001004
1005 egl_connection_t* const cnx = &gEGLImpl;
1006 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001007 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001008 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001009 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1010 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001011 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001012 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001013 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001014
Mathias Agopian518ec112011-05-13 16:21:08 -07001015 if (found) {
1016 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 sGLExtentionMap.add(name, addr);
1018 sGLExtentionSlot++;
1019 }
1020 }
1021
1022 pthread_mutex_unlock(&sExtensionMapMutex);
1023 return addr;
1024}
1025
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001026class FrameCompletionThread : public Thread {
1027public:
1028
1029 static void queueSync(EGLSyncKHR sync) {
1030 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1031 static bool running = false;
1032 if (!running) {
1033 thread->run("GPUFrameCompletion");
1034 running = true;
1035 }
1036 {
1037 Mutex::Autolock lock(thread->mMutex);
1038 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1039 thread->mFramesQueued).string());
1040 thread->mQueue.push_back(sync);
1041 thread->mCondition.signal();
1042 thread->mFramesQueued++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001043 ATRACE_INT("GPU Frames Outstanding", int32_t(thread->mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001044 }
1045 }
1046
1047private:
1048 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1049
1050 virtual bool threadLoop() {
1051 EGLSyncKHR sync;
1052 uint32_t frameNum;
1053 {
1054 Mutex::Autolock lock(mMutex);
1055 while (mQueue.isEmpty()) {
1056 mCondition.wait(mMutex);
1057 }
1058 sync = mQueue[0];
1059 frameNum = mFramesCompleted;
1060 }
1061 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1062 {
1063 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1064 frameNum).string());
1065 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1066 if (result == EGL_FALSE) {
1067 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1068 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1069 ALOGE("FrameCompletion: timeout waiting for fence");
1070 }
1071 eglDestroySyncKHR(dpy, sync);
1072 }
1073 {
1074 Mutex::Autolock lock(mMutex);
1075 mQueue.removeAt(0);
1076 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001077 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001078 }
1079 return true;
1080 }
1081
1082 uint32_t mFramesQueued;
1083 uint32_t mFramesCompleted;
1084 Vector<EGLSyncKHR> mQueue;
1085 Condition mCondition;
1086 Mutex mMutex;
1087};
1088
Dan Stozaa894d082015-02-19 15:27:36 -08001089EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1090 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001091{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001092 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001093 clearError();
1094
Jesse Hallb29e5e82012-04-04 16:53:42 -07001095 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001096 if (!dp) return EGL_FALSE;
1097
Jesse Hallb29e5e82012-04-04 16:53:42 -07001098 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001099 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001100 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001101
Mathias Agopian518ec112011-05-13 16:21:08 -07001102 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001103
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001104 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1105 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1106 if (sync != EGL_NO_SYNC_KHR) {
1107 FrameCompletionThread::queueSync(sync);
1108 }
1109 }
1110
Mathias Agopian7db993a2012-03-25 00:49:46 -07001111 if (CC_UNLIKELY(dp->finishOnSwap)) {
1112 uint32_t pixel;
1113 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1114 if (c) {
1115 // glReadPixels() ensures that the frame is complete
1116 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1117 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1118 }
1119 }
1120
Dan Stozaa894d082015-02-19 15:27:36 -08001121 if (n_rects == 0) {
1122 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1123 }
1124
1125 Vector<android_native_rect_t> androidRects;
1126 for (int r = 0; r < n_rects; ++r) {
1127 int offset = r * 4;
1128 int x = rects[offset];
1129 int y = rects[offset + 1];
1130 int width = rects[offset + 2];
1131 int height = rects[offset + 3];
1132 android_native_rect_t androidRect;
1133 androidRect.left = x;
1134 androidRect.top = y + height;
1135 androidRect.right = x + width;
1136 androidRect.bottom = y;
1137 androidRects.push_back(androidRect);
1138 }
1139 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1140 androidRects.size());
1141
1142 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1143 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1144 rects, n_rects);
1145 } else {
1146 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1147 }
1148}
1149
1150EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1151{
1152 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001153}
1154
1155EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1156 NativePixmapType target)
1157{
1158 clearError();
1159
Jesse Hallb29e5e82012-04-04 16:53:42 -07001160 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001161 if (!dp) return EGL_FALSE;
1162
Jesse Hallb29e5e82012-04-04 16:53:42 -07001163 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001164 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001165 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001166
Mathias Agopian518ec112011-05-13 16:21:08 -07001167 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001168 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001169}
1170
1171const char* eglQueryString(EGLDisplay dpy, EGLint name)
1172{
1173 clearError();
1174
Chia-I Wue57d1352016-08-15 16:10:02 +08001175 // Generate an error quietly when client extensions (as defined by
1176 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1177 // validate_display to generate the error as validate_display would log
1178 // the error, which can be misleading.
1179 //
1180 // If we want to support EGL_EXT_client_extensions later, we can return
1181 // the client extension string here instead.
1182 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001183 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001184
Jesse Hallb29e5e82012-04-04 16:53:42 -07001185 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001186 if (!dp) return (const char *) NULL;
1187
1188 switch (name) {
1189 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001190 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001191 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001192 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001193 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001194 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001195 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001196 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001197 default:
1198 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001199 }
1200 return setError(EGL_BAD_PARAMETER, (const char *)0);
1201}
1202
Mathias Agopianca088332013-03-28 17:44:13 -07001203EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1204{
1205 clearError();
1206
1207 const egl_display_ptr dp = validate_display(dpy);
1208 if (!dp) return (const char *) NULL;
1209
1210 switch (name) {
1211 case EGL_VENDOR:
1212 return dp->disp.queryString.vendor;
1213 case EGL_VERSION:
1214 return dp->disp.queryString.version;
1215 case EGL_EXTENSIONS:
1216 return dp->disp.queryString.extensions;
1217 case EGL_CLIENT_APIS:
1218 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001219 default:
1220 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001221 }
1222 return setError(EGL_BAD_PARAMETER, (const char *)0);
1223}
Mathias Agopian518ec112011-05-13 16:21:08 -07001224
1225// ----------------------------------------------------------------------------
1226// EGL 1.1
1227// ----------------------------------------------------------------------------
1228
1229EGLBoolean eglSurfaceAttrib(
1230 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1231{
1232 clearError();
1233
Jesse Hallb29e5e82012-04-04 16:53:42 -07001234 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001235 if (!dp) return EGL_FALSE;
1236
Jesse Hallb29e5e82012-04-04 16:53:42 -07001237 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001238 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001239 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001240
Pablo Ceballosc18be292016-05-31 14:55:42 -07001241 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001242
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001243 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001244 if (!s->win.get()) {
1245 setError(EGL_BAD_SURFACE, EGL_FALSE);
1246 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001247 int err = native_window_set_auto_refresh(s->win.get(), value ? true : false);
1248 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001249 }
1250
Pablo Ceballosc18be292016-05-31 14:55:42 -07001251 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001252 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001253 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001254 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001255 int err = native_window_enable_frame_timestamps(s->win.get(), value ? true : false);
1256 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001257 }
1258
Mathias Agopian518ec112011-05-13 16:21:08 -07001259 if (s->cnx->egl.eglSurfaceAttrib) {
1260 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001261 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001262 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001263 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001264}
1265
1266EGLBoolean eglBindTexImage(
1267 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1268{
1269 clearError();
1270
Jesse Hallb29e5e82012-04-04 16:53:42 -07001271 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 if (!dp) return EGL_FALSE;
1273
Jesse Hallb29e5e82012-04-04 16:53:42 -07001274 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001275 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001276 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001277
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 egl_surface_t const * const s = get_surface(surface);
1279 if (s->cnx->egl.eglBindTexImage) {
1280 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001281 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001282 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001283 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001284}
1285
1286EGLBoolean eglReleaseTexImage(
1287 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1288{
1289 clearError();
1290
Jesse Hallb29e5e82012-04-04 16:53:42 -07001291 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001292 if (!dp) return EGL_FALSE;
1293
Jesse Hallb29e5e82012-04-04 16:53:42 -07001294 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001295 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001296 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001297
Mathias Agopian518ec112011-05-13 16:21:08 -07001298 egl_surface_t const * const s = get_surface(surface);
1299 if (s->cnx->egl.eglReleaseTexImage) {
1300 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001301 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001302 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001303 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001304}
1305
1306EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1307{
1308 clearError();
1309
Jesse Hallb29e5e82012-04-04 16:53:42 -07001310 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001311 if (!dp) return EGL_FALSE;
1312
1313 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001314 egl_connection_t* const cnx = &gEGLImpl;
1315 if (cnx->dso && cnx->egl.eglSwapInterval) {
1316 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001317 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001318
Mathias Agopian518ec112011-05-13 16:21:08 -07001319 return res;
1320}
1321
1322
1323// ----------------------------------------------------------------------------
1324// EGL 1.2
1325// ----------------------------------------------------------------------------
1326
1327EGLBoolean eglWaitClient(void)
1328{
1329 clearError();
1330
Mathias Agopianada798b2012-02-13 17:09:30 -08001331 egl_connection_t* const cnx = &gEGLImpl;
1332 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001333 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001334
1335 EGLBoolean res;
1336 if (cnx->egl.eglWaitClient) {
1337 res = cnx->egl.eglWaitClient();
1338 } else {
1339 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001340 }
1341 return res;
1342}
1343
1344EGLBoolean eglBindAPI(EGLenum api)
1345{
1346 clearError();
1347
1348 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001349 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001350 }
1351
1352 // bind this API on all EGLs
1353 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001354 egl_connection_t* const cnx = &gEGLImpl;
1355 if (cnx->dso && cnx->egl.eglBindAPI) {
1356 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001357 }
1358 return res;
1359}
1360
1361EGLenum eglQueryAPI(void)
1362{
1363 clearError();
1364
1365 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001366 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001367 }
1368
Mathias Agopianada798b2012-02-13 17:09:30 -08001369 egl_connection_t* const cnx = &gEGLImpl;
1370 if (cnx->dso && cnx->egl.eglQueryAPI) {
1371 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001372 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001373
Mathias Agopian518ec112011-05-13 16:21:08 -07001374 // or, it can only be OpenGL ES
1375 return EGL_OPENGL_ES_API;
1376}
1377
1378EGLBoolean eglReleaseThread(void)
1379{
1380 clearError();
1381
Mathias Agopianada798b2012-02-13 17:09:30 -08001382 egl_connection_t* const cnx = &gEGLImpl;
1383 if (cnx->dso && cnx->egl.eglReleaseThread) {
1384 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001385 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301386
1387 // If there is context bound to the thread, release it
1388 egl_display_t::loseCurrent(get_context(getContext()));
1389
Mathias Agopian518ec112011-05-13 16:21:08 -07001390 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001391 return EGL_TRUE;
1392}
1393
1394EGLSurface eglCreatePbufferFromClientBuffer(
1395 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1396 EGLConfig config, const EGLint *attrib_list)
1397{
1398 clearError();
1399
Jesse Hallb29e5e82012-04-04 16:53:42 -07001400 egl_connection_t* cnx = NULL;
1401 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1402 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001403 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1404 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001405 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001406 }
1407 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1408}
1409
1410// ----------------------------------------------------------------------------
1411// EGL_EGLEXT_VERSION 3
1412// ----------------------------------------------------------------------------
1413
1414EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1415 const EGLint *attrib_list)
1416{
1417 clearError();
1418
Jesse Hallb29e5e82012-04-04 16:53:42 -07001419 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001420 if (!dp) return EGL_FALSE;
1421
Jesse Hallb29e5e82012-04-04 16:53:42 -07001422 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001423 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001424 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001425
1426 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001427 if (s->cnx->egl.eglLockSurfaceKHR) {
1428 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001429 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001431 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001432}
1433
1434EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1435{
1436 clearError();
1437
Jesse Hallb29e5e82012-04-04 16:53:42 -07001438 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001439 if (!dp) return EGL_FALSE;
1440
Jesse Hallb29e5e82012-04-04 16:53:42 -07001441 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001442 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001443 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001444
1445 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001446 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001447 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001448 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001449 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450}
1451
1452EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1453 EGLClientBuffer buffer, const EGLint *attrib_list)
1454{
1455 clearError();
1456
Jesse Hallb29e5e82012-04-04 16:53:42 -07001457 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001458 if (!dp) return EGL_NO_IMAGE_KHR;
1459
Jesse Hallb29e5e82012-04-04 16:53:42 -07001460 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001461 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001462
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001463 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1464 egl_connection_t* const cnx = &gEGLImpl;
1465 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1466 result = cnx->egl.eglCreateImageKHR(
1467 dp->disp.dpy,
1468 c ? c->context : EGL_NO_CONTEXT,
1469 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001470 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001471 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001472}
1473
1474EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1475{
1476 clearError();
1477
Jesse Hallb29e5e82012-04-04 16:53:42 -07001478 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001479 if (!dp) return EGL_FALSE;
1480
Steven Holte646a5c52012-06-04 20:02:11 -07001481 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001482 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001483 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001484 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001485 }
Steven Holte646a5c52012-06-04 20:02:11 -07001486 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001487}
1488
1489// ----------------------------------------------------------------------------
1490// EGL_EGLEXT_VERSION 5
1491// ----------------------------------------------------------------------------
1492
1493
1494EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1495{
1496 clearError();
1497
Jesse Hallb29e5e82012-04-04 16:53:42 -07001498 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001499 if (!dp) return EGL_NO_SYNC_KHR;
1500
Mathias Agopian518ec112011-05-13 16:21:08 -07001501 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001502 egl_connection_t* const cnx = &gEGLImpl;
1503 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1504 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001506 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001507}
1508
1509EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1510{
1511 clearError();
1512
Jesse Hallb29e5e82012-04-04 16:53:42 -07001513 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001514 if (!dp) return EGL_FALSE;
1515
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001517 egl_connection_t* const cnx = &gEGLImpl;
1518 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1519 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001520 }
1521 return result;
1522}
1523
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001524EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1525 clearError();
1526
1527 const egl_display_ptr dp = validate_display(dpy);
1528 if (!dp) return EGL_FALSE;
1529
1530 EGLBoolean result = EGL_FALSE;
1531 egl_connection_t* const cnx = &gEGLImpl;
1532 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1533 result = cnx->egl.eglSignalSyncKHR(
1534 dp->disp.dpy, sync, mode);
1535 }
1536 return result;
1537}
1538
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001539EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1540 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001541{
1542 clearError();
1543
Jesse Hallb29e5e82012-04-04 16:53:42 -07001544 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001545 if (!dp) return EGL_FALSE;
1546
Mathias Agopian737b8962017-02-24 14:32:05 -08001547 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001548 egl_connection_t* const cnx = &gEGLImpl;
1549 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1550 result = cnx->egl.eglClientWaitSyncKHR(
1551 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001552 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001553 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001554}
1555
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001556EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1557 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001558{
1559 clearError();
1560
Jesse Hallb29e5e82012-04-04 16:53:42 -07001561 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001562 if (!dp) return EGL_FALSE;
1563
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001564 EGLBoolean result = EGL_FALSE;
1565 egl_connection_t* const cnx = &gEGLImpl;
1566 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1567 result = cnx->egl.eglGetSyncAttribKHR(
1568 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001569 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001570 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001571}
1572
Season Li000d88f2015-07-01 11:39:40 -07001573EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1574{
1575 clearError();
1576
1577 const egl_display_ptr dp = validate_display(dpy);
1578 if (!dp) return EGL_NO_STREAM_KHR;
1579
1580 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1581 egl_connection_t* const cnx = &gEGLImpl;
1582 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1583 result = cnx->egl.eglCreateStreamKHR(
1584 dp->disp.dpy, attrib_list);
1585 }
1586 return result;
1587}
1588
1589EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1590{
1591 clearError();
1592
1593 const egl_display_ptr dp = validate_display(dpy);
1594 if (!dp) return EGL_FALSE;
1595
1596 EGLBoolean result = EGL_FALSE;
1597 egl_connection_t* const cnx = &gEGLImpl;
1598 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1599 result = cnx->egl.eglDestroyStreamKHR(
1600 dp->disp.dpy, stream);
1601 }
1602 return result;
1603}
1604
1605EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1606 EGLenum attribute, EGLint value)
1607{
1608 clearError();
1609
1610 const egl_display_ptr dp = validate_display(dpy);
1611 if (!dp) return EGL_FALSE;
1612
1613 EGLBoolean result = EGL_FALSE;
1614 egl_connection_t* const cnx = &gEGLImpl;
1615 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1616 result = cnx->egl.eglStreamAttribKHR(
1617 dp->disp.dpy, stream, attribute, value);
1618 }
1619 return result;
1620}
1621
1622EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1623 EGLenum attribute, EGLint *value)
1624{
1625 clearError();
1626
1627 const egl_display_ptr dp = validate_display(dpy);
1628 if (!dp) return EGL_FALSE;
1629
1630 EGLBoolean result = EGL_FALSE;
1631 egl_connection_t* const cnx = &gEGLImpl;
1632 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1633 result = cnx->egl.eglQueryStreamKHR(
1634 dp->disp.dpy, stream, attribute, value);
1635 }
1636 return result;
1637}
1638
1639EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1640 EGLenum attribute, EGLuint64KHR *value)
1641{
1642 clearError();
1643
1644 const egl_display_ptr dp = validate_display(dpy);
1645 if (!dp) return EGL_FALSE;
1646
1647 EGLBoolean result = EGL_FALSE;
1648 egl_connection_t* const cnx = &gEGLImpl;
1649 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1650 result = cnx->egl.eglQueryStreamu64KHR(
1651 dp->disp.dpy, stream, attribute, value);
1652 }
1653 return result;
1654}
1655
1656EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1657 EGLenum attribute, EGLTimeKHR *value)
1658{
1659 clearError();
1660
1661 const egl_display_ptr dp = validate_display(dpy);
1662 if (!dp) return EGL_FALSE;
1663
1664 EGLBoolean result = EGL_FALSE;
1665 egl_connection_t* const cnx = &gEGLImpl;
1666 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1667 result = cnx->egl.eglQueryStreamTimeKHR(
1668 dp->disp.dpy, stream, attribute, value);
1669 }
1670 return result;
1671}
1672
1673EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1674 EGLStreamKHR stream, const EGLint *attrib_list)
1675{
1676 clearError();
1677
1678 egl_display_ptr dp = validate_display(dpy);
1679 if (!dp) return EGL_NO_SURFACE;
1680
1681 egl_connection_t* const cnx = &gEGLImpl;
1682 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1683 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1684 dp->disp.dpy, config, stream, attrib_list);
1685 if (surface != EGL_NO_SURFACE) {
1686 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1687 surface, cnx);
1688 return s;
1689 }
1690 }
1691 return EGL_NO_SURFACE;
1692}
1693
1694EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1695 EGLStreamKHR stream)
1696{
1697 clearError();
1698
1699 const egl_display_ptr dp = validate_display(dpy);
1700 if (!dp) return EGL_FALSE;
1701
1702 EGLBoolean result = EGL_FALSE;
1703 egl_connection_t* const cnx = &gEGLImpl;
1704 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1705 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1706 dp->disp.dpy, stream);
1707 }
1708 return result;
1709}
1710
1711EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1712 EGLStreamKHR stream)
1713{
1714 clearError();
1715
1716 const egl_display_ptr dp = validate_display(dpy);
1717 if (!dp) return EGL_FALSE;
1718
1719 EGLBoolean result = EGL_FALSE;
1720 egl_connection_t* const cnx = &gEGLImpl;
1721 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1722 result = cnx->egl.eglStreamConsumerAcquireKHR(
1723 dp->disp.dpy, stream);
1724 }
1725 return result;
1726}
1727
1728EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1729 EGLStreamKHR stream)
1730{
1731 clearError();
1732
1733 const egl_display_ptr dp = validate_display(dpy);
1734 if (!dp) return EGL_FALSE;
1735
1736 EGLBoolean result = EGL_FALSE;
1737 egl_connection_t* const cnx = &gEGLImpl;
1738 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1739 result = cnx->egl.eglStreamConsumerReleaseKHR(
1740 dp->disp.dpy, stream);
1741 }
1742 return result;
1743}
1744
1745EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1746 EGLDisplay dpy, EGLStreamKHR stream)
1747{
1748 clearError();
1749
1750 const egl_display_ptr dp = validate_display(dpy);
1751 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1752
1753 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1754 egl_connection_t* const cnx = &gEGLImpl;
1755 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1756 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1757 dp->disp.dpy, stream);
1758 }
1759 return result;
1760}
1761
1762EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1763 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1764{
1765 clearError();
1766
1767 const egl_display_ptr dp = validate_display(dpy);
1768 if (!dp) return EGL_NO_STREAM_KHR;
1769
1770 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1771 egl_connection_t* const cnx = &gEGLImpl;
1772 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1773 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1774 dp->disp.dpy, file_descriptor);
1775 }
1776 return result;
1777}
1778
Mathias Agopian518ec112011-05-13 16:21:08 -07001779// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001780// EGL_EGLEXT_VERSION 15
1781// ----------------------------------------------------------------------------
1782
1783EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1784 clearError();
1785 const egl_display_ptr dp = validate_display(dpy);
1786 if (!dp) return EGL_FALSE;
1787 EGLint result = EGL_FALSE;
1788 egl_connection_t* const cnx = &gEGLImpl;
1789 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1790 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1791 }
1792 return result;
1793}
1794
1795// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001796// ANDROID extensions
1797// ----------------------------------------------------------------------------
1798
Jamie Gennis331841b2012-09-06 14:52:00 -07001799EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1800{
1801 clearError();
1802
1803 const egl_display_ptr dp = validate_display(dpy);
1804 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1805
1806 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1807 egl_connection_t* const cnx = &gEGLImpl;
1808 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1809 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1810 }
1811 return result;
1812}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001813
Andy McFadden72841452013-03-01 16:25:32 -08001814EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1815 EGLnsecsANDROID time)
1816{
1817 clearError();
1818
1819 const egl_display_ptr dp = validate_display(dpy);
1820 if (!dp) {
1821 return EGL_FALSE;
1822 }
1823
1824 SurfaceRef _s(dp.get(), surface);
1825 if (!_s.get()) {
1826 setError(EGL_BAD_SURFACE, EGL_FALSE);
1827 return EGL_FALSE;
1828 }
1829
1830 egl_surface_t const * const s = get_surface(surface);
1831 native_window_set_buffers_timestamp(s->win.get(), time);
1832
1833 return EGL_TRUE;
1834}
1835
Craig Donner60761072017-01-27 12:30:44 -08001836EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1837 clearError();
1838
1839 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1840
Mathias Agopian89ed4c82017-02-09 18:48:34 -08001841 const GraphicBuffer* graphicBuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
Craig Donner60761072017-01-27 12:30:44 -08001842 return static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
1843}
1844
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001845// ----------------------------------------------------------------------------
1846// NVIDIA extensions
1847// ----------------------------------------------------------------------------
1848EGLuint64NV eglGetSystemTimeFrequencyNV()
1849{
1850 clearError();
1851
1852 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001853 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001854 }
1855
1856 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001857 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001858
Mathias Agopianada798b2012-02-13 17:09:30 -08001859 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1860 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001861 }
1862
Mathias Agopian737b8962017-02-24 14:32:05 -08001863 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001864}
1865
1866EGLuint64NV eglGetSystemTimeNV()
1867{
1868 clearError();
1869
1870 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001871 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001872 }
1873
1874 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001875 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001876
Mathias Agopianada798b2012-02-13 17:09:30 -08001877 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1878 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001879 }
1880
Mathias Agopian737b8962017-02-24 14:32:05 -08001881 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001882}
Dan Stozaa894d082015-02-19 15:27:36 -08001883
1884// ----------------------------------------------------------------------------
1885// Partial update extension
1886// ----------------------------------------------------------------------------
1887EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1888 EGLint *rects, EGLint n_rects)
1889{
1890 clearError();
1891
1892 const egl_display_ptr dp = validate_display(dpy);
1893 if (!dp) {
1894 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1895 return EGL_FALSE;
1896 }
1897
1898 SurfaceRef _s(dp.get(), surface);
1899 if (!_s.get()) {
1900 setError(EGL_BAD_SURFACE, EGL_FALSE);
1901 return EGL_FALSE;
1902 }
1903
1904 egl_surface_t const * const s = get_surface(surface);
1905 if (s->cnx->egl.eglSetDamageRegionKHR) {
1906 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1907 rects, n_rects);
1908 }
1909
1910 return EGL_FALSE;
1911}
Pablo Ceballosc18be292016-05-31 14:55:42 -07001912
Brian Anderson1049d1d2016-12-16 17:25:57 -08001913EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
1914 EGLuint64KHR *frameId) {
1915 clearError();
1916
1917 const egl_display_ptr dp = validate_display(dpy);
1918 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001919 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001920 }
1921
1922 SurfaceRef _s(dp.get(), surface);
1923 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001924 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001925 }
1926
1927 egl_surface_t const * const s = get_surface(surface);
1928
1929 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001930 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001931 }
1932
1933 uint64_t nextFrameId = 0;
1934 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
1935
1936 if (ret != NO_ERROR) {
1937 // This should not happen. Return an error that is not in the spec
1938 // so it's obvious something is very wrong.
1939 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08001940 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001941 }
1942
1943 *frameId = nextFrameId;
1944 return EGL_TRUE;
1945}
1946
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001947EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
1948 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
1949{
1950 clearError();
1951
1952 const egl_display_ptr dp = validate_display(dpy);
1953 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001954 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001955 }
1956
1957 SurfaceRef _s(dp.get(), surface);
1958 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001959 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001960 }
1961
1962 egl_surface_t const * const s = get_surface(surface);
1963
1964 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001965 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001966 }
1967
1968 nsecs_t* compositeDeadline = nullptr;
1969 nsecs_t* compositeInterval = nullptr;
1970 nsecs_t* compositeToPresentLatency = nullptr;
1971
1972 for (int i = 0; i < numTimestamps; i++) {
1973 switch (names[i]) {
1974 case EGL_COMPOSITE_DEADLINE_ANDROID:
1975 compositeDeadline = &values[i];
1976 break;
1977 case EGL_COMPOSITE_INTERVAL_ANDROID:
1978 compositeInterval = &values[i];
1979 break;
1980 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
1981 compositeToPresentLatency = &values[i];
1982 break;
1983 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08001984 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001985 }
1986 }
1987
1988 status_t ret = native_window_get_compositor_timing(s->win.get(),
1989 compositeDeadline, compositeInterval, compositeToPresentLatency);
1990
1991 switch (ret) {
1992 case NO_ERROR:
1993 return EGL_TRUE;
1994 case INVALID_OPERATION:
Mathias Agopian737b8962017-02-24 14:32:05 -08001995 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001996 default:
1997 // This should not happen. Return an error that is not in the spec
1998 // so it's obvious something is very wrong.
1999 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002000 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002001 }
2002}
2003
2004EGLBoolean eglGetCompositorTimingSupportedANDROID(
2005 EGLDisplay dpy, EGLSurface surface, EGLint name)
2006{
2007 clearError();
2008
2009 const egl_display_ptr dp = validate_display(dpy);
2010 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002011 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002012 }
2013
2014 SurfaceRef _s(dp.get(), surface);
2015 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002016 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002017 }
2018
2019 egl_surface_t const * const s = get_surface(surface);
2020
2021 ANativeWindow* window = s->win.get();
2022 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002023 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002024 }
2025
2026 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002027 case EGL_COMPOSITE_DEADLINE_ANDROID:
2028 case EGL_COMPOSITE_INTERVAL_ANDROID:
2029 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2030 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002031 default:
2032 return EGL_FALSE;
2033 }
2034}
2035
Pablo Ceballosc18be292016-05-31 14:55:42 -07002036EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002037 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002038 EGLnsecsANDROID *values)
2039{
2040 clearError();
2041
2042 const egl_display_ptr dp = validate_display(dpy);
2043 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002044 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002045 }
2046
2047 SurfaceRef _s(dp.get(), surface);
2048 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002049 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002050 }
2051
2052 egl_surface_t const * const s = get_surface(surface);
2053
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002054 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002055 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002056 }
2057
Brian Andersondbd0ea82016-07-22 09:38:59 -07002058 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002059 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002060 nsecs_t* latchTime = nullptr;
2061 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002062 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002063 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002064 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002065 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002066 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002067 nsecs_t* releaseTime = nullptr;
2068
2069 for (int i = 0; i < numTimestamps; i++) {
2070 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002071 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2072 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002073 break;
2074 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2075 acquireTime = &values[i];
2076 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002077 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2078 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002079 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002080 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2081 firstRefreshStartTime = &values[i];
2082 break;
2083 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2084 lastRefreshStartTime = &values[i];
2085 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002086 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2087 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002088 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002089 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2090 displayPresentTime = &values[i];
2091 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002092 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2093 displayRetireTime = &values[i];
2094 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002095 case EGL_DEQUEUE_READY_TIME_ANDROID:
2096 dequeueReadyTime = &values[i];
2097 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002098 case EGL_READS_DONE_TIME_ANDROID:
2099 releaseTime = &values[i];
2100 break;
2101 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002102 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002103 }
2104 }
2105
Brian Anderson1049d1d2016-12-16 17:25:57 -08002106 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002107 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002108 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002109 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002110
Brian Anderson069b3652016-07-22 10:32:47 -07002111 switch (ret) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002112 case NO_ERROR:
2113 return EGL_TRUE;
2114 case NAME_NOT_FOUND:
2115 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
2116 case INVALID_OPERATION:
2117 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2118 case BAD_VALUE:
2119 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2120 default:
2121 // This should not happen. Return an error that is not in the spec
2122 // so it's obvious something is very wrong.
2123 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2124 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002125 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002126}
2127
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002128EGLBoolean eglGetFrameTimestampSupportedANDROID(
2129 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002130{
2131 clearError();
2132
2133 const egl_display_ptr dp = validate_display(dpy);
2134 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002135 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002136 }
2137
2138 SurfaceRef _s(dp.get(), surface);
2139 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002140 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002141 }
2142
2143 egl_surface_t const * const s = get_surface(surface);
2144
2145 ANativeWindow* window = s->win.get();
2146 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002147 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002148 }
2149
2150 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002151 case EGL_COMPOSITE_DEADLINE_ANDROID:
2152 case EGL_COMPOSITE_INTERVAL_ANDROID:
2153 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002154 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002155 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002156 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2157 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2158 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002159 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002160 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002161 case EGL_READS_DONE_TIME_ANDROID:
2162 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002163 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2164 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002165 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002166 return value == 0 ? EGL_FALSE : EGL_TRUE;
2167 }
2168 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2169 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002170 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002171 return value == 0 ? EGL_FALSE : EGL_TRUE;
2172 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002173 default:
2174 return EGL_FALSE;
2175 }
2176}