blob: 1ed9850e3e1568daff9e2306af0ce16b3f551e65 [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>
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -060028#include <EGL/eglext_angle.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
Craig Donner60761072017-01-27 12:30:44 -080030#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080031#include <private/android/AHardwareBufferHelpers.h>
32
Mathias Agopian7db993a2012-03-25 00:49:46 -070033#include <cutils/compiler.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070034#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070035#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070036
Mathias Agopian65421432017-03-08 11:49:05 -080037#include <condition_variable>
38#include <deque>
39#include <mutex>
40#include <unordered_map>
41#include <string>
42#include <thread>
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"
Mathias Agopian65421432017-03-08 11:49:05 -080049#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070050
51using namespace android;
52
53// ----------------------------------------------------------------------------
54
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070055namespace android {
56
Mathias Agopian65421432017-03-08 11:49:05 -080057using nsecs_t = int64_t;
58
Mathias Agopian518ec112011-05-13 16:21:08 -070059struct extention_map_t {
60 const char* name;
61 __eglMustCastToProperFunctionPointerType address;
62};
63
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070064/*
Jesse Hall21558da2013-08-06 15:31:22 -070065 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070066 *
Jesse Hall21558da2013-08-06 15:31:22 -070067 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
68 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070069 *
Jesse Hall21558da2013-08-06 15:31:22 -070070 * The rest (gExtensionString) depend on support in the EGL driver, and are
71 * only available if the driver supports them. However, some of these must be
72 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080073 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070074 * the mandatory extensions are present and may not function properly if some
75 * are missing.
76 *
77 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070078 */
Mathias Agopian737b8962017-02-24 14:32:05 -080079
Mathias Agopian311b4792017-02-28 15:00:49 -080080extern char const * const gBuiltinExtensionString;
81extern char const * const gExtensionString;
Mathias Agopian737b8962017-02-24 14:32:05 -080082
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -060083// clang-format off
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -070084// Extensions implemented by the EGL wrapper.
Mathias Agopian311b4792017-02-28 15:00:49 -080085char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070086 "EGL_KHR_get_all_proc_addresses "
87 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080088 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080089 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080090 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070091 "EGL_ANDROID_get_frame_timestamps "
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070092 "EGL_EXT_surface_SMPTE2086_metadata "
93 "EGL_EXT_surface_CTA861_3_metadata "
Jesse Hall21558da2013-08-06 15:31:22 -070094 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080095
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -070096// Whitelist of extensions exposed to applications if implemented in the vendor driver.
Mathias Agopian311b4792017-02-28 15:00:49 -080097char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070098 "EGL_KHR_image " // mandatory
99 "EGL_KHR_image_base " // mandatory
Krzysztof Kosiński41171c52018-04-19 21:38:54 -0700100 "EGL_EXT_image_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700101 "EGL_KHR_image_pixmap "
102 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700103 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700104 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700105 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700106 "EGL_KHR_gl_texture_cubemap_image "
107 "EGL_KHR_gl_renderbuffer_image "
108 "EGL_KHR_reusable_sync "
109 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700110 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700111 "EGL_KHR_config_attribs "
112 "EGL_KHR_surfaceless_context "
113 "EGL_KHR_stream "
114 "EGL_KHR_stream_fifo "
115 "EGL_KHR_stream_producer_eglsurface "
116 "EGL_KHR_stream_consumer_gltexture "
117 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700118 "EGL_EXT_create_context_robustness "
119 "EGL_NV_system_time "
120 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700121 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700122 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800123 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700124 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800125 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700126 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700127 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700128 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700129 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000130 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800131 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700132 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600133// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700134
135// extensions not exposed to applications but used by the ANDROID system
136// "EGL_ANDROID_blob_cache " // strongly recommended
137// "EGL_IMG_hibernate_process " // optional
138// "EGL_ANDROID_native_fence_sync " // strongly recommended
139// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700140
141/*
142 * EGL Extensions entry-points exposed to 3rd party applications
143 * (keep in sync with gExtensionString above)
144 *
145 */
146static const extention_map_t sExtensionMap[] = {
147 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700148 { "eglLockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
150 { "eglUnlockSurfaceKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700152
153 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 { "eglCreateImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
156 { "eglDestroyImageKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700158
159 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
160 { "eglCreateSyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
162 { "eglDestroySyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
164 { "eglClientWaitSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
166 { "eglSignalSyncKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
168 { "eglGetSyncAttribKHR",
169 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
170
171 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800172 { "eglGetSystemTimeFrequencyNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
174 { "eglGetSystemTimeNV",
175 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700176
Mathias Agopian2bb71682013-03-27 17:32:41 -0700177 // EGL_KHR_wait_sync
178 { "eglWaitSyncKHR",
179 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700180
181 // EGL_ANDROID_presentation_time
182 { "eglPresentationTimeANDROID",
183 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800184
185 // EGL_KHR_swap_buffers_with_damage
186 { "eglSwapBuffersWithDamageKHR",
187 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
188
Craig Donner60761072017-01-27 12:30:44 -0800189 // EGL_ANDROID_get_native_client_buffer
190 { "eglGetNativeClientBufferANDROID",
191 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
192
Dan Stozaa894d082015-02-19 15:27:36 -0800193 // EGL_KHR_partial_update
194 { "eglSetDamageRegionKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700196
197 { "eglCreateStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
199 { "eglDestroyStreamKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
201 { "eglStreamAttribKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
203 { "eglQueryStreamKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
205 { "eglQueryStreamu64KHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
207 { "eglQueryStreamTimeKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
209 { "eglCreateStreamProducerSurfaceKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
211 { "eglStreamConsumerGLTextureExternalKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
213 { "eglStreamConsumerAcquireKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
215 { "eglStreamConsumerReleaseKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
217 { "eglGetStreamFileDescriptorKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
219 { "eglCreateStreamFromFileDescriptorKHR",
220 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700221
222 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800223 { "eglGetNextFrameIdANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800225 { "eglGetCompositorTimingANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
227 { "eglGetCompositorTimingSupportedANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700229 { "eglGetFrameTimestampsANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800231 { "eglGetFrameTimestampSupportedANDROID",
232 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700233
234 // EGL_ANDROID_native_fence_sync
235 { "eglDupNativeFenceFDANDROID",
236 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700237};
238
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700239/*
240 * These extensions entry-points should not be exposed to applications.
241 * They're used internally by the Android EGL layer.
242 */
243#define FILTER_EXTENSIONS(procname) \
244 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
245 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700246 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800249static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
250
Mathias Agopian518ec112011-05-13 16:21:08 -0700251static int sGLExtentionSlot = 0;
252static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
253
254static void(*findProcAddress(const char* name,
255 const extention_map_t* map, size_t n))() {
256 for (uint32_t i=0 ; i<n ; i++) {
257 if (!strcmp(name, map[i].name)) {
258 return map[i].address;
259 }
260 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700261 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700262}
263
264// ----------------------------------------------------------------------------
265
Mathias Agopian518ec112011-05-13 16:21:08 -0700266extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
267extern EGLBoolean egl_init_drivers();
268extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700269extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271} // namespace android;
272
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700273
Mathias Agopian518ec112011-05-13 16:21:08 -0700274// ----------------------------------------------------------------------------
275
276static inline void clearError() { egl_tls_t::clearError(); }
277static inline EGLContext getContext() { return egl_tls_t::getContext(); }
278
279// ----------------------------------------------------------------------------
280
281EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
282{
Jesse Hall1508ae62017-01-19 17:43:26 -0800283 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 clearError();
285
Dan Stozac3289c42014-01-17 11:38:34 -0800286 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700287 if (index >= NUM_DISPLAYS) {
288 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
289 }
290
291 if (egl_init_drivers() == EGL_FALSE) {
292 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
293 }
294
295 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
296 return dpy;
297}
298
299// ----------------------------------------------------------------------------
300// Initialization
301// ----------------------------------------------------------------------------
302
303EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
304{
305 clearError();
306
Jesse Hallb29e5e82012-04-04 16:53:42 -0700307 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800308 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700309
310 EGLBoolean res = dp->initialize(major, minor);
311
312 return res;
313}
314
315EGLBoolean eglTerminate(EGLDisplay dpy)
316{
317 // NOTE: don't unload the drivers b/c some APIs can be called
318 // after eglTerminate() has been called. eglTerminate() only
319 // terminates an EGLDisplay, not a EGL itself.
320
321 clearError();
322
Jesse Hallb29e5e82012-04-04 16:53:42 -0700323 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800324 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700325
326 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800327
Mathias Agopian518ec112011-05-13 16:21:08 -0700328 return res;
329}
330
331// ----------------------------------------------------------------------------
332// configuration
333// ----------------------------------------------------------------------------
334
335EGLBoolean eglGetConfigs( EGLDisplay dpy,
336 EGLConfig *configs,
337 EGLint config_size, EGLint *num_config)
338{
339 clearError();
340
Jesse Hallb29e5e82012-04-04 16:53:42 -0700341 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700342 if (!dp) return EGL_FALSE;
343
Yi Kong48a6cd22018-07-18 10:07:09 -0700344 if (num_config==nullptr) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800345 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700346 }
347
Mathias Agopian7773c432012-02-13 20:06:08 -0800348 EGLBoolean res = EGL_FALSE;
349 *num_config = 0;
350
351 egl_connection_t* const cnx = &gEGLImpl;
352 if (cnx->dso) {
353 res = cnx->egl.eglGetConfigs(
354 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700355 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800356
357 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700358}
359
360EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
361 EGLConfig *configs, EGLint config_size,
362 EGLint *num_config)
363{
364 clearError();
365
Jesse Hallb29e5e82012-04-04 16:53:42 -0700366 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700367 if (!dp) return EGL_FALSE;
368
Yi Kong48a6cd22018-07-18 10:07:09 -0700369 if (num_config==nullptr) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800370 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700371 }
372
Mathias Agopian518ec112011-05-13 16:21:08 -0700373 EGLBoolean res = EGL_FALSE;
374 *num_config = 0;
375
Mathias Agopianada798b2012-02-13 17:09:30 -0800376 egl_connection_t* const cnx = &gEGLImpl;
377 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700378 if (attrib_list) {
379 char value[PROPERTY_VALUE_MAX];
380 property_get("debug.egl.force_msaa", value, "false");
381
382 if (!strcmp(value, "true")) {
383 size_t attribCount = 0;
384 EGLint attrib = attrib_list[0];
385
386 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700387 // if no caveat is requested
Yi Kong48a6cd22018-07-18 10:07:09 -0700388 const EGLint *attribRendererable = nullptr;
389 const EGLint *attribCaveat = nullptr;
Romain Guy1cffc802012-10-15 18:13:05 -0700390
391 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700392 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700393 while (attrib != EGL_NONE) {
394 attrib = attrib_list[attribCount];
395 switch (attrib) {
396 case EGL_RENDERABLE_TYPE:
397 attribRendererable = &attrib_list[attribCount];
398 break;
399 case EGL_CONFIG_CAVEAT:
400 attribCaveat = &attrib_list[attribCount];
401 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800402 default:
403 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700404 }
405 attribCount++;
406 }
407
408 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
409 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800410
Romain Guy1cffc802012-10-15 18:13:05 -0700411 // Insert 2 extra attributes to force-enable MSAA 4x
412 EGLint aaAttribs[attribCount + 4];
413 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
414 aaAttribs[1] = 1;
415 aaAttribs[2] = EGL_SAMPLES;
416 aaAttribs[3] = 4;
417
418 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
419
420 EGLint numConfigAA;
421 EGLBoolean resAA = cnx->egl.eglChooseConfig(
422 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
423
424 if (resAA == EGL_TRUE && numConfigAA > 0) {
425 ALOGD("Enabling MSAA 4x");
426 *num_config = numConfigAA;
427 return resAA;
428 }
429 }
430 }
431 }
432
Mathias Agopian7773c432012-02-13 20:06:08 -0800433 res = cnx->egl.eglChooseConfig(
434 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700435 }
436 return res;
437}
438
439EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
440 EGLint attribute, EGLint *value)
441{
442 clearError();
443
Yi Kong48a6cd22018-07-18 10:07:09 -0700444 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700445 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
446 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800447
Mathias Agopian518ec112011-05-13 16:21:08 -0700448 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800449 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700450}
451
452// ----------------------------------------------------------------------------
453// surfaces
454// ----------------------------------------------------------------------------
455
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700456// Translates EGL color spaces to Android data spaces.
457static android_dataspace dataSpaceFromEGLColorSpace(EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700458 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Chia-I Wu6c354012018-06-20 10:57:48 +0800459 return HAL_DATASPACE_UNKNOWN;
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800461 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600462 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
463 return HAL_DATASPACE_DISPLAY_P3;
464 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
465 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600466 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
467 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600468 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
469 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700470 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT) {
471 return HAL_DATASPACE_BT2020_LINEAR;
472 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_PQ_EXT) {
473 return HAL_DATASPACE_BT2020_PQ;
Jesse Hallc2e41222013-08-08 13:40:22 -0700474 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700475 return HAL_DATASPACE_UNKNOWN;
Jesse Hallc2e41222013-08-08 13:40:22 -0700476}
477
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700478// Get the colorspace value that should be reported from queries. When the colorspace
479// is unknown (no attribute passed), default to reporting LINEAR.
480static EGLint getReportedColorSpace(EGLint colorspace) {
481 return colorspace == EGL_UNKNOWN ? EGL_GL_COLORSPACE_LINEAR_KHR : colorspace;
482}
483
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700484// Returns a list of color spaces understood by the vendor EGL driver.
485static std::vector<EGLint> getDriverColorSpaces(egl_display_ptr dp,
486 android_pixel_format format) {
487 std::vector<EGLint> colorSpaces;
488 if (!dp->hasColorSpaceSupport) return colorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700489
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700490 // OpenGL drivers only support sRGB encoding with 8-bit formats.
491 // RGB_888 is never returned by getNativePixelFormat, but is included for completeness.
492 const bool formatSupportsSRGBEncoding =
493 format == HAL_PIXEL_FORMAT_RGBA_8888 || format == HAL_PIXEL_FORMAT_RGBX_8888 ||
494 format == HAL_PIXEL_FORMAT_RGB_888;
495 const bool formatIsFloatingPoint = format == HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700496
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700497 if (formatSupportsSRGBEncoding) {
498 // sRGB and linear are always supported when color space support is present.
499 colorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
500 colorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
501 // DCI-P3 uses the sRGB transfer function, so it's only relevant for 8-bit formats.
502 if (findExtension(dp->disp.queryString.extensions,
503 "EGL_EXT_gl_colorspace_display_p3")) {
504 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600505 }
506 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700507
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700508 // According to the spec, scRGB is only supported for floating point formats.
509 // For non-linear scRGB, the application is responsible for applying the
510 // transfer function.
511 if (formatIsFloatingPoint) {
512 if (findExtension(dp->disp.queryString.extensions,
513 "EGL_EXT_gl_colorspace_scrgb")) {
514 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_EXT);
515 }
516 if (findExtension(dp->disp.queryString.extensions,
517 "EGL_EXT_gl_colorspace_scrgb_linear")) {
518 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
519 }
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600520 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700521
522 // BT2020 can be used with any pixel format. PQ encoding must be applied by the
523 // application and does not affect the behavior of OpenGL.
524 if (findExtension(dp->disp.queryString.extensions,
525 "EGL_EXT_gl_colorspace_bt2020_linear")) {
526 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
527 }
528 if (findExtension(dp->disp.queryString.extensions,
529 "EGL_EXT_gl_colorspace_bt2020_pq")) {
530 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
531 }
532
533 // Linear DCI-P3 simply uses different primaries than standard RGB and thus
534 // can be used with any pixel format.
535 if (findExtension(dp->disp.queryString.extensions,
536 "EGL_EXT_gl_colorspace_display_p3_linear")) {
537 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
538 }
539 return colorSpaces;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600540}
541
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700542// Cleans up color space related parameters that the driver does not understand.
543// If there is no color space attribute in attrib_list, colorSpace is left
544// unmodified.
545static EGLBoolean processAttributes(egl_display_ptr dp, NativeWindowType window,
546 android_pixel_format format, const EGLint* attrib_list,
547 EGLint* colorSpace,
548 std::vector<EGLint>* strippedAttribList) {
549 for (const EGLint* attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
550 bool copyAttribute = true;
551 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
552 // Fail immediately if the driver doesn't have color space support at all.
553 if (!dp->hasColorSpaceSupport) return false;
554 *colorSpace = attr[1];
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600555
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700556 // Strip the attribute if the driver doesn't understand it.
557 copyAttribute = false;
558 std::vector<EGLint> driverColorSpaces = getDriverColorSpaces(dp, format);
559 for (auto driverColorSpace : driverColorSpaces) {
560 if (attr[1] == driverColorSpace) {
561 copyAttribute = true;
562 break;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600563 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600564 }
565 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700566 if (copyAttribute) {
567 strippedAttribList->push_back(attr[0]);
568 strippedAttribList->push_back(attr[1]);
569 }
570 }
571 // Terminate the attribute list.
572 strippedAttribList->push_back(EGL_NONE);
573
574 // If the passed color space has wide color gamut, check whether the target native window
575 // supports wide color.
576 const bool colorSpaceIsNarrow =
577 *colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700578 *colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR ||
579 *colorSpace == EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700580 if (window && !colorSpaceIsNarrow) {
581 bool windowSupportsWideColor = true;
582 // Ordinarily we'd put a call to native_window_get_wide_color_support
583 // at the beginning of the function so that we'll have the
584 // result when needed elsewhere in the function.
585 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
586 // SurfaceFlinger is required to answer the call below we would
587 // end up in a deadlock situation. By moving the call to only happen
588 // if the application has specifically asked for wide-color we avoid
589 // the deadlock with SurfaceFlinger since it will not ask for a
590 // wide-color surface.
591 int err = native_window_get_wide_color_support(window, &windowSupportsWideColor);
592
593 if (err) {
594 ALOGE("processAttributes: invalid window (win=%p) "
595 "failed (%#x) (already connected to another API?)",
596 window, err);
597 return false;
598 }
599 if (!windowSupportsWideColor) {
600 // Application has asked for a wide-color colorspace but
601 // wide-color support isn't available on the display the window is on.
602 return false;
603 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600604 }
605 return true;
606}
607
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700608// Gets the native pixel format corrsponding to the passed EGLConfig.
609void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config,
610 android_pixel_format* format) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600611 // Set the native window's buffers format to match what this config requests.
612 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
613 // of our native format. So if sRGB gamma is requested, we have to
614 // modify the EGLconfig's format before setting the native window's
615 // format.
616
617 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
618 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
619
620 EGLint a = 0;
621 EGLint r, g, b;
622 r = g = b = 0;
623 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
624 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
625 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
626 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
627 EGLint colorDepth = r + g + b;
628
629 // Today, the driver only understands sRGB and linear on 888X
630 // formats. Strip other colorspaces from the attribute list and
631 // only use them to set the dataspace via
632 // native_window_set_buffers_dataspace
633 // if pixel format is RGBX 8888
634 // TBD: Can test for future extensions that indicate that driver
635 // handles requested color space and we can let it through.
636 // allow SRGB and LINEAR. All others need to be stripped.
637 // else if 565, 4444
638 // TBD: Can we assume these are supported if 8888 is?
639 // else if FP16 or 1010102
640 // strip colorspace from attribs.
641 // endif
642 if (a == 0) {
643 if (colorDepth <= 16) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700644 *format = HAL_PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600645 } else {
646 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
647 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700648 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600649 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700650 *format = HAL_PIXEL_FORMAT_RGBX_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600651 }
652 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700653 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600654 }
655 }
656 } else {
657 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
658 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700659 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600660 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700661 *format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600662 }
663 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700664 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600665 }
666 }
667}
668
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700669EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
670 android_smpte2086_metadata smpteMetadata;
671 if (s->getSmpte2086Metadata(smpteMetadata)) {
672 int err =
673 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
674 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700675 if (err != 0) {
676 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
677 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700678 return EGL_FALSE;
679 }
680 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700681 android_cta861_3_metadata cta8613Metadata;
682 if (s->getCta8613Metadata(cta8613Metadata)) {
683 int err =
684 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
685 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700686 if (err != 0) {
687 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
688 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700689 return EGL_FALSE;
690 }
691 }
692 return EGL_TRUE;
693}
694
Mathias Agopian518ec112011-05-13 16:21:08 -0700695EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
696 NativeWindowType window,
697 const EGLint *attrib_list)
698{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700699 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700700 clearError();
701
Yi Kong48a6cd22018-07-18 10:07:09 -0700702 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700703 egl_display_ptr dp = validate_display_connection(dpy, cnx);
704 if (dp) {
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800705 if (!window) {
706 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
707 }
708
709 int value = 0;
710 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
711 if (!value) {
712 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
713 }
714
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600715 // NOTE: When using Vulkan backend, the Vulkan runtime makes all the
716 // native_window_* calls, so don't do them here.
717 if (cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
718 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
719 if (result < 0) {
720 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
721 "failed (%#x) (already connected to another API?)",
722 window, result);
723 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
724 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700725 }
726
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700727 EGLDisplay iDpy = dp->disp.dpy;
728 android_pixel_format format;
729 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600730
731 // now select correct colorspace and dataspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700732 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700733 std::vector<EGLint> strippedAttribList;
734 if (!processAttributes(dp, window, format, attrib_list, &colorSpace,
735 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600736 ALOGE("error invalid colorspace: %d", colorSpace);
737 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700738 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700739 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800740
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600741 if (cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700742 int err = native_window_set_buffers_format(window, format);
743 if (err != 0) {
744 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700745 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700746 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
747 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
748 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700749
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600750 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
751 if (dataSpace != HAL_DATASPACE_UNKNOWN) {
752 err = native_window_set_buffers_data_space(window, dataSpace);
753 if (err != 0) {
754 ALOGE("error setting native window pixel dataSpace: %s (%d)", strerror(-err),
755 err);
756 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
757 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
758 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800759 }
760 }
761
Jamie Gennis59769462011-11-19 18:04:43 -0800762 // the EGL spec requires that a new EGLSurface default to swap interval
763 // 1, so explicitly set that on the window here.
764 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
765 anw->setSwapInterval(anw, 1);
766
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800768 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600770 egl_surface_t* s =
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700771 new egl_surface_t(dp.get(), config, window, surface,
772 getReportedColorSpace(colorSpace), cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700773 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700774 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700775
776 // EGLSurface creation failed
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600777 if (cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
778 native_window_set_buffers_format(window, 0);
779 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
780 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700781 }
782 return EGL_NO_SURFACE;
783}
784
785EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
786 NativePixmapType pixmap,
787 const EGLint *attrib_list)
788{
789 clearError();
790
Yi Kong48a6cd22018-07-18 10:07:09 -0700791 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700792 egl_display_ptr dp = validate_display_connection(dpy, cnx);
793 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700794 EGLDisplay iDpy = dp->disp.dpy;
795 android_pixel_format format;
796 getNativePixelFormat(iDpy, cnx, config, &format);
797
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600798 // now select a corresponding sRGB format if needed
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700799 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700800 std::vector<EGLint> strippedAttribList;
801 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
802 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600803 ALOGE("error invalid colorspace: %d", colorSpace);
804 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
805 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700806 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600807
Mathias Agopian518ec112011-05-13 16:21:08 -0700808 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800809 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700811 egl_surface_t* s =
Yi Kong48a6cd22018-07-18 10:07:09 -0700812 new egl_surface_t(dp.get(), config, nullptr, surface,
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700813 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700814 return s;
815 }
816 }
817 return EGL_NO_SURFACE;
818}
819
820EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
821 const EGLint *attrib_list)
822{
823 clearError();
824
Yi Kong48a6cd22018-07-18 10:07:09 -0700825 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700826 egl_display_ptr dp = validate_display_connection(dpy, cnx);
827 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600828 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700829 android_pixel_format format;
830 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600831
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700832 // Select correct colorspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700833 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700834 std::vector<EGLint> strippedAttribList;
835 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
836 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600837 ALOGE("error invalid colorspace: %d", colorSpace);
838 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
839 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700840 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600841
Mathias Agopian518ec112011-05-13 16:21:08 -0700842 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800843 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700844 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700845 egl_surface_t* s =
Yi Kong48a6cd22018-07-18 10:07:09 -0700846 new egl_surface_t(dp.get(), config, nullptr, surface,
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700847 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700848 return s;
849 }
850 }
851 return EGL_NO_SURFACE;
852}
Jesse Hall47743382013-02-08 11:13:46 -0800853
Mathias Agopian518ec112011-05-13 16:21:08 -0700854EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
855{
856 clearError();
857
Jesse Hallb29e5e82012-04-04 16:53:42 -0700858 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700859 if (!dp) return EGL_FALSE;
860
Jesse Hallb29e5e82012-04-04 16:53:42 -0700861 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700862 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800863 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700864
865 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800866 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700867 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700868 _s.terminate();
869 }
870 return result;
871}
872
873EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
874 EGLint attribute, EGLint *value)
875{
876 clearError();
877
Jesse Hallb29e5e82012-04-04 16:53:42 -0700878 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700879 if (!dp) return EGL_FALSE;
880
Jesse Hallb29e5e82012-04-04 16:53:42 -0700881 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700882 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800883 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700884
Mathias Agopian518ec112011-05-13 16:21:08 -0700885 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700886 if (s->getColorSpaceAttribute(attribute, value)) {
887 return EGL_TRUE;
888 } else if (s->getSmpte2086Attribute(attribute, value)) {
889 return EGL_TRUE;
890 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600891 return EGL_TRUE;
892 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700893 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700894}
895
Jamie Gennise8696a42012-01-15 18:54:57 -0800896void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800897 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800898 clearError();
899
Jesse Hallb29e5e82012-04-04 16:53:42 -0700900 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800901 if (!dp) {
902 return;
903 }
904
Jesse Hallb29e5e82012-04-04 16:53:42 -0700905 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800906 if (!_s.get()) {
907 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800908 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800909}
910
Mathias Agopian518ec112011-05-13 16:21:08 -0700911// ----------------------------------------------------------------------------
912// Contexts
913// ----------------------------------------------------------------------------
914
915EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
916 EGLContext share_list, const EGLint *attrib_list)
917{
918 clearError();
919
Yi Kong48a6cd22018-07-18 10:07:09 -0700920 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700921 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700922 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700923 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700924 if (!ContextRef(dp.get(), share_list).get()) {
925 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
926 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700927 egl_context_t* const c = get_context(share_list);
928 share_list = c->context;
929 }
930 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800931 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700932 if (context != EGL_NO_CONTEXT) {
933 // figure out if it's a GLESv1 or GLESv2
934 int version = 0;
935 if (attrib_list) {
936 while (*attrib_list != EGL_NONE) {
937 GLint attr = *attrib_list++;
938 GLint value = *attrib_list++;
939 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
940 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800941 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800942 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800943 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700944 }
945 }
946 };
947 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700948 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
949 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700950 return c;
951 }
952 }
953 return EGL_NO_CONTEXT;
954}
955
956EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
957{
958 clearError();
959
Jesse Hallb29e5e82012-04-04 16:53:42 -0700960 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700961 if (!dp)
962 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700963
Jesse Hallb29e5e82012-04-04 16:53:42 -0700964 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700965 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800966 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800967
Mathias Agopian518ec112011-05-13 16:21:08 -0700968 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800969 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700970 if (result == EGL_TRUE) {
971 _c.terminate();
972 }
973 return result;
974}
975
Mathias Agopian518ec112011-05-13 16:21:08 -0700976EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
977 EGLSurface read, EGLContext ctx)
978{
979 clearError();
980
Jesse Hallb29e5e82012-04-04 16:53:42 -0700981 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800982 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700983
Mathias Agopian5b287a62011-05-16 18:58:55 -0700984 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
985 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
986 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700987 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
988 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800989 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700990 }
991
992 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700993 ContextRef _c(dp.get(), ctx);
994 SurfaceRef _d(dp.get(), draw);
995 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700996
997 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700998 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700999 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -08001000 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001001 }
1002
1003 // these are the underlying implementation's object
1004 EGLContext impl_ctx = EGL_NO_CONTEXT;
1005 EGLSurface impl_draw = EGL_NO_SURFACE;
1006 EGLSurface impl_read = EGL_NO_SURFACE;
1007
1008 // these are our objects structs passed in
Yi Kong48a6cd22018-07-18 10:07:09 -07001009 egl_context_t * c = nullptr;
1010 egl_surface_t const * d = nullptr;
1011 egl_surface_t const * r = nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001012
1013 // these are the current objects structs
1014 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001015
Mathias Agopian518ec112011-05-13 16:21:08 -07001016 if (ctx != EGL_NO_CONTEXT) {
1017 c = get_context(ctx);
1018 impl_ctx = c->context;
1019 } else {
1020 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001021 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1022 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001023 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001024 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001025 if (cur_c == nullptr) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001026 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001027 // not an error, there is just no current context.
1028 return EGL_TRUE;
1029 }
1030 }
1031
1032 // retrieve the underlying implementation's draw EGLSurface
1033 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001034 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001035 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001036 impl_draw = d->surface;
1037 }
1038
1039 // retrieve the underlying implementation's read EGLSurface
1040 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001041 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001042 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001043 impl_read = r->surface;
1044 }
1045
Mathias Agopian518ec112011-05-13 16:21:08 -07001046
Jesse Hallb29e5e82012-04-04 16:53:42 -07001047 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001048 draw, read, ctx,
1049 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001050
1051 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001052 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001053 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1054 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001055 _c.acquire();
1056 _r.acquire();
1057 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001058 } else {
1059 setGLHooksThreadSpecific(&gHooksNoContext);
1060 egl_tls_t::setContext(EGL_NO_CONTEXT);
1061 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001062 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001063 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001064 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001065 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001066 }
1067 return result;
1068}
1069
1070
1071EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1072 EGLint attribute, EGLint *value)
1073{
1074 clearError();
1075
Jesse Hallb29e5e82012-04-04 16:53:42 -07001076 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001077 if (!dp) return EGL_FALSE;
1078
Jesse Hallb29e5e82012-04-04 16:53:42 -07001079 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001080 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001081
Mathias Agopian518ec112011-05-13 16:21:08 -07001082 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001083 return c->cnx->egl.eglQueryContext(
1084 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001085
Mathias Agopian518ec112011-05-13 16:21:08 -07001086}
1087
1088EGLContext eglGetCurrentContext(void)
1089{
1090 // could be called before eglInitialize(), but we wouldn't have a context
1091 // then, and this function would correctly return EGL_NO_CONTEXT.
1092
1093 clearError();
1094
1095 EGLContext ctx = getContext();
1096 return ctx;
1097}
1098
1099EGLSurface eglGetCurrentSurface(EGLint readdraw)
1100{
1101 // could be called before eglInitialize(), but we wouldn't have a context
1102 // then, and this function would correctly return EGL_NO_SURFACE.
1103
1104 clearError();
1105
1106 EGLContext ctx = getContext();
1107 if (ctx) {
1108 egl_context_t const * const c = get_context(ctx);
1109 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1110 switch (readdraw) {
1111 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001112 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001113 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1114 }
1115 }
1116 return EGL_NO_SURFACE;
1117}
1118
1119EGLDisplay eglGetCurrentDisplay(void)
1120{
1121 // could be called before eglInitialize(), but we wouldn't have a context
1122 // then, and this function would correctly return EGL_NO_DISPLAY.
1123
1124 clearError();
1125
1126 EGLContext ctx = getContext();
1127 if (ctx) {
1128 egl_context_t const * const c = get_context(ctx);
1129 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1130 return c->dpy;
1131 }
1132 return EGL_NO_DISPLAY;
1133}
1134
1135EGLBoolean eglWaitGL(void)
1136{
Mathias Agopian518ec112011-05-13 16:21:08 -07001137 clearError();
1138
Mathias Agopianada798b2012-02-13 17:09:30 -08001139 egl_connection_t* const cnx = &gEGLImpl;
1140 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001141 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001142
1143 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001144}
1145
1146EGLBoolean eglWaitNative(EGLint engine)
1147{
Mathias Agopian518ec112011-05-13 16:21:08 -07001148 clearError();
1149
Mathias Agopianada798b2012-02-13 17:09:30 -08001150 egl_connection_t* const cnx = &gEGLImpl;
1151 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001152 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001153
1154 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001155}
1156
1157EGLint eglGetError(void)
1158{
Mathias Agopianada798b2012-02-13 17:09:30 -08001159 EGLint err = EGL_SUCCESS;
1160 egl_connection_t* const cnx = &gEGLImpl;
1161 if (cnx->dso) {
1162 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001163 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001164 if (err == EGL_SUCCESS) {
1165 err = egl_tls_t::getError();
1166 }
1167 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001168}
1169
Michael Chockc0ec5e22014-01-27 08:14:33 -08001170static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001171 const char* procname) {
1172 const egl_connection_t* cnx = &gEGLImpl;
Yi Kong48a6cd22018-07-18 10:07:09 -07001173 void* proc = nullptr;
Jesse Hallc07b5202013-07-04 12:08:16 -07001174
Michael Chockc0ec5e22014-01-27 08:14:33 -08001175 proc = dlsym(cnx->libEgl, procname);
1176 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1177
Jesse Hallc07b5202013-07-04 12:08:16 -07001178 proc = dlsym(cnx->libGles2, procname);
1179 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1180
1181 proc = dlsym(cnx->libGles1, procname);
1182 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1183
Yi Kong48a6cd22018-07-18 10:07:09 -07001184 return nullptr;
Jesse Hallc07b5202013-07-04 12:08:16 -07001185}
1186
Mathias Agopian518ec112011-05-13 16:21:08 -07001187__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1188{
1189 // eglGetProcAddress() could be the very first function called
1190 // in which case we must make sure we've initialized ourselves, this
1191 // happens the first time egl_get_display() is called.
1192
1193 clearError();
1194
1195 if (egl_init_drivers() == EGL_FALSE) {
1196 setError(EGL_BAD_PARAMETER, NULL);
Yi Kong48a6cd22018-07-18 10:07:09 -07001197 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 }
1199
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001200 if (FILTER_EXTENSIONS(procname)) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001201 return nullptr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001202 }
1203
Mathias Agopian518ec112011-05-13 16:21:08 -07001204 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001205 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001206 if (addr) return addr;
1207
Michael Chockc0ec5e22014-01-27 08:14:33 -08001208 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001209 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001210
Mathias Agopian518ec112011-05-13 16:21:08 -07001211 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1212 pthread_mutex_lock(&sExtensionMapMutex);
1213
1214 /*
1215 * Since eglGetProcAddress() is not associated to anything, it needs
1216 * to return a function pointer that "works" regardless of what
1217 * the current context is.
1218 *
1219 * For this reason, we return a "forwarder", a small stub that takes
1220 * care of calling the function associated with the context
1221 * currently bound.
1222 *
1223 * We first look for extensions we've already resolved, if we're seeing
1224 * this extension for the first time, we go through all our
1225 * implementations and call eglGetProcAddress() and record the
1226 * result in the appropriate implementation hooks and return the
1227 * address of the forwarder corresponding to that hook set.
1228 *
1229 */
1230
Mathias Agopian65421432017-03-08 11:49:05 -08001231 const std::string name(procname);
1232
1233 auto& extentionMap = sGLExtentionMap;
1234 auto pos = extentionMap.find(name);
1235 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001236 const int slot = sGLExtentionSlot;
1237
Steve Blocke6f43dd2012-01-06 19:20:56 +00001238 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001239 "no more slots for eglGetProcAddress(\"%s\")",
1240 procname);
1241
1242 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1243 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001244
1245 egl_connection_t* const cnx = &gEGLImpl;
1246 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001247 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001248 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001249 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1250 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001251 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001252 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001253 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001254
Mathias Agopian518ec112011-05-13 16:21:08 -07001255 if (found) {
1256 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001257 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001258 sGLExtentionSlot++;
1259 }
1260 }
1261
1262 pthread_mutex_unlock(&sExtensionMapMutex);
1263 return addr;
1264}
1265
Mathias Agopian65421432017-03-08 11:49:05 -08001266class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001267public:
1268
1269 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001270 static FrameCompletionThread thread;
1271
1272 char name[64];
1273
1274 std::lock_guard<std::mutex> lock(thread.mMutex);
1275 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1276 ATRACE_NAME(name);
1277
1278 thread.mQueue.push_back(sync);
1279 thread.mCondition.notify_one();
1280 thread.mFramesQueued++;
1281 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001282 }
1283
1284private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001285
Mathias Agopian65421432017-03-08 11:49:05 -08001286 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1287 std::thread thread(&FrameCompletionThread::loop, this);
1288 thread.detach();
1289 }
1290
1291#pragma clang diagnostic push
1292#pragma clang diagnostic ignored "-Wmissing-noreturn"
1293 void loop() {
1294 while (true) {
1295 threadLoop();
1296 }
1297 }
1298#pragma clang diagnostic pop
1299
1300 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001301 EGLSyncKHR sync;
1302 uint32_t frameNum;
1303 {
Mathias Agopian65421432017-03-08 11:49:05 -08001304 std::unique_lock<std::mutex> lock(mMutex);
1305 while (mQueue.empty()) {
1306 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001307 }
1308 sync = mQueue[0];
1309 frameNum = mFramesCompleted;
1310 }
1311 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1312 {
Mathias Agopian65421432017-03-08 11:49:05 -08001313 char name[64];
1314 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1315 ATRACE_NAME(name);
1316
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001317 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1318 if (result == EGL_FALSE) {
1319 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1320 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1321 ALOGE("FrameCompletion: timeout waiting for fence");
1322 }
1323 eglDestroySyncKHR(dpy, sync);
1324 }
1325 {
Mathias Agopian65421432017-03-08 11:49:05 -08001326 std::lock_guard<std::mutex> lock(mMutex);
1327 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001328 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001329 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001330 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001331 }
1332
1333 uint32_t mFramesQueued;
1334 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001335 std::deque<EGLSyncKHR> mQueue;
1336 std::condition_variable mCondition;
1337 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001338};
1339
Dan Stozaa894d082015-02-19 15:27:36 -08001340EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1341 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001342{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001343 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001344 clearError();
1345
Jesse Hallb29e5e82012-04-04 16:53:42 -07001346 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001347 if (!dp) return EGL_FALSE;
1348
Jesse Hallb29e5e82012-04-04 16:53:42 -07001349 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001350 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001351 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001352
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001353 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001354
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001355 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001356 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001357 if (sync != EGL_NO_SYNC_KHR) {
1358 FrameCompletionThread::queueSync(sync);
1359 }
1360 }
1361
Mathias Agopian7db993a2012-03-25 00:49:46 -07001362 if (CC_UNLIKELY(dp->finishOnSwap)) {
1363 uint32_t pixel;
1364 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1365 if (c) {
1366 // glReadPixels() ensures that the frame is complete
1367 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1368 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1369 }
1370 }
1371
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -06001372 if (s->cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
1373 if (!sendSurfaceMetadata(s)) {
1374 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1375 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1376 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001377 }
1378
Dan Stozaa894d082015-02-19 15:27:36 -08001379 if (n_rects == 0) {
1380 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1381 }
1382
Mathias Agopian65421432017-03-08 11:49:05 -08001383 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001384 for (int r = 0; r < n_rects; ++r) {
1385 int offset = r * 4;
1386 int x = rects[offset];
1387 int y = rects[offset + 1];
1388 int width = rects[offset + 2];
1389 int height = rects[offset + 3];
1390 android_native_rect_t androidRect;
1391 androidRect.left = x;
1392 androidRect.top = y + height;
1393 androidRect.right = x + width;
1394 androidRect.bottom = y;
1395 androidRects.push_back(androidRect);
1396 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -06001397 if (s->cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
1398 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(),
1399 androidRects.size());
1400 }
Dan Stozaa894d082015-02-19 15:27:36 -08001401
1402 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1403 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1404 rects, n_rects);
1405 } else {
1406 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1407 }
1408}
1409
1410EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1411{
Yi Kong48a6cd22018-07-18 10:07:09 -07001412 return eglSwapBuffersWithDamageKHR(dpy, surface, nullptr, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001413}
1414
1415EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1416 NativePixmapType target)
1417{
1418 clearError();
1419
Jesse Hallb29e5e82012-04-04 16:53:42 -07001420 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001421 if (!dp) return EGL_FALSE;
1422
Jesse Hallb29e5e82012-04-04 16:53:42 -07001423 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001424 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001425 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001426
Mathias Agopian518ec112011-05-13 16:21:08 -07001427 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001428 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001429}
1430
1431const char* eglQueryString(EGLDisplay dpy, EGLint name)
1432{
1433 clearError();
1434
Chia-I Wue57d1352016-08-15 16:10:02 +08001435 // Generate an error quietly when client extensions (as defined by
1436 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1437 // validate_display to generate the error as validate_display would log
1438 // the error, which can be misleading.
1439 //
1440 // If we want to support EGL_EXT_client_extensions later, we can return
1441 // the client extension string here instead.
1442 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -07001443 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)nullptr);
Chia-I Wue57d1352016-08-15 16:10:02 +08001444
Jesse Hallb29e5e82012-04-04 16:53:42 -07001445 const egl_display_ptr dp = validate_display(dpy);
Yi Kong48a6cd22018-07-18 10:07:09 -07001446 if (!dp) return (const char *) nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001447
1448 switch (name) {
1449 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001450 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001451 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001452 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001453 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001454 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001455 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001456 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001457 default:
1458 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001459 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001460 return setError(EGL_BAD_PARAMETER, (const char *)nullptr);
Mathias Agopian518ec112011-05-13 16:21:08 -07001461}
1462
Jiyong Park00b15b82017-08-10 20:30:56 +09001463extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001464{
1465 clearError();
1466
1467 const egl_display_ptr dp = validate_display(dpy);
Yi Kong48a6cd22018-07-18 10:07:09 -07001468 if (!dp) return (const char *) nullptr;
Mathias Agopianca088332013-03-28 17:44:13 -07001469
1470 switch (name) {
1471 case EGL_VENDOR:
1472 return dp->disp.queryString.vendor;
1473 case EGL_VERSION:
1474 return dp->disp.queryString.version;
1475 case EGL_EXTENSIONS:
1476 return dp->disp.queryString.extensions;
1477 case EGL_CLIENT_APIS:
1478 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001479 default:
1480 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001481 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001482 return setError(EGL_BAD_PARAMETER, (const char *)nullptr);
Mathias Agopianca088332013-03-28 17:44:13 -07001483}
Mathias Agopian518ec112011-05-13 16:21:08 -07001484
1485// ----------------------------------------------------------------------------
1486// EGL 1.1
1487// ----------------------------------------------------------------------------
1488
1489EGLBoolean eglSurfaceAttrib(
1490 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1491{
1492 clearError();
1493
Jesse Hallb29e5e82012-04-04 16:53:42 -07001494 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001495 if (!dp) return EGL_FALSE;
1496
Jesse Hallb29e5e82012-04-04 16:53:42 -07001497 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001498 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001499 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001500
Pablo Ceballosc18be292016-05-31 14:55:42 -07001501 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001502
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001503 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001504 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001505 setError(EGL_BAD_SURFACE, EGL_FALSE);
1506 }
Mathias Agopian65421432017-03-08 11:49:05 -08001507 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1508 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001509 }
1510
Pablo Ceballosc18be292016-05-31 14:55:42 -07001511 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001512 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001513 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001514 }
Mathias Agopian65421432017-03-08 11:49:05 -08001515 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1516 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001517 }
1518
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001519 if (s->setSmpte2086Attribute(attribute, value)) {
1520 return EGL_TRUE;
1521 } else if (s->setCta8613Attribute(attribute, value)) {
1522 return EGL_TRUE;
1523 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001524 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001525 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001526 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001527 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001528}
1529
1530EGLBoolean eglBindTexImage(
1531 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1532{
1533 clearError();
1534
Jesse Hallb29e5e82012-04-04 16:53:42 -07001535 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001536 if (!dp) return EGL_FALSE;
1537
Jesse Hallb29e5e82012-04-04 16:53:42 -07001538 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001539 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001540 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001541
Mathias Agopian518ec112011-05-13 16:21:08 -07001542 egl_surface_t const * const s = get_surface(surface);
1543 if (s->cnx->egl.eglBindTexImage) {
1544 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001545 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001546 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001547 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001548}
1549
1550EGLBoolean eglReleaseTexImage(
1551 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1552{
1553 clearError();
1554
Jesse Hallb29e5e82012-04-04 16:53:42 -07001555 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001556 if (!dp) return EGL_FALSE;
1557
Jesse Hallb29e5e82012-04-04 16:53:42 -07001558 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001559 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001560 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001561
Mathias Agopian518ec112011-05-13 16:21:08 -07001562 egl_surface_t const * const s = get_surface(surface);
1563 if (s->cnx->egl.eglReleaseTexImage) {
1564 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001565 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001566 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001567 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001568}
1569
1570EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1571{
1572 clearError();
1573
Jesse Hallb29e5e82012-04-04 16:53:42 -07001574 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001575 if (!dp) return EGL_FALSE;
1576
1577 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001578 egl_connection_t* const cnx = &gEGLImpl;
1579 if (cnx->dso && cnx->egl.eglSwapInterval) {
1580 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001581 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001582
Mathias Agopian518ec112011-05-13 16:21:08 -07001583 return res;
1584}
1585
1586
1587// ----------------------------------------------------------------------------
1588// EGL 1.2
1589// ----------------------------------------------------------------------------
1590
1591EGLBoolean eglWaitClient(void)
1592{
1593 clearError();
1594
Mathias Agopianada798b2012-02-13 17:09:30 -08001595 egl_connection_t* const cnx = &gEGLImpl;
1596 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001597 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001598
1599 EGLBoolean res;
1600 if (cnx->egl.eglWaitClient) {
1601 res = cnx->egl.eglWaitClient();
1602 } else {
1603 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001604 }
1605 return res;
1606}
1607
1608EGLBoolean eglBindAPI(EGLenum api)
1609{
1610 clearError();
1611
1612 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001613 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001614 }
1615
1616 // bind this API on all EGLs
1617 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001618 egl_connection_t* const cnx = &gEGLImpl;
1619 if (cnx->dso && cnx->egl.eglBindAPI) {
1620 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001621 }
1622 return res;
1623}
1624
1625EGLenum eglQueryAPI(void)
1626{
1627 clearError();
1628
1629 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001630 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001631 }
1632
Mathias Agopianada798b2012-02-13 17:09:30 -08001633 egl_connection_t* const cnx = &gEGLImpl;
1634 if (cnx->dso && cnx->egl.eglQueryAPI) {
1635 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001636 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001637
Mathias Agopian518ec112011-05-13 16:21:08 -07001638 // or, it can only be OpenGL ES
1639 return EGL_OPENGL_ES_API;
1640}
1641
1642EGLBoolean eglReleaseThread(void)
1643{
1644 clearError();
1645
Mathias Agopianada798b2012-02-13 17:09:30 -08001646 egl_connection_t* const cnx = &gEGLImpl;
1647 if (cnx->dso && cnx->egl.eglReleaseThread) {
1648 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001649 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301650
1651 // If there is context bound to the thread, release it
1652 egl_display_t::loseCurrent(get_context(getContext()));
1653
Mathias Agopian518ec112011-05-13 16:21:08 -07001654 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001655 return EGL_TRUE;
1656}
1657
1658EGLSurface eglCreatePbufferFromClientBuffer(
1659 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1660 EGLConfig config, const EGLint *attrib_list)
1661{
1662 clearError();
1663
Yi Kong48a6cd22018-07-18 10:07:09 -07001664 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -07001665 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1666 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001667 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1668 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001669 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001670 }
1671 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1672}
1673
1674// ----------------------------------------------------------------------------
1675// EGL_EGLEXT_VERSION 3
1676// ----------------------------------------------------------------------------
1677
1678EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1679 const EGLint *attrib_list)
1680{
1681 clearError();
1682
Jesse Hallb29e5e82012-04-04 16:53:42 -07001683 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001684 if (!dp) return EGL_FALSE;
1685
Jesse Hallb29e5e82012-04-04 16:53:42 -07001686 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001687 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001688 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001689
1690 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001691 if (s->cnx->egl.eglLockSurfaceKHR) {
1692 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001693 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001694 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001695 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001696}
1697
1698EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1699{
1700 clearError();
1701
Jesse Hallb29e5e82012-04-04 16:53:42 -07001702 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001703 if (!dp) return EGL_FALSE;
1704
Jesse Hallb29e5e82012-04-04 16:53:42 -07001705 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001706 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001707 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001708
1709 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001710 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001711 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001712 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001713 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001714}
1715
1716EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1717 EGLClientBuffer buffer, const EGLint *attrib_list)
1718{
1719 clearError();
1720
Jesse Hallb29e5e82012-04-04 16:53:42 -07001721 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001722 if (!dp) return EGL_NO_IMAGE_KHR;
1723
Jesse Hallb29e5e82012-04-04 16:53:42 -07001724 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001725 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001726
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001727 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1728 egl_connection_t* const cnx = &gEGLImpl;
1729 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1730 result = cnx->egl.eglCreateImageKHR(
1731 dp->disp.dpy,
1732 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosiński30c1d902018-06-27 17:14:33 -07001733 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001734 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001735 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001736}
1737
1738EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1739{
1740 clearError();
1741
Jesse Hallb29e5e82012-04-04 16:53:42 -07001742 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001743 if (!dp) return EGL_FALSE;
1744
Steven Holte646a5c52012-06-04 20:02:11 -07001745 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001746 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001747 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001748 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001749 }
Steven Holte646a5c52012-06-04 20:02:11 -07001750 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001751}
1752
1753// ----------------------------------------------------------------------------
1754// EGL_EGLEXT_VERSION 5
1755// ----------------------------------------------------------------------------
1756
1757
1758EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1759{
1760 clearError();
1761
Jesse Hallb29e5e82012-04-04 16:53:42 -07001762 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001763 if (!dp) return EGL_NO_SYNC_KHR;
1764
Mathias Agopian518ec112011-05-13 16:21:08 -07001765 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001766 egl_connection_t* const cnx = &gEGLImpl;
1767 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1768 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001769 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001770 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001771}
1772
1773EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1774{
1775 clearError();
1776
Jesse Hallb29e5e82012-04-04 16:53:42 -07001777 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001778 if (!dp) return EGL_FALSE;
1779
Mathias Agopian518ec112011-05-13 16:21:08 -07001780 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001781 egl_connection_t* const cnx = &gEGLImpl;
1782 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1783 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001784 }
1785 return result;
1786}
1787
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001788EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1789 clearError();
1790
1791 const egl_display_ptr dp = validate_display(dpy);
1792 if (!dp) return EGL_FALSE;
1793
1794 EGLBoolean result = EGL_FALSE;
1795 egl_connection_t* const cnx = &gEGLImpl;
1796 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1797 result = cnx->egl.eglSignalSyncKHR(
1798 dp->disp.dpy, sync, mode);
1799 }
1800 return result;
1801}
1802
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001803EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1804 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001805{
1806 clearError();
1807
Jesse Hallb29e5e82012-04-04 16:53:42 -07001808 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001809 if (!dp) return EGL_FALSE;
1810
Mathias Agopian737b8962017-02-24 14:32:05 -08001811 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001812 egl_connection_t* const cnx = &gEGLImpl;
1813 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1814 result = cnx->egl.eglClientWaitSyncKHR(
1815 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001816 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001817 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001818}
1819
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001820EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1821 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001822{
1823 clearError();
1824
Jesse Hallb29e5e82012-04-04 16:53:42 -07001825 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001826 if (!dp) return EGL_FALSE;
1827
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001828 EGLBoolean result = EGL_FALSE;
1829 egl_connection_t* const cnx = &gEGLImpl;
1830 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1831 result = cnx->egl.eglGetSyncAttribKHR(
1832 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001833 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001834 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001835}
1836
Season Li000d88f2015-07-01 11:39:40 -07001837EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1838{
1839 clearError();
1840
1841 const egl_display_ptr dp = validate_display(dpy);
1842 if (!dp) return EGL_NO_STREAM_KHR;
1843
1844 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1845 egl_connection_t* const cnx = &gEGLImpl;
1846 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1847 result = cnx->egl.eglCreateStreamKHR(
1848 dp->disp.dpy, attrib_list);
1849 }
1850 return result;
1851}
1852
1853EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1854{
1855 clearError();
1856
1857 const egl_display_ptr dp = validate_display(dpy);
1858 if (!dp) return EGL_FALSE;
1859
1860 EGLBoolean result = EGL_FALSE;
1861 egl_connection_t* const cnx = &gEGLImpl;
1862 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1863 result = cnx->egl.eglDestroyStreamKHR(
1864 dp->disp.dpy, stream);
1865 }
1866 return result;
1867}
1868
1869EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1870 EGLenum attribute, EGLint value)
1871{
1872 clearError();
1873
1874 const egl_display_ptr dp = validate_display(dpy);
1875 if (!dp) return EGL_FALSE;
1876
1877 EGLBoolean result = EGL_FALSE;
1878 egl_connection_t* const cnx = &gEGLImpl;
1879 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1880 result = cnx->egl.eglStreamAttribKHR(
1881 dp->disp.dpy, stream, attribute, value);
1882 }
1883 return result;
1884}
1885
1886EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1887 EGLenum attribute, EGLint *value)
1888{
1889 clearError();
1890
1891 const egl_display_ptr dp = validate_display(dpy);
1892 if (!dp) return EGL_FALSE;
1893
1894 EGLBoolean result = EGL_FALSE;
1895 egl_connection_t* const cnx = &gEGLImpl;
1896 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1897 result = cnx->egl.eglQueryStreamKHR(
1898 dp->disp.dpy, stream, attribute, value);
1899 }
1900 return result;
1901}
1902
1903EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1904 EGLenum attribute, EGLuint64KHR *value)
1905{
1906 clearError();
1907
1908 const egl_display_ptr dp = validate_display(dpy);
1909 if (!dp) return EGL_FALSE;
1910
1911 EGLBoolean result = EGL_FALSE;
1912 egl_connection_t* const cnx = &gEGLImpl;
1913 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1914 result = cnx->egl.eglQueryStreamu64KHR(
1915 dp->disp.dpy, stream, attribute, value);
1916 }
1917 return result;
1918}
1919
1920EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1921 EGLenum attribute, EGLTimeKHR *value)
1922{
1923 clearError();
1924
1925 const egl_display_ptr dp = validate_display(dpy);
1926 if (!dp) return EGL_FALSE;
1927
1928 EGLBoolean result = EGL_FALSE;
1929 egl_connection_t* const cnx = &gEGLImpl;
1930 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1931 result = cnx->egl.eglQueryStreamTimeKHR(
1932 dp->disp.dpy, stream, attribute, value);
1933 }
1934 return result;
1935}
1936
1937EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1938 EGLStreamKHR stream, const EGLint *attrib_list)
1939{
1940 clearError();
1941
1942 egl_display_ptr dp = validate_display(dpy);
1943 if (!dp) return EGL_NO_SURFACE;
1944
1945 egl_connection_t* const cnx = &gEGLImpl;
1946 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1947 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1948 dp->disp.dpy, config, stream, attrib_list);
1949 if (surface != EGL_NO_SURFACE) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001950 egl_surface_t* s = new egl_surface_t(dp.get(), config, nullptr, surface,
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001951 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001952 return s;
1953 }
1954 }
1955 return EGL_NO_SURFACE;
1956}
1957
1958EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1959 EGLStreamKHR stream)
1960{
1961 clearError();
1962
1963 const egl_display_ptr dp = validate_display(dpy);
1964 if (!dp) return EGL_FALSE;
1965
1966 EGLBoolean result = EGL_FALSE;
1967 egl_connection_t* const cnx = &gEGLImpl;
1968 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1969 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1970 dp->disp.dpy, stream);
1971 }
1972 return result;
1973}
1974
1975EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1976 EGLStreamKHR stream)
1977{
1978 clearError();
1979
1980 const egl_display_ptr dp = validate_display(dpy);
1981 if (!dp) return EGL_FALSE;
1982
1983 EGLBoolean result = EGL_FALSE;
1984 egl_connection_t* const cnx = &gEGLImpl;
1985 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1986 result = cnx->egl.eglStreamConsumerAcquireKHR(
1987 dp->disp.dpy, stream);
1988 }
1989 return result;
1990}
1991
1992EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1993 EGLStreamKHR stream)
1994{
1995 clearError();
1996
1997 const egl_display_ptr dp = validate_display(dpy);
1998 if (!dp) return EGL_FALSE;
1999
2000 EGLBoolean result = EGL_FALSE;
2001 egl_connection_t* const cnx = &gEGLImpl;
2002 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2003 result = cnx->egl.eglStreamConsumerReleaseKHR(
2004 dp->disp.dpy, stream);
2005 }
2006 return result;
2007}
2008
2009EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2010 EGLDisplay dpy, EGLStreamKHR stream)
2011{
2012 clearError();
2013
2014 const egl_display_ptr dp = validate_display(dpy);
2015 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2016
2017 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2018 egl_connection_t* const cnx = &gEGLImpl;
2019 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2020 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2021 dp->disp.dpy, stream);
2022 }
2023 return result;
2024}
2025
2026EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2027 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2028{
2029 clearError();
2030
2031 const egl_display_ptr dp = validate_display(dpy);
2032 if (!dp) return EGL_NO_STREAM_KHR;
2033
2034 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2035 egl_connection_t* const cnx = &gEGLImpl;
2036 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2037 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2038 dp->disp.dpy, file_descriptor);
2039 }
2040 return result;
2041}
2042
Mathias Agopian518ec112011-05-13 16:21:08 -07002043// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002044// EGL_EGLEXT_VERSION 15
2045// ----------------------------------------------------------------------------
2046
2047EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2048 clearError();
2049 const egl_display_ptr dp = validate_display(dpy);
2050 if (!dp) return EGL_FALSE;
2051 EGLint result = EGL_FALSE;
2052 egl_connection_t* const cnx = &gEGLImpl;
2053 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2054 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2055 }
2056 return result;
2057}
2058
2059// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002060// ANDROID extensions
2061// ----------------------------------------------------------------------------
2062
Jamie Gennis331841b2012-09-06 14:52:00 -07002063EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2064{
2065 clearError();
2066
2067 const egl_display_ptr dp = validate_display(dpy);
2068 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2069
2070 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2071 egl_connection_t* const cnx = &gEGLImpl;
2072 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2073 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2074 }
2075 return result;
2076}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002077
Andy McFadden72841452013-03-01 16:25:32 -08002078EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2079 EGLnsecsANDROID time)
2080{
2081 clearError();
2082
2083 const egl_display_ptr dp = validate_display(dpy);
2084 if (!dp) {
2085 return EGL_FALSE;
2086 }
2087
2088 SurfaceRef _s(dp.get(), surface);
2089 if (!_s.get()) {
2090 setError(EGL_BAD_SURFACE, EGL_FALSE);
2091 return EGL_FALSE;
2092 }
2093
2094 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002095 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002096
2097 return EGL_TRUE;
2098}
2099
Craig Donner60761072017-01-27 12:30:44 -08002100EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2101 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002102 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2103 // this function cannot be implemented when this libEGL is built for
2104 // vendors.
2105#ifndef __ANDROID_VNDK__
Yi Kong48a6cd22018-07-18 10:07:09 -07002106 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer) nullptr);
Mathias Agopian61963402017-02-24 16:38:15 -08002107 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002108#else
Yi Kong48a6cd22018-07-18 10:07:09 -07002109 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer) nullptr);
Jiyong Parka243e5d2017-06-21 12:26:51 +09002110#endif
Craig Donner60761072017-01-27 12:30:44 -08002111}
2112
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002113// ----------------------------------------------------------------------------
2114// NVIDIA extensions
2115// ----------------------------------------------------------------------------
2116EGLuint64NV eglGetSystemTimeFrequencyNV()
2117{
2118 clearError();
2119
2120 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002121 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002122 }
2123
2124 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002125 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002126
Mathias Agopianada798b2012-02-13 17:09:30 -08002127 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2128 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002129 }
2130
Mathias Agopian737b8962017-02-24 14:32:05 -08002131 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002132}
2133
2134EGLuint64NV eglGetSystemTimeNV()
2135{
2136 clearError();
2137
2138 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002139 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002140 }
2141
2142 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002143 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002144
Mathias Agopianada798b2012-02-13 17:09:30 -08002145 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2146 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002147 }
2148
Mathias Agopian737b8962017-02-24 14:32:05 -08002149 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002150}
Dan Stozaa894d082015-02-19 15:27:36 -08002151
2152// ----------------------------------------------------------------------------
2153// Partial update extension
2154// ----------------------------------------------------------------------------
2155EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2156 EGLint *rects, EGLint n_rects)
2157{
2158 clearError();
2159
2160 const egl_display_ptr dp = validate_display(dpy);
2161 if (!dp) {
2162 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2163 return EGL_FALSE;
2164 }
2165
2166 SurfaceRef _s(dp.get(), surface);
2167 if (!_s.get()) {
2168 setError(EGL_BAD_SURFACE, EGL_FALSE);
2169 return EGL_FALSE;
2170 }
2171
2172 egl_surface_t const * const s = get_surface(surface);
2173 if (s->cnx->egl.eglSetDamageRegionKHR) {
2174 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2175 rects, n_rects);
2176 }
2177
2178 return EGL_FALSE;
2179}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002180
Brian Anderson1049d1d2016-12-16 17:25:57 -08002181EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2182 EGLuint64KHR *frameId) {
2183 clearError();
2184
2185 const egl_display_ptr dp = validate_display(dpy);
2186 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002187 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002188 }
2189
2190 SurfaceRef _s(dp.get(), surface);
2191 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002192 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002193 }
2194
2195 egl_surface_t const * const s = get_surface(surface);
2196
Mathias Agopian65421432017-03-08 11:49:05 -08002197 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002198 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002199 }
2200
2201 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002202 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002203
Mathias Agopian65421432017-03-08 11:49:05 -08002204 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002205 // This should not happen. Return an error that is not in the spec
2206 // so it's obvious something is very wrong.
2207 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002208 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002209 }
2210
2211 *frameId = nextFrameId;
2212 return EGL_TRUE;
2213}
2214
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002215EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2216 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2217{
2218 clearError();
2219
2220 const egl_display_ptr dp = validate_display(dpy);
2221 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002222 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002223 }
2224
2225 SurfaceRef _s(dp.get(), surface);
2226 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002227 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002228 }
2229
2230 egl_surface_t const * const s = get_surface(surface);
2231
Mathias Agopian65421432017-03-08 11:49:05 -08002232 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002233 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002234 }
2235
2236 nsecs_t* compositeDeadline = nullptr;
2237 nsecs_t* compositeInterval = nullptr;
2238 nsecs_t* compositeToPresentLatency = nullptr;
2239
2240 for (int i = 0; i < numTimestamps; i++) {
2241 switch (names[i]) {
2242 case EGL_COMPOSITE_DEADLINE_ANDROID:
2243 compositeDeadline = &values[i];
2244 break;
2245 case EGL_COMPOSITE_INTERVAL_ANDROID:
2246 compositeInterval = &values[i];
2247 break;
2248 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2249 compositeToPresentLatency = &values[i];
2250 break;
2251 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002252 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002253 }
2254 }
2255
Mathias Agopian65421432017-03-08 11:49:05 -08002256 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002257 compositeDeadline, compositeInterval, compositeToPresentLatency);
2258
2259 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002260 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002261 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002262 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002263 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002264 default:
2265 // This should not happen. Return an error that is not in the spec
2266 // so it's obvious something is very wrong.
2267 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002268 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002269 }
2270}
2271
2272EGLBoolean eglGetCompositorTimingSupportedANDROID(
2273 EGLDisplay dpy, EGLSurface surface, EGLint name)
2274{
2275 clearError();
2276
2277 const egl_display_ptr dp = validate_display(dpy);
2278 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002279 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002280 }
2281
2282 SurfaceRef _s(dp.get(), surface);
2283 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002284 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002285 }
2286
2287 egl_surface_t const * const s = get_surface(surface);
2288
Mathias Agopian65421432017-03-08 11:49:05 -08002289 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002290 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002291 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002292 }
2293
2294 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002295 case EGL_COMPOSITE_DEADLINE_ANDROID:
2296 case EGL_COMPOSITE_INTERVAL_ANDROID:
2297 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2298 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002299 default:
2300 return EGL_FALSE;
2301 }
2302}
2303
Pablo Ceballosc18be292016-05-31 14:55:42 -07002304EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002305 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002306 EGLnsecsANDROID *values)
2307{
2308 clearError();
2309
2310 const egl_display_ptr dp = validate_display(dpy);
2311 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002312 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002313 }
2314
2315 SurfaceRef _s(dp.get(), surface);
2316 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002317 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002318 }
2319
2320 egl_surface_t const * const s = get_surface(surface);
2321
Mathias Agopian65421432017-03-08 11:49:05 -08002322 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002323 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002324 }
2325
Brian Andersondbd0ea82016-07-22 09:38:59 -07002326 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002327 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002328 nsecs_t* latchTime = nullptr;
2329 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002330 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002331 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002332 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002333 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002334 nsecs_t* releaseTime = nullptr;
2335
2336 for (int i = 0; i < numTimestamps; i++) {
2337 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002338 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2339 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002340 break;
2341 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2342 acquireTime = &values[i];
2343 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002344 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2345 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002346 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002347 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2348 firstRefreshStartTime = &values[i];
2349 break;
2350 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2351 lastRefreshStartTime = &values[i];
2352 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002353 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2354 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002355 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002356 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2357 displayPresentTime = &values[i];
2358 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002359 case EGL_DEQUEUE_READY_TIME_ANDROID:
2360 dequeueReadyTime = &values[i];
2361 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002362 case EGL_READS_DONE_TIME_ANDROID:
2363 releaseTime = &values[i];
2364 break;
2365 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002366 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002367 }
2368 }
2369
Mathias Agopian65421432017-03-08 11:49:05 -08002370 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002371 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002372 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002373 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002374
Brian Anderson069b3652016-07-22 10:32:47 -07002375 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002376 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002377 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002378 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002379 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002380 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002381 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002382 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002383 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2384 default:
2385 // This should not happen. Return an error that is not in the spec
2386 // so it's obvious something is very wrong.
2387 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2388 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002389 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002390}
2391
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002392EGLBoolean eglGetFrameTimestampSupportedANDROID(
2393 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002394{
2395 clearError();
2396
2397 const egl_display_ptr dp = validate_display(dpy);
2398 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002399 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002400 }
2401
2402 SurfaceRef _s(dp.get(), surface);
2403 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002404 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002405 }
2406
2407 egl_surface_t const * const s = get_surface(surface);
2408
Mathias Agopian65421432017-03-08 11:49:05 -08002409 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002410 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002411 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002412 }
2413
2414 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002415 case EGL_COMPOSITE_DEADLINE_ANDROID:
2416 case EGL_COMPOSITE_INTERVAL_ANDROID:
2417 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002418 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002419 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002420 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2421 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2422 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002423 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002424 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002425 case EGL_READS_DONE_TIME_ANDROID:
2426 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002427 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2428 int value = 0;
2429 window->query(window,
2430 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2431 return value == 0 ? EGL_FALSE : EGL_TRUE;
2432 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002433 default:
2434 return EGL_FALSE;
2435 }
2436}