blob: d9be9dbc67ac3f01d976077c16e532e0f55273d1 [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 "
Jesse Hall21558da2013-08-06 15:31:22 -070090 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080091
92char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070093 "EGL_KHR_image " // mandatory
94 "EGL_KHR_image_base " // mandatory
95 "EGL_KHR_image_pixmap "
96 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -070097 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070098 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070099 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700100 "EGL_KHR_gl_texture_cubemap_image "
101 "EGL_KHR_gl_renderbuffer_image "
102 "EGL_KHR_reusable_sync "
103 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700104 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700105 "EGL_KHR_config_attribs "
106 "EGL_KHR_surfaceless_context "
107 "EGL_KHR_stream "
108 "EGL_KHR_stream_fifo "
109 "EGL_KHR_stream_producer_eglsurface "
110 "EGL_KHR_stream_consumer_gltexture "
111 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700112 "EGL_EXT_create_context_robustness "
113 "EGL_NV_system_time "
114 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700115 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700116 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800117 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700118 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800119 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700120 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700121 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700122 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700123 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000124 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800125 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700126 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600127// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700128
129// extensions not exposed to applications but used by the ANDROID system
130// "EGL_ANDROID_blob_cache " // strongly recommended
131// "EGL_IMG_hibernate_process " // optional
132// "EGL_ANDROID_native_fence_sync " // strongly recommended
133// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700134// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700135
136/*
137 * EGL Extensions entry-points exposed to 3rd party applications
138 * (keep in sync with gExtensionString above)
139 *
140 */
141static const extention_map_t sExtensionMap[] = {
142 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700143 { "eglLockSurfaceKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
145 { "eglUnlockSurfaceKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700147
148 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700149 { "eglCreateImageKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
151 { "eglDestroyImageKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700153
154 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
155 { "eglCreateSyncKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
157 { "eglDestroySyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
159 { "eglClientWaitSyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
161 { "eglSignalSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
163 { "eglGetSyncAttribKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
165
166 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800167 { "eglGetSystemTimeFrequencyNV",
168 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
169 { "eglGetSystemTimeNV",
170 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700171
Mathias Agopian2bb71682013-03-27 17:32:41 -0700172 // EGL_KHR_wait_sync
173 { "eglWaitSyncKHR",
174 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700175
176 // EGL_ANDROID_presentation_time
177 { "eglPresentationTimeANDROID",
178 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800179
180 // EGL_KHR_swap_buffers_with_damage
181 { "eglSwapBuffersWithDamageKHR",
182 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
183
Craig Donner60761072017-01-27 12:30:44 -0800184 // EGL_ANDROID_get_native_client_buffer
185 { "eglGetNativeClientBufferANDROID",
186 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
187
Dan Stozaa894d082015-02-19 15:27:36 -0800188 // EGL_KHR_partial_update
189 { "eglSetDamageRegionKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700191
192 { "eglCreateStreamKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
194 { "eglDestroyStreamKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
196 { "eglStreamAttribKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
198 { "eglQueryStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
200 { "eglQueryStreamu64KHR",
201 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
202 { "eglQueryStreamTimeKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
204 { "eglCreateStreamProducerSurfaceKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
206 { "eglStreamConsumerGLTextureExternalKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
208 { "eglStreamConsumerAcquireKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
210 { "eglStreamConsumerReleaseKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
212 { "eglGetStreamFileDescriptorKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
214 { "eglCreateStreamFromFileDescriptorKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700216
217 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800218 { "eglGetNextFrameIdANDROID",
219 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800220 { "eglGetCompositorTimingANDROID",
221 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
222 { "eglGetCompositorTimingSupportedANDROID",
223 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700224 { "eglGetFrameTimestampsANDROID",
225 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800226 { "eglGetFrameTimestampSupportedANDROID",
227 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700228
229 // EGL_ANDROID_native_fence_sync
230 { "eglDupNativeFenceFDANDROID",
231 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700232};
233
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700234/*
235 * These extensions entry-points should not be exposed to applications.
236 * They're used internally by the Android EGL layer.
237 */
238#define FILTER_EXTENSIONS(procname) \
239 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
240 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700241 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700242
243
244
Mathias Agopian518ec112011-05-13 16:21:08 -0700245// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800246static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248static int sGLExtentionSlot = 0;
249static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
250
251static void(*findProcAddress(const char* name,
252 const extention_map_t* map, size_t n))() {
253 for (uint32_t i=0 ; i<n ; i++) {
254 if (!strcmp(name, map[i].name)) {
255 return map[i].address;
256 }
257 }
258 return NULL;
259}
260
261// ----------------------------------------------------------------------------
262
Mathias Agopian518ec112011-05-13 16:21:08 -0700263extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
264extern EGLBoolean egl_init_drivers();
265extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700266extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700267
Mathias Agopian518ec112011-05-13 16:21:08 -0700268} // namespace android;
269
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271// ----------------------------------------------------------------------------
272
273static inline void clearError() { egl_tls_t::clearError(); }
274static inline EGLContext getContext() { return egl_tls_t::getContext(); }
275
276// ----------------------------------------------------------------------------
277
278EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
279{
Jesse Hall1508ae62017-01-19 17:43:26 -0800280 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700281 clearError();
282
Dan Stozac3289c42014-01-17 11:38:34 -0800283 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 if (index >= NUM_DISPLAYS) {
285 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
286 }
287
288 if (egl_init_drivers() == EGL_FALSE) {
289 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
290 }
291
292 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
293 return dpy;
294}
295
296// ----------------------------------------------------------------------------
297// Initialization
298// ----------------------------------------------------------------------------
299
300EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
301{
302 clearError();
303
Jesse Hallb29e5e82012-04-04 16:53:42 -0700304 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800305 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700306
307 EGLBoolean res = dp->initialize(major, minor);
308
309 return res;
310}
311
312EGLBoolean eglTerminate(EGLDisplay dpy)
313{
314 // NOTE: don't unload the drivers b/c some APIs can be called
315 // after eglTerminate() has been called. eglTerminate() only
316 // terminates an EGLDisplay, not a EGL itself.
317
318 clearError();
319
Jesse Hallb29e5e82012-04-04 16:53:42 -0700320 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800321 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700322
323 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800324
Mathias Agopian518ec112011-05-13 16:21:08 -0700325 return res;
326}
327
328// ----------------------------------------------------------------------------
329// configuration
330// ----------------------------------------------------------------------------
331
332EGLBoolean eglGetConfigs( EGLDisplay dpy,
333 EGLConfig *configs,
334 EGLint config_size, EGLint *num_config)
335{
336 clearError();
337
Jesse Hallb29e5e82012-04-04 16:53:42 -0700338 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700339 if (!dp) return EGL_FALSE;
340
Mathias Agopian7773c432012-02-13 20:06:08 -0800341 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800342 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700343 }
344
Mathias Agopian7773c432012-02-13 20:06:08 -0800345 EGLBoolean res = EGL_FALSE;
346 *num_config = 0;
347
348 egl_connection_t* const cnx = &gEGLImpl;
349 if (cnx->dso) {
350 res = cnx->egl.eglGetConfigs(
351 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700352 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800353
354 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700355}
356
357EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
358 EGLConfig *configs, EGLint config_size,
359 EGLint *num_config)
360{
361 clearError();
362
Jesse Hallb29e5e82012-04-04 16:53:42 -0700363 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700364 if (!dp) return EGL_FALSE;
365
366 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800367 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700368 }
369
Mathias Agopian518ec112011-05-13 16:21:08 -0700370 EGLBoolean res = EGL_FALSE;
371 *num_config = 0;
372
Mathias Agopianada798b2012-02-13 17:09:30 -0800373 egl_connection_t* const cnx = &gEGLImpl;
374 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700375 if (attrib_list) {
376 char value[PROPERTY_VALUE_MAX];
377 property_get("debug.egl.force_msaa", value, "false");
378
379 if (!strcmp(value, "true")) {
380 size_t attribCount = 0;
381 EGLint attrib = attrib_list[0];
382
383 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700384 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700385 const EGLint *attribRendererable = NULL;
386 const EGLint *attribCaveat = NULL;
387
388 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700389 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700390 while (attrib != EGL_NONE) {
391 attrib = attrib_list[attribCount];
392 switch (attrib) {
393 case EGL_RENDERABLE_TYPE:
394 attribRendererable = &attrib_list[attribCount];
395 break;
396 case EGL_CONFIG_CAVEAT:
397 attribCaveat = &attrib_list[attribCount];
398 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800399 default:
400 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700401 }
402 attribCount++;
403 }
404
405 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
406 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800407
Romain Guy1cffc802012-10-15 18:13:05 -0700408 // Insert 2 extra attributes to force-enable MSAA 4x
409 EGLint aaAttribs[attribCount + 4];
410 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
411 aaAttribs[1] = 1;
412 aaAttribs[2] = EGL_SAMPLES;
413 aaAttribs[3] = 4;
414
415 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
416
417 EGLint numConfigAA;
418 EGLBoolean resAA = cnx->egl.eglChooseConfig(
419 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
420
421 if (resAA == EGL_TRUE && numConfigAA > 0) {
422 ALOGD("Enabling MSAA 4x");
423 *num_config = numConfigAA;
424 return resAA;
425 }
426 }
427 }
428 }
429
Mathias Agopian7773c432012-02-13 20:06:08 -0800430 res = cnx->egl.eglChooseConfig(
431 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 }
433 return res;
434}
435
436EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
437 EGLint attribute, EGLint *value)
438{
439 clearError();
440
Jesse Hallb29e5e82012-04-04 16:53:42 -0700441 egl_connection_t* cnx = NULL;
442 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
443 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800444
Mathias Agopian518ec112011-05-13 16:21:08 -0700445 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800446 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700447}
448
449// ----------------------------------------------------------------------------
450// surfaces
451// ----------------------------------------------------------------------------
452
Jesse Hallc2e41222013-08-08 13:40:22 -0700453// Turn linear formats into corresponding sRGB formats when colorspace is
454// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
455// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800456// the modification isn't possible, the original dataSpace is returned.
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600457static android_dataspace modifyBufferDataspace(android_dataspace dataSpace,
458 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700459 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800460 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700461 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800462 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600463 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
464 return HAL_DATASPACE_DISPLAY_P3;
465 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
466 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600467 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
468 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600469 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
470 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700471 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT) {
472 return HAL_DATASPACE_BT2020_LINEAR;
473 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_PQ_EXT) {
474 return HAL_DATASPACE_BT2020_PQ;
Jesse Hallc2e41222013-08-08 13:40:22 -0700475 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800476 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700477}
478
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700479// Return true if we stripped any EGL_GL_COLORSPACE_KHR or HDR metadata attributes.
480// Protect devices from attributes they don't recognize that are managed by Android
481static EGLBoolean stripAttributes(egl_display_ptr dp, const EGLint* attrib_list,
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -0800482 EGLint format,
483 std::vector<EGLint>& stripped_attrib_list) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600484 std::vector<EGLint> allowedColorSpaces;
485 switch (format) {
486 case HAL_PIXEL_FORMAT_RGBA_8888:
487 case HAL_PIXEL_FORMAT_RGB_565:
488 // driver okay with linear & sRGB for 8888, but can't handle
489 // Display-P3 or other spaces.
490 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
491 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
492 break;
493
494 case HAL_PIXEL_FORMAT_RGBA_FP16:
495 case HAL_PIXEL_FORMAT_RGBA_1010102:
496 default:
497 // driver does not want to see colorspace attributes for 1010102 or fp16.
498 // Future: if driver supports XXXX extension, we can pass down that colorspace
499 break;
500 }
501
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700502 if (!attrib_list) return false;
503
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600504 bool stripped = false;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700505 for (const EGLint* attr = attrib_list; attr[0] != EGL_NONE; attr += 2) {
506 switch (attr[0]) {
507 case EGL_GL_COLORSPACE_KHR: {
508 EGLint colorSpace = attr[1];
509 bool found = false;
510 // Verify that color space is allowed
511 for (auto it : allowedColorSpaces) {
512 if (colorSpace == it) {
513 found = true;
514 }
515 }
Courtney Goeltzenleuchter52de2fd2018-02-01 09:51:58 -0700516 if (found || !dp->haveExtension("EGL_KHR_gl_colorspace")) {
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700517 stripped_attrib_list.push_back(attr[0]);
518 stripped_attrib_list.push_back(attr[1]);
Courtney Goeltzenleuchter52de2fd2018-02-01 09:51:58 -0700519 } else {
520 stripped = true;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600521 }
522 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700523 break;
524 case EGL_SMPTE2086_DISPLAY_PRIMARY_RX_EXT:
525 case EGL_SMPTE2086_DISPLAY_PRIMARY_RY_EXT:
526 case EGL_SMPTE2086_DISPLAY_PRIMARY_GX_EXT:
527 case EGL_SMPTE2086_DISPLAY_PRIMARY_GY_EXT:
528 case EGL_SMPTE2086_DISPLAY_PRIMARY_BX_EXT:
529 case EGL_SMPTE2086_DISPLAY_PRIMARY_BY_EXT:
530 case EGL_SMPTE2086_WHITE_POINT_X_EXT:
531 case EGL_SMPTE2086_WHITE_POINT_Y_EXT:
532 case EGL_SMPTE2086_MAX_LUMINANCE_EXT:
533 case EGL_SMPTE2086_MIN_LUMINANCE_EXT:
534 if (dp->haveExtension("EGL_EXT_surface_SMPTE2086_metadata")) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600535 stripped = true;
536 } else {
537 stripped_attrib_list.push_back(attr[0]);
538 stripped_attrib_list.push_back(attr[1]);
539 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700540 break;
541 case EGL_CTA861_3_MAX_CONTENT_LIGHT_LEVEL_EXT:
542 case EGL_CTA861_3_MAX_FRAME_AVERAGE_LEVEL_EXT:
543 if (dp->haveExtension("EGL_EXT_surface_CTA861_3_metadata")) {
544 stripped = true;
545 } else {
546 stripped_attrib_list.push_back(attr[0]);
547 stripped_attrib_list.push_back(attr[1]);
548 }
549 break;
550 default:
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600551 stripped_attrib_list.push_back(attr[0]);
552 stripped_attrib_list.push_back(attr[1]);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700553 break;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600554 }
555 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700556
557 // Make sure there is at least one attribute
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600558 if (stripped) {
559 stripped_attrib_list.push_back(EGL_NONE);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600560 }
561 return stripped;
562}
563
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600564static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, NativeWindowType window,
565 const EGLint* attrib_list, EGLint& colorSpace,
566 android_dataspace& dataSpace) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600567 colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
568 dataSpace = HAL_DATASPACE_UNKNOWN;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600569
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600570 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
571 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
572 if (*attr == EGL_GL_COLORSPACE_KHR) {
573 colorSpace = attr[1];
574 bool found = false;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600575 bool verify = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600576 // Verify that color space is allowed
577 if (colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
578 colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600579 // SRGB and LINEAR are always supported when EGL_KHR_gl_colorspace
580 // is available, so no need to verify.
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600581 found = true;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600582 verify = false;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700583 } else if (colorSpace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT &&
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600584 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
585 found = true;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700586 } else if (colorSpace == EGL_GL_COLORSPACE_BT2020_PQ_EXT &&
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600587 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
588 found = true;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600589 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_EXT &&
590 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb")) {
591 found = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600592 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
593 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
594 found = true;
595 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
596 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
597 found = true;
598 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
599 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
600 found = true;
601 }
602 if (!found) {
603 return false;
604 }
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600605 if (verify && window) {
606 bool wide_color_support = true;
607 // Ordinarily we'd put a call to native_window_get_wide_color_support
608 // at the beginning of the function so that we'll have the
609 // result when needed elsewhere in the function.
610 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
611 // SurfaceFlinger is required to answer the call below we would
612 // end up in a deadlock situation. By moving the call to only happen
613 // if the application has specifically asked for wide-color we avoid
614 // the deadlock with SurfaceFlinger since it will not ask for a
615 // wide-color surface.
616 int err = native_window_get_wide_color_support(window, &wide_color_support);
617
618 if (err) {
619 ALOGE("getColorSpaceAttribute: invalid window (win=%p) "
620 "failed (%#x) (already connected to another API?)",
621 window, err);
622 return false;
623 }
624 if (!wide_color_support) {
625 // Application has asked for a wide-color colorspace but
626 // wide-color support isn't available on the display the window is on.
627 return false;
628 }
629 }
Courtney Goeltzenleuchter1d4f7a22017-05-05 13:30:25 -0600630 // Only change the dataSpace from default if the application
631 // has explicitly set the color space with a EGL_GL_COLORSPACE_KHR attribute.
632 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600633 }
634 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600635 }
636 return true;
637}
638
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600639static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
640 EGLint& colorSpace, android_dataspace& dataSpace) {
641 return getColorSpaceAttribute(dp, NULL, attrib_list, colorSpace, dataSpace);
642}
643
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600644void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config, EGLint& format) {
645 // Set the native window's buffers format to match what this config requests.
646 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
647 // of our native format. So if sRGB gamma is requested, we have to
648 // modify the EGLconfig's format before setting the native window's
649 // format.
650
651 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
652 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
653
654 EGLint a = 0;
655 EGLint r, g, b;
656 r = g = b = 0;
657 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
658 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
659 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
660 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
661 EGLint colorDepth = r + g + b;
662
663 // Today, the driver only understands sRGB and linear on 888X
664 // formats. Strip other colorspaces from the attribute list and
665 // only use them to set the dataspace via
666 // native_window_set_buffers_dataspace
667 // if pixel format is RGBX 8888
668 // TBD: Can test for future extensions that indicate that driver
669 // handles requested color space and we can let it through.
670 // allow SRGB and LINEAR. All others need to be stripped.
671 // else if 565, 4444
672 // TBD: Can we assume these are supported if 8888 is?
673 // else if FP16 or 1010102
674 // strip colorspace from attribs.
675 // endif
676 if (a == 0) {
677 if (colorDepth <= 16) {
678 format = HAL_PIXEL_FORMAT_RGB_565;
679 } else {
680 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
681 if (colorDepth > 24) {
682 format = HAL_PIXEL_FORMAT_RGBA_1010102;
683 } else {
684 format = HAL_PIXEL_FORMAT_RGBX_8888;
685 }
686 } else {
687 format = HAL_PIXEL_FORMAT_RGBA_FP16;
688 }
689 }
690 } else {
691 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
692 if (colorDepth > 24) {
693 format = HAL_PIXEL_FORMAT_RGBA_1010102;
694 } else {
695 format = HAL_PIXEL_FORMAT_RGBA_8888;
696 }
697 } else {
698 format = HAL_PIXEL_FORMAT_RGBA_FP16;
699 }
700 }
701}
702
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700703EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
704 android_smpte2086_metadata smpteMetadata;
705 if (s->getSmpte2086Metadata(smpteMetadata)) {
706 int err =
707 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
708 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700709 if (err != 0) {
710 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
711 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700712 return EGL_FALSE;
713 }
714 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700715 android_cta861_3_metadata cta8613Metadata;
716 if (s->getCta8613Metadata(cta8613Metadata)) {
717 int err =
718 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
719 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700720 if (err != 0) {
721 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
722 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700723 return EGL_FALSE;
724 }
725 }
726 return EGL_TRUE;
727}
728
Mathias Agopian518ec112011-05-13 16:21:08 -0700729EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
730 NativeWindowType window,
731 const EGLint *attrib_list)
732{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700733 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700734 clearError();
735
Jesse Hallb29e5e82012-04-04 16:53:42 -0700736 egl_connection_t* cnx = NULL;
737 egl_display_ptr dp = validate_display_connection(dpy, cnx);
738 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800739 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700740
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800741 if (!window) {
742 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
743 }
744
745 int value = 0;
746 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
747 if (!value) {
748 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
749 }
750
Andy McFaddend566ce32014-01-07 15:54:17 -0800751 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800752 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800753 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
754 "failed (%#x) (already connected to another API?)",
755 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700756 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700757 }
758
Mathias Agopian95921562017-02-24 14:31:31 -0800759 EGLint format;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600760 getNativePixelFormat(iDpy, cnx, config, format);
761
762 // now select correct colorspace and dataspace based on user's attribute list
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600763 EGLint colorSpace;
764 android_dataspace dataSpace;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600765 if (!getColorSpaceAttribute(dp, window, attrib_list, colorSpace, dataSpace)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600766 ALOGE("error invalid colorspace: %d", colorSpace);
767 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700768 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800769
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600770 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700771 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600772 // Had to modify the attribute list due to use of color space.
773 // Use modified list from here on.
774 attrib_list = strippedAttribList.data();
775 }
776
Jesse Hallc2e41222013-08-08 13:40:22 -0700777 if (format != 0) {
778 int err = native_window_set_buffers_format(window, format);
779 if (err != 0) {
780 ALOGE("error setting native window pixel format: %s (%d)",
781 strerror(-err), err);
782 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
783 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
784 }
785 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700786
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800787 if (dataSpace != 0) {
788 int err = native_window_set_buffers_data_space(window, dataSpace);
789 if (err != 0) {
790 ALOGE("error setting native window pixel dataSpace: %s (%d)",
791 strerror(-err), err);
792 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
793 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
794 }
795 }
796
Jamie Gennis59769462011-11-19 18:04:43 -0800797 // the EGL spec requires that a new EGLSurface default to swap interval
798 // 1, so explicitly set that on the window here.
799 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
800 anw->setSwapInterval(anw, 1);
801
Mathias Agopian518ec112011-05-13 16:21:08 -0700802 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800803 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700804 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600805 egl_surface_t* s =
806 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700807
808 if (setSurfaceMetadata(s, window, origAttribList)) {
809 return s;
810 }
811 eglDestroySurface(dpy, s);
Mathias Agopian518ec112011-05-13 16:21:08 -0700812 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700813
814 // EGLSurface creation failed
815 native_window_set_buffers_format(window, 0);
816 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700817 }
818 return EGL_NO_SURFACE;
819}
820
821EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
822 NativePixmapType pixmap,
823 const EGLint *attrib_list)
824{
825 clearError();
826
Jesse Hallb29e5e82012-04-04 16:53:42 -0700827 egl_connection_t* cnx = NULL;
828 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600829 EGLint colorSpace;
830 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700831 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600832 // now select a corresponding sRGB format if needed
833 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
834 ALOGE("error invalid colorspace: %d", colorSpace);
835 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
836 }
837
Mathias Agopian518ec112011-05-13 16:21:08 -0700838 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800839 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700840 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600841 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700842 return s;
843 }
844 }
845 return EGL_NO_SURFACE;
846}
847
848EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
849 const EGLint *attrib_list)
850{
851 clearError();
852
Jesse Hallb29e5e82012-04-04 16:53:42 -0700853 egl_connection_t* cnx = NULL;
854 egl_display_ptr dp = validate_display_connection(dpy, cnx);
855 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600856 EGLDisplay iDpy = dp->disp.dpy;
857 EGLint format;
858 getNativePixelFormat(iDpy, cnx, config, format);
859
860 // now select correct colorspace and dataspace based on user's attribute list
861 EGLint colorSpace;
862 android_dataspace dataSpace;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600863 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
864 ALOGE("error invalid colorspace: %d", colorSpace);
865 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
866 }
867
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600868 // Pbuffers are not displayed so we don't need to store the
869 // colorspace. We do need to filter out color spaces the
870 // driver doesn't know how to process.
871 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700872 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600873 // Had to modify the attribute list due to use of color space.
874 // Use modified list from here on.
875 attrib_list = strippedAttribList.data();
876 }
877
Mathias Agopian518ec112011-05-13 16:21:08 -0700878 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800879 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700880 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600881 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700882 return s;
883 }
884 }
885 return EGL_NO_SURFACE;
886}
Jesse Hall47743382013-02-08 11:13:46 -0800887
Mathias Agopian518ec112011-05-13 16:21:08 -0700888EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
889{
890 clearError();
891
Jesse Hallb29e5e82012-04-04 16:53:42 -0700892 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700893 if (!dp) return EGL_FALSE;
894
Jesse Hallb29e5e82012-04-04 16:53:42 -0700895 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700896 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800897 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700898
899 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800900 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700901 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700902 _s.terminate();
903 }
904 return result;
905}
906
907EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
908 EGLint attribute, EGLint *value)
909{
910 clearError();
911
Jesse Hallb29e5e82012-04-04 16:53:42 -0700912 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700913 if (!dp) return EGL_FALSE;
914
Jesse Hallb29e5e82012-04-04 16:53:42 -0700915 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700916 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800917 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700918
Mathias Agopian518ec112011-05-13 16:21:08 -0700919 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700920 if (s->getColorSpaceAttribute(attribute, value)) {
921 return EGL_TRUE;
922 } else if (s->getSmpte2086Attribute(attribute, value)) {
923 return EGL_TRUE;
924 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600925 return EGL_TRUE;
926 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700927 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700928}
929
Jamie Gennise8696a42012-01-15 18:54:57 -0800930void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800931 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800932 clearError();
933
Jesse Hallb29e5e82012-04-04 16:53:42 -0700934 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800935 if (!dp) {
936 return;
937 }
938
Jesse Hallb29e5e82012-04-04 16:53:42 -0700939 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800940 if (!_s.get()) {
941 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800942 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800943}
944
Mathias Agopian518ec112011-05-13 16:21:08 -0700945// ----------------------------------------------------------------------------
946// Contexts
947// ----------------------------------------------------------------------------
948
949EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
950 EGLContext share_list, const EGLint *attrib_list)
951{
952 clearError();
953
Jesse Hallb29e5e82012-04-04 16:53:42 -0700954 egl_connection_t* cnx = NULL;
955 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700956 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700957 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700958 if (!ContextRef(dp.get(), share_list).get()) {
959 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
960 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700961 egl_context_t* const c = get_context(share_list);
962 share_list = c->context;
963 }
964 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800965 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700966 if (context != EGL_NO_CONTEXT) {
967 // figure out if it's a GLESv1 or GLESv2
968 int version = 0;
969 if (attrib_list) {
970 while (*attrib_list != EGL_NONE) {
971 GLint attr = *attrib_list++;
972 GLint value = *attrib_list++;
973 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
974 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800975 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800976 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800977 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700978 }
979 }
980 };
981 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700982 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
983 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700984 return c;
985 }
986 }
987 return EGL_NO_CONTEXT;
988}
989
990EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
991{
992 clearError();
993
Jesse Hallb29e5e82012-04-04 16:53:42 -0700994 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700995 if (!dp)
996 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700997
Jesse Hallb29e5e82012-04-04 16:53:42 -0700998 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700999 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001000 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -08001001
Mathias Agopian518ec112011-05-13 16:21:08 -07001002 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -08001003 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -07001004 if (result == EGL_TRUE) {
1005 _c.terminate();
1006 }
1007 return result;
1008}
1009
Mathias Agopian518ec112011-05-13 16:21:08 -07001010EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1011 EGLSurface read, EGLContext ctx)
1012{
1013 clearError();
1014
Jesse Hallb29e5e82012-04-04 16:53:42 -07001015 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -08001016 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001017
Mathias Agopian5b287a62011-05-16 18:58:55 -07001018 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
1019 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
1020 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -07001021 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
1022 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001023 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001024 }
1025
1026 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -07001027 ContextRef _c(dp.get(), ctx);
1028 SurfaceRef _d(dp.get(), draw);
1029 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001030
1031 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -07001032 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001033 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -08001034 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001035 }
1036
1037 // these are the underlying implementation's object
1038 EGLContext impl_ctx = EGL_NO_CONTEXT;
1039 EGLSurface impl_draw = EGL_NO_SURFACE;
1040 EGLSurface impl_read = EGL_NO_SURFACE;
1041
1042 // these are our objects structs passed in
1043 egl_context_t * c = NULL;
1044 egl_surface_t const * d = NULL;
1045 egl_surface_t const * r = NULL;
1046
1047 // these are the current objects structs
1048 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001049
Mathias Agopian518ec112011-05-13 16:21:08 -07001050 if (ctx != EGL_NO_CONTEXT) {
1051 c = get_context(ctx);
1052 impl_ctx = c->context;
1053 } else {
1054 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001055 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1056 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001057 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001058 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001059 if (cur_c == NULL) {
1060 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001061 // not an error, there is just no current context.
1062 return EGL_TRUE;
1063 }
1064 }
1065
1066 // retrieve the underlying implementation's draw EGLSurface
1067 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001068 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001069 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001070 impl_draw = d->surface;
1071 }
1072
1073 // retrieve the underlying implementation's read EGLSurface
1074 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001075 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001076 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001077 impl_read = r->surface;
1078 }
1079
Mathias Agopian518ec112011-05-13 16:21:08 -07001080
Jesse Hallb29e5e82012-04-04 16:53:42 -07001081 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001082 draw, read, ctx,
1083 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001084
1085 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001086 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001087 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1088 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001089 _c.acquire();
1090 _r.acquire();
1091 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001092 } else {
1093 setGLHooksThreadSpecific(&gHooksNoContext);
1094 egl_tls_t::setContext(EGL_NO_CONTEXT);
1095 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001096 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001097
Mike Stroyan47e741b2017-06-01 13:56:18 -06001098 if (cur_c != NULL) {
1099 // Force return to current context for drivers that cannot handle errors
1100 EGLBoolean restore_result = EGL_FALSE;
1101 // get a reference to the old current objects
1102 ContextRef _c2(dp.get(), cur_c);
1103 SurfaceRef _d2(dp.get(), cur_c->draw);
1104 SurfaceRef _r2(dp.get(), cur_c->read);
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001105
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001106 c = cur_c;
1107 impl_ctx = c->context;
1108 impl_draw = EGL_NO_SURFACE;
1109 if (cur_c->draw != EGL_NO_SURFACE) {
1110 d = get_surface(cur_c->draw);
1111 impl_draw = d->surface;
1112 }
1113 impl_read = EGL_NO_SURFACE;
1114 if (cur_c->read != EGL_NO_SURFACE) {
1115 r = get_surface(cur_c->read);
1116 impl_read = r->surface;
1117 }
1118 restore_result = dp->makeCurrent(c, cur_c,
1119 cur_c->draw, cur_c->read, cur_c->context,
1120 impl_draw, impl_read, impl_ctx);
Mike Stroyan47e741b2017-06-01 13:56:18 -06001121 if (restore_result == EGL_TRUE) {
1122 _c2.acquire();
1123 _r2.acquire();
1124 _d2.acquire();
1125 } else {
1126 ALOGE("Could not restore original EGL context");
1127 }
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001128 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001129 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001130 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001131 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001132 }
1133 return result;
1134}
1135
1136
1137EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1138 EGLint attribute, EGLint *value)
1139{
1140 clearError();
1141
Jesse Hallb29e5e82012-04-04 16:53:42 -07001142 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001143 if (!dp) return EGL_FALSE;
1144
Jesse Hallb29e5e82012-04-04 16:53:42 -07001145 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001146 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001147
Mathias Agopian518ec112011-05-13 16:21:08 -07001148 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001149 return c->cnx->egl.eglQueryContext(
1150 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001151
Mathias Agopian518ec112011-05-13 16:21:08 -07001152}
1153
1154EGLContext eglGetCurrentContext(void)
1155{
1156 // could be called before eglInitialize(), but we wouldn't have a context
1157 // then, and this function would correctly return EGL_NO_CONTEXT.
1158
1159 clearError();
1160
1161 EGLContext ctx = getContext();
1162 return ctx;
1163}
1164
1165EGLSurface eglGetCurrentSurface(EGLint readdraw)
1166{
1167 // could be called before eglInitialize(), but we wouldn't have a context
1168 // then, and this function would correctly return EGL_NO_SURFACE.
1169
1170 clearError();
1171
1172 EGLContext ctx = getContext();
1173 if (ctx) {
1174 egl_context_t const * const c = get_context(ctx);
1175 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1176 switch (readdraw) {
1177 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001178 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001179 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1180 }
1181 }
1182 return EGL_NO_SURFACE;
1183}
1184
1185EGLDisplay eglGetCurrentDisplay(void)
1186{
1187 // could be called before eglInitialize(), but we wouldn't have a context
1188 // then, and this function would correctly return EGL_NO_DISPLAY.
1189
1190 clearError();
1191
1192 EGLContext ctx = getContext();
1193 if (ctx) {
1194 egl_context_t const * const c = get_context(ctx);
1195 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1196 return c->dpy;
1197 }
1198 return EGL_NO_DISPLAY;
1199}
1200
1201EGLBoolean eglWaitGL(void)
1202{
Mathias Agopian518ec112011-05-13 16:21:08 -07001203 clearError();
1204
Mathias Agopianada798b2012-02-13 17:09:30 -08001205 egl_connection_t* const cnx = &gEGLImpl;
1206 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001207 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001208
1209 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001210}
1211
1212EGLBoolean eglWaitNative(EGLint engine)
1213{
Mathias Agopian518ec112011-05-13 16:21:08 -07001214 clearError();
1215
Mathias Agopianada798b2012-02-13 17:09:30 -08001216 egl_connection_t* const cnx = &gEGLImpl;
1217 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001218 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001219
1220 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001221}
1222
1223EGLint eglGetError(void)
1224{
Mathias Agopianada798b2012-02-13 17:09:30 -08001225 EGLint err = EGL_SUCCESS;
1226 egl_connection_t* const cnx = &gEGLImpl;
1227 if (cnx->dso) {
1228 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001229 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001230 if (err == EGL_SUCCESS) {
1231 err = egl_tls_t::getError();
1232 }
1233 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001234}
1235
Michael Chockc0ec5e22014-01-27 08:14:33 -08001236static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001237 const char* procname) {
1238 const egl_connection_t* cnx = &gEGLImpl;
1239 void* proc = NULL;
1240
Michael Chockc0ec5e22014-01-27 08:14:33 -08001241 proc = dlsym(cnx->libEgl, procname);
1242 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1243
Jesse Hallc07b5202013-07-04 12:08:16 -07001244 proc = dlsym(cnx->libGles2, procname);
1245 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1246
1247 proc = dlsym(cnx->libGles1, procname);
1248 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1249
1250 return NULL;
1251}
1252
Mathias Agopian518ec112011-05-13 16:21:08 -07001253__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1254{
1255 // eglGetProcAddress() could be the very first function called
1256 // in which case we must make sure we've initialized ourselves, this
1257 // happens the first time egl_get_display() is called.
1258
1259 clearError();
1260
1261 if (egl_init_drivers() == EGL_FALSE) {
1262 setError(EGL_BAD_PARAMETER, NULL);
1263 return NULL;
1264 }
1265
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001266 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001267 return NULL;
1268 }
1269
Mathias Agopian518ec112011-05-13 16:21:08 -07001270 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001271 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 if (addr) return addr;
1273
Michael Chockc0ec5e22014-01-27 08:14:33 -08001274 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001275 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001276
Mathias Agopian518ec112011-05-13 16:21:08 -07001277 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1278 pthread_mutex_lock(&sExtensionMapMutex);
1279
1280 /*
1281 * Since eglGetProcAddress() is not associated to anything, it needs
1282 * to return a function pointer that "works" regardless of what
1283 * the current context is.
1284 *
1285 * For this reason, we return a "forwarder", a small stub that takes
1286 * care of calling the function associated with the context
1287 * currently bound.
1288 *
1289 * We first look for extensions we've already resolved, if we're seeing
1290 * this extension for the first time, we go through all our
1291 * implementations and call eglGetProcAddress() and record the
1292 * result in the appropriate implementation hooks and return the
1293 * address of the forwarder corresponding to that hook set.
1294 *
1295 */
1296
Mathias Agopian65421432017-03-08 11:49:05 -08001297 const std::string name(procname);
1298
1299 auto& extentionMap = sGLExtentionMap;
1300 auto pos = extentionMap.find(name);
1301 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001302 const int slot = sGLExtentionSlot;
1303
Steve Blocke6f43dd2012-01-06 19:20:56 +00001304 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001305 "no more slots for eglGetProcAddress(\"%s\")",
1306 procname);
1307
1308 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1309 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001310
1311 egl_connection_t* const cnx = &gEGLImpl;
1312 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001313 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001314 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001315 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1316 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001317 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001318 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001319 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001320
Mathias Agopian518ec112011-05-13 16:21:08 -07001321 if (found) {
1322 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001323 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001324 sGLExtentionSlot++;
1325 }
1326 }
1327
1328 pthread_mutex_unlock(&sExtensionMapMutex);
1329 return addr;
1330}
1331
Mathias Agopian65421432017-03-08 11:49:05 -08001332class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001333public:
1334
1335 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001336 static FrameCompletionThread thread;
1337
1338 char name[64];
1339
1340 std::lock_guard<std::mutex> lock(thread.mMutex);
1341 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1342 ATRACE_NAME(name);
1343
1344 thread.mQueue.push_back(sync);
1345 thread.mCondition.notify_one();
1346 thread.mFramesQueued++;
1347 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001348 }
1349
1350private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001351
Mathias Agopian65421432017-03-08 11:49:05 -08001352 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1353 std::thread thread(&FrameCompletionThread::loop, this);
1354 thread.detach();
1355 }
1356
1357#pragma clang diagnostic push
1358#pragma clang diagnostic ignored "-Wmissing-noreturn"
1359 void loop() {
1360 while (true) {
1361 threadLoop();
1362 }
1363 }
1364#pragma clang diagnostic pop
1365
1366 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001367 EGLSyncKHR sync;
1368 uint32_t frameNum;
1369 {
Mathias Agopian65421432017-03-08 11:49:05 -08001370 std::unique_lock<std::mutex> lock(mMutex);
1371 while (mQueue.empty()) {
1372 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001373 }
1374 sync = mQueue[0];
1375 frameNum = mFramesCompleted;
1376 }
1377 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1378 {
Mathias Agopian65421432017-03-08 11:49:05 -08001379 char name[64];
1380 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1381 ATRACE_NAME(name);
1382
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001383 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1384 if (result == EGL_FALSE) {
1385 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1386 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1387 ALOGE("FrameCompletion: timeout waiting for fence");
1388 }
1389 eglDestroySyncKHR(dpy, sync);
1390 }
1391 {
Mathias Agopian65421432017-03-08 11:49:05 -08001392 std::lock_guard<std::mutex> lock(mMutex);
1393 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001394 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001395 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001396 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001397 }
1398
1399 uint32_t mFramesQueued;
1400 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001401 std::deque<EGLSyncKHR> mQueue;
1402 std::condition_variable mCondition;
1403 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001404};
1405
Dan Stozaa894d082015-02-19 15:27:36 -08001406EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1407 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001408{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001409 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001410 clearError();
1411
Jesse Hallb29e5e82012-04-04 16:53:42 -07001412 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001413 if (!dp) return EGL_FALSE;
1414
Jesse Hallb29e5e82012-04-04 16:53:42 -07001415 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001416 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001417 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001418
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001419 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001420
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001421 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1422 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1423 if (sync != EGL_NO_SYNC_KHR) {
1424 FrameCompletionThread::queueSync(sync);
1425 }
1426 }
1427
Mathias Agopian7db993a2012-03-25 00:49:46 -07001428 if (CC_UNLIKELY(dp->finishOnSwap)) {
1429 uint32_t pixel;
1430 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1431 if (c) {
1432 // glReadPixels() ensures that the frame is complete
1433 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1434 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1435 }
1436 }
1437
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001438 if (!sendSurfaceMetadata(s)) {
1439 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1440 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1441 }
1442
Dan Stozaa894d082015-02-19 15:27:36 -08001443 if (n_rects == 0) {
1444 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1445 }
1446
Mathias Agopian65421432017-03-08 11:49:05 -08001447 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001448 for (int r = 0; r < n_rects; ++r) {
1449 int offset = r * 4;
1450 int x = rects[offset];
1451 int y = rects[offset + 1];
1452 int width = rects[offset + 2];
1453 int height = rects[offset + 3];
1454 android_native_rect_t androidRect;
1455 androidRect.left = x;
1456 androidRect.top = y + height;
1457 androidRect.right = x + width;
1458 androidRect.bottom = y;
1459 androidRects.push_back(androidRect);
1460 }
Mathias Agopian65421432017-03-08 11:49:05 -08001461 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001462
1463 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1464 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1465 rects, n_rects);
1466 } else {
1467 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1468 }
1469}
1470
1471EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1472{
1473 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001474}
1475
1476EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1477 NativePixmapType target)
1478{
1479 clearError();
1480
Jesse Hallb29e5e82012-04-04 16:53:42 -07001481 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001482 if (!dp) return EGL_FALSE;
1483
Jesse Hallb29e5e82012-04-04 16:53:42 -07001484 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001485 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001486 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001487
Mathias Agopian518ec112011-05-13 16:21:08 -07001488 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001489 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001490}
1491
1492const char* eglQueryString(EGLDisplay dpy, EGLint name)
1493{
1494 clearError();
1495
Chia-I Wue57d1352016-08-15 16:10:02 +08001496 // Generate an error quietly when client extensions (as defined by
1497 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1498 // validate_display to generate the error as validate_display would log
1499 // the error, which can be misleading.
1500 //
1501 // If we want to support EGL_EXT_client_extensions later, we can return
1502 // the client extension string here instead.
1503 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001504 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001505
Jesse Hallb29e5e82012-04-04 16:53:42 -07001506 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001507 if (!dp) return (const char *) NULL;
1508
1509 switch (name) {
1510 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001511 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001512 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001513 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001514 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001515 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001517 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001518 default:
1519 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001520 }
1521 return setError(EGL_BAD_PARAMETER, (const char *)0);
1522}
1523
Jiyong Park00b15b82017-08-10 20:30:56 +09001524extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001525{
1526 clearError();
1527
1528 const egl_display_ptr dp = validate_display(dpy);
1529 if (!dp) return (const char *) NULL;
1530
1531 switch (name) {
1532 case EGL_VENDOR:
1533 return dp->disp.queryString.vendor;
1534 case EGL_VERSION:
1535 return dp->disp.queryString.version;
1536 case EGL_EXTENSIONS:
1537 return dp->disp.queryString.extensions;
1538 case EGL_CLIENT_APIS:
1539 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001540 default:
1541 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001542 }
1543 return setError(EGL_BAD_PARAMETER, (const char *)0);
1544}
Mathias Agopian518ec112011-05-13 16:21:08 -07001545
1546// ----------------------------------------------------------------------------
1547// EGL 1.1
1548// ----------------------------------------------------------------------------
1549
1550EGLBoolean eglSurfaceAttrib(
1551 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1552{
1553 clearError();
1554
Jesse Hallb29e5e82012-04-04 16:53:42 -07001555 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001556 if (!dp) return EGL_FALSE;
1557
Jesse Hallb29e5e82012-04-04 16:53:42 -07001558 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001559 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001560 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001561
Pablo Ceballosc18be292016-05-31 14:55:42 -07001562 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001563
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001564 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001565 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001566 setError(EGL_BAD_SURFACE, EGL_FALSE);
1567 }
Mathias Agopian65421432017-03-08 11:49:05 -08001568 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1569 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001570 }
1571
Pablo Ceballosc18be292016-05-31 14:55:42 -07001572 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001573 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001574 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001575 }
Mathias Agopian65421432017-03-08 11:49:05 -08001576 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1577 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001578 }
1579
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001580 if (s->setSmpte2086Attribute(attribute, value)) {
1581 return EGL_TRUE;
1582 } else if (s->setCta8613Attribute(attribute, value)) {
1583 return EGL_TRUE;
1584 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001585 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001586 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001587 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001588 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001589}
1590
1591EGLBoolean eglBindTexImage(
1592 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1593{
1594 clearError();
1595
Jesse Hallb29e5e82012-04-04 16:53:42 -07001596 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001597 if (!dp) return EGL_FALSE;
1598
Jesse Hallb29e5e82012-04-04 16:53:42 -07001599 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001600 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001601 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001602
Mathias Agopian518ec112011-05-13 16:21:08 -07001603 egl_surface_t const * const s = get_surface(surface);
1604 if (s->cnx->egl.eglBindTexImage) {
1605 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001606 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001607 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001608 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001609}
1610
1611EGLBoolean eglReleaseTexImage(
1612 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1613{
1614 clearError();
1615
Jesse Hallb29e5e82012-04-04 16:53:42 -07001616 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001617 if (!dp) return EGL_FALSE;
1618
Jesse Hallb29e5e82012-04-04 16:53:42 -07001619 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001620 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001621 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001622
Mathias Agopian518ec112011-05-13 16:21:08 -07001623 egl_surface_t const * const s = get_surface(surface);
1624 if (s->cnx->egl.eglReleaseTexImage) {
1625 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001626 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001627 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001628 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001629}
1630
1631EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1632{
1633 clearError();
1634
Jesse Hallb29e5e82012-04-04 16:53:42 -07001635 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001636 if (!dp) return EGL_FALSE;
1637
1638 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001639 egl_connection_t* const cnx = &gEGLImpl;
1640 if (cnx->dso && cnx->egl.eglSwapInterval) {
1641 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001642 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001643
Mathias Agopian518ec112011-05-13 16:21:08 -07001644 return res;
1645}
1646
1647
1648// ----------------------------------------------------------------------------
1649// EGL 1.2
1650// ----------------------------------------------------------------------------
1651
1652EGLBoolean eglWaitClient(void)
1653{
1654 clearError();
1655
Mathias Agopianada798b2012-02-13 17:09:30 -08001656 egl_connection_t* const cnx = &gEGLImpl;
1657 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001658 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001659
1660 EGLBoolean res;
1661 if (cnx->egl.eglWaitClient) {
1662 res = cnx->egl.eglWaitClient();
1663 } else {
1664 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001665 }
1666 return res;
1667}
1668
1669EGLBoolean eglBindAPI(EGLenum api)
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
1677 // bind this API on all EGLs
1678 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001679 egl_connection_t* const cnx = &gEGLImpl;
1680 if (cnx->dso && cnx->egl.eglBindAPI) {
1681 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001682 }
1683 return res;
1684}
1685
1686EGLenum eglQueryAPI(void)
1687{
1688 clearError();
1689
1690 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001691 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001692 }
1693
Mathias Agopianada798b2012-02-13 17:09:30 -08001694 egl_connection_t* const cnx = &gEGLImpl;
1695 if (cnx->dso && cnx->egl.eglQueryAPI) {
1696 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001697 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001698
Mathias Agopian518ec112011-05-13 16:21:08 -07001699 // or, it can only be OpenGL ES
1700 return EGL_OPENGL_ES_API;
1701}
1702
1703EGLBoolean eglReleaseThread(void)
1704{
1705 clearError();
1706
Mathias Agopianada798b2012-02-13 17:09:30 -08001707 egl_connection_t* const cnx = &gEGLImpl;
1708 if (cnx->dso && cnx->egl.eglReleaseThread) {
1709 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001710 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301711
1712 // If there is context bound to the thread, release it
1713 egl_display_t::loseCurrent(get_context(getContext()));
1714
Mathias Agopian518ec112011-05-13 16:21:08 -07001715 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001716 return EGL_TRUE;
1717}
1718
1719EGLSurface eglCreatePbufferFromClientBuffer(
1720 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1721 EGLConfig config, const EGLint *attrib_list)
1722{
1723 clearError();
1724
Jesse Hallb29e5e82012-04-04 16:53:42 -07001725 egl_connection_t* cnx = NULL;
1726 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1727 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001728 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1729 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001730 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001731 }
1732 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1733}
1734
1735// ----------------------------------------------------------------------------
1736// EGL_EGLEXT_VERSION 3
1737// ----------------------------------------------------------------------------
1738
1739EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1740 const EGLint *attrib_list)
1741{
1742 clearError();
1743
Jesse Hallb29e5e82012-04-04 16:53:42 -07001744 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001745 if (!dp) return EGL_FALSE;
1746
Jesse Hallb29e5e82012-04-04 16:53:42 -07001747 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001748 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001749 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001750
1751 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001752 if (s->cnx->egl.eglLockSurfaceKHR) {
1753 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001754 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001755 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001756 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001757}
1758
1759EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1760{
1761 clearError();
1762
Jesse Hallb29e5e82012-04-04 16:53:42 -07001763 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001764 if (!dp) return EGL_FALSE;
1765
Jesse Hallb29e5e82012-04-04 16:53:42 -07001766 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001767 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001768 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001769
1770 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001771 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001772 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001773 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001774 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001775}
1776
1777EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1778 EGLClientBuffer buffer, const EGLint *attrib_list)
1779{
1780 clearError();
1781
Jesse Hallb29e5e82012-04-04 16:53:42 -07001782 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001783 if (!dp) return EGL_NO_IMAGE_KHR;
1784
Jesse Hallb29e5e82012-04-04 16:53:42 -07001785 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001786 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001787
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001788 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1789 egl_connection_t* const cnx = &gEGLImpl;
1790 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1791 result = cnx->egl.eglCreateImageKHR(
1792 dp->disp.dpy,
1793 c ? c->context : EGL_NO_CONTEXT,
1794 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001795 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001796 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001797}
1798
1799EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1800{
1801 clearError();
1802
Jesse Hallb29e5e82012-04-04 16:53:42 -07001803 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001804 if (!dp) return EGL_FALSE;
1805
Steven Holte646a5c52012-06-04 20:02:11 -07001806 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001807 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001808 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001809 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001810 }
Steven Holte646a5c52012-06-04 20:02:11 -07001811 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001812}
1813
1814// ----------------------------------------------------------------------------
1815// EGL_EGLEXT_VERSION 5
1816// ----------------------------------------------------------------------------
1817
1818
1819EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1820{
1821 clearError();
1822
Jesse Hallb29e5e82012-04-04 16:53:42 -07001823 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001824 if (!dp) return EGL_NO_SYNC_KHR;
1825
Mathias Agopian518ec112011-05-13 16:21:08 -07001826 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001827 egl_connection_t* const cnx = &gEGLImpl;
1828 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1829 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001830 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001831 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001832}
1833
1834EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1835{
1836 clearError();
1837
Jesse Hallb29e5e82012-04-04 16:53:42 -07001838 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001839 if (!dp) return EGL_FALSE;
1840
Mathias Agopian518ec112011-05-13 16:21:08 -07001841 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001842 egl_connection_t* const cnx = &gEGLImpl;
1843 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1844 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001845 }
1846 return result;
1847}
1848
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001849EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1850 clearError();
1851
1852 const egl_display_ptr dp = validate_display(dpy);
1853 if (!dp) return EGL_FALSE;
1854
1855 EGLBoolean result = EGL_FALSE;
1856 egl_connection_t* const cnx = &gEGLImpl;
1857 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1858 result = cnx->egl.eglSignalSyncKHR(
1859 dp->disp.dpy, sync, mode);
1860 }
1861 return result;
1862}
1863
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001864EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1865 EGLint flags, EGLTimeKHR timeout)
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 Agopian737b8962017-02-24 14:32:05 -08001872 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001873 egl_connection_t* const cnx = &gEGLImpl;
1874 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1875 result = cnx->egl.eglClientWaitSyncKHR(
1876 dp->disp.dpy, sync, flags, timeout);
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
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001881EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1882 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001883{
1884 clearError();
1885
Jesse Hallb29e5e82012-04-04 16:53:42 -07001886 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001887 if (!dp) return EGL_FALSE;
1888
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001889 EGLBoolean result = EGL_FALSE;
1890 egl_connection_t* const cnx = &gEGLImpl;
1891 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1892 result = cnx->egl.eglGetSyncAttribKHR(
1893 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001894 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001895 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001896}
1897
Season Li000d88f2015-07-01 11:39:40 -07001898EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1899{
1900 clearError();
1901
1902 const egl_display_ptr dp = validate_display(dpy);
1903 if (!dp) return EGL_NO_STREAM_KHR;
1904
1905 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1906 egl_connection_t* const cnx = &gEGLImpl;
1907 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1908 result = cnx->egl.eglCreateStreamKHR(
1909 dp->disp.dpy, attrib_list);
1910 }
1911 return result;
1912}
1913
1914EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
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.eglDestroyStreamKHR) {
1924 result = cnx->egl.eglDestroyStreamKHR(
1925 dp->disp.dpy, stream);
1926 }
1927 return result;
1928}
1929
1930EGLBoolean eglStreamAttribKHR(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.eglStreamAttribKHR) {
1941 result = cnx->egl.eglStreamAttribKHR(
1942 dp->disp.dpy, stream, attribute, value);
1943 }
1944 return result;
1945}
1946
1947EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1948 EGLenum attribute, EGLint *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.eglQueryStreamKHR) {
1958 result = cnx->egl.eglQueryStreamKHR(
1959 dp->disp.dpy, stream, attribute, value);
1960 }
1961 return result;
1962}
1963
1964EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1965 EGLenum attribute, EGLuint64KHR *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.eglQueryStreamu64KHR) {
1975 result = cnx->egl.eglQueryStreamu64KHR(
1976 dp->disp.dpy, stream, attribute, value);
1977 }
1978 return result;
1979}
1980
1981EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1982 EGLenum attribute, EGLTimeKHR *value)
1983{
1984 clearError();
1985
1986 const egl_display_ptr dp = validate_display(dpy);
1987 if (!dp) return EGL_FALSE;
1988
1989 EGLBoolean result = EGL_FALSE;
1990 egl_connection_t* const cnx = &gEGLImpl;
1991 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1992 result = cnx->egl.eglQueryStreamTimeKHR(
1993 dp->disp.dpy, stream, attribute, value);
1994 }
1995 return result;
1996}
1997
1998EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1999 EGLStreamKHR stream, const EGLint *attrib_list)
2000{
2001 clearError();
2002
2003 egl_display_ptr dp = validate_display(dpy);
2004 if (!dp) return EGL_NO_SURFACE;
2005
2006 egl_connection_t* const cnx = &gEGLImpl;
2007 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
2008 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
2009 dp->disp.dpy, config, stream, attrib_list);
2010 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07002011 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
2012 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07002013 return s;
2014 }
2015 }
2016 return EGL_NO_SURFACE;
2017}
2018
2019EGLBoolean eglStreamConsumerGLTextureExternalKHR(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.eglStreamConsumerGLTextureExternalKHR) {
2030 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
2031 dp->disp.dpy, stream);
2032 }
2033 return result;
2034}
2035
2036EGLBoolean eglStreamConsumerAcquireKHR(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.eglStreamConsumerAcquireKHR) {
2047 result = cnx->egl.eglStreamConsumerAcquireKHR(
2048 dp->disp.dpy, stream);
2049 }
2050 return result;
2051}
2052
2053EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2054 EGLStreamKHR stream)
2055{
2056 clearError();
2057
2058 const egl_display_ptr dp = validate_display(dpy);
2059 if (!dp) return EGL_FALSE;
2060
2061 EGLBoolean result = EGL_FALSE;
2062 egl_connection_t* const cnx = &gEGLImpl;
2063 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2064 result = cnx->egl.eglStreamConsumerReleaseKHR(
2065 dp->disp.dpy, stream);
2066 }
2067 return result;
2068}
2069
2070EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2071 EGLDisplay dpy, EGLStreamKHR stream)
2072{
2073 clearError();
2074
2075 const egl_display_ptr dp = validate_display(dpy);
2076 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2077
2078 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2079 egl_connection_t* const cnx = &gEGLImpl;
2080 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2081 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2082 dp->disp.dpy, stream);
2083 }
2084 return result;
2085}
2086
2087EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2088 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2089{
2090 clearError();
2091
2092 const egl_display_ptr dp = validate_display(dpy);
2093 if (!dp) return EGL_NO_STREAM_KHR;
2094
2095 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2096 egl_connection_t* const cnx = &gEGLImpl;
2097 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2098 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2099 dp->disp.dpy, file_descriptor);
2100 }
2101 return result;
2102}
2103
Mathias Agopian518ec112011-05-13 16:21:08 -07002104// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002105// EGL_EGLEXT_VERSION 15
2106// ----------------------------------------------------------------------------
2107
2108EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2109 clearError();
2110 const egl_display_ptr dp = validate_display(dpy);
2111 if (!dp) return EGL_FALSE;
2112 EGLint result = EGL_FALSE;
2113 egl_connection_t* const cnx = &gEGLImpl;
2114 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2115 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2116 }
2117 return result;
2118}
2119
2120// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002121// ANDROID extensions
2122// ----------------------------------------------------------------------------
2123
Jamie Gennis331841b2012-09-06 14:52:00 -07002124EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2125{
2126 clearError();
2127
2128 const egl_display_ptr dp = validate_display(dpy);
2129 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2130
2131 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2132 egl_connection_t* const cnx = &gEGLImpl;
2133 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2134 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2135 }
2136 return result;
2137}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002138
Andy McFadden72841452013-03-01 16:25:32 -08002139EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2140 EGLnsecsANDROID time)
2141{
2142 clearError();
2143
2144 const egl_display_ptr dp = validate_display(dpy);
2145 if (!dp) {
2146 return EGL_FALSE;
2147 }
2148
2149 SurfaceRef _s(dp.get(), surface);
2150 if (!_s.get()) {
2151 setError(EGL_BAD_SURFACE, EGL_FALSE);
2152 return EGL_FALSE;
2153 }
2154
2155 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002156 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002157
2158 return EGL_TRUE;
2159}
2160
Craig Donner60761072017-01-27 12:30:44 -08002161EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2162 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002163 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2164 // this function cannot be implemented when this libEGL is built for
2165 // vendors.
2166#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002167 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002168 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002169#else
2170 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2171#endif
Craig Donner60761072017-01-27 12:30:44 -08002172}
2173
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002174// ----------------------------------------------------------------------------
2175// NVIDIA extensions
2176// ----------------------------------------------------------------------------
2177EGLuint64NV eglGetSystemTimeFrequencyNV()
2178{
2179 clearError();
2180
2181 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002182 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002183 }
2184
2185 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002186 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002187
Mathias Agopianada798b2012-02-13 17:09:30 -08002188 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2189 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002190 }
2191
Mathias Agopian737b8962017-02-24 14:32:05 -08002192 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002193}
2194
2195EGLuint64NV eglGetSystemTimeNV()
2196{
2197 clearError();
2198
2199 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002200 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002201 }
2202
2203 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002204 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002205
Mathias Agopianada798b2012-02-13 17:09:30 -08002206 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2207 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002208 }
2209
Mathias Agopian737b8962017-02-24 14:32:05 -08002210 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002211}
Dan Stozaa894d082015-02-19 15:27:36 -08002212
2213// ----------------------------------------------------------------------------
2214// Partial update extension
2215// ----------------------------------------------------------------------------
2216EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2217 EGLint *rects, EGLint n_rects)
2218{
2219 clearError();
2220
2221 const egl_display_ptr dp = validate_display(dpy);
2222 if (!dp) {
2223 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2224 return EGL_FALSE;
2225 }
2226
2227 SurfaceRef _s(dp.get(), surface);
2228 if (!_s.get()) {
2229 setError(EGL_BAD_SURFACE, EGL_FALSE);
2230 return EGL_FALSE;
2231 }
2232
2233 egl_surface_t const * const s = get_surface(surface);
2234 if (s->cnx->egl.eglSetDamageRegionKHR) {
2235 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2236 rects, n_rects);
2237 }
2238
2239 return EGL_FALSE;
2240}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002241
Brian Anderson1049d1d2016-12-16 17:25:57 -08002242EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2243 EGLuint64KHR *frameId) {
2244 clearError();
2245
2246 const egl_display_ptr dp = validate_display(dpy);
2247 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002248 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002249 }
2250
2251 SurfaceRef _s(dp.get(), surface);
2252 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002253 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002254 }
2255
2256 egl_surface_t const * const s = get_surface(surface);
2257
Mathias Agopian65421432017-03-08 11:49:05 -08002258 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002259 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002260 }
2261
2262 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002263 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002264
Mathias Agopian65421432017-03-08 11:49:05 -08002265 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002266 // This should not happen. Return an error that is not in the spec
2267 // so it's obvious something is very wrong.
2268 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002269 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002270 }
2271
2272 *frameId = nextFrameId;
2273 return EGL_TRUE;
2274}
2275
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002276EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2277 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2278{
2279 clearError();
2280
2281 const egl_display_ptr dp = validate_display(dpy);
2282 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002283 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002284 }
2285
2286 SurfaceRef _s(dp.get(), surface);
2287 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002288 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002289 }
2290
2291 egl_surface_t const * const s = get_surface(surface);
2292
Mathias Agopian65421432017-03-08 11:49:05 -08002293 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002294 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002295 }
2296
2297 nsecs_t* compositeDeadline = nullptr;
2298 nsecs_t* compositeInterval = nullptr;
2299 nsecs_t* compositeToPresentLatency = nullptr;
2300
2301 for (int i = 0; i < numTimestamps; i++) {
2302 switch (names[i]) {
2303 case EGL_COMPOSITE_DEADLINE_ANDROID:
2304 compositeDeadline = &values[i];
2305 break;
2306 case EGL_COMPOSITE_INTERVAL_ANDROID:
2307 compositeInterval = &values[i];
2308 break;
2309 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2310 compositeToPresentLatency = &values[i];
2311 break;
2312 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002313 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002314 }
2315 }
2316
Mathias Agopian65421432017-03-08 11:49:05 -08002317 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002318 compositeDeadline, compositeInterval, compositeToPresentLatency);
2319
2320 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002321 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002322 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002323 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002324 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002325 default:
2326 // This should not happen. Return an error that is not in the spec
2327 // so it's obvious something is very wrong.
2328 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002329 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002330 }
2331}
2332
2333EGLBoolean eglGetCompositorTimingSupportedANDROID(
2334 EGLDisplay dpy, EGLSurface surface, EGLint name)
2335{
2336 clearError();
2337
2338 const egl_display_ptr dp = validate_display(dpy);
2339 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002340 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002341 }
2342
2343 SurfaceRef _s(dp.get(), surface);
2344 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002345 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002346 }
2347
2348 egl_surface_t const * const s = get_surface(surface);
2349
Mathias Agopian65421432017-03-08 11:49:05 -08002350 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002351 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002352 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002353 }
2354
2355 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002356 case EGL_COMPOSITE_DEADLINE_ANDROID:
2357 case EGL_COMPOSITE_INTERVAL_ANDROID:
2358 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2359 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002360 default:
2361 return EGL_FALSE;
2362 }
2363}
2364
Pablo Ceballosc18be292016-05-31 14:55:42 -07002365EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002366 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002367 EGLnsecsANDROID *values)
2368{
2369 clearError();
2370
2371 const egl_display_ptr dp = validate_display(dpy);
2372 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002373 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002374 }
2375
2376 SurfaceRef _s(dp.get(), surface);
2377 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002378 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002379 }
2380
2381 egl_surface_t const * const s = get_surface(surface);
2382
Mathias Agopian65421432017-03-08 11:49:05 -08002383 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002384 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002385 }
2386
Brian Andersondbd0ea82016-07-22 09:38:59 -07002387 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002388 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002389 nsecs_t* latchTime = nullptr;
2390 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002391 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002392 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002393 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002394 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002395 nsecs_t* releaseTime = nullptr;
2396
2397 for (int i = 0; i < numTimestamps; i++) {
2398 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002399 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2400 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002401 break;
2402 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2403 acquireTime = &values[i];
2404 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002405 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2406 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002407 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002408 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2409 firstRefreshStartTime = &values[i];
2410 break;
2411 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2412 lastRefreshStartTime = &values[i];
2413 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002414 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2415 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002416 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002417 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2418 displayPresentTime = &values[i];
2419 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002420 case EGL_DEQUEUE_READY_TIME_ANDROID:
2421 dequeueReadyTime = &values[i];
2422 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002423 case EGL_READS_DONE_TIME_ANDROID:
2424 releaseTime = &values[i];
2425 break;
2426 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002427 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002428 }
2429 }
2430
Mathias Agopian65421432017-03-08 11:49:05 -08002431 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002432 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002433 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002434 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002435
Brian Anderson069b3652016-07-22 10:32:47 -07002436 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002437 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002438 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002439 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002440 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002441 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002442 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002443 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002444 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2445 default:
2446 // This should not happen. Return an error that is not in the spec
2447 // so it's obvious something is very wrong.
2448 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2449 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002450 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002451}
2452
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002453EGLBoolean eglGetFrameTimestampSupportedANDROID(
2454 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002455{
2456 clearError();
2457
2458 const egl_display_ptr dp = validate_display(dpy);
2459 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002460 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002461 }
2462
2463 SurfaceRef _s(dp.get(), surface);
2464 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002465 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002466 }
2467
2468 egl_surface_t const * const s = get_surface(surface);
2469
Mathias Agopian65421432017-03-08 11:49:05 -08002470 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002471 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002472 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002473 }
2474
2475 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002476 case EGL_COMPOSITE_DEADLINE_ANDROID:
2477 case EGL_COMPOSITE_INTERVAL_ANDROID:
2478 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002479 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002480 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002481 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2482 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2483 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002484 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002485 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002486 case EGL_READS_DONE_TIME_ANDROID:
2487 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002488 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2489 int value = 0;
2490 window->query(window,
2491 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2492 return value == 0 ? EGL_FALSE : EGL_TRUE;
2493 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002494 default:
2495 return EGL_FALSE;
2496 }
2497}