blob: e824238ac3fbadcc2f04207f64d8c4e093588456 [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) {
Chia-I Wubf433ab2018-06-20 10:57:48 +0800459 return HAL_DATASPACE_UNKNOWN;
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800461 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600462 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
463 return HAL_DATASPACE_DISPLAY_P3;
464 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
465 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600466 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
467 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600468 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
469 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700470 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT) {
471 return HAL_DATASPACE_BT2020_LINEAR;
472 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_PQ_EXT) {
473 return HAL_DATASPACE_BT2020_PQ;
Jesse Hallc2e41222013-08-08 13:40:22 -0700474 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700475 return HAL_DATASPACE_UNKNOWN;
Jesse Hallc2e41222013-08-08 13:40:22 -0700476}
477
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700478// Get the colorspace value that should be reported from queries. When the colorspace
479// is unknown (no attribute passed), default to reporting LINEAR.
480static EGLint getReportedColorSpace(EGLint colorspace) {
481 return colorspace == EGL_UNKNOWN ? EGL_GL_COLORSPACE_LINEAR_KHR : colorspace;
482}
483
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700484// Returns a list of color spaces understood by the vendor EGL driver.
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700485static std::vector<EGLint> getDriverColorSpaces(egl_display_ptr dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700486 std::vector<EGLint> colorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700487
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700488 // sRGB and linear are always supported when color space support is present.
489 colorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
490 colorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700491
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700492 if (findExtension(dp->disp.queryString.extensions,
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700493 "EGL_EXT_gl_colorspace_display_p3")) {
494 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
495 }
496 if (findExtension(dp->disp.queryString.extensions,
497 "EGL_EXT_gl_colorspace_scrgb")) {
498 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_EXT);
499 }
500 if (findExtension(dp->disp.queryString.extensions,
501 "EGL_EXT_gl_colorspace_scrgb_linear")) {
502 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
503 }
504 if (findExtension(dp->disp.queryString.extensions,
505 "EGL_EXT_gl_colorspace_bt2020_linear")) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700506 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
507 }
508 if (findExtension(dp->disp.queryString.extensions,
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700509 "EGL_EXT_gl_colorspace_bt2020_pq")) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700510 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
511 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700512 if (findExtension(dp->disp.queryString.extensions,
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700513 "EGL_EXT_gl_colorspace_display_p3_linear")) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700514 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
515 }
516 return colorSpaces;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600517}
518
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700519// Cleans up color space related parameters that the driver does not understand.
520// If there is no color space attribute in attrib_list, colorSpace is left
521// unmodified.
522static EGLBoolean processAttributes(egl_display_ptr dp, NativeWindowType window,
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700523 const EGLint* attrib_list, EGLint* colorSpace,
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700524 std::vector<EGLint>* strippedAttribList) {
525 for (const EGLint* attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
526 bool copyAttribute = true;
527 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700528 switch (attr[1]) {
529 case EGL_GL_COLORSPACE_LINEAR_KHR:
530 case EGL_GL_COLORSPACE_SRGB_KHR:
531 case EGL_GL_COLORSPACE_DISPLAY_P3_EXT:
532 case EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT:
533 case EGL_GL_COLORSPACE_SCRGB_EXT:
534 case EGL_GL_COLORSPACE_BT2020_LINEAR_EXT:
535 case EGL_GL_COLORSPACE_BT2020_PQ_EXT:
536 case EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT:
537 // Fail immediately if the driver doesn't have color space support at all.
538 if (!dp->hasColorSpaceSupport) return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
539 break;
540 default:
541 // BAD_ATTRIBUTE if attr is not any of the EGL_GL_COLORSPACE_*
542 return setError(EGL_BAD_ATTRIBUTE, EGL_FALSE);
543 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700544 *colorSpace = attr[1];
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600545
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700546 // Strip the attribute if the driver doesn't understand it.
547 copyAttribute = false;
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700548 std::vector<EGLint> driverColorSpaces = getDriverColorSpaces(dp);
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700549 for (auto driverColorSpace : driverColorSpaces) {
550 if (attr[1] == driverColorSpace) {
551 copyAttribute = true;
552 break;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600553 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600554 }
555 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700556 if (copyAttribute) {
557 strippedAttribList->push_back(attr[0]);
558 strippedAttribList->push_back(attr[1]);
559 }
560 }
561 // Terminate the attribute list.
562 strippedAttribList->push_back(EGL_NONE);
563
564 // If the passed color space has wide color gamut, check whether the target native window
565 // supports wide color.
566 const bool colorSpaceIsNarrow =
567 *colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700568 *colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR ||
569 *colorSpace == EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700570 if (window && !colorSpaceIsNarrow) {
571 bool windowSupportsWideColor = true;
572 // Ordinarily we'd put a call to native_window_get_wide_color_support
573 // at the beginning of the function so that we'll have the
574 // result when needed elsewhere in the function.
575 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
576 // SurfaceFlinger is required to answer the call below we would
577 // end up in a deadlock situation. By moving the call to only happen
578 // if the application has specifically asked for wide-color we avoid
579 // the deadlock with SurfaceFlinger since it will not ask for a
580 // wide-color surface.
581 int err = native_window_get_wide_color_support(window, &windowSupportsWideColor);
582
583 if (err) {
584 ALOGE("processAttributes: invalid window (win=%p) "
585 "failed (%#x) (already connected to another API?)",
586 window, err);
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700587 return setError(EGL_BAD_NATIVE_WINDOW, EGL_FALSE);
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700588 }
589 if (!windowSupportsWideColor) {
590 // Application has asked for a wide-color colorspace but
591 // wide-color support isn't available on the display the window is on.
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700592 return setError(EGL_BAD_MATCH, EGL_FALSE);
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700593 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600594 }
595 return true;
596}
597
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700598// Gets the native pixel format corrsponding to the passed EGLConfig.
599void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config,
600 android_pixel_format* format) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600601 // Set the native window's buffers format to match what this config requests.
602 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
603 // of our native format. So if sRGB gamma is requested, we have to
604 // modify the EGLconfig's format before setting the native window's
605 // format.
606
607 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
608 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
609
610 EGLint a = 0;
611 EGLint r, g, b;
612 r = g = b = 0;
613 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
614 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
615 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
616 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
617 EGLint colorDepth = r + g + b;
618
619 // Today, the driver only understands sRGB and linear on 888X
620 // formats. Strip other colorspaces from the attribute list and
621 // only use them to set the dataspace via
622 // native_window_set_buffers_dataspace
623 // if pixel format is RGBX 8888
624 // TBD: Can test for future extensions that indicate that driver
625 // handles requested color space and we can let it through.
626 // allow SRGB and LINEAR. All others need to be stripped.
627 // else if 565, 4444
628 // TBD: Can we assume these are supported if 8888 is?
629 // else if FP16 or 1010102
630 // strip colorspace from attribs.
631 // endif
632 if (a == 0) {
633 if (colorDepth <= 16) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700634 *format = HAL_PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600635 } else {
636 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
637 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700638 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600639 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700640 *format = HAL_PIXEL_FORMAT_RGBX_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600641 }
642 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700643 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600644 }
645 }
646 } else {
647 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
648 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700649 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600650 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700651 *format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600652 }
653 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700654 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600655 }
656 }
657}
658
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700659EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
660 android_smpte2086_metadata smpteMetadata;
661 if (s->getSmpte2086Metadata(smpteMetadata)) {
662 int err =
663 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
664 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700665 if (err != 0) {
666 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
667 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700668 return EGL_FALSE;
669 }
670 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700671 android_cta861_3_metadata cta8613Metadata;
672 if (s->getCta8613Metadata(cta8613Metadata)) {
673 int err =
674 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
675 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700676 if (err != 0) {
677 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
678 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700679 return EGL_FALSE;
680 }
681 }
682 return EGL_TRUE;
683}
684
Mathias Agopian518ec112011-05-13 16:21:08 -0700685EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
686 NativeWindowType window,
687 const EGLint *attrib_list)
688{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700689 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700690 clearError();
691
Jesse Hallb29e5e82012-04-04 16:53:42 -0700692 egl_connection_t* cnx = NULL;
693 egl_display_ptr dp = validate_display_connection(dpy, cnx);
694 if (dp) {
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800695 if (!window) {
696 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
697 }
698
699 int value = 0;
700 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
701 if (!value) {
702 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
703 }
704
Andy McFaddend566ce32014-01-07 15:54:17 -0800705 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800706 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800707 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
708 "failed (%#x) (already connected to another API?)",
709 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700710 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700711 }
712
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700713 EGLDisplay iDpy = dp->disp.dpy;
714 android_pixel_format format;
715 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600716
717 // now select correct colorspace and dataspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700718 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700719 std::vector<EGLint> strippedAttribList;
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700720 if (!processAttributes(dp, window, attrib_list, &colorSpace,
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700721 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600722 ALOGE("error invalid colorspace: %d", colorSpace);
Weiwan Liub922a7b2018-09-27 15:56:25 -0700723 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700724 return EGL_NO_SURFACE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700725 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700726 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800727
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700728 {
Jesse Hallc2e41222013-08-08 13:40:22 -0700729 int err = native_window_set_buffers_format(window, format);
730 if (err != 0) {
731 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700732 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700733 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
734 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
735 }
736 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700737
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700738 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
Weiwan Liu34cd2272018-09-27 15:58:55 -0700739 // Set dataSpace even if it could be HAL_DATASPACE_UNKNOWN. HAL_DATASPACE_UNKNOWN
740 // is the default value, but it may have changed at this point.
741 int err = native_window_set_buffers_data_space(window, dataSpace);
742 if (err != 0) {
743 ALOGE("error setting native window pixel dataSpace: %s (%d)",
744 strerror(-err), err);
745 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
746 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800747 }
748
Jamie Gennis59769462011-11-19 18:04:43 -0800749 // the EGL spec requires that a new EGLSurface default to swap interval
750 // 1, so explicitly set that on the window here.
751 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
752 anw->setSwapInterval(anw, 1);
753
Mathias Agopian518ec112011-05-13 16:21:08 -0700754 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800755 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600757 egl_surface_t* s =
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700758 new egl_surface_t(dp.get(), config, window, surface,
759 getReportedColorSpace(colorSpace), cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700760 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700761 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700762
763 // EGLSurface creation failed
764 native_window_set_buffers_format(window, 0);
765 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700766 }
767 return EGL_NO_SURFACE;
768}
769
770EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
771 NativePixmapType pixmap,
772 const EGLint *attrib_list)
773{
774 clearError();
775
Jesse Hallb29e5e82012-04-04 16:53:42 -0700776 egl_connection_t* cnx = NULL;
777 egl_display_ptr dp = validate_display_connection(dpy, cnx);
778 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700779 EGLDisplay iDpy = dp->disp.dpy;
780 android_pixel_format format;
781 getNativePixelFormat(iDpy, cnx, config, &format);
782
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600783 // now select a corresponding sRGB format if needed
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700784 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700785 std::vector<EGLint> strippedAttribList;
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700786 if (!processAttributes(dp, nullptr, attrib_list, &colorSpace,
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700787 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600788 ALOGE("error invalid colorspace: %d", colorSpace);
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700789 return EGL_NO_SURFACE;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600790 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700791 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600792
Mathias Agopian518ec112011-05-13 16:21:08 -0700793 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800794 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700795 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700796 egl_surface_t* s =
797 new egl_surface_t(dp.get(), config, NULL, surface,
798 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 return s;
800 }
801 }
802 return EGL_NO_SURFACE;
803}
804
805EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
806 const EGLint *attrib_list)
807{
808 clearError();
809
Jesse Hallb29e5e82012-04-04 16:53:42 -0700810 egl_connection_t* cnx = NULL;
811 egl_display_ptr dp = validate_display_connection(dpy, cnx);
812 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600813 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700814 android_pixel_format format;
815 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600816
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700817 // Select correct colorspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700818 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700819 std::vector<EGLint> strippedAttribList;
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700820 if (!processAttributes(dp, nullptr, attrib_list, &colorSpace,
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700821 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600822 ALOGE("error invalid colorspace: %d", colorSpace);
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700823 return EGL_NO_SURFACE;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600824 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700825 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600826
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800828 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700829 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700830 egl_surface_t* s =
831 new egl_surface_t(dp.get(), config, NULL, surface,
832 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700833 return s;
834 }
835 }
836 return EGL_NO_SURFACE;
837}
Jesse Hall47743382013-02-08 11:13:46 -0800838
Mathias Agopian518ec112011-05-13 16:21:08 -0700839EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
840{
841 clearError();
842
Jesse Hallb29e5e82012-04-04 16:53:42 -0700843 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700844 if (!dp) return EGL_FALSE;
845
Jesse Hallb29e5e82012-04-04 16:53:42 -0700846 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700847 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800848 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700849
850 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800851 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700852 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 _s.terminate();
854 }
855 return result;
856}
857
858EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
859 EGLint attribute, EGLint *value)
860{
861 clearError();
862
Jesse Hallb29e5e82012-04-04 16:53:42 -0700863 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700864 if (!dp) return EGL_FALSE;
865
Jesse Hallb29e5e82012-04-04 16:53:42 -0700866 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700867 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800868 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700869
Mathias Agopian518ec112011-05-13 16:21:08 -0700870 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700871 if (s->getColorSpaceAttribute(attribute, value)) {
872 return EGL_TRUE;
873 } else if (s->getSmpte2086Attribute(attribute, value)) {
874 return EGL_TRUE;
875 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600876 return EGL_TRUE;
877 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700878 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700879}
880
Jamie Gennise8696a42012-01-15 18:54:57 -0800881void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800882 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800883 clearError();
884
Jesse Hallb29e5e82012-04-04 16:53:42 -0700885 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800886 if (!dp) {
887 return;
888 }
889
Jesse Hallb29e5e82012-04-04 16:53:42 -0700890 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800891 if (!_s.get()) {
892 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800893 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800894}
895
Mathias Agopian518ec112011-05-13 16:21:08 -0700896// ----------------------------------------------------------------------------
897// Contexts
898// ----------------------------------------------------------------------------
899
900EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
901 EGLContext share_list, const EGLint *attrib_list)
902{
903 clearError();
904
Jesse Hallb29e5e82012-04-04 16:53:42 -0700905 egl_connection_t* cnx = NULL;
906 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700907 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700908 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700909 if (!ContextRef(dp.get(), share_list).get()) {
910 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
911 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700912 egl_context_t* const c = get_context(share_list);
913 share_list = c->context;
914 }
915 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800916 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700917 if (context != EGL_NO_CONTEXT) {
918 // figure out if it's a GLESv1 or GLESv2
919 int version = 0;
920 if (attrib_list) {
921 while (*attrib_list != EGL_NONE) {
922 GLint attr = *attrib_list++;
923 GLint value = *attrib_list++;
924 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
925 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800926 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800927 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800928 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700929 }
930 }
931 };
932 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700933 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
934 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700935 return c;
936 }
937 }
938 return EGL_NO_CONTEXT;
939}
940
941EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
942{
943 clearError();
944
Jesse Hallb29e5e82012-04-04 16:53:42 -0700945 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700946 if (!dp)
947 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700948
Jesse Hallb29e5e82012-04-04 16:53:42 -0700949 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700950 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800951 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800952
Mathias Agopian518ec112011-05-13 16:21:08 -0700953 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800954 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700955 if (result == EGL_TRUE) {
956 _c.terminate();
957 }
958 return result;
959}
960
Mathias Agopian518ec112011-05-13 16:21:08 -0700961EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
962 EGLSurface read, EGLContext ctx)
963{
964 clearError();
965
Jesse Hallb29e5e82012-04-04 16:53:42 -0700966 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800967 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700968
Mathias Agopian5b287a62011-05-16 18:58:55 -0700969 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
970 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
971 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700972 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
973 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800974 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700975 }
976
977 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700978 ContextRef _c(dp.get(), ctx);
979 SurfaceRef _d(dp.get(), draw);
980 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700981
982 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700983 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700984 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800985 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700986 }
987
988 // these are the underlying implementation's object
989 EGLContext impl_ctx = EGL_NO_CONTEXT;
990 EGLSurface impl_draw = EGL_NO_SURFACE;
991 EGLSurface impl_read = EGL_NO_SURFACE;
992
993 // these are our objects structs passed in
994 egl_context_t * c = NULL;
995 egl_surface_t const * d = NULL;
996 egl_surface_t const * r = NULL;
997
998 // these are the current objects structs
999 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001000
Mathias Agopian518ec112011-05-13 16:21:08 -07001001 if (ctx != EGL_NO_CONTEXT) {
1002 c = get_context(ctx);
1003 impl_ctx = c->context;
1004 } else {
1005 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001006 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1007 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001008 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001009 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 if (cur_c == NULL) {
1011 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001012 // not an error, there is just no current context.
1013 return EGL_TRUE;
1014 }
1015 }
1016
1017 // retrieve the underlying implementation's draw EGLSurface
1018 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001019 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001020 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001021 impl_draw = d->surface;
1022 }
1023
1024 // retrieve the underlying implementation's read EGLSurface
1025 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001026 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001027 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001028 impl_read = r->surface;
1029 }
1030
Mathias Agopian518ec112011-05-13 16:21:08 -07001031
Jesse Hallb29e5e82012-04-04 16:53:42 -07001032 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001033 draw, read, ctx,
1034 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001035
1036 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001037 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001038 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1039 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001040 _c.acquire();
1041 _r.acquire();
1042 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001043 } else {
1044 setGLHooksThreadSpecific(&gHooksNoContext);
1045 egl_tls_t::setContext(EGL_NO_CONTEXT);
1046 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001047 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001048 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001049 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001050 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001051 }
1052 return result;
1053}
1054
1055
1056EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1057 EGLint attribute, EGLint *value)
1058{
1059 clearError();
1060
Jesse Hallb29e5e82012-04-04 16:53:42 -07001061 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001062 if (!dp) return EGL_FALSE;
1063
Jesse Hallb29e5e82012-04-04 16:53:42 -07001064 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001065 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001066
Mathias Agopian518ec112011-05-13 16:21:08 -07001067 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001068 return c->cnx->egl.eglQueryContext(
1069 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001070
Mathias Agopian518ec112011-05-13 16:21:08 -07001071}
1072
1073EGLContext eglGetCurrentContext(void)
1074{
1075 // could be called before eglInitialize(), but we wouldn't have a context
1076 // then, and this function would correctly return EGL_NO_CONTEXT.
1077
1078 clearError();
1079
1080 EGLContext ctx = getContext();
1081 return ctx;
1082}
1083
1084EGLSurface eglGetCurrentSurface(EGLint readdraw)
1085{
1086 // could be called before eglInitialize(), but we wouldn't have a context
1087 // then, and this function would correctly return EGL_NO_SURFACE.
1088
1089 clearError();
1090
1091 EGLContext ctx = getContext();
1092 if (ctx) {
1093 egl_context_t const * const c = get_context(ctx);
1094 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1095 switch (readdraw) {
1096 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001097 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001098 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1099 }
1100 }
1101 return EGL_NO_SURFACE;
1102}
1103
1104EGLDisplay eglGetCurrentDisplay(void)
1105{
1106 // could be called before eglInitialize(), but we wouldn't have a context
1107 // then, and this function would correctly return EGL_NO_DISPLAY.
1108
1109 clearError();
1110
1111 EGLContext ctx = getContext();
1112 if (ctx) {
1113 egl_context_t const * const c = get_context(ctx);
1114 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1115 return c->dpy;
1116 }
1117 return EGL_NO_DISPLAY;
1118}
1119
1120EGLBoolean eglWaitGL(void)
1121{
Mathias Agopian518ec112011-05-13 16:21:08 -07001122 clearError();
1123
Mathias Agopianada798b2012-02-13 17:09:30 -08001124 egl_connection_t* const cnx = &gEGLImpl;
1125 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001126 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001127
1128 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001129}
1130
1131EGLBoolean eglWaitNative(EGLint engine)
1132{
Mathias Agopian518ec112011-05-13 16:21:08 -07001133 clearError();
1134
Mathias Agopianada798b2012-02-13 17:09:30 -08001135 egl_connection_t* const cnx = &gEGLImpl;
1136 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001137 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001138
1139 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001140}
1141
1142EGLint eglGetError(void)
1143{
Mathias Agopianada798b2012-02-13 17:09:30 -08001144 EGLint err = EGL_SUCCESS;
1145 egl_connection_t* const cnx = &gEGLImpl;
1146 if (cnx->dso) {
1147 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001148 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001149 if (err == EGL_SUCCESS) {
1150 err = egl_tls_t::getError();
1151 }
1152 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001153}
1154
Michael Chockc0ec5e22014-01-27 08:14:33 -08001155static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001156 const char* procname) {
1157 const egl_connection_t* cnx = &gEGLImpl;
1158 void* proc = NULL;
1159
Michael Chockc0ec5e22014-01-27 08:14:33 -08001160 proc = dlsym(cnx->libEgl, procname);
1161 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1162
Jesse Hallc07b5202013-07-04 12:08:16 -07001163 proc = dlsym(cnx->libGles2, procname);
1164 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1165
1166 proc = dlsym(cnx->libGles1, procname);
1167 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1168
1169 return NULL;
1170}
1171
Mathias Agopian518ec112011-05-13 16:21:08 -07001172__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1173{
1174 // eglGetProcAddress() could be the very first function called
1175 // in which case we must make sure we've initialized ourselves, this
1176 // happens the first time egl_get_display() is called.
1177
1178 clearError();
1179
1180 if (egl_init_drivers() == EGL_FALSE) {
1181 setError(EGL_BAD_PARAMETER, NULL);
1182 return NULL;
1183 }
1184
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001185 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001186 return NULL;
1187 }
1188
Mathias Agopian518ec112011-05-13 16:21:08 -07001189 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001190 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001191 if (addr) return addr;
1192
Michael Chockc0ec5e22014-01-27 08:14:33 -08001193 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001194 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001195
Mathias Agopian518ec112011-05-13 16:21:08 -07001196 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1197 pthread_mutex_lock(&sExtensionMapMutex);
1198
1199 /*
1200 * Since eglGetProcAddress() is not associated to anything, it needs
1201 * to return a function pointer that "works" regardless of what
1202 * the current context is.
1203 *
1204 * For this reason, we return a "forwarder", a small stub that takes
1205 * care of calling the function associated with the context
1206 * currently bound.
1207 *
1208 * We first look for extensions we've already resolved, if we're seeing
1209 * this extension for the first time, we go through all our
1210 * implementations and call eglGetProcAddress() and record the
1211 * result in the appropriate implementation hooks and return the
1212 * address of the forwarder corresponding to that hook set.
1213 *
1214 */
1215
Mathias Agopian65421432017-03-08 11:49:05 -08001216 const std::string name(procname);
1217
1218 auto& extentionMap = sGLExtentionMap;
1219 auto pos = extentionMap.find(name);
1220 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001221 const int slot = sGLExtentionSlot;
1222
Steve Blocke6f43dd2012-01-06 19:20:56 +00001223 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001224 "no more slots for eglGetProcAddress(\"%s\")",
1225 procname);
1226
1227 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1228 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001229
1230 egl_connection_t* const cnx = &gEGLImpl;
1231 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001232 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001233 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001234 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1235 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001236 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001237 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001238 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001239
Mathias Agopian518ec112011-05-13 16:21:08 -07001240 if (found) {
1241 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001242 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001243 sGLExtentionSlot++;
1244 }
1245 }
1246
1247 pthread_mutex_unlock(&sExtensionMapMutex);
1248 return addr;
1249}
1250
Mathias Agopian65421432017-03-08 11:49:05 -08001251class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001252public:
1253
1254 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001255 static FrameCompletionThread thread;
1256
1257 char name[64];
1258
1259 std::lock_guard<std::mutex> lock(thread.mMutex);
1260 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1261 ATRACE_NAME(name);
1262
1263 thread.mQueue.push_back(sync);
1264 thread.mCondition.notify_one();
1265 thread.mFramesQueued++;
1266 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001267 }
1268
1269private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001270
Mathias Agopian65421432017-03-08 11:49:05 -08001271 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1272 std::thread thread(&FrameCompletionThread::loop, this);
1273 thread.detach();
1274 }
1275
1276#pragma clang diagnostic push
1277#pragma clang diagnostic ignored "-Wmissing-noreturn"
1278 void loop() {
1279 while (true) {
1280 threadLoop();
1281 }
1282 }
1283#pragma clang diagnostic pop
1284
1285 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001286 EGLSyncKHR sync;
1287 uint32_t frameNum;
1288 {
Mathias Agopian65421432017-03-08 11:49:05 -08001289 std::unique_lock<std::mutex> lock(mMutex);
1290 while (mQueue.empty()) {
1291 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001292 }
1293 sync = mQueue[0];
1294 frameNum = mFramesCompleted;
1295 }
1296 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1297 {
Mathias Agopian65421432017-03-08 11:49:05 -08001298 char name[64];
1299 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1300 ATRACE_NAME(name);
1301
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001302 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1303 if (result == EGL_FALSE) {
1304 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1305 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1306 ALOGE("FrameCompletion: timeout waiting for fence");
1307 }
1308 eglDestroySyncKHR(dpy, sync);
1309 }
1310 {
Mathias Agopian65421432017-03-08 11:49:05 -08001311 std::lock_guard<std::mutex> lock(mMutex);
1312 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001313 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001314 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001315 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001316 }
1317
1318 uint32_t mFramesQueued;
1319 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001320 std::deque<EGLSyncKHR> mQueue;
1321 std::condition_variable mCondition;
1322 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001323};
1324
Dan Stozaa894d082015-02-19 15:27:36 -08001325EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1326 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001327{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001328 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001329 clearError();
1330
Jesse Hallb29e5e82012-04-04 16:53:42 -07001331 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001332 if (!dp) return EGL_FALSE;
1333
Jesse Hallb29e5e82012-04-04 16:53:42 -07001334 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001335 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001336 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001337
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001338 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001339
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001340 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1341 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1342 if (sync != EGL_NO_SYNC_KHR) {
1343 FrameCompletionThread::queueSync(sync);
1344 }
1345 }
1346
Mathias Agopian7db993a2012-03-25 00:49:46 -07001347 if (CC_UNLIKELY(dp->finishOnSwap)) {
1348 uint32_t pixel;
1349 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1350 if (c) {
1351 // glReadPixels() ensures that the frame is complete
1352 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1353 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1354 }
1355 }
1356
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001357 if (!sendSurfaceMetadata(s)) {
1358 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1359 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1360 }
1361
Dan Stozaa894d082015-02-19 15:27:36 -08001362 if (n_rects == 0) {
1363 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1364 }
1365
Mathias Agopian65421432017-03-08 11:49:05 -08001366 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001367 for (int r = 0; r < n_rects; ++r) {
1368 int offset = r * 4;
1369 int x = rects[offset];
1370 int y = rects[offset + 1];
1371 int width = rects[offset + 2];
1372 int height = rects[offset + 3];
1373 android_native_rect_t androidRect;
1374 androidRect.left = x;
1375 androidRect.top = y + height;
1376 androidRect.right = x + width;
1377 androidRect.bottom = y;
1378 androidRects.push_back(androidRect);
1379 }
Mathias Agopian65421432017-03-08 11:49:05 -08001380 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001381
1382 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1383 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1384 rects, n_rects);
1385 } else {
1386 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1387 }
1388}
1389
1390EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1391{
1392 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001393}
1394
1395EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1396 NativePixmapType target)
1397{
1398 clearError();
1399
Jesse Hallb29e5e82012-04-04 16:53:42 -07001400 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001401 if (!dp) return EGL_FALSE;
1402
Jesse Hallb29e5e82012-04-04 16:53:42 -07001403 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001404 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001405 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001406
Mathias Agopian518ec112011-05-13 16:21:08 -07001407 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001408 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001409}
1410
1411const char* eglQueryString(EGLDisplay dpy, EGLint name)
1412{
1413 clearError();
1414
Chia-I Wue57d1352016-08-15 16:10:02 +08001415 // Generate an error quietly when client extensions (as defined by
1416 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1417 // validate_display to generate the error as validate_display would log
1418 // the error, which can be misleading.
1419 //
1420 // If we want to support EGL_EXT_client_extensions later, we can return
1421 // the client extension string here instead.
1422 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001423 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001424
Jesse Hallb29e5e82012-04-04 16:53:42 -07001425 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001426 if (!dp) return (const char *) NULL;
1427
1428 switch (name) {
1429 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001430 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001431 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001432 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001433 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001434 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001435 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001436 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001437 default:
1438 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001439 }
1440 return setError(EGL_BAD_PARAMETER, (const char *)0);
1441}
1442
Jiyong Park00b15b82017-08-10 20:30:56 +09001443extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001444{
1445 clearError();
1446
1447 const egl_display_ptr dp = validate_display(dpy);
1448 if (!dp) return (const char *) NULL;
1449
1450 switch (name) {
1451 case EGL_VENDOR:
1452 return dp->disp.queryString.vendor;
1453 case EGL_VERSION:
1454 return dp->disp.queryString.version;
1455 case EGL_EXTENSIONS:
1456 return dp->disp.queryString.extensions;
1457 case EGL_CLIENT_APIS:
1458 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001459 default:
1460 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001461 }
1462 return setError(EGL_BAD_PARAMETER, (const char *)0);
1463}
Mathias Agopian518ec112011-05-13 16:21:08 -07001464
1465// ----------------------------------------------------------------------------
1466// EGL 1.1
1467// ----------------------------------------------------------------------------
1468
1469EGLBoolean eglSurfaceAttrib(
1470 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1471{
1472 clearError();
1473
Jesse Hallb29e5e82012-04-04 16:53:42 -07001474 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001475 if (!dp) return EGL_FALSE;
1476
Jesse Hallb29e5e82012-04-04 16:53:42 -07001477 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001478 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001479 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001480
Pablo Ceballosc18be292016-05-31 14:55:42 -07001481 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001482
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001483 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001484 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001485 setError(EGL_BAD_SURFACE, EGL_FALSE);
1486 }
Mathias Agopian65421432017-03-08 11:49:05 -08001487 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1488 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001489 }
1490
Pablo Ceballosc18be292016-05-31 14:55:42 -07001491 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001492 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001493 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001494 }
Mathias Agopian65421432017-03-08 11:49:05 -08001495 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1496 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001497 }
1498
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001499 if (s->setSmpte2086Attribute(attribute, value)) {
1500 return EGL_TRUE;
1501 } else if (s->setCta8613Attribute(attribute, value)) {
1502 return EGL_TRUE;
1503 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001504 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001505 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001506 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001507 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001508}
1509
1510EGLBoolean eglBindTexImage(
1511 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1512{
1513 clearError();
1514
Jesse Hallb29e5e82012-04-04 16:53:42 -07001515 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 if (!dp) return EGL_FALSE;
1517
Jesse Hallb29e5e82012-04-04 16:53:42 -07001518 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001519 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001520 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001521
Mathias Agopian518ec112011-05-13 16:21:08 -07001522 egl_surface_t const * const s = get_surface(surface);
1523 if (s->cnx->egl.eglBindTexImage) {
1524 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001525 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001526 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001527 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001528}
1529
1530EGLBoolean eglReleaseTexImage(
1531 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1532{
1533 clearError();
1534
Jesse Hallb29e5e82012-04-04 16:53:42 -07001535 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001536 if (!dp) return EGL_FALSE;
1537
Jesse Hallb29e5e82012-04-04 16:53:42 -07001538 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001539 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001540 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001541
Mathias Agopian518ec112011-05-13 16:21:08 -07001542 egl_surface_t const * const s = get_surface(surface);
1543 if (s->cnx->egl.eglReleaseTexImage) {
1544 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001545 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001546 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001547 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001548}
1549
1550EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1551{
1552 clearError();
1553
Jesse Hallb29e5e82012-04-04 16:53:42 -07001554 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001555 if (!dp) return EGL_FALSE;
1556
1557 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001558 egl_connection_t* const cnx = &gEGLImpl;
1559 if (cnx->dso && cnx->egl.eglSwapInterval) {
1560 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001561 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001562
Mathias Agopian518ec112011-05-13 16:21:08 -07001563 return res;
1564}
1565
1566
1567// ----------------------------------------------------------------------------
1568// EGL 1.2
1569// ----------------------------------------------------------------------------
1570
1571EGLBoolean eglWaitClient(void)
1572{
1573 clearError();
1574
Mathias Agopianada798b2012-02-13 17:09:30 -08001575 egl_connection_t* const cnx = &gEGLImpl;
1576 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001577 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001578
1579 EGLBoolean res;
1580 if (cnx->egl.eglWaitClient) {
1581 res = cnx->egl.eglWaitClient();
1582 } else {
1583 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001584 }
1585 return res;
1586}
1587
1588EGLBoolean eglBindAPI(EGLenum api)
1589{
1590 clearError();
1591
1592 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001593 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001594 }
1595
1596 // bind this API on all EGLs
1597 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001598 egl_connection_t* const cnx = &gEGLImpl;
1599 if (cnx->dso && cnx->egl.eglBindAPI) {
1600 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001601 }
1602 return res;
1603}
1604
1605EGLenum eglQueryAPI(void)
1606{
1607 clearError();
1608
1609 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001610 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001611 }
1612
Mathias Agopianada798b2012-02-13 17:09:30 -08001613 egl_connection_t* const cnx = &gEGLImpl;
1614 if (cnx->dso && cnx->egl.eglQueryAPI) {
1615 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001616 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001617
Mathias Agopian518ec112011-05-13 16:21:08 -07001618 // or, it can only be OpenGL ES
1619 return EGL_OPENGL_ES_API;
1620}
1621
1622EGLBoolean eglReleaseThread(void)
1623{
1624 clearError();
1625
Mathias Agopianada798b2012-02-13 17:09:30 -08001626 egl_connection_t* const cnx = &gEGLImpl;
1627 if (cnx->dso && cnx->egl.eglReleaseThread) {
1628 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001629 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301630
1631 // If there is context bound to the thread, release it
1632 egl_display_t::loseCurrent(get_context(getContext()));
1633
Mathias Agopian518ec112011-05-13 16:21:08 -07001634 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001635 return EGL_TRUE;
1636}
1637
1638EGLSurface eglCreatePbufferFromClientBuffer(
1639 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1640 EGLConfig config, const EGLint *attrib_list)
1641{
1642 clearError();
1643
Jesse Hallb29e5e82012-04-04 16:53:42 -07001644 egl_connection_t* cnx = NULL;
1645 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1646 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001647 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1648 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001649 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001650 }
1651 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1652}
1653
1654// ----------------------------------------------------------------------------
1655// EGL_EGLEXT_VERSION 3
1656// ----------------------------------------------------------------------------
1657
1658EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1659 const EGLint *attrib_list)
1660{
1661 clearError();
1662
Jesse Hallb29e5e82012-04-04 16:53:42 -07001663 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001664 if (!dp) return EGL_FALSE;
1665
Jesse Hallb29e5e82012-04-04 16:53:42 -07001666 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001667 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001668 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001669
1670 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001671 if (s->cnx->egl.eglLockSurfaceKHR) {
1672 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001673 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001674 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001675 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001676}
1677
1678EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1679{
1680 clearError();
1681
Jesse Hallb29e5e82012-04-04 16:53:42 -07001682 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001683 if (!dp) return EGL_FALSE;
1684
Jesse Hallb29e5e82012-04-04 16:53:42 -07001685 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001686 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001687 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001688
1689 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001690 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001691 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001692 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001693 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001694}
1695
1696EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1697 EGLClientBuffer buffer, const EGLint *attrib_list)
1698{
1699 clearError();
1700
Jesse Hallb29e5e82012-04-04 16:53:42 -07001701 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001702 if (!dp) return EGL_NO_IMAGE_KHR;
1703
Jesse Hallb29e5e82012-04-04 16:53:42 -07001704 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001705 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001706
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001707 // Temporary hack: eglImageCreateKHR should accept EGL_GL_COLORSPACE_LINEAR_KHR,
1708 // EGL_GL_COLORSPACE_SRGB_KHR and EGL_GL_COLORSPACE_DEFAULT_EXT if
1709 // EGL_EXT_image_gl_colorspace is supported, but some drivers don't like
1710 // the DEFAULT value and generate an error.
1711 std::vector<EGLint> strippedAttribList;
1712 for (const EGLint *attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
1713 if (attr[0] == EGL_GL_COLORSPACE_KHR &&
1714 dp->haveExtension("EGL_EXT_image_gl_colorspace")) {
1715 if (attr[1] != EGL_GL_COLORSPACE_LINEAR_KHR &&
1716 attr[1] != EGL_GL_COLORSPACE_SRGB_KHR) {
1717 continue;
1718 }
1719 }
1720 strippedAttribList.push_back(attr[0]);
1721 strippedAttribList.push_back(attr[1]);
1722 }
1723 strippedAttribList.push_back(EGL_NONE);
1724
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001725 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1726 egl_connection_t* const cnx = &gEGLImpl;
1727 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1728 result = cnx->egl.eglCreateImageKHR(
1729 dp->disp.dpy,
1730 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001731 target, buffer, strippedAttribList.data());
Mathias Agopian518ec112011-05-13 16:21:08 -07001732 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001733 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001734}
1735
1736EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1737{
1738 clearError();
1739
Jesse Hallb29e5e82012-04-04 16:53:42 -07001740 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001741 if (!dp) return EGL_FALSE;
1742
Steven Holte646a5c52012-06-04 20:02:11 -07001743 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001744 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001745 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001746 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001747 }
Steven Holte646a5c52012-06-04 20:02:11 -07001748 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001749}
1750
1751// ----------------------------------------------------------------------------
1752// EGL_EGLEXT_VERSION 5
1753// ----------------------------------------------------------------------------
1754
1755
1756EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1757{
1758 clearError();
1759
Jesse Hallb29e5e82012-04-04 16:53:42 -07001760 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001761 if (!dp) return EGL_NO_SYNC_KHR;
1762
Mathias Agopian518ec112011-05-13 16:21:08 -07001763 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001764 egl_connection_t* const cnx = &gEGLImpl;
1765 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1766 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001767 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001768 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001769}
1770
1771EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1772{
1773 clearError();
1774
Jesse Hallb29e5e82012-04-04 16:53:42 -07001775 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001776 if (!dp) return EGL_FALSE;
1777
Mathias Agopian518ec112011-05-13 16:21:08 -07001778 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001779 egl_connection_t* const cnx = &gEGLImpl;
1780 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1781 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001782 }
1783 return result;
1784}
1785
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001786EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1787 clearError();
1788
1789 const egl_display_ptr dp = validate_display(dpy);
1790 if (!dp) return EGL_FALSE;
1791
1792 EGLBoolean result = EGL_FALSE;
1793 egl_connection_t* const cnx = &gEGLImpl;
1794 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1795 result = cnx->egl.eglSignalSyncKHR(
1796 dp->disp.dpy, sync, mode);
1797 }
1798 return result;
1799}
1800
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001801EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1802 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001803{
1804 clearError();
1805
Jesse Hallb29e5e82012-04-04 16:53:42 -07001806 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001807 if (!dp) return EGL_FALSE;
1808
Mathias Agopian737b8962017-02-24 14:32:05 -08001809 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001810 egl_connection_t* const cnx = &gEGLImpl;
1811 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1812 result = cnx->egl.eglClientWaitSyncKHR(
1813 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001814 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001815 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001816}
1817
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001818EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1819 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001820{
1821 clearError();
1822
Jesse Hallb29e5e82012-04-04 16:53:42 -07001823 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001824 if (!dp) return EGL_FALSE;
1825
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001826 EGLBoolean result = EGL_FALSE;
1827 egl_connection_t* const cnx = &gEGLImpl;
1828 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1829 result = cnx->egl.eglGetSyncAttribKHR(
1830 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001831 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001832 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001833}
1834
Season Li000d88f2015-07-01 11:39:40 -07001835EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1836{
1837 clearError();
1838
1839 const egl_display_ptr dp = validate_display(dpy);
1840 if (!dp) return EGL_NO_STREAM_KHR;
1841
1842 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1843 egl_connection_t* const cnx = &gEGLImpl;
1844 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1845 result = cnx->egl.eglCreateStreamKHR(
1846 dp->disp.dpy, attrib_list);
1847 }
1848 return result;
1849}
1850
1851EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1852{
1853 clearError();
1854
1855 const egl_display_ptr dp = validate_display(dpy);
1856 if (!dp) return EGL_FALSE;
1857
1858 EGLBoolean result = EGL_FALSE;
1859 egl_connection_t* const cnx = &gEGLImpl;
1860 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1861 result = cnx->egl.eglDestroyStreamKHR(
1862 dp->disp.dpy, stream);
1863 }
1864 return result;
1865}
1866
1867EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1868 EGLenum attribute, EGLint value)
1869{
1870 clearError();
1871
1872 const egl_display_ptr dp = validate_display(dpy);
1873 if (!dp) return EGL_FALSE;
1874
1875 EGLBoolean result = EGL_FALSE;
1876 egl_connection_t* const cnx = &gEGLImpl;
1877 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1878 result = cnx->egl.eglStreamAttribKHR(
1879 dp->disp.dpy, stream, attribute, value);
1880 }
1881 return result;
1882}
1883
1884EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1885 EGLenum attribute, EGLint *value)
1886{
1887 clearError();
1888
1889 const egl_display_ptr dp = validate_display(dpy);
1890 if (!dp) return EGL_FALSE;
1891
1892 EGLBoolean result = EGL_FALSE;
1893 egl_connection_t* const cnx = &gEGLImpl;
1894 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1895 result = cnx->egl.eglQueryStreamKHR(
1896 dp->disp.dpy, stream, attribute, value);
1897 }
1898 return result;
1899}
1900
1901EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1902 EGLenum attribute, EGLuint64KHR *value)
1903{
1904 clearError();
1905
1906 const egl_display_ptr dp = validate_display(dpy);
1907 if (!dp) return EGL_FALSE;
1908
1909 EGLBoolean result = EGL_FALSE;
1910 egl_connection_t* const cnx = &gEGLImpl;
1911 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1912 result = cnx->egl.eglQueryStreamu64KHR(
1913 dp->disp.dpy, stream, attribute, value);
1914 }
1915 return result;
1916}
1917
1918EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1919 EGLenum attribute, EGLTimeKHR *value)
1920{
1921 clearError();
1922
1923 const egl_display_ptr dp = validate_display(dpy);
1924 if (!dp) return EGL_FALSE;
1925
1926 EGLBoolean result = EGL_FALSE;
1927 egl_connection_t* const cnx = &gEGLImpl;
1928 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1929 result = cnx->egl.eglQueryStreamTimeKHR(
1930 dp->disp.dpy, stream, attribute, value);
1931 }
1932 return result;
1933}
1934
1935EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1936 EGLStreamKHR stream, const EGLint *attrib_list)
1937{
1938 clearError();
1939
1940 egl_display_ptr dp = validate_display(dpy);
1941 if (!dp) return EGL_NO_SURFACE;
1942
1943 egl_connection_t* const cnx = &gEGLImpl;
1944 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1945 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1946 dp->disp.dpy, config, stream, attrib_list);
1947 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001948 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
1949 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001950 return s;
1951 }
1952 }
1953 return EGL_NO_SURFACE;
1954}
1955
1956EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1957 EGLStreamKHR stream)
1958{
1959 clearError();
1960
1961 const egl_display_ptr dp = validate_display(dpy);
1962 if (!dp) return EGL_FALSE;
1963
1964 EGLBoolean result = EGL_FALSE;
1965 egl_connection_t* const cnx = &gEGLImpl;
1966 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1967 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1968 dp->disp.dpy, stream);
1969 }
1970 return result;
1971}
1972
1973EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1974 EGLStreamKHR stream)
1975{
1976 clearError();
1977
1978 const egl_display_ptr dp = validate_display(dpy);
1979 if (!dp) return EGL_FALSE;
1980
1981 EGLBoolean result = EGL_FALSE;
1982 egl_connection_t* const cnx = &gEGLImpl;
1983 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1984 result = cnx->egl.eglStreamConsumerAcquireKHR(
1985 dp->disp.dpy, stream);
1986 }
1987 return result;
1988}
1989
1990EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1991 EGLStreamKHR stream)
1992{
1993 clearError();
1994
1995 const egl_display_ptr dp = validate_display(dpy);
1996 if (!dp) return EGL_FALSE;
1997
1998 EGLBoolean result = EGL_FALSE;
1999 egl_connection_t* const cnx = &gEGLImpl;
2000 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2001 result = cnx->egl.eglStreamConsumerReleaseKHR(
2002 dp->disp.dpy, stream);
2003 }
2004 return result;
2005}
2006
2007EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2008 EGLDisplay dpy, EGLStreamKHR stream)
2009{
2010 clearError();
2011
2012 const egl_display_ptr dp = validate_display(dpy);
2013 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2014
2015 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2016 egl_connection_t* const cnx = &gEGLImpl;
2017 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2018 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2019 dp->disp.dpy, stream);
2020 }
2021 return result;
2022}
2023
2024EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2025 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2026{
2027 clearError();
2028
2029 const egl_display_ptr dp = validate_display(dpy);
2030 if (!dp) return EGL_NO_STREAM_KHR;
2031
2032 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2033 egl_connection_t* const cnx = &gEGLImpl;
2034 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2035 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2036 dp->disp.dpy, file_descriptor);
2037 }
2038 return result;
2039}
2040
Mathias Agopian518ec112011-05-13 16:21:08 -07002041// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002042// EGL_EGLEXT_VERSION 15
2043// ----------------------------------------------------------------------------
2044
2045EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2046 clearError();
2047 const egl_display_ptr dp = validate_display(dpy);
2048 if (!dp) return EGL_FALSE;
2049 EGLint result = EGL_FALSE;
2050 egl_connection_t* const cnx = &gEGLImpl;
2051 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2052 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2053 }
2054 return result;
2055}
2056
2057// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002058// ANDROID extensions
2059// ----------------------------------------------------------------------------
2060
Jamie Gennis331841b2012-09-06 14:52:00 -07002061EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2062{
2063 clearError();
2064
2065 const egl_display_ptr dp = validate_display(dpy);
2066 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2067
2068 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2069 egl_connection_t* const cnx = &gEGLImpl;
2070 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2071 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2072 }
2073 return result;
2074}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002075
Andy McFadden72841452013-03-01 16:25:32 -08002076EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2077 EGLnsecsANDROID time)
2078{
2079 clearError();
2080
2081 const egl_display_ptr dp = validate_display(dpy);
2082 if (!dp) {
2083 return EGL_FALSE;
2084 }
2085
2086 SurfaceRef _s(dp.get(), surface);
2087 if (!_s.get()) {
2088 setError(EGL_BAD_SURFACE, EGL_FALSE);
2089 return EGL_FALSE;
2090 }
2091
2092 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002093 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002094
2095 return EGL_TRUE;
2096}
2097
Craig Donner60761072017-01-27 12:30:44 -08002098EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2099 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002100 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2101 // this function cannot be implemented when this libEGL is built for
2102 // vendors.
2103#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002104 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002105 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002106#else
2107 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2108#endif
Craig Donner60761072017-01-27 12:30:44 -08002109}
2110
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002111// ----------------------------------------------------------------------------
2112// NVIDIA extensions
2113// ----------------------------------------------------------------------------
2114EGLuint64NV eglGetSystemTimeFrequencyNV()
2115{
2116 clearError();
2117
2118 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002119 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002120 }
2121
2122 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002123 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002124
Mathias Agopianada798b2012-02-13 17:09:30 -08002125 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2126 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002127 }
2128
Mathias Agopian737b8962017-02-24 14:32:05 -08002129 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002130}
2131
2132EGLuint64NV eglGetSystemTimeNV()
2133{
2134 clearError();
2135
2136 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002137 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002138 }
2139
2140 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002141 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002142
Mathias Agopianada798b2012-02-13 17:09:30 -08002143 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2144 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002145 }
2146
Mathias Agopian737b8962017-02-24 14:32:05 -08002147 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002148}
Dan Stozaa894d082015-02-19 15:27:36 -08002149
2150// ----------------------------------------------------------------------------
2151// Partial update extension
2152// ----------------------------------------------------------------------------
2153EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2154 EGLint *rects, EGLint n_rects)
2155{
2156 clearError();
2157
2158 const egl_display_ptr dp = validate_display(dpy);
2159 if (!dp) {
2160 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2161 return EGL_FALSE;
2162 }
2163
2164 SurfaceRef _s(dp.get(), surface);
2165 if (!_s.get()) {
2166 setError(EGL_BAD_SURFACE, EGL_FALSE);
2167 return EGL_FALSE;
2168 }
2169
2170 egl_surface_t const * const s = get_surface(surface);
2171 if (s->cnx->egl.eglSetDamageRegionKHR) {
2172 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2173 rects, n_rects);
2174 }
2175
2176 return EGL_FALSE;
2177}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002178
Brian Anderson1049d1d2016-12-16 17:25:57 -08002179EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2180 EGLuint64KHR *frameId) {
2181 clearError();
2182
2183 const egl_display_ptr dp = validate_display(dpy);
2184 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002185 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002186 }
2187
2188 SurfaceRef _s(dp.get(), surface);
2189 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002190 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002191 }
2192
2193 egl_surface_t const * const s = get_surface(surface);
2194
Mathias Agopian65421432017-03-08 11:49:05 -08002195 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002196 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002197 }
2198
2199 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002200 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002201
Mathias Agopian65421432017-03-08 11:49:05 -08002202 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002203 // This should not happen. Return an error that is not in the spec
2204 // so it's obvious something is very wrong.
2205 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002206 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002207 }
2208
2209 *frameId = nextFrameId;
2210 return EGL_TRUE;
2211}
2212
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002213EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2214 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2215{
2216 clearError();
2217
2218 const egl_display_ptr dp = validate_display(dpy);
2219 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002220 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002221 }
2222
2223 SurfaceRef _s(dp.get(), surface);
2224 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002225 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002226 }
2227
2228 egl_surface_t const * const s = get_surface(surface);
2229
Mathias Agopian65421432017-03-08 11:49:05 -08002230 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002231 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002232 }
2233
2234 nsecs_t* compositeDeadline = nullptr;
2235 nsecs_t* compositeInterval = nullptr;
2236 nsecs_t* compositeToPresentLatency = nullptr;
2237
2238 for (int i = 0; i < numTimestamps; i++) {
2239 switch (names[i]) {
2240 case EGL_COMPOSITE_DEADLINE_ANDROID:
2241 compositeDeadline = &values[i];
2242 break;
2243 case EGL_COMPOSITE_INTERVAL_ANDROID:
2244 compositeInterval = &values[i];
2245 break;
2246 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2247 compositeToPresentLatency = &values[i];
2248 break;
2249 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002250 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002251 }
2252 }
2253
Mathias Agopian65421432017-03-08 11:49:05 -08002254 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002255 compositeDeadline, compositeInterval, compositeToPresentLatency);
2256
2257 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002258 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002259 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002260 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002261 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002262 default:
2263 // This should not happen. Return an error that is not in the spec
2264 // so it's obvious something is very wrong.
2265 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002266 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002267 }
2268}
2269
2270EGLBoolean eglGetCompositorTimingSupportedANDROID(
2271 EGLDisplay dpy, EGLSurface surface, EGLint name)
2272{
2273 clearError();
2274
2275 const egl_display_ptr dp = validate_display(dpy);
2276 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002277 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002278 }
2279
2280 SurfaceRef _s(dp.get(), surface);
2281 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002282 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002283 }
2284
2285 egl_surface_t const * const s = get_surface(surface);
2286
Mathias Agopian65421432017-03-08 11:49:05 -08002287 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002288 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002289 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002290 }
2291
2292 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002293 case EGL_COMPOSITE_DEADLINE_ANDROID:
2294 case EGL_COMPOSITE_INTERVAL_ANDROID:
2295 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2296 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002297 default:
2298 return EGL_FALSE;
2299 }
2300}
2301
Pablo Ceballosc18be292016-05-31 14:55:42 -07002302EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002303 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002304 EGLnsecsANDROID *values)
2305{
2306 clearError();
2307
2308 const egl_display_ptr dp = validate_display(dpy);
2309 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002310 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002311 }
2312
2313 SurfaceRef _s(dp.get(), surface);
2314 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002315 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002316 }
2317
2318 egl_surface_t const * const s = get_surface(surface);
2319
Mathias Agopian65421432017-03-08 11:49:05 -08002320 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002321 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002322 }
2323
Brian Andersondbd0ea82016-07-22 09:38:59 -07002324 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002325 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002326 nsecs_t* latchTime = nullptr;
2327 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002328 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002329 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002330 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002331 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002332 nsecs_t* releaseTime = nullptr;
2333
2334 for (int i = 0; i < numTimestamps; i++) {
2335 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002336 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2337 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002338 break;
2339 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2340 acquireTime = &values[i];
2341 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002342 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2343 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002344 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002345 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2346 firstRefreshStartTime = &values[i];
2347 break;
2348 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2349 lastRefreshStartTime = &values[i];
2350 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002351 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2352 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002353 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002354 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2355 displayPresentTime = &values[i];
2356 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002357 case EGL_DEQUEUE_READY_TIME_ANDROID:
2358 dequeueReadyTime = &values[i];
2359 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002360 case EGL_READS_DONE_TIME_ANDROID:
2361 releaseTime = &values[i];
2362 break;
2363 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002364 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002365 }
2366 }
2367
Mathias Agopian65421432017-03-08 11:49:05 -08002368 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002369 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002370 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002371 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002372
Brian Anderson069b3652016-07-22 10:32:47 -07002373 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002374 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002375 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002376 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002377 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002378 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002379 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002380 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002381 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2382 default:
2383 // This should not happen. Return an error that is not in the spec
2384 // so it's obvious something is very wrong.
2385 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2386 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002387 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002388}
2389
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002390EGLBoolean eglGetFrameTimestampSupportedANDROID(
2391 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002392{
2393 clearError();
2394
2395 const egl_display_ptr dp = validate_display(dpy);
2396 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002397 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002398 }
2399
2400 SurfaceRef _s(dp.get(), surface);
2401 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002402 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002403 }
2404
2405 egl_surface_t const * const s = get_surface(surface);
2406
Mathias Agopian65421432017-03-08 11:49:05 -08002407 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002408 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002409 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002410 }
2411
2412 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002413 case EGL_COMPOSITE_DEADLINE_ANDROID:
2414 case EGL_COMPOSITE_INTERVAL_ANDROID:
2415 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002416 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002417 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002418 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2419 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2420 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002421 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002422 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002423 case EGL_READS_DONE_TIME_ANDROID:
2424 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002425 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2426 int value = 0;
2427 window->query(window,
2428 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2429 return value == 0 ? EGL_FALSE : EGL_TRUE;
2430 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002431 default:
2432 return EGL_FALSE;
2433 }
2434}