blob: 46e7a97e2c678e82d15075d5069bb9b3d13ed134 [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 Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700703EGLBoolean setSurfaceMetadata(egl_surface_t* s, NativeWindowType window,
704 const EGLint *attrib_list) {
705 // set any HDR metadata
706 bool smpte2086 = false;
707 bool cta8613 = false;
708 if (attrib_list == nullptr) return EGL_TRUE;
709
710 for (const EGLint* attr = attrib_list; attr[0] != EGL_NONE; attr += 2) {
711 smpte2086 |= s->setSmpte2086Attribute(attr[0], attr[1]);
712 cta8613 |= s->setCta8613Attribute(attr[0], attr[1]);
713 }
714 if (smpte2086) {
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -0800715 android_smpte2086_metadata metadata = s->getSmpte2086Metadata();
716 int err = native_window_set_buffers_smpte2086_metadata(window, &metadata);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700717 if (err != 0) {
718 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
719 strerror(-err), err);
720 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
721 return EGL_FALSE;
722 }
723 }
724 if (cta8613) {
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -0800725 android_cta861_3_metadata metadata = s->getCta8613Metadata();
726 int err = native_window_set_buffers_cta861_3_metadata(window, &metadata);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700727 if (err != 0) {
728 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
729 strerror(-err), err);
730 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
731 return EGL_FALSE;
732 }
733 }
734 return EGL_TRUE;
735}
736
Mathias Agopian518ec112011-05-13 16:21:08 -0700737EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
738 NativeWindowType window,
739 const EGLint *attrib_list)
740{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700741 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700742 clearError();
743
Jesse Hallb29e5e82012-04-04 16:53:42 -0700744 egl_connection_t* cnx = NULL;
745 egl_display_ptr dp = validate_display_connection(dpy, cnx);
746 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800747 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700748
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800749 if (!window) {
750 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
751 }
752
753 int value = 0;
754 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
755 if (!value) {
756 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
757 }
758
Andy McFaddend566ce32014-01-07 15:54:17 -0800759 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800760 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800761 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
762 "failed (%#x) (already connected to another API?)",
763 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700764 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700765 }
766
Mathias Agopian95921562017-02-24 14:31:31 -0800767 EGLint format;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600768 getNativePixelFormat(iDpy, cnx, config, format);
769
770 // now select correct colorspace and dataspace based on user's attribute list
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600771 EGLint colorSpace;
772 android_dataspace dataSpace;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600773 if (!getColorSpaceAttribute(dp, window, attrib_list, colorSpace, dataSpace)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600774 ALOGE("error invalid colorspace: %d", colorSpace);
775 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700776 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800777
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600778 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700779 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600780 // Had to modify the attribute list due to use of color space.
781 // Use modified list from here on.
782 attrib_list = strippedAttribList.data();
783 }
784
Jesse Hallc2e41222013-08-08 13:40:22 -0700785 if (format != 0) {
786 int err = native_window_set_buffers_format(window, format);
787 if (err != 0) {
788 ALOGE("error setting native window pixel format: %s (%d)",
789 strerror(-err), err);
790 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
791 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
792 }
793 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700794
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800795 if (dataSpace != 0) {
796 int err = native_window_set_buffers_data_space(window, dataSpace);
797 if (err != 0) {
798 ALOGE("error setting native window pixel dataSpace: %s (%d)",
799 strerror(-err), err);
800 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
801 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
802 }
803 }
804
Jamie Gennis59769462011-11-19 18:04:43 -0800805 // the EGL spec requires that a new EGLSurface default to swap interval
806 // 1, so explicitly set that on the window here.
807 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
808 anw->setSwapInterval(anw, 1);
809
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800811 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700812 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600813 egl_surface_t* s =
814 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700815
816 if (setSurfaceMetadata(s, window, origAttribList)) {
817 return s;
818 }
819 eglDestroySurface(dpy, s);
Mathias Agopian518ec112011-05-13 16:21:08 -0700820 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700821
822 // EGLSurface creation failed
823 native_window_set_buffers_format(window, 0);
824 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700825 }
826 return EGL_NO_SURFACE;
827}
828
829EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
830 NativePixmapType pixmap,
831 const EGLint *attrib_list)
832{
833 clearError();
834
Jesse Hallb29e5e82012-04-04 16:53:42 -0700835 egl_connection_t* cnx = NULL;
836 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600837 EGLint colorSpace;
838 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700839 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600840 // now select a corresponding sRGB format if needed
841 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
842 ALOGE("error invalid colorspace: %d", colorSpace);
843 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
844 }
845
Mathias Agopian518ec112011-05-13 16:21:08 -0700846 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800847 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700848 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600849 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700850 return s;
851 }
852 }
853 return EGL_NO_SURFACE;
854}
855
856EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
857 const EGLint *attrib_list)
858{
859 clearError();
860
Jesse Hallb29e5e82012-04-04 16:53:42 -0700861 egl_connection_t* cnx = NULL;
862 egl_display_ptr dp = validate_display_connection(dpy, cnx);
863 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600864 EGLDisplay iDpy = dp->disp.dpy;
865 EGLint format;
866 getNativePixelFormat(iDpy, cnx, config, format);
867
868 // now select correct colorspace and dataspace based on user's attribute list
869 EGLint colorSpace;
870 android_dataspace dataSpace;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600871 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
872 ALOGE("error invalid colorspace: %d", colorSpace);
873 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
874 }
875
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600876 // Pbuffers are not displayed so we don't need to store the
877 // colorspace. We do need to filter out color spaces the
878 // driver doesn't know how to process.
879 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700880 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600881 // Had to modify the attribute list due to use of color space.
882 // Use modified list from here on.
883 attrib_list = strippedAttribList.data();
884 }
885
Mathias Agopian518ec112011-05-13 16:21:08 -0700886 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800887 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700888 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600889 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700890 return s;
891 }
892 }
893 return EGL_NO_SURFACE;
894}
Jesse Hall47743382013-02-08 11:13:46 -0800895
Mathias Agopian518ec112011-05-13 16:21:08 -0700896EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
897{
898 clearError();
899
Jesse Hallb29e5e82012-04-04 16:53:42 -0700900 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700901 if (!dp) return EGL_FALSE;
902
Jesse Hallb29e5e82012-04-04 16:53:42 -0700903 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700904 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800905 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700906
907 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800908 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700909 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700910 _s.terminate();
911 }
912 return result;
913}
914
915EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
916 EGLint attribute, EGLint *value)
917{
918 clearError();
919
Jesse Hallb29e5e82012-04-04 16:53:42 -0700920 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700921 if (!dp) return EGL_FALSE;
922
Jesse Hallb29e5e82012-04-04 16:53:42 -0700923 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700924 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800925 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700926
Mathias Agopian518ec112011-05-13 16:21:08 -0700927 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700928 if (s->getColorSpaceAttribute(attribute, value)) {
929 return EGL_TRUE;
930 } else if (s->getSmpte2086Attribute(attribute, value)) {
931 return EGL_TRUE;
932 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600933 return EGL_TRUE;
934 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700935 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700936}
937
Jamie Gennise8696a42012-01-15 18:54:57 -0800938void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800939 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800940 clearError();
941
Jesse Hallb29e5e82012-04-04 16:53:42 -0700942 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800943 if (!dp) {
944 return;
945 }
946
Jesse Hallb29e5e82012-04-04 16:53:42 -0700947 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800948 if (!_s.get()) {
949 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800950 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800951}
952
Mathias Agopian518ec112011-05-13 16:21:08 -0700953// ----------------------------------------------------------------------------
954// Contexts
955// ----------------------------------------------------------------------------
956
957EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
958 EGLContext share_list, const EGLint *attrib_list)
959{
960 clearError();
961
Jesse Hallb29e5e82012-04-04 16:53:42 -0700962 egl_connection_t* cnx = NULL;
963 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700964 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700965 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700966 if (!ContextRef(dp.get(), share_list).get()) {
967 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
968 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 egl_context_t* const c = get_context(share_list);
970 share_list = c->context;
971 }
972 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800973 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700974 if (context != EGL_NO_CONTEXT) {
975 // figure out if it's a GLESv1 or GLESv2
976 int version = 0;
977 if (attrib_list) {
978 while (*attrib_list != EGL_NONE) {
979 GLint attr = *attrib_list++;
980 GLint value = *attrib_list++;
981 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
982 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800983 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800984 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800985 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700986 }
987 }
988 };
989 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700990 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
991 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700992 return c;
993 }
994 }
995 return EGL_NO_CONTEXT;
996}
997
998EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
999{
1000 clearError();
1001
Jesse Hallb29e5e82012-04-04 16:53:42 -07001002 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001003 if (!dp)
1004 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001005
Jesse Hallb29e5e82012-04-04 16:53:42 -07001006 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001007 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001008 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -08001009
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -08001011 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -07001012 if (result == EGL_TRUE) {
1013 _c.terminate();
1014 }
1015 return result;
1016}
1017
Mathias Agopian518ec112011-05-13 16:21:08 -07001018EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1019 EGLSurface read, EGLContext ctx)
1020{
1021 clearError();
1022
Jesse Hallb29e5e82012-04-04 16:53:42 -07001023 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -08001024 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001025
Mathias Agopian5b287a62011-05-16 18:58:55 -07001026 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
1027 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
1028 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -07001029 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
1030 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001031 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001032 }
1033
1034 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -07001035 ContextRef _c(dp.get(), ctx);
1036 SurfaceRef _d(dp.get(), draw);
1037 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001038
1039 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -07001040 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001041 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -08001042 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001043 }
1044
1045 // these are the underlying implementation's object
1046 EGLContext impl_ctx = EGL_NO_CONTEXT;
1047 EGLSurface impl_draw = EGL_NO_SURFACE;
1048 EGLSurface impl_read = EGL_NO_SURFACE;
1049
1050 // these are our objects structs passed in
1051 egl_context_t * c = NULL;
1052 egl_surface_t const * d = NULL;
1053 egl_surface_t const * r = NULL;
1054
1055 // these are the current objects structs
1056 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001057
Mathias Agopian518ec112011-05-13 16:21:08 -07001058 if (ctx != EGL_NO_CONTEXT) {
1059 c = get_context(ctx);
1060 impl_ctx = c->context;
1061 } else {
1062 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001063 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1064 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001065 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001066 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001067 if (cur_c == NULL) {
1068 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001069 // not an error, there is just no current context.
1070 return EGL_TRUE;
1071 }
1072 }
1073
1074 // retrieve the underlying implementation's draw EGLSurface
1075 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001076 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001077 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001078 impl_draw = d->surface;
1079 }
1080
1081 // retrieve the underlying implementation's read EGLSurface
1082 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001083 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001084 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001085 impl_read = r->surface;
1086 }
1087
Mathias Agopian518ec112011-05-13 16:21:08 -07001088
Jesse Hallb29e5e82012-04-04 16:53:42 -07001089 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001090 draw, read, ctx,
1091 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001092
1093 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001094 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001095 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1096 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001097 _c.acquire();
1098 _r.acquire();
1099 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001100 } else {
1101 setGLHooksThreadSpecific(&gHooksNoContext);
1102 egl_tls_t::setContext(EGL_NO_CONTEXT);
1103 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001104 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001105
Mike Stroyan47e741b2017-06-01 13:56:18 -06001106 if (cur_c != NULL) {
1107 // Force return to current context for drivers that cannot handle errors
1108 EGLBoolean restore_result = EGL_FALSE;
1109 // get a reference to the old current objects
1110 ContextRef _c2(dp.get(), cur_c);
1111 SurfaceRef _d2(dp.get(), cur_c->draw);
1112 SurfaceRef _r2(dp.get(), cur_c->read);
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001113
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001114 c = cur_c;
1115 impl_ctx = c->context;
1116 impl_draw = EGL_NO_SURFACE;
1117 if (cur_c->draw != EGL_NO_SURFACE) {
1118 d = get_surface(cur_c->draw);
1119 impl_draw = d->surface;
1120 }
1121 impl_read = EGL_NO_SURFACE;
1122 if (cur_c->read != EGL_NO_SURFACE) {
1123 r = get_surface(cur_c->read);
1124 impl_read = r->surface;
1125 }
1126 restore_result = dp->makeCurrent(c, cur_c,
1127 cur_c->draw, cur_c->read, cur_c->context,
1128 impl_draw, impl_read, impl_ctx);
Mike Stroyan47e741b2017-06-01 13:56:18 -06001129 if (restore_result == EGL_TRUE) {
1130 _c2.acquire();
1131 _r2.acquire();
1132 _d2.acquire();
1133 } else {
1134 ALOGE("Could not restore original EGL context");
1135 }
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001136 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001137 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001138 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001139 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001140 }
1141 return result;
1142}
1143
1144
1145EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1146 EGLint attribute, EGLint *value)
1147{
1148 clearError();
1149
Jesse Hallb29e5e82012-04-04 16:53:42 -07001150 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001151 if (!dp) return EGL_FALSE;
1152
Jesse Hallb29e5e82012-04-04 16:53:42 -07001153 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001154 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001155
Mathias Agopian518ec112011-05-13 16:21:08 -07001156 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001157 return c->cnx->egl.eglQueryContext(
1158 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001159
Mathias Agopian518ec112011-05-13 16:21:08 -07001160}
1161
1162EGLContext eglGetCurrentContext(void)
1163{
1164 // could be called before eglInitialize(), but we wouldn't have a context
1165 // then, and this function would correctly return EGL_NO_CONTEXT.
1166
1167 clearError();
1168
1169 EGLContext ctx = getContext();
1170 return ctx;
1171}
1172
1173EGLSurface eglGetCurrentSurface(EGLint readdraw)
1174{
1175 // could be called before eglInitialize(), but we wouldn't have a context
1176 // then, and this function would correctly return EGL_NO_SURFACE.
1177
1178 clearError();
1179
1180 EGLContext ctx = getContext();
1181 if (ctx) {
1182 egl_context_t const * const c = get_context(ctx);
1183 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1184 switch (readdraw) {
1185 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001186 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001187 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1188 }
1189 }
1190 return EGL_NO_SURFACE;
1191}
1192
1193EGLDisplay eglGetCurrentDisplay(void)
1194{
1195 // could be called before eglInitialize(), but we wouldn't have a context
1196 // then, and this function would correctly return EGL_NO_DISPLAY.
1197
1198 clearError();
1199
1200 EGLContext ctx = getContext();
1201 if (ctx) {
1202 egl_context_t const * const c = get_context(ctx);
1203 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1204 return c->dpy;
1205 }
1206 return EGL_NO_DISPLAY;
1207}
1208
1209EGLBoolean eglWaitGL(void)
1210{
Mathias Agopian518ec112011-05-13 16:21:08 -07001211 clearError();
1212
Mathias Agopianada798b2012-02-13 17:09:30 -08001213 egl_connection_t* const cnx = &gEGLImpl;
1214 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001215 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001216
1217 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001218}
1219
1220EGLBoolean eglWaitNative(EGLint engine)
1221{
Mathias Agopian518ec112011-05-13 16:21:08 -07001222 clearError();
1223
Mathias Agopianada798b2012-02-13 17:09:30 -08001224 egl_connection_t* const cnx = &gEGLImpl;
1225 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001226 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001227
1228 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001229}
1230
1231EGLint eglGetError(void)
1232{
Mathias Agopianada798b2012-02-13 17:09:30 -08001233 EGLint err = EGL_SUCCESS;
1234 egl_connection_t* const cnx = &gEGLImpl;
1235 if (cnx->dso) {
1236 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001237 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001238 if (err == EGL_SUCCESS) {
1239 err = egl_tls_t::getError();
1240 }
1241 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001242}
1243
Michael Chockc0ec5e22014-01-27 08:14:33 -08001244static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001245 const char* procname) {
1246 const egl_connection_t* cnx = &gEGLImpl;
1247 void* proc = NULL;
1248
Michael Chockc0ec5e22014-01-27 08:14:33 -08001249 proc = dlsym(cnx->libEgl, procname);
1250 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1251
Jesse Hallc07b5202013-07-04 12:08:16 -07001252 proc = dlsym(cnx->libGles2, procname);
1253 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1254
1255 proc = dlsym(cnx->libGles1, procname);
1256 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1257
1258 return NULL;
1259}
1260
Mathias Agopian518ec112011-05-13 16:21:08 -07001261__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1262{
1263 // eglGetProcAddress() could be the very first function called
1264 // in which case we must make sure we've initialized ourselves, this
1265 // happens the first time egl_get_display() is called.
1266
1267 clearError();
1268
1269 if (egl_init_drivers() == EGL_FALSE) {
1270 setError(EGL_BAD_PARAMETER, NULL);
1271 return NULL;
1272 }
1273
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001274 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001275 return NULL;
1276 }
1277
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001279 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001280 if (addr) return addr;
1281
Michael Chockc0ec5e22014-01-27 08:14:33 -08001282 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001283 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001284
Mathias Agopian518ec112011-05-13 16:21:08 -07001285 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1286 pthread_mutex_lock(&sExtensionMapMutex);
1287
1288 /*
1289 * Since eglGetProcAddress() is not associated to anything, it needs
1290 * to return a function pointer that "works" regardless of what
1291 * the current context is.
1292 *
1293 * For this reason, we return a "forwarder", a small stub that takes
1294 * care of calling the function associated with the context
1295 * currently bound.
1296 *
1297 * We first look for extensions we've already resolved, if we're seeing
1298 * this extension for the first time, we go through all our
1299 * implementations and call eglGetProcAddress() and record the
1300 * result in the appropriate implementation hooks and return the
1301 * address of the forwarder corresponding to that hook set.
1302 *
1303 */
1304
Mathias Agopian65421432017-03-08 11:49:05 -08001305 const std::string name(procname);
1306
1307 auto& extentionMap = sGLExtentionMap;
1308 auto pos = extentionMap.find(name);
1309 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001310 const int slot = sGLExtentionSlot;
1311
Steve Blocke6f43dd2012-01-06 19:20:56 +00001312 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001313 "no more slots for eglGetProcAddress(\"%s\")",
1314 procname);
1315
1316 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1317 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001318
1319 egl_connection_t* const cnx = &gEGLImpl;
1320 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001321 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001322 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001323 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1324 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001325 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001326 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001327 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001328
Mathias Agopian518ec112011-05-13 16:21:08 -07001329 if (found) {
1330 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001331 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001332 sGLExtentionSlot++;
1333 }
1334 }
1335
1336 pthread_mutex_unlock(&sExtensionMapMutex);
1337 return addr;
1338}
1339
Mathias Agopian65421432017-03-08 11:49:05 -08001340class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001341public:
1342
1343 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001344 static FrameCompletionThread thread;
1345
1346 char name[64];
1347
1348 std::lock_guard<std::mutex> lock(thread.mMutex);
1349 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1350 ATRACE_NAME(name);
1351
1352 thread.mQueue.push_back(sync);
1353 thread.mCondition.notify_one();
1354 thread.mFramesQueued++;
1355 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001356 }
1357
1358private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001359
Mathias Agopian65421432017-03-08 11:49:05 -08001360 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1361 std::thread thread(&FrameCompletionThread::loop, this);
1362 thread.detach();
1363 }
1364
1365#pragma clang diagnostic push
1366#pragma clang diagnostic ignored "-Wmissing-noreturn"
1367 void loop() {
1368 while (true) {
1369 threadLoop();
1370 }
1371 }
1372#pragma clang diagnostic pop
1373
1374 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001375 EGLSyncKHR sync;
1376 uint32_t frameNum;
1377 {
Mathias Agopian65421432017-03-08 11:49:05 -08001378 std::unique_lock<std::mutex> lock(mMutex);
1379 while (mQueue.empty()) {
1380 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001381 }
1382 sync = mQueue[0];
1383 frameNum = mFramesCompleted;
1384 }
1385 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1386 {
Mathias Agopian65421432017-03-08 11:49:05 -08001387 char name[64];
1388 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1389 ATRACE_NAME(name);
1390
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001391 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1392 if (result == EGL_FALSE) {
1393 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1394 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1395 ALOGE("FrameCompletion: timeout waiting for fence");
1396 }
1397 eglDestroySyncKHR(dpy, sync);
1398 }
1399 {
Mathias Agopian65421432017-03-08 11:49:05 -08001400 std::lock_guard<std::mutex> lock(mMutex);
1401 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001402 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001403 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001404 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001405 }
1406
1407 uint32_t mFramesQueued;
1408 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001409 std::deque<EGLSyncKHR> mQueue;
1410 std::condition_variable mCondition;
1411 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001412};
1413
Dan Stozaa894d082015-02-19 15:27:36 -08001414EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1415 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001416{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001417 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001418 clearError();
1419
Jesse Hallb29e5e82012-04-04 16:53:42 -07001420 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001421 if (!dp) return EGL_FALSE;
1422
Jesse Hallb29e5e82012-04-04 16:53:42 -07001423 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001424 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001425 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001426
Mathias Agopian518ec112011-05-13 16:21:08 -07001427 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001428
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001429 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1430 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1431 if (sync != EGL_NO_SYNC_KHR) {
1432 FrameCompletionThread::queueSync(sync);
1433 }
1434 }
1435
Mathias Agopian7db993a2012-03-25 00:49:46 -07001436 if (CC_UNLIKELY(dp->finishOnSwap)) {
1437 uint32_t pixel;
1438 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1439 if (c) {
1440 // glReadPixels() ensures that the frame is complete
1441 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1442 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1443 }
1444 }
1445
Dan Stozaa894d082015-02-19 15:27:36 -08001446 if (n_rects == 0) {
1447 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1448 }
1449
Mathias Agopian65421432017-03-08 11:49:05 -08001450 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001451 for (int r = 0; r < n_rects; ++r) {
1452 int offset = r * 4;
1453 int x = rects[offset];
1454 int y = rects[offset + 1];
1455 int width = rects[offset + 2];
1456 int height = rects[offset + 3];
1457 android_native_rect_t androidRect;
1458 androidRect.left = x;
1459 androidRect.top = y + height;
1460 androidRect.right = x + width;
1461 androidRect.bottom = y;
1462 androidRects.push_back(androidRect);
1463 }
Mathias Agopian65421432017-03-08 11:49:05 -08001464 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001465
1466 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1467 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1468 rects, n_rects);
1469 } else {
1470 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1471 }
1472}
1473
1474EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1475{
1476 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001477}
1478
1479EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1480 NativePixmapType target)
1481{
1482 clearError();
1483
Jesse Hallb29e5e82012-04-04 16:53:42 -07001484 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001485 if (!dp) return EGL_FALSE;
1486
Jesse Hallb29e5e82012-04-04 16:53:42 -07001487 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001488 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001489 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001490
Mathias Agopian518ec112011-05-13 16:21:08 -07001491 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001492 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001493}
1494
1495const char* eglQueryString(EGLDisplay dpy, EGLint name)
1496{
1497 clearError();
1498
Chia-I Wue57d1352016-08-15 16:10:02 +08001499 // Generate an error quietly when client extensions (as defined by
1500 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1501 // validate_display to generate the error as validate_display would log
1502 // the error, which can be misleading.
1503 //
1504 // If we want to support EGL_EXT_client_extensions later, we can return
1505 // the client extension string here instead.
1506 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001507 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001508
Jesse Hallb29e5e82012-04-04 16:53:42 -07001509 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001510 if (!dp) return (const char *) NULL;
1511
1512 switch (name) {
1513 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001514 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001515 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001516 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001517 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001518 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001519 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001520 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001521 default:
1522 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001523 }
1524 return setError(EGL_BAD_PARAMETER, (const char *)0);
1525}
1526
Jiyong Park00b15b82017-08-10 20:30:56 +09001527extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001528{
1529 clearError();
1530
1531 const egl_display_ptr dp = validate_display(dpy);
1532 if (!dp) return (const char *) NULL;
1533
1534 switch (name) {
1535 case EGL_VENDOR:
1536 return dp->disp.queryString.vendor;
1537 case EGL_VERSION:
1538 return dp->disp.queryString.version;
1539 case EGL_EXTENSIONS:
1540 return dp->disp.queryString.extensions;
1541 case EGL_CLIENT_APIS:
1542 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001543 default:
1544 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001545 }
1546 return setError(EGL_BAD_PARAMETER, (const char *)0);
1547}
Mathias Agopian518ec112011-05-13 16:21:08 -07001548
1549// ----------------------------------------------------------------------------
1550// EGL 1.1
1551// ----------------------------------------------------------------------------
1552
1553EGLBoolean eglSurfaceAttrib(
1554 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1555{
1556 clearError();
1557
Jesse Hallb29e5e82012-04-04 16:53:42 -07001558 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001559 if (!dp) return EGL_FALSE;
1560
Jesse Hallb29e5e82012-04-04 16:53:42 -07001561 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001562 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001563 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001564
Pablo Ceballosc18be292016-05-31 14:55:42 -07001565 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001566
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001567 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001568 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001569 setError(EGL_BAD_SURFACE, EGL_FALSE);
1570 }
Mathias Agopian65421432017-03-08 11:49:05 -08001571 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1572 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001573 }
1574
Pablo Ceballosc18be292016-05-31 14:55:42 -07001575 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001576 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001577 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001578 }
Mathias Agopian65421432017-03-08 11:49:05 -08001579 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1580 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001581 }
1582
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001583 if (s->setSmpte2086Attribute(attribute, value)) {
1584 return EGL_TRUE;
1585 } else if (s->setCta8613Attribute(attribute, value)) {
1586 return EGL_TRUE;
1587 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001588 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001589 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001590 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001591 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001592}
1593
1594EGLBoolean eglBindTexImage(
1595 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1596{
1597 clearError();
1598
Jesse Hallb29e5e82012-04-04 16:53:42 -07001599 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001600 if (!dp) return EGL_FALSE;
1601
Jesse Hallb29e5e82012-04-04 16:53:42 -07001602 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001603 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001604 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001605
Mathias Agopian518ec112011-05-13 16:21:08 -07001606 egl_surface_t const * const s = get_surface(surface);
1607 if (s->cnx->egl.eglBindTexImage) {
1608 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001609 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001610 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001611 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001612}
1613
1614EGLBoolean eglReleaseTexImage(
1615 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1616{
1617 clearError();
1618
Jesse Hallb29e5e82012-04-04 16:53:42 -07001619 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001620 if (!dp) return EGL_FALSE;
1621
Jesse Hallb29e5e82012-04-04 16:53:42 -07001622 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001623 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001624 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001625
Mathias Agopian518ec112011-05-13 16:21:08 -07001626 egl_surface_t const * const s = get_surface(surface);
1627 if (s->cnx->egl.eglReleaseTexImage) {
1628 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001629 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001630 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001631 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001632}
1633
1634EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1635{
1636 clearError();
1637
Jesse Hallb29e5e82012-04-04 16:53:42 -07001638 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001639 if (!dp) return EGL_FALSE;
1640
1641 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001642 egl_connection_t* const cnx = &gEGLImpl;
1643 if (cnx->dso && cnx->egl.eglSwapInterval) {
1644 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001645 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001646
Mathias Agopian518ec112011-05-13 16:21:08 -07001647 return res;
1648}
1649
1650
1651// ----------------------------------------------------------------------------
1652// EGL 1.2
1653// ----------------------------------------------------------------------------
1654
1655EGLBoolean eglWaitClient(void)
1656{
1657 clearError();
1658
Mathias Agopianada798b2012-02-13 17:09:30 -08001659 egl_connection_t* const cnx = &gEGLImpl;
1660 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001661 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001662
1663 EGLBoolean res;
1664 if (cnx->egl.eglWaitClient) {
1665 res = cnx->egl.eglWaitClient();
1666 } else {
1667 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001668 }
1669 return res;
1670}
1671
1672EGLBoolean eglBindAPI(EGLenum api)
1673{
1674 clearError();
1675
1676 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001677 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001678 }
1679
1680 // bind this API on all EGLs
1681 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001682 egl_connection_t* const cnx = &gEGLImpl;
1683 if (cnx->dso && cnx->egl.eglBindAPI) {
1684 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001685 }
1686 return res;
1687}
1688
1689EGLenum eglQueryAPI(void)
1690{
1691 clearError();
1692
1693 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001694 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001695 }
1696
Mathias Agopianada798b2012-02-13 17:09:30 -08001697 egl_connection_t* const cnx = &gEGLImpl;
1698 if (cnx->dso && cnx->egl.eglQueryAPI) {
1699 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001700 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001701
Mathias Agopian518ec112011-05-13 16:21:08 -07001702 // or, it can only be OpenGL ES
1703 return EGL_OPENGL_ES_API;
1704}
1705
1706EGLBoolean eglReleaseThread(void)
1707{
1708 clearError();
1709
Mathias Agopianada798b2012-02-13 17:09:30 -08001710 egl_connection_t* const cnx = &gEGLImpl;
1711 if (cnx->dso && cnx->egl.eglReleaseThread) {
1712 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001713 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301714
1715 // If there is context bound to the thread, release it
1716 egl_display_t::loseCurrent(get_context(getContext()));
1717
Mathias Agopian518ec112011-05-13 16:21:08 -07001718 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001719 return EGL_TRUE;
1720}
1721
1722EGLSurface eglCreatePbufferFromClientBuffer(
1723 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1724 EGLConfig config, const EGLint *attrib_list)
1725{
1726 clearError();
1727
Jesse Hallb29e5e82012-04-04 16:53:42 -07001728 egl_connection_t* cnx = NULL;
1729 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1730 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001731 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1732 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001733 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001734 }
1735 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1736}
1737
1738// ----------------------------------------------------------------------------
1739// EGL_EGLEXT_VERSION 3
1740// ----------------------------------------------------------------------------
1741
1742EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1743 const EGLint *attrib_list)
1744{
1745 clearError();
1746
Jesse Hallb29e5e82012-04-04 16:53:42 -07001747 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001748 if (!dp) return EGL_FALSE;
1749
Jesse Hallb29e5e82012-04-04 16:53:42 -07001750 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001751 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001752 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001753
1754 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001755 if (s->cnx->egl.eglLockSurfaceKHR) {
1756 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001757 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001758 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001759 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001760}
1761
1762EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1763{
1764 clearError();
1765
Jesse Hallb29e5e82012-04-04 16:53:42 -07001766 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001767 if (!dp) return EGL_FALSE;
1768
Jesse Hallb29e5e82012-04-04 16:53:42 -07001769 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001770 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001771 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001772
1773 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001774 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001775 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001776 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001777 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001778}
1779
1780EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1781 EGLClientBuffer buffer, const EGLint *attrib_list)
1782{
1783 clearError();
1784
Jesse Hallb29e5e82012-04-04 16:53:42 -07001785 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001786 if (!dp) return EGL_NO_IMAGE_KHR;
1787
Jesse Hallb29e5e82012-04-04 16:53:42 -07001788 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001789 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001790
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001791 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1792 egl_connection_t* const cnx = &gEGLImpl;
1793 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1794 result = cnx->egl.eglCreateImageKHR(
1795 dp->disp.dpy,
1796 c ? c->context : EGL_NO_CONTEXT,
1797 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001798 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001799 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001800}
1801
1802EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1803{
1804 clearError();
1805
Jesse Hallb29e5e82012-04-04 16:53:42 -07001806 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001807 if (!dp) return EGL_FALSE;
1808
Steven Holte646a5c52012-06-04 20:02:11 -07001809 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001810 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001811 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001812 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001813 }
Steven Holte646a5c52012-06-04 20:02:11 -07001814 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001815}
1816
1817// ----------------------------------------------------------------------------
1818// EGL_EGLEXT_VERSION 5
1819// ----------------------------------------------------------------------------
1820
1821
1822EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1823{
1824 clearError();
1825
Jesse Hallb29e5e82012-04-04 16:53:42 -07001826 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001827 if (!dp) return EGL_NO_SYNC_KHR;
1828
Mathias Agopian518ec112011-05-13 16:21:08 -07001829 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001830 egl_connection_t* const cnx = &gEGLImpl;
1831 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1832 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001833 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001834 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001835}
1836
1837EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1838{
1839 clearError();
1840
Jesse Hallb29e5e82012-04-04 16:53:42 -07001841 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001842 if (!dp) return EGL_FALSE;
1843
Mathias Agopian518ec112011-05-13 16:21:08 -07001844 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001845 egl_connection_t* const cnx = &gEGLImpl;
1846 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1847 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001848 }
1849 return result;
1850}
1851
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001852EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1853 clearError();
1854
1855 const egl_display_ptr dp = validate_display(dpy);
1856 if (!dp) return EGL_FALSE;
1857
1858 EGLBoolean result = EGL_FALSE;
1859 egl_connection_t* const cnx = &gEGLImpl;
1860 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1861 result = cnx->egl.eglSignalSyncKHR(
1862 dp->disp.dpy, sync, mode);
1863 }
1864 return result;
1865}
1866
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001867EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1868 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001869{
1870 clearError();
1871
Jesse Hallb29e5e82012-04-04 16:53:42 -07001872 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001873 if (!dp) return EGL_FALSE;
1874
Mathias Agopian737b8962017-02-24 14:32:05 -08001875 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001876 egl_connection_t* const cnx = &gEGLImpl;
1877 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1878 result = cnx->egl.eglClientWaitSyncKHR(
1879 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001880 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001881 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001882}
1883
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001884EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1885 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001886{
1887 clearError();
1888
Jesse Hallb29e5e82012-04-04 16:53:42 -07001889 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001890 if (!dp) return EGL_FALSE;
1891
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001892 EGLBoolean result = EGL_FALSE;
1893 egl_connection_t* const cnx = &gEGLImpl;
1894 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1895 result = cnx->egl.eglGetSyncAttribKHR(
1896 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001897 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001898 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001899}
1900
Season Li000d88f2015-07-01 11:39:40 -07001901EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1902{
1903 clearError();
1904
1905 const egl_display_ptr dp = validate_display(dpy);
1906 if (!dp) return EGL_NO_STREAM_KHR;
1907
1908 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1909 egl_connection_t* const cnx = &gEGLImpl;
1910 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1911 result = cnx->egl.eglCreateStreamKHR(
1912 dp->disp.dpy, attrib_list);
1913 }
1914 return result;
1915}
1916
1917EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1918{
1919 clearError();
1920
1921 const egl_display_ptr dp = validate_display(dpy);
1922 if (!dp) return EGL_FALSE;
1923
1924 EGLBoolean result = EGL_FALSE;
1925 egl_connection_t* const cnx = &gEGLImpl;
1926 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1927 result = cnx->egl.eglDestroyStreamKHR(
1928 dp->disp.dpy, stream);
1929 }
1930 return result;
1931}
1932
1933EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1934 EGLenum attribute, EGLint value)
1935{
1936 clearError();
1937
1938 const egl_display_ptr dp = validate_display(dpy);
1939 if (!dp) return EGL_FALSE;
1940
1941 EGLBoolean result = EGL_FALSE;
1942 egl_connection_t* const cnx = &gEGLImpl;
1943 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1944 result = cnx->egl.eglStreamAttribKHR(
1945 dp->disp.dpy, stream, attribute, value);
1946 }
1947 return result;
1948}
1949
1950EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1951 EGLenum attribute, EGLint *value)
1952{
1953 clearError();
1954
1955 const egl_display_ptr dp = validate_display(dpy);
1956 if (!dp) return EGL_FALSE;
1957
1958 EGLBoolean result = EGL_FALSE;
1959 egl_connection_t* const cnx = &gEGLImpl;
1960 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1961 result = cnx->egl.eglQueryStreamKHR(
1962 dp->disp.dpy, stream, attribute, value);
1963 }
1964 return result;
1965}
1966
1967EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1968 EGLenum attribute, EGLuint64KHR *value)
1969{
1970 clearError();
1971
1972 const egl_display_ptr dp = validate_display(dpy);
1973 if (!dp) return EGL_FALSE;
1974
1975 EGLBoolean result = EGL_FALSE;
1976 egl_connection_t* const cnx = &gEGLImpl;
1977 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1978 result = cnx->egl.eglQueryStreamu64KHR(
1979 dp->disp.dpy, stream, attribute, value);
1980 }
1981 return result;
1982}
1983
1984EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1985 EGLenum attribute, EGLTimeKHR *value)
1986{
1987 clearError();
1988
1989 const egl_display_ptr dp = validate_display(dpy);
1990 if (!dp) return EGL_FALSE;
1991
1992 EGLBoolean result = EGL_FALSE;
1993 egl_connection_t* const cnx = &gEGLImpl;
1994 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1995 result = cnx->egl.eglQueryStreamTimeKHR(
1996 dp->disp.dpy, stream, attribute, value);
1997 }
1998 return result;
1999}
2000
2001EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
2002 EGLStreamKHR stream, const EGLint *attrib_list)
2003{
2004 clearError();
2005
2006 egl_display_ptr dp = validate_display(dpy);
2007 if (!dp) return EGL_NO_SURFACE;
2008
2009 egl_connection_t* const cnx = &gEGLImpl;
2010 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
2011 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
2012 dp->disp.dpy, config, stream, attrib_list);
2013 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07002014 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
2015 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07002016 return s;
2017 }
2018 }
2019 return EGL_NO_SURFACE;
2020}
2021
2022EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
2023 EGLStreamKHR stream)
2024{
2025 clearError();
2026
2027 const egl_display_ptr dp = validate_display(dpy);
2028 if (!dp) return EGL_FALSE;
2029
2030 EGLBoolean result = EGL_FALSE;
2031 egl_connection_t* const cnx = &gEGLImpl;
2032 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
2033 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
2034 dp->disp.dpy, stream);
2035 }
2036 return result;
2037}
2038
2039EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
2040 EGLStreamKHR stream)
2041{
2042 clearError();
2043
2044 const egl_display_ptr dp = validate_display(dpy);
2045 if (!dp) return EGL_FALSE;
2046
2047 EGLBoolean result = EGL_FALSE;
2048 egl_connection_t* const cnx = &gEGLImpl;
2049 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
2050 result = cnx->egl.eglStreamConsumerAcquireKHR(
2051 dp->disp.dpy, stream);
2052 }
2053 return result;
2054}
2055
2056EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2057 EGLStreamKHR stream)
2058{
2059 clearError();
2060
2061 const egl_display_ptr dp = validate_display(dpy);
2062 if (!dp) return EGL_FALSE;
2063
2064 EGLBoolean result = EGL_FALSE;
2065 egl_connection_t* const cnx = &gEGLImpl;
2066 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2067 result = cnx->egl.eglStreamConsumerReleaseKHR(
2068 dp->disp.dpy, stream);
2069 }
2070 return result;
2071}
2072
2073EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2074 EGLDisplay dpy, EGLStreamKHR stream)
2075{
2076 clearError();
2077
2078 const egl_display_ptr dp = validate_display(dpy);
2079 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2080
2081 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2082 egl_connection_t* const cnx = &gEGLImpl;
2083 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2084 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2085 dp->disp.dpy, stream);
2086 }
2087 return result;
2088}
2089
2090EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2091 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2092{
2093 clearError();
2094
2095 const egl_display_ptr dp = validate_display(dpy);
2096 if (!dp) return EGL_NO_STREAM_KHR;
2097
2098 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2099 egl_connection_t* const cnx = &gEGLImpl;
2100 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2101 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2102 dp->disp.dpy, file_descriptor);
2103 }
2104 return result;
2105}
2106
Mathias Agopian518ec112011-05-13 16:21:08 -07002107// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002108// EGL_EGLEXT_VERSION 15
2109// ----------------------------------------------------------------------------
2110
2111EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2112 clearError();
2113 const egl_display_ptr dp = validate_display(dpy);
2114 if (!dp) return EGL_FALSE;
2115 EGLint result = EGL_FALSE;
2116 egl_connection_t* const cnx = &gEGLImpl;
2117 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2118 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2119 }
2120 return result;
2121}
2122
2123// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002124// ANDROID extensions
2125// ----------------------------------------------------------------------------
2126
Jamie Gennis331841b2012-09-06 14:52:00 -07002127EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2128{
2129 clearError();
2130
2131 const egl_display_ptr dp = validate_display(dpy);
2132 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2133
2134 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2135 egl_connection_t* const cnx = &gEGLImpl;
2136 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2137 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2138 }
2139 return result;
2140}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002141
Andy McFadden72841452013-03-01 16:25:32 -08002142EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2143 EGLnsecsANDROID time)
2144{
2145 clearError();
2146
2147 const egl_display_ptr dp = validate_display(dpy);
2148 if (!dp) {
2149 return EGL_FALSE;
2150 }
2151
2152 SurfaceRef _s(dp.get(), surface);
2153 if (!_s.get()) {
2154 setError(EGL_BAD_SURFACE, EGL_FALSE);
2155 return EGL_FALSE;
2156 }
2157
2158 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002159 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002160
2161 return EGL_TRUE;
2162}
2163
Craig Donner60761072017-01-27 12:30:44 -08002164EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2165 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002166 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2167 // this function cannot be implemented when this libEGL is built for
2168 // vendors.
2169#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002170 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002171 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002172#else
2173 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2174#endif
Craig Donner60761072017-01-27 12:30:44 -08002175}
2176
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002177// ----------------------------------------------------------------------------
2178// NVIDIA extensions
2179// ----------------------------------------------------------------------------
2180EGLuint64NV eglGetSystemTimeFrequencyNV()
2181{
2182 clearError();
2183
2184 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002185 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002186 }
2187
2188 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002189 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002190
Mathias Agopianada798b2012-02-13 17:09:30 -08002191 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2192 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002193 }
2194
Mathias Agopian737b8962017-02-24 14:32:05 -08002195 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002196}
2197
2198EGLuint64NV eglGetSystemTimeNV()
2199{
2200 clearError();
2201
2202 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002203 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002204 }
2205
2206 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002207 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002208
Mathias Agopianada798b2012-02-13 17:09:30 -08002209 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2210 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002211 }
2212
Mathias Agopian737b8962017-02-24 14:32:05 -08002213 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002214}
Dan Stozaa894d082015-02-19 15:27:36 -08002215
2216// ----------------------------------------------------------------------------
2217// Partial update extension
2218// ----------------------------------------------------------------------------
2219EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2220 EGLint *rects, EGLint n_rects)
2221{
2222 clearError();
2223
2224 const egl_display_ptr dp = validate_display(dpy);
2225 if (!dp) {
2226 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2227 return EGL_FALSE;
2228 }
2229
2230 SurfaceRef _s(dp.get(), surface);
2231 if (!_s.get()) {
2232 setError(EGL_BAD_SURFACE, EGL_FALSE);
2233 return EGL_FALSE;
2234 }
2235
2236 egl_surface_t const * const s = get_surface(surface);
2237 if (s->cnx->egl.eglSetDamageRegionKHR) {
2238 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2239 rects, n_rects);
2240 }
2241
2242 return EGL_FALSE;
2243}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002244
Brian Anderson1049d1d2016-12-16 17:25:57 -08002245EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2246 EGLuint64KHR *frameId) {
2247 clearError();
2248
2249 const egl_display_ptr dp = validate_display(dpy);
2250 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002251 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002252 }
2253
2254 SurfaceRef _s(dp.get(), surface);
2255 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002256 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002257 }
2258
2259 egl_surface_t const * const s = get_surface(surface);
2260
Mathias Agopian65421432017-03-08 11:49:05 -08002261 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002262 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002263 }
2264
2265 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002266 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002267
Mathias Agopian65421432017-03-08 11:49:05 -08002268 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002269 // This should not happen. Return an error that is not in the spec
2270 // so it's obvious something is very wrong.
2271 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002272 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002273 }
2274
2275 *frameId = nextFrameId;
2276 return EGL_TRUE;
2277}
2278
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002279EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2280 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2281{
2282 clearError();
2283
2284 const egl_display_ptr dp = validate_display(dpy);
2285 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002286 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002287 }
2288
2289 SurfaceRef _s(dp.get(), surface);
2290 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002291 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002292 }
2293
2294 egl_surface_t const * const s = get_surface(surface);
2295
Mathias Agopian65421432017-03-08 11:49:05 -08002296 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002297 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002298 }
2299
2300 nsecs_t* compositeDeadline = nullptr;
2301 nsecs_t* compositeInterval = nullptr;
2302 nsecs_t* compositeToPresentLatency = nullptr;
2303
2304 for (int i = 0; i < numTimestamps; i++) {
2305 switch (names[i]) {
2306 case EGL_COMPOSITE_DEADLINE_ANDROID:
2307 compositeDeadline = &values[i];
2308 break;
2309 case EGL_COMPOSITE_INTERVAL_ANDROID:
2310 compositeInterval = &values[i];
2311 break;
2312 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2313 compositeToPresentLatency = &values[i];
2314 break;
2315 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002316 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002317 }
2318 }
2319
Mathias Agopian65421432017-03-08 11:49:05 -08002320 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002321 compositeDeadline, compositeInterval, compositeToPresentLatency);
2322
2323 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002324 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002325 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002326 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002327 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002328 default:
2329 // This should not happen. Return an error that is not in the spec
2330 // so it's obvious something is very wrong.
2331 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002332 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002333 }
2334}
2335
2336EGLBoolean eglGetCompositorTimingSupportedANDROID(
2337 EGLDisplay dpy, EGLSurface surface, EGLint name)
2338{
2339 clearError();
2340
2341 const egl_display_ptr dp = validate_display(dpy);
2342 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002343 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002344 }
2345
2346 SurfaceRef _s(dp.get(), surface);
2347 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002348 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002349 }
2350
2351 egl_surface_t const * const s = get_surface(surface);
2352
Mathias Agopian65421432017-03-08 11:49:05 -08002353 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002354 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002355 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002356 }
2357
2358 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002359 case EGL_COMPOSITE_DEADLINE_ANDROID:
2360 case EGL_COMPOSITE_INTERVAL_ANDROID:
2361 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2362 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002363 default:
2364 return EGL_FALSE;
2365 }
2366}
2367
Pablo Ceballosc18be292016-05-31 14:55:42 -07002368EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002369 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002370 EGLnsecsANDROID *values)
2371{
2372 clearError();
2373
2374 const egl_display_ptr dp = validate_display(dpy);
2375 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002376 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002377 }
2378
2379 SurfaceRef _s(dp.get(), surface);
2380 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002381 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002382 }
2383
2384 egl_surface_t const * const s = get_surface(surface);
2385
Mathias Agopian65421432017-03-08 11:49:05 -08002386 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002387 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002388 }
2389
Brian Andersondbd0ea82016-07-22 09:38:59 -07002390 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002391 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002392 nsecs_t* latchTime = nullptr;
2393 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002394 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002395 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002396 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002397 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002398 nsecs_t* releaseTime = nullptr;
2399
2400 for (int i = 0; i < numTimestamps; i++) {
2401 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002402 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2403 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002404 break;
2405 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2406 acquireTime = &values[i];
2407 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002408 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2409 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002410 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002411 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2412 firstRefreshStartTime = &values[i];
2413 break;
2414 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2415 lastRefreshStartTime = &values[i];
2416 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002417 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2418 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002419 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002420 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2421 displayPresentTime = &values[i];
2422 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002423 case EGL_DEQUEUE_READY_TIME_ANDROID:
2424 dequeueReadyTime = &values[i];
2425 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002426 case EGL_READS_DONE_TIME_ANDROID:
2427 releaseTime = &values[i];
2428 break;
2429 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002430 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002431 }
2432 }
2433
Mathias Agopian65421432017-03-08 11:49:05 -08002434 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002435 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002436 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002437 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002438
Brian Anderson069b3652016-07-22 10:32:47 -07002439 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002440 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002441 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002442 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002443 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002444 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002445 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002446 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002447 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2448 default:
2449 // This should not happen. Return an error that is not in the spec
2450 // so it's obvious something is very wrong.
2451 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2452 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002453 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002454}
2455
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002456EGLBoolean eglGetFrameTimestampSupportedANDROID(
2457 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002458{
2459 clearError();
2460
2461 const egl_display_ptr dp = validate_display(dpy);
2462 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002463 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002464 }
2465
2466 SurfaceRef _s(dp.get(), surface);
2467 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002468 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002469 }
2470
2471 egl_surface_t const * const s = get_surface(surface);
2472
Mathias Agopian65421432017-03-08 11:49:05 -08002473 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002474 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002475 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002476 }
2477
2478 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002479 case EGL_COMPOSITE_DEADLINE_ANDROID:
2480 case EGL_COMPOSITE_INTERVAL_ANDROID:
2481 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002482 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002483 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002484 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2485 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2486 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002487 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002488 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002489 case EGL_READS_DONE_TIME_ANDROID:
2490 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002491 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2492 int value = 0;
2493 window->query(window,
2494 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2495 return value == 0 ? EGL_FALSE : EGL_TRUE;
2496 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002497 default:
2498 return EGL_FALSE;
2499 }
2500}