blob: 138963e29f7207881baa36fc8a775d48f4cdbed2 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
Craig Donner60761072017-01-27 12:30:44 -080029#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080030#include <private/android/AHardwareBufferHelpers.h>
31
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Mathias Agopian65421432017-03-08 11:49:05 -080036#include <condition_variable>
37#include <deque>
38#include <mutex>
39#include <unordered_map>
40#include <string>
41#include <thread>
Mathias Agopian518ec112011-05-13 16:21:08 -070042
Mathias Agopian39c24a22013-04-04 23:17:56 -070043#include "../egl_impl.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070044
45#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070046#include "egl_object.h"
47#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080048#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070049
50using namespace android;
51
52// ----------------------------------------------------------------------------
53
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070054namespace android {
55
Mathias Agopian65421432017-03-08 11:49:05 -080056using nsecs_t = int64_t;
57
Mathias Agopian518ec112011-05-13 16:21:08 -070058struct extention_map_t {
59 const char* name;
60 __eglMustCastToProperFunctionPointerType address;
61};
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063/*
Jesse Hall21558da2013-08-06 15:31:22 -070064 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070065 *
Jesse Hall21558da2013-08-06 15:31:22 -070066 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
67 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070068 *
Jesse Hall21558da2013-08-06 15:31:22 -070069 * The rest (gExtensionString) depend on support in the EGL driver, and are
70 * only available if the driver supports them. However, some of these must be
71 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080072 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070073 * the mandatory extensions are present and may not function properly if some
74 * are missing.
75 *
76 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070077 */
Mathias Agopian737b8962017-02-24 14:32:05 -080078
Mathias Agopian311b4792017-02-28 15:00:49 -080079extern char const * const gBuiltinExtensionString;
80extern char const * const gExtensionString;
Mathias Agopian737b8962017-02-24 14:32:05 -080081
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -060082// clang-format off
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -070083// Extensions implemented by the EGL wrapper.
Mathias Agopian311b4792017-02-28 15:00:49 -080084char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070085 "EGL_KHR_get_all_proc_addresses "
86 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080087 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080088 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080089 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070090 "EGL_ANDROID_get_frame_timestamps "
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070091 "EGL_EXT_surface_SMPTE2086_metadata "
92 "EGL_EXT_surface_CTA861_3_metadata "
Jesse Hall21558da2013-08-06 15:31:22 -070093 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080094
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -070095// Whitelist of extensions exposed to applications if implemented in the vendor driver.
Mathias Agopian311b4792017-02-28 15:00:49 -080096char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_image " // mandatory
98 "EGL_KHR_image_base " // mandatory
Krzysztof Kosiński41171c52018-04-19 21:38:54 -070099 "EGL_EXT_image_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700100 "EGL_KHR_image_pixmap "
101 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700102 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700104 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700105 "EGL_KHR_gl_texture_cubemap_image "
106 "EGL_KHR_gl_renderbuffer_image "
107 "EGL_KHR_reusable_sync "
108 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700109 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700110 "EGL_KHR_config_attribs "
111 "EGL_KHR_surfaceless_context "
112 "EGL_KHR_stream "
113 "EGL_KHR_stream_fifo "
114 "EGL_KHR_stream_producer_eglsurface "
115 "EGL_KHR_stream_consumer_gltexture "
116 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700117 "EGL_EXT_create_context_robustness "
118 "EGL_NV_system_time "
119 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700120 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700121 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800122 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700123 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800124 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700125 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700126 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700127 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700128 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000129 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800130 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700131 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600132// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700133
134// extensions not exposed to applications but used by the ANDROID system
135// "EGL_ANDROID_blob_cache " // strongly recommended
136// "EGL_IMG_hibernate_process " // optional
137// "EGL_ANDROID_native_fence_sync " // strongly recommended
138// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700139// "EGL_ANDROID_image_crop " // optional
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 }
261 return NULL;
262}
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
Mathias Agopian7773c432012-02-13 20:06:08 -0800344 if (num_config==0) {
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
369 if (num_config==0) {
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
Romain Guy1cffc802012-10-15 18:13:05 -0700388 const EGLint *attribRendererable = NULL;
389 const EGLint *attribCaveat = NULL;
390
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
Jesse Hallb29e5e82012-04-04 16:53:42 -0700444 egl_connection_t* cnx = NULL;
445 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) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800459 return HAL_DATASPACE_SRGB_LINEAR;
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ńskif2fc4e92018-04-18 16:29:49 -0700478// Returns a list of color spaces understood by the vendor EGL driver.
479static std::vector<EGLint> getDriverColorSpaces(egl_display_ptr dp,
480 android_pixel_format format) {
481 std::vector<EGLint> colorSpaces;
482 if (!dp->hasColorSpaceSupport) return colorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700483
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700484 // OpenGL drivers only support sRGB encoding with 8-bit formats.
485 // RGB_888 is never returned by getNativePixelFormat, but is included for completeness.
486 const bool formatSupportsSRGBEncoding =
487 format == HAL_PIXEL_FORMAT_RGBA_8888 || format == HAL_PIXEL_FORMAT_RGBX_8888 ||
488 format == HAL_PIXEL_FORMAT_RGB_888;
489 const bool formatIsFloatingPoint = format == HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700490
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700491 if (formatSupportsSRGBEncoding) {
492 // sRGB and linear are always supported when color space support is present.
493 colorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
494 colorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
495 // DCI-P3 uses the sRGB transfer function, so it's only relevant for 8-bit formats.
496 if (findExtension(dp->disp.queryString.extensions,
497 "EGL_EXT_gl_colorspace_display_p3")) {
498 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600499 }
500 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700501
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700502 // According to the spec, scRGB is only supported for floating point formats.
503 // For non-linear scRGB, the application is responsible for applying the
504 // transfer function.
505 if (formatIsFloatingPoint) {
506 if (findExtension(dp->disp.queryString.extensions,
507 "EGL_EXT_gl_colorspace_scrgb")) {
508 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_EXT);
509 }
510 if (findExtension(dp->disp.queryString.extensions,
511 "EGL_EXT_gl_colorspace_scrgb_linear")) {
512 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
513 }
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600514 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700515
516 // BT2020 can be used with any pixel format. PQ encoding must be applied by the
517 // application and does not affect the behavior of OpenGL.
518 if (findExtension(dp->disp.queryString.extensions,
519 "EGL_EXT_gl_colorspace_bt2020_linear")) {
520 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
521 }
522 if (findExtension(dp->disp.queryString.extensions,
523 "EGL_EXT_gl_colorspace_bt2020_pq")) {
524 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
525 }
526
527 // Linear DCI-P3 simply uses different primaries than standard RGB and thus
528 // can be used with any pixel format.
529 if (findExtension(dp->disp.queryString.extensions,
530 "EGL_EXT_gl_colorspace_display_p3_linear")) {
531 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
532 }
533 return colorSpaces;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600534}
535
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700536// Cleans up color space related parameters that the driver does not understand.
537// If there is no color space attribute in attrib_list, colorSpace is left
538// unmodified.
539static EGLBoolean processAttributes(egl_display_ptr dp, NativeWindowType window,
540 android_pixel_format format, const EGLint* attrib_list,
541 EGLint* colorSpace,
542 std::vector<EGLint>* strippedAttribList) {
543 for (const EGLint* attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
544 bool copyAttribute = true;
545 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
546 // Fail immediately if the driver doesn't have color space support at all.
547 if (!dp->hasColorSpaceSupport) return false;
548 *colorSpace = attr[1];
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600549
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700550 // Strip the attribute if the driver doesn't understand it.
551 copyAttribute = false;
552 std::vector<EGLint> driverColorSpaces = getDriverColorSpaces(dp, format);
553 for (auto driverColorSpace : driverColorSpaces) {
554 if (attr[1] == driverColorSpace) {
555 copyAttribute = true;
556 break;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600557 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600558 }
559 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700560 if (copyAttribute) {
561 strippedAttribList->push_back(attr[0]);
562 strippedAttribList->push_back(attr[1]);
563 }
564 }
565 // Terminate the attribute list.
566 strippedAttribList->push_back(EGL_NONE);
567
568 // If the passed color space has wide color gamut, check whether the target native window
569 // supports wide color.
570 const bool colorSpaceIsNarrow =
571 *colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
572 *colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR;
573 if (window && !colorSpaceIsNarrow) {
574 bool windowSupportsWideColor = true;
575 // Ordinarily we'd put a call to native_window_get_wide_color_support
576 // at the beginning of the function so that we'll have the
577 // result when needed elsewhere in the function.
578 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
579 // SurfaceFlinger is required to answer the call below we would
580 // end up in a deadlock situation. By moving the call to only happen
581 // if the application has specifically asked for wide-color we avoid
582 // the deadlock with SurfaceFlinger since it will not ask for a
583 // wide-color surface.
584 int err = native_window_get_wide_color_support(window, &windowSupportsWideColor);
585
586 if (err) {
587 ALOGE("processAttributes: invalid window (win=%p) "
588 "failed (%#x) (already connected to another API?)",
589 window, err);
590 return false;
591 }
592 if (!windowSupportsWideColor) {
593 // Application has asked for a wide-color colorspace but
594 // wide-color support isn't available on the display the window is on.
595 return false;
596 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600597 }
598 return true;
599}
600
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700601// Gets the native pixel format corrsponding to the passed EGLConfig.
602void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config,
603 android_pixel_format* format) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600604 // Set the native window's buffers format to match what this config requests.
605 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
606 // of our native format. So if sRGB gamma is requested, we have to
607 // modify the EGLconfig's format before setting the native window's
608 // format.
609
610 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
611 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
612
613 EGLint a = 0;
614 EGLint r, g, b;
615 r = g = b = 0;
616 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
617 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
618 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
619 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
620 EGLint colorDepth = r + g + b;
621
622 // Today, the driver only understands sRGB and linear on 888X
623 // formats. Strip other colorspaces from the attribute list and
624 // only use them to set the dataspace via
625 // native_window_set_buffers_dataspace
626 // if pixel format is RGBX 8888
627 // TBD: Can test for future extensions that indicate that driver
628 // handles requested color space and we can let it through.
629 // allow SRGB and LINEAR. All others need to be stripped.
630 // else if 565, 4444
631 // TBD: Can we assume these are supported if 8888 is?
632 // else if FP16 or 1010102
633 // strip colorspace from attribs.
634 // endif
635 if (a == 0) {
636 if (colorDepth <= 16) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700637 *format = HAL_PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600638 } else {
639 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
640 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700641 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600642 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700643 *format = HAL_PIXEL_FORMAT_RGBX_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600644 }
645 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700646 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600647 }
648 }
649 } else {
650 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
651 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700652 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600653 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700654 *format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600655 }
656 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700657 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600658 }
659 }
660}
661
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700662EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
663 android_smpte2086_metadata smpteMetadata;
664 if (s->getSmpte2086Metadata(smpteMetadata)) {
665 int err =
666 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
667 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700668 if (err != 0) {
669 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
670 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700671 return EGL_FALSE;
672 }
673 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700674 android_cta861_3_metadata cta8613Metadata;
675 if (s->getCta8613Metadata(cta8613Metadata)) {
676 int err =
677 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
678 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700679 if (err != 0) {
680 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
681 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700682 return EGL_FALSE;
683 }
684 }
685 return EGL_TRUE;
686}
687
Mathias Agopian518ec112011-05-13 16:21:08 -0700688EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
689 NativeWindowType window,
690 const EGLint *attrib_list)
691{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700692 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700693 clearError();
694
Jesse Hallb29e5e82012-04-04 16:53:42 -0700695 egl_connection_t* cnx = NULL;
696 egl_display_ptr dp = validate_display_connection(dpy, cnx);
697 if (dp) {
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800698 if (!window) {
699 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
700 }
701
702 int value = 0;
703 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
704 if (!value) {
705 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
706 }
707
Andy McFaddend566ce32014-01-07 15:54:17 -0800708 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800709 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800710 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
711 "failed (%#x) (already connected to another API?)",
712 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700713 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700714 }
715
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700716 EGLDisplay iDpy = dp->disp.dpy;
717 android_pixel_format format;
718 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600719
720 // now select correct colorspace and dataspace based on user's attribute list
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700721 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
722 std::vector<EGLint> strippedAttribList;
723 if (!processAttributes(dp, window, format, attrib_list, &colorSpace,
724 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600725 ALOGE("error invalid colorspace: %d", colorSpace);
726 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700727 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700728 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800729
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700730 {
Jesse Hallc2e41222013-08-08 13:40:22 -0700731 int err = native_window_set_buffers_format(window, format);
732 if (err != 0) {
733 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700734 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700735 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
736 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
737 }
738 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700739
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700740 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
741 if (dataSpace != HAL_DATASPACE_UNKNOWN) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800742 int err = native_window_set_buffers_data_space(window, dataSpace);
743 if (err != 0) {
744 ALOGE("error setting native window pixel dataSpace: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700745 strerror(-err), err);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800746 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
747 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
748 }
749 }
750
Jamie Gennis59769462011-11-19 18:04:43 -0800751 // the EGL spec requires that a new EGLSurface default to swap interval
752 // 1, so explicitly set that on the window here.
753 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
754 anw->setSwapInterval(anw, 1);
755
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800757 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600759 egl_surface_t* s =
760 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700761 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700762 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700763
764 // EGLSurface creation failed
765 native_window_set_buffers_format(window, 0);
766 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 }
768 return EGL_NO_SURFACE;
769}
770
771EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
772 NativePixmapType pixmap,
773 const EGLint *attrib_list)
774{
775 clearError();
776
Jesse Hallb29e5e82012-04-04 16:53:42 -0700777 egl_connection_t* cnx = NULL;
778 egl_display_ptr dp = validate_display_connection(dpy, cnx);
779 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700780 EGLDisplay iDpy = dp->disp.dpy;
781 android_pixel_format format;
782 getNativePixelFormat(iDpy, cnx, config, &format);
783
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600784 // now select a corresponding sRGB format if needed
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700785 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
786 std::vector<EGLint> strippedAttribList;
787 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
788 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600789 ALOGE("error invalid colorspace: %d", colorSpace);
790 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
791 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700792 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600793
Mathias Agopian518ec112011-05-13 16:21:08 -0700794 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800795 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700796 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600797 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 return s;
799 }
800 }
801 return EGL_NO_SURFACE;
802}
803
804EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
805 const EGLint *attrib_list)
806{
807 clearError();
808
Jesse Hallb29e5e82012-04-04 16:53:42 -0700809 egl_connection_t* cnx = NULL;
810 egl_display_ptr dp = validate_display_connection(dpy, cnx);
811 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600812 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700813 android_pixel_format format;
814 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600815
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700816 // Select correct colorspace based on user's attribute list
817 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
818 std::vector<EGLint> strippedAttribList;
819 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
820 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600821 ALOGE("error invalid colorspace: %d", colorSpace);
822 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
823 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700824 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600825
Mathias Agopian518ec112011-05-13 16:21:08 -0700826 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800827 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700828 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600829 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700830 return s;
831 }
832 }
833 return EGL_NO_SURFACE;
834}
Jesse Hall47743382013-02-08 11:13:46 -0800835
Mathias Agopian518ec112011-05-13 16:21:08 -0700836EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
837{
838 clearError();
839
Jesse Hallb29e5e82012-04-04 16:53:42 -0700840 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700841 if (!dp) return EGL_FALSE;
842
Jesse Hallb29e5e82012-04-04 16:53:42 -0700843 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700844 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800845 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700846
847 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800848 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700849 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700850 _s.terminate();
851 }
852 return result;
853}
854
855EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
856 EGLint attribute, EGLint *value)
857{
858 clearError();
859
Jesse Hallb29e5e82012-04-04 16:53:42 -0700860 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700861 if (!dp) return EGL_FALSE;
862
Jesse Hallb29e5e82012-04-04 16:53:42 -0700863 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700864 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800865 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700866
Mathias Agopian518ec112011-05-13 16:21:08 -0700867 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700868 if (s->getColorSpaceAttribute(attribute, value)) {
869 return EGL_TRUE;
870 } else if (s->getSmpte2086Attribute(attribute, value)) {
871 return EGL_TRUE;
872 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600873 return EGL_TRUE;
874 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700875 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700876}
877
Jamie Gennise8696a42012-01-15 18:54:57 -0800878void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800879 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800880 clearError();
881
Jesse Hallb29e5e82012-04-04 16:53:42 -0700882 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800883 if (!dp) {
884 return;
885 }
886
Jesse Hallb29e5e82012-04-04 16:53:42 -0700887 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800888 if (!_s.get()) {
889 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800890 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800891}
892
Mathias Agopian518ec112011-05-13 16:21:08 -0700893// ----------------------------------------------------------------------------
894// Contexts
895// ----------------------------------------------------------------------------
896
897EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
898 EGLContext share_list, const EGLint *attrib_list)
899{
900 clearError();
901
Jesse Hallb29e5e82012-04-04 16:53:42 -0700902 egl_connection_t* cnx = NULL;
903 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700904 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700905 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700906 if (!ContextRef(dp.get(), share_list).get()) {
907 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
908 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700909 egl_context_t* const c = get_context(share_list);
910 share_list = c->context;
911 }
912 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800913 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700914 if (context != EGL_NO_CONTEXT) {
915 // figure out if it's a GLESv1 or GLESv2
916 int version = 0;
917 if (attrib_list) {
918 while (*attrib_list != EGL_NONE) {
919 GLint attr = *attrib_list++;
920 GLint value = *attrib_list++;
921 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
922 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800923 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800924 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800925 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700926 }
927 }
928 };
929 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700930 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
931 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700932 return c;
933 }
934 }
935 return EGL_NO_CONTEXT;
936}
937
938EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
939{
940 clearError();
941
Jesse Hallb29e5e82012-04-04 16:53:42 -0700942 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700943 if (!dp)
944 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700945
Jesse Hallb29e5e82012-04-04 16:53:42 -0700946 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700947 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800948 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800949
Mathias Agopian518ec112011-05-13 16:21:08 -0700950 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800951 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700952 if (result == EGL_TRUE) {
953 _c.terminate();
954 }
955 return result;
956}
957
Mathias Agopian518ec112011-05-13 16:21:08 -0700958EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
959 EGLSurface read, EGLContext ctx)
960{
961 clearError();
962
Jesse Hallb29e5e82012-04-04 16:53:42 -0700963 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800964 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700965
Mathias Agopian5b287a62011-05-16 18:58:55 -0700966 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
967 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
968 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
970 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800971 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700972 }
973
974 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700975 ContextRef _c(dp.get(), ctx);
976 SurfaceRef _d(dp.get(), draw);
977 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700978
979 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700980 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700981 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800982 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 }
984
985 // these are the underlying implementation's object
986 EGLContext impl_ctx = EGL_NO_CONTEXT;
987 EGLSurface impl_draw = EGL_NO_SURFACE;
988 EGLSurface impl_read = EGL_NO_SURFACE;
989
990 // these are our objects structs passed in
991 egl_context_t * c = NULL;
992 egl_surface_t const * d = NULL;
993 egl_surface_t const * r = NULL;
994
995 // these are the current objects structs
996 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800997
Mathias Agopian518ec112011-05-13 16:21:08 -0700998 if (ctx != EGL_NO_CONTEXT) {
999 c = get_context(ctx);
1000 impl_ctx = c->context;
1001 } else {
1002 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001003 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1004 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001005 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001006 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001007 if (cur_c == NULL) {
1008 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001009 // not an error, there is just no current context.
1010 return EGL_TRUE;
1011 }
1012 }
1013
1014 // retrieve the underlying implementation's draw EGLSurface
1015 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001016 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001018 impl_draw = d->surface;
1019 }
1020
1021 // retrieve the underlying implementation's read EGLSurface
1022 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001023 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001024 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001025 impl_read = r->surface;
1026 }
1027
Mathias Agopian518ec112011-05-13 16:21:08 -07001028
Jesse Hallb29e5e82012-04-04 16:53:42 -07001029 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001030 draw, read, ctx,
1031 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001032
1033 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001034 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001035 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1036 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001037 _c.acquire();
1038 _r.acquire();
1039 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001040 } else {
1041 setGLHooksThreadSpecific(&gHooksNoContext);
1042 egl_tls_t::setContext(EGL_NO_CONTEXT);
1043 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001044 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001045 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001046 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001047 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001048 }
1049 return result;
1050}
1051
1052
1053EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1054 EGLint attribute, EGLint *value)
1055{
1056 clearError();
1057
Jesse Hallb29e5e82012-04-04 16:53:42 -07001058 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001059 if (!dp) return EGL_FALSE;
1060
Jesse Hallb29e5e82012-04-04 16:53:42 -07001061 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001062 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001063
Mathias Agopian518ec112011-05-13 16:21:08 -07001064 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001065 return c->cnx->egl.eglQueryContext(
1066 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001067
Mathias Agopian518ec112011-05-13 16:21:08 -07001068}
1069
1070EGLContext eglGetCurrentContext(void)
1071{
1072 // could be called before eglInitialize(), but we wouldn't have a context
1073 // then, and this function would correctly return EGL_NO_CONTEXT.
1074
1075 clearError();
1076
1077 EGLContext ctx = getContext();
1078 return ctx;
1079}
1080
1081EGLSurface eglGetCurrentSurface(EGLint readdraw)
1082{
1083 // could be called before eglInitialize(), but we wouldn't have a context
1084 // then, and this function would correctly return EGL_NO_SURFACE.
1085
1086 clearError();
1087
1088 EGLContext ctx = getContext();
1089 if (ctx) {
1090 egl_context_t const * const c = get_context(ctx);
1091 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1092 switch (readdraw) {
1093 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001094 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001095 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1096 }
1097 }
1098 return EGL_NO_SURFACE;
1099}
1100
1101EGLDisplay eglGetCurrentDisplay(void)
1102{
1103 // could be called before eglInitialize(), but we wouldn't have a context
1104 // then, and this function would correctly return EGL_NO_DISPLAY.
1105
1106 clearError();
1107
1108 EGLContext ctx = getContext();
1109 if (ctx) {
1110 egl_context_t const * const c = get_context(ctx);
1111 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1112 return c->dpy;
1113 }
1114 return EGL_NO_DISPLAY;
1115}
1116
1117EGLBoolean eglWaitGL(void)
1118{
Mathias Agopian518ec112011-05-13 16:21:08 -07001119 clearError();
1120
Mathias Agopianada798b2012-02-13 17:09:30 -08001121 egl_connection_t* const cnx = &gEGLImpl;
1122 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001123 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001124
1125 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001126}
1127
1128EGLBoolean eglWaitNative(EGLint engine)
1129{
Mathias Agopian518ec112011-05-13 16:21:08 -07001130 clearError();
1131
Mathias Agopianada798b2012-02-13 17:09:30 -08001132 egl_connection_t* const cnx = &gEGLImpl;
1133 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001134 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001135
1136 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001137}
1138
1139EGLint eglGetError(void)
1140{
Mathias Agopianada798b2012-02-13 17:09:30 -08001141 EGLint err = EGL_SUCCESS;
1142 egl_connection_t* const cnx = &gEGLImpl;
1143 if (cnx->dso) {
1144 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001145 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001146 if (err == EGL_SUCCESS) {
1147 err = egl_tls_t::getError();
1148 }
1149 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001150}
1151
Michael Chockc0ec5e22014-01-27 08:14:33 -08001152static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001153 const char* procname) {
1154 const egl_connection_t* cnx = &gEGLImpl;
1155 void* proc = NULL;
1156
Michael Chockc0ec5e22014-01-27 08:14:33 -08001157 proc = dlsym(cnx->libEgl, procname);
1158 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1159
Jesse Hallc07b5202013-07-04 12:08:16 -07001160 proc = dlsym(cnx->libGles2, procname);
1161 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1162
1163 proc = dlsym(cnx->libGles1, procname);
1164 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1165
1166 return NULL;
1167}
1168
Mathias Agopian518ec112011-05-13 16:21:08 -07001169__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1170{
1171 // eglGetProcAddress() could be the very first function called
1172 // in which case we must make sure we've initialized ourselves, this
1173 // happens the first time egl_get_display() is called.
1174
1175 clearError();
1176
1177 if (egl_init_drivers() == EGL_FALSE) {
1178 setError(EGL_BAD_PARAMETER, NULL);
1179 return NULL;
1180 }
1181
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001182 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001183 return NULL;
1184 }
1185
Mathias Agopian518ec112011-05-13 16:21:08 -07001186 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001187 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001188 if (addr) return addr;
1189
Michael Chockc0ec5e22014-01-27 08:14:33 -08001190 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001191 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001192
Mathias Agopian518ec112011-05-13 16:21:08 -07001193 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1194 pthread_mutex_lock(&sExtensionMapMutex);
1195
1196 /*
1197 * Since eglGetProcAddress() is not associated to anything, it needs
1198 * to return a function pointer that "works" regardless of what
1199 * the current context is.
1200 *
1201 * For this reason, we return a "forwarder", a small stub that takes
1202 * care of calling the function associated with the context
1203 * currently bound.
1204 *
1205 * We first look for extensions we've already resolved, if we're seeing
1206 * this extension for the first time, we go through all our
1207 * implementations and call eglGetProcAddress() and record the
1208 * result in the appropriate implementation hooks and return the
1209 * address of the forwarder corresponding to that hook set.
1210 *
1211 */
1212
Mathias Agopian65421432017-03-08 11:49:05 -08001213 const std::string name(procname);
1214
1215 auto& extentionMap = sGLExtentionMap;
1216 auto pos = extentionMap.find(name);
1217 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001218 const int slot = sGLExtentionSlot;
1219
Steve Blocke6f43dd2012-01-06 19:20:56 +00001220 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001221 "no more slots for eglGetProcAddress(\"%s\")",
1222 procname);
1223
1224 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1225 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001226
1227 egl_connection_t* const cnx = &gEGLImpl;
1228 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001229 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001230 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001231 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1232 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001233 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001234 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001235 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001236
Mathias Agopian518ec112011-05-13 16:21:08 -07001237 if (found) {
1238 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001239 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001240 sGLExtentionSlot++;
1241 }
1242 }
1243
1244 pthread_mutex_unlock(&sExtensionMapMutex);
1245 return addr;
1246}
1247
Mathias Agopian65421432017-03-08 11:49:05 -08001248class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001249public:
1250
1251 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001252 static FrameCompletionThread thread;
1253
1254 char name[64];
1255
1256 std::lock_guard<std::mutex> lock(thread.mMutex);
1257 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1258 ATRACE_NAME(name);
1259
1260 thread.mQueue.push_back(sync);
1261 thread.mCondition.notify_one();
1262 thread.mFramesQueued++;
1263 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001264 }
1265
1266private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001267
Mathias Agopian65421432017-03-08 11:49:05 -08001268 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1269 std::thread thread(&FrameCompletionThread::loop, this);
1270 thread.detach();
1271 }
1272
1273#pragma clang diagnostic push
1274#pragma clang diagnostic ignored "-Wmissing-noreturn"
1275 void loop() {
1276 while (true) {
1277 threadLoop();
1278 }
1279 }
1280#pragma clang diagnostic pop
1281
1282 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001283 EGLSyncKHR sync;
1284 uint32_t frameNum;
1285 {
Mathias Agopian65421432017-03-08 11:49:05 -08001286 std::unique_lock<std::mutex> lock(mMutex);
1287 while (mQueue.empty()) {
1288 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001289 }
1290 sync = mQueue[0];
1291 frameNum = mFramesCompleted;
1292 }
1293 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1294 {
Mathias Agopian65421432017-03-08 11:49:05 -08001295 char name[64];
1296 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1297 ATRACE_NAME(name);
1298
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001299 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1300 if (result == EGL_FALSE) {
1301 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1302 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1303 ALOGE("FrameCompletion: timeout waiting for fence");
1304 }
1305 eglDestroySyncKHR(dpy, sync);
1306 }
1307 {
Mathias Agopian65421432017-03-08 11:49:05 -08001308 std::lock_guard<std::mutex> lock(mMutex);
1309 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001310 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001311 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001312 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001313 }
1314
1315 uint32_t mFramesQueued;
1316 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001317 std::deque<EGLSyncKHR> mQueue;
1318 std::condition_variable mCondition;
1319 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001320};
1321
Dan Stozaa894d082015-02-19 15:27:36 -08001322EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1323 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001324{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001325 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001326 clearError();
1327
Jesse Hallb29e5e82012-04-04 16:53:42 -07001328 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001329 if (!dp) return EGL_FALSE;
1330
Jesse Hallb29e5e82012-04-04 16:53:42 -07001331 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001332 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001333 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001334
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001335 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001336
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001337 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1338 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1339 if (sync != EGL_NO_SYNC_KHR) {
1340 FrameCompletionThread::queueSync(sync);
1341 }
1342 }
1343
Mathias Agopian7db993a2012-03-25 00:49:46 -07001344 if (CC_UNLIKELY(dp->finishOnSwap)) {
1345 uint32_t pixel;
1346 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1347 if (c) {
1348 // glReadPixels() ensures that the frame is complete
1349 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1350 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1351 }
1352 }
1353
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001354 if (!sendSurfaceMetadata(s)) {
1355 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1356 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1357 }
1358
Dan Stozaa894d082015-02-19 15:27:36 -08001359 if (n_rects == 0) {
1360 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1361 }
1362
Mathias Agopian65421432017-03-08 11:49:05 -08001363 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001364 for (int r = 0; r < n_rects; ++r) {
1365 int offset = r * 4;
1366 int x = rects[offset];
1367 int y = rects[offset + 1];
1368 int width = rects[offset + 2];
1369 int height = rects[offset + 3];
1370 android_native_rect_t androidRect;
1371 androidRect.left = x;
1372 androidRect.top = y + height;
1373 androidRect.right = x + width;
1374 androidRect.bottom = y;
1375 androidRects.push_back(androidRect);
1376 }
Mathias Agopian65421432017-03-08 11:49:05 -08001377 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001378
1379 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1380 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1381 rects, n_rects);
1382 } else {
1383 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1384 }
1385}
1386
1387EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1388{
1389 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001390}
1391
1392EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1393 NativePixmapType target)
1394{
1395 clearError();
1396
Jesse Hallb29e5e82012-04-04 16:53:42 -07001397 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001398 if (!dp) return EGL_FALSE;
1399
Jesse Hallb29e5e82012-04-04 16:53:42 -07001400 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001401 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001402 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001403
Mathias Agopian518ec112011-05-13 16:21:08 -07001404 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001405 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001406}
1407
1408const char* eglQueryString(EGLDisplay dpy, EGLint name)
1409{
1410 clearError();
1411
Chia-I Wue57d1352016-08-15 16:10:02 +08001412 // Generate an error quietly when client extensions (as defined by
1413 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1414 // validate_display to generate the error as validate_display would log
1415 // the error, which can be misleading.
1416 //
1417 // If we want to support EGL_EXT_client_extensions later, we can return
1418 // the client extension string here instead.
1419 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001420 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001421
Jesse Hallb29e5e82012-04-04 16:53:42 -07001422 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001423 if (!dp) return (const char *) NULL;
1424
1425 switch (name) {
1426 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001427 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001428 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001429 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001431 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001432 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001433 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001434 default:
1435 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001436 }
1437 return setError(EGL_BAD_PARAMETER, (const char *)0);
1438}
1439
Jiyong Park00b15b82017-08-10 20:30:56 +09001440extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001441{
1442 clearError();
1443
1444 const egl_display_ptr dp = validate_display(dpy);
1445 if (!dp) return (const char *) NULL;
1446
1447 switch (name) {
1448 case EGL_VENDOR:
1449 return dp->disp.queryString.vendor;
1450 case EGL_VERSION:
1451 return dp->disp.queryString.version;
1452 case EGL_EXTENSIONS:
1453 return dp->disp.queryString.extensions;
1454 case EGL_CLIENT_APIS:
1455 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001456 default:
1457 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001458 }
1459 return setError(EGL_BAD_PARAMETER, (const char *)0);
1460}
Mathias Agopian518ec112011-05-13 16:21:08 -07001461
1462// ----------------------------------------------------------------------------
1463// EGL 1.1
1464// ----------------------------------------------------------------------------
1465
1466EGLBoolean eglSurfaceAttrib(
1467 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1468{
1469 clearError();
1470
Jesse Hallb29e5e82012-04-04 16:53:42 -07001471 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001472 if (!dp) return EGL_FALSE;
1473
Jesse Hallb29e5e82012-04-04 16:53:42 -07001474 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001475 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001476 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001477
Pablo Ceballosc18be292016-05-31 14:55:42 -07001478 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001479
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001480 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001481 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001482 setError(EGL_BAD_SURFACE, EGL_FALSE);
1483 }
Mathias Agopian65421432017-03-08 11:49:05 -08001484 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1485 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001486 }
1487
Pablo Ceballosc18be292016-05-31 14:55:42 -07001488 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001489 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001490 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001491 }
Mathias Agopian65421432017-03-08 11:49:05 -08001492 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1493 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001494 }
1495
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001496 if (s->setSmpte2086Attribute(attribute, value)) {
1497 return EGL_TRUE;
1498 } else if (s->setCta8613Attribute(attribute, value)) {
1499 return EGL_TRUE;
1500 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001501 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001502 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001504 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505}
1506
1507EGLBoolean eglBindTexImage(
1508 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1509{
1510 clearError();
1511
Jesse Hallb29e5e82012-04-04 16:53:42 -07001512 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001513 if (!dp) return EGL_FALSE;
1514
Jesse Hallb29e5e82012-04-04 16:53:42 -07001515 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001516 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001517 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001518
Mathias Agopian518ec112011-05-13 16:21:08 -07001519 egl_surface_t const * const s = get_surface(surface);
1520 if (s->cnx->egl.eglBindTexImage) {
1521 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001522 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001523 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001524 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001525}
1526
1527EGLBoolean eglReleaseTexImage(
1528 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1529{
1530 clearError();
1531
Jesse Hallb29e5e82012-04-04 16:53:42 -07001532 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001533 if (!dp) return EGL_FALSE;
1534
Jesse Hallb29e5e82012-04-04 16:53:42 -07001535 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001536 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001537 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001538
Mathias Agopian518ec112011-05-13 16:21:08 -07001539 egl_surface_t const * const s = get_surface(surface);
1540 if (s->cnx->egl.eglReleaseTexImage) {
1541 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001542 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001543 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001544 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001545}
1546
1547EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1548{
1549 clearError();
1550
Jesse Hallb29e5e82012-04-04 16:53:42 -07001551 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001552 if (!dp) return EGL_FALSE;
1553
1554 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001555 egl_connection_t* const cnx = &gEGLImpl;
1556 if (cnx->dso && cnx->egl.eglSwapInterval) {
1557 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001558 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001559
Mathias Agopian518ec112011-05-13 16:21:08 -07001560 return res;
1561}
1562
1563
1564// ----------------------------------------------------------------------------
1565// EGL 1.2
1566// ----------------------------------------------------------------------------
1567
1568EGLBoolean eglWaitClient(void)
1569{
1570 clearError();
1571
Mathias Agopianada798b2012-02-13 17:09:30 -08001572 egl_connection_t* const cnx = &gEGLImpl;
1573 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001574 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001575
1576 EGLBoolean res;
1577 if (cnx->egl.eglWaitClient) {
1578 res = cnx->egl.eglWaitClient();
1579 } else {
1580 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001581 }
1582 return res;
1583}
1584
1585EGLBoolean eglBindAPI(EGLenum api)
1586{
1587 clearError();
1588
1589 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001590 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001591 }
1592
1593 // bind this API on all EGLs
1594 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001595 egl_connection_t* const cnx = &gEGLImpl;
1596 if (cnx->dso && cnx->egl.eglBindAPI) {
1597 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001598 }
1599 return res;
1600}
1601
1602EGLenum eglQueryAPI(void)
1603{
1604 clearError();
1605
1606 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001607 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001608 }
1609
Mathias Agopianada798b2012-02-13 17:09:30 -08001610 egl_connection_t* const cnx = &gEGLImpl;
1611 if (cnx->dso && cnx->egl.eglQueryAPI) {
1612 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001613 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001614
Mathias Agopian518ec112011-05-13 16:21:08 -07001615 // or, it can only be OpenGL ES
1616 return EGL_OPENGL_ES_API;
1617}
1618
1619EGLBoolean eglReleaseThread(void)
1620{
1621 clearError();
1622
Mathias Agopianada798b2012-02-13 17:09:30 -08001623 egl_connection_t* const cnx = &gEGLImpl;
1624 if (cnx->dso && cnx->egl.eglReleaseThread) {
1625 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001626 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301627
1628 // If there is context bound to the thread, release it
1629 egl_display_t::loseCurrent(get_context(getContext()));
1630
Mathias Agopian518ec112011-05-13 16:21:08 -07001631 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001632 return EGL_TRUE;
1633}
1634
1635EGLSurface eglCreatePbufferFromClientBuffer(
1636 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1637 EGLConfig config, const EGLint *attrib_list)
1638{
1639 clearError();
1640
Jesse Hallb29e5e82012-04-04 16:53:42 -07001641 egl_connection_t* cnx = NULL;
1642 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1643 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001644 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1645 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001646 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001647 }
1648 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1649}
1650
1651// ----------------------------------------------------------------------------
1652// EGL_EGLEXT_VERSION 3
1653// ----------------------------------------------------------------------------
1654
1655EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1656 const EGLint *attrib_list)
1657{
1658 clearError();
1659
Jesse Hallb29e5e82012-04-04 16:53:42 -07001660 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001661 if (!dp) return EGL_FALSE;
1662
Jesse Hallb29e5e82012-04-04 16:53:42 -07001663 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001664 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001665 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001666
1667 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001668 if (s->cnx->egl.eglLockSurfaceKHR) {
1669 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001670 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001671 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001672 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001673}
1674
1675EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1676{
1677 clearError();
1678
Jesse Hallb29e5e82012-04-04 16:53:42 -07001679 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001680 if (!dp) return EGL_FALSE;
1681
Jesse Hallb29e5e82012-04-04 16:53:42 -07001682 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001683 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001684 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001685
1686 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001687 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001688 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001689 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001690 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001691}
1692
1693EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1694 EGLClientBuffer buffer, const EGLint *attrib_list)
1695{
1696 clearError();
1697
Jesse Hallb29e5e82012-04-04 16:53:42 -07001698 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001699 if (!dp) return EGL_NO_IMAGE_KHR;
1700
Jesse Hallb29e5e82012-04-04 16:53:42 -07001701 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001702 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001703
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001704 // Temporary hack: eglImageCreateKHR should accept EGL_GL_COLORSPACE_LINEAR_KHR,
1705 // EGL_GL_COLORSPACE_SRGB_KHR and EGL_GL_COLORSPACE_DEFAULT_EXT if
1706 // EGL_EXT_image_gl_colorspace is supported, but some drivers don't like
1707 // the DEFAULT value and generate an error.
1708 std::vector<EGLint> strippedAttribList;
1709 for (const EGLint *attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
1710 if (attr[0] == EGL_GL_COLORSPACE_KHR &&
1711 dp->haveExtension("EGL_EXT_image_gl_colorspace")) {
1712 if (attr[1] != EGL_GL_COLORSPACE_LINEAR_KHR &&
1713 attr[1] != EGL_GL_COLORSPACE_SRGB_KHR) {
1714 continue;
1715 }
1716 }
1717 strippedAttribList.push_back(attr[0]);
1718 strippedAttribList.push_back(attr[1]);
1719 }
1720 strippedAttribList.push_back(EGL_NONE);
1721
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001722 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1723 egl_connection_t* const cnx = &gEGLImpl;
1724 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1725 result = cnx->egl.eglCreateImageKHR(
1726 dp->disp.dpy,
1727 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001728 target, buffer, strippedAttribList.data());
Mathias Agopian518ec112011-05-13 16:21:08 -07001729 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001730 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001731}
1732
1733EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1734{
1735 clearError();
1736
Jesse Hallb29e5e82012-04-04 16:53:42 -07001737 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001738 if (!dp) return EGL_FALSE;
1739
Steven Holte646a5c52012-06-04 20:02:11 -07001740 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001741 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001742 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001743 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001744 }
Steven Holte646a5c52012-06-04 20:02:11 -07001745 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001746}
1747
1748// ----------------------------------------------------------------------------
1749// EGL_EGLEXT_VERSION 5
1750// ----------------------------------------------------------------------------
1751
1752
1753EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1754{
1755 clearError();
1756
Jesse Hallb29e5e82012-04-04 16:53:42 -07001757 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001758 if (!dp) return EGL_NO_SYNC_KHR;
1759
Mathias Agopian518ec112011-05-13 16:21:08 -07001760 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001761 egl_connection_t* const cnx = &gEGLImpl;
1762 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1763 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001764 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001765 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001766}
1767
1768EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1769{
1770 clearError();
1771
Jesse Hallb29e5e82012-04-04 16:53:42 -07001772 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001773 if (!dp) return EGL_FALSE;
1774
Mathias Agopian518ec112011-05-13 16:21:08 -07001775 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001776 egl_connection_t* const cnx = &gEGLImpl;
1777 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1778 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001779 }
1780 return result;
1781}
1782
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001783EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1784 clearError();
1785
1786 const egl_display_ptr dp = validate_display(dpy);
1787 if (!dp) return EGL_FALSE;
1788
1789 EGLBoolean result = EGL_FALSE;
1790 egl_connection_t* const cnx = &gEGLImpl;
1791 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1792 result = cnx->egl.eglSignalSyncKHR(
1793 dp->disp.dpy, sync, mode);
1794 }
1795 return result;
1796}
1797
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001798EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1799 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001800{
1801 clearError();
1802
Jesse Hallb29e5e82012-04-04 16:53:42 -07001803 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001804 if (!dp) return EGL_FALSE;
1805
Mathias Agopian737b8962017-02-24 14:32:05 -08001806 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001807 egl_connection_t* const cnx = &gEGLImpl;
1808 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1809 result = cnx->egl.eglClientWaitSyncKHR(
1810 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001811 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001812 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001813}
1814
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001815EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1816 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001817{
1818 clearError();
1819
Jesse Hallb29e5e82012-04-04 16:53:42 -07001820 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001821 if (!dp) return EGL_FALSE;
1822
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001823 EGLBoolean result = EGL_FALSE;
1824 egl_connection_t* const cnx = &gEGLImpl;
1825 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1826 result = cnx->egl.eglGetSyncAttribKHR(
1827 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001828 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001829 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001830}
1831
Season Li000d88f2015-07-01 11:39:40 -07001832EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1833{
1834 clearError();
1835
1836 const egl_display_ptr dp = validate_display(dpy);
1837 if (!dp) return EGL_NO_STREAM_KHR;
1838
1839 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1840 egl_connection_t* const cnx = &gEGLImpl;
1841 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1842 result = cnx->egl.eglCreateStreamKHR(
1843 dp->disp.dpy, attrib_list);
1844 }
1845 return result;
1846}
1847
1848EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1849{
1850 clearError();
1851
1852 const egl_display_ptr dp = validate_display(dpy);
1853 if (!dp) return EGL_FALSE;
1854
1855 EGLBoolean result = EGL_FALSE;
1856 egl_connection_t* const cnx = &gEGLImpl;
1857 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1858 result = cnx->egl.eglDestroyStreamKHR(
1859 dp->disp.dpy, stream);
1860 }
1861 return result;
1862}
1863
1864EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1865 EGLenum attribute, EGLint value)
1866{
1867 clearError();
1868
1869 const egl_display_ptr dp = validate_display(dpy);
1870 if (!dp) return EGL_FALSE;
1871
1872 EGLBoolean result = EGL_FALSE;
1873 egl_connection_t* const cnx = &gEGLImpl;
1874 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1875 result = cnx->egl.eglStreamAttribKHR(
1876 dp->disp.dpy, stream, attribute, value);
1877 }
1878 return result;
1879}
1880
1881EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1882 EGLenum attribute, EGLint *value)
1883{
1884 clearError();
1885
1886 const egl_display_ptr dp = validate_display(dpy);
1887 if (!dp) return EGL_FALSE;
1888
1889 EGLBoolean result = EGL_FALSE;
1890 egl_connection_t* const cnx = &gEGLImpl;
1891 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1892 result = cnx->egl.eglQueryStreamKHR(
1893 dp->disp.dpy, stream, attribute, value);
1894 }
1895 return result;
1896}
1897
1898EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1899 EGLenum attribute, EGLuint64KHR *value)
1900{
1901 clearError();
1902
1903 const egl_display_ptr dp = validate_display(dpy);
1904 if (!dp) return EGL_FALSE;
1905
1906 EGLBoolean result = EGL_FALSE;
1907 egl_connection_t* const cnx = &gEGLImpl;
1908 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1909 result = cnx->egl.eglQueryStreamu64KHR(
1910 dp->disp.dpy, stream, attribute, value);
1911 }
1912 return result;
1913}
1914
1915EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1916 EGLenum attribute, EGLTimeKHR *value)
1917{
1918 clearError();
1919
1920 const egl_display_ptr dp = validate_display(dpy);
1921 if (!dp) return EGL_FALSE;
1922
1923 EGLBoolean result = EGL_FALSE;
1924 egl_connection_t* const cnx = &gEGLImpl;
1925 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1926 result = cnx->egl.eglQueryStreamTimeKHR(
1927 dp->disp.dpy, stream, attribute, value);
1928 }
1929 return result;
1930}
1931
1932EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1933 EGLStreamKHR stream, const EGLint *attrib_list)
1934{
1935 clearError();
1936
1937 egl_display_ptr dp = validate_display(dpy);
1938 if (!dp) return EGL_NO_SURFACE;
1939
1940 egl_connection_t* const cnx = &gEGLImpl;
1941 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1942 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1943 dp->disp.dpy, config, stream, attrib_list);
1944 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001945 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
1946 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001947 return s;
1948 }
1949 }
1950 return EGL_NO_SURFACE;
1951}
1952
1953EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1954 EGLStreamKHR stream)
1955{
1956 clearError();
1957
1958 const egl_display_ptr dp = validate_display(dpy);
1959 if (!dp) return EGL_FALSE;
1960
1961 EGLBoolean result = EGL_FALSE;
1962 egl_connection_t* const cnx = &gEGLImpl;
1963 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1964 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1965 dp->disp.dpy, stream);
1966 }
1967 return result;
1968}
1969
1970EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1971 EGLStreamKHR stream)
1972{
1973 clearError();
1974
1975 const egl_display_ptr dp = validate_display(dpy);
1976 if (!dp) return EGL_FALSE;
1977
1978 EGLBoolean result = EGL_FALSE;
1979 egl_connection_t* const cnx = &gEGLImpl;
1980 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1981 result = cnx->egl.eglStreamConsumerAcquireKHR(
1982 dp->disp.dpy, stream);
1983 }
1984 return result;
1985}
1986
1987EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1988 EGLStreamKHR stream)
1989{
1990 clearError();
1991
1992 const egl_display_ptr dp = validate_display(dpy);
1993 if (!dp) return EGL_FALSE;
1994
1995 EGLBoolean result = EGL_FALSE;
1996 egl_connection_t* const cnx = &gEGLImpl;
1997 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1998 result = cnx->egl.eglStreamConsumerReleaseKHR(
1999 dp->disp.dpy, stream);
2000 }
2001 return result;
2002}
2003
2004EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2005 EGLDisplay dpy, EGLStreamKHR stream)
2006{
2007 clearError();
2008
2009 const egl_display_ptr dp = validate_display(dpy);
2010 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2011
2012 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2013 egl_connection_t* const cnx = &gEGLImpl;
2014 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2015 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2016 dp->disp.dpy, stream);
2017 }
2018 return result;
2019}
2020
2021EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2022 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2023{
2024 clearError();
2025
2026 const egl_display_ptr dp = validate_display(dpy);
2027 if (!dp) return EGL_NO_STREAM_KHR;
2028
2029 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2030 egl_connection_t* const cnx = &gEGLImpl;
2031 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2032 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2033 dp->disp.dpy, file_descriptor);
2034 }
2035 return result;
2036}
2037
Mathias Agopian518ec112011-05-13 16:21:08 -07002038// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002039// EGL_EGLEXT_VERSION 15
2040// ----------------------------------------------------------------------------
2041
2042EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2043 clearError();
2044 const egl_display_ptr dp = validate_display(dpy);
2045 if (!dp) return EGL_FALSE;
2046 EGLint result = EGL_FALSE;
2047 egl_connection_t* const cnx = &gEGLImpl;
2048 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2049 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2050 }
2051 return result;
2052}
2053
2054// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002055// ANDROID extensions
2056// ----------------------------------------------------------------------------
2057
Jamie Gennis331841b2012-09-06 14:52:00 -07002058EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2059{
2060 clearError();
2061
2062 const egl_display_ptr dp = validate_display(dpy);
2063 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2064
2065 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2066 egl_connection_t* const cnx = &gEGLImpl;
2067 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2068 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2069 }
2070 return result;
2071}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002072
Andy McFadden72841452013-03-01 16:25:32 -08002073EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2074 EGLnsecsANDROID time)
2075{
2076 clearError();
2077
2078 const egl_display_ptr dp = validate_display(dpy);
2079 if (!dp) {
2080 return EGL_FALSE;
2081 }
2082
2083 SurfaceRef _s(dp.get(), surface);
2084 if (!_s.get()) {
2085 setError(EGL_BAD_SURFACE, EGL_FALSE);
2086 return EGL_FALSE;
2087 }
2088
2089 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002090 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002091
2092 return EGL_TRUE;
2093}
2094
Craig Donner60761072017-01-27 12:30:44 -08002095EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2096 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002097 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2098 // this function cannot be implemented when this libEGL is built for
2099 // vendors.
2100#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002101 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002102 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002103#else
2104 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2105#endif
Craig Donner60761072017-01-27 12:30:44 -08002106}
2107
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002108// ----------------------------------------------------------------------------
2109// NVIDIA extensions
2110// ----------------------------------------------------------------------------
2111EGLuint64NV eglGetSystemTimeFrequencyNV()
2112{
2113 clearError();
2114
2115 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002116 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002117 }
2118
2119 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002120 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002121
Mathias Agopianada798b2012-02-13 17:09:30 -08002122 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2123 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002124 }
2125
Mathias Agopian737b8962017-02-24 14:32:05 -08002126 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002127}
2128
2129EGLuint64NV eglGetSystemTimeNV()
2130{
2131 clearError();
2132
2133 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002134 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002135 }
2136
2137 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002138 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002139
Mathias Agopianada798b2012-02-13 17:09:30 -08002140 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2141 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002142 }
2143
Mathias Agopian737b8962017-02-24 14:32:05 -08002144 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002145}
Dan Stozaa894d082015-02-19 15:27:36 -08002146
2147// ----------------------------------------------------------------------------
2148// Partial update extension
2149// ----------------------------------------------------------------------------
2150EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2151 EGLint *rects, EGLint n_rects)
2152{
2153 clearError();
2154
2155 const egl_display_ptr dp = validate_display(dpy);
2156 if (!dp) {
2157 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2158 return EGL_FALSE;
2159 }
2160
2161 SurfaceRef _s(dp.get(), surface);
2162 if (!_s.get()) {
2163 setError(EGL_BAD_SURFACE, EGL_FALSE);
2164 return EGL_FALSE;
2165 }
2166
2167 egl_surface_t const * const s = get_surface(surface);
2168 if (s->cnx->egl.eglSetDamageRegionKHR) {
2169 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2170 rects, n_rects);
2171 }
2172
2173 return EGL_FALSE;
2174}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002175
Brian Anderson1049d1d2016-12-16 17:25:57 -08002176EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2177 EGLuint64KHR *frameId) {
2178 clearError();
2179
2180 const egl_display_ptr dp = validate_display(dpy);
2181 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002182 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002183 }
2184
2185 SurfaceRef _s(dp.get(), surface);
2186 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002187 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002188 }
2189
2190 egl_surface_t const * const s = get_surface(surface);
2191
Mathias Agopian65421432017-03-08 11:49:05 -08002192 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002193 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002194 }
2195
2196 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002197 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002198
Mathias Agopian65421432017-03-08 11:49:05 -08002199 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002200 // This should not happen. Return an error that is not in the spec
2201 // so it's obvious something is very wrong.
2202 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002203 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002204 }
2205
2206 *frameId = nextFrameId;
2207 return EGL_TRUE;
2208}
2209
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002210EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2211 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2212{
2213 clearError();
2214
2215 const egl_display_ptr dp = validate_display(dpy);
2216 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002217 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002218 }
2219
2220 SurfaceRef _s(dp.get(), surface);
2221 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002222 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002223 }
2224
2225 egl_surface_t const * const s = get_surface(surface);
2226
Mathias Agopian65421432017-03-08 11:49:05 -08002227 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002228 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002229 }
2230
2231 nsecs_t* compositeDeadline = nullptr;
2232 nsecs_t* compositeInterval = nullptr;
2233 nsecs_t* compositeToPresentLatency = nullptr;
2234
2235 for (int i = 0; i < numTimestamps; i++) {
2236 switch (names[i]) {
2237 case EGL_COMPOSITE_DEADLINE_ANDROID:
2238 compositeDeadline = &values[i];
2239 break;
2240 case EGL_COMPOSITE_INTERVAL_ANDROID:
2241 compositeInterval = &values[i];
2242 break;
2243 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2244 compositeToPresentLatency = &values[i];
2245 break;
2246 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002247 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002248 }
2249 }
2250
Mathias Agopian65421432017-03-08 11:49:05 -08002251 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002252 compositeDeadline, compositeInterval, compositeToPresentLatency);
2253
2254 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002255 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002256 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002257 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002258 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002259 default:
2260 // This should not happen. Return an error that is not in the spec
2261 // so it's obvious something is very wrong.
2262 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002263 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002264 }
2265}
2266
2267EGLBoolean eglGetCompositorTimingSupportedANDROID(
2268 EGLDisplay dpy, EGLSurface surface, EGLint name)
2269{
2270 clearError();
2271
2272 const egl_display_ptr dp = validate_display(dpy);
2273 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002274 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002275 }
2276
2277 SurfaceRef _s(dp.get(), surface);
2278 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002279 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002280 }
2281
2282 egl_surface_t const * const s = get_surface(surface);
2283
Mathias Agopian65421432017-03-08 11:49:05 -08002284 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002285 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002286 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002287 }
2288
2289 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002290 case EGL_COMPOSITE_DEADLINE_ANDROID:
2291 case EGL_COMPOSITE_INTERVAL_ANDROID:
2292 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2293 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002294 default:
2295 return EGL_FALSE;
2296 }
2297}
2298
Pablo Ceballosc18be292016-05-31 14:55:42 -07002299EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002300 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002301 EGLnsecsANDROID *values)
2302{
2303 clearError();
2304
2305 const egl_display_ptr dp = validate_display(dpy);
2306 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002307 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002308 }
2309
2310 SurfaceRef _s(dp.get(), surface);
2311 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002312 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002313 }
2314
2315 egl_surface_t const * const s = get_surface(surface);
2316
Mathias Agopian65421432017-03-08 11:49:05 -08002317 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002318 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002319 }
2320
Brian Andersondbd0ea82016-07-22 09:38:59 -07002321 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002322 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002323 nsecs_t* latchTime = nullptr;
2324 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002325 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002326 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002327 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002328 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002329 nsecs_t* releaseTime = nullptr;
2330
2331 for (int i = 0; i < numTimestamps; i++) {
2332 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002333 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2334 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002335 break;
2336 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2337 acquireTime = &values[i];
2338 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002339 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2340 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002341 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002342 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2343 firstRefreshStartTime = &values[i];
2344 break;
2345 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2346 lastRefreshStartTime = &values[i];
2347 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002348 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2349 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002350 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002351 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2352 displayPresentTime = &values[i];
2353 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002354 case EGL_DEQUEUE_READY_TIME_ANDROID:
2355 dequeueReadyTime = &values[i];
2356 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002357 case EGL_READS_DONE_TIME_ANDROID:
2358 releaseTime = &values[i];
2359 break;
2360 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002361 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002362 }
2363 }
2364
Mathias Agopian65421432017-03-08 11:49:05 -08002365 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002366 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002367 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002368 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002369
Brian Anderson069b3652016-07-22 10:32:47 -07002370 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002371 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002372 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002373 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002374 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002375 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002376 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002377 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002378 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2379 default:
2380 // This should not happen. Return an error that is not in the spec
2381 // so it's obvious something is very wrong.
2382 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2383 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002384 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002385}
2386
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002387EGLBoolean eglGetFrameTimestampSupportedANDROID(
2388 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002389{
2390 clearError();
2391
2392 const egl_display_ptr dp = validate_display(dpy);
2393 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002394 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002395 }
2396
2397 SurfaceRef _s(dp.get(), surface);
2398 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002399 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002400 }
2401
2402 egl_surface_t const * const s = get_surface(surface);
2403
Mathias Agopian65421432017-03-08 11:49:05 -08002404 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002405 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002406 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002407 }
2408
2409 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002410 case EGL_COMPOSITE_DEADLINE_ANDROID:
2411 case EGL_COMPOSITE_INTERVAL_ANDROID:
2412 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002413 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002414 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002415 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2416 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2417 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002418 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002419 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002420 case EGL_READS_DONE_TIME_ANDROID:
2421 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002422 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2423 int value = 0;
2424 window->query(window,
2425 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2426 return value == 0 ? EGL_FALSE : EGL_TRUE;
2427 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002428 default:
2429 return EGL_FALSE;
2430 }
2431}