blob: 087c1c1f399afe6a27bb8cabcf9be8731f9553e9 [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 Liuab4f07e2018-09-27 15:47:29 -0700723 return EGL_NO_SURFACE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700724 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700725 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800726
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700727 {
Jesse Hallc2e41222013-08-08 13:40:22 -0700728 int err = native_window_set_buffers_format(window, format);
729 if (err != 0) {
730 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700731 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700732 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
733 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
734 }
735 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700736
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700737 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
738 if (dataSpace != HAL_DATASPACE_UNKNOWN) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800739 int err = native_window_set_buffers_data_space(window, dataSpace);
740 if (err != 0) {
741 ALOGE("error setting native window pixel dataSpace: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700742 strerror(-err), err);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800743 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
744 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
745 }
746 }
747
Jamie Gennis59769462011-11-19 18:04:43 -0800748 // the EGL spec requires that a new EGLSurface default to swap interval
749 // 1, so explicitly set that on the window here.
750 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
751 anw->setSwapInterval(anw, 1);
752
Mathias Agopian518ec112011-05-13 16:21:08 -0700753 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800754 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700755 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600756 egl_surface_t* s =
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700757 new egl_surface_t(dp.get(), config, window, surface,
758 getReportedColorSpace(colorSpace), cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700759 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700760 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700761
762 // EGLSurface creation failed
763 native_window_set_buffers_format(window, 0);
764 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700765 }
766 return EGL_NO_SURFACE;
767}
768
769EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
770 NativePixmapType pixmap,
771 const EGLint *attrib_list)
772{
773 clearError();
774
Jesse Hallb29e5e82012-04-04 16:53:42 -0700775 egl_connection_t* cnx = NULL;
776 egl_display_ptr dp = validate_display_connection(dpy, cnx);
777 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700778 EGLDisplay iDpy = dp->disp.dpy;
779 android_pixel_format format;
780 getNativePixelFormat(iDpy, cnx, config, &format);
781
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600782 // now select a corresponding sRGB format if needed
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700783 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700784 std::vector<EGLint> strippedAttribList;
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700785 if (!processAttributes(dp, nullptr, attrib_list, &colorSpace,
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700786 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600787 ALOGE("error invalid colorspace: %d", colorSpace);
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700788 return EGL_NO_SURFACE;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600789 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700790 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600791
Mathias Agopian518ec112011-05-13 16:21:08 -0700792 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800793 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700794 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700795 egl_surface_t* s =
796 new egl_surface_t(dp.get(), config, NULL, surface,
797 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 return s;
799 }
800 }
801 return EGL_NO_SURFACE;
802}
803
804EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
805 const EGLint *attrib_list)
806{
807 clearError();
808
Jesse Hallb29e5e82012-04-04 16:53:42 -0700809 egl_connection_t* cnx = NULL;
810 egl_display_ptr dp = validate_display_connection(dpy, cnx);
811 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600812 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700813 android_pixel_format format;
814 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600815
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700816 // Select correct colorspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700817 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700818 std::vector<EGLint> strippedAttribList;
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700819 if (!processAttributes(dp, nullptr, attrib_list, &colorSpace,
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700820 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600821 ALOGE("error invalid colorspace: %d", colorSpace);
Weiwan Liuab4f07e2018-09-27 15:47:29 -0700822 return EGL_NO_SURFACE;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600823 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700824 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600825
Mathias Agopian518ec112011-05-13 16:21:08 -0700826 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800827 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700828 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700829 egl_surface_t* s =
830 new egl_surface_t(dp.get(), config, NULL, surface,
831 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700832 return s;
833 }
834 }
835 return EGL_NO_SURFACE;
836}
Jesse Hall47743382013-02-08 11:13:46 -0800837
Mathias Agopian518ec112011-05-13 16:21:08 -0700838EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
839{
840 clearError();
841
Jesse Hallb29e5e82012-04-04 16:53:42 -0700842 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700843 if (!dp) return EGL_FALSE;
844
Jesse Hallb29e5e82012-04-04 16:53:42 -0700845 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700846 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800847 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700848
849 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800850 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700851 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700852 _s.terminate();
853 }
854 return result;
855}
856
857EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
858 EGLint attribute, EGLint *value)
859{
860 clearError();
861
Jesse Hallb29e5e82012-04-04 16:53:42 -0700862 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700863 if (!dp) return EGL_FALSE;
864
Jesse Hallb29e5e82012-04-04 16:53:42 -0700865 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700866 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800867 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700868
Mathias Agopian518ec112011-05-13 16:21:08 -0700869 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700870 if (s->getColorSpaceAttribute(attribute, value)) {
871 return EGL_TRUE;
872 } else if (s->getSmpte2086Attribute(attribute, value)) {
873 return EGL_TRUE;
874 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600875 return EGL_TRUE;
876 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700877 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700878}
879
Jamie Gennise8696a42012-01-15 18:54:57 -0800880void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800881 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800882 clearError();
883
Jesse Hallb29e5e82012-04-04 16:53:42 -0700884 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800885 if (!dp) {
886 return;
887 }
888
Jesse Hallb29e5e82012-04-04 16:53:42 -0700889 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800890 if (!_s.get()) {
891 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800892 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800893}
894
Mathias Agopian518ec112011-05-13 16:21:08 -0700895// ----------------------------------------------------------------------------
896// Contexts
897// ----------------------------------------------------------------------------
898
899EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
900 EGLContext share_list, const EGLint *attrib_list)
901{
902 clearError();
903
Jesse Hallb29e5e82012-04-04 16:53:42 -0700904 egl_connection_t* cnx = NULL;
905 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700906 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700907 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700908 if (!ContextRef(dp.get(), share_list).get()) {
909 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
910 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700911 egl_context_t* const c = get_context(share_list);
912 share_list = c->context;
913 }
914 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800915 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700916 if (context != EGL_NO_CONTEXT) {
917 // figure out if it's a GLESv1 or GLESv2
918 int version = 0;
919 if (attrib_list) {
920 while (*attrib_list != EGL_NONE) {
921 GLint attr = *attrib_list++;
922 GLint value = *attrib_list++;
923 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
924 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800925 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800926 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800927 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700928 }
929 }
930 };
931 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700932 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
933 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700934 return c;
935 }
936 }
937 return EGL_NO_CONTEXT;
938}
939
940EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
941{
942 clearError();
943
Jesse Hallb29e5e82012-04-04 16:53:42 -0700944 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700945 if (!dp)
946 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700947
Jesse Hallb29e5e82012-04-04 16:53:42 -0700948 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700949 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800950 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800951
Mathias Agopian518ec112011-05-13 16:21:08 -0700952 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800953 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700954 if (result == EGL_TRUE) {
955 _c.terminate();
956 }
957 return result;
958}
959
Mathias Agopian518ec112011-05-13 16:21:08 -0700960EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
961 EGLSurface read, EGLContext ctx)
962{
963 clearError();
964
Jesse Hallb29e5e82012-04-04 16:53:42 -0700965 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800966 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700967
Mathias Agopian5b287a62011-05-16 18:58:55 -0700968 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
969 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
970 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700971 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
972 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800973 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700974 }
975
976 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700977 ContextRef _c(dp.get(), ctx);
978 SurfaceRef _d(dp.get(), draw);
979 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700980
981 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700982 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800984 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700985 }
986
987 // these are the underlying implementation's object
988 EGLContext impl_ctx = EGL_NO_CONTEXT;
989 EGLSurface impl_draw = EGL_NO_SURFACE;
990 EGLSurface impl_read = EGL_NO_SURFACE;
991
992 // these are our objects structs passed in
993 egl_context_t * c = NULL;
994 egl_surface_t const * d = NULL;
995 egl_surface_t const * r = NULL;
996
997 // these are the current objects structs
998 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800999
Mathias Agopian518ec112011-05-13 16:21:08 -07001000 if (ctx != EGL_NO_CONTEXT) {
1001 c = get_context(ctx);
1002 impl_ctx = c->context;
1003 } else {
1004 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001005 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1006 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001007 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001008 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001009 if (cur_c == NULL) {
1010 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001011 // not an error, there is just no current context.
1012 return EGL_TRUE;
1013 }
1014 }
1015
1016 // retrieve the underlying implementation's draw EGLSurface
1017 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001018 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001020 impl_draw = d->surface;
1021 }
1022
1023 // retrieve the underlying implementation's read EGLSurface
1024 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001025 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001026 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001027 impl_read = r->surface;
1028 }
1029
Mathias Agopian518ec112011-05-13 16:21:08 -07001030
Jesse Hallb29e5e82012-04-04 16:53:42 -07001031 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001032 draw, read, ctx,
1033 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001034
1035 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001036 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001037 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1038 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001039 _c.acquire();
1040 _r.acquire();
1041 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001042 } else {
1043 setGLHooksThreadSpecific(&gHooksNoContext);
1044 egl_tls_t::setContext(EGL_NO_CONTEXT);
1045 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001046 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001047 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001048 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001049 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001050 }
1051 return result;
1052}
1053
1054
1055EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1056 EGLint attribute, EGLint *value)
1057{
1058 clearError();
1059
Jesse Hallb29e5e82012-04-04 16:53:42 -07001060 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001061 if (!dp) return EGL_FALSE;
1062
Jesse Hallb29e5e82012-04-04 16:53:42 -07001063 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001064 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001065
Mathias Agopian518ec112011-05-13 16:21:08 -07001066 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001067 return c->cnx->egl.eglQueryContext(
1068 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001069
Mathias Agopian518ec112011-05-13 16:21:08 -07001070}
1071
1072EGLContext eglGetCurrentContext(void)
1073{
1074 // could be called before eglInitialize(), but we wouldn't have a context
1075 // then, and this function would correctly return EGL_NO_CONTEXT.
1076
1077 clearError();
1078
1079 EGLContext ctx = getContext();
1080 return ctx;
1081}
1082
1083EGLSurface eglGetCurrentSurface(EGLint readdraw)
1084{
1085 // could be called before eglInitialize(), but we wouldn't have a context
1086 // then, and this function would correctly return EGL_NO_SURFACE.
1087
1088 clearError();
1089
1090 EGLContext ctx = getContext();
1091 if (ctx) {
1092 egl_context_t const * const c = get_context(ctx);
1093 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1094 switch (readdraw) {
1095 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001096 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001097 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1098 }
1099 }
1100 return EGL_NO_SURFACE;
1101}
1102
1103EGLDisplay eglGetCurrentDisplay(void)
1104{
1105 // could be called before eglInitialize(), but we wouldn't have a context
1106 // then, and this function would correctly return EGL_NO_DISPLAY.
1107
1108 clearError();
1109
1110 EGLContext ctx = getContext();
1111 if (ctx) {
1112 egl_context_t const * const c = get_context(ctx);
1113 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1114 return c->dpy;
1115 }
1116 return EGL_NO_DISPLAY;
1117}
1118
1119EGLBoolean eglWaitGL(void)
1120{
Mathias Agopian518ec112011-05-13 16:21:08 -07001121 clearError();
1122
Mathias Agopianada798b2012-02-13 17:09:30 -08001123 egl_connection_t* const cnx = &gEGLImpl;
1124 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001125 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001126
1127 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001128}
1129
1130EGLBoolean eglWaitNative(EGLint engine)
1131{
Mathias Agopian518ec112011-05-13 16:21:08 -07001132 clearError();
1133
Mathias Agopianada798b2012-02-13 17:09:30 -08001134 egl_connection_t* const cnx = &gEGLImpl;
1135 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001136 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001137
1138 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001139}
1140
1141EGLint eglGetError(void)
1142{
Mathias Agopianada798b2012-02-13 17:09:30 -08001143 EGLint err = EGL_SUCCESS;
1144 egl_connection_t* const cnx = &gEGLImpl;
1145 if (cnx->dso) {
1146 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001147 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001148 if (err == EGL_SUCCESS) {
1149 err = egl_tls_t::getError();
1150 }
1151 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001152}
1153
Michael Chockc0ec5e22014-01-27 08:14:33 -08001154static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001155 const char* procname) {
1156 const egl_connection_t* cnx = &gEGLImpl;
1157 void* proc = NULL;
1158
Michael Chockc0ec5e22014-01-27 08:14:33 -08001159 proc = dlsym(cnx->libEgl, procname);
1160 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1161
Jesse Hallc07b5202013-07-04 12:08:16 -07001162 proc = dlsym(cnx->libGles2, procname);
1163 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1164
1165 proc = dlsym(cnx->libGles1, procname);
1166 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1167
1168 return NULL;
1169}
1170
Mathias Agopian518ec112011-05-13 16:21:08 -07001171__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1172{
1173 // eglGetProcAddress() could be the very first function called
1174 // in which case we must make sure we've initialized ourselves, this
1175 // happens the first time egl_get_display() is called.
1176
1177 clearError();
1178
1179 if (egl_init_drivers() == EGL_FALSE) {
1180 setError(EGL_BAD_PARAMETER, NULL);
1181 return NULL;
1182 }
1183
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001184 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001185 return NULL;
1186 }
1187
Mathias Agopian518ec112011-05-13 16:21:08 -07001188 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001189 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001190 if (addr) return addr;
1191
Michael Chockc0ec5e22014-01-27 08:14:33 -08001192 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001193 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001194
Mathias Agopian518ec112011-05-13 16:21:08 -07001195 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1196 pthread_mutex_lock(&sExtensionMapMutex);
1197
1198 /*
1199 * Since eglGetProcAddress() is not associated to anything, it needs
1200 * to return a function pointer that "works" regardless of what
1201 * the current context is.
1202 *
1203 * For this reason, we return a "forwarder", a small stub that takes
1204 * care of calling the function associated with the context
1205 * currently bound.
1206 *
1207 * We first look for extensions we've already resolved, if we're seeing
1208 * this extension for the first time, we go through all our
1209 * implementations and call eglGetProcAddress() and record the
1210 * result in the appropriate implementation hooks and return the
1211 * address of the forwarder corresponding to that hook set.
1212 *
1213 */
1214
Mathias Agopian65421432017-03-08 11:49:05 -08001215 const std::string name(procname);
1216
1217 auto& extentionMap = sGLExtentionMap;
1218 auto pos = extentionMap.find(name);
1219 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001220 const int slot = sGLExtentionSlot;
1221
Steve Blocke6f43dd2012-01-06 19:20:56 +00001222 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001223 "no more slots for eglGetProcAddress(\"%s\")",
1224 procname);
1225
1226 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1227 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001228
1229 egl_connection_t* const cnx = &gEGLImpl;
1230 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001231 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001232 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001233 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1234 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001235 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001236 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001237 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001238
Mathias Agopian518ec112011-05-13 16:21:08 -07001239 if (found) {
1240 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001241 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001242 sGLExtentionSlot++;
1243 }
1244 }
1245
1246 pthread_mutex_unlock(&sExtensionMapMutex);
1247 return addr;
1248}
1249
Mathias Agopian65421432017-03-08 11:49:05 -08001250class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001251public:
1252
1253 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001254 static FrameCompletionThread thread;
1255
1256 char name[64];
1257
1258 std::lock_guard<std::mutex> lock(thread.mMutex);
1259 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1260 ATRACE_NAME(name);
1261
1262 thread.mQueue.push_back(sync);
1263 thread.mCondition.notify_one();
1264 thread.mFramesQueued++;
1265 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001266 }
1267
1268private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001269
Mathias Agopian65421432017-03-08 11:49:05 -08001270 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1271 std::thread thread(&FrameCompletionThread::loop, this);
1272 thread.detach();
1273 }
1274
1275#pragma clang diagnostic push
1276#pragma clang diagnostic ignored "-Wmissing-noreturn"
1277 void loop() {
1278 while (true) {
1279 threadLoop();
1280 }
1281 }
1282#pragma clang diagnostic pop
1283
1284 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001285 EGLSyncKHR sync;
1286 uint32_t frameNum;
1287 {
Mathias Agopian65421432017-03-08 11:49:05 -08001288 std::unique_lock<std::mutex> lock(mMutex);
1289 while (mQueue.empty()) {
1290 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001291 }
1292 sync = mQueue[0];
1293 frameNum = mFramesCompleted;
1294 }
1295 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1296 {
Mathias Agopian65421432017-03-08 11:49:05 -08001297 char name[64];
1298 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1299 ATRACE_NAME(name);
1300
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001301 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1302 if (result == EGL_FALSE) {
1303 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1304 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1305 ALOGE("FrameCompletion: timeout waiting for fence");
1306 }
1307 eglDestroySyncKHR(dpy, sync);
1308 }
1309 {
Mathias Agopian65421432017-03-08 11:49:05 -08001310 std::lock_guard<std::mutex> lock(mMutex);
1311 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001312 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001313 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001314 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001315 }
1316
1317 uint32_t mFramesQueued;
1318 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001319 std::deque<EGLSyncKHR> mQueue;
1320 std::condition_variable mCondition;
1321 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001322};
1323
Dan Stozaa894d082015-02-19 15:27:36 -08001324EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1325 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001326{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001327 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001328 clearError();
1329
Jesse Hallb29e5e82012-04-04 16:53:42 -07001330 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001331 if (!dp) return EGL_FALSE;
1332
Jesse Hallb29e5e82012-04-04 16:53:42 -07001333 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001334 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001335 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001336
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001337 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001338
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001339 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1340 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1341 if (sync != EGL_NO_SYNC_KHR) {
1342 FrameCompletionThread::queueSync(sync);
1343 }
1344 }
1345
Mathias Agopian7db993a2012-03-25 00:49:46 -07001346 if (CC_UNLIKELY(dp->finishOnSwap)) {
1347 uint32_t pixel;
1348 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1349 if (c) {
1350 // glReadPixels() ensures that the frame is complete
1351 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1352 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1353 }
1354 }
1355
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001356 if (!sendSurfaceMetadata(s)) {
1357 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1358 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1359 }
1360
Dan Stozaa894d082015-02-19 15:27:36 -08001361 if (n_rects == 0) {
1362 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1363 }
1364
Mathias Agopian65421432017-03-08 11:49:05 -08001365 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001366 for (int r = 0; r < n_rects; ++r) {
1367 int offset = r * 4;
1368 int x = rects[offset];
1369 int y = rects[offset + 1];
1370 int width = rects[offset + 2];
1371 int height = rects[offset + 3];
1372 android_native_rect_t androidRect;
1373 androidRect.left = x;
1374 androidRect.top = y + height;
1375 androidRect.right = x + width;
1376 androidRect.bottom = y;
1377 androidRects.push_back(androidRect);
1378 }
Mathias Agopian65421432017-03-08 11:49:05 -08001379 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001380
1381 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1382 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1383 rects, n_rects);
1384 } else {
1385 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1386 }
1387}
1388
1389EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1390{
1391 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001392}
1393
1394EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1395 NativePixmapType target)
1396{
1397 clearError();
1398
Jesse Hallb29e5e82012-04-04 16:53:42 -07001399 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001400 if (!dp) return EGL_FALSE;
1401
Jesse Hallb29e5e82012-04-04 16:53:42 -07001402 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001403 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001404 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001405
Mathias Agopian518ec112011-05-13 16:21:08 -07001406 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001407 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001408}
1409
1410const char* eglQueryString(EGLDisplay dpy, EGLint name)
1411{
1412 clearError();
1413
Chia-I Wue57d1352016-08-15 16:10:02 +08001414 // Generate an error quietly when client extensions (as defined by
1415 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1416 // validate_display to generate the error as validate_display would log
1417 // the error, which can be misleading.
1418 //
1419 // If we want to support EGL_EXT_client_extensions later, we can return
1420 // the client extension string here instead.
1421 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001422 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001423
Jesse Hallb29e5e82012-04-04 16:53:42 -07001424 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 if (!dp) return (const char *) NULL;
1426
1427 switch (name) {
1428 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001429 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001431 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001432 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001433 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001434 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001435 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001436 default:
1437 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001438 }
1439 return setError(EGL_BAD_PARAMETER, (const char *)0);
1440}
1441
Jiyong Park00b15b82017-08-10 20:30:56 +09001442extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001443{
1444 clearError();
1445
1446 const egl_display_ptr dp = validate_display(dpy);
1447 if (!dp) return (const char *) NULL;
1448
1449 switch (name) {
1450 case EGL_VENDOR:
1451 return dp->disp.queryString.vendor;
1452 case EGL_VERSION:
1453 return dp->disp.queryString.version;
1454 case EGL_EXTENSIONS:
1455 return dp->disp.queryString.extensions;
1456 case EGL_CLIENT_APIS:
1457 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001458 default:
1459 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001460 }
1461 return setError(EGL_BAD_PARAMETER, (const char *)0);
1462}
Mathias Agopian518ec112011-05-13 16:21:08 -07001463
1464// ----------------------------------------------------------------------------
1465// EGL 1.1
1466// ----------------------------------------------------------------------------
1467
1468EGLBoolean eglSurfaceAttrib(
1469 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1470{
1471 clearError();
1472
Jesse Hallb29e5e82012-04-04 16:53:42 -07001473 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001474 if (!dp) return EGL_FALSE;
1475
Jesse Hallb29e5e82012-04-04 16:53:42 -07001476 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001477 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001478 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001479
Pablo Ceballosc18be292016-05-31 14:55:42 -07001480 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001481
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001482 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001483 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001484 setError(EGL_BAD_SURFACE, EGL_FALSE);
1485 }
Mathias Agopian65421432017-03-08 11:49:05 -08001486 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1487 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001488 }
1489
Pablo Ceballosc18be292016-05-31 14:55:42 -07001490 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001491 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001492 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001493 }
Mathias Agopian65421432017-03-08 11:49:05 -08001494 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1495 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001496 }
1497
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001498 if (s->setSmpte2086Attribute(attribute, value)) {
1499 return EGL_TRUE;
1500 } else if (s->setCta8613Attribute(attribute, value)) {
1501 return EGL_TRUE;
1502 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001504 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001506 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001507}
1508
1509EGLBoolean eglBindTexImage(
1510 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1511{
1512 clearError();
1513
Jesse Hallb29e5e82012-04-04 16:53:42 -07001514 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001515 if (!dp) return EGL_FALSE;
1516
Jesse Hallb29e5e82012-04-04 16:53:42 -07001517 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001518 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001519 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001520
Mathias Agopian518ec112011-05-13 16:21:08 -07001521 egl_surface_t const * const s = get_surface(surface);
1522 if (s->cnx->egl.eglBindTexImage) {
1523 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001524 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001525 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001526 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001527}
1528
1529EGLBoolean eglReleaseTexImage(
1530 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1531{
1532 clearError();
1533
Jesse Hallb29e5e82012-04-04 16:53:42 -07001534 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001535 if (!dp) return EGL_FALSE;
1536
Jesse Hallb29e5e82012-04-04 16:53:42 -07001537 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001538 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001539 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001540
Mathias Agopian518ec112011-05-13 16:21:08 -07001541 egl_surface_t const * const s = get_surface(surface);
1542 if (s->cnx->egl.eglReleaseTexImage) {
1543 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001544 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001545 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001546 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001547}
1548
1549EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1550{
1551 clearError();
1552
Jesse Hallb29e5e82012-04-04 16:53:42 -07001553 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001554 if (!dp) return EGL_FALSE;
1555
1556 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001557 egl_connection_t* const cnx = &gEGLImpl;
1558 if (cnx->dso && cnx->egl.eglSwapInterval) {
1559 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001560 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001561
Mathias Agopian518ec112011-05-13 16:21:08 -07001562 return res;
1563}
1564
1565
1566// ----------------------------------------------------------------------------
1567// EGL 1.2
1568// ----------------------------------------------------------------------------
1569
1570EGLBoolean eglWaitClient(void)
1571{
1572 clearError();
1573
Mathias Agopianada798b2012-02-13 17:09:30 -08001574 egl_connection_t* const cnx = &gEGLImpl;
1575 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001576 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001577
1578 EGLBoolean res;
1579 if (cnx->egl.eglWaitClient) {
1580 res = cnx->egl.eglWaitClient();
1581 } else {
1582 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001583 }
1584 return res;
1585}
1586
1587EGLBoolean eglBindAPI(EGLenum api)
1588{
1589 clearError();
1590
1591 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001592 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001593 }
1594
1595 // bind this API on all EGLs
1596 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001597 egl_connection_t* const cnx = &gEGLImpl;
1598 if (cnx->dso && cnx->egl.eglBindAPI) {
1599 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001600 }
1601 return res;
1602}
1603
1604EGLenum eglQueryAPI(void)
1605{
1606 clearError();
1607
1608 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001609 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001610 }
1611
Mathias Agopianada798b2012-02-13 17:09:30 -08001612 egl_connection_t* const cnx = &gEGLImpl;
1613 if (cnx->dso && cnx->egl.eglQueryAPI) {
1614 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001615 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001616
Mathias Agopian518ec112011-05-13 16:21:08 -07001617 // or, it can only be OpenGL ES
1618 return EGL_OPENGL_ES_API;
1619}
1620
1621EGLBoolean eglReleaseThread(void)
1622{
1623 clearError();
1624
Mathias Agopianada798b2012-02-13 17:09:30 -08001625 egl_connection_t* const cnx = &gEGLImpl;
1626 if (cnx->dso && cnx->egl.eglReleaseThread) {
1627 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001628 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301629
1630 // If there is context bound to the thread, release it
1631 egl_display_t::loseCurrent(get_context(getContext()));
1632
Mathias Agopian518ec112011-05-13 16:21:08 -07001633 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001634 return EGL_TRUE;
1635}
1636
1637EGLSurface eglCreatePbufferFromClientBuffer(
1638 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1639 EGLConfig config, const EGLint *attrib_list)
1640{
1641 clearError();
1642
Jesse Hallb29e5e82012-04-04 16:53:42 -07001643 egl_connection_t* cnx = NULL;
1644 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1645 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001646 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1647 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001648 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001649 }
1650 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1651}
1652
1653// ----------------------------------------------------------------------------
1654// EGL_EGLEXT_VERSION 3
1655// ----------------------------------------------------------------------------
1656
1657EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1658 const EGLint *attrib_list)
1659{
1660 clearError();
1661
Jesse Hallb29e5e82012-04-04 16:53:42 -07001662 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001663 if (!dp) return EGL_FALSE;
1664
Jesse Hallb29e5e82012-04-04 16:53:42 -07001665 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001666 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001667 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001668
1669 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001670 if (s->cnx->egl.eglLockSurfaceKHR) {
1671 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001672 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001673 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001674 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001675}
1676
1677EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1678{
1679 clearError();
1680
Jesse Hallb29e5e82012-04-04 16:53:42 -07001681 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001682 if (!dp) return EGL_FALSE;
1683
Jesse Hallb29e5e82012-04-04 16:53:42 -07001684 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001685 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001686 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001687
1688 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001689 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001690 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001691 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001692 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001693}
1694
1695EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1696 EGLClientBuffer buffer, const EGLint *attrib_list)
1697{
1698 clearError();
1699
Jesse Hallb29e5e82012-04-04 16:53:42 -07001700 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001701 if (!dp) return EGL_NO_IMAGE_KHR;
1702
Jesse Hallb29e5e82012-04-04 16:53:42 -07001703 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001704 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001705
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001706 // Temporary hack: eglImageCreateKHR should accept EGL_GL_COLORSPACE_LINEAR_KHR,
1707 // EGL_GL_COLORSPACE_SRGB_KHR and EGL_GL_COLORSPACE_DEFAULT_EXT if
1708 // EGL_EXT_image_gl_colorspace is supported, but some drivers don't like
1709 // the DEFAULT value and generate an error.
1710 std::vector<EGLint> strippedAttribList;
1711 for (const EGLint *attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
1712 if (attr[0] == EGL_GL_COLORSPACE_KHR &&
1713 dp->haveExtension("EGL_EXT_image_gl_colorspace")) {
1714 if (attr[1] != EGL_GL_COLORSPACE_LINEAR_KHR &&
1715 attr[1] != EGL_GL_COLORSPACE_SRGB_KHR) {
1716 continue;
1717 }
1718 }
1719 strippedAttribList.push_back(attr[0]);
1720 strippedAttribList.push_back(attr[1]);
1721 }
1722 strippedAttribList.push_back(EGL_NONE);
1723
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001724 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1725 egl_connection_t* const cnx = &gEGLImpl;
1726 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1727 result = cnx->egl.eglCreateImageKHR(
1728 dp->disp.dpy,
1729 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001730 target, buffer, strippedAttribList.data());
Mathias Agopian518ec112011-05-13 16:21:08 -07001731 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001732 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001733}
1734
1735EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1736{
1737 clearError();
1738
Jesse Hallb29e5e82012-04-04 16:53:42 -07001739 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001740 if (!dp) return EGL_FALSE;
1741
Steven Holte646a5c52012-06-04 20:02:11 -07001742 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001743 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001744 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001745 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001746 }
Steven Holte646a5c52012-06-04 20:02:11 -07001747 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001748}
1749
1750// ----------------------------------------------------------------------------
1751// EGL_EGLEXT_VERSION 5
1752// ----------------------------------------------------------------------------
1753
1754
1755EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1756{
1757 clearError();
1758
Jesse Hallb29e5e82012-04-04 16:53:42 -07001759 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001760 if (!dp) return EGL_NO_SYNC_KHR;
1761
Mathias Agopian518ec112011-05-13 16:21:08 -07001762 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001763 egl_connection_t* const cnx = &gEGLImpl;
1764 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1765 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001766 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001767 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001768}
1769
1770EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1771{
1772 clearError();
1773
Jesse Hallb29e5e82012-04-04 16:53:42 -07001774 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001775 if (!dp) return EGL_FALSE;
1776
Mathias Agopian518ec112011-05-13 16:21:08 -07001777 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001778 egl_connection_t* const cnx = &gEGLImpl;
1779 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1780 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001781 }
1782 return result;
1783}
1784
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001785EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1786 clearError();
1787
1788 const egl_display_ptr dp = validate_display(dpy);
1789 if (!dp) return EGL_FALSE;
1790
1791 EGLBoolean result = EGL_FALSE;
1792 egl_connection_t* const cnx = &gEGLImpl;
1793 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1794 result = cnx->egl.eglSignalSyncKHR(
1795 dp->disp.dpy, sync, mode);
1796 }
1797 return result;
1798}
1799
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001800EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1801 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001802{
1803 clearError();
1804
Jesse Hallb29e5e82012-04-04 16:53:42 -07001805 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001806 if (!dp) return EGL_FALSE;
1807
Mathias Agopian737b8962017-02-24 14:32:05 -08001808 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001809 egl_connection_t* const cnx = &gEGLImpl;
1810 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1811 result = cnx->egl.eglClientWaitSyncKHR(
1812 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001813 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001814 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001815}
1816
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001817EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1818 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001819{
1820 clearError();
1821
Jesse Hallb29e5e82012-04-04 16:53:42 -07001822 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001823 if (!dp) return EGL_FALSE;
1824
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001825 EGLBoolean result = EGL_FALSE;
1826 egl_connection_t* const cnx = &gEGLImpl;
1827 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1828 result = cnx->egl.eglGetSyncAttribKHR(
1829 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001830 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001831 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001832}
1833
Season Li000d88f2015-07-01 11:39:40 -07001834EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1835{
1836 clearError();
1837
1838 const egl_display_ptr dp = validate_display(dpy);
1839 if (!dp) return EGL_NO_STREAM_KHR;
1840
1841 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1842 egl_connection_t* const cnx = &gEGLImpl;
1843 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1844 result = cnx->egl.eglCreateStreamKHR(
1845 dp->disp.dpy, attrib_list);
1846 }
1847 return result;
1848}
1849
1850EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1851{
1852 clearError();
1853
1854 const egl_display_ptr dp = validate_display(dpy);
1855 if (!dp) return EGL_FALSE;
1856
1857 EGLBoolean result = EGL_FALSE;
1858 egl_connection_t* const cnx = &gEGLImpl;
1859 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1860 result = cnx->egl.eglDestroyStreamKHR(
1861 dp->disp.dpy, stream);
1862 }
1863 return result;
1864}
1865
1866EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1867 EGLenum attribute, EGLint value)
1868{
1869 clearError();
1870
1871 const egl_display_ptr dp = validate_display(dpy);
1872 if (!dp) return EGL_FALSE;
1873
1874 EGLBoolean result = EGL_FALSE;
1875 egl_connection_t* const cnx = &gEGLImpl;
1876 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1877 result = cnx->egl.eglStreamAttribKHR(
1878 dp->disp.dpy, stream, attribute, value);
1879 }
1880 return result;
1881}
1882
1883EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1884 EGLenum attribute, EGLint *value)
1885{
1886 clearError();
1887
1888 const egl_display_ptr dp = validate_display(dpy);
1889 if (!dp) return EGL_FALSE;
1890
1891 EGLBoolean result = EGL_FALSE;
1892 egl_connection_t* const cnx = &gEGLImpl;
1893 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1894 result = cnx->egl.eglQueryStreamKHR(
1895 dp->disp.dpy, stream, attribute, value);
1896 }
1897 return result;
1898}
1899
1900EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1901 EGLenum attribute, EGLuint64KHR *value)
1902{
1903 clearError();
1904
1905 const egl_display_ptr dp = validate_display(dpy);
1906 if (!dp) return EGL_FALSE;
1907
1908 EGLBoolean result = EGL_FALSE;
1909 egl_connection_t* const cnx = &gEGLImpl;
1910 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1911 result = cnx->egl.eglQueryStreamu64KHR(
1912 dp->disp.dpy, stream, attribute, value);
1913 }
1914 return result;
1915}
1916
1917EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1918 EGLenum attribute, EGLTimeKHR *value)
1919{
1920 clearError();
1921
1922 const egl_display_ptr dp = validate_display(dpy);
1923 if (!dp) return EGL_FALSE;
1924
1925 EGLBoolean result = EGL_FALSE;
1926 egl_connection_t* const cnx = &gEGLImpl;
1927 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1928 result = cnx->egl.eglQueryStreamTimeKHR(
1929 dp->disp.dpy, stream, attribute, value);
1930 }
1931 return result;
1932}
1933
1934EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1935 EGLStreamKHR stream, const EGLint *attrib_list)
1936{
1937 clearError();
1938
1939 egl_display_ptr dp = validate_display(dpy);
1940 if (!dp) return EGL_NO_SURFACE;
1941
1942 egl_connection_t* const cnx = &gEGLImpl;
1943 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1944 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1945 dp->disp.dpy, config, stream, attrib_list);
1946 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001947 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
1948 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001949 return s;
1950 }
1951 }
1952 return EGL_NO_SURFACE;
1953}
1954
1955EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1956 EGLStreamKHR stream)
1957{
1958 clearError();
1959
1960 const egl_display_ptr dp = validate_display(dpy);
1961 if (!dp) return EGL_FALSE;
1962
1963 EGLBoolean result = EGL_FALSE;
1964 egl_connection_t* const cnx = &gEGLImpl;
1965 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1966 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1967 dp->disp.dpy, stream);
1968 }
1969 return result;
1970}
1971
1972EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1973 EGLStreamKHR stream)
1974{
1975 clearError();
1976
1977 const egl_display_ptr dp = validate_display(dpy);
1978 if (!dp) return EGL_FALSE;
1979
1980 EGLBoolean result = EGL_FALSE;
1981 egl_connection_t* const cnx = &gEGLImpl;
1982 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1983 result = cnx->egl.eglStreamConsumerAcquireKHR(
1984 dp->disp.dpy, stream);
1985 }
1986 return result;
1987}
1988
1989EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1990 EGLStreamKHR stream)
1991{
1992 clearError();
1993
1994 const egl_display_ptr dp = validate_display(dpy);
1995 if (!dp) return EGL_FALSE;
1996
1997 EGLBoolean result = EGL_FALSE;
1998 egl_connection_t* const cnx = &gEGLImpl;
1999 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2000 result = cnx->egl.eglStreamConsumerReleaseKHR(
2001 dp->disp.dpy, stream);
2002 }
2003 return result;
2004}
2005
2006EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2007 EGLDisplay dpy, EGLStreamKHR stream)
2008{
2009 clearError();
2010
2011 const egl_display_ptr dp = validate_display(dpy);
2012 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2013
2014 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2015 egl_connection_t* const cnx = &gEGLImpl;
2016 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2017 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2018 dp->disp.dpy, stream);
2019 }
2020 return result;
2021}
2022
2023EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2024 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2025{
2026 clearError();
2027
2028 const egl_display_ptr dp = validate_display(dpy);
2029 if (!dp) return EGL_NO_STREAM_KHR;
2030
2031 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2032 egl_connection_t* const cnx = &gEGLImpl;
2033 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2034 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2035 dp->disp.dpy, file_descriptor);
2036 }
2037 return result;
2038}
2039
Mathias Agopian518ec112011-05-13 16:21:08 -07002040// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002041// EGL_EGLEXT_VERSION 15
2042// ----------------------------------------------------------------------------
2043
2044EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2045 clearError();
2046 const egl_display_ptr dp = validate_display(dpy);
2047 if (!dp) return EGL_FALSE;
2048 EGLint result = EGL_FALSE;
2049 egl_connection_t* const cnx = &gEGLImpl;
2050 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2051 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2052 }
2053 return result;
2054}
2055
2056// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002057// ANDROID extensions
2058// ----------------------------------------------------------------------------
2059
Jamie Gennis331841b2012-09-06 14:52:00 -07002060EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2061{
2062 clearError();
2063
2064 const egl_display_ptr dp = validate_display(dpy);
2065 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2066
2067 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2068 egl_connection_t* const cnx = &gEGLImpl;
2069 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2070 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2071 }
2072 return result;
2073}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002074
Andy McFadden72841452013-03-01 16:25:32 -08002075EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2076 EGLnsecsANDROID time)
2077{
2078 clearError();
2079
2080 const egl_display_ptr dp = validate_display(dpy);
2081 if (!dp) {
2082 return EGL_FALSE;
2083 }
2084
2085 SurfaceRef _s(dp.get(), surface);
2086 if (!_s.get()) {
2087 setError(EGL_BAD_SURFACE, EGL_FALSE);
2088 return EGL_FALSE;
2089 }
2090
2091 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002092 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002093
2094 return EGL_TRUE;
2095}
2096
Craig Donner60761072017-01-27 12:30:44 -08002097EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2098 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002099 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2100 // this function cannot be implemented when this libEGL is built for
2101 // vendors.
2102#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002103 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002104 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002105#else
2106 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2107#endif
Craig Donner60761072017-01-27 12:30:44 -08002108}
2109
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002110// ----------------------------------------------------------------------------
2111// NVIDIA extensions
2112// ----------------------------------------------------------------------------
2113EGLuint64NV eglGetSystemTimeFrequencyNV()
2114{
2115 clearError();
2116
2117 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002118 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002119 }
2120
2121 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002122 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002123
Mathias Agopianada798b2012-02-13 17:09:30 -08002124 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2125 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002126 }
2127
Mathias Agopian737b8962017-02-24 14:32:05 -08002128 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002129}
2130
2131EGLuint64NV eglGetSystemTimeNV()
2132{
2133 clearError();
2134
2135 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002136 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002137 }
2138
2139 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002140 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002141
Mathias Agopianada798b2012-02-13 17:09:30 -08002142 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2143 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002144 }
2145
Mathias Agopian737b8962017-02-24 14:32:05 -08002146 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002147}
Dan Stozaa894d082015-02-19 15:27:36 -08002148
2149// ----------------------------------------------------------------------------
2150// Partial update extension
2151// ----------------------------------------------------------------------------
2152EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2153 EGLint *rects, EGLint n_rects)
2154{
2155 clearError();
2156
2157 const egl_display_ptr dp = validate_display(dpy);
2158 if (!dp) {
2159 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2160 return EGL_FALSE;
2161 }
2162
2163 SurfaceRef _s(dp.get(), surface);
2164 if (!_s.get()) {
2165 setError(EGL_BAD_SURFACE, EGL_FALSE);
2166 return EGL_FALSE;
2167 }
2168
2169 egl_surface_t const * const s = get_surface(surface);
2170 if (s->cnx->egl.eglSetDamageRegionKHR) {
2171 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2172 rects, n_rects);
2173 }
2174
2175 return EGL_FALSE;
2176}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002177
Brian Anderson1049d1d2016-12-16 17:25:57 -08002178EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2179 EGLuint64KHR *frameId) {
2180 clearError();
2181
2182 const egl_display_ptr dp = validate_display(dpy);
2183 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002184 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002185 }
2186
2187 SurfaceRef _s(dp.get(), surface);
2188 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002189 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002190 }
2191
2192 egl_surface_t const * const s = get_surface(surface);
2193
Mathias Agopian65421432017-03-08 11:49:05 -08002194 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002195 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002196 }
2197
2198 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002199 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002200
Mathias Agopian65421432017-03-08 11:49:05 -08002201 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002202 // This should not happen. Return an error that is not in the spec
2203 // so it's obvious something is very wrong.
2204 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002205 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002206 }
2207
2208 *frameId = nextFrameId;
2209 return EGL_TRUE;
2210}
2211
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002212EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2213 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2214{
2215 clearError();
2216
2217 const egl_display_ptr dp = validate_display(dpy);
2218 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002219 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002220 }
2221
2222 SurfaceRef _s(dp.get(), surface);
2223 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002224 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002225 }
2226
2227 egl_surface_t const * const s = get_surface(surface);
2228
Mathias Agopian65421432017-03-08 11:49:05 -08002229 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002230 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002231 }
2232
2233 nsecs_t* compositeDeadline = nullptr;
2234 nsecs_t* compositeInterval = nullptr;
2235 nsecs_t* compositeToPresentLatency = nullptr;
2236
2237 for (int i = 0; i < numTimestamps; i++) {
2238 switch (names[i]) {
2239 case EGL_COMPOSITE_DEADLINE_ANDROID:
2240 compositeDeadline = &values[i];
2241 break;
2242 case EGL_COMPOSITE_INTERVAL_ANDROID:
2243 compositeInterval = &values[i];
2244 break;
2245 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2246 compositeToPresentLatency = &values[i];
2247 break;
2248 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002249 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002250 }
2251 }
2252
Mathias Agopian65421432017-03-08 11:49:05 -08002253 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002254 compositeDeadline, compositeInterval, compositeToPresentLatency);
2255
2256 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002257 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002258 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002259 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002260 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002261 default:
2262 // This should not happen. Return an error that is not in the spec
2263 // so it's obvious something is very wrong.
2264 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002265 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002266 }
2267}
2268
2269EGLBoolean eglGetCompositorTimingSupportedANDROID(
2270 EGLDisplay dpy, EGLSurface surface, EGLint name)
2271{
2272 clearError();
2273
2274 const egl_display_ptr dp = validate_display(dpy);
2275 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002276 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002277 }
2278
2279 SurfaceRef _s(dp.get(), surface);
2280 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002281 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002282 }
2283
2284 egl_surface_t const * const s = get_surface(surface);
2285
Mathias Agopian65421432017-03-08 11:49:05 -08002286 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002287 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002288 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002289 }
2290
2291 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002292 case EGL_COMPOSITE_DEADLINE_ANDROID:
2293 case EGL_COMPOSITE_INTERVAL_ANDROID:
2294 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2295 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002296 default:
2297 return EGL_FALSE;
2298 }
2299}
2300
Pablo Ceballosc18be292016-05-31 14:55:42 -07002301EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002302 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002303 EGLnsecsANDROID *values)
2304{
2305 clearError();
2306
2307 const egl_display_ptr dp = validate_display(dpy);
2308 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002309 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002310 }
2311
2312 SurfaceRef _s(dp.get(), surface);
2313 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002314 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002315 }
2316
2317 egl_surface_t const * const s = get_surface(surface);
2318
Mathias Agopian65421432017-03-08 11:49:05 -08002319 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002320 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002321 }
2322
Brian Andersondbd0ea82016-07-22 09:38:59 -07002323 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002324 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002325 nsecs_t* latchTime = nullptr;
2326 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002327 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002328 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002329 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002330 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002331 nsecs_t* releaseTime = nullptr;
2332
2333 for (int i = 0; i < numTimestamps; i++) {
2334 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002335 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2336 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002337 break;
2338 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2339 acquireTime = &values[i];
2340 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002341 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2342 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002343 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002344 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2345 firstRefreshStartTime = &values[i];
2346 break;
2347 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2348 lastRefreshStartTime = &values[i];
2349 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002350 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2351 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002352 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002353 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2354 displayPresentTime = &values[i];
2355 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002356 case EGL_DEQUEUE_READY_TIME_ANDROID:
2357 dequeueReadyTime = &values[i];
2358 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002359 case EGL_READS_DONE_TIME_ANDROID:
2360 releaseTime = &values[i];
2361 break;
2362 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002363 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002364 }
2365 }
2366
Mathias Agopian65421432017-03-08 11:49:05 -08002367 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002368 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002369 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002370 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002371
Brian Anderson069b3652016-07-22 10:32:47 -07002372 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002373 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002374 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002375 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002376 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002377 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002378 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002379 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002380 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2381 default:
2382 // This should not happen. Return an error that is not in the spec
2383 // so it's obvious something is very wrong.
2384 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2385 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002386 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002387}
2388
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002389EGLBoolean eglGetFrameTimestampSupportedANDROID(
2390 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002391{
2392 clearError();
2393
2394 const egl_display_ptr dp = validate_display(dpy);
2395 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002396 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002397 }
2398
2399 SurfaceRef _s(dp.get(), surface);
2400 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002401 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002402 }
2403
2404 egl_surface_t const * const s = get_surface(surface);
2405
Mathias Agopian65421432017-03-08 11:49:05 -08002406 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002407 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002408 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002409 }
2410
2411 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002412 case EGL_COMPOSITE_DEADLINE_ANDROID:
2413 case EGL_COMPOSITE_INTERVAL_ANDROID:
2414 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002415 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002416 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002417 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2418 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2419 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002420 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002421 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002422 case EGL_READS_DONE_TIME_ANDROID:
2423 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002424 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2425 int value = 0;
2426 window->query(window,
2427 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2428 return value == 0 ? EGL_FALSE : EGL_TRUE;
2429 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002430 default:
2431 return EGL_FALSE;
2432 }
2433}