blob: d2dc514ff26edc5047508af825a7ea1b4d5b8daa [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 }
Chris Forbes8e8fb4a2018-10-04 09:52:42 -0700565
566 // If the driver doesn't understand it, we should map sRGB-encoded P3 to
567 // sRGB rather than just dropping the colorspace on the floor.
568 // For this format, the driver is expected to apply the sRGB
569 // transfer function during framebuffer operations.
570 if (!copyAttribute && attr[1] == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
571 strippedAttribList->push_back(attr[0]);
572 strippedAttribList->push_back(EGL_GL_COLORSPACE_SRGB_KHR);
573 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600574 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700575 if (copyAttribute) {
576 strippedAttribList->push_back(attr[0]);
577 strippedAttribList->push_back(attr[1]);
578 }
579 }
580 // Terminate the attribute list.
581 strippedAttribList->push_back(EGL_NONE);
582
583 // If the passed color space has wide color gamut, check whether the target native window
584 // supports wide color.
585 const bool colorSpaceIsNarrow =
586 *colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700587 *colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR ||
588 *colorSpace == EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700589 if (window && !colorSpaceIsNarrow) {
590 bool windowSupportsWideColor = true;
591 // Ordinarily we'd put a call to native_window_get_wide_color_support
592 // at the beginning of the function so that we'll have the
593 // result when needed elsewhere in the function.
594 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
595 // SurfaceFlinger is required to answer the call below we would
596 // end up in a deadlock situation. By moving the call to only happen
597 // if the application has specifically asked for wide-color we avoid
598 // the deadlock with SurfaceFlinger since it will not ask for a
599 // wide-color surface.
600 int err = native_window_get_wide_color_support(window, &windowSupportsWideColor);
601
602 if (err) {
603 ALOGE("processAttributes: invalid window (win=%p) "
604 "failed (%#x) (already connected to another API?)",
605 window, err);
606 return false;
607 }
608 if (!windowSupportsWideColor) {
609 // Application has asked for a wide-color colorspace but
610 // wide-color support isn't available on the display the window is on.
611 return false;
612 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600613 }
614 return true;
615}
616
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700617// Gets the native pixel format corrsponding to the passed EGLConfig.
618void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config,
619 android_pixel_format* format) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600620 // Set the native window's buffers format to match what this config requests.
621 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
622 // of our native format. So if sRGB gamma is requested, we have to
623 // modify the EGLconfig's format before setting the native window's
624 // format.
625
626 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
627 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
628
629 EGLint a = 0;
630 EGLint r, g, b;
631 r = g = b = 0;
632 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
633 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
634 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
635 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
636 EGLint colorDepth = r + g + b;
637
638 // Today, the driver only understands sRGB and linear on 888X
639 // formats. Strip other colorspaces from the attribute list and
640 // only use them to set the dataspace via
641 // native_window_set_buffers_dataspace
642 // if pixel format is RGBX 8888
643 // TBD: Can test for future extensions that indicate that driver
644 // handles requested color space and we can let it through.
645 // allow SRGB and LINEAR. All others need to be stripped.
646 // else if 565, 4444
647 // TBD: Can we assume these are supported if 8888 is?
648 // else if FP16 or 1010102
649 // strip colorspace from attribs.
650 // endif
651 if (a == 0) {
652 if (colorDepth <= 16) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700653 *format = HAL_PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600654 } else {
655 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
656 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700657 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600658 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700659 *format = HAL_PIXEL_FORMAT_RGBX_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600660 }
661 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700662 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600663 }
664 }
665 } else {
666 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
667 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700668 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600669 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700670 *format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600671 }
672 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700673 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600674 }
675 }
676}
677
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700678EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
679 android_smpte2086_metadata smpteMetadata;
680 if (s->getSmpte2086Metadata(smpteMetadata)) {
681 int err =
682 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
683 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700684 if (err != 0) {
685 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
686 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700687 return EGL_FALSE;
688 }
689 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700690 android_cta861_3_metadata cta8613Metadata;
691 if (s->getCta8613Metadata(cta8613Metadata)) {
692 int err =
693 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
694 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700695 if (err != 0) {
696 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
697 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700698 return EGL_FALSE;
699 }
700 }
701 return EGL_TRUE;
702}
703
Mathias Agopian518ec112011-05-13 16:21:08 -0700704EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
705 NativeWindowType window,
706 const EGLint *attrib_list)
707{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700708 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700709 clearError();
710
Yi Kong48a6cd22018-07-18 10:07:09 -0700711 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700712 egl_display_ptr dp = validate_display_connection(dpy, cnx);
713 if (dp) {
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800714 if (!window) {
715 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
716 }
717
718 int value = 0;
719 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
720 if (!value) {
721 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
722 }
723
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600724 // NOTE: When using Vulkan backend, the Vulkan runtime makes all the
725 // native_window_* calls, so don't do them here.
726 if (cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
727 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
728 if (result < 0) {
729 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
730 "failed (%#x) (already connected to another API?)",
731 window, result);
732 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
733 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700734 }
735
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700736 EGLDisplay iDpy = dp->disp.dpy;
737 android_pixel_format format;
738 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600739
740 // now select correct colorspace and dataspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700741 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700742 std::vector<EGLint> strippedAttribList;
743 if (!processAttributes(dp, window, format, attrib_list, &colorSpace,
744 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600745 ALOGE("error invalid colorspace: %d", colorSpace);
746 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700747 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700748 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800749
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600750 if (cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700751 int err = native_window_set_buffers_format(window, format);
752 if (err != 0) {
753 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700754 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700755 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
756 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
757 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700758
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600759 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
760 if (dataSpace != HAL_DATASPACE_UNKNOWN) {
761 err = native_window_set_buffers_data_space(window, dataSpace);
762 if (err != 0) {
763 ALOGE("error setting native window pixel dataSpace: %s (%d)", strerror(-err),
764 err);
765 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
766 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
767 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800768 }
769 }
770
Jamie Gennis59769462011-11-19 18:04:43 -0800771 // the EGL spec requires that a new EGLSurface default to swap interval
772 // 1, so explicitly set that on the window here.
773 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
774 anw->setSwapInterval(anw, 1);
775
Mathias Agopian518ec112011-05-13 16:21:08 -0700776 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800777 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700778 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600779 egl_surface_t* s =
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700780 new egl_surface_t(dp.get(), config, window, surface,
781 getReportedColorSpace(colorSpace), cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700782 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700783 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700784
785 // EGLSurface creation failed
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -0600786 if (cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
787 native_window_set_buffers_format(window, 0);
788 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
789 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700790 }
791 return EGL_NO_SURFACE;
792}
793
794EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
795 NativePixmapType pixmap,
796 const EGLint *attrib_list)
797{
798 clearError();
799
Yi Kong48a6cd22018-07-18 10:07:09 -0700800 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700801 egl_display_ptr dp = validate_display_connection(dpy, cnx);
802 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700803 EGLDisplay iDpy = dp->disp.dpy;
804 android_pixel_format format;
805 getNativePixelFormat(iDpy, cnx, config, &format);
806
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600807 // now select a corresponding sRGB format if needed
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700808 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700809 std::vector<EGLint> strippedAttribList;
810 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
811 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600812 ALOGE("error invalid colorspace: %d", colorSpace);
813 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
814 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700815 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600816
Mathias Agopian518ec112011-05-13 16:21:08 -0700817 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800818 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700819 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700820 egl_surface_t* s =
Yi Kong48a6cd22018-07-18 10:07:09 -0700821 new egl_surface_t(dp.get(), config, nullptr, surface,
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700822 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700823 return s;
824 }
825 }
826 return EGL_NO_SURFACE;
827}
828
829EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
830 const EGLint *attrib_list)
831{
832 clearError();
833
Yi Kong48a6cd22018-07-18 10:07:09 -0700834 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700835 egl_display_ptr dp = validate_display_connection(dpy, cnx);
836 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600837 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700838 android_pixel_format format;
839 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600840
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700841 // Select correct colorspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700842 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700843 std::vector<EGLint> strippedAttribList;
844 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
845 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600846 ALOGE("error invalid colorspace: %d", colorSpace);
847 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
848 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700849 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600850
Mathias Agopian518ec112011-05-13 16:21:08 -0700851 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800852 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700854 egl_surface_t* s =
Yi Kong48a6cd22018-07-18 10:07:09 -0700855 new egl_surface_t(dp.get(), config, nullptr, surface,
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700856 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700857 return s;
858 }
859 }
860 return EGL_NO_SURFACE;
861}
Jesse Hall47743382013-02-08 11:13:46 -0800862
Mathias Agopian518ec112011-05-13 16:21:08 -0700863EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
864{
865 clearError();
866
Jesse Hallb29e5e82012-04-04 16:53:42 -0700867 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700868 if (!dp) return EGL_FALSE;
869
Jesse Hallb29e5e82012-04-04 16:53:42 -0700870 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700871 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800872 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700873
874 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800875 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700876 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700877 _s.terminate();
878 }
879 return result;
880}
881
882EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
883 EGLint attribute, EGLint *value)
884{
885 clearError();
886
Jesse Hallb29e5e82012-04-04 16:53:42 -0700887 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700888 if (!dp) return EGL_FALSE;
889
Jesse Hallb29e5e82012-04-04 16:53:42 -0700890 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700891 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800892 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700893
Mathias Agopian518ec112011-05-13 16:21:08 -0700894 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700895 if (s->getColorSpaceAttribute(attribute, value)) {
896 return EGL_TRUE;
897 } else if (s->getSmpte2086Attribute(attribute, value)) {
898 return EGL_TRUE;
899 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600900 return EGL_TRUE;
901 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700902 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700903}
904
Jamie Gennise8696a42012-01-15 18:54:57 -0800905void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800906 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800907 clearError();
908
Jesse Hallb29e5e82012-04-04 16:53:42 -0700909 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800910 if (!dp) {
911 return;
912 }
913
Jesse Hallb29e5e82012-04-04 16:53:42 -0700914 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800915 if (!_s.get()) {
916 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800917 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800918}
919
Mathias Agopian518ec112011-05-13 16:21:08 -0700920// ----------------------------------------------------------------------------
921// Contexts
922// ----------------------------------------------------------------------------
923
924EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
925 EGLContext share_list, const EGLint *attrib_list)
926{
927 clearError();
928
Yi Kong48a6cd22018-07-18 10:07:09 -0700929 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700930 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700931 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700932 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700933 if (!ContextRef(dp.get(), share_list).get()) {
934 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
935 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700936 egl_context_t* const c = get_context(share_list);
937 share_list = c->context;
938 }
939 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800940 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700941 if (context != EGL_NO_CONTEXT) {
942 // figure out if it's a GLESv1 or GLESv2
943 int version = 0;
944 if (attrib_list) {
945 while (*attrib_list != EGL_NONE) {
946 GLint attr = *attrib_list++;
947 GLint value = *attrib_list++;
948 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
949 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800950 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800951 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800952 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700953 }
954 }
955 };
956 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700957 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
958 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700959 return c;
960 }
961 }
962 return EGL_NO_CONTEXT;
963}
964
965EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
966{
967 clearError();
968
Jesse Hallb29e5e82012-04-04 16:53:42 -0700969 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700970 if (!dp)
971 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700972
Jesse Hallb29e5e82012-04-04 16:53:42 -0700973 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700974 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800975 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800976
Mathias Agopian518ec112011-05-13 16:21:08 -0700977 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800978 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700979 if (result == EGL_TRUE) {
980 _c.terminate();
981 }
982 return result;
983}
984
Mathias Agopian518ec112011-05-13 16:21:08 -0700985EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
986 EGLSurface read, EGLContext ctx)
987{
988 clearError();
989
Jesse Hallb29e5e82012-04-04 16:53:42 -0700990 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800991 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700992
Mathias Agopian5b287a62011-05-16 18:58:55 -0700993 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
994 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
995 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700996 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
997 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800998 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700999 }
1000
1001 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -07001002 ContextRef _c(dp.get(), ctx);
1003 SurfaceRef _d(dp.get(), draw);
1004 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001005
1006 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -07001007 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001008 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -08001009 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 }
1011
1012 // these are the underlying implementation's object
1013 EGLContext impl_ctx = EGL_NO_CONTEXT;
1014 EGLSurface impl_draw = EGL_NO_SURFACE;
1015 EGLSurface impl_read = EGL_NO_SURFACE;
1016
1017 // these are our objects structs passed in
Yi Kong48a6cd22018-07-18 10:07:09 -07001018 egl_context_t * c = nullptr;
1019 egl_surface_t const * d = nullptr;
1020 egl_surface_t const * r = nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001021
1022 // these are the current objects structs
1023 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001024
Mathias Agopian518ec112011-05-13 16:21:08 -07001025 if (ctx != EGL_NO_CONTEXT) {
1026 c = get_context(ctx);
1027 impl_ctx = c->context;
1028 } else {
1029 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001030 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1031 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001032 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001033 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001034 if (cur_c == nullptr) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001035 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001036 // not an error, there is just no current context.
1037 return EGL_TRUE;
1038 }
1039 }
1040
1041 // retrieve the underlying implementation's draw EGLSurface
1042 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001043 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001044 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001045 impl_draw = d->surface;
1046 }
1047
1048 // retrieve the underlying implementation's read EGLSurface
1049 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001050 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001051 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001052 impl_read = r->surface;
1053 }
1054
Mathias Agopian518ec112011-05-13 16:21:08 -07001055
Jesse Hallb29e5e82012-04-04 16:53:42 -07001056 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001057 draw, read, ctx,
1058 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001059
1060 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001061 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001062 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1063 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001064 _c.acquire();
1065 _r.acquire();
1066 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001067 } else {
1068 setGLHooksThreadSpecific(&gHooksNoContext);
1069 egl_tls_t::setContext(EGL_NO_CONTEXT);
1070 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001071 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001072 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001073 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001074 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001075 }
1076 return result;
1077}
1078
1079
1080EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1081 EGLint attribute, EGLint *value)
1082{
1083 clearError();
1084
Jesse Hallb29e5e82012-04-04 16:53:42 -07001085 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001086 if (!dp) return EGL_FALSE;
1087
Jesse Hallb29e5e82012-04-04 16:53:42 -07001088 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001089 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001090
Mathias Agopian518ec112011-05-13 16:21:08 -07001091 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001092 return c->cnx->egl.eglQueryContext(
1093 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001094
Mathias Agopian518ec112011-05-13 16:21:08 -07001095}
1096
1097EGLContext eglGetCurrentContext(void)
1098{
1099 // could be called before eglInitialize(), but we wouldn't have a context
1100 // then, and this function would correctly return EGL_NO_CONTEXT.
1101
1102 clearError();
1103
1104 EGLContext ctx = getContext();
1105 return ctx;
1106}
1107
1108EGLSurface eglGetCurrentSurface(EGLint readdraw)
1109{
1110 // could be called before eglInitialize(), but we wouldn't have a context
1111 // then, and this function would correctly return EGL_NO_SURFACE.
1112
1113 clearError();
1114
1115 EGLContext ctx = getContext();
1116 if (ctx) {
1117 egl_context_t const * const c = get_context(ctx);
1118 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1119 switch (readdraw) {
1120 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001121 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001122 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1123 }
1124 }
1125 return EGL_NO_SURFACE;
1126}
1127
1128EGLDisplay eglGetCurrentDisplay(void)
1129{
1130 // could be called before eglInitialize(), but we wouldn't have a context
1131 // then, and this function would correctly return EGL_NO_DISPLAY.
1132
1133 clearError();
1134
1135 EGLContext ctx = getContext();
1136 if (ctx) {
1137 egl_context_t const * const c = get_context(ctx);
1138 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1139 return c->dpy;
1140 }
1141 return EGL_NO_DISPLAY;
1142}
1143
1144EGLBoolean eglWaitGL(void)
1145{
Mathias Agopian518ec112011-05-13 16:21:08 -07001146 clearError();
1147
Mathias Agopianada798b2012-02-13 17:09:30 -08001148 egl_connection_t* const cnx = &gEGLImpl;
1149 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001150 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001151
1152 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001153}
1154
1155EGLBoolean eglWaitNative(EGLint engine)
1156{
Mathias Agopian518ec112011-05-13 16:21:08 -07001157 clearError();
1158
Mathias Agopianada798b2012-02-13 17:09:30 -08001159 egl_connection_t* const cnx = &gEGLImpl;
1160 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001161 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001162
1163 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001164}
1165
1166EGLint eglGetError(void)
1167{
Mathias Agopianada798b2012-02-13 17:09:30 -08001168 EGLint err = EGL_SUCCESS;
1169 egl_connection_t* const cnx = &gEGLImpl;
1170 if (cnx->dso) {
1171 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001172 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001173 if (err == EGL_SUCCESS) {
1174 err = egl_tls_t::getError();
1175 }
1176 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001177}
1178
Michael Chockc0ec5e22014-01-27 08:14:33 -08001179static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001180 const char* procname) {
1181 const egl_connection_t* cnx = &gEGLImpl;
Yi Kong48a6cd22018-07-18 10:07:09 -07001182 void* proc = nullptr;
Jesse Hallc07b5202013-07-04 12:08:16 -07001183
Michael Chockc0ec5e22014-01-27 08:14:33 -08001184 proc = dlsym(cnx->libEgl, procname);
1185 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1186
Jesse Hallc07b5202013-07-04 12:08:16 -07001187 proc = dlsym(cnx->libGles2, procname);
1188 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1189
1190 proc = dlsym(cnx->libGles1, procname);
1191 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1192
Yi Kong48a6cd22018-07-18 10:07:09 -07001193 return nullptr;
Jesse Hallc07b5202013-07-04 12:08:16 -07001194}
1195
Mathias Agopian518ec112011-05-13 16:21:08 -07001196__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1197{
1198 // eglGetProcAddress() could be the very first function called
1199 // in which case we must make sure we've initialized ourselves, this
1200 // happens the first time egl_get_display() is called.
1201
1202 clearError();
1203
1204 if (egl_init_drivers() == EGL_FALSE) {
1205 setError(EGL_BAD_PARAMETER, NULL);
Yi Kong48a6cd22018-07-18 10:07:09 -07001206 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001207 }
1208
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001209 if (FILTER_EXTENSIONS(procname)) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001210 return nullptr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001211 }
1212
Mathias Agopian518ec112011-05-13 16:21:08 -07001213 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001214 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001215 if (addr) return addr;
1216
Michael Chockc0ec5e22014-01-27 08:14:33 -08001217 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001218 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001219
Mathias Agopian518ec112011-05-13 16:21:08 -07001220 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1221 pthread_mutex_lock(&sExtensionMapMutex);
1222
1223 /*
1224 * Since eglGetProcAddress() is not associated to anything, it needs
1225 * to return a function pointer that "works" regardless of what
1226 * the current context is.
1227 *
1228 * For this reason, we return a "forwarder", a small stub that takes
1229 * care of calling the function associated with the context
1230 * currently bound.
1231 *
1232 * We first look for extensions we've already resolved, if we're seeing
1233 * this extension for the first time, we go through all our
1234 * implementations and call eglGetProcAddress() and record the
1235 * result in the appropriate implementation hooks and return the
1236 * address of the forwarder corresponding to that hook set.
1237 *
1238 */
1239
Mathias Agopian65421432017-03-08 11:49:05 -08001240 const std::string name(procname);
1241
1242 auto& extentionMap = sGLExtentionMap;
1243 auto pos = extentionMap.find(name);
1244 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001245 const int slot = sGLExtentionSlot;
1246
Steve Blocke6f43dd2012-01-06 19:20:56 +00001247 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001248 "no more slots for eglGetProcAddress(\"%s\")",
1249 procname);
1250
1251 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1252 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001253
1254 egl_connection_t* const cnx = &gEGLImpl;
1255 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001256 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001257 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001258 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1259 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001260 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001261 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001262 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001263
Mathias Agopian518ec112011-05-13 16:21:08 -07001264 if (found) {
1265 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001266 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001267 sGLExtentionSlot++;
1268 }
1269 }
1270
1271 pthread_mutex_unlock(&sExtensionMapMutex);
1272 return addr;
1273}
1274
Mathias Agopian65421432017-03-08 11:49:05 -08001275class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001276public:
1277
1278 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001279 static FrameCompletionThread thread;
1280
1281 char name[64];
1282
1283 std::lock_guard<std::mutex> lock(thread.mMutex);
1284 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1285 ATRACE_NAME(name);
1286
1287 thread.mQueue.push_back(sync);
1288 thread.mCondition.notify_one();
1289 thread.mFramesQueued++;
1290 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001291 }
1292
1293private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001294
Mathias Agopian65421432017-03-08 11:49:05 -08001295 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1296 std::thread thread(&FrameCompletionThread::loop, this);
1297 thread.detach();
1298 }
1299
1300#pragma clang diagnostic push
1301#pragma clang diagnostic ignored "-Wmissing-noreturn"
1302 void loop() {
1303 while (true) {
1304 threadLoop();
1305 }
1306 }
1307#pragma clang diagnostic pop
1308
1309 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001310 EGLSyncKHR sync;
1311 uint32_t frameNum;
1312 {
Mathias Agopian65421432017-03-08 11:49:05 -08001313 std::unique_lock<std::mutex> lock(mMutex);
1314 while (mQueue.empty()) {
1315 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001316 }
1317 sync = mQueue[0];
1318 frameNum = mFramesCompleted;
1319 }
1320 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1321 {
Mathias Agopian65421432017-03-08 11:49:05 -08001322 char name[64];
1323 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1324 ATRACE_NAME(name);
1325
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001326 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1327 if (result == EGL_FALSE) {
1328 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1329 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1330 ALOGE("FrameCompletion: timeout waiting for fence");
1331 }
1332 eglDestroySyncKHR(dpy, sync);
1333 }
1334 {
Mathias Agopian65421432017-03-08 11:49:05 -08001335 std::lock_guard<std::mutex> lock(mMutex);
1336 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001337 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001338 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001339 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001340 }
1341
1342 uint32_t mFramesQueued;
1343 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001344 std::deque<EGLSyncKHR> mQueue;
1345 std::condition_variable mCondition;
1346 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001347};
1348
Dan Stozaa894d082015-02-19 15:27:36 -08001349EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1350 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001351{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001352 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001353 clearError();
1354
Jesse Hallb29e5e82012-04-04 16:53:42 -07001355 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001356 if (!dp) return EGL_FALSE;
1357
Jesse Hallb29e5e82012-04-04 16:53:42 -07001358 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001359 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001360 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001361
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001362 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001363
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001364 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001365 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001366 if (sync != EGL_NO_SYNC_KHR) {
1367 FrameCompletionThread::queueSync(sync);
1368 }
1369 }
1370
Mathias Agopian7db993a2012-03-25 00:49:46 -07001371 if (CC_UNLIKELY(dp->finishOnSwap)) {
1372 uint32_t pixel;
1373 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1374 if (c) {
1375 // glReadPixels() ensures that the frame is complete
1376 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1377 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1378 }
1379 }
1380
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -06001381 if (s->cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
1382 if (!sendSurfaceMetadata(s)) {
1383 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1384 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1385 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001386 }
1387
Dan Stozaa894d082015-02-19 15:27:36 -08001388 if (n_rects == 0) {
1389 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1390 }
1391
Mathias Agopian65421432017-03-08 11:49:05 -08001392 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001393 for (int r = 0; r < n_rects; ++r) {
1394 int offset = r * 4;
1395 int x = rects[offset];
1396 int y = rects[offset + 1];
1397 int width = rects[offset + 2];
1398 int height = rects[offset + 3];
1399 android_native_rect_t androidRect;
1400 androidRect.left = x;
1401 androidRect.top = y + height;
1402 androidRect.right = x + width;
1403 androidRect.bottom = y;
1404 androidRects.push_back(androidRect);
1405 }
Courtney Goeltzenleuchter555f1df2018-09-25 14:34:29 -06001406 if (s->cnx->angleBackend != EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE) {
1407 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(),
1408 androidRects.size());
1409 }
Dan Stozaa894d082015-02-19 15:27:36 -08001410
1411 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1412 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1413 rects, n_rects);
1414 } else {
1415 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1416 }
1417}
1418
1419EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1420{
Yi Kong48a6cd22018-07-18 10:07:09 -07001421 return eglSwapBuffersWithDamageKHR(dpy, surface, nullptr, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001422}
1423
1424EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1425 NativePixmapType target)
1426{
1427 clearError();
1428
Jesse Hallb29e5e82012-04-04 16:53:42 -07001429 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 if (!dp) return EGL_FALSE;
1431
Jesse Hallb29e5e82012-04-04 16:53:42 -07001432 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001433 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001434 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001435
Mathias Agopian518ec112011-05-13 16:21:08 -07001436 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001437 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001438}
1439
1440const char* eglQueryString(EGLDisplay dpy, EGLint name)
1441{
1442 clearError();
1443
Chia-I Wue57d1352016-08-15 16:10:02 +08001444 // Generate an error quietly when client extensions (as defined by
1445 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1446 // validate_display to generate the error as validate_display would log
1447 // the error, which can be misleading.
1448 //
1449 // If we want to support EGL_EXT_client_extensions later, we can return
1450 // the client extension string here instead.
1451 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -07001452 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)nullptr);
Chia-I Wue57d1352016-08-15 16:10:02 +08001453
Jesse Hallb29e5e82012-04-04 16:53:42 -07001454 const egl_display_ptr dp = validate_display(dpy);
Yi Kong48a6cd22018-07-18 10:07:09 -07001455 if (!dp) return (const char *) nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001456
1457 switch (name) {
1458 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001459 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001460 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001461 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001462 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001463 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001464 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001465 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001466 default:
1467 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001468 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001469 return setError(EGL_BAD_PARAMETER, (const char *)nullptr);
Mathias Agopian518ec112011-05-13 16:21:08 -07001470}
1471
Jiyong Park00b15b82017-08-10 20:30:56 +09001472extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001473{
1474 clearError();
1475
1476 const egl_display_ptr dp = validate_display(dpy);
Yi Kong48a6cd22018-07-18 10:07:09 -07001477 if (!dp) return (const char *) nullptr;
Mathias Agopianca088332013-03-28 17:44:13 -07001478
1479 switch (name) {
1480 case EGL_VENDOR:
1481 return dp->disp.queryString.vendor;
1482 case EGL_VERSION:
1483 return dp->disp.queryString.version;
1484 case EGL_EXTENSIONS:
1485 return dp->disp.queryString.extensions;
1486 case EGL_CLIENT_APIS:
1487 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001488 default:
1489 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001490 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001491 return setError(EGL_BAD_PARAMETER, (const char *)nullptr);
Mathias Agopianca088332013-03-28 17:44:13 -07001492}
Mathias Agopian518ec112011-05-13 16:21:08 -07001493
1494// ----------------------------------------------------------------------------
1495// EGL 1.1
1496// ----------------------------------------------------------------------------
1497
1498EGLBoolean eglSurfaceAttrib(
1499 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1500{
1501 clearError();
1502
Jesse Hallb29e5e82012-04-04 16:53:42 -07001503 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001504 if (!dp) return EGL_FALSE;
1505
Jesse Hallb29e5e82012-04-04 16:53:42 -07001506 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001507 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001508 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001509
Pablo Ceballosc18be292016-05-31 14:55:42 -07001510 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001511
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001512 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001513 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001514 setError(EGL_BAD_SURFACE, EGL_FALSE);
1515 }
Mathias Agopian65421432017-03-08 11:49:05 -08001516 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1517 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001518 }
1519
Pablo Ceballosc18be292016-05-31 14:55:42 -07001520 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001521 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001522 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001523 }
Mathias Agopian65421432017-03-08 11:49:05 -08001524 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1525 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001526 }
1527
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001528 if (s->setSmpte2086Attribute(attribute, value)) {
1529 return EGL_TRUE;
1530 } else if (s->setCta8613Attribute(attribute, value)) {
1531 return EGL_TRUE;
1532 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001533 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001534 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001535 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001536 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001537}
1538
1539EGLBoolean eglBindTexImage(
1540 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1541{
1542 clearError();
1543
Jesse Hallb29e5e82012-04-04 16:53:42 -07001544 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001545 if (!dp) return EGL_FALSE;
1546
Jesse Hallb29e5e82012-04-04 16:53:42 -07001547 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001548 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001549 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001550
Mathias Agopian518ec112011-05-13 16:21:08 -07001551 egl_surface_t const * const s = get_surface(surface);
1552 if (s->cnx->egl.eglBindTexImage) {
1553 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001554 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001555 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001556 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001557}
1558
1559EGLBoolean eglReleaseTexImage(
1560 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1561{
1562 clearError();
1563
Jesse Hallb29e5e82012-04-04 16:53:42 -07001564 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001565 if (!dp) return EGL_FALSE;
1566
Jesse Hallb29e5e82012-04-04 16:53:42 -07001567 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001568 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001569 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001570
Mathias Agopian518ec112011-05-13 16:21:08 -07001571 egl_surface_t const * const s = get_surface(surface);
1572 if (s->cnx->egl.eglReleaseTexImage) {
1573 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001574 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001575 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001576 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001577}
1578
1579EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1580{
1581 clearError();
1582
Jesse Hallb29e5e82012-04-04 16:53:42 -07001583 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001584 if (!dp) return EGL_FALSE;
1585
1586 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001587 egl_connection_t* const cnx = &gEGLImpl;
1588 if (cnx->dso && cnx->egl.eglSwapInterval) {
1589 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001590 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001591
Mathias Agopian518ec112011-05-13 16:21:08 -07001592 return res;
1593}
1594
1595
1596// ----------------------------------------------------------------------------
1597// EGL 1.2
1598// ----------------------------------------------------------------------------
1599
1600EGLBoolean eglWaitClient(void)
1601{
1602 clearError();
1603
Mathias Agopianada798b2012-02-13 17:09:30 -08001604 egl_connection_t* const cnx = &gEGLImpl;
1605 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001606 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001607
1608 EGLBoolean res;
1609 if (cnx->egl.eglWaitClient) {
1610 res = cnx->egl.eglWaitClient();
1611 } else {
1612 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001613 }
1614 return res;
1615}
1616
1617EGLBoolean eglBindAPI(EGLenum api)
1618{
1619 clearError();
1620
1621 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001622 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001623 }
1624
1625 // bind this API on all EGLs
1626 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001627 egl_connection_t* const cnx = &gEGLImpl;
1628 if (cnx->dso && cnx->egl.eglBindAPI) {
1629 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001630 }
1631 return res;
1632}
1633
1634EGLenum eglQueryAPI(void)
1635{
1636 clearError();
1637
1638 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001639 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001640 }
1641
Mathias Agopianada798b2012-02-13 17:09:30 -08001642 egl_connection_t* const cnx = &gEGLImpl;
1643 if (cnx->dso && cnx->egl.eglQueryAPI) {
1644 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001645 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001646
Mathias Agopian518ec112011-05-13 16:21:08 -07001647 // or, it can only be OpenGL ES
1648 return EGL_OPENGL_ES_API;
1649}
1650
1651EGLBoolean eglReleaseThread(void)
1652{
1653 clearError();
1654
Mathias Agopianada798b2012-02-13 17:09:30 -08001655 egl_connection_t* const cnx = &gEGLImpl;
1656 if (cnx->dso && cnx->egl.eglReleaseThread) {
1657 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001658 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301659
1660 // If there is context bound to the thread, release it
1661 egl_display_t::loseCurrent(get_context(getContext()));
1662
Mathias Agopian518ec112011-05-13 16:21:08 -07001663 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001664 return EGL_TRUE;
1665}
1666
1667EGLSurface eglCreatePbufferFromClientBuffer(
1668 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1669 EGLConfig config, const EGLint *attrib_list)
1670{
1671 clearError();
1672
Yi Kong48a6cd22018-07-18 10:07:09 -07001673 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -07001674 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1675 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001676 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1677 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001678 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001679 }
1680 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1681}
1682
1683// ----------------------------------------------------------------------------
1684// EGL_EGLEXT_VERSION 3
1685// ----------------------------------------------------------------------------
1686
1687EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1688 const EGLint *attrib_list)
1689{
1690 clearError();
1691
Jesse Hallb29e5e82012-04-04 16:53:42 -07001692 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001693 if (!dp) return EGL_FALSE;
1694
Jesse Hallb29e5e82012-04-04 16:53:42 -07001695 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001696 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001697 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001698
1699 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001700 if (s->cnx->egl.eglLockSurfaceKHR) {
1701 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001702 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001703 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001704 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001705}
1706
1707EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1708{
1709 clearError();
1710
Jesse Hallb29e5e82012-04-04 16:53:42 -07001711 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001712 if (!dp) return EGL_FALSE;
1713
Jesse Hallb29e5e82012-04-04 16:53:42 -07001714 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001715 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001716 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001717
1718 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001719 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001720 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001721 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001722 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001723}
1724
1725EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1726 EGLClientBuffer buffer, const EGLint *attrib_list)
1727{
1728 clearError();
1729
Jesse Hallb29e5e82012-04-04 16:53:42 -07001730 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001731 if (!dp) return EGL_NO_IMAGE_KHR;
1732
Jesse Hallb29e5e82012-04-04 16:53:42 -07001733 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001734 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001735
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001736 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1737 egl_connection_t* const cnx = &gEGLImpl;
1738 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1739 result = cnx->egl.eglCreateImageKHR(
1740 dp->disp.dpy,
1741 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosiński30c1d902018-06-27 17:14:33 -07001742 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001743 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001744 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001745}
1746
1747EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1748{
1749 clearError();
1750
Jesse Hallb29e5e82012-04-04 16:53:42 -07001751 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001752 if (!dp) return EGL_FALSE;
1753
Steven Holte646a5c52012-06-04 20:02:11 -07001754 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001755 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001756 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001757 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001758 }
Steven Holte646a5c52012-06-04 20:02:11 -07001759 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001760}
1761
1762// ----------------------------------------------------------------------------
1763// EGL_EGLEXT_VERSION 5
1764// ----------------------------------------------------------------------------
1765
1766
1767EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1768{
1769 clearError();
1770
Jesse Hallb29e5e82012-04-04 16:53:42 -07001771 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001772 if (!dp) return EGL_NO_SYNC_KHR;
1773
Mathias Agopian518ec112011-05-13 16:21:08 -07001774 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001775 egl_connection_t* const cnx = &gEGLImpl;
1776 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1777 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001778 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001779 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001780}
1781
1782EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1783{
1784 clearError();
1785
Jesse Hallb29e5e82012-04-04 16:53:42 -07001786 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001787 if (!dp) return EGL_FALSE;
1788
Mathias Agopian518ec112011-05-13 16:21:08 -07001789 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001790 egl_connection_t* const cnx = &gEGLImpl;
1791 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1792 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001793 }
1794 return result;
1795}
1796
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001797EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1798 clearError();
1799
1800 const egl_display_ptr dp = validate_display(dpy);
1801 if (!dp) return EGL_FALSE;
1802
1803 EGLBoolean result = EGL_FALSE;
1804 egl_connection_t* const cnx = &gEGLImpl;
1805 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1806 result = cnx->egl.eglSignalSyncKHR(
1807 dp->disp.dpy, sync, mode);
1808 }
1809 return result;
1810}
1811
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001812EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1813 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001814{
1815 clearError();
1816
Jesse Hallb29e5e82012-04-04 16:53:42 -07001817 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001818 if (!dp) return EGL_FALSE;
1819
Mathias Agopian737b8962017-02-24 14:32:05 -08001820 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001821 egl_connection_t* const cnx = &gEGLImpl;
1822 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1823 result = cnx->egl.eglClientWaitSyncKHR(
1824 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001825 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001826 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001827}
1828
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001829EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1830 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001831{
1832 clearError();
1833
Jesse Hallb29e5e82012-04-04 16:53:42 -07001834 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001835 if (!dp) return EGL_FALSE;
1836
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001837 EGLBoolean result = EGL_FALSE;
1838 egl_connection_t* const cnx = &gEGLImpl;
1839 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1840 result = cnx->egl.eglGetSyncAttribKHR(
1841 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001842 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001843 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001844}
1845
Season Li000d88f2015-07-01 11:39:40 -07001846EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1847{
1848 clearError();
1849
1850 const egl_display_ptr dp = validate_display(dpy);
1851 if (!dp) return EGL_NO_STREAM_KHR;
1852
1853 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1854 egl_connection_t* const cnx = &gEGLImpl;
1855 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1856 result = cnx->egl.eglCreateStreamKHR(
1857 dp->disp.dpy, attrib_list);
1858 }
1859 return result;
1860}
1861
1862EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1863{
1864 clearError();
1865
1866 const egl_display_ptr dp = validate_display(dpy);
1867 if (!dp) return EGL_FALSE;
1868
1869 EGLBoolean result = EGL_FALSE;
1870 egl_connection_t* const cnx = &gEGLImpl;
1871 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1872 result = cnx->egl.eglDestroyStreamKHR(
1873 dp->disp.dpy, stream);
1874 }
1875 return result;
1876}
1877
1878EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1879 EGLenum attribute, EGLint value)
1880{
1881 clearError();
1882
1883 const egl_display_ptr dp = validate_display(dpy);
1884 if (!dp) return EGL_FALSE;
1885
1886 EGLBoolean result = EGL_FALSE;
1887 egl_connection_t* const cnx = &gEGLImpl;
1888 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1889 result = cnx->egl.eglStreamAttribKHR(
1890 dp->disp.dpy, stream, attribute, value);
1891 }
1892 return result;
1893}
1894
1895EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1896 EGLenum attribute, EGLint *value)
1897{
1898 clearError();
1899
1900 const egl_display_ptr dp = validate_display(dpy);
1901 if (!dp) return EGL_FALSE;
1902
1903 EGLBoolean result = EGL_FALSE;
1904 egl_connection_t* const cnx = &gEGLImpl;
1905 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1906 result = cnx->egl.eglQueryStreamKHR(
1907 dp->disp.dpy, stream, attribute, value);
1908 }
1909 return result;
1910}
1911
1912EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1913 EGLenum attribute, EGLuint64KHR *value)
1914{
1915 clearError();
1916
1917 const egl_display_ptr dp = validate_display(dpy);
1918 if (!dp) return EGL_FALSE;
1919
1920 EGLBoolean result = EGL_FALSE;
1921 egl_connection_t* const cnx = &gEGLImpl;
1922 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1923 result = cnx->egl.eglQueryStreamu64KHR(
1924 dp->disp.dpy, stream, attribute, value);
1925 }
1926 return result;
1927}
1928
1929EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1930 EGLenum attribute, EGLTimeKHR *value)
1931{
1932 clearError();
1933
1934 const egl_display_ptr dp = validate_display(dpy);
1935 if (!dp) return EGL_FALSE;
1936
1937 EGLBoolean result = EGL_FALSE;
1938 egl_connection_t* const cnx = &gEGLImpl;
1939 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1940 result = cnx->egl.eglQueryStreamTimeKHR(
1941 dp->disp.dpy, stream, attribute, value);
1942 }
1943 return result;
1944}
1945
1946EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1947 EGLStreamKHR stream, const EGLint *attrib_list)
1948{
1949 clearError();
1950
1951 egl_display_ptr dp = validate_display(dpy);
1952 if (!dp) return EGL_NO_SURFACE;
1953
1954 egl_connection_t* const cnx = &gEGLImpl;
1955 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1956 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1957 dp->disp.dpy, config, stream, attrib_list);
1958 if (surface != EGL_NO_SURFACE) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001959 egl_surface_t* s = new egl_surface_t(dp.get(), config, nullptr, surface,
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001960 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001961 return s;
1962 }
1963 }
1964 return EGL_NO_SURFACE;
1965}
1966
1967EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1968 EGLStreamKHR stream)
1969{
1970 clearError();
1971
1972 const egl_display_ptr dp = validate_display(dpy);
1973 if (!dp) return EGL_FALSE;
1974
1975 EGLBoolean result = EGL_FALSE;
1976 egl_connection_t* const cnx = &gEGLImpl;
1977 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1978 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1979 dp->disp.dpy, stream);
1980 }
1981 return result;
1982}
1983
1984EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1985 EGLStreamKHR stream)
1986{
1987 clearError();
1988
1989 const egl_display_ptr dp = validate_display(dpy);
1990 if (!dp) return EGL_FALSE;
1991
1992 EGLBoolean result = EGL_FALSE;
1993 egl_connection_t* const cnx = &gEGLImpl;
1994 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1995 result = cnx->egl.eglStreamConsumerAcquireKHR(
1996 dp->disp.dpy, stream);
1997 }
1998 return result;
1999}
2000
2001EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2002 EGLStreamKHR stream)
2003{
2004 clearError();
2005
2006 const egl_display_ptr dp = validate_display(dpy);
2007 if (!dp) return EGL_FALSE;
2008
2009 EGLBoolean result = EGL_FALSE;
2010 egl_connection_t* const cnx = &gEGLImpl;
2011 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2012 result = cnx->egl.eglStreamConsumerReleaseKHR(
2013 dp->disp.dpy, stream);
2014 }
2015 return result;
2016}
2017
2018EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2019 EGLDisplay dpy, EGLStreamKHR stream)
2020{
2021 clearError();
2022
2023 const egl_display_ptr dp = validate_display(dpy);
2024 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2025
2026 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2027 egl_connection_t* const cnx = &gEGLImpl;
2028 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2029 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2030 dp->disp.dpy, stream);
2031 }
2032 return result;
2033}
2034
2035EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2036 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2037{
2038 clearError();
2039
2040 const egl_display_ptr dp = validate_display(dpy);
2041 if (!dp) return EGL_NO_STREAM_KHR;
2042
2043 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2044 egl_connection_t* const cnx = &gEGLImpl;
2045 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2046 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2047 dp->disp.dpy, file_descriptor);
2048 }
2049 return result;
2050}
2051
Mathias Agopian518ec112011-05-13 16:21:08 -07002052// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002053// EGL_EGLEXT_VERSION 15
2054// ----------------------------------------------------------------------------
2055
2056EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2057 clearError();
2058 const egl_display_ptr dp = validate_display(dpy);
2059 if (!dp) return EGL_FALSE;
2060 EGLint result = EGL_FALSE;
2061 egl_connection_t* const cnx = &gEGLImpl;
2062 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2063 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2064 }
2065 return result;
2066}
2067
2068// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002069// ANDROID extensions
2070// ----------------------------------------------------------------------------
2071
Jamie Gennis331841b2012-09-06 14:52:00 -07002072EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2073{
2074 clearError();
2075
2076 const egl_display_ptr dp = validate_display(dpy);
2077 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2078
2079 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2080 egl_connection_t* const cnx = &gEGLImpl;
2081 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2082 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2083 }
2084 return result;
2085}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002086
Andy McFadden72841452013-03-01 16:25:32 -08002087EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2088 EGLnsecsANDROID time)
2089{
2090 clearError();
2091
2092 const egl_display_ptr dp = validate_display(dpy);
2093 if (!dp) {
2094 return EGL_FALSE;
2095 }
2096
2097 SurfaceRef _s(dp.get(), surface);
2098 if (!_s.get()) {
2099 setError(EGL_BAD_SURFACE, EGL_FALSE);
2100 return EGL_FALSE;
2101 }
2102
2103 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002104 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002105
2106 return EGL_TRUE;
2107}
2108
Craig Donner60761072017-01-27 12:30:44 -08002109EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2110 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002111 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2112 // this function cannot be implemented when this libEGL is built for
2113 // vendors.
2114#ifndef __ANDROID_VNDK__
Yi Kong48a6cd22018-07-18 10:07:09 -07002115 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer) nullptr);
Mathias Agopian61963402017-02-24 16:38:15 -08002116 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002117#else
Yi Kong48a6cd22018-07-18 10:07:09 -07002118 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer) nullptr);
Jiyong Parka243e5d2017-06-21 12:26:51 +09002119#endif
Craig Donner60761072017-01-27 12:30:44 -08002120}
2121
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002122// ----------------------------------------------------------------------------
2123// NVIDIA extensions
2124// ----------------------------------------------------------------------------
2125EGLuint64NV eglGetSystemTimeFrequencyNV()
2126{
2127 clearError();
2128
2129 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002130 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002131 }
2132
2133 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002134 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002135
Mathias Agopianada798b2012-02-13 17:09:30 -08002136 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2137 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002138 }
2139
Mathias Agopian737b8962017-02-24 14:32:05 -08002140 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002141}
2142
2143EGLuint64NV eglGetSystemTimeNV()
2144{
2145 clearError();
2146
2147 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002148 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002149 }
2150
2151 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002152 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002153
Mathias Agopianada798b2012-02-13 17:09:30 -08002154 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2155 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002156 }
2157
Mathias Agopian737b8962017-02-24 14:32:05 -08002158 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002159}
Dan Stozaa894d082015-02-19 15:27:36 -08002160
2161// ----------------------------------------------------------------------------
2162// Partial update extension
2163// ----------------------------------------------------------------------------
2164EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2165 EGLint *rects, EGLint n_rects)
2166{
2167 clearError();
2168
2169 const egl_display_ptr dp = validate_display(dpy);
2170 if (!dp) {
2171 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2172 return EGL_FALSE;
2173 }
2174
2175 SurfaceRef _s(dp.get(), surface);
2176 if (!_s.get()) {
2177 setError(EGL_BAD_SURFACE, EGL_FALSE);
2178 return EGL_FALSE;
2179 }
2180
2181 egl_surface_t const * const s = get_surface(surface);
2182 if (s->cnx->egl.eglSetDamageRegionKHR) {
2183 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2184 rects, n_rects);
2185 }
2186
2187 return EGL_FALSE;
2188}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002189
Brian Anderson1049d1d2016-12-16 17:25:57 -08002190EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2191 EGLuint64KHR *frameId) {
2192 clearError();
2193
2194 const egl_display_ptr dp = validate_display(dpy);
2195 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002196 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002197 }
2198
2199 SurfaceRef _s(dp.get(), surface);
2200 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002201 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002202 }
2203
2204 egl_surface_t const * const s = get_surface(surface);
2205
Mathias Agopian65421432017-03-08 11:49:05 -08002206 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002207 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002208 }
2209
2210 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002211 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002212
Mathias Agopian65421432017-03-08 11:49:05 -08002213 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002214 // This should not happen. Return an error that is not in the spec
2215 // so it's obvious something is very wrong.
2216 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002217 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002218 }
2219
2220 *frameId = nextFrameId;
2221 return EGL_TRUE;
2222}
2223
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002224EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2225 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2226{
2227 clearError();
2228
2229 const egl_display_ptr dp = validate_display(dpy);
2230 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002231 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002232 }
2233
2234 SurfaceRef _s(dp.get(), surface);
2235 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002236 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002237 }
2238
2239 egl_surface_t const * const s = get_surface(surface);
2240
Mathias Agopian65421432017-03-08 11:49:05 -08002241 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002242 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002243 }
2244
2245 nsecs_t* compositeDeadline = nullptr;
2246 nsecs_t* compositeInterval = nullptr;
2247 nsecs_t* compositeToPresentLatency = nullptr;
2248
2249 for (int i = 0; i < numTimestamps; i++) {
2250 switch (names[i]) {
2251 case EGL_COMPOSITE_DEADLINE_ANDROID:
2252 compositeDeadline = &values[i];
2253 break;
2254 case EGL_COMPOSITE_INTERVAL_ANDROID:
2255 compositeInterval = &values[i];
2256 break;
2257 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2258 compositeToPresentLatency = &values[i];
2259 break;
2260 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002261 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002262 }
2263 }
2264
Mathias Agopian65421432017-03-08 11:49:05 -08002265 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002266 compositeDeadline, compositeInterval, compositeToPresentLatency);
2267
2268 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002269 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002270 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002271 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002272 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002273 default:
2274 // This should not happen. Return an error that is not in the spec
2275 // so it's obvious something is very wrong.
2276 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002277 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002278 }
2279}
2280
2281EGLBoolean eglGetCompositorTimingSupportedANDROID(
2282 EGLDisplay dpy, EGLSurface surface, EGLint name)
2283{
2284 clearError();
2285
2286 const egl_display_ptr dp = validate_display(dpy);
2287 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002288 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002289 }
2290
2291 SurfaceRef _s(dp.get(), surface);
2292 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002293 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002294 }
2295
2296 egl_surface_t const * const s = get_surface(surface);
2297
Mathias Agopian65421432017-03-08 11:49:05 -08002298 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002299 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002300 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002301 }
2302
2303 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002304 case EGL_COMPOSITE_DEADLINE_ANDROID:
2305 case EGL_COMPOSITE_INTERVAL_ANDROID:
2306 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2307 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002308 default:
2309 return EGL_FALSE;
2310 }
2311}
2312
Pablo Ceballosc18be292016-05-31 14:55:42 -07002313EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002314 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002315 EGLnsecsANDROID *values)
2316{
2317 clearError();
2318
2319 const egl_display_ptr dp = validate_display(dpy);
2320 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002321 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002322 }
2323
2324 SurfaceRef _s(dp.get(), surface);
2325 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002326 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002327 }
2328
2329 egl_surface_t const * const s = get_surface(surface);
2330
Mathias Agopian65421432017-03-08 11:49:05 -08002331 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002332 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002333 }
2334
Brian Andersondbd0ea82016-07-22 09:38:59 -07002335 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002336 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002337 nsecs_t* latchTime = nullptr;
2338 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002339 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002340 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002341 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002342 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002343 nsecs_t* releaseTime = nullptr;
2344
2345 for (int i = 0; i < numTimestamps; i++) {
2346 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002347 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2348 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002349 break;
2350 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2351 acquireTime = &values[i];
2352 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002353 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2354 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002355 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002356 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2357 firstRefreshStartTime = &values[i];
2358 break;
2359 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2360 lastRefreshStartTime = &values[i];
2361 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002362 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2363 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002364 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002365 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2366 displayPresentTime = &values[i];
2367 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002368 case EGL_DEQUEUE_READY_TIME_ANDROID:
2369 dequeueReadyTime = &values[i];
2370 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002371 case EGL_READS_DONE_TIME_ANDROID:
2372 releaseTime = &values[i];
2373 break;
2374 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002375 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002376 }
2377 }
2378
Mathias Agopian65421432017-03-08 11:49:05 -08002379 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002380 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002381 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002382 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002383
Brian Anderson069b3652016-07-22 10:32:47 -07002384 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002385 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002386 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002387 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002388 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002389 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002390 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002391 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002392 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2393 default:
2394 // This should not happen. Return an error that is not in the spec
2395 // so it's obvious something is very wrong.
2396 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2397 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002398 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002399}
2400
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002401EGLBoolean eglGetFrameTimestampSupportedANDROID(
2402 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002403{
2404 clearError();
2405
2406 const egl_display_ptr dp = validate_display(dpy);
2407 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002408 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002409 }
2410
2411 SurfaceRef _s(dp.get(), surface);
2412 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002413 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002414 }
2415
2416 egl_surface_t const * const s = get_surface(surface);
2417
Mathias Agopian65421432017-03-08 11:49:05 -08002418 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002419 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002420 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002421 }
2422
2423 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002424 case EGL_COMPOSITE_DEADLINE_ANDROID:
2425 case EGL_COMPOSITE_INTERVAL_ANDROID:
2426 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002427 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002428 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002429 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2430 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2431 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002432 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002433 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002434 case EGL_READS_DONE_TIME_ANDROID:
2435 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002436 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2437 int value = 0;
2438 window->query(window,
2439 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2440 return value == 0 ? EGL_FALSE : EGL_TRUE;
2441 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002442 default:
2443 return EGL_FALSE;
2444 }
2445}