blob: 3312b031a0895c9918e8ce4534d931114a80ccee [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ński12752442018-05-08 15:25:31 -0700478// Get the colorspace value that should be reported from queries. When the colorspace
479// is unknown (no attribute passed), default to reporting LINEAR.
480static EGLint getReportedColorSpace(EGLint colorspace) {
481 return colorspace == EGL_UNKNOWN ? EGL_GL_COLORSPACE_LINEAR_KHR : colorspace;
482}
483
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700484// Returns a list of color spaces understood by the vendor EGL driver.
485static std::vector<EGLint> getDriverColorSpaces(egl_display_ptr dp,
486 android_pixel_format format) {
487 std::vector<EGLint> colorSpaces;
488 if (!dp->hasColorSpaceSupport) return colorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700489
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700490 // OpenGL drivers only support sRGB encoding with 8-bit formats.
491 // RGB_888 is never returned by getNativePixelFormat, but is included for completeness.
492 const bool formatSupportsSRGBEncoding =
493 format == HAL_PIXEL_FORMAT_RGBA_8888 || format == HAL_PIXEL_FORMAT_RGBX_8888 ||
494 format == HAL_PIXEL_FORMAT_RGB_888;
495 const bool formatIsFloatingPoint = format == HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700496
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700497 if (formatSupportsSRGBEncoding) {
498 // sRGB and linear are always supported when color space support is present.
499 colorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
500 colorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
501 // DCI-P3 uses the sRGB transfer function, so it's only relevant for 8-bit formats.
502 if (findExtension(dp->disp.queryString.extensions,
503 "EGL_EXT_gl_colorspace_display_p3")) {
504 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600505 }
506 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700507
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700508 // According to the spec, scRGB is only supported for floating point formats.
509 // For non-linear scRGB, the application is responsible for applying the
510 // transfer function.
511 if (formatIsFloatingPoint) {
512 if (findExtension(dp->disp.queryString.extensions,
513 "EGL_EXT_gl_colorspace_scrgb")) {
514 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_EXT);
515 }
516 if (findExtension(dp->disp.queryString.extensions,
517 "EGL_EXT_gl_colorspace_scrgb_linear")) {
518 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
519 }
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600520 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700521
522 // BT2020 can be used with any pixel format. PQ encoding must be applied by the
523 // application and does not affect the behavior of OpenGL.
524 if (findExtension(dp->disp.queryString.extensions,
525 "EGL_EXT_gl_colorspace_bt2020_linear")) {
526 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
527 }
528 if (findExtension(dp->disp.queryString.extensions,
529 "EGL_EXT_gl_colorspace_bt2020_pq")) {
530 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
531 }
532
533 // Linear DCI-P3 simply uses different primaries than standard RGB and thus
534 // can be used with any pixel format.
535 if (findExtension(dp->disp.queryString.extensions,
536 "EGL_EXT_gl_colorspace_display_p3_linear")) {
537 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
538 }
539 return colorSpaces;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600540}
541
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700542// Cleans up color space related parameters that the driver does not understand.
543// If there is no color space attribute in attrib_list, colorSpace is left
544// unmodified.
545static EGLBoolean processAttributes(egl_display_ptr dp, NativeWindowType window,
546 android_pixel_format format, const EGLint* attrib_list,
547 EGLint* colorSpace,
548 std::vector<EGLint>* strippedAttribList) {
549 for (const EGLint* attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
550 bool copyAttribute = true;
551 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
552 // Fail immediately if the driver doesn't have color space support at all.
553 if (!dp->hasColorSpaceSupport) return false;
554 *colorSpace = attr[1];
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600555
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700556 // Strip the attribute if the driver doesn't understand it.
557 copyAttribute = false;
558 std::vector<EGLint> driverColorSpaces = getDriverColorSpaces(dp, format);
559 for (auto driverColorSpace : driverColorSpaces) {
560 if (attr[1] == driverColorSpace) {
561 copyAttribute = true;
562 break;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600563 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600564 }
565 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700566 if (copyAttribute) {
567 strippedAttribList->push_back(attr[0]);
568 strippedAttribList->push_back(attr[1]);
569 }
570 }
571 // Terminate the attribute list.
572 strippedAttribList->push_back(EGL_NONE);
573
574 // If the passed color space has wide color gamut, check whether the target native window
575 // supports wide color.
576 const bool colorSpaceIsNarrow =
577 *colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700578 *colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR ||
579 *colorSpace == EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700580 if (window && !colorSpaceIsNarrow) {
581 bool windowSupportsWideColor = true;
582 // Ordinarily we'd put a call to native_window_get_wide_color_support
583 // at the beginning of the function so that we'll have the
584 // result when needed elsewhere in the function.
585 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
586 // SurfaceFlinger is required to answer the call below we would
587 // end up in a deadlock situation. By moving the call to only happen
588 // if the application has specifically asked for wide-color we avoid
589 // the deadlock with SurfaceFlinger since it will not ask for a
590 // wide-color surface.
591 int err = native_window_get_wide_color_support(window, &windowSupportsWideColor);
592
593 if (err) {
594 ALOGE("processAttributes: invalid window (win=%p) "
595 "failed (%#x) (already connected to another API?)",
596 window, err);
597 return false;
598 }
599 if (!windowSupportsWideColor) {
600 // Application has asked for a wide-color colorspace but
601 // wide-color support isn't available on the display the window is on.
602 return false;
603 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600604 }
605 return true;
606}
607
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700608// Gets the native pixel format corrsponding to the passed EGLConfig.
609void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config,
610 android_pixel_format* format) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600611 // Set the native window's buffers format to match what this config requests.
612 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
613 // of our native format. So if sRGB gamma is requested, we have to
614 // modify the EGLconfig's format before setting the native window's
615 // format.
616
617 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
618 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
619
620 EGLint a = 0;
621 EGLint r, g, b;
622 r = g = b = 0;
623 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
624 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
625 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
626 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
627 EGLint colorDepth = r + g + b;
628
629 // Today, the driver only understands sRGB and linear on 888X
630 // formats. Strip other colorspaces from the attribute list and
631 // only use them to set the dataspace via
632 // native_window_set_buffers_dataspace
633 // if pixel format is RGBX 8888
634 // TBD: Can test for future extensions that indicate that driver
635 // handles requested color space and we can let it through.
636 // allow SRGB and LINEAR. All others need to be stripped.
637 // else if 565, 4444
638 // TBD: Can we assume these are supported if 8888 is?
639 // else if FP16 or 1010102
640 // strip colorspace from attribs.
641 // endif
642 if (a == 0) {
643 if (colorDepth <= 16) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700644 *format = HAL_PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600645 } else {
646 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
647 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700648 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600649 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700650 *format = HAL_PIXEL_FORMAT_RGBX_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600651 }
652 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700653 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600654 }
655 }
656 } else {
657 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
658 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700659 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600660 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700661 *format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600662 }
663 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700664 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600665 }
666 }
667}
668
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700669EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
670 android_smpte2086_metadata smpteMetadata;
671 if (s->getSmpte2086Metadata(smpteMetadata)) {
672 int err =
673 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
674 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700675 if (err != 0) {
676 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
677 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700678 return EGL_FALSE;
679 }
680 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700681 android_cta861_3_metadata cta8613Metadata;
682 if (s->getCta8613Metadata(cta8613Metadata)) {
683 int err =
684 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
685 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700686 if (err != 0) {
687 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
688 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700689 return EGL_FALSE;
690 }
691 }
692 return EGL_TRUE;
693}
694
Mathias Agopian518ec112011-05-13 16:21:08 -0700695EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
696 NativeWindowType window,
697 const EGLint *attrib_list)
698{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700699 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700700 clearError();
701
Jesse Hallb29e5e82012-04-04 16:53:42 -0700702 egl_connection_t* cnx = NULL;
703 egl_display_ptr dp = validate_display_connection(dpy, cnx);
704 if (dp) {
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800705 if (!window) {
706 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
707 }
708
709 int value = 0;
710 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
711 if (!value) {
712 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
713 }
714
Andy McFaddend566ce32014-01-07 15:54:17 -0800715 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800716 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800717 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
718 "failed (%#x) (already connected to another API?)",
719 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700720 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700721 }
722
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700723 EGLDisplay iDpy = dp->disp.dpy;
724 android_pixel_format format;
725 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600726
727 // now select correct colorspace and dataspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700728 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700729 std::vector<EGLint> strippedAttribList;
730 if (!processAttributes(dp, window, format, attrib_list, &colorSpace,
731 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600732 ALOGE("error invalid colorspace: %d", colorSpace);
733 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700734 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700735 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800736
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700737 {
Jesse Hallc2e41222013-08-08 13:40:22 -0700738 int err = native_window_set_buffers_format(window, format);
739 if (err != 0) {
740 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700741 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700742 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
743 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
744 }
745 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700746
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700747 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
748 if (dataSpace != HAL_DATASPACE_UNKNOWN) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800749 int err = native_window_set_buffers_data_space(window, dataSpace);
750 if (err != 0) {
751 ALOGE("error setting native window pixel dataSpace: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700752 strerror(-err), err);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800753 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
754 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
755 }
756 }
757
Jamie Gennis59769462011-11-19 18:04:43 -0800758 // the EGL spec requires that a new EGLSurface default to swap interval
759 // 1, so explicitly set that on the window here.
760 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
761 anw->setSwapInterval(anw, 1);
762
Mathias Agopian518ec112011-05-13 16:21:08 -0700763 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800764 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700765 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600766 egl_surface_t* s =
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700767 new egl_surface_t(dp.get(), config, window, surface,
768 getReportedColorSpace(colorSpace), cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700769 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700770 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700771
772 // EGLSurface creation failed
773 native_window_set_buffers_format(window, 0);
774 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700775 }
776 return EGL_NO_SURFACE;
777}
778
779EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
780 NativePixmapType pixmap,
781 const EGLint *attrib_list)
782{
783 clearError();
784
Jesse Hallb29e5e82012-04-04 16:53:42 -0700785 egl_connection_t* cnx = NULL;
786 egl_display_ptr dp = validate_display_connection(dpy, cnx);
787 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700788 EGLDisplay iDpy = dp->disp.dpy;
789 android_pixel_format format;
790 getNativePixelFormat(iDpy, cnx, config, &format);
791
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600792 // now select a corresponding sRGB format if needed
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700793 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700794 std::vector<EGLint> strippedAttribList;
795 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
796 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600797 ALOGE("error invalid colorspace: %d", colorSpace);
798 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
799 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700800 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600801
Mathias Agopian518ec112011-05-13 16:21:08 -0700802 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800803 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700804 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700805 egl_surface_t* s =
806 new egl_surface_t(dp.get(), config, NULL, surface,
807 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700808 return s;
809 }
810 }
811 return EGL_NO_SURFACE;
812}
813
814EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
815 const EGLint *attrib_list)
816{
817 clearError();
818
Jesse Hallb29e5e82012-04-04 16:53:42 -0700819 egl_connection_t* cnx = NULL;
820 egl_display_ptr dp = validate_display_connection(dpy, cnx);
821 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600822 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700823 android_pixel_format format;
824 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600825
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700826 // Select correct colorspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700827 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700828 std::vector<EGLint> strippedAttribList;
829 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
830 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600831 ALOGE("error invalid colorspace: %d", colorSpace);
832 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
833 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700834 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600835
Mathias Agopian518ec112011-05-13 16:21:08 -0700836 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800837 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700838 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700839 egl_surface_t* s =
840 new egl_surface_t(dp.get(), config, NULL, surface,
841 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700842 return s;
843 }
844 }
845 return EGL_NO_SURFACE;
846}
Jesse Hall47743382013-02-08 11:13:46 -0800847
Mathias Agopian518ec112011-05-13 16:21:08 -0700848EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
849{
850 clearError();
851
Jesse Hallb29e5e82012-04-04 16:53:42 -0700852 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 if (!dp) return EGL_FALSE;
854
Jesse Hallb29e5e82012-04-04 16:53:42 -0700855 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700856 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800857 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700858
859 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800860 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700861 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700862 _s.terminate();
863 }
864 return result;
865}
866
867EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
868 EGLint attribute, EGLint *value)
869{
870 clearError();
871
Jesse Hallb29e5e82012-04-04 16:53:42 -0700872 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700873 if (!dp) return EGL_FALSE;
874
Jesse Hallb29e5e82012-04-04 16:53:42 -0700875 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700876 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800877 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700878
Mathias Agopian518ec112011-05-13 16:21:08 -0700879 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700880 if (s->getColorSpaceAttribute(attribute, value)) {
881 return EGL_TRUE;
882 } else if (s->getSmpte2086Attribute(attribute, value)) {
883 return EGL_TRUE;
884 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600885 return EGL_TRUE;
886 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700887 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700888}
889
Jamie Gennise8696a42012-01-15 18:54:57 -0800890void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800891 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800892 clearError();
893
Jesse Hallb29e5e82012-04-04 16:53:42 -0700894 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800895 if (!dp) {
896 return;
897 }
898
Jesse Hallb29e5e82012-04-04 16:53:42 -0700899 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800900 if (!_s.get()) {
901 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800902 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800903}
904
Mathias Agopian518ec112011-05-13 16:21:08 -0700905// ----------------------------------------------------------------------------
906// Contexts
907// ----------------------------------------------------------------------------
908
909EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
910 EGLContext share_list, const EGLint *attrib_list)
911{
912 clearError();
913
Jesse Hallb29e5e82012-04-04 16:53:42 -0700914 egl_connection_t* cnx = NULL;
915 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700916 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700917 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700918 if (!ContextRef(dp.get(), share_list).get()) {
919 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
920 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700921 egl_context_t* const c = get_context(share_list);
922 share_list = c->context;
923 }
924 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800925 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700926 if (context != EGL_NO_CONTEXT) {
927 // figure out if it's a GLESv1 or GLESv2
928 int version = 0;
929 if (attrib_list) {
930 while (*attrib_list != EGL_NONE) {
931 GLint attr = *attrib_list++;
932 GLint value = *attrib_list++;
933 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
934 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800935 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800936 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800937 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700938 }
939 }
940 };
941 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700942 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
943 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700944 return c;
945 }
946 }
947 return EGL_NO_CONTEXT;
948}
949
950EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
951{
952 clearError();
953
Jesse Hallb29e5e82012-04-04 16:53:42 -0700954 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700955 if (!dp)
956 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700957
Jesse Hallb29e5e82012-04-04 16:53:42 -0700958 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700959 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800960 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800961
Mathias Agopian518ec112011-05-13 16:21:08 -0700962 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800963 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700964 if (result == EGL_TRUE) {
965 _c.terminate();
966 }
967 return result;
968}
969
Mathias Agopian518ec112011-05-13 16:21:08 -0700970EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
971 EGLSurface read, EGLContext ctx)
972{
973 clearError();
974
Jesse Hallb29e5e82012-04-04 16:53:42 -0700975 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800976 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700977
Mathias Agopian5b287a62011-05-16 18:58:55 -0700978 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
979 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
980 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700981 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
982 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800983 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700984 }
985
986 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700987 ContextRef _c(dp.get(), ctx);
988 SurfaceRef _d(dp.get(), draw);
989 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700990
991 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700992 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700993 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800994 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700995 }
996
997 // these are the underlying implementation's object
998 EGLContext impl_ctx = EGL_NO_CONTEXT;
999 EGLSurface impl_draw = EGL_NO_SURFACE;
1000 EGLSurface impl_read = EGL_NO_SURFACE;
1001
1002 // these are our objects structs passed in
1003 egl_context_t * c = NULL;
1004 egl_surface_t const * d = NULL;
1005 egl_surface_t const * r = NULL;
1006
1007 // these are the current objects structs
1008 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001009
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 if (ctx != EGL_NO_CONTEXT) {
1011 c = get_context(ctx);
1012 impl_ctx = c->context;
1013 } else {
1014 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001015 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1016 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001017 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001018 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 if (cur_c == NULL) {
1020 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001021 // not an error, there is just no current context.
1022 return EGL_TRUE;
1023 }
1024 }
1025
1026 // retrieve the underlying implementation's draw EGLSurface
1027 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001028 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001029 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001030 impl_draw = d->surface;
1031 }
1032
1033 // retrieve the underlying implementation's read EGLSurface
1034 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001035 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001036 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001037 impl_read = r->surface;
1038 }
1039
Mathias Agopian518ec112011-05-13 16:21:08 -07001040
Jesse Hallb29e5e82012-04-04 16:53:42 -07001041 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001042 draw, read, ctx,
1043 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001044
1045 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001046 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001047 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1048 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001049 _c.acquire();
1050 _r.acquire();
1051 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001052 } else {
1053 setGLHooksThreadSpecific(&gHooksNoContext);
1054 egl_tls_t::setContext(EGL_NO_CONTEXT);
1055 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001056 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001057 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001058 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001059 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001060 }
1061 return result;
1062}
1063
1064
1065EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1066 EGLint attribute, EGLint *value)
1067{
1068 clearError();
1069
Jesse Hallb29e5e82012-04-04 16:53:42 -07001070 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001071 if (!dp) return EGL_FALSE;
1072
Jesse Hallb29e5e82012-04-04 16:53:42 -07001073 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001074 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001075
Mathias Agopian518ec112011-05-13 16:21:08 -07001076 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001077 return c->cnx->egl.eglQueryContext(
1078 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001079
Mathias Agopian518ec112011-05-13 16:21:08 -07001080}
1081
1082EGLContext eglGetCurrentContext(void)
1083{
1084 // could be called before eglInitialize(), but we wouldn't have a context
1085 // then, and this function would correctly return EGL_NO_CONTEXT.
1086
1087 clearError();
1088
1089 EGLContext ctx = getContext();
1090 return ctx;
1091}
1092
1093EGLSurface eglGetCurrentSurface(EGLint readdraw)
1094{
1095 // could be called before eglInitialize(), but we wouldn't have a context
1096 // then, and this function would correctly return EGL_NO_SURFACE.
1097
1098 clearError();
1099
1100 EGLContext ctx = getContext();
1101 if (ctx) {
1102 egl_context_t const * const c = get_context(ctx);
1103 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1104 switch (readdraw) {
1105 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001106 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001107 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1108 }
1109 }
1110 return EGL_NO_SURFACE;
1111}
1112
1113EGLDisplay eglGetCurrentDisplay(void)
1114{
1115 // could be called before eglInitialize(), but we wouldn't have a context
1116 // then, and this function would correctly return EGL_NO_DISPLAY.
1117
1118 clearError();
1119
1120 EGLContext ctx = getContext();
1121 if (ctx) {
1122 egl_context_t const * const c = get_context(ctx);
1123 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1124 return c->dpy;
1125 }
1126 return EGL_NO_DISPLAY;
1127}
1128
1129EGLBoolean eglWaitGL(void)
1130{
Mathias Agopian518ec112011-05-13 16:21:08 -07001131 clearError();
1132
Mathias Agopianada798b2012-02-13 17:09:30 -08001133 egl_connection_t* const cnx = &gEGLImpl;
1134 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001135 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001136
1137 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001138}
1139
1140EGLBoolean eglWaitNative(EGLint engine)
1141{
Mathias Agopian518ec112011-05-13 16:21:08 -07001142 clearError();
1143
Mathias Agopianada798b2012-02-13 17:09:30 -08001144 egl_connection_t* const cnx = &gEGLImpl;
1145 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001146 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001147
1148 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001149}
1150
1151EGLint eglGetError(void)
1152{
Mathias Agopianada798b2012-02-13 17:09:30 -08001153 EGLint err = EGL_SUCCESS;
1154 egl_connection_t* const cnx = &gEGLImpl;
1155 if (cnx->dso) {
1156 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001157 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001158 if (err == EGL_SUCCESS) {
1159 err = egl_tls_t::getError();
1160 }
1161 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001162}
1163
Michael Chockc0ec5e22014-01-27 08:14:33 -08001164static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001165 const char* procname) {
1166 const egl_connection_t* cnx = &gEGLImpl;
1167 void* proc = NULL;
1168
Michael Chockc0ec5e22014-01-27 08:14:33 -08001169 proc = dlsym(cnx->libEgl, procname);
1170 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1171
Jesse Hallc07b5202013-07-04 12:08:16 -07001172 proc = dlsym(cnx->libGles2, procname);
1173 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1174
1175 proc = dlsym(cnx->libGles1, procname);
1176 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1177
1178 return NULL;
1179}
1180
Mathias Agopian518ec112011-05-13 16:21:08 -07001181__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1182{
1183 // eglGetProcAddress() could be the very first function called
1184 // in which case we must make sure we've initialized ourselves, this
1185 // happens the first time egl_get_display() is called.
1186
1187 clearError();
1188
1189 if (egl_init_drivers() == EGL_FALSE) {
1190 setError(EGL_BAD_PARAMETER, NULL);
1191 return NULL;
1192 }
1193
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001194 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001195 return NULL;
1196 }
1197
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001199 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001200 if (addr) return addr;
1201
Michael Chockc0ec5e22014-01-27 08:14:33 -08001202 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001203 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001204
Mathias Agopian518ec112011-05-13 16:21:08 -07001205 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1206 pthread_mutex_lock(&sExtensionMapMutex);
1207
1208 /*
1209 * Since eglGetProcAddress() is not associated to anything, it needs
1210 * to return a function pointer that "works" regardless of what
1211 * the current context is.
1212 *
1213 * For this reason, we return a "forwarder", a small stub that takes
1214 * care of calling the function associated with the context
1215 * currently bound.
1216 *
1217 * We first look for extensions we've already resolved, if we're seeing
1218 * this extension for the first time, we go through all our
1219 * implementations and call eglGetProcAddress() and record the
1220 * result in the appropriate implementation hooks and return the
1221 * address of the forwarder corresponding to that hook set.
1222 *
1223 */
1224
Mathias Agopian65421432017-03-08 11:49:05 -08001225 const std::string name(procname);
1226
1227 auto& extentionMap = sGLExtentionMap;
1228 auto pos = extentionMap.find(name);
1229 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001230 const int slot = sGLExtentionSlot;
1231
Steve Blocke6f43dd2012-01-06 19:20:56 +00001232 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001233 "no more slots for eglGetProcAddress(\"%s\")",
1234 procname);
1235
1236 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1237 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001238
1239 egl_connection_t* const cnx = &gEGLImpl;
1240 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001241 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001242 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001243 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1244 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001245 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001246 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001247 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001248
Mathias Agopian518ec112011-05-13 16:21:08 -07001249 if (found) {
1250 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001251 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001252 sGLExtentionSlot++;
1253 }
1254 }
1255
1256 pthread_mutex_unlock(&sExtensionMapMutex);
1257 return addr;
1258}
1259
Mathias Agopian65421432017-03-08 11:49:05 -08001260class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001261public:
1262
1263 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001264 static FrameCompletionThread thread;
1265
1266 char name[64];
1267
1268 std::lock_guard<std::mutex> lock(thread.mMutex);
1269 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1270 ATRACE_NAME(name);
1271
1272 thread.mQueue.push_back(sync);
1273 thread.mCondition.notify_one();
1274 thread.mFramesQueued++;
1275 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001276 }
1277
1278private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001279
Mathias Agopian65421432017-03-08 11:49:05 -08001280 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1281 std::thread thread(&FrameCompletionThread::loop, this);
1282 thread.detach();
1283 }
1284
1285#pragma clang diagnostic push
1286#pragma clang diagnostic ignored "-Wmissing-noreturn"
1287 void loop() {
1288 while (true) {
1289 threadLoop();
1290 }
1291 }
1292#pragma clang diagnostic pop
1293
1294 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001295 EGLSyncKHR sync;
1296 uint32_t frameNum;
1297 {
Mathias Agopian65421432017-03-08 11:49:05 -08001298 std::unique_lock<std::mutex> lock(mMutex);
1299 while (mQueue.empty()) {
1300 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001301 }
1302 sync = mQueue[0];
1303 frameNum = mFramesCompleted;
1304 }
1305 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1306 {
Mathias Agopian65421432017-03-08 11:49:05 -08001307 char name[64];
1308 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1309 ATRACE_NAME(name);
1310
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001311 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1312 if (result == EGL_FALSE) {
1313 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1314 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1315 ALOGE("FrameCompletion: timeout waiting for fence");
1316 }
1317 eglDestroySyncKHR(dpy, sync);
1318 }
1319 {
Mathias Agopian65421432017-03-08 11:49:05 -08001320 std::lock_guard<std::mutex> lock(mMutex);
1321 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001322 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001323 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001324 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001325 }
1326
1327 uint32_t mFramesQueued;
1328 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001329 std::deque<EGLSyncKHR> mQueue;
1330 std::condition_variable mCondition;
1331 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001332};
1333
Dan Stozaa894d082015-02-19 15:27:36 -08001334EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1335 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001336{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001337 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001338 clearError();
1339
Jesse Hallb29e5e82012-04-04 16:53:42 -07001340 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001341 if (!dp) return EGL_FALSE;
1342
Jesse Hallb29e5e82012-04-04 16:53:42 -07001343 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001344 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001345 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001346
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001347 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001348
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001349 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1350 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1351 if (sync != EGL_NO_SYNC_KHR) {
1352 FrameCompletionThread::queueSync(sync);
1353 }
1354 }
1355
Mathias Agopian7db993a2012-03-25 00:49:46 -07001356 if (CC_UNLIKELY(dp->finishOnSwap)) {
1357 uint32_t pixel;
1358 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1359 if (c) {
1360 // glReadPixels() ensures that the frame is complete
1361 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1362 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1363 }
1364 }
1365
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001366 if (!sendSurfaceMetadata(s)) {
1367 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1368 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1369 }
1370
Dan Stozaa894d082015-02-19 15:27:36 -08001371 if (n_rects == 0) {
1372 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1373 }
1374
Mathias Agopian65421432017-03-08 11:49:05 -08001375 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001376 for (int r = 0; r < n_rects; ++r) {
1377 int offset = r * 4;
1378 int x = rects[offset];
1379 int y = rects[offset + 1];
1380 int width = rects[offset + 2];
1381 int height = rects[offset + 3];
1382 android_native_rect_t androidRect;
1383 androidRect.left = x;
1384 androidRect.top = y + height;
1385 androidRect.right = x + width;
1386 androidRect.bottom = y;
1387 androidRects.push_back(androidRect);
1388 }
Mathias Agopian65421432017-03-08 11:49:05 -08001389 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001390
1391 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1392 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1393 rects, n_rects);
1394 } else {
1395 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1396 }
1397}
1398
1399EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1400{
1401 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001402}
1403
1404EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1405 NativePixmapType target)
1406{
1407 clearError();
1408
Jesse Hallb29e5e82012-04-04 16:53:42 -07001409 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001410 if (!dp) return EGL_FALSE;
1411
Jesse Hallb29e5e82012-04-04 16:53:42 -07001412 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001413 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001414 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001415
Mathias Agopian518ec112011-05-13 16:21:08 -07001416 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001417 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001418}
1419
1420const char* eglQueryString(EGLDisplay dpy, EGLint name)
1421{
1422 clearError();
1423
Chia-I Wue57d1352016-08-15 16:10:02 +08001424 // Generate an error quietly when client extensions (as defined by
1425 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1426 // validate_display to generate the error as validate_display would log
1427 // the error, which can be misleading.
1428 //
1429 // If we want to support EGL_EXT_client_extensions later, we can return
1430 // the client extension string here instead.
1431 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001432 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001433
Jesse Hallb29e5e82012-04-04 16:53:42 -07001434 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001435 if (!dp) return (const char *) NULL;
1436
1437 switch (name) {
1438 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001439 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001440 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001441 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001442 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001443 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001444 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001445 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001446 default:
1447 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001448 }
1449 return setError(EGL_BAD_PARAMETER, (const char *)0);
1450}
1451
Jiyong Park00b15b82017-08-10 20:30:56 +09001452extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001453{
1454 clearError();
1455
1456 const egl_display_ptr dp = validate_display(dpy);
1457 if (!dp) return (const char *) NULL;
1458
1459 switch (name) {
1460 case EGL_VENDOR:
1461 return dp->disp.queryString.vendor;
1462 case EGL_VERSION:
1463 return dp->disp.queryString.version;
1464 case EGL_EXTENSIONS:
1465 return dp->disp.queryString.extensions;
1466 case EGL_CLIENT_APIS:
1467 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001468 default:
1469 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001470 }
1471 return setError(EGL_BAD_PARAMETER, (const char *)0);
1472}
Mathias Agopian518ec112011-05-13 16:21:08 -07001473
1474// ----------------------------------------------------------------------------
1475// EGL 1.1
1476// ----------------------------------------------------------------------------
1477
1478EGLBoolean eglSurfaceAttrib(
1479 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1480{
1481 clearError();
1482
Jesse Hallb29e5e82012-04-04 16:53:42 -07001483 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001484 if (!dp) return EGL_FALSE;
1485
Jesse Hallb29e5e82012-04-04 16:53:42 -07001486 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001487 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001488 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001489
Pablo Ceballosc18be292016-05-31 14:55:42 -07001490 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001491
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001492 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001493 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001494 setError(EGL_BAD_SURFACE, EGL_FALSE);
1495 }
Mathias Agopian65421432017-03-08 11:49:05 -08001496 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1497 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001498 }
1499
Pablo Ceballosc18be292016-05-31 14:55:42 -07001500 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001501 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001502 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001503 }
Mathias Agopian65421432017-03-08 11:49:05 -08001504 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1505 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001506 }
1507
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001508 if (s->setSmpte2086Attribute(attribute, value)) {
1509 return EGL_TRUE;
1510 } else if (s->setCta8613Attribute(attribute, value)) {
1511 return EGL_TRUE;
1512 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001513 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001514 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001515 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001516 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001517}
1518
1519EGLBoolean eglBindTexImage(
1520 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1521{
1522 clearError();
1523
Jesse Hallb29e5e82012-04-04 16:53:42 -07001524 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001525 if (!dp) return EGL_FALSE;
1526
Jesse Hallb29e5e82012-04-04 16:53:42 -07001527 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001528 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001529 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001530
Mathias Agopian518ec112011-05-13 16:21:08 -07001531 egl_surface_t const * const s = get_surface(surface);
1532 if (s->cnx->egl.eglBindTexImage) {
1533 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001534 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001535 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001536 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001537}
1538
1539EGLBoolean eglReleaseTexImage(
1540 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1541{
1542 clearError();
1543
Jesse Hallb29e5e82012-04-04 16:53:42 -07001544 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001545 if (!dp) return EGL_FALSE;
1546
Jesse Hallb29e5e82012-04-04 16:53:42 -07001547 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001548 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001549 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001550
Mathias Agopian518ec112011-05-13 16:21:08 -07001551 egl_surface_t const * const s = get_surface(surface);
1552 if (s->cnx->egl.eglReleaseTexImage) {
1553 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001554 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001555 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001556 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001557}
1558
1559EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1560{
1561 clearError();
1562
Jesse Hallb29e5e82012-04-04 16:53:42 -07001563 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001564 if (!dp) return EGL_FALSE;
1565
1566 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001567 egl_connection_t* const cnx = &gEGLImpl;
1568 if (cnx->dso && cnx->egl.eglSwapInterval) {
1569 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001570 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001571
Mathias Agopian518ec112011-05-13 16:21:08 -07001572 return res;
1573}
1574
1575
1576// ----------------------------------------------------------------------------
1577// EGL 1.2
1578// ----------------------------------------------------------------------------
1579
1580EGLBoolean eglWaitClient(void)
1581{
1582 clearError();
1583
Mathias Agopianada798b2012-02-13 17:09:30 -08001584 egl_connection_t* const cnx = &gEGLImpl;
1585 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001586 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001587
1588 EGLBoolean res;
1589 if (cnx->egl.eglWaitClient) {
1590 res = cnx->egl.eglWaitClient();
1591 } else {
1592 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001593 }
1594 return res;
1595}
1596
1597EGLBoolean eglBindAPI(EGLenum api)
1598{
1599 clearError();
1600
1601 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001602 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001603 }
1604
1605 // bind this API on all EGLs
1606 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001607 egl_connection_t* const cnx = &gEGLImpl;
1608 if (cnx->dso && cnx->egl.eglBindAPI) {
1609 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001610 }
1611 return res;
1612}
1613
1614EGLenum eglQueryAPI(void)
1615{
1616 clearError();
1617
1618 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001619 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001620 }
1621
Mathias Agopianada798b2012-02-13 17:09:30 -08001622 egl_connection_t* const cnx = &gEGLImpl;
1623 if (cnx->dso && cnx->egl.eglQueryAPI) {
1624 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001625 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001626
Mathias Agopian518ec112011-05-13 16:21:08 -07001627 // or, it can only be OpenGL ES
1628 return EGL_OPENGL_ES_API;
1629}
1630
1631EGLBoolean eglReleaseThread(void)
1632{
1633 clearError();
1634
Mathias Agopianada798b2012-02-13 17:09:30 -08001635 egl_connection_t* const cnx = &gEGLImpl;
1636 if (cnx->dso && cnx->egl.eglReleaseThread) {
1637 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001638 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301639
1640 // If there is context bound to the thread, release it
1641 egl_display_t::loseCurrent(get_context(getContext()));
1642
Mathias Agopian518ec112011-05-13 16:21:08 -07001643 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001644 return EGL_TRUE;
1645}
1646
1647EGLSurface eglCreatePbufferFromClientBuffer(
1648 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1649 EGLConfig config, const EGLint *attrib_list)
1650{
1651 clearError();
1652
Jesse Hallb29e5e82012-04-04 16:53:42 -07001653 egl_connection_t* cnx = NULL;
1654 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1655 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001656 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1657 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001658 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001659 }
1660 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1661}
1662
1663// ----------------------------------------------------------------------------
1664// EGL_EGLEXT_VERSION 3
1665// ----------------------------------------------------------------------------
1666
1667EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1668 const EGLint *attrib_list)
1669{
1670 clearError();
1671
Jesse Hallb29e5e82012-04-04 16:53:42 -07001672 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001673 if (!dp) return EGL_FALSE;
1674
Jesse Hallb29e5e82012-04-04 16:53:42 -07001675 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001676 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001677 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001678
1679 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001680 if (s->cnx->egl.eglLockSurfaceKHR) {
1681 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001682 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001683 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001684 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001685}
1686
1687EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1688{
1689 clearError();
1690
Jesse Hallb29e5e82012-04-04 16:53:42 -07001691 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001692 if (!dp) return EGL_FALSE;
1693
Jesse Hallb29e5e82012-04-04 16:53:42 -07001694 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001695 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001696 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001697
1698 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001699 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001700 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001701 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001702 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001703}
1704
1705EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1706 EGLClientBuffer buffer, const EGLint *attrib_list)
1707{
1708 clearError();
1709
Jesse Hallb29e5e82012-04-04 16:53:42 -07001710 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001711 if (!dp) return EGL_NO_IMAGE_KHR;
1712
Jesse Hallb29e5e82012-04-04 16:53:42 -07001713 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001714 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001715
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001716 // Temporary hack: eglImageCreateKHR should accept EGL_GL_COLORSPACE_LINEAR_KHR,
1717 // EGL_GL_COLORSPACE_SRGB_KHR and EGL_GL_COLORSPACE_DEFAULT_EXT if
1718 // EGL_EXT_image_gl_colorspace is supported, but some drivers don't like
1719 // the DEFAULT value and generate an error.
1720 std::vector<EGLint> strippedAttribList;
1721 for (const EGLint *attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
1722 if (attr[0] == EGL_GL_COLORSPACE_KHR &&
1723 dp->haveExtension("EGL_EXT_image_gl_colorspace")) {
1724 if (attr[1] != EGL_GL_COLORSPACE_LINEAR_KHR &&
1725 attr[1] != EGL_GL_COLORSPACE_SRGB_KHR) {
1726 continue;
1727 }
1728 }
1729 strippedAttribList.push_back(attr[0]);
1730 strippedAttribList.push_back(attr[1]);
1731 }
1732 strippedAttribList.push_back(EGL_NONE);
1733
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001734 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1735 egl_connection_t* const cnx = &gEGLImpl;
1736 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1737 result = cnx->egl.eglCreateImageKHR(
1738 dp->disp.dpy,
1739 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001740 target, buffer, strippedAttribList.data());
Mathias Agopian518ec112011-05-13 16:21:08 -07001741 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001742 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001743}
1744
1745EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1746{
1747 clearError();
1748
Jesse Hallb29e5e82012-04-04 16:53:42 -07001749 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001750 if (!dp) return EGL_FALSE;
1751
Steven Holte646a5c52012-06-04 20:02:11 -07001752 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001753 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001754 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001755 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001756 }
Steven Holte646a5c52012-06-04 20:02:11 -07001757 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001758}
1759
1760// ----------------------------------------------------------------------------
1761// EGL_EGLEXT_VERSION 5
1762// ----------------------------------------------------------------------------
1763
1764
1765EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1766{
1767 clearError();
1768
Jesse Hallb29e5e82012-04-04 16:53:42 -07001769 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001770 if (!dp) return EGL_NO_SYNC_KHR;
1771
Mathias Agopian518ec112011-05-13 16:21:08 -07001772 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001773 egl_connection_t* const cnx = &gEGLImpl;
1774 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1775 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001776 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001777 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001778}
1779
1780EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1781{
1782 clearError();
1783
Jesse Hallb29e5e82012-04-04 16:53:42 -07001784 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001785 if (!dp) return EGL_FALSE;
1786
Mathias Agopian518ec112011-05-13 16:21:08 -07001787 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001788 egl_connection_t* const cnx = &gEGLImpl;
1789 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1790 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001791 }
1792 return result;
1793}
1794
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001795EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1796 clearError();
1797
1798 const egl_display_ptr dp = validate_display(dpy);
1799 if (!dp) return EGL_FALSE;
1800
1801 EGLBoolean result = EGL_FALSE;
1802 egl_connection_t* const cnx = &gEGLImpl;
1803 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1804 result = cnx->egl.eglSignalSyncKHR(
1805 dp->disp.dpy, sync, mode);
1806 }
1807 return result;
1808}
1809
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001810EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1811 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001812{
1813 clearError();
1814
Jesse Hallb29e5e82012-04-04 16:53:42 -07001815 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001816 if (!dp) return EGL_FALSE;
1817
Mathias Agopian737b8962017-02-24 14:32:05 -08001818 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001819 egl_connection_t* const cnx = &gEGLImpl;
1820 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1821 result = cnx->egl.eglClientWaitSyncKHR(
1822 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001823 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001824 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001825}
1826
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001827EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1828 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001829{
1830 clearError();
1831
Jesse Hallb29e5e82012-04-04 16:53:42 -07001832 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001833 if (!dp) return EGL_FALSE;
1834
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001835 EGLBoolean result = EGL_FALSE;
1836 egl_connection_t* const cnx = &gEGLImpl;
1837 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1838 result = cnx->egl.eglGetSyncAttribKHR(
1839 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001840 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001841 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001842}
1843
Season Li000d88f2015-07-01 11:39:40 -07001844EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1845{
1846 clearError();
1847
1848 const egl_display_ptr dp = validate_display(dpy);
1849 if (!dp) return EGL_NO_STREAM_KHR;
1850
1851 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1852 egl_connection_t* const cnx = &gEGLImpl;
1853 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1854 result = cnx->egl.eglCreateStreamKHR(
1855 dp->disp.dpy, attrib_list);
1856 }
1857 return result;
1858}
1859
1860EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1861{
1862 clearError();
1863
1864 const egl_display_ptr dp = validate_display(dpy);
1865 if (!dp) return EGL_FALSE;
1866
1867 EGLBoolean result = EGL_FALSE;
1868 egl_connection_t* const cnx = &gEGLImpl;
1869 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1870 result = cnx->egl.eglDestroyStreamKHR(
1871 dp->disp.dpy, stream);
1872 }
1873 return result;
1874}
1875
1876EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1877 EGLenum attribute, EGLint value)
1878{
1879 clearError();
1880
1881 const egl_display_ptr dp = validate_display(dpy);
1882 if (!dp) return EGL_FALSE;
1883
1884 EGLBoolean result = EGL_FALSE;
1885 egl_connection_t* const cnx = &gEGLImpl;
1886 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1887 result = cnx->egl.eglStreamAttribKHR(
1888 dp->disp.dpy, stream, attribute, value);
1889 }
1890 return result;
1891}
1892
1893EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1894 EGLenum attribute, EGLint *value)
1895{
1896 clearError();
1897
1898 const egl_display_ptr dp = validate_display(dpy);
1899 if (!dp) return EGL_FALSE;
1900
1901 EGLBoolean result = EGL_FALSE;
1902 egl_connection_t* const cnx = &gEGLImpl;
1903 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1904 result = cnx->egl.eglQueryStreamKHR(
1905 dp->disp.dpy, stream, attribute, value);
1906 }
1907 return result;
1908}
1909
1910EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1911 EGLenum attribute, EGLuint64KHR *value)
1912{
1913 clearError();
1914
1915 const egl_display_ptr dp = validate_display(dpy);
1916 if (!dp) return EGL_FALSE;
1917
1918 EGLBoolean result = EGL_FALSE;
1919 egl_connection_t* const cnx = &gEGLImpl;
1920 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1921 result = cnx->egl.eglQueryStreamu64KHR(
1922 dp->disp.dpy, stream, attribute, value);
1923 }
1924 return result;
1925}
1926
1927EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1928 EGLenum attribute, EGLTimeKHR *value)
1929{
1930 clearError();
1931
1932 const egl_display_ptr dp = validate_display(dpy);
1933 if (!dp) return EGL_FALSE;
1934
1935 EGLBoolean result = EGL_FALSE;
1936 egl_connection_t* const cnx = &gEGLImpl;
1937 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1938 result = cnx->egl.eglQueryStreamTimeKHR(
1939 dp->disp.dpy, stream, attribute, value);
1940 }
1941 return result;
1942}
1943
1944EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1945 EGLStreamKHR stream, const EGLint *attrib_list)
1946{
1947 clearError();
1948
1949 egl_display_ptr dp = validate_display(dpy);
1950 if (!dp) return EGL_NO_SURFACE;
1951
1952 egl_connection_t* const cnx = &gEGLImpl;
1953 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1954 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1955 dp->disp.dpy, config, stream, attrib_list);
1956 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001957 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
1958 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001959 return s;
1960 }
1961 }
1962 return EGL_NO_SURFACE;
1963}
1964
1965EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1966 EGLStreamKHR stream)
1967{
1968 clearError();
1969
1970 const egl_display_ptr dp = validate_display(dpy);
1971 if (!dp) return EGL_FALSE;
1972
1973 EGLBoolean result = EGL_FALSE;
1974 egl_connection_t* const cnx = &gEGLImpl;
1975 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1976 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1977 dp->disp.dpy, stream);
1978 }
1979 return result;
1980}
1981
1982EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1983 EGLStreamKHR stream)
1984{
1985 clearError();
1986
1987 const egl_display_ptr dp = validate_display(dpy);
1988 if (!dp) return EGL_FALSE;
1989
1990 EGLBoolean result = EGL_FALSE;
1991 egl_connection_t* const cnx = &gEGLImpl;
1992 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1993 result = cnx->egl.eglStreamConsumerAcquireKHR(
1994 dp->disp.dpy, stream);
1995 }
1996 return result;
1997}
1998
1999EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2000 EGLStreamKHR stream)
2001{
2002 clearError();
2003
2004 const egl_display_ptr dp = validate_display(dpy);
2005 if (!dp) return EGL_FALSE;
2006
2007 EGLBoolean result = EGL_FALSE;
2008 egl_connection_t* const cnx = &gEGLImpl;
2009 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2010 result = cnx->egl.eglStreamConsumerReleaseKHR(
2011 dp->disp.dpy, stream);
2012 }
2013 return result;
2014}
2015
2016EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2017 EGLDisplay dpy, EGLStreamKHR stream)
2018{
2019 clearError();
2020
2021 const egl_display_ptr dp = validate_display(dpy);
2022 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2023
2024 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2025 egl_connection_t* const cnx = &gEGLImpl;
2026 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2027 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2028 dp->disp.dpy, stream);
2029 }
2030 return result;
2031}
2032
2033EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2034 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2035{
2036 clearError();
2037
2038 const egl_display_ptr dp = validate_display(dpy);
2039 if (!dp) return EGL_NO_STREAM_KHR;
2040
2041 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2042 egl_connection_t* const cnx = &gEGLImpl;
2043 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2044 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2045 dp->disp.dpy, file_descriptor);
2046 }
2047 return result;
2048}
2049
Mathias Agopian518ec112011-05-13 16:21:08 -07002050// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002051// EGL_EGLEXT_VERSION 15
2052// ----------------------------------------------------------------------------
2053
2054EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2055 clearError();
2056 const egl_display_ptr dp = validate_display(dpy);
2057 if (!dp) return EGL_FALSE;
2058 EGLint result = EGL_FALSE;
2059 egl_connection_t* const cnx = &gEGLImpl;
2060 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2061 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2062 }
2063 return result;
2064}
2065
2066// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002067// ANDROID extensions
2068// ----------------------------------------------------------------------------
2069
Jamie Gennis331841b2012-09-06 14:52:00 -07002070EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2071{
2072 clearError();
2073
2074 const egl_display_ptr dp = validate_display(dpy);
2075 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2076
2077 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2078 egl_connection_t* const cnx = &gEGLImpl;
2079 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2080 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2081 }
2082 return result;
2083}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002084
Andy McFadden72841452013-03-01 16:25:32 -08002085EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2086 EGLnsecsANDROID time)
2087{
2088 clearError();
2089
2090 const egl_display_ptr dp = validate_display(dpy);
2091 if (!dp) {
2092 return EGL_FALSE;
2093 }
2094
2095 SurfaceRef _s(dp.get(), surface);
2096 if (!_s.get()) {
2097 setError(EGL_BAD_SURFACE, EGL_FALSE);
2098 return EGL_FALSE;
2099 }
2100
2101 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002102 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002103
2104 return EGL_TRUE;
2105}
2106
Craig Donner60761072017-01-27 12:30:44 -08002107EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2108 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002109 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2110 // this function cannot be implemented when this libEGL is built for
2111 // vendors.
2112#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002113 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002114 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002115#else
2116 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2117#endif
Craig Donner60761072017-01-27 12:30:44 -08002118}
2119
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002120// ----------------------------------------------------------------------------
2121// NVIDIA extensions
2122// ----------------------------------------------------------------------------
2123EGLuint64NV eglGetSystemTimeFrequencyNV()
2124{
2125 clearError();
2126
2127 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002128 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002129 }
2130
2131 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002132 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002133
Mathias Agopianada798b2012-02-13 17:09:30 -08002134 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2135 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002136 }
2137
Mathias Agopian737b8962017-02-24 14:32:05 -08002138 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002139}
2140
2141EGLuint64NV eglGetSystemTimeNV()
2142{
2143 clearError();
2144
2145 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002146 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002147 }
2148
2149 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002150 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002151
Mathias Agopianada798b2012-02-13 17:09:30 -08002152 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2153 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002154 }
2155
Mathias Agopian737b8962017-02-24 14:32:05 -08002156 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002157}
Dan Stozaa894d082015-02-19 15:27:36 -08002158
2159// ----------------------------------------------------------------------------
2160// Partial update extension
2161// ----------------------------------------------------------------------------
2162EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2163 EGLint *rects, EGLint n_rects)
2164{
2165 clearError();
2166
2167 const egl_display_ptr dp = validate_display(dpy);
2168 if (!dp) {
2169 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2170 return EGL_FALSE;
2171 }
2172
2173 SurfaceRef _s(dp.get(), surface);
2174 if (!_s.get()) {
2175 setError(EGL_BAD_SURFACE, EGL_FALSE);
2176 return EGL_FALSE;
2177 }
2178
2179 egl_surface_t const * const s = get_surface(surface);
2180 if (s->cnx->egl.eglSetDamageRegionKHR) {
2181 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2182 rects, n_rects);
2183 }
2184
2185 return EGL_FALSE;
2186}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002187
Brian Anderson1049d1d2016-12-16 17:25:57 -08002188EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2189 EGLuint64KHR *frameId) {
2190 clearError();
2191
2192 const egl_display_ptr dp = validate_display(dpy);
2193 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002194 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002195 }
2196
2197 SurfaceRef _s(dp.get(), surface);
2198 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002199 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002200 }
2201
2202 egl_surface_t const * const s = get_surface(surface);
2203
Mathias Agopian65421432017-03-08 11:49:05 -08002204 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002205 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002206 }
2207
2208 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002209 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002210
Mathias Agopian65421432017-03-08 11:49:05 -08002211 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002212 // This should not happen. Return an error that is not in the spec
2213 // so it's obvious something is very wrong.
2214 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002215 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002216 }
2217
2218 *frameId = nextFrameId;
2219 return EGL_TRUE;
2220}
2221
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002222EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2223 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2224{
2225 clearError();
2226
2227 const egl_display_ptr dp = validate_display(dpy);
2228 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002229 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002230 }
2231
2232 SurfaceRef _s(dp.get(), surface);
2233 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002234 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002235 }
2236
2237 egl_surface_t const * const s = get_surface(surface);
2238
Mathias Agopian65421432017-03-08 11:49:05 -08002239 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002240 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002241 }
2242
2243 nsecs_t* compositeDeadline = nullptr;
2244 nsecs_t* compositeInterval = nullptr;
2245 nsecs_t* compositeToPresentLatency = nullptr;
2246
2247 for (int i = 0; i < numTimestamps; i++) {
2248 switch (names[i]) {
2249 case EGL_COMPOSITE_DEADLINE_ANDROID:
2250 compositeDeadline = &values[i];
2251 break;
2252 case EGL_COMPOSITE_INTERVAL_ANDROID:
2253 compositeInterval = &values[i];
2254 break;
2255 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2256 compositeToPresentLatency = &values[i];
2257 break;
2258 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002259 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002260 }
2261 }
2262
Mathias Agopian65421432017-03-08 11:49:05 -08002263 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002264 compositeDeadline, compositeInterval, compositeToPresentLatency);
2265
2266 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002267 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002268 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002269 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002270 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002271 default:
2272 // This should not happen. Return an error that is not in the spec
2273 // so it's obvious something is very wrong.
2274 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002275 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002276 }
2277}
2278
2279EGLBoolean eglGetCompositorTimingSupportedANDROID(
2280 EGLDisplay dpy, EGLSurface surface, EGLint name)
2281{
2282 clearError();
2283
2284 const egl_display_ptr dp = validate_display(dpy);
2285 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002286 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002287 }
2288
2289 SurfaceRef _s(dp.get(), surface);
2290 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002291 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002292 }
2293
2294 egl_surface_t const * const s = get_surface(surface);
2295
Mathias Agopian65421432017-03-08 11:49:05 -08002296 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002297 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002298 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002299 }
2300
2301 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002302 case EGL_COMPOSITE_DEADLINE_ANDROID:
2303 case EGL_COMPOSITE_INTERVAL_ANDROID:
2304 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2305 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002306 default:
2307 return EGL_FALSE;
2308 }
2309}
2310
Pablo Ceballosc18be292016-05-31 14:55:42 -07002311EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002312 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002313 EGLnsecsANDROID *values)
2314{
2315 clearError();
2316
2317 const egl_display_ptr dp = validate_display(dpy);
2318 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002319 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002320 }
2321
2322 SurfaceRef _s(dp.get(), surface);
2323 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002324 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002325 }
2326
2327 egl_surface_t const * const s = get_surface(surface);
2328
Mathias Agopian65421432017-03-08 11:49:05 -08002329 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002330 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002331 }
2332
Brian Andersondbd0ea82016-07-22 09:38:59 -07002333 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002334 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002335 nsecs_t* latchTime = nullptr;
2336 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002337 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002338 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002339 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002340 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002341 nsecs_t* releaseTime = nullptr;
2342
2343 for (int i = 0; i < numTimestamps; i++) {
2344 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002345 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2346 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002347 break;
2348 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2349 acquireTime = &values[i];
2350 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002351 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2352 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002353 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002354 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2355 firstRefreshStartTime = &values[i];
2356 break;
2357 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2358 lastRefreshStartTime = &values[i];
2359 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002360 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2361 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002362 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002363 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2364 displayPresentTime = &values[i];
2365 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002366 case EGL_DEQUEUE_READY_TIME_ANDROID:
2367 dequeueReadyTime = &values[i];
2368 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002369 case EGL_READS_DONE_TIME_ANDROID:
2370 releaseTime = &values[i];
2371 break;
2372 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002373 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002374 }
2375 }
2376
Mathias Agopian65421432017-03-08 11:49:05 -08002377 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002378 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002379 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002380 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002381
Brian Anderson069b3652016-07-22 10:32:47 -07002382 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002383 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002384 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002385 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002386 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002387 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002388 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002389 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002390 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2391 default:
2392 // This should not happen. Return an error that is not in the spec
2393 // so it's obvious something is very wrong.
2394 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2395 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002396 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002397}
2398
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002399EGLBoolean eglGetFrameTimestampSupportedANDROID(
2400 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002401{
2402 clearError();
2403
2404 const egl_display_ptr dp = validate_display(dpy);
2405 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002406 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002407 }
2408
2409 SurfaceRef _s(dp.get(), surface);
2410 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002411 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002412 }
2413
2414 egl_surface_t const * const s = get_surface(surface);
2415
Mathias Agopian65421432017-03-08 11:49:05 -08002416 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002417 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002418 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002419 }
2420
2421 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002422 case EGL_COMPOSITE_DEADLINE_ANDROID:
2423 case EGL_COMPOSITE_INTERVAL_ANDROID:
2424 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002425 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002426 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002427 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2428 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2429 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002430 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002431 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002432 case EGL_READS_DONE_TIME_ANDROID:
2433 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002434 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2435 int value = 0;
2436 window->query(window,
2437 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2438 return value == 0 ? EGL_FALSE : EGL_TRUE;
2439 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002440 default:
2441 return EGL_FALSE;
2442 }
2443}