blob: 2cd7c4c94339c3da521461dd3c4862beb8c3eea9 [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
Mathias Agopian311b4792017-02-28 15:00:49 -080083char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070084 "EGL_KHR_get_all_proc_addresses "
85 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080086 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080087 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080088 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070089 "EGL_ANDROID_get_frame_timestamps "
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070090 "EGL_EXT_surface_SMPTE2086_metadata "
91 "EGL_EXT_surface_CTA861_3_metadata "
Jesse Hall21558da2013-08-06 15:31:22 -070092 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080093
94char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095 "EGL_KHR_image " // mandatory
96 "EGL_KHR_image_base " // mandatory
Krzysztof KosiƄski4245cba2018-03-30 16:21:39 -070097 "EGL_KHR_image_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070098 "EGL_KHR_image_pixmap "
99 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700100 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700101 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700102 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_cubemap_image "
104 "EGL_KHR_gl_renderbuffer_image "
105 "EGL_KHR_reusable_sync "
106 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700107 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700108 "EGL_KHR_config_attribs "
109 "EGL_KHR_surfaceless_context "
110 "EGL_KHR_stream "
111 "EGL_KHR_stream_fifo "
112 "EGL_KHR_stream_producer_eglsurface "
113 "EGL_KHR_stream_consumer_gltexture "
114 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700115 "EGL_EXT_create_context_robustness "
116 "EGL_NV_system_time "
117 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700118 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700119 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800120 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700121 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800122 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700123 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700124 "EGL_KHR_mutable_render_buffer "
Mika IsojÀrvif37864b2016-04-15 11:58:56 -0700125 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700126 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000127 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800128 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700129 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600130// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700131
132// extensions not exposed to applications but used by the ANDROID system
133// "EGL_ANDROID_blob_cache " // strongly recommended
134// "EGL_IMG_hibernate_process " // optional
135// "EGL_ANDROID_native_fence_sync " // strongly recommended
136// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700137// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700138
139/*
140 * EGL Extensions entry-points exposed to 3rd party applications
141 * (keep in sync with gExtensionString above)
142 *
143 */
144static const extention_map_t sExtensionMap[] = {
145 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700146 { "eglLockSurfaceKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
148 { "eglUnlockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700150
151 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700152 { "eglCreateImageKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
154 { "eglDestroyImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700156
157 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
158 { "eglCreateSyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
160 { "eglDestroySyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
162 { "eglClientWaitSyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
164 { "eglSignalSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
166 { "eglGetSyncAttribKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
168
169 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800170 { "eglGetSystemTimeFrequencyNV",
171 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
172 { "eglGetSystemTimeNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700174
Mathias Agopian2bb71682013-03-27 17:32:41 -0700175 // EGL_KHR_wait_sync
176 { "eglWaitSyncKHR",
177 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700178
179 // EGL_ANDROID_presentation_time
180 { "eglPresentationTimeANDROID",
181 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800182
183 // EGL_KHR_swap_buffers_with_damage
184 { "eglSwapBuffersWithDamageKHR",
185 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
186
Craig Donner60761072017-01-27 12:30:44 -0800187 // EGL_ANDROID_get_native_client_buffer
188 { "eglGetNativeClientBufferANDROID",
189 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
190
Dan Stozaa894d082015-02-19 15:27:36 -0800191 // EGL_KHR_partial_update
192 { "eglSetDamageRegionKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700194
195 { "eglCreateStreamKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
197 { "eglDestroyStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
199 { "eglStreamAttribKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
201 { "eglQueryStreamKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
203 { "eglQueryStreamu64KHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
205 { "eglQueryStreamTimeKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
207 { "eglCreateStreamProducerSurfaceKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
209 { "eglStreamConsumerGLTextureExternalKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
211 { "eglStreamConsumerAcquireKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
213 { "eglStreamConsumerReleaseKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
215 { "eglGetStreamFileDescriptorKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
217 { "eglCreateStreamFromFileDescriptorKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700219
220 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800221 { "eglGetNextFrameIdANDROID",
222 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800223 { "eglGetCompositorTimingANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
225 { "eglGetCompositorTimingSupportedANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700227 { "eglGetFrameTimestampsANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800229 { "eglGetFrameTimestampSupportedANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700231
232 // EGL_ANDROID_native_fence_sync
233 { "eglDupNativeFenceFDANDROID",
234 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700235};
236
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700237/*
238 * These extensions entry-points should not be exposed to applications.
239 * They're used internally by the Android EGL layer.
240 */
241#define FILTER_EXTENSIONS(procname) \
242 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
243 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700244 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700245
Mathias Agopian518ec112011-05-13 16:21:08 -0700246// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800247static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
248
Mathias Agopian518ec112011-05-13 16:21:08 -0700249static int sGLExtentionSlot = 0;
250static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
251
252static void(*findProcAddress(const char* name,
253 const extention_map_t* map, size_t n))() {
254 for (uint32_t i=0 ; i<n ; i++) {
255 if (!strcmp(name, map[i].name)) {
256 return map[i].address;
257 }
258 }
259 return NULL;
260}
261
262// ----------------------------------------------------------------------------
263
Mathias Agopian518ec112011-05-13 16:21:08 -0700264extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
265extern EGLBoolean egl_init_drivers();
266extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700267extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700268
Mathias Agopian518ec112011-05-13 16:21:08 -0700269} // namespace android;
270
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700271
Mathias Agopian518ec112011-05-13 16:21:08 -0700272// ----------------------------------------------------------------------------
273
274static inline void clearError() { egl_tls_t::clearError(); }
275static inline EGLContext getContext() { return egl_tls_t::getContext(); }
276
277// ----------------------------------------------------------------------------
278
279EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
280{
Jesse Hall1508ae62017-01-19 17:43:26 -0800281 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700282 clearError();
283
Dan Stozac3289c42014-01-17 11:38:34 -0800284 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700285 if (index >= NUM_DISPLAYS) {
286 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
287 }
288
289 if (egl_init_drivers() == EGL_FALSE) {
290 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
291 }
292
293 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
294 return dpy;
295}
296
297// ----------------------------------------------------------------------------
298// Initialization
299// ----------------------------------------------------------------------------
300
301EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
302{
303 clearError();
304
Jesse Hallb29e5e82012-04-04 16:53:42 -0700305 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800306 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700307
308 EGLBoolean res = dp->initialize(major, minor);
309
310 return res;
311}
312
313EGLBoolean eglTerminate(EGLDisplay dpy)
314{
315 // NOTE: don't unload the drivers b/c some APIs can be called
316 // after eglTerminate() has been called. eglTerminate() only
317 // terminates an EGLDisplay, not a EGL itself.
318
319 clearError();
320
Jesse Hallb29e5e82012-04-04 16:53:42 -0700321 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800322 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700323
324 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800325
Mathias Agopian518ec112011-05-13 16:21:08 -0700326 return res;
327}
328
329// ----------------------------------------------------------------------------
330// configuration
331// ----------------------------------------------------------------------------
332
333EGLBoolean eglGetConfigs( EGLDisplay dpy,
334 EGLConfig *configs,
335 EGLint config_size, EGLint *num_config)
336{
337 clearError();
338
Jesse Hallb29e5e82012-04-04 16:53:42 -0700339 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700340 if (!dp) return EGL_FALSE;
341
Mathias Agopian7773c432012-02-13 20:06:08 -0800342 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800343 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700344 }
345
Mathias Agopian7773c432012-02-13 20:06:08 -0800346 EGLBoolean res = EGL_FALSE;
347 *num_config = 0;
348
349 egl_connection_t* const cnx = &gEGLImpl;
350 if (cnx->dso) {
351 res = cnx->egl.eglGetConfigs(
352 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700353 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800354
355 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700356}
357
358EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
359 EGLConfig *configs, EGLint config_size,
360 EGLint *num_config)
361{
362 clearError();
363
Jesse Hallb29e5e82012-04-04 16:53:42 -0700364 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700365 if (!dp) return EGL_FALSE;
366
367 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800368 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700369 }
370
Mathias Agopian518ec112011-05-13 16:21:08 -0700371 EGLBoolean res = EGL_FALSE;
372 *num_config = 0;
373
Mathias Agopianada798b2012-02-13 17:09:30 -0800374 egl_connection_t* const cnx = &gEGLImpl;
375 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700376 if (attrib_list) {
377 char value[PROPERTY_VALUE_MAX];
378 property_get("debug.egl.force_msaa", value, "false");
379
380 if (!strcmp(value, "true")) {
381 size_t attribCount = 0;
382 EGLint attrib = attrib_list[0];
383
384 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700385 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700386 const EGLint *attribRendererable = NULL;
387 const EGLint *attribCaveat = NULL;
388
389 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700390 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700391 while (attrib != EGL_NONE) {
392 attrib = attrib_list[attribCount];
393 switch (attrib) {
394 case EGL_RENDERABLE_TYPE:
395 attribRendererable = &attrib_list[attribCount];
396 break;
397 case EGL_CONFIG_CAVEAT:
398 attribCaveat = &attrib_list[attribCount];
399 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800400 default:
401 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700402 }
403 attribCount++;
404 }
405
406 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
407 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800408
Romain Guy1cffc802012-10-15 18:13:05 -0700409 // Insert 2 extra attributes to force-enable MSAA 4x
410 EGLint aaAttribs[attribCount + 4];
411 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
412 aaAttribs[1] = 1;
413 aaAttribs[2] = EGL_SAMPLES;
414 aaAttribs[3] = 4;
415
416 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
417
418 EGLint numConfigAA;
419 EGLBoolean resAA = cnx->egl.eglChooseConfig(
420 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
421
422 if (resAA == EGL_TRUE && numConfigAA > 0) {
423 ALOGD("Enabling MSAA 4x");
424 *num_config = numConfigAA;
425 return resAA;
426 }
427 }
428 }
429 }
430
Mathias Agopian7773c432012-02-13 20:06:08 -0800431 res = cnx->egl.eglChooseConfig(
432 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700433 }
434 return res;
435}
436
437EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
438 EGLint attribute, EGLint *value)
439{
440 clearError();
441
Jesse Hallb29e5e82012-04-04 16:53:42 -0700442 egl_connection_t* cnx = NULL;
443 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
444 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800445
Mathias Agopian518ec112011-05-13 16:21:08 -0700446 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800447 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700448}
449
450// ----------------------------------------------------------------------------
451// surfaces
452// ----------------------------------------------------------------------------
453
Jesse Hallc2e41222013-08-08 13:40:22 -0700454// Turn linear formats into corresponding sRGB formats when colorspace is
455// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
456// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800457// the modification isn't possible, the original dataSpace is returned.
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600458static android_dataspace modifyBufferDataspace(android_dataspace dataSpace,
459 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800461 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700462 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800463 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600464 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
465 return HAL_DATASPACE_DISPLAY_P3;
466 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
467 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600468 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
469 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600470 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
471 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700472 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT) {
473 return HAL_DATASPACE_BT2020_LINEAR;
474 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_PQ_EXT) {
475 return HAL_DATASPACE_BT2020_PQ;
Jesse Hallc2e41222013-08-08 13:40:22 -0700476 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800477 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700478}
479
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700480// stripAttributes is used by eglCreateWindowSurface, eglCreatePbufferSurface
481// and eglCreatePixmapSurface to clean up color space related Window parameters
482// that a driver does not advertise support for.
483// Return true if stripped_attrib_list has stripped contents.
484
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700485static EGLBoolean stripAttributes(egl_display_ptr dp, const EGLint* attrib_list,
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -0800486 EGLint format,
487 std::vector<EGLint>& stripped_attrib_list) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600488 std::vector<EGLint> allowedColorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700489 bool haveColorSpaceSupport = dp->haveExtension("EGL_KHR_gl_colorspace");
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600490 switch (format) {
491 case HAL_PIXEL_FORMAT_RGBA_8888:
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700492 if (haveColorSpaceSupport) {
493 // Spec says:
494 // [fn1] Only OpenGL and OpenGL ES contexts which support sRGB
495 // rendering must respect requests for EGL_GL_COLORSPACE_SRGB_KHR, and
496 // only to sRGB formats supported by the context (normally just SRGB8)
497 // Older versions not supporting sRGB rendering will ignore this
498 // surface attribute.
499 //
500 // We support sRGB and pixel format is SRGB8, so allow
501 // the EGL_GL_COLORSPACE_SRGB_KHR and
502 // EGL_GL_COLORSPACE_LINEAR_KHR
503 // colorspaces to be specified.
504
505 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
506 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
507 }
508 if (findExtension(dp->disp.queryString.extensions,
509 "EGL_EXT_gl_colorspace_display_p3_linear")) {
510 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
511 }
512 if (findExtension(dp->disp.queryString.extensions,
513 "EGL_EXT_gl_colorspace_display_p3")) {
514 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
515 }
516 if (findExtension(dp->disp.queryString.extensions,
517 "EGL_EXT_gl_colorspace_bt2020_linear")) {
518 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
519 }
520 if (findExtension(dp->disp.queryString.extensions,
521 "EGL_EXT_gl_colorspace_bt2020_pq")) {
522 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
523 }
524 if (findExtension(dp->disp.queryString.extensions,
525 "EGL_EXT_gl_colorspace_scrgb_linear")) {
526 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
527 }
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600528 break;
529
530 case HAL_PIXEL_FORMAT_RGBA_FP16:
531 case HAL_PIXEL_FORMAT_RGBA_1010102:
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700532 case HAL_PIXEL_FORMAT_RGB_565:
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600533 // Future: if driver supports XXXX extension, we can pass down that colorspace
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700534 default:
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600535 break;
536 }
537
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700538 if (!attrib_list) return false;
539
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600540 bool stripped = false;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700541 for (const EGLint* attr = attrib_list; attr[0] != EGL_NONE; attr += 2) {
542 switch (attr[0]) {
543 case EGL_GL_COLORSPACE_KHR: {
544 EGLint colorSpace = attr[1];
545 bool found = false;
546 // Verify that color space is allowed
547 for (auto it : allowedColorSpaces) {
548 if (colorSpace == it) {
549 found = true;
550 }
551 }
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700552 if (found) {
553 // Found supported attribute
554 stripped_attrib_list.push_back(attr[0]);
555 stripped_attrib_list.push_back(attr[1]);
556 } else if (!haveColorSpaceSupport) {
557 // Device does not support colorspace extension
558 // pass on the attribute and let downstream
559 // components validate like normal
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700560 stripped_attrib_list.push_back(attr[0]);
561 stripped_attrib_list.push_back(attr[1]);
Courtney Goeltzenleuchter52de2fd2018-02-01 09:51:58 -0700562 } else {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700563 // Found supported attribute that driver does not
564 // support, strip it.
Courtney Goeltzenleuchter52de2fd2018-02-01 09:51:58 -0700565 stripped = true;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600566 }
567 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700568 break;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700569 default:
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600570 stripped_attrib_list.push_back(attr[0]);
571 stripped_attrib_list.push_back(attr[1]);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700572 break;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600573 }
574 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700575
576 // Make sure there is at least one attribute
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600577 if (stripped) {
578 stripped_attrib_list.push_back(EGL_NONE);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600579 }
580 return stripped;
581}
582
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600583static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, NativeWindowType window,
584 const EGLint* attrib_list, EGLint& colorSpace,
585 android_dataspace& dataSpace) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600586 colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
587 dataSpace = HAL_DATASPACE_UNKNOWN;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600588
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600589 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
590 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
591 if (*attr == EGL_GL_COLORSPACE_KHR) {
592 colorSpace = attr[1];
593 bool found = false;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600594 bool verify = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600595 // Verify that color space is allowed
596 if (colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
597 colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600598 // SRGB and LINEAR are always supported when EGL_KHR_gl_colorspace
599 // is available, so no need to verify.
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600600 found = true;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600601 verify = false;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700602 } else if (colorSpace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT &&
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600603 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
604 found = true;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700605 } else if (colorSpace == EGL_GL_COLORSPACE_BT2020_PQ_EXT &&
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600606 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
607 found = true;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600608 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_EXT &&
609 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb")) {
610 found = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600611 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
612 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
613 found = true;
614 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
615 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
616 found = true;
617 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
618 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
619 found = true;
620 }
621 if (!found) {
622 return false;
623 }
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600624 if (verify && window) {
625 bool wide_color_support = true;
626 // Ordinarily we'd put a call to native_window_get_wide_color_support
627 // at the beginning of the function so that we'll have the
628 // result when needed elsewhere in the function.
629 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
630 // SurfaceFlinger is required to answer the call below we would
631 // end up in a deadlock situation. By moving the call to only happen
632 // if the application has specifically asked for wide-color we avoid
633 // the deadlock with SurfaceFlinger since it will not ask for a
634 // wide-color surface.
635 int err = native_window_get_wide_color_support(window, &wide_color_support);
636
637 if (err) {
638 ALOGE("getColorSpaceAttribute: invalid window (win=%p) "
639 "failed (%#x) (already connected to another API?)",
640 window, err);
641 return false;
642 }
643 if (!wide_color_support) {
644 // Application has asked for a wide-color colorspace but
645 // wide-color support isn't available on the display the window is on.
646 return false;
647 }
648 }
Courtney Goeltzenleuchter1d4f7a22017-05-05 13:30:25 -0600649 // Only change the dataSpace from default if the application
650 // has explicitly set the color space with a EGL_GL_COLORSPACE_KHR attribute.
651 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600652 }
653 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600654 }
655 return true;
656}
657
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600658static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
659 EGLint& colorSpace, android_dataspace& dataSpace) {
660 return getColorSpaceAttribute(dp, NULL, attrib_list, colorSpace, dataSpace);
661}
662
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600663void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config, EGLint& format) {
664 // Set the native window's buffers format to match what this config requests.
665 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
666 // of our native format. So if sRGB gamma is requested, we have to
667 // modify the EGLconfig's format before setting the native window's
668 // format.
669
670 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
671 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
672
673 EGLint a = 0;
674 EGLint r, g, b;
675 r = g = b = 0;
676 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
677 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
678 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
679 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
680 EGLint colorDepth = r + g + b;
681
682 // Today, the driver only understands sRGB and linear on 888X
683 // formats. Strip other colorspaces from the attribute list and
684 // only use them to set the dataspace via
685 // native_window_set_buffers_dataspace
686 // if pixel format is RGBX 8888
687 // TBD: Can test for future extensions that indicate that driver
688 // handles requested color space and we can let it through.
689 // allow SRGB and LINEAR. All others need to be stripped.
690 // else if 565, 4444
691 // TBD: Can we assume these are supported if 8888 is?
692 // else if FP16 or 1010102
693 // strip colorspace from attribs.
694 // endif
695 if (a == 0) {
696 if (colorDepth <= 16) {
697 format = HAL_PIXEL_FORMAT_RGB_565;
698 } else {
699 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
700 if (colorDepth > 24) {
701 format = HAL_PIXEL_FORMAT_RGBA_1010102;
702 } else {
703 format = HAL_PIXEL_FORMAT_RGBX_8888;
704 }
705 } else {
706 format = HAL_PIXEL_FORMAT_RGBA_FP16;
707 }
708 }
709 } else {
710 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
711 if (colorDepth > 24) {
712 format = HAL_PIXEL_FORMAT_RGBA_1010102;
713 } else {
714 format = HAL_PIXEL_FORMAT_RGBA_8888;
715 }
716 } else {
717 format = HAL_PIXEL_FORMAT_RGBA_FP16;
718 }
719 }
720}
721
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700722EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
723 android_smpte2086_metadata smpteMetadata;
724 if (s->getSmpte2086Metadata(smpteMetadata)) {
725 int err =
726 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
727 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700728 if (err != 0) {
729 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
730 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700731 return EGL_FALSE;
732 }
733 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700734 android_cta861_3_metadata cta8613Metadata;
735 if (s->getCta8613Metadata(cta8613Metadata)) {
736 int err =
737 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
738 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700739 if (err != 0) {
740 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
741 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700742 return EGL_FALSE;
743 }
744 }
745 return EGL_TRUE;
746}
747
Mathias Agopian518ec112011-05-13 16:21:08 -0700748EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
749 NativeWindowType window,
750 const EGLint *attrib_list)
751{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700752 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700753 clearError();
754
Jesse Hallb29e5e82012-04-04 16:53:42 -0700755 egl_connection_t* cnx = NULL;
756 egl_display_ptr dp = validate_display_connection(dpy, cnx);
757 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800758 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700759
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800760 if (!window) {
761 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
762 }
763
764 int value = 0;
765 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
766 if (!value) {
767 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
768 }
769
Andy McFaddend566ce32014-01-07 15:54:17 -0800770 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800771 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800772 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
773 "failed (%#x) (already connected to another API?)",
774 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700775 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700776 }
777
Mathias Agopian95921562017-02-24 14:31:31 -0800778 EGLint format;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600779 getNativePixelFormat(iDpy, cnx, config, format);
780
781 // now select correct colorspace and dataspace based on user's attribute list
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600782 EGLint colorSpace;
783 android_dataspace dataSpace;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600784 if (!getColorSpaceAttribute(dp, window, attrib_list, colorSpace, dataSpace)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600785 ALOGE("error invalid colorspace: %d", colorSpace);
786 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700787 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800788
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600789 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700790 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600791 // Had to modify the attribute list due to use of color space.
792 // Use modified list from here on.
793 attrib_list = strippedAttribList.data();
794 }
795
Jesse Hallc2e41222013-08-08 13:40:22 -0700796 if (format != 0) {
797 int err = native_window_set_buffers_format(window, format);
798 if (err != 0) {
799 ALOGE("error setting native window pixel format: %s (%d)",
800 strerror(-err), err);
801 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
802 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
803 }
804 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700805
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800806 if (dataSpace != 0) {
807 int err = native_window_set_buffers_data_space(window, dataSpace);
808 if (err != 0) {
809 ALOGE("error setting native window pixel dataSpace: %s (%d)",
810 strerror(-err), err);
811 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
812 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
813 }
814 }
815
Jamie Gennis59769462011-11-19 18:04:43 -0800816 // the EGL spec requires that a new EGLSurface default to swap interval
817 // 1, so explicitly set that on the window here.
818 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
819 anw->setSwapInterval(anw, 1);
820
Mathias Agopian518ec112011-05-13 16:21:08 -0700821 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800822 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700823 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600824 egl_surface_t* s =
825 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700826 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700828
829 // EGLSurface creation failed
830 native_window_set_buffers_format(window, 0);
831 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700832 }
833 return EGL_NO_SURFACE;
834}
835
836EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
837 NativePixmapType pixmap,
838 const EGLint *attrib_list)
839{
840 clearError();
841
Jesse Hallb29e5e82012-04-04 16:53:42 -0700842 egl_connection_t* cnx = NULL;
843 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600844 EGLint colorSpace;
845 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700846 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600847 // now select a corresponding sRGB format if needed
848 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
849 ALOGE("error invalid colorspace: %d", colorSpace);
850 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
851 }
852
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800854 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700855 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600856 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700857 return s;
858 }
859 }
860 return EGL_NO_SURFACE;
861}
862
863EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
864 const EGLint *attrib_list)
865{
866 clearError();
867
Jesse Hallb29e5e82012-04-04 16:53:42 -0700868 egl_connection_t* cnx = NULL;
869 egl_display_ptr dp = validate_display_connection(dpy, cnx);
870 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600871 EGLDisplay iDpy = dp->disp.dpy;
872 EGLint format;
873 getNativePixelFormat(iDpy, cnx, config, format);
874
875 // now select correct colorspace and dataspace based on user's attribute list
876 EGLint colorSpace;
877 android_dataspace dataSpace;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600878 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
879 ALOGE("error invalid colorspace: %d", colorSpace);
880 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
881 }
882
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600883 // Pbuffers are not displayed so we don't need to store the
884 // colorspace. We do need to filter out color spaces the
885 // driver doesn't know how to process.
886 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700887 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600888 // Had to modify the attribute list due to use of color space.
889 // Use modified list from here on.
890 attrib_list = strippedAttribList.data();
891 }
892
Mathias Agopian518ec112011-05-13 16:21:08 -0700893 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800894 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700895 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600896 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700897 return s;
898 }
899 }
900 return EGL_NO_SURFACE;
901}
Jesse Hall47743382013-02-08 11:13:46 -0800902
Mathias Agopian518ec112011-05-13 16:21:08 -0700903EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
904{
905 clearError();
906
Jesse Hallb29e5e82012-04-04 16:53:42 -0700907 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700908 if (!dp) return EGL_FALSE;
909
Jesse Hallb29e5e82012-04-04 16:53:42 -0700910 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700911 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800912 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700913
914 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800915 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700916 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700917 _s.terminate();
918 }
919 return result;
920}
921
922EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
923 EGLint attribute, EGLint *value)
924{
925 clearError();
926
Jesse Hallb29e5e82012-04-04 16:53:42 -0700927 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700928 if (!dp) return EGL_FALSE;
929
Jesse Hallb29e5e82012-04-04 16:53:42 -0700930 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700931 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800932 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700933
Mathias Agopian518ec112011-05-13 16:21:08 -0700934 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700935 if (s->getColorSpaceAttribute(attribute, value)) {
936 return EGL_TRUE;
937 } else if (s->getSmpte2086Attribute(attribute, value)) {
938 return EGL_TRUE;
939 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600940 return EGL_TRUE;
941 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700942 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700943}
944
Jamie Gennise8696a42012-01-15 18:54:57 -0800945void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800946 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800947 clearError();
948
Jesse Hallb29e5e82012-04-04 16:53:42 -0700949 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800950 if (!dp) {
951 return;
952 }
953
Jesse Hallb29e5e82012-04-04 16:53:42 -0700954 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800955 if (!_s.get()) {
956 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800957 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800958}
959
Mathias Agopian518ec112011-05-13 16:21:08 -0700960// ----------------------------------------------------------------------------
961// Contexts
962// ----------------------------------------------------------------------------
963
964EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
965 EGLContext share_list, const EGLint *attrib_list)
966{
967 clearError();
968
Jesse Hallb29e5e82012-04-04 16:53:42 -0700969 egl_connection_t* cnx = NULL;
970 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700971 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700972 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700973 if (!ContextRef(dp.get(), share_list).get()) {
974 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
975 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700976 egl_context_t* const c = get_context(share_list);
977 share_list = c->context;
978 }
979 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800980 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700981 if (context != EGL_NO_CONTEXT) {
982 // figure out if it's a GLESv1 or GLESv2
983 int version = 0;
984 if (attrib_list) {
985 while (*attrib_list != EGL_NONE) {
986 GLint attr = *attrib_list++;
987 GLint value = *attrib_list++;
988 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
989 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800990 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800991 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800992 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700993 }
994 }
995 };
996 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700997 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
998 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700999 return c;
1000 }
1001 }
1002 return EGL_NO_CONTEXT;
1003}
1004
1005EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1006{
1007 clearError();
1008
Jesse Hallb29e5e82012-04-04 16:53:42 -07001009 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001010 if (!dp)
1011 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001012
Jesse Hallb29e5e82012-04-04 16:53:42 -07001013 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001014 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001015 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -08001016
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -08001018 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 if (result == EGL_TRUE) {
1020 _c.terminate();
1021 }
1022 return result;
1023}
1024
Mathias Agopian518ec112011-05-13 16:21:08 -07001025EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1026 EGLSurface read, EGLContext ctx)
1027{
1028 clearError();
1029
Jesse Hallb29e5e82012-04-04 16:53:42 -07001030 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -08001031 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001032
Mathias Agopian5b287a62011-05-16 18:58:55 -07001033 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
1034 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
1035 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -07001036 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
1037 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001038 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001039 }
1040
1041 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -07001042 ContextRef _c(dp.get(), ctx);
1043 SurfaceRef _d(dp.get(), draw);
1044 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001045
1046 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -07001047 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001048 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -08001049 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001050 }
1051
1052 // these are the underlying implementation's object
1053 EGLContext impl_ctx = EGL_NO_CONTEXT;
1054 EGLSurface impl_draw = EGL_NO_SURFACE;
1055 EGLSurface impl_read = EGL_NO_SURFACE;
1056
1057 // these are our objects structs passed in
1058 egl_context_t * c = NULL;
1059 egl_surface_t const * d = NULL;
1060 egl_surface_t const * r = NULL;
1061
1062 // these are the current objects structs
1063 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001064
Mathias Agopian518ec112011-05-13 16:21:08 -07001065 if (ctx != EGL_NO_CONTEXT) {
1066 c = get_context(ctx);
1067 impl_ctx = c->context;
1068 } else {
1069 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001070 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1071 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001072 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001073 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001074 if (cur_c == NULL) {
1075 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001076 // not an error, there is just no current context.
1077 return EGL_TRUE;
1078 }
1079 }
1080
1081 // retrieve the underlying implementation's draw EGLSurface
1082 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001083 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001084 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001085 impl_draw = d->surface;
1086 }
1087
1088 // retrieve the underlying implementation's read EGLSurface
1089 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001090 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001091 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001092 impl_read = r->surface;
1093 }
1094
Mathias Agopian518ec112011-05-13 16:21:08 -07001095
Jesse Hallb29e5e82012-04-04 16:53:42 -07001096 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001097 draw, read, ctx,
1098 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001099
1100 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001101 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001102 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1103 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001104 _c.acquire();
1105 _r.acquire();
1106 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001107 } else {
1108 setGLHooksThreadSpecific(&gHooksNoContext);
1109 egl_tls_t::setContext(EGL_NO_CONTEXT);
1110 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001111 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +00001112 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001113 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001114 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001115 }
1116 return result;
1117}
1118
1119
1120EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1121 EGLint attribute, EGLint *value)
1122{
1123 clearError();
1124
Jesse Hallb29e5e82012-04-04 16:53:42 -07001125 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001126 if (!dp) return EGL_FALSE;
1127
Jesse Hallb29e5e82012-04-04 16:53:42 -07001128 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001129 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001130
Mathias Agopian518ec112011-05-13 16:21:08 -07001131 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001132 return c->cnx->egl.eglQueryContext(
1133 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001134
Mathias Agopian518ec112011-05-13 16:21:08 -07001135}
1136
1137EGLContext eglGetCurrentContext(void)
1138{
1139 // could be called before eglInitialize(), but we wouldn't have a context
1140 // then, and this function would correctly return EGL_NO_CONTEXT.
1141
1142 clearError();
1143
1144 EGLContext ctx = getContext();
1145 return ctx;
1146}
1147
1148EGLSurface eglGetCurrentSurface(EGLint readdraw)
1149{
1150 // could be called before eglInitialize(), but we wouldn't have a context
1151 // then, and this function would correctly return EGL_NO_SURFACE.
1152
1153 clearError();
1154
1155 EGLContext ctx = getContext();
1156 if (ctx) {
1157 egl_context_t const * const c = get_context(ctx);
1158 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1159 switch (readdraw) {
1160 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001161 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001162 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1163 }
1164 }
1165 return EGL_NO_SURFACE;
1166}
1167
1168EGLDisplay eglGetCurrentDisplay(void)
1169{
1170 // could be called before eglInitialize(), but we wouldn't have a context
1171 // then, and this function would correctly return EGL_NO_DISPLAY.
1172
1173 clearError();
1174
1175 EGLContext ctx = getContext();
1176 if (ctx) {
1177 egl_context_t const * const c = get_context(ctx);
1178 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1179 return c->dpy;
1180 }
1181 return EGL_NO_DISPLAY;
1182}
1183
1184EGLBoolean eglWaitGL(void)
1185{
Mathias Agopian518ec112011-05-13 16:21:08 -07001186 clearError();
1187
Mathias Agopianada798b2012-02-13 17:09:30 -08001188 egl_connection_t* const cnx = &gEGLImpl;
1189 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001190 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001191
1192 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001193}
1194
1195EGLBoolean eglWaitNative(EGLint engine)
1196{
Mathias Agopian518ec112011-05-13 16:21:08 -07001197 clearError();
1198
Mathias Agopianada798b2012-02-13 17:09:30 -08001199 egl_connection_t* const cnx = &gEGLImpl;
1200 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001201 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001202
1203 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001204}
1205
1206EGLint eglGetError(void)
1207{
Mathias Agopianada798b2012-02-13 17:09:30 -08001208 EGLint err = EGL_SUCCESS;
1209 egl_connection_t* const cnx = &gEGLImpl;
1210 if (cnx->dso) {
1211 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001212 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001213 if (err == EGL_SUCCESS) {
1214 err = egl_tls_t::getError();
1215 }
1216 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001217}
1218
Michael Chockc0ec5e22014-01-27 08:14:33 -08001219static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001220 const char* procname) {
1221 const egl_connection_t* cnx = &gEGLImpl;
1222 void* proc = NULL;
1223
Michael Chockc0ec5e22014-01-27 08:14:33 -08001224 proc = dlsym(cnx->libEgl, procname);
1225 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1226
Jesse Hallc07b5202013-07-04 12:08:16 -07001227 proc = dlsym(cnx->libGles2, procname);
1228 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1229
1230 proc = dlsym(cnx->libGles1, procname);
1231 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1232
1233 return NULL;
1234}
1235
Mathias Agopian518ec112011-05-13 16:21:08 -07001236__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1237{
1238 // eglGetProcAddress() could be the very first function called
1239 // in which case we must make sure we've initialized ourselves, this
1240 // happens the first time egl_get_display() is called.
1241
1242 clearError();
1243
1244 if (egl_init_drivers() == EGL_FALSE) {
1245 setError(EGL_BAD_PARAMETER, NULL);
1246 return NULL;
1247 }
1248
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001249 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001250 return NULL;
1251 }
1252
Mathias Agopian518ec112011-05-13 16:21:08 -07001253 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001254 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001255 if (addr) return addr;
1256
Michael Chockc0ec5e22014-01-27 08:14:33 -08001257 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001258 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001259
Mathias Agopian518ec112011-05-13 16:21:08 -07001260 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1261 pthread_mutex_lock(&sExtensionMapMutex);
1262
1263 /*
1264 * Since eglGetProcAddress() is not associated to anything, it needs
1265 * to return a function pointer that "works" regardless of what
1266 * the current context is.
1267 *
1268 * For this reason, we return a "forwarder", a small stub that takes
1269 * care of calling the function associated with the context
1270 * currently bound.
1271 *
1272 * We first look for extensions we've already resolved, if we're seeing
1273 * this extension for the first time, we go through all our
1274 * implementations and call eglGetProcAddress() and record the
1275 * result in the appropriate implementation hooks and return the
1276 * address of the forwarder corresponding to that hook set.
1277 *
1278 */
1279
Mathias Agopian65421432017-03-08 11:49:05 -08001280 const std::string name(procname);
1281
1282 auto& extentionMap = sGLExtentionMap;
1283 auto pos = extentionMap.find(name);
1284 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001285 const int slot = sGLExtentionSlot;
1286
Steve Blocke6f43dd2012-01-06 19:20:56 +00001287 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001288 "no more slots for eglGetProcAddress(\"%s\")",
1289 procname);
1290
1291 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1292 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001293
1294 egl_connection_t* const cnx = &gEGLImpl;
1295 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001296 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001297 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001298 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1299 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001300 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001301 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001302 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001303
Mathias Agopian518ec112011-05-13 16:21:08 -07001304 if (found) {
1305 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001306 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001307 sGLExtentionSlot++;
1308 }
1309 }
1310
1311 pthread_mutex_unlock(&sExtensionMapMutex);
1312 return addr;
1313}
1314
Mathias Agopian65421432017-03-08 11:49:05 -08001315class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001316public:
1317
1318 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001319 static FrameCompletionThread thread;
1320
1321 char name[64];
1322
1323 std::lock_guard<std::mutex> lock(thread.mMutex);
1324 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1325 ATRACE_NAME(name);
1326
1327 thread.mQueue.push_back(sync);
1328 thread.mCondition.notify_one();
1329 thread.mFramesQueued++;
1330 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001331 }
1332
1333private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001334
Mathias Agopian65421432017-03-08 11:49:05 -08001335 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1336 std::thread thread(&FrameCompletionThread::loop, this);
1337 thread.detach();
1338 }
1339
1340#pragma clang diagnostic push
1341#pragma clang diagnostic ignored "-Wmissing-noreturn"
1342 void loop() {
1343 while (true) {
1344 threadLoop();
1345 }
1346 }
1347#pragma clang diagnostic pop
1348
1349 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001350 EGLSyncKHR sync;
1351 uint32_t frameNum;
1352 {
Mathias Agopian65421432017-03-08 11:49:05 -08001353 std::unique_lock<std::mutex> lock(mMutex);
1354 while (mQueue.empty()) {
1355 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001356 }
1357 sync = mQueue[0];
1358 frameNum = mFramesCompleted;
1359 }
1360 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1361 {
Mathias Agopian65421432017-03-08 11:49:05 -08001362 char name[64];
1363 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1364 ATRACE_NAME(name);
1365
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001366 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1367 if (result == EGL_FALSE) {
1368 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1369 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1370 ALOGE("FrameCompletion: timeout waiting for fence");
1371 }
1372 eglDestroySyncKHR(dpy, sync);
1373 }
1374 {
Mathias Agopian65421432017-03-08 11:49:05 -08001375 std::lock_guard<std::mutex> lock(mMutex);
1376 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001377 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001378 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001379 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001380 }
1381
1382 uint32_t mFramesQueued;
1383 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001384 std::deque<EGLSyncKHR> mQueue;
1385 std::condition_variable mCondition;
1386 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001387};
1388
Dan Stozaa894d082015-02-19 15:27:36 -08001389EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1390 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001391{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001392 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001393 clearError();
1394
Jesse Hallb29e5e82012-04-04 16:53:42 -07001395 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001396 if (!dp) return EGL_FALSE;
1397
Jesse Hallb29e5e82012-04-04 16:53:42 -07001398 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001399 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001400 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001401
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001402 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001403
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001404 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1405 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1406 if (sync != EGL_NO_SYNC_KHR) {
1407 FrameCompletionThread::queueSync(sync);
1408 }
1409 }
1410
Mathias Agopian7db993a2012-03-25 00:49:46 -07001411 if (CC_UNLIKELY(dp->finishOnSwap)) {
1412 uint32_t pixel;
1413 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1414 if (c) {
1415 // glReadPixels() ensures that the frame is complete
1416 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1417 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1418 }
1419 }
1420
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001421 if (!sendSurfaceMetadata(s)) {
1422 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1423 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1424 }
1425
Dan Stozaa894d082015-02-19 15:27:36 -08001426 if (n_rects == 0) {
1427 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1428 }
1429
Mathias Agopian65421432017-03-08 11:49:05 -08001430 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001431 for (int r = 0; r < n_rects; ++r) {
1432 int offset = r * 4;
1433 int x = rects[offset];
1434 int y = rects[offset + 1];
1435 int width = rects[offset + 2];
1436 int height = rects[offset + 3];
1437 android_native_rect_t androidRect;
1438 androidRect.left = x;
1439 androidRect.top = y + height;
1440 androidRect.right = x + width;
1441 androidRect.bottom = y;
1442 androidRects.push_back(androidRect);
1443 }
Mathias Agopian65421432017-03-08 11:49:05 -08001444 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001445
1446 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1447 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1448 rects, n_rects);
1449 } else {
1450 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1451 }
1452}
1453
1454EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1455{
1456 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001457}
1458
1459EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1460 NativePixmapType target)
1461{
1462 clearError();
1463
Jesse Hallb29e5e82012-04-04 16:53:42 -07001464 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001465 if (!dp) return EGL_FALSE;
1466
Jesse Hallb29e5e82012-04-04 16:53:42 -07001467 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001468 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001469 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001470
Mathias Agopian518ec112011-05-13 16:21:08 -07001471 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001472 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001473}
1474
1475const char* eglQueryString(EGLDisplay dpy, EGLint name)
1476{
1477 clearError();
1478
Chia-I Wue57d1352016-08-15 16:10:02 +08001479 // Generate an error quietly when client extensions (as defined by
1480 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1481 // validate_display to generate the error as validate_display would log
1482 // the error, which can be misleading.
1483 //
1484 // If we want to support EGL_EXT_client_extensions later, we can return
1485 // the client extension string here instead.
1486 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001487 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001488
Jesse Hallb29e5e82012-04-04 16:53:42 -07001489 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001490 if (!dp) return (const char *) NULL;
1491
1492 switch (name) {
1493 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001494 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001495 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001496 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001497 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001498 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001499 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001500 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001501 default:
1502 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 }
1504 return setError(EGL_BAD_PARAMETER, (const char *)0);
1505}
1506
Jiyong Park00b15b82017-08-10 20:30:56 +09001507extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001508{
1509 clearError();
1510
1511 const egl_display_ptr dp = validate_display(dpy);
1512 if (!dp) return (const char *) NULL;
1513
1514 switch (name) {
1515 case EGL_VENDOR:
1516 return dp->disp.queryString.vendor;
1517 case EGL_VERSION:
1518 return dp->disp.queryString.version;
1519 case EGL_EXTENSIONS:
1520 return dp->disp.queryString.extensions;
1521 case EGL_CLIENT_APIS:
1522 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001523 default:
1524 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001525 }
1526 return setError(EGL_BAD_PARAMETER, (const char *)0);
1527}
Mathias Agopian518ec112011-05-13 16:21:08 -07001528
1529// ----------------------------------------------------------------------------
1530// EGL 1.1
1531// ----------------------------------------------------------------------------
1532
1533EGLBoolean eglSurfaceAttrib(
1534 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1535{
1536 clearError();
1537
Jesse Hallb29e5e82012-04-04 16:53:42 -07001538 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001539 if (!dp) return EGL_FALSE;
1540
Jesse Hallb29e5e82012-04-04 16:53:42 -07001541 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001542 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001543 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001544
Pablo Ceballosc18be292016-05-31 14:55:42 -07001545 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001546
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001547 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001548 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001549 setError(EGL_BAD_SURFACE, EGL_FALSE);
1550 }
Mathias Agopian65421432017-03-08 11:49:05 -08001551 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1552 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001553 }
1554
Pablo Ceballosc18be292016-05-31 14:55:42 -07001555 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001556 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001557 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001558 }
Mathias Agopian65421432017-03-08 11:49:05 -08001559 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1560 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001561 }
1562
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001563 if (s->setSmpte2086Attribute(attribute, value)) {
1564 return EGL_TRUE;
1565 } else if (s->setCta8613Attribute(attribute, value)) {
1566 return EGL_TRUE;
1567 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001568 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001569 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001570 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001571 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001572}
1573
1574EGLBoolean eglBindTexImage(
1575 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1576{
1577 clearError();
1578
Jesse Hallb29e5e82012-04-04 16:53:42 -07001579 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001580 if (!dp) return EGL_FALSE;
1581
Jesse Hallb29e5e82012-04-04 16:53:42 -07001582 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001583 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001584 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001585
Mathias Agopian518ec112011-05-13 16:21:08 -07001586 egl_surface_t const * const s = get_surface(surface);
1587 if (s->cnx->egl.eglBindTexImage) {
1588 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001589 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001590 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001591 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001592}
1593
1594EGLBoolean eglReleaseTexImage(
1595 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1596{
1597 clearError();
1598
Jesse Hallb29e5e82012-04-04 16:53:42 -07001599 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001600 if (!dp) return EGL_FALSE;
1601
Jesse Hallb29e5e82012-04-04 16:53:42 -07001602 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001603 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001604 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001605
Mathias Agopian518ec112011-05-13 16:21:08 -07001606 egl_surface_t const * const s = get_surface(surface);
1607 if (s->cnx->egl.eglReleaseTexImage) {
1608 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001609 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001610 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001611 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001612}
1613
1614EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1615{
1616 clearError();
1617
Jesse Hallb29e5e82012-04-04 16:53:42 -07001618 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001619 if (!dp) return EGL_FALSE;
1620
1621 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001622 egl_connection_t* const cnx = &gEGLImpl;
1623 if (cnx->dso && cnx->egl.eglSwapInterval) {
1624 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001625 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001626
Mathias Agopian518ec112011-05-13 16:21:08 -07001627 return res;
1628}
1629
1630
1631// ----------------------------------------------------------------------------
1632// EGL 1.2
1633// ----------------------------------------------------------------------------
1634
1635EGLBoolean eglWaitClient(void)
1636{
1637 clearError();
1638
Mathias Agopianada798b2012-02-13 17:09:30 -08001639 egl_connection_t* const cnx = &gEGLImpl;
1640 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001641 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001642
1643 EGLBoolean res;
1644 if (cnx->egl.eglWaitClient) {
1645 res = cnx->egl.eglWaitClient();
1646 } else {
1647 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001648 }
1649 return res;
1650}
1651
1652EGLBoolean eglBindAPI(EGLenum api)
1653{
1654 clearError();
1655
1656 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001657 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001658 }
1659
1660 // bind this API on all EGLs
1661 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001662 egl_connection_t* const cnx = &gEGLImpl;
1663 if (cnx->dso && cnx->egl.eglBindAPI) {
1664 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001665 }
1666 return res;
1667}
1668
1669EGLenum eglQueryAPI(void)
1670{
1671 clearError();
1672
1673 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001674 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001675 }
1676
Mathias Agopianada798b2012-02-13 17:09:30 -08001677 egl_connection_t* const cnx = &gEGLImpl;
1678 if (cnx->dso && cnx->egl.eglQueryAPI) {
1679 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001680 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001681
Mathias Agopian518ec112011-05-13 16:21:08 -07001682 // or, it can only be OpenGL ES
1683 return EGL_OPENGL_ES_API;
1684}
1685
1686EGLBoolean eglReleaseThread(void)
1687{
1688 clearError();
1689
Mathias Agopianada798b2012-02-13 17:09:30 -08001690 egl_connection_t* const cnx = &gEGLImpl;
1691 if (cnx->dso && cnx->egl.eglReleaseThread) {
1692 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001693 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301694
1695 // If there is context bound to the thread, release it
1696 egl_display_t::loseCurrent(get_context(getContext()));
1697
Mathias Agopian518ec112011-05-13 16:21:08 -07001698 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001699 return EGL_TRUE;
1700}
1701
1702EGLSurface eglCreatePbufferFromClientBuffer(
1703 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1704 EGLConfig config, const EGLint *attrib_list)
1705{
1706 clearError();
1707
Jesse Hallb29e5e82012-04-04 16:53:42 -07001708 egl_connection_t* cnx = NULL;
1709 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1710 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001711 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1712 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001713 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001714 }
1715 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1716}
1717
1718// ----------------------------------------------------------------------------
1719// EGL_EGLEXT_VERSION 3
1720// ----------------------------------------------------------------------------
1721
1722EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1723 const EGLint *attrib_list)
1724{
1725 clearError();
1726
Jesse Hallb29e5e82012-04-04 16:53:42 -07001727 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001728 if (!dp) return EGL_FALSE;
1729
Jesse Hallb29e5e82012-04-04 16:53:42 -07001730 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001731 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001732 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001733
1734 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001735 if (s->cnx->egl.eglLockSurfaceKHR) {
1736 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001737 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001738 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001739 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001740}
1741
1742EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1743{
1744 clearError();
1745
Jesse Hallb29e5e82012-04-04 16:53:42 -07001746 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001747 if (!dp) return EGL_FALSE;
1748
Jesse Hallb29e5e82012-04-04 16:53:42 -07001749 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001750 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001751 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001752
1753 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001754 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001755 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001756 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001757 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001758}
1759
1760EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1761 EGLClientBuffer buffer, const EGLint *attrib_list)
1762{
1763 clearError();
1764
Jesse Hallb29e5e82012-04-04 16:53:42 -07001765 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001766 if (!dp) return EGL_NO_IMAGE_KHR;
1767
Jesse Hallb29e5e82012-04-04 16:53:42 -07001768 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001769 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001770
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001771 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1772 egl_connection_t* const cnx = &gEGLImpl;
1773 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1774 result = cnx->egl.eglCreateImageKHR(
1775 dp->disp.dpy,
1776 c ? c->context : EGL_NO_CONTEXT,
1777 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001778 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001779 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001780}
1781
1782EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1783{
1784 clearError();
1785
Jesse Hallb29e5e82012-04-04 16:53:42 -07001786 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001787 if (!dp) return EGL_FALSE;
1788
Steven Holte646a5c52012-06-04 20:02:11 -07001789 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001790 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001791 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001792 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001793 }
Steven Holte646a5c52012-06-04 20:02:11 -07001794 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001795}
1796
1797// ----------------------------------------------------------------------------
1798// EGL_EGLEXT_VERSION 5
1799// ----------------------------------------------------------------------------
1800
1801
1802EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1803{
1804 clearError();
1805
Jesse Hallb29e5e82012-04-04 16:53:42 -07001806 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001807 if (!dp) return EGL_NO_SYNC_KHR;
1808
Mathias Agopian518ec112011-05-13 16:21:08 -07001809 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001810 egl_connection_t* const cnx = &gEGLImpl;
1811 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1812 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001813 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001814 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001815}
1816
1817EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1818{
1819 clearError();
1820
Jesse Hallb29e5e82012-04-04 16:53:42 -07001821 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001822 if (!dp) return EGL_FALSE;
1823
Mathias Agopian518ec112011-05-13 16:21:08 -07001824 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001825 egl_connection_t* const cnx = &gEGLImpl;
1826 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1827 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001828 }
1829 return result;
1830}
1831
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001832EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1833 clearError();
1834
1835 const egl_display_ptr dp = validate_display(dpy);
1836 if (!dp) return EGL_FALSE;
1837
1838 EGLBoolean result = EGL_FALSE;
1839 egl_connection_t* const cnx = &gEGLImpl;
1840 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1841 result = cnx->egl.eglSignalSyncKHR(
1842 dp->disp.dpy, sync, mode);
1843 }
1844 return result;
1845}
1846
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001847EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1848 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001849{
1850 clearError();
1851
Jesse Hallb29e5e82012-04-04 16:53:42 -07001852 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001853 if (!dp) return EGL_FALSE;
1854
Mathias Agopian737b8962017-02-24 14:32:05 -08001855 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001856 egl_connection_t* const cnx = &gEGLImpl;
1857 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1858 result = cnx->egl.eglClientWaitSyncKHR(
1859 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001860 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001861 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001862}
1863
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001864EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1865 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001866{
1867 clearError();
1868
Jesse Hallb29e5e82012-04-04 16:53:42 -07001869 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001870 if (!dp) return EGL_FALSE;
1871
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001872 EGLBoolean result = EGL_FALSE;
1873 egl_connection_t* const cnx = &gEGLImpl;
1874 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1875 result = cnx->egl.eglGetSyncAttribKHR(
1876 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001877 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001878 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001879}
1880
Season Li000d88f2015-07-01 11:39:40 -07001881EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1882{
1883 clearError();
1884
1885 const egl_display_ptr dp = validate_display(dpy);
1886 if (!dp) return EGL_NO_STREAM_KHR;
1887
1888 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1889 egl_connection_t* const cnx = &gEGLImpl;
1890 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1891 result = cnx->egl.eglCreateStreamKHR(
1892 dp->disp.dpy, attrib_list);
1893 }
1894 return result;
1895}
1896
1897EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1898{
1899 clearError();
1900
1901 const egl_display_ptr dp = validate_display(dpy);
1902 if (!dp) return EGL_FALSE;
1903
1904 EGLBoolean result = EGL_FALSE;
1905 egl_connection_t* const cnx = &gEGLImpl;
1906 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1907 result = cnx->egl.eglDestroyStreamKHR(
1908 dp->disp.dpy, stream);
1909 }
1910 return result;
1911}
1912
1913EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1914 EGLenum attribute, EGLint value)
1915{
1916 clearError();
1917
1918 const egl_display_ptr dp = validate_display(dpy);
1919 if (!dp) return EGL_FALSE;
1920
1921 EGLBoolean result = EGL_FALSE;
1922 egl_connection_t* const cnx = &gEGLImpl;
1923 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1924 result = cnx->egl.eglStreamAttribKHR(
1925 dp->disp.dpy, stream, attribute, value);
1926 }
1927 return result;
1928}
1929
1930EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1931 EGLenum attribute, EGLint *value)
1932{
1933 clearError();
1934
1935 const egl_display_ptr dp = validate_display(dpy);
1936 if (!dp) return EGL_FALSE;
1937
1938 EGLBoolean result = EGL_FALSE;
1939 egl_connection_t* const cnx = &gEGLImpl;
1940 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1941 result = cnx->egl.eglQueryStreamKHR(
1942 dp->disp.dpy, stream, attribute, value);
1943 }
1944 return result;
1945}
1946
1947EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1948 EGLenum attribute, EGLuint64KHR *value)
1949{
1950 clearError();
1951
1952 const egl_display_ptr dp = validate_display(dpy);
1953 if (!dp) return EGL_FALSE;
1954
1955 EGLBoolean result = EGL_FALSE;
1956 egl_connection_t* const cnx = &gEGLImpl;
1957 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1958 result = cnx->egl.eglQueryStreamu64KHR(
1959 dp->disp.dpy, stream, attribute, value);
1960 }
1961 return result;
1962}
1963
1964EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1965 EGLenum attribute, EGLTimeKHR *value)
1966{
1967 clearError();
1968
1969 const egl_display_ptr dp = validate_display(dpy);
1970 if (!dp) return EGL_FALSE;
1971
1972 EGLBoolean result = EGL_FALSE;
1973 egl_connection_t* const cnx = &gEGLImpl;
1974 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1975 result = cnx->egl.eglQueryStreamTimeKHR(
1976 dp->disp.dpy, stream, attribute, value);
1977 }
1978 return result;
1979}
1980
1981EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1982 EGLStreamKHR stream, const EGLint *attrib_list)
1983{
1984 clearError();
1985
1986 egl_display_ptr dp = validate_display(dpy);
1987 if (!dp) return EGL_NO_SURFACE;
1988
1989 egl_connection_t* const cnx = &gEGLImpl;
1990 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1991 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1992 dp->disp.dpy, config, stream, attrib_list);
1993 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07001994 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
1995 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001996 return s;
1997 }
1998 }
1999 return EGL_NO_SURFACE;
2000}
2001
2002EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
2003 EGLStreamKHR stream)
2004{
2005 clearError();
2006
2007 const egl_display_ptr dp = validate_display(dpy);
2008 if (!dp) return EGL_FALSE;
2009
2010 EGLBoolean result = EGL_FALSE;
2011 egl_connection_t* const cnx = &gEGLImpl;
2012 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
2013 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
2014 dp->disp.dpy, stream);
2015 }
2016 return result;
2017}
2018
2019EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
2020 EGLStreamKHR stream)
2021{
2022 clearError();
2023
2024 const egl_display_ptr dp = validate_display(dpy);
2025 if (!dp) return EGL_FALSE;
2026
2027 EGLBoolean result = EGL_FALSE;
2028 egl_connection_t* const cnx = &gEGLImpl;
2029 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
2030 result = cnx->egl.eglStreamConsumerAcquireKHR(
2031 dp->disp.dpy, stream);
2032 }
2033 return result;
2034}
2035
2036EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2037 EGLStreamKHR stream)
2038{
2039 clearError();
2040
2041 const egl_display_ptr dp = validate_display(dpy);
2042 if (!dp) return EGL_FALSE;
2043
2044 EGLBoolean result = EGL_FALSE;
2045 egl_connection_t* const cnx = &gEGLImpl;
2046 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2047 result = cnx->egl.eglStreamConsumerReleaseKHR(
2048 dp->disp.dpy, stream);
2049 }
2050 return result;
2051}
2052
2053EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2054 EGLDisplay dpy, EGLStreamKHR stream)
2055{
2056 clearError();
2057
2058 const egl_display_ptr dp = validate_display(dpy);
2059 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2060
2061 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2062 egl_connection_t* const cnx = &gEGLImpl;
2063 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2064 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2065 dp->disp.dpy, stream);
2066 }
2067 return result;
2068}
2069
2070EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2071 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2072{
2073 clearError();
2074
2075 const egl_display_ptr dp = validate_display(dpy);
2076 if (!dp) return EGL_NO_STREAM_KHR;
2077
2078 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2079 egl_connection_t* const cnx = &gEGLImpl;
2080 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2081 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2082 dp->disp.dpy, file_descriptor);
2083 }
2084 return result;
2085}
2086
Mathias Agopian518ec112011-05-13 16:21:08 -07002087// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002088// EGL_EGLEXT_VERSION 15
2089// ----------------------------------------------------------------------------
2090
2091EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2092 clearError();
2093 const egl_display_ptr dp = validate_display(dpy);
2094 if (!dp) return EGL_FALSE;
2095 EGLint result = EGL_FALSE;
2096 egl_connection_t* const cnx = &gEGLImpl;
2097 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2098 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2099 }
2100 return result;
2101}
2102
2103// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002104// ANDROID extensions
2105// ----------------------------------------------------------------------------
2106
Jamie Gennis331841b2012-09-06 14:52:00 -07002107EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2108{
2109 clearError();
2110
2111 const egl_display_ptr dp = validate_display(dpy);
2112 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2113
2114 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2115 egl_connection_t* const cnx = &gEGLImpl;
2116 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2117 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2118 }
2119 return result;
2120}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002121
Andy McFadden72841452013-03-01 16:25:32 -08002122EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2123 EGLnsecsANDROID time)
2124{
2125 clearError();
2126
2127 const egl_display_ptr dp = validate_display(dpy);
2128 if (!dp) {
2129 return EGL_FALSE;
2130 }
2131
2132 SurfaceRef _s(dp.get(), surface);
2133 if (!_s.get()) {
2134 setError(EGL_BAD_SURFACE, EGL_FALSE);
2135 return EGL_FALSE;
2136 }
2137
2138 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002139 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002140
2141 return EGL_TRUE;
2142}
2143
Craig Donner60761072017-01-27 12:30:44 -08002144EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2145 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002146 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2147 // this function cannot be implemented when this libEGL is built for
2148 // vendors.
2149#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002150 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002151 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002152#else
2153 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2154#endif
Craig Donner60761072017-01-27 12:30:44 -08002155}
2156
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002157// ----------------------------------------------------------------------------
2158// NVIDIA extensions
2159// ----------------------------------------------------------------------------
2160EGLuint64NV eglGetSystemTimeFrequencyNV()
2161{
2162 clearError();
2163
2164 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002165 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002166 }
2167
2168 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002169 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002170
Mathias Agopianada798b2012-02-13 17:09:30 -08002171 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2172 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002173 }
2174
Mathias Agopian737b8962017-02-24 14:32:05 -08002175 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002176}
2177
2178EGLuint64NV eglGetSystemTimeNV()
2179{
2180 clearError();
2181
2182 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002183 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002184 }
2185
2186 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002187 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002188
Mathias Agopianada798b2012-02-13 17:09:30 -08002189 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2190 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002191 }
2192
Mathias Agopian737b8962017-02-24 14:32:05 -08002193 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002194}
Dan Stozaa894d082015-02-19 15:27:36 -08002195
2196// ----------------------------------------------------------------------------
2197// Partial update extension
2198// ----------------------------------------------------------------------------
2199EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2200 EGLint *rects, EGLint n_rects)
2201{
2202 clearError();
2203
2204 const egl_display_ptr dp = validate_display(dpy);
2205 if (!dp) {
2206 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2207 return EGL_FALSE;
2208 }
2209
2210 SurfaceRef _s(dp.get(), surface);
2211 if (!_s.get()) {
2212 setError(EGL_BAD_SURFACE, EGL_FALSE);
2213 return EGL_FALSE;
2214 }
2215
2216 egl_surface_t const * const s = get_surface(surface);
2217 if (s->cnx->egl.eglSetDamageRegionKHR) {
2218 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2219 rects, n_rects);
2220 }
2221
2222 return EGL_FALSE;
2223}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002224
Brian Anderson1049d1d2016-12-16 17:25:57 -08002225EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2226 EGLuint64KHR *frameId) {
2227 clearError();
2228
2229 const egl_display_ptr dp = validate_display(dpy);
2230 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002231 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002232 }
2233
2234 SurfaceRef _s(dp.get(), surface);
2235 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002236 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002237 }
2238
2239 egl_surface_t const * const s = get_surface(surface);
2240
Mathias Agopian65421432017-03-08 11:49:05 -08002241 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002242 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002243 }
2244
2245 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002246 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002247
Mathias Agopian65421432017-03-08 11:49:05 -08002248 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002249 // This should not happen. Return an error that is not in the spec
2250 // so it's obvious something is very wrong.
2251 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002252 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002253 }
2254
2255 *frameId = nextFrameId;
2256 return EGL_TRUE;
2257}
2258
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002259EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2260 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2261{
2262 clearError();
2263
2264 const egl_display_ptr dp = validate_display(dpy);
2265 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002266 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002267 }
2268
2269 SurfaceRef _s(dp.get(), surface);
2270 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002271 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002272 }
2273
2274 egl_surface_t const * const s = get_surface(surface);
2275
Mathias Agopian65421432017-03-08 11:49:05 -08002276 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002277 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002278 }
2279
2280 nsecs_t* compositeDeadline = nullptr;
2281 nsecs_t* compositeInterval = nullptr;
2282 nsecs_t* compositeToPresentLatency = nullptr;
2283
2284 for (int i = 0; i < numTimestamps; i++) {
2285 switch (names[i]) {
2286 case EGL_COMPOSITE_DEADLINE_ANDROID:
2287 compositeDeadline = &values[i];
2288 break;
2289 case EGL_COMPOSITE_INTERVAL_ANDROID:
2290 compositeInterval = &values[i];
2291 break;
2292 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2293 compositeToPresentLatency = &values[i];
2294 break;
2295 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002296 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002297 }
2298 }
2299
Mathias Agopian65421432017-03-08 11:49:05 -08002300 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002301 compositeDeadline, compositeInterval, compositeToPresentLatency);
2302
2303 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002304 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002305 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002306 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002307 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002308 default:
2309 // This should not happen. Return an error that is not in the spec
2310 // so it's obvious something is very wrong.
2311 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002312 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002313 }
2314}
2315
2316EGLBoolean eglGetCompositorTimingSupportedANDROID(
2317 EGLDisplay dpy, EGLSurface surface, EGLint name)
2318{
2319 clearError();
2320
2321 const egl_display_ptr dp = validate_display(dpy);
2322 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002323 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002324 }
2325
2326 SurfaceRef _s(dp.get(), surface);
2327 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002328 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002329 }
2330
2331 egl_surface_t const * const s = get_surface(surface);
2332
Mathias Agopian65421432017-03-08 11:49:05 -08002333 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002334 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002335 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002336 }
2337
2338 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002339 case EGL_COMPOSITE_DEADLINE_ANDROID:
2340 case EGL_COMPOSITE_INTERVAL_ANDROID:
2341 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2342 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002343 default:
2344 return EGL_FALSE;
2345 }
2346}
2347
Pablo Ceballosc18be292016-05-31 14:55:42 -07002348EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002349 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002350 EGLnsecsANDROID *values)
2351{
2352 clearError();
2353
2354 const egl_display_ptr dp = validate_display(dpy);
2355 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002356 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002357 }
2358
2359 SurfaceRef _s(dp.get(), surface);
2360 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002361 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002362 }
2363
2364 egl_surface_t const * const s = get_surface(surface);
2365
Mathias Agopian65421432017-03-08 11:49:05 -08002366 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002367 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002368 }
2369
Brian Andersondbd0ea82016-07-22 09:38:59 -07002370 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002371 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002372 nsecs_t* latchTime = nullptr;
2373 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002374 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002375 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002376 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002377 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002378 nsecs_t* releaseTime = nullptr;
2379
2380 for (int i = 0; i < numTimestamps; i++) {
2381 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002382 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2383 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002384 break;
2385 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2386 acquireTime = &values[i];
2387 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002388 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2389 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002390 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002391 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2392 firstRefreshStartTime = &values[i];
2393 break;
2394 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2395 lastRefreshStartTime = &values[i];
2396 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002397 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2398 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002399 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002400 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2401 displayPresentTime = &values[i];
2402 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002403 case EGL_DEQUEUE_READY_TIME_ANDROID:
2404 dequeueReadyTime = &values[i];
2405 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002406 case EGL_READS_DONE_TIME_ANDROID:
2407 releaseTime = &values[i];
2408 break;
2409 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002410 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002411 }
2412 }
2413
Mathias Agopian65421432017-03-08 11:49:05 -08002414 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002415 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002416 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002417 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002418
Brian Anderson069b3652016-07-22 10:32:47 -07002419 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002420 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002421 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002422 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002423 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002424 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002425 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002426 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002427 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2428 default:
2429 // This should not happen. Return an error that is not in the spec
2430 // so it's obvious something is very wrong.
2431 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2432 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002433 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002434}
2435
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002436EGLBoolean eglGetFrameTimestampSupportedANDROID(
2437 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002438{
2439 clearError();
2440
2441 const egl_display_ptr dp = validate_display(dpy);
2442 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002443 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002444 }
2445
2446 SurfaceRef _s(dp.get(), surface);
2447 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002448 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002449 }
2450
2451 egl_surface_t const * const s = get_surface(surface);
2452
Mathias Agopian65421432017-03-08 11:49:05 -08002453 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002454 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002455 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002456 }
2457
2458 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002459 case EGL_COMPOSITE_DEADLINE_ANDROID:
2460 case EGL_COMPOSITE_INTERVAL_ANDROID:
2461 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002462 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002463 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002464 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2465 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2466 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002467 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002468 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002469 case EGL_READS_DONE_TIME_ANDROID:
2470 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002471 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2472 int value = 0;
2473 window->query(window,
2474 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2475 return value == 0 ? EGL_FALSE : EGL_TRUE;
2476 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002477 default:
2478 return EGL_FALSE;
2479 }
2480}