blob: 86430b08d8831145bce44ccfc2f05e13a0b79e62 [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
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700139
140/*
141 * EGL Extensions entry-points exposed to 3rd party applications
142 * (keep in sync with gExtensionString above)
143 *
144 */
145static const extention_map_t sExtensionMap[] = {
146 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700147 { "eglLockSurfaceKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
149 { "eglUnlockSurfaceKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700151
152 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700153 { "eglCreateImageKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
155 { "eglDestroyImageKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700157
158 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
159 { "eglCreateSyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
161 { "eglDestroySyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
163 { "eglClientWaitSyncKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
165 { "eglSignalSyncKHR",
166 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
167 { "eglGetSyncAttribKHR",
168 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
169
170 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800171 { "eglGetSystemTimeFrequencyNV",
172 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
173 { "eglGetSystemTimeNV",
174 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700175
Mathias Agopian2bb71682013-03-27 17:32:41 -0700176 // EGL_KHR_wait_sync
177 { "eglWaitSyncKHR",
178 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700179
180 // EGL_ANDROID_presentation_time
181 { "eglPresentationTimeANDROID",
182 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800183
184 // EGL_KHR_swap_buffers_with_damage
185 { "eglSwapBuffersWithDamageKHR",
186 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
187
Craig Donner60761072017-01-27 12:30:44 -0800188 // EGL_ANDROID_get_native_client_buffer
189 { "eglGetNativeClientBufferANDROID",
190 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
191
Dan Stozaa894d082015-02-19 15:27:36 -0800192 // EGL_KHR_partial_update
193 { "eglSetDamageRegionKHR",
194 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700195
196 { "eglCreateStreamKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
198 { "eglDestroyStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
200 { "eglStreamAttribKHR",
201 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
202 { "eglQueryStreamKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
204 { "eglQueryStreamu64KHR",
205 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
206 { "eglQueryStreamTimeKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
208 { "eglCreateStreamProducerSurfaceKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
210 { "eglStreamConsumerGLTextureExternalKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
212 { "eglStreamConsumerAcquireKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
214 { "eglStreamConsumerReleaseKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
216 { "eglGetStreamFileDescriptorKHR",
217 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
218 { "eglCreateStreamFromFileDescriptorKHR",
219 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700220
221 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800222 { "eglGetNextFrameIdANDROID",
223 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800224 { "eglGetCompositorTimingANDROID",
225 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
226 { "eglGetCompositorTimingSupportedANDROID",
227 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700228 { "eglGetFrameTimestampsANDROID",
229 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800230 { "eglGetFrameTimestampSupportedANDROID",
231 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700232
233 // EGL_ANDROID_native_fence_sync
234 { "eglDupNativeFenceFDANDROID",
235 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700236};
237
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700238/*
239 * These extensions entry-points should not be exposed to applications.
240 * They're used internally by the Android EGL layer.
241 */
242#define FILTER_EXTENSIONS(procname) \
243 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
244 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700245 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700246
Mathias Agopian518ec112011-05-13 16:21:08 -0700247// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800248static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
249
Mathias Agopian518ec112011-05-13 16:21:08 -0700250static int sGLExtentionSlot = 0;
251static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
252
253static void(*findProcAddress(const char* name,
254 const extention_map_t* map, size_t n))() {
255 for (uint32_t i=0 ; i<n ; i++) {
256 if (!strcmp(name, map[i].name)) {
257 return map[i].address;
258 }
259 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700260 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -0700261}
262
263// ----------------------------------------------------------------------------
264
Mathias Agopian518ec112011-05-13 16:21:08 -0700265extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
266extern EGLBoolean egl_init_drivers();
267extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700268extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700269
Mathias Agopian518ec112011-05-13 16:21:08 -0700270} // namespace android;
271
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700272
Mathias Agopian518ec112011-05-13 16:21:08 -0700273// ----------------------------------------------------------------------------
274
275static inline void clearError() { egl_tls_t::clearError(); }
276static inline EGLContext getContext() { return egl_tls_t::getContext(); }
277
278// ----------------------------------------------------------------------------
279
280EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
281{
Jesse Hall1508ae62017-01-19 17:43:26 -0800282 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700283 clearError();
284
Dan Stozac3289c42014-01-17 11:38:34 -0800285 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700286 if (index >= NUM_DISPLAYS) {
287 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
288 }
289
290 if (egl_init_drivers() == EGL_FALSE) {
291 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
292 }
293
294 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
295 return dpy;
296}
297
298// ----------------------------------------------------------------------------
299// Initialization
300// ----------------------------------------------------------------------------
301
302EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
303{
304 clearError();
305
Jesse Hallb29e5e82012-04-04 16:53:42 -0700306 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800307 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700308
309 EGLBoolean res = dp->initialize(major, minor);
310
311 return res;
312}
313
314EGLBoolean eglTerminate(EGLDisplay dpy)
315{
316 // NOTE: don't unload the drivers b/c some APIs can be called
317 // after eglTerminate() has been called. eglTerminate() only
318 // terminates an EGLDisplay, not a EGL itself.
319
320 clearError();
321
Jesse Hallb29e5e82012-04-04 16:53:42 -0700322 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800323 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700324
325 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800326
Mathias Agopian518ec112011-05-13 16:21:08 -0700327 return res;
328}
329
330// ----------------------------------------------------------------------------
331// configuration
332// ----------------------------------------------------------------------------
333
334EGLBoolean eglGetConfigs( EGLDisplay dpy,
335 EGLConfig *configs,
336 EGLint config_size, EGLint *num_config)
337{
338 clearError();
339
Jesse Hallb29e5e82012-04-04 16:53:42 -0700340 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700341 if (!dp) return EGL_FALSE;
342
Yi Kong48a6cd22018-07-18 10:07:09 -0700343 if (num_config==nullptr) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800344 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700345 }
346
Mathias Agopian7773c432012-02-13 20:06:08 -0800347 EGLBoolean res = EGL_FALSE;
348 *num_config = 0;
349
350 egl_connection_t* const cnx = &gEGLImpl;
351 if (cnx->dso) {
352 res = cnx->egl.eglGetConfigs(
353 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700354 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800355
356 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700357}
358
359EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
360 EGLConfig *configs, EGLint config_size,
361 EGLint *num_config)
362{
363 clearError();
364
Jesse Hallb29e5e82012-04-04 16:53:42 -0700365 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700366 if (!dp) return EGL_FALSE;
367
Yi Kong48a6cd22018-07-18 10:07:09 -0700368 if (num_config==nullptr) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800369 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700370 }
371
Mathias Agopian518ec112011-05-13 16:21:08 -0700372 EGLBoolean res = EGL_FALSE;
373 *num_config = 0;
374
Mathias Agopianada798b2012-02-13 17:09:30 -0800375 egl_connection_t* const cnx = &gEGLImpl;
376 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700377 if (attrib_list) {
378 char value[PROPERTY_VALUE_MAX];
379 property_get("debug.egl.force_msaa", value, "false");
380
381 if (!strcmp(value, "true")) {
382 size_t attribCount = 0;
383 EGLint attrib = attrib_list[0];
384
385 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700386 // if no caveat is requested
Yi Kong48a6cd22018-07-18 10:07:09 -0700387 const EGLint *attribRendererable = nullptr;
388 const EGLint *attribCaveat = nullptr;
Romain Guy1cffc802012-10-15 18:13:05 -0700389
390 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700391 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700392 while (attrib != EGL_NONE) {
393 attrib = attrib_list[attribCount];
394 switch (attrib) {
395 case EGL_RENDERABLE_TYPE:
396 attribRendererable = &attrib_list[attribCount];
397 break;
398 case EGL_CONFIG_CAVEAT:
399 attribCaveat = &attrib_list[attribCount];
400 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800401 default:
402 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700403 }
404 attribCount++;
405 }
406
407 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
408 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800409
Romain Guy1cffc802012-10-15 18:13:05 -0700410 // Insert 2 extra attributes to force-enable MSAA 4x
411 EGLint aaAttribs[attribCount + 4];
412 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
413 aaAttribs[1] = 1;
414 aaAttribs[2] = EGL_SAMPLES;
415 aaAttribs[3] = 4;
416
417 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
418
419 EGLint numConfigAA;
420 EGLBoolean resAA = cnx->egl.eglChooseConfig(
421 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
422
423 if (resAA == EGL_TRUE && numConfigAA > 0) {
424 ALOGD("Enabling MSAA 4x");
425 *num_config = numConfigAA;
426 return resAA;
427 }
428 }
429 }
430 }
431
Mathias Agopian7773c432012-02-13 20:06:08 -0800432 res = cnx->egl.eglChooseConfig(
433 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700434 }
435 return res;
436}
437
438EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
439 EGLint attribute, EGLint *value)
440{
441 clearError();
442
Yi Kong48a6cd22018-07-18 10:07:09 -0700443 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700444 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
445 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800446
Mathias Agopian518ec112011-05-13 16:21:08 -0700447 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800448 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700449}
450
451// ----------------------------------------------------------------------------
452// surfaces
453// ----------------------------------------------------------------------------
454
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700455// Translates EGL color spaces to Android data spaces.
456static android_dataspace dataSpaceFromEGLColorSpace(EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700457 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800458 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700459 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800460 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600461 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
462 return HAL_DATASPACE_DISPLAY_P3;
463 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
464 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600465 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
466 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600467 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
468 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700469 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT) {
470 return HAL_DATASPACE_BT2020_LINEAR;
471 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_PQ_EXT) {
472 return HAL_DATASPACE_BT2020_PQ;
Jesse Hallc2e41222013-08-08 13:40:22 -0700473 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700474 return HAL_DATASPACE_UNKNOWN;
Jesse Hallc2e41222013-08-08 13:40:22 -0700475}
476
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700477// Get the colorspace value that should be reported from queries. When the colorspace
478// is unknown (no attribute passed), default to reporting LINEAR.
479static EGLint getReportedColorSpace(EGLint colorspace) {
480 return colorspace == EGL_UNKNOWN ? EGL_GL_COLORSPACE_LINEAR_KHR : colorspace;
481}
482
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700483// Returns a list of color spaces understood by the vendor EGL driver.
484static std::vector<EGLint> getDriverColorSpaces(egl_display_ptr dp,
485 android_pixel_format format) {
486 std::vector<EGLint> colorSpaces;
487 if (!dp->hasColorSpaceSupport) return colorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700488
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700489 // OpenGL drivers only support sRGB encoding with 8-bit formats.
490 // RGB_888 is never returned by getNativePixelFormat, but is included for completeness.
491 const bool formatSupportsSRGBEncoding =
492 format == HAL_PIXEL_FORMAT_RGBA_8888 || format == HAL_PIXEL_FORMAT_RGBX_8888 ||
493 format == HAL_PIXEL_FORMAT_RGB_888;
494 const bool formatIsFloatingPoint = format == HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700495
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700496 if (formatSupportsSRGBEncoding) {
497 // sRGB and linear are always supported when color space support is present.
498 colorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
499 colorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
500 // DCI-P3 uses the sRGB transfer function, so it's only relevant for 8-bit formats.
501 if (findExtension(dp->disp.queryString.extensions,
502 "EGL_EXT_gl_colorspace_display_p3")) {
503 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600504 }
505 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700506
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700507 // According to the spec, scRGB is only supported for floating point formats.
508 // For non-linear scRGB, the application is responsible for applying the
509 // transfer function.
510 if (formatIsFloatingPoint) {
511 if (findExtension(dp->disp.queryString.extensions,
512 "EGL_EXT_gl_colorspace_scrgb")) {
513 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_EXT);
514 }
515 if (findExtension(dp->disp.queryString.extensions,
516 "EGL_EXT_gl_colorspace_scrgb_linear")) {
517 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
518 }
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600519 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700520
521 // BT2020 can be used with any pixel format. PQ encoding must be applied by the
522 // application and does not affect the behavior of OpenGL.
523 if (findExtension(dp->disp.queryString.extensions,
524 "EGL_EXT_gl_colorspace_bt2020_linear")) {
525 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
526 }
527 if (findExtension(dp->disp.queryString.extensions,
528 "EGL_EXT_gl_colorspace_bt2020_pq")) {
529 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
530 }
531
532 // Linear DCI-P3 simply uses different primaries than standard RGB and thus
533 // can be used with any pixel format.
534 if (findExtension(dp->disp.queryString.extensions,
535 "EGL_EXT_gl_colorspace_display_p3_linear")) {
536 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
537 }
538 return colorSpaces;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600539}
540
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700541// Cleans up color space related parameters that the driver does not understand.
542// If there is no color space attribute in attrib_list, colorSpace is left
543// unmodified.
544static EGLBoolean processAttributes(egl_display_ptr dp, NativeWindowType window,
545 android_pixel_format format, const EGLint* attrib_list,
546 EGLint* colorSpace,
547 std::vector<EGLint>* strippedAttribList) {
548 for (const EGLint* attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
549 bool copyAttribute = true;
550 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
551 // Fail immediately if the driver doesn't have color space support at all.
552 if (!dp->hasColorSpaceSupport) return false;
553 *colorSpace = attr[1];
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600554
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700555 // Strip the attribute if the driver doesn't understand it.
556 copyAttribute = false;
557 std::vector<EGLint> driverColorSpaces = getDriverColorSpaces(dp, format);
558 for (auto driverColorSpace : driverColorSpaces) {
559 if (attr[1] == driverColorSpace) {
560 copyAttribute = true;
561 break;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600562 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600563 }
564 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700565 if (copyAttribute) {
566 strippedAttribList->push_back(attr[0]);
567 strippedAttribList->push_back(attr[1]);
568 }
569 }
570 // Terminate the attribute list.
571 strippedAttribList->push_back(EGL_NONE);
572
573 // If the passed color space has wide color gamut, check whether the target native window
574 // supports wide color.
575 const bool colorSpaceIsNarrow =
576 *colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700577 *colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR ||
578 *colorSpace == EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700579 if (window && !colorSpaceIsNarrow) {
580 bool windowSupportsWideColor = true;
581 // Ordinarily we'd put a call to native_window_get_wide_color_support
582 // at the beginning of the function so that we'll have the
583 // result when needed elsewhere in the function.
584 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
585 // SurfaceFlinger is required to answer the call below we would
586 // end up in a deadlock situation. By moving the call to only happen
587 // if the application has specifically asked for wide-color we avoid
588 // the deadlock with SurfaceFlinger since it will not ask for a
589 // wide-color surface.
590 int err = native_window_get_wide_color_support(window, &windowSupportsWideColor);
591
592 if (err) {
593 ALOGE("processAttributes: invalid window (win=%p) "
594 "failed (%#x) (already connected to another API?)",
595 window, err);
596 return false;
597 }
598 if (!windowSupportsWideColor) {
599 // Application has asked for a wide-color colorspace but
600 // wide-color support isn't available on the display the window is on.
601 return false;
602 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600603 }
604 return true;
605}
606
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700607// Gets the native pixel format corrsponding to the passed EGLConfig.
608void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config,
609 android_pixel_format* format) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600610 // Set the native window's buffers format to match what this config requests.
611 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
612 // of our native format. So if sRGB gamma is requested, we have to
613 // modify the EGLconfig's format before setting the native window's
614 // format.
615
616 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
617 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
618
619 EGLint a = 0;
620 EGLint r, g, b;
621 r = g = b = 0;
622 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
623 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
624 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
625 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
626 EGLint colorDepth = r + g + b;
627
628 // Today, the driver only understands sRGB and linear on 888X
629 // formats. Strip other colorspaces from the attribute list and
630 // only use them to set the dataspace via
631 // native_window_set_buffers_dataspace
632 // if pixel format is RGBX 8888
633 // TBD: Can test for future extensions that indicate that driver
634 // handles requested color space and we can let it through.
635 // allow SRGB and LINEAR. All others need to be stripped.
636 // else if 565, 4444
637 // TBD: Can we assume these are supported if 8888 is?
638 // else if FP16 or 1010102
639 // strip colorspace from attribs.
640 // endif
641 if (a == 0) {
642 if (colorDepth <= 16) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700643 *format = HAL_PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600644 } else {
645 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
646 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700647 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600648 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700649 *format = HAL_PIXEL_FORMAT_RGBX_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600650 }
651 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700652 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600653 }
654 }
655 } else {
656 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
657 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700658 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600659 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700660 *format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600661 }
662 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700663 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600664 }
665 }
666}
667
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700668EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
669 android_smpte2086_metadata smpteMetadata;
670 if (s->getSmpte2086Metadata(smpteMetadata)) {
671 int err =
672 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
673 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700674 if (err != 0) {
675 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
676 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700677 return EGL_FALSE;
678 }
679 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700680 android_cta861_3_metadata cta8613Metadata;
681 if (s->getCta8613Metadata(cta8613Metadata)) {
682 int err =
683 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
684 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700685 if (err != 0) {
686 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
687 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700688 return EGL_FALSE;
689 }
690 }
691 return EGL_TRUE;
692}
693
Mathias Agopian518ec112011-05-13 16:21:08 -0700694EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
695 NativeWindowType window,
696 const EGLint *attrib_list)
697{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700698 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700699 clearError();
700
Yi Kong48a6cd22018-07-18 10:07:09 -0700701 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700702 egl_display_ptr dp = validate_display_connection(dpy, cnx);
703 if (dp) {
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800704 if (!window) {
705 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
706 }
707
708 int value = 0;
709 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
710 if (!value) {
711 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
712 }
713
Andy McFaddend566ce32014-01-07 15:54:17 -0800714 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800715 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800716 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
717 "failed (%#x) (already connected to another API?)",
718 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700719 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700720 }
721
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700722 EGLDisplay iDpy = dp->disp.dpy;
723 android_pixel_format format;
724 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600725
726 // now select correct colorspace and dataspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700727 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700728 std::vector<EGLint> strippedAttribList;
729 if (!processAttributes(dp, window, format, attrib_list, &colorSpace,
730 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600731 ALOGE("error invalid colorspace: %d", colorSpace);
732 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700733 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700734 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800735
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700736 {
Jesse Hallc2e41222013-08-08 13:40:22 -0700737 int err = native_window_set_buffers_format(window, format);
738 if (err != 0) {
739 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700740 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700741 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
742 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
743 }
744 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700745
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700746 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
747 if (dataSpace != HAL_DATASPACE_UNKNOWN) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800748 int err = native_window_set_buffers_data_space(window, dataSpace);
749 if (err != 0) {
750 ALOGE("error setting native window pixel dataSpace: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700751 strerror(-err), err);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800752 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
753 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
754 }
755 }
756
Jamie Gennis59769462011-11-19 18:04:43 -0800757 // the EGL spec requires that a new EGLSurface default to swap interval
758 // 1, so explicitly set that on the window here.
759 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
760 anw->setSwapInterval(anw, 1);
761
Mathias Agopian518ec112011-05-13 16:21:08 -0700762 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800763 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700764 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600765 egl_surface_t* s =
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700766 new egl_surface_t(dp.get(), config, window, surface,
767 getReportedColorSpace(colorSpace), cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700768 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700770
771 // EGLSurface creation failed
772 native_window_set_buffers_format(window, 0);
773 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700774 }
775 return EGL_NO_SURFACE;
776}
777
778EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
779 NativePixmapType pixmap,
780 const EGLint *attrib_list)
781{
782 clearError();
783
Yi Kong48a6cd22018-07-18 10:07:09 -0700784 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700785 egl_display_ptr dp = validate_display_connection(dpy, cnx);
786 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700787 EGLDisplay iDpy = dp->disp.dpy;
788 android_pixel_format format;
789 getNativePixelFormat(iDpy, cnx, config, &format);
790
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600791 // now select a corresponding sRGB format if needed
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700792 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700793 std::vector<EGLint> strippedAttribList;
794 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
795 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600796 ALOGE("error invalid colorspace: %d", colorSpace);
797 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
798 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700799 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600800
Mathias Agopian518ec112011-05-13 16:21:08 -0700801 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800802 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700803 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700804 egl_surface_t* s =
Yi Kong48a6cd22018-07-18 10:07:09 -0700805 new egl_surface_t(dp.get(), config, nullptr, surface,
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700806 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700807 return s;
808 }
809 }
810 return EGL_NO_SURFACE;
811}
812
813EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
814 const EGLint *attrib_list)
815{
816 clearError();
817
Yi Kong48a6cd22018-07-18 10:07:09 -0700818 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700819 egl_display_ptr dp = validate_display_connection(dpy, cnx);
820 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600821 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700822 android_pixel_format format;
823 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600824
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700825 // Select correct colorspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700826 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700827 std::vector<EGLint> strippedAttribList;
828 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
829 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600830 ALOGE("error invalid colorspace: %d", colorSpace);
831 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
832 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700833 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600834
Mathias Agopian518ec112011-05-13 16:21:08 -0700835 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800836 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700837 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700838 egl_surface_t* s =
Yi Kong48a6cd22018-07-18 10:07:09 -0700839 new egl_surface_t(dp.get(), config, nullptr, surface,
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700840 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700841 return s;
842 }
843 }
844 return EGL_NO_SURFACE;
845}
Jesse Hall47743382013-02-08 11:13:46 -0800846
Mathias Agopian518ec112011-05-13 16:21:08 -0700847EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
848{
849 clearError();
850
Jesse Hallb29e5e82012-04-04 16:53:42 -0700851 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700852 if (!dp) return EGL_FALSE;
853
Jesse Hallb29e5e82012-04-04 16:53:42 -0700854 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700855 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800856 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700857
858 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800859 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700860 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700861 _s.terminate();
862 }
863 return result;
864}
865
866EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
867 EGLint attribute, EGLint *value)
868{
869 clearError();
870
Jesse Hallb29e5e82012-04-04 16:53:42 -0700871 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700872 if (!dp) return EGL_FALSE;
873
Jesse Hallb29e5e82012-04-04 16:53:42 -0700874 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700875 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800876 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700877
Mathias Agopian518ec112011-05-13 16:21:08 -0700878 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700879 if (s->getColorSpaceAttribute(attribute, value)) {
880 return EGL_TRUE;
881 } else if (s->getSmpte2086Attribute(attribute, value)) {
882 return EGL_TRUE;
883 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600884 return EGL_TRUE;
885 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700886 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700887}
888
Jamie Gennise8696a42012-01-15 18:54:57 -0800889void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800890 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800891 clearError();
892
Jesse Hallb29e5e82012-04-04 16:53:42 -0700893 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800894 if (!dp) {
895 return;
896 }
897
Jesse Hallb29e5e82012-04-04 16:53:42 -0700898 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800899 if (!_s.get()) {
900 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800901 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800902}
903
Mathias Agopian518ec112011-05-13 16:21:08 -0700904// ----------------------------------------------------------------------------
905// Contexts
906// ----------------------------------------------------------------------------
907
908EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
909 EGLContext share_list, const EGLint *attrib_list)
910{
911 clearError();
912
Yi Kong48a6cd22018-07-18 10:07:09 -0700913 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700914 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700915 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700916 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700917 if (!ContextRef(dp.get(), share_list).get()) {
918 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
919 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700920 egl_context_t* const c = get_context(share_list);
921 share_list = c->context;
922 }
923 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800924 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700925 if (context != EGL_NO_CONTEXT) {
926 // figure out if it's a GLESv1 or GLESv2
927 int version = 0;
928 if (attrib_list) {
929 while (*attrib_list != EGL_NONE) {
930 GLint attr = *attrib_list++;
931 GLint value = *attrib_list++;
932 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
933 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800934 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800935 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800936 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700937 }
938 }
939 };
940 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700941 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
942 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700943 return c;
944 }
945 }
946 return EGL_NO_CONTEXT;
947}
948
949EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
950{
951 clearError();
952
Jesse Hallb29e5e82012-04-04 16:53:42 -0700953 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700954 if (!dp)
955 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700956
Jesse Hallb29e5e82012-04-04 16:53:42 -0700957 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700958 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800959 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800960
Mathias Agopian518ec112011-05-13 16:21:08 -0700961 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800962 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700963 if (result == EGL_TRUE) {
964 _c.terminate();
965 }
966 return result;
967}
968
Mathias Agopian518ec112011-05-13 16:21:08 -0700969EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
970 EGLSurface read, EGLContext ctx)
971{
972 clearError();
973
Jesse Hallb29e5e82012-04-04 16:53:42 -0700974 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800975 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700976
Mathias Agopian5b287a62011-05-16 18:58:55 -0700977 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
978 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
979 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700980 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
981 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800982 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 }
984
985 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700986 ContextRef _c(dp.get(), ctx);
987 SurfaceRef _d(dp.get(), draw);
988 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700989
990 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700991 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700992 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800993 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700994 }
995
996 // these are the underlying implementation's object
997 EGLContext impl_ctx = EGL_NO_CONTEXT;
998 EGLSurface impl_draw = EGL_NO_SURFACE;
999 EGLSurface impl_read = EGL_NO_SURFACE;
1000
1001 // these are our objects structs passed in
Yi Kong48a6cd22018-07-18 10:07:09 -07001002 egl_context_t * c = nullptr;
1003 egl_surface_t const * d = nullptr;
1004 egl_surface_t const * r = nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001005
1006 // these are the current objects structs
1007 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001008
Mathias Agopian518ec112011-05-13 16:21:08 -07001009 if (ctx != EGL_NO_CONTEXT) {
1010 c = get_context(ctx);
1011 impl_ctx = c->context;
1012 } else {
1013 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001014 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1015 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001016 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001017 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001018 if (cur_c == nullptr) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001020 // not an error, there is just no current context.
1021 return EGL_TRUE;
1022 }
1023 }
1024
1025 // retrieve the underlying implementation's draw EGLSurface
1026 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001027 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001028 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001029 impl_draw = d->surface;
1030 }
1031
1032 // retrieve the underlying implementation's read EGLSurface
1033 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001034 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001035 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001036 impl_read = r->surface;
1037 }
1038
Mathias Agopian518ec112011-05-13 16:21:08 -07001039
Jesse Hallb29e5e82012-04-04 16:53:42 -07001040 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001041 draw, read, ctx,
1042 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001043
1044 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001045 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001046 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1047 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001048 _c.acquire();
1049 _r.acquire();
1050 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001051 } else {
1052 setGLHooksThreadSpecific(&gHooksNoContext);
1053 egl_tls_t::setContext(EGL_NO_CONTEXT);
1054 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001055 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001056 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001057 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001058 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001059 }
1060 return result;
1061}
1062
1063
1064EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1065 EGLint attribute, EGLint *value)
1066{
1067 clearError();
1068
Jesse Hallb29e5e82012-04-04 16:53:42 -07001069 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001070 if (!dp) return EGL_FALSE;
1071
Jesse Hallb29e5e82012-04-04 16:53:42 -07001072 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001073 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001074
Mathias Agopian518ec112011-05-13 16:21:08 -07001075 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001076 return c->cnx->egl.eglQueryContext(
1077 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001078
Mathias Agopian518ec112011-05-13 16:21:08 -07001079}
1080
1081EGLContext eglGetCurrentContext(void)
1082{
1083 // could be called before eglInitialize(), but we wouldn't have a context
1084 // then, and this function would correctly return EGL_NO_CONTEXT.
1085
1086 clearError();
1087
1088 EGLContext ctx = getContext();
1089 return ctx;
1090}
1091
1092EGLSurface eglGetCurrentSurface(EGLint readdraw)
1093{
1094 // could be called before eglInitialize(), but we wouldn't have a context
1095 // then, and this function would correctly return EGL_NO_SURFACE.
1096
1097 clearError();
1098
1099 EGLContext ctx = getContext();
1100 if (ctx) {
1101 egl_context_t const * const c = get_context(ctx);
1102 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1103 switch (readdraw) {
1104 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001105 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001106 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1107 }
1108 }
1109 return EGL_NO_SURFACE;
1110}
1111
1112EGLDisplay eglGetCurrentDisplay(void)
1113{
1114 // could be called before eglInitialize(), but we wouldn't have a context
1115 // then, and this function would correctly return EGL_NO_DISPLAY.
1116
1117 clearError();
1118
1119 EGLContext ctx = getContext();
1120 if (ctx) {
1121 egl_context_t const * const c = get_context(ctx);
1122 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1123 return c->dpy;
1124 }
1125 return EGL_NO_DISPLAY;
1126}
1127
1128EGLBoolean eglWaitGL(void)
1129{
Mathias Agopian518ec112011-05-13 16:21:08 -07001130 clearError();
1131
Mathias Agopianada798b2012-02-13 17:09:30 -08001132 egl_connection_t* const cnx = &gEGLImpl;
1133 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001134 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001135
1136 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001137}
1138
1139EGLBoolean eglWaitNative(EGLint engine)
1140{
Mathias Agopian518ec112011-05-13 16:21:08 -07001141 clearError();
1142
Mathias Agopianada798b2012-02-13 17:09:30 -08001143 egl_connection_t* const cnx = &gEGLImpl;
1144 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001145 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001146
1147 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001148}
1149
1150EGLint eglGetError(void)
1151{
Mathias Agopianada798b2012-02-13 17:09:30 -08001152 EGLint err = EGL_SUCCESS;
1153 egl_connection_t* const cnx = &gEGLImpl;
1154 if (cnx->dso) {
1155 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001156 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001157 if (err == EGL_SUCCESS) {
1158 err = egl_tls_t::getError();
1159 }
1160 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001161}
1162
Michael Chockc0ec5e22014-01-27 08:14:33 -08001163static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001164 const char* procname) {
1165 const egl_connection_t* cnx = &gEGLImpl;
Yi Kong48a6cd22018-07-18 10:07:09 -07001166 void* proc = nullptr;
Jesse Hallc07b5202013-07-04 12:08:16 -07001167
Michael Chockc0ec5e22014-01-27 08:14:33 -08001168 proc = dlsym(cnx->libEgl, procname);
1169 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1170
Jesse Hallc07b5202013-07-04 12:08:16 -07001171 proc = dlsym(cnx->libGles2, procname);
1172 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1173
1174 proc = dlsym(cnx->libGles1, procname);
1175 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1176
Yi Kong48a6cd22018-07-18 10:07:09 -07001177 return nullptr;
Jesse Hallc07b5202013-07-04 12:08:16 -07001178}
1179
Mathias Agopian518ec112011-05-13 16:21:08 -07001180__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1181{
1182 // eglGetProcAddress() could be the very first function called
1183 // in which case we must make sure we've initialized ourselves, this
1184 // happens the first time egl_get_display() is called.
1185
1186 clearError();
1187
1188 if (egl_init_drivers() == EGL_FALSE) {
1189 setError(EGL_BAD_PARAMETER, NULL);
Yi Kong48a6cd22018-07-18 10:07:09 -07001190 return nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001191 }
1192
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001193 if (FILTER_EXTENSIONS(procname)) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001194 return nullptr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001195 }
1196
Mathias Agopian518ec112011-05-13 16:21:08 -07001197 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001198 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001199 if (addr) return addr;
1200
Michael Chockc0ec5e22014-01-27 08:14:33 -08001201 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001202 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001203
Mathias Agopian518ec112011-05-13 16:21:08 -07001204 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1205 pthread_mutex_lock(&sExtensionMapMutex);
1206
1207 /*
1208 * Since eglGetProcAddress() is not associated to anything, it needs
1209 * to return a function pointer that "works" regardless of what
1210 * the current context is.
1211 *
1212 * For this reason, we return a "forwarder", a small stub that takes
1213 * care of calling the function associated with the context
1214 * currently bound.
1215 *
1216 * We first look for extensions we've already resolved, if we're seeing
1217 * this extension for the first time, we go through all our
1218 * implementations and call eglGetProcAddress() and record the
1219 * result in the appropriate implementation hooks and return the
1220 * address of the forwarder corresponding to that hook set.
1221 *
1222 */
1223
Mathias Agopian65421432017-03-08 11:49:05 -08001224 const std::string name(procname);
1225
1226 auto& extentionMap = sGLExtentionMap;
1227 auto pos = extentionMap.find(name);
1228 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001229 const int slot = sGLExtentionSlot;
1230
Steve Blocke6f43dd2012-01-06 19:20:56 +00001231 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001232 "no more slots for eglGetProcAddress(\"%s\")",
1233 procname);
1234
1235 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1236 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001237
1238 egl_connection_t* const cnx = &gEGLImpl;
1239 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001240 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001241 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001242 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1243 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001244 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001245 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001246 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001247
Mathias Agopian518ec112011-05-13 16:21:08 -07001248 if (found) {
1249 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001250 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001251 sGLExtentionSlot++;
1252 }
1253 }
1254
1255 pthread_mutex_unlock(&sExtensionMapMutex);
1256 return addr;
1257}
1258
Mathias Agopian65421432017-03-08 11:49:05 -08001259class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001260public:
1261
1262 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001263 static FrameCompletionThread thread;
1264
1265 char name[64];
1266
1267 std::lock_guard<std::mutex> lock(thread.mMutex);
1268 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1269 ATRACE_NAME(name);
1270
1271 thread.mQueue.push_back(sync);
1272 thread.mCondition.notify_one();
1273 thread.mFramesQueued++;
1274 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001275 }
1276
1277private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001278
Mathias Agopian65421432017-03-08 11:49:05 -08001279 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1280 std::thread thread(&FrameCompletionThread::loop, this);
1281 thread.detach();
1282 }
1283
1284#pragma clang diagnostic push
1285#pragma clang diagnostic ignored "-Wmissing-noreturn"
1286 void loop() {
1287 while (true) {
1288 threadLoop();
1289 }
1290 }
1291#pragma clang diagnostic pop
1292
1293 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001294 EGLSyncKHR sync;
1295 uint32_t frameNum;
1296 {
Mathias Agopian65421432017-03-08 11:49:05 -08001297 std::unique_lock<std::mutex> lock(mMutex);
1298 while (mQueue.empty()) {
1299 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001300 }
1301 sync = mQueue[0];
1302 frameNum = mFramesCompleted;
1303 }
1304 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1305 {
Mathias Agopian65421432017-03-08 11:49:05 -08001306 char name[64];
1307 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1308 ATRACE_NAME(name);
1309
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001310 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1311 if (result == EGL_FALSE) {
1312 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1313 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1314 ALOGE("FrameCompletion: timeout waiting for fence");
1315 }
1316 eglDestroySyncKHR(dpy, sync);
1317 }
1318 {
Mathias Agopian65421432017-03-08 11:49:05 -08001319 std::lock_guard<std::mutex> lock(mMutex);
1320 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001321 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001322 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001323 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001324 }
1325
1326 uint32_t mFramesQueued;
1327 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001328 std::deque<EGLSyncKHR> mQueue;
1329 std::condition_variable mCondition;
1330 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001331};
1332
Dan Stozaa894d082015-02-19 15:27:36 -08001333EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1334 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001335{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001336 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001337 clearError();
1338
Jesse Hallb29e5e82012-04-04 16:53:42 -07001339 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001340 if (!dp) return EGL_FALSE;
1341
Jesse Hallb29e5e82012-04-04 16:53:42 -07001342 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001343 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001344 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001345
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001346 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001347
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001348 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001349 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001350 if (sync != EGL_NO_SYNC_KHR) {
1351 FrameCompletionThread::queueSync(sync);
1352 }
1353 }
1354
Mathias Agopian7db993a2012-03-25 00:49:46 -07001355 if (CC_UNLIKELY(dp->finishOnSwap)) {
1356 uint32_t pixel;
1357 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1358 if (c) {
1359 // glReadPixels() ensures that the frame is complete
1360 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1361 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1362 }
1363 }
1364
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001365 if (!sendSurfaceMetadata(s)) {
1366 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1367 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1368 }
1369
Dan Stozaa894d082015-02-19 15:27:36 -08001370 if (n_rects == 0) {
1371 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1372 }
1373
Mathias Agopian65421432017-03-08 11:49:05 -08001374 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001375 for (int r = 0; r < n_rects; ++r) {
1376 int offset = r * 4;
1377 int x = rects[offset];
1378 int y = rects[offset + 1];
1379 int width = rects[offset + 2];
1380 int height = rects[offset + 3];
1381 android_native_rect_t androidRect;
1382 androidRect.left = x;
1383 androidRect.top = y + height;
1384 androidRect.right = x + width;
1385 androidRect.bottom = y;
1386 androidRects.push_back(androidRect);
1387 }
Mathias Agopian65421432017-03-08 11:49:05 -08001388 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001389
1390 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1391 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1392 rects, n_rects);
1393 } else {
1394 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1395 }
1396}
1397
1398EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1399{
Yi Kong48a6cd22018-07-18 10:07:09 -07001400 return eglSwapBuffersWithDamageKHR(dpy, surface, nullptr, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001401}
1402
1403EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1404 NativePixmapType target)
1405{
1406 clearError();
1407
Jesse Hallb29e5e82012-04-04 16:53:42 -07001408 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001409 if (!dp) return EGL_FALSE;
1410
Jesse Hallb29e5e82012-04-04 16:53:42 -07001411 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001412 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001413 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001414
Mathias Agopian518ec112011-05-13 16:21:08 -07001415 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001416 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001417}
1418
1419const char* eglQueryString(EGLDisplay dpy, EGLint name)
1420{
1421 clearError();
1422
Chia-I Wue57d1352016-08-15 16:10:02 +08001423 // Generate an error quietly when client extensions (as defined by
1424 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1425 // validate_display to generate the error as validate_display would log
1426 // the error, which can be misleading.
1427 //
1428 // If we want to support EGL_EXT_client_extensions later, we can return
1429 // the client extension string here instead.
1430 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -07001431 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)nullptr);
Chia-I Wue57d1352016-08-15 16:10:02 +08001432
Jesse Hallb29e5e82012-04-04 16:53:42 -07001433 const egl_display_ptr dp = validate_display(dpy);
Yi Kong48a6cd22018-07-18 10:07:09 -07001434 if (!dp) return (const char *) nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001435
1436 switch (name) {
1437 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001438 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001439 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001440 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001441 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001442 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001443 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001444 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001445 default:
1446 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001447 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001448 return setError(EGL_BAD_PARAMETER, (const char *)nullptr);
Mathias Agopian518ec112011-05-13 16:21:08 -07001449}
1450
Jiyong Park00b15b82017-08-10 20:30:56 +09001451extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001452{
1453 clearError();
1454
1455 const egl_display_ptr dp = validate_display(dpy);
Yi Kong48a6cd22018-07-18 10:07:09 -07001456 if (!dp) return (const char *) nullptr;
Mathias Agopianca088332013-03-28 17:44:13 -07001457
1458 switch (name) {
1459 case EGL_VENDOR:
1460 return dp->disp.queryString.vendor;
1461 case EGL_VERSION:
1462 return dp->disp.queryString.version;
1463 case EGL_EXTENSIONS:
1464 return dp->disp.queryString.extensions;
1465 case EGL_CLIENT_APIS:
1466 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001467 default:
1468 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001469 }
Yi Kong48a6cd22018-07-18 10:07:09 -07001470 return setError(EGL_BAD_PARAMETER, (const char *)nullptr);
Mathias Agopianca088332013-03-28 17:44:13 -07001471}
Mathias Agopian518ec112011-05-13 16:21:08 -07001472
1473// ----------------------------------------------------------------------------
1474// EGL 1.1
1475// ----------------------------------------------------------------------------
1476
1477EGLBoolean eglSurfaceAttrib(
1478 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1479{
1480 clearError();
1481
Jesse Hallb29e5e82012-04-04 16:53:42 -07001482 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001483 if (!dp) return EGL_FALSE;
1484
Jesse Hallb29e5e82012-04-04 16:53:42 -07001485 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001486 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001487 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001488
Pablo Ceballosc18be292016-05-31 14:55:42 -07001489 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001490
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001491 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001492 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001493 setError(EGL_BAD_SURFACE, EGL_FALSE);
1494 }
Mathias Agopian65421432017-03-08 11:49:05 -08001495 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1496 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001497 }
1498
Pablo Ceballosc18be292016-05-31 14:55:42 -07001499 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001500 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001501 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001502 }
Mathias Agopian65421432017-03-08 11:49:05 -08001503 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1504 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001505 }
1506
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001507 if (s->setSmpte2086Attribute(attribute, value)) {
1508 return EGL_TRUE;
1509 } else if (s->setCta8613Attribute(attribute, value)) {
1510 return EGL_TRUE;
1511 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001512 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001513 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001514 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001515 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001516}
1517
1518EGLBoolean eglBindTexImage(
1519 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1520{
1521 clearError();
1522
Jesse Hallb29e5e82012-04-04 16:53:42 -07001523 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001524 if (!dp) return EGL_FALSE;
1525
Jesse Hallb29e5e82012-04-04 16:53:42 -07001526 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001527 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001528 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001529
Mathias Agopian518ec112011-05-13 16:21:08 -07001530 egl_surface_t const * const s = get_surface(surface);
1531 if (s->cnx->egl.eglBindTexImage) {
1532 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001533 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001534 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001535 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001536}
1537
1538EGLBoolean eglReleaseTexImage(
1539 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1540{
1541 clearError();
1542
Jesse Hallb29e5e82012-04-04 16:53:42 -07001543 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001544 if (!dp) return EGL_FALSE;
1545
Jesse Hallb29e5e82012-04-04 16:53:42 -07001546 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001547 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001548 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001549
Mathias Agopian518ec112011-05-13 16:21:08 -07001550 egl_surface_t const * const s = get_surface(surface);
1551 if (s->cnx->egl.eglReleaseTexImage) {
1552 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001553 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001554 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001555 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001556}
1557
1558EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1559{
1560 clearError();
1561
Jesse Hallb29e5e82012-04-04 16:53:42 -07001562 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001563 if (!dp) return EGL_FALSE;
1564
1565 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001566 egl_connection_t* const cnx = &gEGLImpl;
1567 if (cnx->dso && cnx->egl.eglSwapInterval) {
1568 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001569 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001570
Mathias Agopian518ec112011-05-13 16:21:08 -07001571 return res;
1572}
1573
1574
1575// ----------------------------------------------------------------------------
1576// EGL 1.2
1577// ----------------------------------------------------------------------------
1578
1579EGLBoolean eglWaitClient(void)
1580{
1581 clearError();
1582
Mathias Agopianada798b2012-02-13 17:09:30 -08001583 egl_connection_t* const cnx = &gEGLImpl;
1584 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001585 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001586
1587 EGLBoolean res;
1588 if (cnx->egl.eglWaitClient) {
1589 res = cnx->egl.eglWaitClient();
1590 } else {
1591 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001592 }
1593 return res;
1594}
1595
1596EGLBoolean eglBindAPI(EGLenum api)
1597{
1598 clearError();
1599
1600 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001601 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001602 }
1603
1604 // bind this API on all EGLs
1605 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001606 egl_connection_t* const cnx = &gEGLImpl;
1607 if (cnx->dso && cnx->egl.eglBindAPI) {
1608 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001609 }
1610 return res;
1611}
1612
1613EGLenum eglQueryAPI(void)
1614{
1615 clearError();
1616
1617 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001618 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001619 }
1620
Mathias Agopianada798b2012-02-13 17:09:30 -08001621 egl_connection_t* const cnx = &gEGLImpl;
1622 if (cnx->dso && cnx->egl.eglQueryAPI) {
1623 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001624 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001625
Mathias Agopian518ec112011-05-13 16:21:08 -07001626 // or, it can only be OpenGL ES
1627 return EGL_OPENGL_ES_API;
1628}
1629
1630EGLBoolean eglReleaseThread(void)
1631{
1632 clearError();
1633
Mathias Agopianada798b2012-02-13 17:09:30 -08001634 egl_connection_t* const cnx = &gEGLImpl;
1635 if (cnx->dso && cnx->egl.eglReleaseThread) {
1636 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001637 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301638
1639 // If there is context bound to the thread, release it
1640 egl_display_t::loseCurrent(get_context(getContext()));
1641
Mathias Agopian518ec112011-05-13 16:21:08 -07001642 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001643 return EGL_TRUE;
1644}
1645
1646EGLSurface eglCreatePbufferFromClientBuffer(
1647 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1648 EGLConfig config, const EGLint *attrib_list)
1649{
1650 clearError();
1651
Yi Kong48a6cd22018-07-18 10:07:09 -07001652 egl_connection_t* cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -07001653 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1654 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001655 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1656 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001657 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001658 }
1659 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1660}
1661
1662// ----------------------------------------------------------------------------
1663// EGL_EGLEXT_VERSION 3
1664// ----------------------------------------------------------------------------
1665
1666EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1667 const EGLint *attrib_list)
1668{
1669 clearError();
1670
Jesse Hallb29e5e82012-04-04 16:53:42 -07001671 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001672 if (!dp) return EGL_FALSE;
1673
Jesse Hallb29e5e82012-04-04 16:53:42 -07001674 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001675 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001676 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001677
1678 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001679 if (s->cnx->egl.eglLockSurfaceKHR) {
1680 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001681 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001682 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001683 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001684}
1685
1686EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1687{
1688 clearError();
1689
Jesse Hallb29e5e82012-04-04 16:53:42 -07001690 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001691 if (!dp) return EGL_FALSE;
1692
Jesse Hallb29e5e82012-04-04 16:53:42 -07001693 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001694 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001695 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001696
1697 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001698 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001699 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001700 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001701 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001702}
1703
1704EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1705 EGLClientBuffer buffer, const EGLint *attrib_list)
1706{
1707 clearError();
1708
Jesse Hallb29e5e82012-04-04 16:53:42 -07001709 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001710 if (!dp) return EGL_NO_IMAGE_KHR;
1711
Jesse Hallb29e5e82012-04-04 16:53:42 -07001712 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001713 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001714
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001715 // Temporary hack: eglImageCreateKHR should accept EGL_GL_COLORSPACE_LINEAR_KHR,
1716 // EGL_GL_COLORSPACE_SRGB_KHR and EGL_GL_COLORSPACE_DEFAULT_EXT if
1717 // EGL_EXT_image_gl_colorspace is supported, but some drivers don't like
1718 // the DEFAULT value and generate an error.
1719 std::vector<EGLint> strippedAttribList;
1720 for (const EGLint *attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
1721 if (attr[0] == EGL_GL_COLORSPACE_KHR &&
1722 dp->haveExtension("EGL_EXT_image_gl_colorspace")) {
1723 if (attr[1] != EGL_GL_COLORSPACE_LINEAR_KHR &&
1724 attr[1] != EGL_GL_COLORSPACE_SRGB_KHR) {
1725 continue;
1726 }
1727 }
1728 strippedAttribList.push_back(attr[0]);
1729 strippedAttribList.push_back(attr[1]);
1730 }
1731 strippedAttribList.push_back(EGL_NONE);
1732
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001733 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1734 egl_connection_t* const cnx = &gEGLImpl;
1735 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1736 result = cnx->egl.eglCreateImageKHR(
1737 dp->disp.dpy,
1738 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001739 target, buffer, strippedAttribList.data());
Mathias Agopian518ec112011-05-13 16:21:08 -07001740 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001741 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001742}
1743
1744EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1745{
1746 clearError();
1747
Jesse Hallb29e5e82012-04-04 16:53:42 -07001748 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001749 if (!dp) return EGL_FALSE;
1750
Steven Holte646a5c52012-06-04 20:02:11 -07001751 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001752 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001753 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001754 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001755 }
Steven Holte646a5c52012-06-04 20:02:11 -07001756 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001757}
1758
1759// ----------------------------------------------------------------------------
1760// EGL_EGLEXT_VERSION 5
1761// ----------------------------------------------------------------------------
1762
1763
1764EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1765{
1766 clearError();
1767
Jesse Hallb29e5e82012-04-04 16:53:42 -07001768 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001769 if (!dp) return EGL_NO_SYNC_KHR;
1770
Mathias Agopian518ec112011-05-13 16:21:08 -07001771 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001772 egl_connection_t* const cnx = &gEGLImpl;
1773 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1774 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001775 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001776 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001777}
1778
1779EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1780{
1781 clearError();
1782
Jesse Hallb29e5e82012-04-04 16:53:42 -07001783 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001784 if (!dp) return EGL_FALSE;
1785
Mathias Agopian518ec112011-05-13 16:21:08 -07001786 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001787 egl_connection_t* const cnx = &gEGLImpl;
1788 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1789 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001790 }
1791 return result;
1792}
1793
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001794EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1795 clearError();
1796
1797 const egl_display_ptr dp = validate_display(dpy);
1798 if (!dp) return EGL_FALSE;
1799
1800 EGLBoolean result = EGL_FALSE;
1801 egl_connection_t* const cnx = &gEGLImpl;
1802 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1803 result = cnx->egl.eglSignalSyncKHR(
1804 dp->disp.dpy, sync, mode);
1805 }
1806 return result;
1807}
1808
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001809EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1810 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001811{
1812 clearError();
1813
Jesse Hallb29e5e82012-04-04 16:53:42 -07001814 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001815 if (!dp) return EGL_FALSE;
1816
Mathias Agopian737b8962017-02-24 14:32:05 -08001817 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001818 egl_connection_t* const cnx = &gEGLImpl;
1819 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1820 result = cnx->egl.eglClientWaitSyncKHR(
1821 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001822 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001823 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001824}
1825
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001826EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1827 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001828{
1829 clearError();
1830
Jesse Hallb29e5e82012-04-04 16:53:42 -07001831 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001832 if (!dp) return EGL_FALSE;
1833
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001834 EGLBoolean result = EGL_FALSE;
1835 egl_connection_t* const cnx = &gEGLImpl;
1836 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1837 result = cnx->egl.eglGetSyncAttribKHR(
1838 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001839 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001840 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001841}
1842
Season Li000d88f2015-07-01 11:39:40 -07001843EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1844{
1845 clearError();
1846
1847 const egl_display_ptr dp = validate_display(dpy);
1848 if (!dp) return EGL_NO_STREAM_KHR;
1849
1850 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1851 egl_connection_t* const cnx = &gEGLImpl;
1852 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1853 result = cnx->egl.eglCreateStreamKHR(
1854 dp->disp.dpy, attrib_list);
1855 }
1856 return result;
1857}
1858
1859EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1860{
1861 clearError();
1862
1863 const egl_display_ptr dp = validate_display(dpy);
1864 if (!dp) return EGL_FALSE;
1865
1866 EGLBoolean result = EGL_FALSE;
1867 egl_connection_t* const cnx = &gEGLImpl;
1868 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1869 result = cnx->egl.eglDestroyStreamKHR(
1870 dp->disp.dpy, stream);
1871 }
1872 return result;
1873}
1874
1875EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1876 EGLenum attribute, EGLint value)
1877{
1878 clearError();
1879
1880 const egl_display_ptr dp = validate_display(dpy);
1881 if (!dp) return EGL_FALSE;
1882
1883 EGLBoolean result = EGL_FALSE;
1884 egl_connection_t* const cnx = &gEGLImpl;
1885 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1886 result = cnx->egl.eglStreamAttribKHR(
1887 dp->disp.dpy, stream, attribute, value);
1888 }
1889 return result;
1890}
1891
1892EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1893 EGLenum attribute, EGLint *value)
1894{
1895 clearError();
1896
1897 const egl_display_ptr dp = validate_display(dpy);
1898 if (!dp) return EGL_FALSE;
1899
1900 EGLBoolean result = EGL_FALSE;
1901 egl_connection_t* const cnx = &gEGLImpl;
1902 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1903 result = cnx->egl.eglQueryStreamKHR(
1904 dp->disp.dpy, stream, attribute, value);
1905 }
1906 return result;
1907}
1908
1909EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1910 EGLenum attribute, EGLuint64KHR *value)
1911{
1912 clearError();
1913
1914 const egl_display_ptr dp = validate_display(dpy);
1915 if (!dp) return EGL_FALSE;
1916
1917 EGLBoolean result = EGL_FALSE;
1918 egl_connection_t* const cnx = &gEGLImpl;
1919 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1920 result = cnx->egl.eglQueryStreamu64KHR(
1921 dp->disp.dpy, stream, attribute, value);
1922 }
1923 return result;
1924}
1925
1926EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1927 EGLenum attribute, EGLTimeKHR *value)
1928{
1929 clearError();
1930
1931 const egl_display_ptr dp = validate_display(dpy);
1932 if (!dp) return EGL_FALSE;
1933
1934 EGLBoolean result = EGL_FALSE;
1935 egl_connection_t* const cnx = &gEGLImpl;
1936 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1937 result = cnx->egl.eglQueryStreamTimeKHR(
1938 dp->disp.dpy, stream, attribute, value);
1939 }
1940 return result;
1941}
1942
1943EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1944 EGLStreamKHR stream, const EGLint *attrib_list)
1945{
1946 clearError();
1947
1948 egl_display_ptr dp = validate_display(dpy);
1949 if (!dp) return EGL_NO_SURFACE;
1950
1951 egl_connection_t* const cnx = &gEGLImpl;
1952 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1953 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1954 dp->disp.dpy, config, stream, attrib_list);
1955 if (surface != EGL_NO_SURFACE) {
Yi Kong48a6cd22018-07-18 10:07:09 -07001956 egl_surface_t* s = new egl_surface_t(dp.get(), config, nullptr, surface,
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001957 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001958 return s;
1959 }
1960 }
1961 return EGL_NO_SURFACE;
1962}
1963
1964EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1965 EGLStreamKHR stream)
1966{
1967 clearError();
1968
1969 const egl_display_ptr dp = validate_display(dpy);
1970 if (!dp) return EGL_FALSE;
1971
1972 EGLBoolean result = EGL_FALSE;
1973 egl_connection_t* const cnx = &gEGLImpl;
1974 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1975 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1976 dp->disp.dpy, stream);
1977 }
1978 return result;
1979}
1980
1981EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1982 EGLStreamKHR stream)
1983{
1984 clearError();
1985
1986 const egl_display_ptr dp = validate_display(dpy);
1987 if (!dp) return EGL_FALSE;
1988
1989 EGLBoolean result = EGL_FALSE;
1990 egl_connection_t* const cnx = &gEGLImpl;
1991 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1992 result = cnx->egl.eglStreamConsumerAcquireKHR(
1993 dp->disp.dpy, stream);
1994 }
1995 return result;
1996}
1997
1998EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1999 EGLStreamKHR stream)
2000{
2001 clearError();
2002
2003 const egl_display_ptr dp = validate_display(dpy);
2004 if (!dp) return EGL_FALSE;
2005
2006 EGLBoolean result = EGL_FALSE;
2007 egl_connection_t* const cnx = &gEGLImpl;
2008 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2009 result = cnx->egl.eglStreamConsumerReleaseKHR(
2010 dp->disp.dpy, stream);
2011 }
2012 return result;
2013}
2014
2015EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2016 EGLDisplay dpy, EGLStreamKHR stream)
2017{
2018 clearError();
2019
2020 const egl_display_ptr dp = validate_display(dpy);
2021 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2022
2023 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2024 egl_connection_t* const cnx = &gEGLImpl;
2025 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2026 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2027 dp->disp.dpy, stream);
2028 }
2029 return result;
2030}
2031
2032EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2033 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2034{
2035 clearError();
2036
2037 const egl_display_ptr dp = validate_display(dpy);
2038 if (!dp) return EGL_NO_STREAM_KHR;
2039
2040 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2041 egl_connection_t* const cnx = &gEGLImpl;
2042 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2043 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2044 dp->disp.dpy, file_descriptor);
2045 }
2046 return result;
2047}
2048
Mathias Agopian518ec112011-05-13 16:21:08 -07002049// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002050// EGL_EGLEXT_VERSION 15
2051// ----------------------------------------------------------------------------
2052
2053EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2054 clearError();
2055 const egl_display_ptr dp = validate_display(dpy);
2056 if (!dp) return EGL_FALSE;
2057 EGLint result = EGL_FALSE;
2058 egl_connection_t* const cnx = &gEGLImpl;
2059 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2060 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2061 }
2062 return result;
2063}
2064
2065// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002066// ANDROID extensions
2067// ----------------------------------------------------------------------------
2068
Jamie Gennis331841b2012-09-06 14:52:00 -07002069EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2070{
2071 clearError();
2072
2073 const egl_display_ptr dp = validate_display(dpy);
2074 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2075
2076 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2077 egl_connection_t* const cnx = &gEGLImpl;
2078 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2079 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2080 }
2081 return result;
2082}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002083
Andy McFadden72841452013-03-01 16:25:32 -08002084EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2085 EGLnsecsANDROID time)
2086{
2087 clearError();
2088
2089 const egl_display_ptr dp = validate_display(dpy);
2090 if (!dp) {
2091 return EGL_FALSE;
2092 }
2093
2094 SurfaceRef _s(dp.get(), surface);
2095 if (!_s.get()) {
2096 setError(EGL_BAD_SURFACE, EGL_FALSE);
2097 return EGL_FALSE;
2098 }
2099
2100 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002101 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002102
2103 return EGL_TRUE;
2104}
2105
Craig Donner60761072017-01-27 12:30:44 -08002106EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2107 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002108 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2109 // this function cannot be implemented when this libEGL is built for
2110 // vendors.
2111#ifndef __ANDROID_VNDK__
Yi Kong48a6cd22018-07-18 10:07:09 -07002112 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer) nullptr);
Mathias Agopian61963402017-02-24 16:38:15 -08002113 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002114#else
Yi Kong48a6cd22018-07-18 10:07:09 -07002115 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer) nullptr);
Jiyong Parka243e5d2017-06-21 12:26:51 +09002116#endif
Craig Donner60761072017-01-27 12:30:44 -08002117}
2118
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002119// ----------------------------------------------------------------------------
2120// NVIDIA extensions
2121// ----------------------------------------------------------------------------
2122EGLuint64NV eglGetSystemTimeFrequencyNV()
2123{
2124 clearError();
2125
2126 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002127 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002128 }
2129
2130 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002131 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002132
Mathias Agopianada798b2012-02-13 17:09:30 -08002133 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2134 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002135 }
2136
Mathias Agopian737b8962017-02-24 14:32:05 -08002137 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002138}
2139
2140EGLuint64NV eglGetSystemTimeNV()
2141{
2142 clearError();
2143
2144 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002145 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002146 }
2147
2148 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002149 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002150
Mathias Agopianada798b2012-02-13 17:09:30 -08002151 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2152 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002153 }
2154
Mathias Agopian737b8962017-02-24 14:32:05 -08002155 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002156}
Dan Stozaa894d082015-02-19 15:27:36 -08002157
2158// ----------------------------------------------------------------------------
2159// Partial update extension
2160// ----------------------------------------------------------------------------
2161EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2162 EGLint *rects, EGLint n_rects)
2163{
2164 clearError();
2165
2166 const egl_display_ptr dp = validate_display(dpy);
2167 if (!dp) {
2168 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2169 return EGL_FALSE;
2170 }
2171
2172 SurfaceRef _s(dp.get(), surface);
2173 if (!_s.get()) {
2174 setError(EGL_BAD_SURFACE, EGL_FALSE);
2175 return EGL_FALSE;
2176 }
2177
2178 egl_surface_t const * const s = get_surface(surface);
2179 if (s->cnx->egl.eglSetDamageRegionKHR) {
2180 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2181 rects, n_rects);
2182 }
2183
2184 return EGL_FALSE;
2185}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002186
Brian Anderson1049d1d2016-12-16 17:25:57 -08002187EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2188 EGLuint64KHR *frameId) {
2189 clearError();
2190
2191 const egl_display_ptr dp = validate_display(dpy);
2192 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002193 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002194 }
2195
2196 SurfaceRef _s(dp.get(), surface);
2197 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002198 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002199 }
2200
2201 egl_surface_t const * const s = get_surface(surface);
2202
Mathias Agopian65421432017-03-08 11:49:05 -08002203 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002204 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002205 }
2206
2207 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002208 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002209
Mathias Agopian65421432017-03-08 11:49:05 -08002210 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002211 // This should not happen. Return an error that is not in the spec
2212 // so it's obvious something is very wrong.
2213 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002214 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002215 }
2216
2217 *frameId = nextFrameId;
2218 return EGL_TRUE;
2219}
2220
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002221EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2222 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2223{
2224 clearError();
2225
2226 const egl_display_ptr dp = validate_display(dpy);
2227 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002228 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002229 }
2230
2231 SurfaceRef _s(dp.get(), surface);
2232 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002233 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002234 }
2235
2236 egl_surface_t const * const s = get_surface(surface);
2237
Mathias Agopian65421432017-03-08 11:49:05 -08002238 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002239 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002240 }
2241
2242 nsecs_t* compositeDeadline = nullptr;
2243 nsecs_t* compositeInterval = nullptr;
2244 nsecs_t* compositeToPresentLatency = nullptr;
2245
2246 for (int i = 0; i < numTimestamps; i++) {
2247 switch (names[i]) {
2248 case EGL_COMPOSITE_DEADLINE_ANDROID:
2249 compositeDeadline = &values[i];
2250 break;
2251 case EGL_COMPOSITE_INTERVAL_ANDROID:
2252 compositeInterval = &values[i];
2253 break;
2254 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2255 compositeToPresentLatency = &values[i];
2256 break;
2257 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002258 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002259 }
2260 }
2261
Mathias Agopian65421432017-03-08 11:49:05 -08002262 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002263 compositeDeadline, compositeInterval, compositeToPresentLatency);
2264
2265 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002266 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002267 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002268 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002269 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002270 default:
2271 // This should not happen. Return an error that is not in the spec
2272 // so it's obvious something is very wrong.
2273 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002274 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002275 }
2276}
2277
2278EGLBoolean eglGetCompositorTimingSupportedANDROID(
2279 EGLDisplay dpy, EGLSurface surface, EGLint name)
2280{
2281 clearError();
2282
2283 const egl_display_ptr dp = validate_display(dpy);
2284 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002285 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002286 }
2287
2288 SurfaceRef _s(dp.get(), surface);
2289 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002290 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002291 }
2292
2293 egl_surface_t const * const s = get_surface(surface);
2294
Mathias Agopian65421432017-03-08 11:49:05 -08002295 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002296 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002297 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002298 }
2299
2300 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002301 case EGL_COMPOSITE_DEADLINE_ANDROID:
2302 case EGL_COMPOSITE_INTERVAL_ANDROID:
2303 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2304 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002305 default:
2306 return EGL_FALSE;
2307 }
2308}
2309
Pablo Ceballosc18be292016-05-31 14:55:42 -07002310EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002311 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002312 EGLnsecsANDROID *values)
2313{
2314 clearError();
2315
2316 const egl_display_ptr dp = validate_display(dpy);
2317 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002318 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002319 }
2320
2321 SurfaceRef _s(dp.get(), surface);
2322 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002323 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002324 }
2325
2326 egl_surface_t const * const s = get_surface(surface);
2327
Mathias Agopian65421432017-03-08 11:49:05 -08002328 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002329 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002330 }
2331
Brian Andersondbd0ea82016-07-22 09:38:59 -07002332 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002333 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002334 nsecs_t* latchTime = nullptr;
2335 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002336 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002337 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002338 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002339 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002340 nsecs_t* releaseTime = nullptr;
2341
2342 for (int i = 0; i < numTimestamps; i++) {
2343 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002344 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2345 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002346 break;
2347 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2348 acquireTime = &values[i];
2349 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002350 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2351 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002352 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002353 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2354 firstRefreshStartTime = &values[i];
2355 break;
2356 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2357 lastRefreshStartTime = &values[i];
2358 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002359 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2360 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002361 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002362 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2363 displayPresentTime = &values[i];
2364 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002365 case EGL_DEQUEUE_READY_TIME_ANDROID:
2366 dequeueReadyTime = &values[i];
2367 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002368 case EGL_READS_DONE_TIME_ANDROID:
2369 releaseTime = &values[i];
2370 break;
2371 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002372 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002373 }
2374 }
2375
Mathias Agopian65421432017-03-08 11:49:05 -08002376 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002377 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002378 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002379 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002380
Brian Anderson069b3652016-07-22 10:32:47 -07002381 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002382 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002383 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002384 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002385 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002386 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002387 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002388 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002389 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2390 default:
2391 // This should not happen. Return an error that is not in the spec
2392 // so it's obvious something is very wrong.
2393 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2394 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002395 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002396}
2397
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002398EGLBoolean eglGetFrameTimestampSupportedANDROID(
2399 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002400{
2401 clearError();
2402
2403 const egl_display_ptr dp = validate_display(dpy);
2404 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002405 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002406 }
2407
2408 SurfaceRef _s(dp.get(), surface);
2409 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002410 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002411 }
2412
2413 egl_surface_t const * const s = get_surface(surface);
2414
Mathias Agopian65421432017-03-08 11:49:05 -08002415 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002416 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002417 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002418 }
2419
2420 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002421 case EGL_COMPOSITE_DEADLINE_ANDROID:
2422 case EGL_COMPOSITE_INTERVAL_ANDROID:
2423 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002424 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002425 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002426 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2427 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2428 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002429 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002430 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002431 case EGL_READS_DONE_TIME_ANDROID:
2432 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002433 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2434 int value = 0;
2435 window->query(window,
2436 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2437 return value == 0 ? EGL_FALSE : EGL_TRUE;
2438 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002439 default:
2440 return EGL_FALSE;
2441 }
2442}