blob: 40adf8e229dec5762de101cbf7d7f7cb0c76d9ef [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
Craig Donner60761072017-01-27 12:30:44 -080029#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080030#include <private/android/AHardwareBufferHelpers.h>
31
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Mathias Agopian65421432017-03-08 11:49:05 -080036#include <condition_variable>
37#include <deque>
38#include <mutex>
39#include <unordered_map>
40#include <string>
41#include <thread>
Mathias Agopian518ec112011-05-13 16:21:08 -070042
Mathias Agopian39c24a22013-04-04 23:17:56 -070043#include "../egl_impl.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070044
45#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070046#include "egl_object.h"
47#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080048#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070049
50using namespace android;
51
52// ----------------------------------------------------------------------------
53
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070054namespace android {
55
Mathias Agopian65421432017-03-08 11:49:05 -080056using nsecs_t = int64_t;
57
Mathias Agopian518ec112011-05-13 16:21:08 -070058struct extention_map_t {
59 const char* name;
60 __eglMustCastToProperFunctionPointerType address;
61};
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063/*
Jesse Hall21558da2013-08-06 15:31:22 -070064 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070065 *
Jesse Hall21558da2013-08-06 15:31:22 -070066 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
67 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070068 *
Jesse Hall21558da2013-08-06 15:31:22 -070069 * The rest (gExtensionString) depend on support in the EGL driver, and are
70 * only available if the driver supports them. However, some of these must be
71 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080072 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070073 * the mandatory extensions are present and may not function properly if some
74 * are missing.
75 *
76 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070077 */
Mathias Agopian737b8962017-02-24 14:32:05 -080078
Mathias Agopian311b4792017-02-28 15:00:49 -080079extern char const * const gBuiltinExtensionString;
80extern char const * const gExtensionString;
Mathias Agopian737b8962017-02-24 14:32:05 -080081
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -060082// clang-format off
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -070083// Extensions implemented by the EGL wrapper.
Mathias Agopian311b4792017-02-28 15:00:49 -080084char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070085 "EGL_KHR_get_all_proc_addresses "
86 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080087 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080088 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080089 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070090 "EGL_ANDROID_get_frame_timestamps "
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070091 "EGL_EXT_surface_SMPTE2086_metadata "
92 "EGL_EXT_surface_CTA861_3_metadata "
Jesse Hall21558da2013-08-06 15:31:22 -070093 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080094
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -070095// Whitelist of extensions exposed to applications if implemented in the vendor driver.
Mathias Agopian311b4792017-02-28 15:00:49 -080096char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_image " // mandatory
98 "EGL_KHR_image_base " // mandatory
Krzysztof Kosiński41171c52018-04-19 21:38:54 -070099 "EGL_EXT_image_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700100 "EGL_KHR_image_pixmap "
101 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700102 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700104 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700105 "EGL_KHR_gl_texture_cubemap_image "
106 "EGL_KHR_gl_renderbuffer_image "
107 "EGL_KHR_reusable_sync "
108 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700109 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700110 "EGL_KHR_config_attribs "
111 "EGL_KHR_surfaceless_context "
112 "EGL_KHR_stream "
113 "EGL_KHR_stream_fifo "
114 "EGL_KHR_stream_producer_eglsurface "
115 "EGL_KHR_stream_consumer_gltexture "
116 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700117 "EGL_EXT_create_context_robustness "
118 "EGL_NV_system_time "
119 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700120 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700121 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800122 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700123 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800124 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700125 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700126 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700127 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700128 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000129 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800130 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700131 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600132// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700133
134// extensions not exposed to applications but used by the ANDROID system
135// "EGL_ANDROID_blob_cache " // strongly recommended
136// "EGL_IMG_hibernate_process " // optional
137// "EGL_ANDROID_native_fence_sync " // strongly recommended
138// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700139// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700140
141/*
142 * EGL Extensions entry-points exposed to 3rd party applications
143 * (keep in sync with gExtensionString above)
144 *
145 */
146static const extention_map_t sExtensionMap[] = {
147 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700148 { "eglLockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
150 { "eglUnlockSurfaceKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700152
153 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 { "eglCreateImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
156 { "eglDestroyImageKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700158
159 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
160 { "eglCreateSyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
162 { "eglDestroySyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
164 { "eglClientWaitSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
166 { "eglSignalSyncKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
168 { "eglGetSyncAttribKHR",
169 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
170
171 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800172 { "eglGetSystemTimeFrequencyNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
174 { "eglGetSystemTimeNV",
175 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700176
Mathias Agopian2bb71682013-03-27 17:32:41 -0700177 // EGL_KHR_wait_sync
178 { "eglWaitSyncKHR",
179 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700180
181 // EGL_ANDROID_presentation_time
182 { "eglPresentationTimeANDROID",
183 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800184
185 // EGL_KHR_swap_buffers_with_damage
186 { "eglSwapBuffersWithDamageKHR",
187 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
188
Craig Donner60761072017-01-27 12:30:44 -0800189 // EGL_ANDROID_get_native_client_buffer
190 { "eglGetNativeClientBufferANDROID",
191 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
192
Dan Stozaa894d082015-02-19 15:27:36 -0800193 // EGL_KHR_partial_update
194 { "eglSetDamageRegionKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700196
197 { "eglCreateStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
199 { "eglDestroyStreamKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
201 { "eglStreamAttribKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
203 { "eglQueryStreamKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
205 { "eglQueryStreamu64KHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
207 { "eglQueryStreamTimeKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
209 { "eglCreateStreamProducerSurfaceKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
211 { "eglStreamConsumerGLTextureExternalKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
213 { "eglStreamConsumerAcquireKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
215 { "eglStreamConsumerReleaseKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
217 { "eglGetStreamFileDescriptorKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
219 { "eglCreateStreamFromFileDescriptorKHR",
220 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700221
222 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800223 { "eglGetNextFrameIdANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800225 { "eglGetCompositorTimingANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
227 { "eglGetCompositorTimingSupportedANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700229 { "eglGetFrameTimestampsANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800231 { "eglGetFrameTimestampSupportedANDROID",
232 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700233
234 // EGL_ANDROID_native_fence_sync
235 { "eglDupNativeFenceFDANDROID",
236 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700237};
238
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700239/*
240 * These extensions entry-points should not be exposed to applications.
241 * They're used internally by the Android EGL layer.
242 */
243#define FILTER_EXTENSIONS(procname) \
244 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
245 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700246 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800249static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
250
Mathias Agopian518ec112011-05-13 16:21:08 -0700251static int sGLExtentionSlot = 0;
252static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
253
254static void(*findProcAddress(const char* name,
255 const extention_map_t* map, size_t n))() {
256 for (uint32_t i=0 ; i<n ; i++) {
257 if (!strcmp(name, map[i].name)) {
258 return map[i].address;
259 }
260 }
261 return NULL;
262}
263
264// ----------------------------------------------------------------------------
265
Mathias Agopian518ec112011-05-13 16:21:08 -0700266extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
267extern EGLBoolean egl_init_drivers();
268extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700269extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271} // namespace android;
272
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700273
Mathias Agopian518ec112011-05-13 16:21:08 -0700274// ----------------------------------------------------------------------------
275
276static inline void clearError() { egl_tls_t::clearError(); }
277static inline EGLContext getContext() { return egl_tls_t::getContext(); }
278
279// ----------------------------------------------------------------------------
280
281EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
282{
Jesse Hall1508ae62017-01-19 17:43:26 -0800283 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 clearError();
285
Dan Stozac3289c42014-01-17 11:38:34 -0800286 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700287 if (index >= NUM_DISPLAYS) {
288 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
289 }
290
291 if (egl_init_drivers() == EGL_FALSE) {
292 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
293 }
294
295 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
296 return dpy;
297}
298
299// ----------------------------------------------------------------------------
300// Initialization
301// ----------------------------------------------------------------------------
302
303EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
304{
305 clearError();
306
Jesse Hallb29e5e82012-04-04 16:53:42 -0700307 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800308 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700309
310 EGLBoolean res = dp->initialize(major, minor);
311
312 return res;
313}
314
315EGLBoolean eglTerminate(EGLDisplay dpy)
316{
317 // NOTE: don't unload the drivers b/c some APIs can be called
318 // after eglTerminate() has been called. eglTerminate() only
319 // terminates an EGLDisplay, not a EGL itself.
320
321 clearError();
322
Jesse Hallb29e5e82012-04-04 16:53:42 -0700323 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800324 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700325
326 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800327
Mathias Agopian518ec112011-05-13 16:21:08 -0700328 return res;
329}
330
331// ----------------------------------------------------------------------------
332// configuration
333// ----------------------------------------------------------------------------
334
335EGLBoolean eglGetConfigs( EGLDisplay dpy,
336 EGLConfig *configs,
337 EGLint config_size, EGLint *num_config)
338{
339 clearError();
340
Jesse Hallb29e5e82012-04-04 16:53:42 -0700341 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700342 if (!dp) return EGL_FALSE;
343
Mathias Agopian7773c432012-02-13 20:06:08 -0800344 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800345 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700346 }
347
Mathias Agopian7773c432012-02-13 20:06:08 -0800348 EGLBoolean res = EGL_FALSE;
349 *num_config = 0;
350
351 egl_connection_t* const cnx = &gEGLImpl;
352 if (cnx->dso) {
353 res = cnx->egl.eglGetConfigs(
354 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700355 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800356
357 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700358}
359
360EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
361 EGLConfig *configs, EGLint config_size,
362 EGLint *num_config)
363{
364 clearError();
365
Jesse Hallb29e5e82012-04-04 16:53:42 -0700366 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700367 if (!dp) return EGL_FALSE;
368
369 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800370 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700371 }
372
Mathias Agopian518ec112011-05-13 16:21:08 -0700373 EGLBoolean res = EGL_FALSE;
374 *num_config = 0;
375
Mathias Agopianada798b2012-02-13 17:09:30 -0800376 egl_connection_t* const cnx = &gEGLImpl;
377 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700378 if (attrib_list) {
379 char value[PROPERTY_VALUE_MAX];
380 property_get("debug.egl.force_msaa", value, "false");
381
382 if (!strcmp(value, "true")) {
383 size_t attribCount = 0;
384 EGLint attrib = attrib_list[0];
385
386 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700387 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700388 const EGLint *attribRendererable = NULL;
389 const EGLint *attribCaveat = NULL;
390
391 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700392 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700393 while (attrib != EGL_NONE) {
394 attrib = attrib_list[attribCount];
395 switch (attrib) {
396 case EGL_RENDERABLE_TYPE:
397 attribRendererable = &attrib_list[attribCount];
398 break;
399 case EGL_CONFIG_CAVEAT:
400 attribCaveat = &attrib_list[attribCount];
401 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800402 default:
403 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700404 }
405 attribCount++;
406 }
407
408 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
409 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800410
Romain Guy1cffc802012-10-15 18:13:05 -0700411 // Insert 2 extra attributes to force-enable MSAA 4x
412 EGLint aaAttribs[attribCount + 4];
413 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
414 aaAttribs[1] = 1;
415 aaAttribs[2] = EGL_SAMPLES;
416 aaAttribs[3] = 4;
417
418 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
419
420 EGLint numConfigAA;
421 EGLBoolean resAA = cnx->egl.eglChooseConfig(
422 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
423
424 if (resAA == EGL_TRUE && numConfigAA > 0) {
425 ALOGD("Enabling MSAA 4x");
426 *num_config = numConfigAA;
427 return resAA;
428 }
429 }
430 }
431 }
432
Mathias Agopian7773c432012-02-13 20:06:08 -0800433 res = cnx->egl.eglChooseConfig(
434 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700435 }
436 return res;
437}
438
439EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
440 EGLint attribute, EGLint *value)
441{
442 clearError();
443
Jesse Hallb29e5e82012-04-04 16:53:42 -0700444 egl_connection_t* cnx = NULL;
445 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
446 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800447
Mathias Agopian518ec112011-05-13 16:21:08 -0700448 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800449 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700450}
451
452// ----------------------------------------------------------------------------
453// surfaces
454// ----------------------------------------------------------------------------
455
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700456// Translates EGL color spaces to Android data spaces.
457static android_dataspace dataSpaceFromEGLColorSpace(EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700458 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Chia-I Wubf433ab2018-06-20 10:57:48 +0800459 return HAL_DATASPACE_UNKNOWN;
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800461 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600462 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
463 return HAL_DATASPACE_DISPLAY_P3;
464 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
465 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600466 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
467 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600468 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
469 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700470 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT) {
471 return HAL_DATASPACE_BT2020_LINEAR;
472 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_PQ_EXT) {
473 return HAL_DATASPACE_BT2020_PQ;
Jesse Hallc2e41222013-08-08 13:40:22 -0700474 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700475 return HAL_DATASPACE_UNKNOWN;
Jesse Hallc2e41222013-08-08 13:40:22 -0700476}
477
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700478// Get the colorspace value that should be reported from queries. When the colorspace
479// is unknown (no attribute passed), default to reporting LINEAR.
480static EGLint getReportedColorSpace(EGLint colorspace) {
481 return colorspace == EGL_UNKNOWN ? EGL_GL_COLORSPACE_LINEAR_KHR : colorspace;
482}
483
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700484// Returns a list of color spaces understood by the vendor EGL driver.
485static std::vector<EGLint> getDriverColorSpaces(egl_display_ptr dp,
486 android_pixel_format format) {
487 std::vector<EGLint> colorSpaces;
488 if (!dp->hasColorSpaceSupport) return colorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700489
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700490 // OpenGL drivers only support sRGB encoding with 8-bit formats.
491 // RGB_888 is never returned by getNativePixelFormat, but is included for completeness.
492 const bool formatSupportsSRGBEncoding =
493 format == HAL_PIXEL_FORMAT_RGBA_8888 || format == HAL_PIXEL_FORMAT_RGBX_8888 ||
494 format == HAL_PIXEL_FORMAT_RGB_888;
495 const bool formatIsFloatingPoint = format == HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700496
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700497 if (formatSupportsSRGBEncoding) {
498 // sRGB and linear are always supported when color space support is present.
499 colorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
500 colorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
501 // DCI-P3 uses the sRGB transfer function, so it's only relevant for 8-bit formats.
502 if (findExtension(dp->disp.queryString.extensions,
503 "EGL_EXT_gl_colorspace_display_p3")) {
504 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600505 }
506 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700507
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700508 // According to the spec, scRGB is only supported for floating point formats.
509 // For non-linear scRGB, the application is responsible for applying the
510 // transfer function.
511 if (formatIsFloatingPoint) {
512 if (findExtension(dp->disp.queryString.extensions,
513 "EGL_EXT_gl_colorspace_scrgb")) {
514 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_EXT);
515 }
516 if (findExtension(dp->disp.queryString.extensions,
517 "EGL_EXT_gl_colorspace_scrgb_linear")) {
518 colorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
519 }
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600520 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700521
522 // BT2020 can be used with any pixel format. PQ encoding must be applied by the
523 // application and does not affect the behavior of OpenGL.
524 if (findExtension(dp->disp.queryString.extensions,
525 "EGL_EXT_gl_colorspace_bt2020_linear")) {
526 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
527 }
528 if (findExtension(dp->disp.queryString.extensions,
529 "EGL_EXT_gl_colorspace_bt2020_pq")) {
530 colorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
531 }
532
533 // Linear DCI-P3 simply uses different primaries than standard RGB and thus
534 // can be used with any pixel format.
535 if (findExtension(dp->disp.queryString.extensions,
536 "EGL_EXT_gl_colorspace_display_p3_linear")) {
537 colorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
538 }
539 return colorSpaces;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600540}
541
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700542// Cleans up color space related parameters that the driver does not understand.
543// If there is no color space attribute in attrib_list, colorSpace is left
544// unmodified.
545static EGLBoolean processAttributes(egl_display_ptr dp, NativeWindowType window,
546 android_pixel_format format, const EGLint* attrib_list,
547 EGLint* colorSpace,
548 std::vector<EGLint>* strippedAttribList) {
549 for (const EGLint* attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
550 bool copyAttribute = true;
551 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
552 // Fail immediately if the driver doesn't have color space support at all.
553 if (!dp->hasColorSpaceSupport) return false;
554 *colorSpace = attr[1];
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600555
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700556 // Strip the attribute if the driver doesn't understand it.
557 copyAttribute = false;
558 std::vector<EGLint> driverColorSpaces = getDriverColorSpaces(dp, format);
559 for (auto driverColorSpace : driverColorSpaces) {
560 if (attr[1] == driverColorSpace) {
561 copyAttribute = true;
562 break;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600563 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600564 }
Chris Forbesff499942018-10-04 09:52:42 -0700565
566 // If the driver doesn't understand it, we should map sRGB-encoded P3 to
567 // sRGB rather than just dropping the colorspace on the floor.
568 // For this format, the driver is expected to apply the sRGB
569 // transfer function during framebuffer operations.
570 if (!copyAttribute && attr[1] == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
571 strippedAttribList->push_back(attr[0]);
572 strippedAttribList->push_back(EGL_GL_COLORSPACE_SRGB_KHR);
573 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600574 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700575 if (copyAttribute) {
576 strippedAttribList->push_back(attr[0]);
577 strippedAttribList->push_back(attr[1]);
578 }
579 }
580 // Terminate the attribute list.
581 strippedAttribList->push_back(EGL_NONE);
582
583 // If the passed color space has wide color gamut, check whether the target native window
584 // supports wide color.
585 const bool colorSpaceIsNarrow =
586 *colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700587 *colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR ||
588 *colorSpace == EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700589 if (window && !colorSpaceIsNarrow) {
590 bool windowSupportsWideColor = true;
591 // Ordinarily we'd put a call to native_window_get_wide_color_support
592 // at the beginning of the function so that we'll have the
593 // result when needed elsewhere in the function.
594 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
595 // SurfaceFlinger is required to answer the call below we would
596 // end up in a deadlock situation. By moving the call to only happen
597 // if the application has specifically asked for wide-color we avoid
598 // the deadlock with SurfaceFlinger since it will not ask for a
599 // wide-color surface.
600 int err = native_window_get_wide_color_support(window, &windowSupportsWideColor);
601
602 if (err) {
603 ALOGE("processAttributes: invalid window (win=%p) "
604 "failed (%#x) (already connected to another API?)",
605 window, err);
606 return false;
607 }
608 if (!windowSupportsWideColor) {
609 // Application has asked for a wide-color colorspace but
610 // wide-color support isn't available on the display the window is on.
611 return false;
612 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600613 }
614 return true;
615}
616
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700617// Gets the native pixel format corrsponding to the passed EGLConfig.
618void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config,
619 android_pixel_format* format) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600620 // Set the native window's buffers format to match what this config requests.
621 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
622 // of our native format. So if sRGB gamma is requested, we have to
623 // modify the EGLconfig's format before setting the native window's
624 // format.
625
626 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
627 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
628
629 EGLint a = 0;
630 EGLint r, g, b;
631 r = g = b = 0;
632 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
633 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
634 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
635 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
636 EGLint colorDepth = r + g + b;
637
638 // Today, the driver only understands sRGB and linear on 888X
639 // formats. Strip other colorspaces from the attribute list and
640 // only use them to set the dataspace via
641 // native_window_set_buffers_dataspace
642 // if pixel format is RGBX 8888
643 // TBD: Can test for future extensions that indicate that driver
644 // handles requested color space and we can let it through.
645 // allow SRGB and LINEAR. All others need to be stripped.
646 // else if 565, 4444
647 // TBD: Can we assume these are supported if 8888 is?
648 // else if FP16 or 1010102
649 // strip colorspace from attribs.
650 // endif
651 if (a == 0) {
652 if (colorDepth <= 16) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700653 *format = HAL_PIXEL_FORMAT_RGB_565;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600654 } else {
655 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
656 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700657 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600658 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700659 *format = HAL_PIXEL_FORMAT_RGBX_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600660 }
661 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700662 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600663 }
664 }
665 } else {
666 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
667 if (colorDepth > 24) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700668 *format = HAL_PIXEL_FORMAT_RGBA_1010102;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600669 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700670 *format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600671 }
672 } else {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700673 *format = HAL_PIXEL_FORMAT_RGBA_FP16;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600674 }
675 }
676}
677
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700678EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
679 android_smpte2086_metadata smpteMetadata;
680 if (s->getSmpte2086Metadata(smpteMetadata)) {
681 int err =
682 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
683 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700684 if (err != 0) {
685 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
686 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700687 return EGL_FALSE;
688 }
689 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700690 android_cta861_3_metadata cta8613Metadata;
691 if (s->getCta8613Metadata(cta8613Metadata)) {
692 int err =
693 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
694 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700695 if (err != 0) {
696 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
697 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700698 return EGL_FALSE;
699 }
700 }
701 return EGL_TRUE;
702}
703
Mathias Agopian518ec112011-05-13 16:21:08 -0700704EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
705 NativeWindowType window,
706 const EGLint *attrib_list)
707{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700708 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700709 clearError();
710
Jesse Hallb29e5e82012-04-04 16:53:42 -0700711 egl_connection_t* cnx = NULL;
712 egl_display_ptr dp = validate_display_connection(dpy, cnx);
713 if (dp) {
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800714 if (!window) {
715 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
716 }
717
718 int value = 0;
719 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
720 if (!value) {
721 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
722 }
723
Andy McFaddend566ce32014-01-07 15:54:17 -0800724 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800725 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800726 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
727 "failed (%#x) (already connected to another API?)",
728 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700729 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700730 }
731
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700732 EGLDisplay iDpy = dp->disp.dpy;
733 android_pixel_format format;
734 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600735
736 // now select correct colorspace and dataspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700737 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700738 std::vector<EGLint> strippedAttribList;
739 if (!processAttributes(dp, window, format, attrib_list, &colorSpace,
740 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600741 ALOGE("error invalid colorspace: %d", colorSpace);
742 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700743 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700744 attrib_list = strippedAttribList.data();
Alistair Strachan733a8072015-02-12 12:33:25 -0800745
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700746 {
Jesse Hallc2e41222013-08-08 13:40:22 -0700747 int err = native_window_set_buffers_format(window, format);
748 if (err != 0) {
749 ALOGE("error setting native window pixel format: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700750 strerror(-err), err);
Jesse Hallc2e41222013-08-08 13:40:22 -0700751 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
752 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
753 }
754 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700755
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700756 android_dataspace dataSpace = dataSpaceFromEGLColorSpace(colorSpace);
757 if (dataSpace != HAL_DATASPACE_UNKNOWN) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800758 int err = native_window_set_buffers_data_space(window, dataSpace);
759 if (err != 0) {
760 ALOGE("error setting native window pixel dataSpace: %s (%d)",
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700761 strerror(-err), err);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800762 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
763 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
764 }
765 }
766
Jamie Gennis59769462011-11-19 18:04:43 -0800767 // the EGL spec requires that a new EGLSurface default to swap interval
768 // 1, so explicitly set that on the window here.
769 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
770 anw->setSwapInterval(anw, 1);
771
Mathias Agopian518ec112011-05-13 16:21:08 -0700772 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800773 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700774 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600775 egl_surface_t* s =
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700776 new egl_surface_t(dp.get(), config, window, surface,
777 getReportedColorSpace(colorSpace), cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700778 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700779 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700780
781 // EGLSurface creation failed
782 native_window_set_buffers_format(window, 0);
783 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700784 }
785 return EGL_NO_SURFACE;
786}
787
788EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
789 NativePixmapType pixmap,
790 const EGLint *attrib_list)
791{
792 clearError();
793
Jesse Hallb29e5e82012-04-04 16:53:42 -0700794 egl_connection_t* cnx = NULL;
795 egl_display_ptr dp = validate_display_connection(dpy, cnx);
796 if (dp) {
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700797 EGLDisplay iDpy = dp->disp.dpy;
798 android_pixel_format format;
799 getNativePixelFormat(iDpy, cnx, config, &format);
800
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600801 // now select a corresponding sRGB format if needed
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700802 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700803 std::vector<EGLint> strippedAttribList;
804 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
805 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600806 ALOGE("error invalid colorspace: %d", colorSpace);
807 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
808 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700809 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600810
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800812 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700813 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700814 egl_surface_t* s =
815 new egl_surface_t(dp.get(), config, NULL, surface,
816 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700817 return s;
818 }
819 }
820 return EGL_NO_SURFACE;
821}
822
823EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
824 const EGLint *attrib_list)
825{
826 clearError();
827
Jesse Hallb29e5e82012-04-04 16:53:42 -0700828 egl_connection_t* cnx = NULL;
829 egl_display_ptr dp = validate_display_connection(dpy, cnx);
830 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600831 EGLDisplay iDpy = dp->disp.dpy;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700832 android_pixel_format format;
833 getNativePixelFormat(iDpy, cnx, config, &format);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600834
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700835 // Select correct colorspace based on user's attribute list
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700836 EGLint colorSpace = EGL_UNKNOWN;
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700837 std::vector<EGLint> strippedAttribList;
838 if (!processAttributes(dp, nullptr, format, attrib_list, &colorSpace,
839 &strippedAttribList)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600840 ALOGE("error invalid colorspace: %d", colorSpace);
841 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
842 }
Krzysztof Kosińskif2fc4e92018-04-18 16:29:49 -0700843 attrib_list = strippedAttribList.data();
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600844
Mathias Agopian518ec112011-05-13 16:21:08 -0700845 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800846 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700847 if (surface != EGL_NO_SURFACE) {
Krzysztof Kosiński12752442018-05-08 15:25:31 -0700848 egl_surface_t* s =
849 new egl_surface_t(dp.get(), config, NULL, surface,
850 getReportedColorSpace(colorSpace), cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700851 return s;
852 }
853 }
854 return EGL_NO_SURFACE;
855}
Jesse Hall47743382013-02-08 11:13:46 -0800856
Mathias Agopian518ec112011-05-13 16:21:08 -0700857EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
858{
859 clearError();
860
Jesse Hallb29e5e82012-04-04 16:53:42 -0700861 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700862 if (!dp) return EGL_FALSE;
863
Jesse Hallb29e5e82012-04-04 16:53:42 -0700864 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700865 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800866 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700867
868 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800869 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700870 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700871 _s.terminate();
872 }
873 return result;
874}
875
876EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
877 EGLint attribute, EGLint *value)
878{
879 clearError();
880
Jesse Hallb29e5e82012-04-04 16:53:42 -0700881 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700882 if (!dp) return EGL_FALSE;
883
Jesse Hallb29e5e82012-04-04 16:53:42 -0700884 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700885 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800886 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700887
Mathias Agopian518ec112011-05-13 16:21:08 -0700888 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700889 if (s->getColorSpaceAttribute(attribute, value)) {
890 return EGL_TRUE;
891 } else if (s->getSmpte2086Attribute(attribute, value)) {
892 return EGL_TRUE;
893 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600894 return EGL_TRUE;
895 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700896 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700897}
898
Jamie Gennise8696a42012-01-15 18:54:57 -0800899void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800900 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800901 clearError();
902
Jesse Hallb29e5e82012-04-04 16:53:42 -0700903 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800904 if (!dp) {
905 return;
906 }
907
Jesse Hallb29e5e82012-04-04 16:53:42 -0700908 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800909 if (!_s.get()) {
910 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800911 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800912}
913
Mathias Agopian518ec112011-05-13 16:21:08 -0700914// ----------------------------------------------------------------------------
915// Contexts
916// ----------------------------------------------------------------------------
917
918EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
919 EGLContext share_list, const EGLint *attrib_list)
920{
921 clearError();
922
Jesse Hallb29e5e82012-04-04 16:53:42 -0700923 egl_connection_t* cnx = NULL;
924 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700925 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700926 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700927 if (!ContextRef(dp.get(), share_list).get()) {
928 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
929 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700930 egl_context_t* const c = get_context(share_list);
931 share_list = c->context;
932 }
933 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800934 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700935 if (context != EGL_NO_CONTEXT) {
936 // figure out if it's a GLESv1 or GLESv2
937 int version = 0;
938 if (attrib_list) {
939 while (*attrib_list != EGL_NONE) {
940 GLint attr = *attrib_list++;
941 GLint value = *attrib_list++;
942 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
943 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800944 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800945 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800946 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700947 }
948 }
949 };
950 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700951 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
952 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700953 return c;
954 }
955 }
956 return EGL_NO_CONTEXT;
957}
958
959EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
960{
961 clearError();
962
Jesse Hallb29e5e82012-04-04 16:53:42 -0700963 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700964 if (!dp)
965 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700966
Jesse Hallb29e5e82012-04-04 16:53:42 -0700967 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700968 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800969 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800970
Mathias Agopian518ec112011-05-13 16:21:08 -0700971 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800972 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700973 if (result == EGL_TRUE) {
974 _c.terminate();
975 }
976 return result;
977}
978
Mathias Agopian518ec112011-05-13 16:21:08 -0700979EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
980 EGLSurface read, EGLContext ctx)
981{
982 clearError();
983
Jesse Hallb29e5e82012-04-04 16:53:42 -0700984 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800985 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700986
Mathias Agopian5b287a62011-05-16 18:58:55 -0700987 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
988 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
989 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700990 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
991 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800992 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700993 }
994
995 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700996 ContextRef _c(dp.get(), ctx);
997 SurfaceRef _d(dp.get(), draw);
998 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700999
1000 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -07001001 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001002 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -08001003 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001004 }
1005
1006 // these are the underlying implementation's object
1007 EGLContext impl_ctx = EGL_NO_CONTEXT;
1008 EGLSurface impl_draw = EGL_NO_SURFACE;
1009 EGLSurface impl_read = EGL_NO_SURFACE;
1010
1011 // these are our objects structs passed in
1012 egl_context_t * c = NULL;
1013 egl_surface_t const * d = NULL;
1014 egl_surface_t const * r = NULL;
1015
1016 // these are the current objects structs
1017 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001018
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 if (ctx != EGL_NO_CONTEXT) {
1020 c = get_context(ctx);
1021 impl_ctx = c->context;
1022 } else {
1023 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001024 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1025 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001026 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001027 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001028 if (cur_c == NULL) {
1029 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001030 // not an error, there is just no current context.
1031 return EGL_TRUE;
1032 }
1033 }
1034
1035 // retrieve the underlying implementation's draw EGLSurface
1036 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001037 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001038 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001039 impl_draw = d->surface;
1040 }
1041
1042 // retrieve the underlying implementation's read EGLSurface
1043 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001044 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001045 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001046 impl_read = r->surface;
1047 }
1048
Mathias Agopian518ec112011-05-13 16:21:08 -07001049
Jesse Hallb29e5e82012-04-04 16:53:42 -07001050 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001051 draw, read, ctx,
1052 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001053
1054 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001055 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001056 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1057 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001058 _c.acquire();
1059 _r.acquire();
1060 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001061 } else {
1062 setGLHooksThreadSpecific(&gHooksNoContext);
1063 egl_tls_t::setContext(EGL_NO_CONTEXT);
1064 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001065 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001066 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001067 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001068 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001069 }
1070 return result;
1071}
1072
1073
1074EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1075 EGLint attribute, EGLint *value)
1076{
1077 clearError();
1078
Jesse Hallb29e5e82012-04-04 16:53:42 -07001079 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001080 if (!dp) return EGL_FALSE;
1081
Jesse Hallb29e5e82012-04-04 16:53:42 -07001082 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001083 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001084
Mathias Agopian518ec112011-05-13 16:21:08 -07001085 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001086 return c->cnx->egl.eglQueryContext(
1087 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001088
Mathias Agopian518ec112011-05-13 16:21:08 -07001089}
1090
1091EGLContext eglGetCurrentContext(void)
1092{
1093 // could be called before eglInitialize(), but we wouldn't have a context
1094 // then, and this function would correctly return EGL_NO_CONTEXT.
1095
1096 clearError();
1097
1098 EGLContext ctx = getContext();
1099 return ctx;
1100}
1101
1102EGLSurface eglGetCurrentSurface(EGLint readdraw)
1103{
1104 // could be called before eglInitialize(), but we wouldn't have a context
1105 // then, and this function would correctly return EGL_NO_SURFACE.
1106
1107 clearError();
1108
1109 EGLContext ctx = getContext();
1110 if (ctx) {
1111 egl_context_t const * const c = get_context(ctx);
1112 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1113 switch (readdraw) {
1114 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001115 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001116 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1117 }
1118 }
1119 return EGL_NO_SURFACE;
1120}
1121
1122EGLDisplay eglGetCurrentDisplay(void)
1123{
1124 // could be called before eglInitialize(), but we wouldn't have a context
1125 // then, and this function would correctly return EGL_NO_DISPLAY.
1126
1127 clearError();
1128
1129 EGLContext ctx = getContext();
1130 if (ctx) {
1131 egl_context_t const * const c = get_context(ctx);
1132 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1133 return c->dpy;
1134 }
1135 return EGL_NO_DISPLAY;
1136}
1137
1138EGLBoolean eglWaitGL(void)
1139{
Mathias Agopian518ec112011-05-13 16:21:08 -07001140 clearError();
1141
Mathias Agopianada798b2012-02-13 17:09:30 -08001142 egl_connection_t* const cnx = &gEGLImpl;
1143 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001144 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001145
1146 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001147}
1148
1149EGLBoolean eglWaitNative(EGLint engine)
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.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001158}
1159
1160EGLint eglGetError(void)
1161{
Mathias Agopianada798b2012-02-13 17:09:30 -08001162 EGLint err = EGL_SUCCESS;
1163 egl_connection_t* const cnx = &gEGLImpl;
1164 if (cnx->dso) {
1165 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001166 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001167 if (err == EGL_SUCCESS) {
1168 err = egl_tls_t::getError();
1169 }
1170 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001171}
1172
Michael Chockc0ec5e22014-01-27 08:14:33 -08001173static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001174 const char* procname) {
1175 const egl_connection_t* cnx = &gEGLImpl;
1176 void* proc = NULL;
1177
Michael Chockc0ec5e22014-01-27 08:14:33 -08001178 proc = dlsym(cnx->libEgl, procname);
1179 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1180
Jesse Hallc07b5202013-07-04 12:08:16 -07001181 proc = dlsym(cnx->libGles2, procname);
1182 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1183
1184 proc = dlsym(cnx->libGles1, procname);
1185 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1186
1187 return NULL;
1188}
1189
Mathias Agopian518ec112011-05-13 16:21:08 -07001190__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1191{
1192 // eglGetProcAddress() could be the very first function called
1193 // in which case we must make sure we've initialized ourselves, this
1194 // happens the first time egl_get_display() is called.
1195
1196 clearError();
1197
1198 if (egl_init_drivers() == EGL_FALSE) {
1199 setError(EGL_BAD_PARAMETER, NULL);
1200 return NULL;
1201 }
1202
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001203 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001204 return NULL;
1205 }
1206
Mathias Agopian518ec112011-05-13 16:21:08 -07001207 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001208 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001209 if (addr) return addr;
1210
Michael Chockc0ec5e22014-01-27 08:14:33 -08001211 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001212 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001213
Mathias Agopian518ec112011-05-13 16:21:08 -07001214 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1215 pthread_mutex_lock(&sExtensionMapMutex);
1216
1217 /*
1218 * Since eglGetProcAddress() is not associated to anything, it needs
1219 * to return a function pointer that "works" regardless of what
1220 * the current context is.
1221 *
1222 * For this reason, we return a "forwarder", a small stub that takes
1223 * care of calling the function associated with the context
1224 * currently bound.
1225 *
1226 * We first look for extensions we've already resolved, if we're seeing
1227 * this extension for the first time, we go through all our
1228 * implementations and call eglGetProcAddress() and record the
1229 * result in the appropriate implementation hooks and return the
1230 * address of the forwarder corresponding to that hook set.
1231 *
1232 */
1233
Mathias Agopian65421432017-03-08 11:49:05 -08001234 const std::string name(procname);
1235
1236 auto& extentionMap = sGLExtentionMap;
1237 auto pos = extentionMap.find(name);
1238 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001239 const int slot = sGLExtentionSlot;
1240
Steve Blocke6f43dd2012-01-06 19:20:56 +00001241 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001242 "no more slots for eglGetProcAddress(\"%s\")",
1243 procname);
1244
1245 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1246 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001247
1248 egl_connection_t* const cnx = &gEGLImpl;
1249 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001250 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001251 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001252 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1253 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001254 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001255 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001256 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001257
Mathias Agopian518ec112011-05-13 16:21:08 -07001258 if (found) {
1259 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001260 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001261 sGLExtentionSlot++;
1262 }
1263 }
1264
1265 pthread_mutex_unlock(&sExtensionMapMutex);
1266 return addr;
1267}
1268
Mathias Agopian65421432017-03-08 11:49:05 -08001269class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001270public:
1271
1272 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001273 static FrameCompletionThread thread;
1274
1275 char name[64];
1276
1277 std::lock_guard<std::mutex> lock(thread.mMutex);
1278 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1279 ATRACE_NAME(name);
1280
1281 thread.mQueue.push_back(sync);
1282 thread.mCondition.notify_one();
1283 thread.mFramesQueued++;
1284 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001285 }
1286
1287private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001288
Mathias Agopian65421432017-03-08 11:49:05 -08001289 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1290 std::thread thread(&FrameCompletionThread::loop, this);
1291 thread.detach();
1292 }
1293
1294#pragma clang diagnostic push
1295#pragma clang diagnostic ignored "-Wmissing-noreturn"
1296 void loop() {
1297 while (true) {
1298 threadLoop();
1299 }
1300 }
1301#pragma clang diagnostic pop
1302
1303 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001304 EGLSyncKHR sync;
1305 uint32_t frameNum;
1306 {
Mathias Agopian65421432017-03-08 11:49:05 -08001307 std::unique_lock<std::mutex> lock(mMutex);
1308 while (mQueue.empty()) {
1309 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001310 }
1311 sync = mQueue[0];
1312 frameNum = mFramesCompleted;
1313 }
1314 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1315 {
Mathias Agopian65421432017-03-08 11:49:05 -08001316 char name[64];
1317 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1318 ATRACE_NAME(name);
1319
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001320 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1321 if (result == EGL_FALSE) {
1322 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1323 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1324 ALOGE("FrameCompletion: timeout waiting for fence");
1325 }
1326 eglDestroySyncKHR(dpy, sync);
1327 }
1328 {
Mathias Agopian65421432017-03-08 11:49:05 -08001329 std::lock_guard<std::mutex> lock(mMutex);
1330 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001331 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001332 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001333 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001334 }
1335
1336 uint32_t mFramesQueued;
1337 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001338 std::deque<EGLSyncKHR> mQueue;
1339 std::condition_variable mCondition;
1340 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001341};
1342
Dan Stozaa894d082015-02-19 15:27:36 -08001343EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1344 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001345{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001346 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001347 clearError();
1348
Jesse Hallb29e5e82012-04-04 16:53:42 -07001349 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001350 if (!dp) return EGL_FALSE;
1351
Jesse Hallb29e5e82012-04-04 16:53:42 -07001352 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001353 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001354 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001355
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001356 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001357
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001358 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1359 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1360 if (sync != EGL_NO_SYNC_KHR) {
1361 FrameCompletionThread::queueSync(sync);
1362 }
1363 }
1364
Mathias Agopian7db993a2012-03-25 00:49:46 -07001365 if (CC_UNLIKELY(dp->finishOnSwap)) {
1366 uint32_t pixel;
1367 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1368 if (c) {
1369 // glReadPixels() ensures that the frame is complete
1370 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1371 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1372 }
1373 }
1374
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001375 if (!sendSurfaceMetadata(s)) {
1376 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1377 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1378 }
1379
Dan Stozaa894d082015-02-19 15:27:36 -08001380 if (n_rects == 0) {
1381 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1382 }
1383
Mathias Agopian65421432017-03-08 11:49:05 -08001384 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001385 for (int r = 0; r < n_rects; ++r) {
1386 int offset = r * 4;
1387 int x = rects[offset];
1388 int y = rects[offset + 1];
1389 int width = rects[offset + 2];
1390 int height = rects[offset + 3];
1391 android_native_rect_t androidRect;
1392 androidRect.left = x;
1393 androidRect.top = y + height;
1394 androidRect.right = x + width;
1395 androidRect.bottom = y;
1396 androidRects.push_back(androidRect);
1397 }
Mathias Agopian65421432017-03-08 11:49:05 -08001398 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001399
1400 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1401 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1402 rects, n_rects);
1403 } else {
1404 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1405 }
1406}
1407
1408EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1409{
1410 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001411}
1412
1413EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1414 NativePixmapType target)
1415{
1416 clearError();
1417
Jesse Hallb29e5e82012-04-04 16:53:42 -07001418 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001419 if (!dp) return EGL_FALSE;
1420
Jesse Hallb29e5e82012-04-04 16:53:42 -07001421 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001422 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001423 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001424
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001426 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001427}
1428
1429const char* eglQueryString(EGLDisplay dpy, EGLint name)
1430{
1431 clearError();
1432
Chia-I Wue57d1352016-08-15 16:10:02 +08001433 // Generate an error quietly when client extensions (as defined by
1434 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1435 // validate_display to generate the error as validate_display would log
1436 // the error, which can be misleading.
1437 //
1438 // If we want to support EGL_EXT_client_extensions later, we can return
1439 // the client extension string here instead.
1440 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001441 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001442
Jesse Hallb29e5e82012-04-04 16:53:42 -07001443 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001444 if (!dp) return (const char *) NULL;
1445
1446 switch (name) {
1447 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001448 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001449 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001450 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001451 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001452 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001453 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001454 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001455 default:
1456 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001457 }
1458 return setError(EGL_BAD_PARAMETER, (const char *)0);
1459}
1460
Jiyong Park00b15b82017-08-10 20:30:56 +09001461extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001462{
1463 clearError();
1464
1465 const egl_display_ptr dp = validate_display(dpy);
1466 if (!dp) return (const char *) NULL;
1467
1468 switch (name) {
1469 case EGL_VENDOR:
1470 return dp->disp.queryString.vendor;
1471 case EGL_VERSION:
1472 return dp->disp.queryString.version;
1473 case EGL_EXTENSIONS:
1474 return dp->disp.queryString.extensions;
1475 case EGL_CLIENT_APIS:
1476 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001477 default:
1478 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001479 }
1480 return setError(EGL_BAD_PARAMETER, (const char *)0);
1481}
Mathias Agopian518ec112011-05-13 16:21:08 -07001482
1483// ----------------------------------------------------------------------------
1484// EGL 1.1
1485// ----------------------------------------------------------------------------
1486
1487EGLBoolean eglSurfaceAttrib(
1488 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1489{
1490 clearError();
1491
Jesse Hallb29e5e82012-04-04 16:53:42 -07001492 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001493 if (!dp) return EGL_FALSE;
1494
Jesse Hallb29e5e82012-04-04 16:53:42 -07001495 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001496 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001497 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001498
Pablo Ceballosc18be292016-05-31 14:55:42 -07001499 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001500
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001501 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001502 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001503 setError(EGL_BAD_SURFACE, EGL_FALSE);
1504 }
Mathias Agopian65421432017-03-08 11:49:05 -08001505 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1506 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001507 }
1508
Pablo Ceballosc18be292016-05-31 14:55:42 -07001509 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001510 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001511 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001512 }
Mathias Agopian65421432017-03-08 11:49:05 -08001513 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1514 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001515 }
1516
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001517 if (s->setSmpte2086Attribute(attribute, value)) {
1518 return EGL_TRUE;
1519 } else if (s->setCta8613Attribute(attribute, value)) {
1520 return EGL_TRUE;
1521 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001522 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001523 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001524 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001525 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001526}
1527
1528EGLBoolean eglBindTexImage(
1529 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1530{
1531 clearError();
1532
Jesse Hallb29e5e82012-04-04 16:53:42 -07001533 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001534 if (!dp) return EGL_FALSE;
1535
Jesse Hallb29e5e82012-04-04 16:53:42 -07001536 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001537 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001538 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001539
Mathias Agopian518ec112011-05-13 16:21:08 -07001540 egl_surface_t const * const s = get_surface(surface);
1541 if (s->cnx->egl.eglBindTexImage) {
1542 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001543 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001544 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001545 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001546}
1547
1548EGLBoolean eglReleaseTexImage(
1549 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1550{
1551 clearError();
1552
Jesse Hallb29e5e82012-04-04 16:53:42 -07001553 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001554 if (!dp) return EGL_FALSE;
1555
Jesse Hallb29e5e82012-04-04 16:53:42 -07001556 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001557 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001558 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001559
Mathias Agopian518ec112011-05-13 16:21:08 -07001560 egl_surface_t const * const s = get_surface(surface);
1561 if (s->cnx->egl.eglReleaseTexImage) {
1562 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001563 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001564 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001565 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001566}
1567
1568EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1569{
1570 clearError();
1571
Jesse Hallb29e5e82012-04-04 16:53:42 -07001572 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001573 if (!dp) return EGL_FALSE;
1574
1575 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001576 egl_connection_t* const cnx = &gEGLImpl;
1577 if (cnx->dso && cnx->egl.eglSwapInterval) {
1578 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001579 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001580
Mathias Agopian518ec112011-05-13 16:21:08 -07001581 return res;
1582}
1583
1584
1585// ----------------------------------------------------------------------------
1586// EGL 1.2
1587// ----------------------------------------------------------------------------
1588
1589EGLBoolean eglWaitClient(void)
1590{
1591 clearError();
1592
Mathias Agopianada798b2012-02-13 17:09:30 -08001593 egl_connection_t* const cnx = &gEGLImpl;
1594 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001595 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001596
1597 EGLBoolean res;
1598 if (cnx->egl.eglWaitClient) {
1599 res = cnx->egl.eglWaitClient();
1600 } else {
1601 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001602 }
1603 return res;
1604}
1605
1606EGLBoolean eglBindAPI(EGLenum api)
1607{
1608 clearError();
1609
1610 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001611 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001612 }
1613
1614 // bind this API on all EGLs
1615 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001616 egl_connection_t* const cnx = &gEGLImpl;
1617 if (cnx->dso && cnx->egl.eglBindAPI) {
1618 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001619 }
1620 return res;
1621}
1622
1623EGLenum eglQueryAPI(void)
1624{
1625 clearError();
1626
1627 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001628 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001629 }
1630
Mathias Agopianada798b2012-02-13 17:09:30 -08001631 egl_connection_t* const cnx = &gEGLImpl;
1632 if (cnx->dso && cnx->egl.eglQueryAPI) {
1633 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001634 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001635
Mathias Agopian518ec112011-05-13 16:21:08 -07001636 // or, it can only be OpenGL ES
1637 return EGL_OPENGL_ES_API;
1638}
1639
1640EGLBoolean eglReleaseThread(void)
1641{
1642 clearError();
1643
Mathias Agopianada798b2012-02-13 17:09:30 -08001644 egl_connection_t* const cnx = &gEGLImpl;
1645 if (cnx->dso && cnx->egl.eglReleaseThread) {
1646 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001647 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301648
1649 // If there is context bound to the thread, release it
1650 egl_display_t::loseCurrent(get_context(getContext()));
1651
Mathias Agopian518ec112011-05-13 16:21:08 -07001652 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001653 return EGL_TRUE;
1654}
1655
1656EGLSurface eglCreatePbufferFromClientBuffer(
1657 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1658 EGLConfig config, const EGLint *attrib_list)
1659{
1660 clearError();
1661
Jesse Hallb29e5e82012-04-04 16:53:42 -07001662 egl_connection_t* cnx = NULL;
1663 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1664 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001665 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1666 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001667 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001668 }
1669 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1670}
1671
1672// ----------------------------------------------------------------------------
1673// EGL_EGLEXT_VERSION 3
1674// ----------------------------------------------------------------------------
1675
1676EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1677 const EGLint *attrib_list)
1678{
1679 clearError();
1680
Jesse Hallb29e5e82012-04-04 16:53:42 -07001681 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001682 if (!dp) return EGL_FALSE;
1683
Jesse Hallb29e5e82012-04-04 16:53:42 -07001684 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001685 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001686 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001687
1688 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001689 if (s->cnx->egl.eglLockSurfaceKHR) {
1690 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001691 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001692 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001693 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001694}
1695
1696EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1697{
1698 clearError();
1699
Jesse Hallb29e5e82012-04-04 16:53:42 -07001700 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001701 if (!dp) return EGL_FALSE;
1702
Jesse Hallb29e5e82012-04-04 16:53:42 -07001703 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001704 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001705 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001706
1707 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001708 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001709 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001710 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001711 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001712}
1713
1714EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1715 EGLClientBuffer buffer, const EGLint *attrib_list)
1716{
1717 clearError();
1718
Jesse Hallb29e5e82012-04-04 16:53:42 -07001719 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001720 if (!dp) return EGL_NO_IMAGE_KHR;
1721
Jesse Hallb29e5e82012-04-04 16:53:42 -07001722 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001723 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001724
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001725 // Temporary hack: eglImageCreateKHR should accept EGL_GL_COLORSPACE_LINEAR_KHR,
1726 // EGL_GL_COLORSPACE_SRGB_KHR and EGL_GL_COLORSPACE_DEFAULT_EXT if
1727 // EGL_EXT_image_gl_colorspace is supported, but some drivers don't like
1728 // the DEFAULT value and generate an error.
1729 std::vector<EGLint> strippedAttribList;
1730 for (const EGLint *attr = attrib_list; attr && attr[0] != EGL_NONE; attr += 2) {
1731 if (attr[0] == EGL_GL_COLORSPACE_KHR &&
1732 dp->haveExtension("EGL_EXT_image_gl_colorspace")) {
1733 if (attr[1] != EGL_GL_COLORSPACE_LINEAR_KHR &&
1734 attr[1] != EGL_GL_COLORSPACE_SRGB_KHR) {
1735 continue;
1736 }
1737 }
1738 strippedAttribList.push_back(attr[0]);
1739 strippedAttribList.push_back(attr[1]);
1740 }
1741 strippedAttribList.push_back(EGL_NONE);
1742
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001743 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1744 egl_connection_t* const cnx = &gEGLImpl;
1745 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1746 result = cnx->egl.eglCreateImageKHR(
1747 dp->disp.dpy,
1748 c ? c->context : EGL_NO_CONTEXT,
Krzysztof Kosińskiae98af52018-04-26 18:33:36 -07001749 target, buffer, strippedAttribList.data());
Mathias Agopian518ec112011-05-13 16:21:08 -07001750 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001751 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001752}
1753
1754EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1755{
1756 clearError();
1757
Jesse Hallb29e5e82012-04-04 16:53:42 -07001758 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001759 if (!dp) return EGL_FALSE;
1760
Steven Holte646a5c52012-06-04 20:02:11 -07001761 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001762 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001763 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001764 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001765 }
Steven Holte646a5c52012-06-04 20:02:11 -07001766 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001767}
1768
1769// ----------------------------------------------------------------------------
1770// EGL_EGLEXT_VERSION 5
1771// ----------------------------------------------------------------------------
1772
1773
1774EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1775{
1776 clearError();
1777
Jesse Hallb29e5e82012-04-04 16:53:42 -07001778 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001779 if (!dp) return EGL_NO_SYNC_KHR;
1780
Mathias Agopian518ec112011-05-13 16:21:08 -07001781 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001782 egl_connection_t* const cnx = &gEGLImpl;
1783 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1784 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001785 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001786 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001787}
1788
1789EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1790{
1791 clearError();
1792
Jesse Hallb29e5e82012-04-04 16:53:42 -07001793 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001794 if (!dp) return EGL_FALSE;
1795
Mathias Agopian518ec112011-05-13 16:21:08 -07001796 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001797 egl_connection_t* const cnx = &gEGLImpl;
1798 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1799 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001800 }
1801 return result;
1802}
1803
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001804EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1805 clearError();
1806
1807 const egl_display_ptr dp = validate_display(dpy);
1808 if (!dp) return EGL_FALSE;
1809
1810 EGLBoolean result = EGL_FALSE;
1811 egl_connection_t* const cnx = &gEGLImpl;
1812 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1813 result = cnx->egl.eglSignalSyncKHR(
1814 dp->disp.dpy, sync, mode);
1815 }
1816 return result;
1817}
1818
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001819EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1820 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001821{
1822 clearError();
1823
Jesse Hallb29e5e82012-04-04 16:53:42 -07001824 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001825 if (!dp) return EGL_FALSE;
1826
Mathias Agopian737b8962017-02-24 14:32:05 -08001827 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001828 egl_connection_t* const cnx = &gEGLImpl;
1829 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1830 result = cnx->egl.eglClientWaitSyncKHR(
1831 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001832 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001833 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001834}
1835
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001836EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1837 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001838{
1839 clearError();
1840
Jesse Hallb29e5e82012-04-04 16:53:42 -07001841 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001842 if (!dp) return EGL_FALSE;
1843
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001844 EGLBoolean result = EGL_FALSE;
1845 egl_connection_t* const cnx = &gEGLImpl;
1846 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1847 result = cnx->egl.eglGetSyncAttribKHR(
1848 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001849 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001850 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001851}
1852
Season Li000d88f2015-07-01 11:39:40 -07001853EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1854{
1855 clearError();
1856
1857 const egl_display_ptr dp = validate_display(dpy);
1858 if (!dp) return EGL_NO_STREAM_KHR;
1859
1860 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1861 egl_connection_t* const cnx = &gEGLImpl;
1862 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1863 result = cnx->egl.eglCreateStreamKHR(
1864 dp->disp.dpy, attrib_list);
1865 }
1866 return result;
1867}
1868
1869EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1870{
1871 clearError();
1872
1873 const egl_display_ptr dp = validate_display(dpy);
1874 if (!dp) return EGL_FALSE;
1875
1876 EGLBoolean result = EGL_FALSE;
1877 egl_connection_t* const cnx = &gEGLImpl;
1878 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1879 result = cnx->egl.eglDestroyStreamKHR(
1880 dp->disp.dpy, stream);
1881 }
1882 return result;
1883}
1884
1885EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1886 EGLenum attribute, EGLint value)
1887{
1888 clearError();
1889
1890 const egl_display_ptr dp = validate_display(dpy);
1891 if (!dp) return EGL_FALSE;
1892
1893 EGLBoolean result = EGL_FALSE;
1894 egl_connection_t* const cnx = &gEGLImpl;
1895 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1896 result = cnx->egl.eglStreamAttribKHR(
1897 dp->disp.dpy, stream, attribute, value);
1898 }
1899 return result;
1900}
1901
1902EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1903 EGLenum attribute, EGLint *value)
1904{
1905 clearError();
1906
1907 const egl_display_ptr dp = validate_display(dpy);
1908 if (!dp) return EGL_FALSE;
1909
1910 EGLBoolean result = EGL_FALSE;
1911 egl_connection_t* const cnx = &gEGLImpl;
1912 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1913 result = cnx->egl.eglQueryStreamKHR(
1914 dp->disp.dpy, stream, attribute, value);
1915 }
1916 return result;
1917}
1918
1919EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1920 EGLenum attribute, EGLuint64KHR *value)
1921{
1922 clearError();
1923
1924 const egl_display_ptr dp = validate_display(dpy);
1925 if (!dp) return EGL_FALSE;
1926
1927 EGLBoolean result = EGL_FALSE;
1928 egl_connection_t* const cnx = &gEGLImpl;
1929 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1930 result = cnx->egl.eglQueryStreamu64KHR(
1931 dp->disp.dpy, stream, attribute, value);
1932 }
1933 return result;
1934}
1935
1936EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1937 EGLenum attribute, EGLTimeKHR *value)
1938{
1939 clearError();
1940
1941 const egl_display_ptr dp = validate_display(dpy);
1942 if (!dp) return EGL_FALSE;
1943
1944 EGLBoolean result = EGL_FALSE;
1945 egl_connection_t* const cnx = &gEGLImpl;
1946 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1947 result = cnx->egl.eglQueryStreamTimeKHR(
1948 dp->disp.dpy, stream, attribute, value);
1949 }
1950 return result;
1951}
1952
1953EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1954 EGLStreamKHR stream, const EGLint *attrib_list)
1955{
1956 clearError();
1957
1958 egl_display_ptr dp = validate_display(dpy);
1959 if (!dp) return EGL_NO_SURFACE;
1960
1961 egl_connection_t* const cnx = &gEGLImpl;
1962 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1963 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1964 dp->disp.dpy, config, stream, attrib_list);
1965 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001966 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
1967 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001968 return s;
1969 }
1970 }
1971 return EGL_NO_SURFACE;
1972}
1973
1974EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1975 EGLStreamKHR stream)
1976{
1977 clearError();
1978
1979 const egl_display_ptr dp = validate_display(dpy);
1980 if (!dp) return EGL_FALSE;
1981
1982 EGLBoolean result = EGL_FALSE;
1983 egl_connection_t* const cnx = &gEGLImpl;
1984 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1985 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1986 dp->disp.dpy, stream);
1987 }
1988 return result;
1989}
1990
1991EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1992 EGLStreamKHR stream)
1993{
1994 clearError();
1995
1996 const egl_display_ptr dp = validate_display(dpy);
1997 if (!dp) return EGL_FALSE;
1998
1999 EGLBoolean result = EGL_FALSE;
2000 egl_connection_t* const cnx = &gEGLImpl;
2001 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
2002 result = cnx->egl.eglStreamConsumerAcquireKHR(
2003 dp->disp.dpy, stream);
2004 }
2005 return result;
2006}
2007
2008EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2009 EGLStreamKHR stream)
2010{
2011 clearError();
2012
2013 const egl_display_ptr dp = validate_display(dpy);
2014 if (!dp) return EGL_FALSE;
2015
2016 EGLBoolean result = EGL_FALSE;
2017 egl_connection_t* const cnx = &gEGLImpl;
2018 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2019 result = cnx->egl.eglStreamConsumerReleaseKHR(
2020 dp->disp.dpy, stream);
2021 }
2022 return result;
2023}
2024
2025EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2026 EGLDisplay dpy, EGLStreamKHR stream)
2027{
2028 clearError();
2029
2030 const egl_display_ptr dp = validate_display(dpy);
2031 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2032
2033 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2034 egl_connection_t* const cnx = &gEGLImpl;
2035 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2036 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2037 dp->disp.dpy, stream);
2038 }
2039 return result;
2040}
2041
2042EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2043 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2044{
2045 clearError();
2046
2047 const egl_display_ptr dp = validate_display(dpy);
2048 if (!dp) return EGL_NO_STREAM_KHR;
2049
2050 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2051 egl_connection_t* const cnx = &gEGLImpl;
2052 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2053 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2054 dp->disp.dpy, file_descriptor);
2055 }
2056 return result;
2057}
2058
Mathias Agopian518ec112011-05-13 16:21:08 -07002059// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002060// EGL_EGLEXT_VERSION 15
2061// ----------------------------------------------------------------------------
2062
2063EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2064 clearError();
2065 const egl_display_ptr dp = validate_display(dpy);
2066 if (!dp) return EGL_FALSE;
2067 EGLint result = EGL_FALSE;
2068 egl_connection_t* const cnx = &gEGLImpl;
2069 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2070 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2071 }
2072 return result;
2073}
2074
2075// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002076// ANDROID extensions
2077// ----------------------------------------------------------------------------
2078
Jamie Gennis331841b2012-09-06 14:52:00 -07002079EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2080{
2081 clearError();
2082
2083 const egl_display_ptr dp = validate_display(dpy);
2084 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2085
2086 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2087 egl_connection_t* const cnx = &gEGLImpl;
2088 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2089 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2090 }
2091 return result;
2092}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002093
Andy McFadden72841452013-03-01 16:25:32 -08002094EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2095 EGLnsecsANDROID time)
2096{
2097 clearError();
2098
2099 const egl_display_ptr dp = validate_display(dpy);
2100 if (!dp) {
2101 return EGL_FALSE;
2102 }
2103
2104 SurfaceRef _s(dp.get(), surface);
2105 if (!_s.get()) {
2106 setError(EGL_BAD_SURFACE, EGL_FALSE);
2107 return EGL_FALSE;
2108 }
2109
2110 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002111 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002112
2113 return EGL_TRUE;
2114}
2115
Craig Donner60761072017-01-27 12:30:44 -08002116EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2117 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002118 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2119 // this function cannot be implemented when this libEGL is built for
2120 // vendors.
2121#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002122 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002123 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002124#else
2125 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2126#endif
Craig Donner60761072017-01-27 12:30:44 -08002127}
2128
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002129// ----------------------------------------------------------------------------
2130// NVIDIA extensions
2131// ----------------------------------------------------------------------------
2132EGLuint64NV eglGetSystemTimeFrequencyNV()
2133{
2134 clearError();
2135
2136 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002137 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002138 }
2139
2140 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002141 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002142
Mathias Agopianada798b2012-02-13 17:09:30 -08002143 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2144 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002145 }
2146
Mathias Agopian737b8962017-02-24 14:32:05 -08002147 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002148}
2149
2150EGLuint64NV eglGetSystemTimeNV()
2151{
2152 clearError();
2153
2154 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002155 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002156 }
2157
2158 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002159 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002160
Mathias Agopianada798b2012-02-13 17:09:30 -08002161 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2162 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002163 }
2164
Mathias Agopian737b8962017-02-24 14:32:05 -08002165 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002166}
Dan Stozaa894d082015-02-19 15:27:36 -08002167
2168// ----------------------------------------------------------------------------
2169// Partial update extension
2170// ----------------------------------------------------------------------------
2171EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2172 EGLint *rects, EGLint n_rects)
2173{
2174 clearError();
2175
2176 const egl_display_ptr dp = validate_display(dpy);
2177 if (!dp) {
2178 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2179 return EGL_FALSE;
2180 }
2181
2182 SurfaceRef _s(dp.get(), surface);
2183 if (!_s.get()) {
2184 setError(EGL_BAD_SURFACE, EGL_FALSE);
2185 return EGL_FALSE;
2186 }
2187
2188 egl_surface_t const * const s = get_surface(surface);
2189 if (s->cnx->egl.eglSetDamageRegionKHR) {
2190 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2191 rects, n_rects);
2192 }
2193
2194 return EGL_FALSE;
2195}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002196
Brian Anderson1049d1d2016-12-16 17:25:57 -08002197EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2198 EGLuint64KHR *frameId) {
2199 clearError();
2200
2201 const egl_display_ptr dp = validate_display(dpy);
2202 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002203 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002204 }
2205
2206 SurfaceRef _s(dp.get(), surface);
2207 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002208 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002209 }
2210
2211 egl_surface_t const * const s = get_surface(surface);
2212
Mathias Agopian65421432017-03-08 11:49:05 -08002213 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002214 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002215 }
2216
2217 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002218 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002219
Mathias Agopian65421432017-03-08 11:49:05 -08002220 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002221 // This should not happen. Return an error that is not in the spec
2222 // so it's obvious something is very wrong.
2223 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002224 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002225 }
2226
2227 *frameId = nextFrameId;
2228 return EGL_TRUE;
2229}
2230
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002231EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2232 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2233{
2234 clearError();
2235
2236 const egl_display_ptr dp = validate_display(dpy);
2237 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002238 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002239 }
2240
2241 SurfaceRef _s(dp.get(), surface);
2242 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002243 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002244 }
2245
2246 egl_surface_t const * const s = get_surface(surface);
2247
Mathias Agopian65421432017-03-08 11:49:05 -08002248 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002249 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002250 }
2251
2252 nsecs_t* compositeDeadline = nullptr;
2253 nsecs_t* compositeInterval = nullptr;
2254 nsecs_t* compositeToPresentLatency = nullptr;
2255
2256 for (int i = 0; i < numTimestamps; i++) {
2257 switch (names[i]) {
2258 case EGL_COMPOSITE_DEADLINE_ANDROID:
2259 compositeDeadline = &values[i];
2260 break;
2261 case EGL_COMPOSITE_INTERVAL_ANDROID:
2262 compositeInterval = &values[i];
2263 break;
2264 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2265 compositeToPresentLatency = &values[i];
2266 break;
2267 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002268 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002269 }
2270 }
2271
Mathias Agopian65421432017-03-08 11:49:05 -08002272 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002273 compositeDeadline, compositeInterval, compositeToPresentLatency);
2274
2275 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002276 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002277 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002278 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002279 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002280 default:
2281 // This should not happen. Return an error that is not in the spec
2282 // so it's obvious something is very wrong.
2283 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002284 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002285 }
2286}
2287
2288EGLBoolean eglGetCompositorTimingSupportedANDROID(
2289 EGLDisplay dpy, EGLSurface surface, EGLint name)
2290{
2291 clearError();
2292
2293 const egl_display_ptr dp = validate_display(dpy);
2294 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002295 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002296 }
2297
2298 SurfaceRef _s(dp.get(), surface);
2299 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002300 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002301 }
2302
2303 egl_surface_t const * const s = get_surface(surface);
2304
Mathias Agopian65421432017-03-08 11:49:05 -08002305 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002306 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002307 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002308 }
2309
2310 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002311 case EGL_COMPOSITE_DEADLINE_ANDROID:
2312 case EGL_COMPOSITE_INTERVAL_ANDROID:
2313 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2314 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002315 default:
2316 return EGL_FALSE;
2317 }
2318}
2319
Pablo Ceballosc18be292016-05-31 14:55:42 -07002320EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002321 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002322 EGLnsecsANDROID *values)
2323{
2324 clearError();
2325
2326 const egl_display_ptr dp = validate_display(dpy);
2327 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002328 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002329 }
2330
2331 SurfaceRef _s(dp.get(), surface);
2332 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002333 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002334 }
2335
2336 egl_surface_t const * const s = get_surface(surface);
2337
Mathias Agopian65421432017-03-08 11:49:05 -08002338 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002339 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002340 }
2341
Brian Andersondbd0ea82016-07-22 09:38:59 -07002342 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002343 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002344 nsecs_t* latchTime = nullptr;
2345 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002346 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002347 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002348 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002349 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002350 nsecs_t* releaseTime = nullptr;
2351
2352 for (int i = 0; i < numTimestamps; i++) {
2353 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002354 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2355 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002356 break;
2357 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2358 acquireTime = &values[i];
2359 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002360 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2361 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002362 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002363 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2364 firstRefreshStartTime = &values[i];
2365 break;
2366 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2367 lastRefreshStartTime = &values[i];
2368 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002369 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2370 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002371 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002372 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2373 displayPresentTime = &values[i];
2374 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002375 case EGL_DEQUEUE_READY_TIME_ANDROID:
2376 dequeueReadyTime = &values[i];
2377 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002378 case EGL_READS_DONE_TIME_ANDROID:
2379 releaseTime = &values[i];
2380 break;
2381 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002382 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002383 }
2384 }
2385
Mathias Agopian65421432017-03-08 11:49:05 -08002386 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002387 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002388 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002389 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002390
Brian Anderson069b3652016-07-22 10:32:47 -07002391 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002392 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002393 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002394 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002395 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002396 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002397 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002398 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002399 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2400 default:
2401 // This should not happen. Return an error that is not in the spec
2402 // so it's obvious something is very wrong.
2403 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2404 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002405 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002406}
2407
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002408EGLBoolean eglGetFrameTimestampSupportedANDROID(
2409 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002410{
2411 clearError();
2412
2413 const egl_display_ptr dp = validate_display(dpy);
2414 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002415 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002416 }
2417
2418 SurfaceRef _s(dp.get(), surface);
2419 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002420 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002421 }
2422
2423 egl_surface_t const * const s = get_surface(surface);
2424
Mathias Agopian65421432017-03-08 11:49:05 -08002425 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002426 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002427 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002428 }
2429
2430 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002431 case EGL_COMPOSITE_DEADLINE_ANDROID:
2432 case EGL_COMPOSITE_INTERVAL_ANDROID:
2433 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002434 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002435 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002436 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2437 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2438 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002439 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002440 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002441 case EGL_READS_DONE_TIME_ANDROID:
2442 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002443 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2444 int value = 0;
2445 window->query(window,
2446 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2447 return value == 0 ? EGL_FALSE : EGL_TRUE;
2448 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002449 default:
2450 return EGL_FALSE;
2451 }
2452}