blob: 3615577f151198cf96af13f945e21bff040f27ca [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
Craig Donner60761072017-01-27 12:30:44 -080029#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080030#include <private/android/AHardwareBufferHelpers.h>
31
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Mathias Agopian65421432017-03-08 11:49:05 -080036#include <condition_variable>
37#include <deque>
38#include <mutex>
39#include <unordered_map>
40#include <string>
41#include <thread>
Mathias Agopian518ec112011-05-13 16:21:08 -070042
Mathias Agopian39c24a22013-04-04 23:17:56 -070043#include "../egl_impl.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070044
45#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070046#include "egl_object.h"
47#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080048#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070049
50using namespace android;
51
52// ----------------------------------------------------------------------------
53
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070054namespace android {
55
Mathias Agopian65421432017-03-08 11:49:05 -080056using nsecs_t = int64_t;
57
Mathias Agopian518ec112011-05-13 16:21:08 -070058struct extention_map_t {
59 const char* name;
60 __eglMustCastToProperFunctionPointerType address;
61};
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063/*
Jesse Hall21558da2013-08-06 15:31:22 -070064 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070065 *
Jesse Hall21558da2013-08-06 15:31:22 -070066 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
67 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070068 *
Jesse Hall21558da2013-08-06 15:31:22 -070069 * The rest (gExtensionString) depend on support in the EGL driver, and are
70 * only available if the driver supports them. However, some of these must be
71 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080072 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070073 * the mandatory extensions are present and may not function properly if some
74 * are missing.
75 *
76 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070077 */
Mathias Agopian737b8962017-02-24 14:32:05 -080078
Mathias Agopian311b4792017-02-28 15:00:49 -080079extern char const * const gBuiltinExtensionString;
80extern char const * const gExtensionString;
Mathias Agopian737b8962017-02-24 14:32:05 -080081
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -060082// clang-format off
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -070083// Extensions implemented by the EGL wrapper.
Mathias Agopian311b4792017-02-28 15:00:49 -080084char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070085 "EGL_KHR_get_all_proc_addresses "
86 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080087 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080088 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080089 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070090 "EGL_ANDROID_get_frame_timestamps "
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070091 "EGL_EXT_surface_SMPTE2086_metadata "
92 "EGL_EXT_surface_CTA861_3_metadata "
Jesse Hall21558da2013-08-06 15:31:22 -070093 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080094
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -070095// Whitelist of extensions exposed to applications if implemented in the vendor driver.
Mathias Agopian311b4792017-02-28 15:00:49 -080096char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_image " // mandatory
98 "EGL_KHR_image_base " // mandatory
Krzysztof Kosiński41171c52018-04-19 21:38:54 -070099 "EGL_EXT_image_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700100 "EGL_KHR_image_pixmap "
101 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700102 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700104 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700105 "EGL_KHR_gl_texture_cubemap_image "
106 "EGL_KHR_gl_renderbuffer_image "
107 "EGL_KHR_reusable_sync "
108 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700109 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700110 "EGL_KHR_config_attribs "
111 "EGL_KHR_surfaceless_context "
112 "EGL_KHR_stream "
113 "EGL_KHR_stream_fifo "
114 "EGL_KHR_stream_producer_eglsurface "
115 "EGL_KHR_stream_consumer_gltexture "
116 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700117 "EGL_EXT_create_context_robustness "
118 "EGL_NV_system_time "
119 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700120 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700121 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800122 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700123 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800124 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700125 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700126 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700127 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700128 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000129 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800130 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700131 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600132// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700133
134// extensions not exposed to applications but used by the ANDROID system
135// "EGL_ANDROID_blob_cache " // strongly recommended
136// "EGL_IMG_hibernate_process " // optional
137// "EGL_ANDROID_native_fence_sync " // strongly recommended
138// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700139// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700140
141/*
142 * EGL Extensions entry-points exposed to 3rd party applications
143 * (keep in sync with gExtensionString above)
144 *
145 */
146static const extention_map_t sExtensionMap[] = {
147 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700148 { "eglLockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
150 { "eglUnlockSurfaceKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700152
153 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 { "eglCreateImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
156 { "eglDestroyImageKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700158
159 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
160 { "eglCreateSyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
162 { "eglDestroySyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
164 { "eglClientWaitSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
166 { "eglSignalSyncKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
168 { "eglGetSyncAttribKHR",
169 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
170
171 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800172 { "eglGetSystemTimeFrequencyNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
174 { "eglGetSystemTimeNV",
175 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700176
Mathias Agopian2bb71682013-03-27 17:32:41 -0700177 // EGL_KHR_wait_sync
178 { "eglWaitSyncKHR",
179 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700180
181 // EGL_ANDROID_presentation_time
182 { "eglPresentationTimeANDROID",
183 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800184
185 // EGL_KHR_swap_buffers_with_damage
186 { "eglSwapBuffersWithDamageKHR",
187 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
188
Craig Donner60761072017-01-27 12:30:44 -0800189 // EGL_ANDROID_get_native_client_buffer
190 { "eglGetNativeClientBufferANDROID",
191 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
192
Dan Stozaa894d082015-02-19 15:27:36 -0800193 // EGL_KHR_partial_update
194 { "eglSetDamageRegionKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700196
197 { "eglCreateStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
199 { "eglDestroyStreamKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
201 { "eglStreamAttribKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
203 { "eglQueryStreamKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
205 { "eglQueryStreamu64KHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
207 { "eglQueryStreamTimeKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
209 { "eglCreateStreamProducerSurfaceKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
211 { "eglStreamConsumerGLTextureExternalKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
213 { "eglStreamConsumerAcquireKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
215 { "eglStreamConsumerReleaseKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
217 { "eglGetStreamFileDescriptorKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
219 { "eglCreateStreamFromFileDescriptorKHR",
220 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700221
222 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800223 { "eglGetNextFrameIdANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800225 { "eglGetCompositorTimingANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
227 { "eglGetCompositorTimingSupportedANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700229 { "eglGetFrameTimestampsANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800231 { "eglGetFrameTimestampSupportedANDROID",
232 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700233
234 // EGL_ANDROID_native_fence_sync
235 { "eglDupNativeFenceFDANDROID",
236 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700237};
238
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700239/*
240 * These extensions entry-points should not be exposed to applications.
241 * They're used internally by the Android EGL layer.
242 */
243#define FILTER_EXTENSIONS(procname) \
244 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
245 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700246 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800249static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
250
Mathias Agopian518ec112011-05-13 16:21:08 -0700251static int sGLExtentionSlot = 0;
252static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
253
254static void(*findProcAddress(const char* name,
255 const extention_map_t* map, size_t n))() {
256 for (uint32_t i=0 ; i<n ; i++) {
257 if (!strcmp(name, map[i].name)) {
258 return map[i].address;
259 }
260 }
261 return NULL;
262}
263
264// ----------------------------------------------------------------------------
265
Mathias Agopian518ec112011-05-13 16:21:08 -0700266extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
267extern EGLBoolean egl_init_drivers();
268extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700269extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271} // namespace android;
272
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700273
Mathias Agopian518ec112011-05-13 16:21:08 -0700274// ----------------------------------------------------------------------------
275
276static inline void clearError() { egl_tls_t::clearError(); }
277static inline EGLContext getContext() { return egl_tls_t::getContext(); }
278
279// ----------------------------------------------------------------------------
280
281EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
282{
Jesse Hall1508ae62017-01-19 17:43:26 -0800283 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 clearError();
285
Dan Stozac3289c42014-01-17 11:38:34 -0800286 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700287 if (index >= NUM_DISPLAYS) {
288 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
289 }
290
291 if (egl_init_drivers() == EGL_FALSE) {
292 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
293 }
294
295 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
296 return dpy;
297}
298
299// ----------------------------------------------------------------------------
300// Initialization
301// ----------------------------------------------------------------------------
302
303EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
304{
305 clearError();
306
Jesse Hallb29e5e82012-04-04 16:53:42 -0700307 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800308 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700309
310 EGLBoolean res = dp->initialize(major, minor);
311
312 return res;
313}
314
315EGLBoolean eglTerminate(EGLDisplay dpy)
316{
317 // NOTE: don't unload the drivers b/c some APIs can be called
318 // after eglTerminate() has been called. eglTerminate() only
319 // terminates an EGLDisplay, not a EGL itself.
320
321 clearError();
322
Jesse Hallb29e5e82012-04-04 16:53:42 -0700323 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800324 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700325
326 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800327
Mathias Agopian518ec112011-05-13 16:21:08 -0700328 return res;
329}
330
331// ----------------------------------------------------------------------------
332// configuration
333// ----------------------------------------------------------------------------
334
335EGLBoolean eglGetConfigs( EGLDisplay dpy,
336 EGLConfig *configs,
337 EGLint config_size, EGLint *num_config)
338{
339 clearError();
340
Jesse Hallb29e5e82012-04-04 16:53:42 -0700341 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700342 if (!dp) return EGL_FALSE;
343
Mathias Agopian7773c432012-02-13 20:06:08 -0800344 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800345 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700346 }
347
Mathias Agopian7773c432012-02-13 20:06:08 -0800348 EGLBoolean res = EGL_FALSE;
349 *num_config = 0;
350
351 egl_connection_t* const cnx = &gEGLImpl;
352 if (cnx->dso) {
353 res = cnx->egl.eglGetConfigs(
354 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700355 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800356
357 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700358}
359
360EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
361 EGLConfig *configs, EGLint config_size,
362 EGLint *num_config)
363{
364 clearError();
365
Jesse Hallb29e5e82012-04-04 16:53:42 -0700366 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700367 if (!dp) return EGL_FALSE;
368
369 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800370 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700371 }
372
Mathias Agopian518ec112011-05-13 16:21:08 -0700373 EGLBoolean res = EGL_FALSE;
374 *num_config = 0;
375
Mathias Agopianada798b2012-02-13 17:09:30 -0800376 egl_connection_t* const cnx = &gEGLImpl;
377 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700378 if (attrib_list) {
379 char value[PROPERTY_VALUE_MAX];
380 property_get("debug.egl.force_msaa", value, "false");
381
382 if (!strcmp(value, "true")) {
383 size_t attribCount = 0;
384 EGLint attrib = attrib_list[0];
385
386 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700387 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700388 const EGLint *attribRendererable = NULL;
389 const EGLint *attribCaveat = NULL;
390
391 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700392 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700393 while (attrib != EGL_NONE) {
394 attrib = attrib_list[attribCount];
395 switch (attrib) {
396 case EGL_RENDERABLE_TYPE:
397 attribRendererable = &attrib_list[attribCount];
398 break;
399 case EGL_CONFIG_CAVEAT:
400 attribCaveat = &attrib_list[attribCount];
401 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800402 default:
403 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700404 }
405 attribCount++;
406 }
407
408 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
409 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800410
Romain Guy1cffc802012-10-15 18:13:05 -0700411 // Insert 2 extra attributes to force-enable MSAA 4x
412 EGLint aaAttribs[attribCount + 4];
413 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
414 aaAttribs[1] = 1;
415 aaAttribs[2] = EGL_SAMPLES;
416 aaAttribs[3] = 4;
417
418 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
419
420 EGLint numConfigAA;
421 EGLBoolean resAA = cnx->egl.eglChooseConfig(
422 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
423
424 if (resAA == EGL_TRUE && numConfigAA > 0) {
425 ALOGD("Enabling MSAA 4x");
426 *num_config = numConfigAA;
427 return resAA;
428 }
429 }
430 }
431 }
432
Mathias Agopian7773c432012-02-13 20:06:08 -0800433 res = cnx->egl.eglChooseConfig(
434 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700435 }
436 return res;
437}
438
439EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
440 EGLint attribute, EGLint *value)
441{
442 clearError();
443
Jesse Hallb29e5e82012-04-04 16:53:42 -0700444 egl_connection_t* cnx = NULL;
445 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
446 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800447
Mathias Agopian518ec112011-05-13 16:21:08 -0700448 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800449 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700450}
451
452// ----------------------------------------------------------------------------
453// surfaces
454// ----------------------------------------------------------------------------
455
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700456// Translates EGL color spaces to Android data spaces.
457static android_dataspace dataSpaceFromEGLColorSpace(EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700458 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800459 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800461 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600462 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
463 return HAL_DATASPACE_DISPLAY_P3;
464 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
465 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600466 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
467 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600468 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
469 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700470 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT) {
471 return HAL_DATASPACE_BT2020_LINEAR;
472 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_PQ_EXT) {
473 return HAL_DATASPACE_BT2020_PQ;
Jesse Hallc2e41222013-08-08 13:40:22 -0700474 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700475 return HAL_DATASPACE_UNKNOWN;
Jesse Hallc2e41222013-08-08 13:40:22 -0700476}
477
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700478// Returns a list of color spaces understood by the vendor EGL driver.
479static std::vector<EGLint> getDriverColorSpaces(egl_display_ptr dp,
480 android_pixel_format format) {
481 std::vector<EGLint> colorSpaces;
482 if (!dp->hasColorSpaceSupport) return colorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700483
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700484 // OpenGL drivers only support sRGB encoding with 8-bit formats.
485 // RGB_888 is never returned by getNativePixelFormat, but is included for completeness.
486 const bool formatSupportsSRGBEncoding =
487 format == HAL_PIXEL_FORMAT_RGBA_8888 || format == HAL_PIXEL_FORMAT_RGBX_8888 ||
488 format == HAL_PIXEL_FORMAT_RGB_888;
489 const bool formatIsFloatingPoint = format == HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700490
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700491 if (formatSupportsSRGBEncoding) {
492 // sRGB and linear are always supported when color space support is present.
493 colorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
494 colorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
495 // DCI-P3 uses the sRGB transfer function, so it's only relevant for 8-bit formats.
496 if (findExtension(dp->disp.queryString.extensions,
497 "EGL_EXT_gl_colorspace_display_p3")) {
498 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600499 }
500 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700501
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700502 // According to the spec, scRGB is only supported for floating point formats.
503 // For non-linear scRGB, the application is responsible for applying the
504 // transfer function.
505 if (formatIsFloatingPoint) {
506 if (findExtension(dp->disp.queryString.extensions,
507 "EGL_EXT_gl_colorspace_scrgb")) {
508 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_EXT);
509 }
510 if (findExtension(dp->disp.queryString.extensions,
511 "EGL_EXT_gl_colorspace_scrgb_linear")) {
512 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
513 }
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600514 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700515
516 // BT2020 can be used with any pixel format. PQ encoding must be applied by the
517 // application and does not affect the behavior of OpenGL.
518 if (findExtension(dp->disp.queryString.extensions,
519 "EGL_EXT_gl_colorspace_bt2020_linear")) {
520 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
521 }
522 if (findExtension(dp->disp.queryString.extensions,
523 "EGL_EXT_gl_colorspace_bt2020_pq")) {
524 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
525 }
526
527 // Linear DCI-P3 simply uses different primaries than standard RGB and thus
528 // can be used with any pixel format.
529 if (findExtension(dp->disp.queryString.extensions,
530 "EGL_EXT_gl_colorspace_display_p3_linear")) {
531 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
532 }
533 return colorSpaces;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600534}
535
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700536// Cleans up color space related parameters that the driver does not understand.
537// If there is no color space attribute in attrib_list, colorSpace is left
538// unmodified.
539static EGLBoolean processAttributes(egl_display_ptr dp, NativeWindowType window,
540 android_pixel_format format, const EGLint* attrib_list,
541 EGLint* colorSpace,
542 std::vector<EGLint>* strippedAttribList) {
543 for (const EGLint* attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
544 bool copyAttribute = true;
545 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
546 // Fail immediately if the driver doesn't have color space support at all.
547 if (!dp->hasColorSpaceSupport) return false;
548 *colorSpace = attr[1];
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600549
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700550 // Strip the attribute if the driver doesn't understand it.
551 copyAttribute = false;
552 std::vector<EGLint> driverColorSpaces = getDriverColorSpaces(dp, format);
553 for (auto driverColorSpace : driverColorSpaces) {
554 if (attr[1] == driverColorSpace) {
555 copyAttribute = true;
556 break;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600557 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600558 }
559 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700560 if (copyAttribute) {
561 strippedAttribList->push_back(attr[0]);
562 strippedAttribList->push_back(attr[1]);
563 }
564 }
565 // Terminate the attribute list.
566 strippedAttribList->push_back(EGL_NONE);
567
568 // If the passed color space has wide color gamut, check whether the target native window
569 // supports wide color.
570 const bool colorSpaceIsNarrow =
571 *colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
572 *colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR;
573 if (window && !colorSpaceIsNarrow) {
574 bool windowSupportsWideColor = true;
575 // Ordinarily we'd put a call to native_window_get_wide_color_support
576 // at the beginning of the function so that we'll have the
577 // result when needed elsewhere in the function.
578 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
579 // SurfaceFlinger is required to answer the call below we would
580 // end up in a deadlock situation. By moving the call to only happen
581 // if the application has specifically asked for wide-color we avoid
582 // the deadlock with SurfaceFlinger since it will not ask for a
583 // wide-color surface.
584 int err = native_window_get_wide_color_support(window, &windowSupportsWideColor);
585
586 if (err) {
587 ALOGE("processAttributes: invalid window (win=%p) "
588 "failed (%#x) (already connected to another API?)",
589 window, err);
590 return false;
591 }
592 if (!windowSupportsWideColor) {
593 // Application has asked for a wide-color colorspace but
594 // wide-color support isn't available on the display the window is on.
595 return false;
596 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600597 }
598 return true;
599}
600
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700601// Gets the native pixel format corrsponding to the passed EGLConfig.
602void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config,
603 android_pixel_format* format) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600604 // Set the native window's buffers format to match what this config requests.
605 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
606 // of our native format. So if sRGB gamma is requested, we have to
607 // modify the EGLconfig's format before setting the native window's
608 // format.
609
610 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
611 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
612
613 EGLint a = 0;
614 EGLint r, g, b;
615 r = g = b = 0;
616 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
617 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
618 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
619 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
620 EGLint colorDepth = r + g + b;
621
622 // Today, the driver only understands sRGB and linear on 888X
623 // formats. Strip other colorspaces from the attribute list and
624 // only use them to set the dataspace via
625 // native_window_set_buffers_dataspace
626 // if pixel format is RGBX 8888
627 // TBD: Can test for future extensions that indicate that driver
628 // handles requested color space and we can let it through.
629 // allow SRGB and LINEAR. All others need to be stripped.
630 // else if 565, 4444
631 // TBD: Can we assume these are supported if 8888 is?
632 // else if FP16 or 1010102
633 // strip colorspace from attribs.
634 // endif
635 if (a == 0) {
636 if (colorDepth <= 16) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700637 *format = HAL_PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600638 } else {
639 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
640 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700641 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600642 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700643 *format = HAL_PIXEL_FORMAT_RGBX_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600644 }
645 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700646 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600647 }
648 }
649 } else {
650 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
651 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700652 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600653 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700654 *format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600655 }
656 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700657 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600658 }
659 }
660}
661
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700662EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
663 android_smpte2086_metadata smpteMetadata;
664 if (s->getSmpte2086Metadata(smpteMetadata)) {
665 int err =
666 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
667 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700668 if (err != 0) {
669 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
670 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700671 return EGL_FALSE;
672 }
673 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700674 android_cta861_3_metadata cta8613Metadata;
675 if (s->getCta8613Metadata(cta8613Metadata)) {
676 int err =
677 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
678 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700679 if (err != 0) {
680 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
681 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700682 return EGL_FALSE;
683 }
684 }
685 return EGL_TRUE;
686}
687
Mathias Agopian518ec112011-05-13 16:21:08 -0700688EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
689 NativeWindowType window,
690 const EGLint *attrib_list)
691{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700692 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700693 clearError();
694
Jesse Hallb29e5e82012-04-04 16:53:42 -0700695 egl_connection_t* cnx = NULL;
696 egl_display_ptr dp = validate_display_connection(dpy, cnx);
697 if (dp) {
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800698 if (!window) {
699 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
700 }
701
702 int value = 0;
703 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
704 if (!value) {
705 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
706 }
707
Andy McFaddend566ce32014-01-07 15:54:17 -0800708 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800709 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800710 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
711 "failed (%#x) (already connected to another API?)",
712 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700713 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700714 }
715
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700716 EGLDisplay iDpy = dp->disp.dpy;
717 android_pixel_format format;
718 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600719
720 // now select correct colorspace and dataspace based on user's attribute list
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700721 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
722 std::vector<EGLint> strippedAttribList;
723 if (!processAttributes(dp, window, format, attrib_list, &colorSpace,
724 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600725 ALOGE("error invalid colorspace: %d", colorSpace);
726 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700727 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700728 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800729
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700730 {
Jesse Hallc2e41222013-08-08 13:40:22 -0700731 int err = native_window_set_buffers_format(window, format);
732 if (err != 0) {
733 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700734 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700735 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
736 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
737 }
738 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700739
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700740 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
741 if (dataSpace != HAL_DATASPACE_UNKNOWN) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800742 int err = native_window_set_buffers_data_space(window, dataSpace);
743 if (err != 0) {
744 ALOGE("error setting native window pixel dataSpace: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700745 strerror(-err), err);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800746 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
747 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
748 }
749 }
750
Jamie Gennis59769462011-11-19 18:04:43 -0800751 // the EGL spec requires that a new EGLSurface default to swap interval
752 // 1, so explicitly set that on the window here.
753 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
754 anw->setSwapInterval(anw, 1);
755
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800757 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600759 egl_surface_t* s =
760 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700761 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700762 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700763
764 // EGLSurface creation failed
765 native_window_set_buffers_format(window, 0);
766 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 }
768 return EGL_NO_SURFACE;
769}
770
771EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
772 NativePixmapType pixmap,
773 const EGLint *attrib_list)
774{
775 clearError();
776
Jesse Hallb29e5e82012-04-04 16:53:42 -0700777 egl_connection_t* cnx = NULL;
778 egl_display_ptr dp = validate_display_connection(dpy, cnx);
779 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700780 EGLDisplay iDpy = dp->disp.dpy;
781 android_pixel_format format;
782 getNativePixelFormat(iDpy, cnx, config, &format);
783
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600784 // now select a corresponding sRGB format if needed
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700785 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
786 std::vector<EGLint> strippedAttribList;
787 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
788 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600789 ALOGE("error invalid colorspace: %d", colorSpace);
790 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
791 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700792 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600793
Mathias Agopian518ec112011-05-13 16:21:08 -0700794 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800795 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700796 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600797 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 return s;
799 }
800 }
801 return EGL_NO_SURFACE;
802}
803
804EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
805 const EGLint *attrib_list)
806{
807 clearError();
808
Jesse Hallb29e5e82012-04-04 16:53:42 -0700809 egl_connection_t* cnx = NULL;
810 egl_display_ptr dp = validate_display_connection(dpy, cnx);
811 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600812 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700813 android_pixel_format format;
814 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600815
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700816 // Select correct colorspace based on user's attribute list
817 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
818 std::vector<EGLint> strippedAttribList;
819 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
820 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600821 ALOGE("error invalid colorspace: %d", colorSpace);
822 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
823 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700824 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600825
Mathias Agopian518ec112011-05-13 16:21:08 -0700826 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800827 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700828 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600829 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700830 return s;
831 }
832 }
833 return EGL_NO_SURFACE;
834}
Jesse Hall47743382013-02-08 11:13:46 -0800835
Mathias Agopian518ec112011-05-13 16:21:08 -0700836EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
837{
838 clearError();
839
Jesse Hallb29e5e82012-04-04 16:53:42 -0700840 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700841 if (!dp) return EGL_FALSE;
842
Jesse Hallb29e5e82012-04-04 16:53:42 -0700843 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700844 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800845 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700846
847 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800848 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700849 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700850 _s.terminate();
851 }
852 return result;
853}
854
855EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
856 EGLint attribute, EGLint *value)
857{
858 clearError();
859
Jesse Hallb29e5e82012-04-04 16:53:42 -0700860 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700861 if (!dp) return EGL_FALSE;
862
Jesse Hallb29e5e82012-04-04 16:53:42 -0700863 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700864 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800865 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700866
Mathias Agopian518ec112011-05-13 16:21:08 -0700867 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700868 if (s->getColorSpaceAttribute(attribute, value)) {
869 return EGL_TRUE;
870 } else if (s->getSmpte2086Attribute(attribute, value)) {
871 return EGL_TRUE;
872 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600873 return EGL_TRUE;
874 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700875 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700876}
877
Jamie Gennise8696a42012-01-15 18:54:57 -0800878void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800879 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800880 clearError();
881
Jesse Hallb29e5e82012-04-04 16:53:42 -0700882 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800883 if (!dp) {
884 return;
885 }
886
Jesse Hallb29e5e82012-04-04 16:53:42 -0700887 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800888 if (!_s.get()) {
889 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800890 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800891}
892
Mathias Agopian518ec112011-05-13 16:21:08 -0700893// ----------------------------------------------------------------------------
894// Contexts
895// ----------------------------------------------------------------------------
896
897EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
898 EGLContext share_list, const EGLint *attrib_list)
899{
900 clearError();
901
Jesse Hallb29e5e82012-04-04 16:53:42 -0700902 egl_connection_t* cnx = NULL;
903 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700904 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700905 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700906 if (!ContextRef(dp.get(), share_list).get()) {
907 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
908 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700909 egl_context_t* const c = get_context(share_list);
910 share_list = c->context;
911 }
912 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800913 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700914 if (context != EGL_NO_CONTEXT) {
915 // figure out if it's a GLESv1 or GLESv2
916 int version = 0;
917 if (attrib_list) {
918 while (*attrib_list != EGL_NONE) {
919 GLint attr = *attrib_list++;
920 GLint value = *attrib_list++;
921 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
922 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800923 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800924 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800925 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700926 }
927 }
928 };
929 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700930 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
931 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700932 return c;
933 }
934 }
935 return EGL_NO_CONTEXT;
936}
937
938EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
939{
940 clearError();
941
Jesse Hallb29e5e82012-04-04 16:53:42 -0700942 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700943 if (!dp)
944 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700945
Jesse Hallb29e5e82012-04-04 16:53:42 -0700946 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700947 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800948 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800949
Mathias Agopian518ec112011-05-13 16:21:08 -0700950 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800951 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700952 if (result == EGL_TRUE) {
953 _c.terminate();
954 }
955 return result;
956}
957
Mathias Agopian518ec112011-05-13 16:21:08 -0700958EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
959 EGLSurface read, EGLContext ctx)
960{
961 clearError();
962
Jesse Hallb29e5e82012-04-04 16:53:42 -0700963 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800964 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700965
Mathias Agopian5b287a62011-05-16 18:58:55 -0700966 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
967 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
968 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
970 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800971 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700972 }
973
974 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700975 ContextRef _c(dp.get(), ctx);
976 SurfaceRef _d(dp.get(), draw);
977 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700978
979 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700980 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700981 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800982 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 }
984
985 // these are the underlying implementation's object
986 EGLContext impl_ctx = EGL_NO_CONTEXT;
987 EGLSurface impl_draw = EGL_NO_SURFACE;
988 EGLSurface impl_read = EGL_NO_SURFACE;
989
990 // these are our objects structs passed in
991 egl_context_t * c = NULL;
992 egl_surface_t const * d = NULL;
993 egl_surface_t const * r = NULL;
994
995 // these are the current objects structs
996 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800997
Mathias Agopian518ec112011-05-13 16:21:08 -0700998 if (ctx != EGL_NO_CONTEXT) {
999 c = get_context(ctx);
1000 impl_ctx = c->context;
1001 } else {
1002 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001003 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1004 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001005 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001006 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001007 if (cur_c == NULL) {
1008 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001009 // not an error, there is just no current context.
1010 return EGL_TRUE;
1011 }
1012 }
1013
1014 // retrieve the underlying implementation's draw EGLSurface
1015 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001016 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001018 impl_draw = d->surface;
1019 }
1020
1021 // retrieve the underlying implementation's read EGLSurface
1022 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001023 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001024 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001025 impl_read = r->surface;
1026 }
1027
Mathias Agopian518ec112011-05-13 16:21:08 -07001028
Jesse Hallb29e5e82012-04-04 16:53:42 -07001029 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001030 draw, read, ctx,
1031 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001032
1033 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001034 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001035 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1036 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001037 _c.acquire();
1038 _r.acquire();
1039 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001040 } else {
1041 setGLHooksThreadSpecific(&gHooksNoContext);
1042 egl_tls_t::setContext(EGL_NO_CONTEXT);
1043 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001044 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001045
Mike Stroyan47e741b2017-06-01 13:56:18 -06001046 if (cur_c != NULL) {
1047 // Force return to current context for drivers that cannot handle errors
1048 EGLBoolean restore_result = EGL_FALSE;
1049 // get a reference to the old current objects
1050 ContextRef _c2(dp.get(), cur_c);
1051 SurfaceRef _d2(dp.get(), cur_c->draw);
1052 SurfaceRef _r2(dp.get(), cur_c->read);
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001053
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001054 c = cur_c;
1055 impl_ctx = c->context;
1056 impl_draw = EGL_NO_SURFACE;
1057 if (cur_c->draw != EGL_NO_SURFACE) {
1058 d = get_surface(cur_c->draw);
1059 impl_draw = d->surface;
1060 }
1061 impl_read = EGL_NO_SURFACE;
1062 if (cur_c->read != EGL_NO_SURFACE) {
1063 r = get_surface(cur_c->read);
1064 impl_read = r->surface;
1065 }
1066 restore_result = dp->makeCurrent(c, cur_c,
1067 cur_c->draw, cur_c->read, cur_c->context,
1068 impl_draw, impl_read, impl_ctx);
Mike Stroyan47e741b2017-06-01 13:56:18 -06001069 if (restore_result == EGL_TRUE) {
1070 _c2.acquire();
1071 _r2.acquire();
1072 _d2.acquire();
1073 } else {
1074 ALOGE("Could not restore original EGL context");
1075 }
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001076 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001077 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001078 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001079 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001080 }
1081 return result;
1082}
1083
1084
1085EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1086 EGLint attribute, EGLint *value)
1087{
1088 clearError();
1089
Jesse Hallb29e5e82012-04-04 16:53:42 -07001090 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001091 if (!dp) return EGL_FALSE;
1092
Jesse Hallb29e5e82012-04-04 16:53:42 -07001093 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001094 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001095
Mathias Agopian518ec112011-05-13 16:21:08 -07001096 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001097 return c->cnx->egl.eglQueryContext(
1098 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001099
Mathias Agopian518ec112011-05-13 16:21:08 -07001100}
1101
1102EGLContext eglGetCurrentContext(void)
1103{
1104 // could be called before eglInitialize(), but we wouldn't have a context
1105 // then, and this function would correctly return EGL_NO_CONTEXT.
1106
1107 clearError();
1108
1109 EGLContext ctx = getContext();
1110 return ctx;
1111}
1112
1113EGLSurface eglGetCurrentSurface(EGLint readdraw)
1114{
1115 // could be called before eglInitialize(), but we wouldn't have a context
1116 // then, and this function would correctly return EGL_NO_SURFACE.
1117
1118 clearError();
1119
1120 EGLContext ctx = getContext();
1121 if (ctx) {
1122 egl_context_t const * const c = get_context(ctx);
1123 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1124 switch (readdraw) {
1125 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001126 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001127 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1128 }
1129 }
1130 return EGL_NO_SURFACE;
1131}
1132
1133EGLDisplay eglGetCurrentDisplay(void)
1134{
1135 // could be called before eglInitialize(), but we wouldn't have a context
1136 // then, and this function would correctly return EGL_NO_DISPLAY.
1137
1138 clearError();
1139
1140 EGLContext ctx = getContext();
1141 if (ctx) {
1142 egl_context_t const * const c = get_context(ctx);
1143 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1144 return c->dpy;
1145 }
1146 return EGL_NO_DISPLAY;
1147}
1148
1149EGLBoolean eglWaitGL(void)
1150{
Mathias Agopian518ec112011-05-13 16:21:08 -07001151 clearError();
1152
Mathias Agopianada798b2012-02-13 17:09:30 -08001153 egl_connection_t* const cnx = &gEGLImpl;
1154 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001155 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001156
1157 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001158}
1159
1160EGLBoolean eglWaitNative(EGLint engine)
1161{
Mathias Agopian518ec112011-05-13 16:21:08 -07001162 clearError();
1163
Mathias Agopianada798b2012-02-13 17:09:30 -08001164 egl_connection_t* const cnx = &gEGLImpl;
1165 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001166 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001167
1168 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001169}
1170
1171EGLint eglGetError(void)
1172{
Mathias Agopianada798b2012-02-13 17:09:30 -08001173 EGLint err = EGL_SUCCESS;
1174 egl_connection_t* const cnx = &gEGLImpl;
1175 if (cnx->dso) {
1176 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001177 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001178 if (err == EGL_SUCCESS) {
1179 err = egl_tls_t::getError();
1180 }
1181 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001182}
1183
Michael Chockc0ec5e22014-01-27 08:14:33 -08001184static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001185 const char* procname) {
1186 const egl_connection_t* cnx = &gEGLImpl;
1187 void* proc = NULL;
1188
Michael Chockc0ec5e22014-01-27 08:14:33 -08001189 proc = dlsym(cnx->libEgl, procname);
1190 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1191
Jesse Hallc07b5202013-07-04 12:08:16 -07001192 proc = dlsym(cnx->libGles2, procname);
1193 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1194
1195 proc = dlsym(cnx->libGles1, procname);
1196 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1197
1198 return NULL;
1199}
1200
Mathias Agopian518ec112011-05-13 16:21:08 -07001201__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1202{
1203 // eglGetProcAddress() could be the very first function called
1204 // in which case we must make sure we've initialized ourselves, this
1205 // happens the first time egl_get_display() is called.
1206
1207 clearError();
1208
1209 if (egl_init_drivers() == EGL_FALSE) {
1210 setError(EGL_BAD_PARAMETER, NULL);
1211 return NULL;
1212 }
1213
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001214 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001215 return NULL;
1216 }
1217
Mathias Agopian518ec112011-05-13 16:21:08 -07001218 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001219 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001220 if (addr) return addr;
1221
Michael Chockc0ec5e22014-01-27 08:14:33 -08001222 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001223 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001224
Mathias Agopian518ec112011-05-13 16:21:08 -07001225 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1226 pthread_mutex_lock(&sExtensionMapMutex);
1227
1228 /*
1229 * Since eglGetProcAddress() is not associated to anything, it needs
1230 * to return a function pointer that "works" regardless of what
1231 * the current context is.
1232 *
1233 * For this reason, we return a "forwarder", a small stub that takes
1234 * care of calling the function associated with the context
1235 * currently bound.
1236 *
1237 * We first look for extensions we've already resolved, if we're seeing
1238 * this extension for the first time, we go through all our
1239 * implementations and call eglGetProcAddress() and record the
1240 * result in the appropriate implementation hooks and return the
1241 * address of the forwarder corresponding to that hook set.
1242 *
1243 */
1244
Mathias Agopian65421432017-03-08 11:49:05 -08001245 const std::string name(procname);
1246
1247 auto& extentionMap = sGLExtentionMap;
1248 auto pos = extentionMap.find(name);
1249 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001250 const int slot = sGLExtentionSlot;
1251
Steve Blocke6f43dd2012-01-06 19:20:56 +00001252 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001253 "no more slots for eglGetProcAddress(\"%s\")",
1254 procname);
1255
1256 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1257 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001258
1259 egl_connection_t* const cnx = &gEGLImpl;
1260 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001261 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001262 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001263 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1264 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001265 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001266 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001267 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001268
Mathias Agopian518ec112011-05-13 16:21:08 -07001269 if (found) {
1270 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001271 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 sGLExtentionSlot++;
1273 }
1274 }
1275
1276 pthread_mutex_unlock(&sExtensionMapMutex);
1277 return addr;
1278}
1279
Mathias Agopian65421432017-03-08 11:49:05 -08001280class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001281public:
1282
1283 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001284 static FrameCompletionThread thread;
1285
1286 char name[64];
1287
1288 std::lock_guard<std::mutex> lock(thread.mMutex);
1289 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1290 ATRACE_NAME(name);
1291
1292 thread.mQueue.push_back(sync);
1293 thread.mCondition.notify_one();
1294 thread.mFramesQueued++;
1295 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001296 }
1297
1298private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001299
Mathias Agopian65421432017-03-08 11:49:05 -08001300 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1301 std::thread thread(&FrameCompletionThread::loop, this);
1302 thread.detach();
1303 }
1304
1305#pragma clang diagnostic push
1306#pragma clang diagnostic ignored "-Wmissing-noreturn"
1307 void loop() {
1308 while (true) {
1309 threadLoop();
1310 }
1311 }
1312#pragma clang diagnostic pop
1313
1314 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001315 EGLSyncKHR sync;
1316 uint32_t frameNum;
1317 {
Mathias Agopian65421432017-03-08 11:49:05 -08001318 std::unique_lock<std::mutex> lock(mMutex);
1319 while (mQueue.empty()) {
1320 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001321 }
1322 sync = mQueue[0];
1323 frameNum = mFramesCompleted;
1324 }
1325 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1326 {
Mathias Agopian65421432017-03-08 11:49:05 -08001327 char name[64];
1328 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1329 ATRACE_NAME(name);
1330
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001331 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1332 if (result == EGL_FALSE) {
1333 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1334 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1335 ALOGE("FrameCompletion: timeout waiting for fence");
1336 }
1337 eglDestroySyncKHR(dpy, sync);
1338 }
1339 {
Mathias Agopian65421432017-03-08 11:49:05 -08001340 std::lock_guard<std::mutex> lock(mMutex);
1341 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001342 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001343 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001344 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001345 }
1346
1347 uint32_t mFramesQueued;
1348 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001349 std::deque<EGLSyncKHR> mQueue;
1350 std::condition_variable mCondition;
1351 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001352};
1353
Dan Stozaa894d082015-02-19 15:27:36 -08001354EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1355 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001356{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001357 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001358 clearError();
1359
Jesse Hallb29e5e82012-04-04 16:53:42 -07001360 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001361 if (!dp) return EGL_FALSE;
1362
Jesse Hallb29e5e82012-04-04 16:53:42 -07001363 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001364 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001365 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001366
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001367 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001368
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001369 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1370 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1371 if (sync != EGL_NO_SYNC_KHR) {
1372 FrameCompletionThread::queueSync(sync);
1373 }
1374 }
1375
Mathias Agopian7db993a2012-03-25 00:49:46 -07001376 if (CC_UNLIKELY(dp->finishOnSwap)) {
1377 uint32_t pixel;
1378 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1379 if (c) {
1380 // glReadPixels() ensures that the frame is complete
1381 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1382 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1383 }
1384 }
1385
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001386 if (!sendSurfaceMetadata(s)) {
1387 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1388 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1389 }
1390
Dan Stozaa894d082015-02-19 15:27:36 -08001391 if (n_rects == 0) {
1392 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1393 }
1394
Mathias Agopian65421432017-03-08 11:49:05 -08001395 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001396 for (int r = 0; r < n_rects; ++r) {
1397 int offset = r * 4;
1398 int x = rects[offset];
1399 int y = rects[offset + 1];
1400 int width = rects[offset + 2];
1401 int height = rects[offset + 3];
1402 android_native_rect_t androidRect;
1403 androidRect.left = x;
1404 androidRect.top = y + height;
1405 androidRect.right = x + width;
1406 androidRect.bottom = y;
1407 androidRects.push_back(androidRect);
1408 }
Mathias Agopian65421432017-03-08 11:49:05 -08001409 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001410
1411 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1412 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1413 rects, n_rects);
1414 } else {
1415 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1416 }
1417}
1418
1419EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1420{
1421 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001422}
1423
1424EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1425 NativePixmapType target)
1426{
1427 clearError();
1428
Jesse Hallb29e5e82012-04-04 16:53:42 -07001429 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 if (!dp) return EGL_FALSE;
1431
Jesse Hallb29e5e82012-04-04 16:53:42 -07001432 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001433 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001434 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001435
Mathias Agopian518ec112011-05-13 16:21:08 -07001436 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001437 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001438}
1439
1440const char* eglQueryString(EGLDisplay dpy, EGLint name)
1441{
1442 clearError();
1443
Chia-I Wue57d1352016-08-15 16:10:02 +08001444 // Generate an error quietly when client extensions (as defined by
1445 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1446 // validate_display to generate the error as validate_display would log
1447 // the error, which can be misleading.
1448 //
1449 // If we want to support EGL_EXT_client_extensions later, we can return
1450 // the client extension string here instead.
1451 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001452 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001453
Jesse Hallb29e5e82012-04-04 16:53:42 -07001454 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001455 if (!dp) return (const char *) NULL;
1456
1457 switch (name) {
1458 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001459 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001460 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001461 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001462 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001463 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001464 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001465 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001466 default:
1467 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001468 }
1469 return setError(EGL_BAD_PARAMETER, (const char *)0);
1470}
1471
Jiyong Park00b15b82017-08-10 20:30:56 +09001472extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001473{
1474 clearError();
1475
1476 const egl_display_ptr dp = validate_display(dpy);
1477 if (!dp) return (const char *) NULL;
1478
1479 switch (name) {
1480 case EGL_VENDOR:
1481 return dp->disp.queryString.vendor;
1482 case EGL_VERSION:
1483 return dp->disp.queryString.version;
1484 case EGL_EXTENSIONS:
1485 return dp->disp.queryString.extensions;
1486 case EGL_CLIENT_APIS:
1487 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001488 default:
1489 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001490 }
1491 return setError(EGL_BAD_PARAMETER, (const char *)0);
1492}
Mathias Agopian518ec112011-05-13 16:21:08 -07001493
1494// ----------------------------------------------------------------------------
1495// EGL 1.1
1496// ----------------------------------------------------------------------------
1497
1498EGLBoolean eglSurfaceAttrib(
1499 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1500{
1501 clearError();
1502
Jesse Hallb29e5e82012-04-04 16:53:42 -07001503 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001504 if (!dp) return EGL_FALSE;
1505
Jesse Hallb29e5e82012-04-04 16:53:42 -07001506 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001507 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001508 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001509
Pablo Ceballosc18be292016-05-31 14:55:42 -07001510 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001511
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001512 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001513 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001514 setError(EGL_BAD_SURFACE, EGL_FALSE);
1515 }
Mathias Agopian65421432017-03-08 11:49:05 -08001516 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1517 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001518 }
1519
Pablo Ceballosc18be292016-05-31 14:55:42 -07001520 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001521 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001522 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001523 }
Mathias Agopian65421432017-03-08 11:49:05 -08001524 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1525 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001526 }
1527
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001528 if (s->setSmpte2086Attribute(attribute, value)) {
1529 return EGL_TRUE;
1530 } else if (s->setCta8613Attribute(attribute, value)) {
1531 return EGL_TRUE;
1532 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001533 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001534 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001535 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001536 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001537}
1538
1539EGLBoolean eglBindTexImage(
1540 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1541{
1542 clearError();
1543
Jesse Hallb29e5e82012-04-04 16:53:42 -07001544 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001545 if (!dp) return EGL_FALSE;
1546
Jesse Hallb29e5e82012-04-04 16:53:42 -07001547 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001548 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001549 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001550
Mathias Agopian518ec112011-05-13 16:21:08 -07001551 egl_surface_t const * const s = get_surface(surface);
1552 if (s->cnx->egl.eglBindTexImage) {
1553 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001554 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001555 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001556 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001557}
1558
1559EGLBoolean eglReleaseTexImage(
1560 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1561{
1562 clearError();
1563
Jesse Hallb29e5e82012-04-04 16:53:42 -07001564 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001565 if (!dp) return EGL_FALSE;
1566
Jesse Hallb29e5e82012-04-04 16:53:42 -07001567 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001568 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001569 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001570
Mathias Agopian518ec112011-05-13 16:21:08 -07001571 egl_surface_t const * const s = get_surface(surface);
1572 if (s->cnx->egl.eglReleaseTexImage) {
1573 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001574 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001575 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001576 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001577}
1578
1579EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1580{
1581 clearError();
1582
Jesse Hallb29e5e82012-04-04 16:53:42 -07001583 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001584 if (!dp) return EGL_FALSE;
1585
1586 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001587 egl_connection_t* const cnx = &gEGLImpl;
1588 if (cnx->dso && cnx->egl.eglSwapInterval) {
1589 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001590 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001591
Mathias Agopian518ec112011-05-13 16:21:08 -07001592 return res;
1593}
1594
1595
1596// ----------------------------------------------------------------------------
1597// EGL 1.2
1598// ----------------------------------------------------------------------------
1599
1600EGLBoolean eglWaitClient(void)
1601{
1602 clearError();
1603
Mathias Agopianada798b2012-02-13 17:09:30 -08001604 egl_connection_t* const cnx = &gEGLImpl;
1605 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001606 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001607
1608 EGLBoolean res;
1609 if (cnx->egl.eglWaitClient) {
1610 res = cnx->egl.eglWaitClient();
1611 } else {
1612 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001613 }
1614 return res;
1615}
1616
1617EGLBoolean eglBindAPI(EGLenum api)
1618{
1619 clearError();
1620
1621 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001622 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001623 }
1624
1625 // bind this API on all EGLs
1626 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001627 egl_connection_t* const cnx = &gEGLImpl;
1628 if (cnx->dso && cnx->egl.eglBindAPI) {
1629 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001630 }
1631 return res;
1632}
1633
1634EGLenum eglQueryAPI(void)
1635{
1636 clearError();
1637
1638 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001639 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001640 }
1641
Mathias Agopianada798b2012-02-13 17:09:30 -08001642 egl_connection_t* const cnx = &gEGLImpl;
1643 if (cnx->dso && cnx->egl.eglQueryAPI) {
1644 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001645 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001646
Mathias Agopian518ec112011-05-13 16:21:08 -07001647 // or, it can only be OpenGL ES
1648 return EGL_OPENGL_ES_API;
1649}
1650
1651EGLBoolean eglReleaseThread(void)
1652{
1653 clearError();
1654
Mathias Agopianada798b2012-02-13 17:09:30 -08001655 egl_connection_t* const cnx = &gEGLImpl;
1656 if (cnx->dso && cnx->egl.eglReleaseThread) {
1657 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001658 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301659
1660 // If there is context bound to the thread, release it
1661 egl_display_t::loseCurrent(get_context(getContext()));
1662
Mathias Agopian518ec112011-05-13 16:21:08 -07001663 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001664 return EGL_TRUE;
1665}
1666
1667EGLSurface eglCreatePbufferFromClientBuffer(
1668 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1669 EGLConfig config, const EGLint *attrib_list)
1670{
1671 clearError();
1672
Jesse Hallb29e5e82012-04-04 16:53:42 -07001673 egl_connection_t* cnx = NULL;
1674 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1675 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001676 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1677 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001678 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001679 }
1680 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1681}
1682
1683// ----------------------------------------------------------------------------
1684// EGL_EGLEXT_VERSION 3
1685// ----------------------------------------------------------------------------
1686
1687EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1688 const EGLint *attrib_list)
1689{
1690 clearError();
1691
Jesse Hallb29e5e82012-04-04 16:53:42 -07001692 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001693 if (!dp) return EGL_FALSE;
1694
Jesse Hallb29e5e82012-04-04 16:53:42 -07001695 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001696 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001697 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001698
1699 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001700 if (s->cnx->egl.eglLockSurfaceKHR) {
1701 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001702 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001703 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001704 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001705}
1706
1707EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1708{
1709 clearError();
1710
Jesse Hallb29e5e82012-04-04 16:53:42 -07001711 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001712 if (!dp) return EGL_FALSE;
1713
Jesse Hallb29e5e82012-04-04 16:53:42 -07001714 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001715 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001716 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001717
1718 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001719 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001720 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001721 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001722 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001723}
1724
1725EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1726 EGLClientBuffer buffer, const EGLint *attrib_list)
1727{
1728 clearError();
1729
Jesse Hallb29e5e82012-04-04 16:53:42 -07001730 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001731 if (!dp) return EGL_NO_IMAGE_KHR;
1732
Jesse Hallb29e5e82012-04-04 16:53:42 -07001733 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001734 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001735
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001736 // Temporary hack: eglImageCreateKHR should accept EGL_GL_COLORSPACE_LINEAR_KHR,
1737 // EGL_GL_COLORSPACE_SRGB_KHR and EGL_GL_COLORSPACE_DEFAULT_EXT if
1738 // EGL_EXT_image_gl_colorspace is supported, but some drivers don't like
1739 // the DEFAULT value and generate an error.
1740 std::vector<EGLint> strippedAttribList;
1741 for (const EGLint *attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
1742 if (attr[0] == EGL_GL_COLORSPACE_KHR &&
1743 dp->haveExtension("EGL_EXT_image_gl_colorspace")) {
1744 if (attr[1] != EGL_GL_COLORSPACE_LINEAR_KHR &&
1745 attr[1] != EGL_GL_COLORSPACE_SRGB_KHR) {
1746 continue;
1747 }
1748 }
1749 strippedAttribList.push_back(attr[0]);
1750 strippedAttribList.push_back(attr[1]);
1751 }
1752 strippedAttribList.push_back(EGL_NONE);
1753
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001754 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1755 egl_connection_t* const cnx = &gEGLImpl;
1756 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1757 result = cnx->egl.eglCreateImageKHR(
1758 dp->disp.dpy,
1759 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001760 target, buffer, strippedAttribList.data());
Mathias Agopian518ec112011-05-13 16:21:08 -07001761 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001762 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001763}
1764
1765EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1766{
1767 clearError();
1768
Jesse Hallb29e5e82012-04-04 16:53:42 -07001769 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001770 if (!dp) return EGL_FALSE;
1771
Steven Holte646a5c52012-06-04 20:02:11 -07001772 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001773 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001774 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001775 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001776 }
Steven Holte646a5c52012-06-04 20:02:11 -07001777 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001778}
1779
1780// ----------------------------------------------------------------------------
1781// EGL_EGLEXT_VERSION 5
1782// ----------------------------------------------------------------------------
1783
1784
1785EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1786{
1787 clearError();
1788
Jesse Hallb29e5e82012-04-04 16:53:42 -07001789 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001790 if (!dp) return EGL_NO_SYNC_KHR;
1791
Mathias Agopian518ec112011-05-13 16:21:08 -07001792 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001793 egl_connection_t* const cnx = &gEGLImpl;
1794 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1795 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001796 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001797 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001798}
1799
1800EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1801{
1802 clearError();
1803
Jesse Hallb29e5e82012-04-04 16:53:42 -07001804 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001805 if (!dp) return EGL_FALSE;
1806
Mathias Agopian518ec112011-05-13 16:21:08 -07001807 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001808 egl_connection_t* const cnx = &gEGLImpl;
1809 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1810 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001811 }
1812 return result;
1813}
1814
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001815EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1816 clearError();
1817
1818 const egl_display_ptr dp = validate_display(dpy);
1819 if (!dp) return EGL_FALSE;
1820
1821 EGLBoolean result = EGL_FALSE;
1822 egl_connection_t* const cnx = &gEGLImpl;
1823 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1824 result = cnx->egl.eglSignalSyncKHR(
1825 dp->disp.dpy, sync, mode);
1826 }
1827 return result;
1828}
1829
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001830EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1831 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001832{
1833 clearError();
1834
Jesse Hallb29e5e82012-04-04 16:53:42 -07001835 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001836 if (!dp) return EGL_FALSE;
1837
Mathias Agopian737b8962017-02-24 14:32:05 -08001838 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001839 egl_connection_t* const cnx = &gEGLImpl;
1840 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1841 result = cnx->egl.eglClientWaitSyncKHR(
1842 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001843 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001844 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001845}
1846
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001847EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1848 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001849{
1850 clearError();
1851
Jesse Hallb29e5e82012-04-04 16:53:42 -07001852 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001853 if (!dp) return EGL_FALSE;
1854
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001855 EGLBoolean result = EGL_FALSE;
1856 egl_connection_t* const cnx = &gEGLImpl;
1857 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1858 result = cnx->egl.eglGetSyncAttribKHR(
1859 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001860 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001861 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001862}
1863
Season Li000d88f2015-07-01 11:39:40 -07001864EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1865{
1866 clearError();
1867
1868 const egl_display_ptr dp = validate_display(dpy);
1869 if (!dp) return EGL_NO_STREAM_KHR;
1870
1871 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1872 egl_connection_t* const cnx = &gEGLImpl;
1873 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1874 result = cnx->egl.eglCreateStreamKHR(
1875 dp->disp.dpy, attrib_list);
1876 }
1877 return result;
1878}
1879
1880EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1881{
1882 clearError();
1883
1884 const egl_display_ptr dp = validate_display(dpy);
1885 if (!dp) return EGL_FALSE;
1886
1887 EGLBoolean result = EGL_FALSE;
1888 egl_connection_t* const cnx = &gEGLImpl;
1889 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1890 result = cnx->egl.eglDestroyStreamKHR(
1891 dp->disp.dpy, stream);
1892 }
1893 return result;
1894}
1895
1896EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1897 EGLenum attribute, EGLint value)
1898{
1899 clearError();
1900
1901 const egl_display_ptr dp = validate_display(dpy);
1902 if (!dp) return EGL_FALSE;
1903
1904 EGLBoolean result = EGL_FALSE;
1905 egl_connection_t* const cnx = &gEGLImpl;
1906 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1907 result = cnx->egl.eglStreamAttribKHR(
1908 dp->disp.dpy, stream, attribute, value);
1909 }
1910 return result;
1911}
1912
1913EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1914 EGLenum attribute, EGLint *value)
1915{
1916 clearError();
1917
1918 const egl_display_ptr dp = validate_display(dpy);
1919 if (!dp) return EGL_FALSE;
1920
1921 EGLBoolean result = EGL_FALSE;
1922 egl_connection_t* const cnx = &gEGLImpl;
1923 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1924 result = cnx->egl.eglQueryStreamKHR(
1925 dp->disp.dpy, stream, attribute, value);
1926 }
1927 return result;
1928}
1929
1930EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1931 EGLenum attribute, EGLuint64KHR *value)
1932{
1933 clearError();
1934
1935 const egl_display_ptr dp = validate_display(dpy);
1936 if (!dp) return EGL_FALSE;
1937
1938 EGLBoolean result = EGL_FALSE;
1939 egl_connection_t* const cnx = &gEGLImpl;
1940 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1941 result = cnx->egl.eglQueryStreamu64KHR(
1942 dp->disp.dpy, stream, attribute, value);
1943 }
1944 return result;
1945}
1946
1947EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1948 EGLenum attribute, EGLTimeKHR *value)
1949{
1950 clearError();
1951
1952 const egl_display_ptr dp = validate_display(dpy);
1953 if (!dp) return EGL_FALSE;
1954
1955 EGLBoolean result = EGL_FALSE;
1956 egl_connection_t* const cnx = &gEGLImpl;
1957 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1958 result = cnx->egl.eglQueryStreamTimeKHR(
1959 dp->disp.dpy, stream, attribute, value);
1960 }
1961 return result;
1962}
1963
1964EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1965 EGLStreamKHR stream, const EGLint *attrib_list)
1966{
1967 clearError();
1968
1969 egl_display_ptr dp = validate_display(dpy);
1970 if (!dp) return EGL_NO_SURFACE;
1971
1972 egl_connection_t* const cnx = &gEGLImpl;
1973 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1974 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1975 dp->disp.dpy, config, stream, attrib_list);
1976 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001977 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
1978 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001979 return s;
1980 }
1981 }
1982 return EGL_NO_SURFACE;
1983}
1984
1985EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1986 EGLStreamKHR stream)
1987{
1988 clearError();
1989
1990 const egl_display_ptr dp = validate_display(dpy);
1991 if (!dp) return EGL_FALSE;
1992
1993 EGLBoolean result = EGL_FALSE;
1994 egl_connection_t* const cnx = &gEGLImpl;
1995 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1996 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1997 dp->disp.dpy, stream);
1998 }
1999 return result;
2000}
2001
2002EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
2003 EGLStreamKHR stream)
2004{
2005 clearError();
2006
2007 const egl_display_ptr dp = validate_display(dpy);
2008 if (!dp) return EGL_FALSE;
2009
2010 EGLBoolean result = EGL_FALSE;
2011 egl_connection_t* const cnx = &gEGLImpl;
2012 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
2013 result = cnx->egl.eglStreamConsumerAcquireKHR(
2014 dp->disp.dpy, stream);
2015 }
2016 return result;
2017}
2018
2019EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2020 EGLStreamKHR stream)
2021{
2022 clearError();
2023
2024 const egl_display_ptr dp = validate_display(dpy);
2025 if (!dp) return EGL_FALSE;
2026
2027 EGLBoolean result = EGL_FALSE;
2028 egl_connection_t* const cnx = &gEGLImpl;
2029 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2030 result = cnx->egl.eglStreamConsumerReleaseKHR(
2031 dp->disp.dpy, stream);
2032 }
2033 return result;
2034}
2035
2036EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2037 EGLDisplay dpy, EGLStreamKHR stream)
2038{
2039 clearError();
2040
2041 const egl_display_ptr dp = validate_display(dpy);
2042 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2043
2044 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2045 egl_connection_t* const cnx = &gEGLImpl;
2046 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2047 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2048 dp->disp.dpy, stream);
2049 }
2050 return result;
2051}
2052
2053EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2054 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2055{
2056 clearError();
2057
2058 const egl_display_ptr dp = validate_display(dpy);
2059 if (!dp) return EGL_NO_STREAM_KHR;
2060
2061 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2062 egl_connection_t* const cnx = &gEGLImpl;
2063 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2064 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2065 dp->disp.dpy, file_descriptor);
2066 }
2067 return result;
2068}
2069
Mathias Agopian518ec112011-05-13 16:21:08 -07002070// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002071// EGL_EGLEXT_VERSION 15
2072// ----------------------------------------------------------------------------
2073
2074EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2075 clearError();
2076 const egl_display_ptr dp = validate_display(dpy);
2077 if (!dp) return EGL_FALSE;
2078 EGLint result = EGL_FALSE;
2079 egl_connection_t* const cnx = &gEGLImpl;
2080 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2081 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2082 }
2083 return result;
2084}
2085
2086// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002087// ANDROID extensions
2088// ----------------------------------------------------------------------------
2089
Jamie Gennis331841b2012-09-06 14:52:00 -07002090EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2091{
2092 clearError();
2093
2094 const egl_display_ptr dp = validate_display(dpy);
2095 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2096
2097 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2098 egl_connection_t* const cnx = &gEGLImpl;
2099 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2100 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2101 }
2102 return result;
2103}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002104
Andy McFadden72841452013-03-01 16:25:32 -08002105EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2106 EGLnsecsANDROID time)
2107{
2108 clearError();
2109
2110 const egl_display_ptr dp = validate_display(dpy);
2111 if (!dp) {
2112 return EGL_FALSE;
2113 }
2114
2115 SurfaceRef _s(dp.get(), surface);
2116 if (!_s.get()) {
2117 setError(EGL_BAD_SURFACE, EGL_FALSE);
2118 return EGL_FALSE;
2119 }
2120
2121 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002122 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002123
2124 return EGL_TRUE;
2125}
2126
Craig Donner60761072017-01-27 12:30:44 -08002127EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2128 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002129 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2130 // this function cannot be implemented when this libEGL is built for
2131 // vendors.
2132#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002133 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002134 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002135#else
2136 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2137#endif
Craig Donner60761072017-01-27 12:30:44 -08002138}
2139
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002140// ----------------------------------------------------------------------------
2141// NVIDIA extensions
2142// ----------------------------------------------------------------------------
2143EGLuint64NV eglGetSystemTimeFrequencyNV()
2144{
2145 clearError();
2146
2147 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002148 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002149 }
2150
2151 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002152 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002153
Mathias Agopianada798b2012-02-13 17:09:30 -08002154 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2155 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002156 }
2157
Mathias Agopian737b8962017-02-24 14:32:05 -08002158 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002159}
2160
2161EGLuint64NV eglGetSystemTimeNV()
2162{
2163 clearError();
2164
2165 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002166 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002167 }
2168
2169 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002170 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002171
Mathias Agopianada798b2012-02-13 17:09:30 -08002172 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2173 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002174 }
2175
Mathias Agopian737b8962017-02-24 14:32:05 -08002176 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002177}
Dan Stozaa894d082015-02-19 15:27:36 -08002178
2179// ----------------------------------------------------------------------------
2180// Partial update extension
2181// ----------------------------------------------------------------------------
2182EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2183 EGLint *rects, EGLint n_rects)
2184{
2185 clearError();
2186
2187 const egl_display_ptr dp = validate_display(dpy);
2188 if (!dp) {
2189 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2190 return EGL_FALSE;
2191 }
2192
2193 SurfaceRef _s(dp.get(), surface);
2194 if (!_s.get()) {
2195 setError(EGL_BAD_SURFACE, EGL_FALSE);
2196 return EGL_FALSE;
2197 }
2198
2199 egl_surface_t const * const s = get_surface(surface);
2200 if (s->cnx->egl.eglSetDamageRegionKHR) {
2201 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2202 rects, n_rects);
2203 }
2204
2205 return EGL_FALSE;
2206}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002207
Brian Anderson1049d1d2016-12-16 17:25:57 -08002208EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2209 EGLuint64KHR *frameId) {
2210 clearError();
2211
2212 const egl_display_ptr dp = validate_display(dpy);
2213 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002214 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002215 }
2216
2217 SurfaceRef _s(dp.get(), surface);
2218 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002219 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002220 }
2221
2222 egl_surface_t const * const s = get_surface(surface);
2223
Mathias Agopian65421432017-03-08 11:49:05 -08002224 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002225 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002226 }
2227
2228 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002229 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002230
Mathias Agopian65421432017-03-08 11:49:05 -08002231 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002232 // This should not happen. Return an error that is not in the spec
2233 // so it's obvious something is very wrong.
2234 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002235 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002236 }
2237
2238 *frameId = nextFrameId;
2239 return EGL_TRUE;
2240}
2241
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002242EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2243 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2244{
2245 clearError();
2246
2247 const egl_display_ptr dp = validate_display(dpy);
2248 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002249 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002250 }
2251
2252 SurfaceRef _s(dp.get(), surface);
2253 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002254 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002255 }
2256
2257 egl_surface_t const * const s = get_surface(surface);
2258
Mathias Agopian65421432017-03-08 11:49:05 -08002259 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002260 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002261 }
2262
2263 nsecs_t* compositeDeadline = nullptr;
2264 nsecs_t* compositeInterval = nullptr;
2265 nsecs_t* compositeToPresentLatency = nullptr;
2266
2267 for (int i = 0; i < numTimestamps; i++) {
2268 switch (names[i]) {
2269 case EGL_COMPOSITE_DEADLINE_ANDROID:
2270 compositeDeadline = &values[i];
2271 break;
2272 case EGL_COMPOSITE_INTERVAL_ANDROID:
2273 compositeInterval = &values[i];
2274 break;
2275 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2276 compositeToPresentLatency = &values[i];
2277 break;
2278 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002279 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002280 }
2281 }
2282
Mathias Agopian65421432017-03-08 11:49:05 -08002283 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002284 compositeDeadline, compositeInterval, compositeToPresentLatency);
2285
2286 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002287 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002288 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002289 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002290 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002291 default:
2292 // This should not happen. Return an error that is not in the spec
2293 // so it's obvious something is very wrong.
2294 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002295 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002296 }
2297}
2298
2299EGLBoolean eglGetCompositorTimingSupportedANDROID(
2300 EGLDisplay dpy, EGLSurface surface, EGLint name)
2301{
2302 clearError();
2303
2304 const egl_display_ptr dp = validate_display(dpy);
2305 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002306 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002307 }
2308
2309 SurfaceRef _s(dp.get(), surface);
2310 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002311 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002312 }
2313
2314 egl_surface_t const * const s = get_surface(surface);
2315
Mathias Agopian65421432017-03-08 11:49:05 -08002316 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002317 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002318 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002319 }
2320
2321 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002322 case EGL_COMPOSITE_DEADLINE_ANDROID:
2323 case EGL_COMPOSITE_INTERVAL_ANDROID:
2324 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2325 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002326 default:
2327 return EGL_FALSE;
2328 }
2329}
2330
Pablo Ceballosc18be292016-05-31 14:55:42 -07002331EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002332 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002333 EGLnsecsANDROID *values)
2334{
2335 clearError();
2336
2337 const egl_display_ptr dp = validate_display(dpy);
2338 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002339 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002340 }
2341
2342 SurfaceRef _s(dp.get(), surface);
2343 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002344 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002345 }
2346
2347 egl_surface_t const * const s = get_surface(surface);
2348
Mathias Agopian65421432017-03-08 11:49:05 -08002349 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002350 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002351 }
2352
Brian Andersondbd0ea82016-07-22 09:38:59 -07002353 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002354 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002355 nsecs_t* latchTime = nullptr;
2356 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002357 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002358 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002359 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002360 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002361 nsecs_t* releaseTime = nullptr;
2362
2363 for (int i = 0; i < numTimestamps; i++) {
2364 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002365 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2366 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002367 break;
2368 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2369 acquireTime = &values[i];
2370 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002371 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2372 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002373 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002374 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2375 firstRefreshStartTime = &values[i];
2376 break;
2377 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2378 lastRefreshStartTime = &values[i];
2379 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002380 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2381 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002382 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002383 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2384 displayPresentTime = &values[i];
2385 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002386 case EGL_DEQUEUE_READY_TIME_ANDROID:
2387 dequeueReadyTime = &values[i];
2388 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002389 case EGL_READS_DONE_TIME_ANDROID:
2390 releaseTime = &values[i];
2391 break;
2392 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002393 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002394 }
2395 }
2396
Mathias Agopian65421432017-03-08 11:49:05 -08002397 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002398 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002399 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002400 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002401
Brian Anderson069b3652016-07-22 10:32:47 -07002402 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002403 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002404 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002405 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002406 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002407 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002408 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002409 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002410 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2411 default:
2412 // This should not happen. Return an error that is not in the spec
2413 // so it's obvious something is very wrong.
2414 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2415 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002416 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002417}
2418
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002419EGLBoolean eglGetFrameTimestampSupportedANDROID(
2420 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002421{
2422 clearError();
2423
2424 const egl_display_ptr dp = validate_display(dpy);
2425 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002426 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002427 }
2428
2429 SurfaceRef _s(dp.get(), surface);
2430 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002431 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002432 }
2433
2434 egl_surface_t const * const s = get_surface(surface);
2435
Mathias Agopian65421432017-03-08 11:49:05 -08002436 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002437 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002438 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002439 }
2440
2441 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002442 case EGL_COMPOSITE_DEADLINE_ANDROID:
2443 case EGL_COMPOSITE_INTERVAL_ANDROID:
2444 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002445 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002446 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002447 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2448 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2449 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002450 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002451 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002452 case EGL_READS_DONE_TIME_ANDROID:
2453 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002454 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2455 int value = 0;
2456 window->query(window,
2457 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2458 return value == 0 ? EGL_FALSE : EGL_TRUE;
2459 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002460 default:
2461 return EGL_FALSE;
2462 }
2463}