blob: 484e0baffc5159ecb96f4b9a79afd271abf94eea [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 Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600482 EGLint format,
483 std::vector<EGLint>& stripped_attrib_list) {
484 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 }
516 if (found && dp->haveExtension("EGL_KHR_gl_colorspace")) {
517 stripped = true;
518 } else {
519 stripped_attrib_list.push_back(attr[0]);
520 stripped_attrib_list.push_back(attr[1]);
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) {
715 int err = native_window_set_buffers_smpte2086_metadata(window, s->getSmpte2086Metadata());
716 if (err != 0) {
717 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
718 strerror(-err), err);
719 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
720 return EGL_FALSE;
721 }
722 }
723 if (cta8613) {
724 int err = native_window_set_buffers_cta861_3_metadata(window, s->getCta8613Metadata());
725 if (err != 0) {
726 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
727 strerror(-err), err);
728 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
729 return EGL_FALSE;
730 }
731 }
732 return EGL_TRUE;
733}
734
Mathias Agopian518ec112011-05-13 16:21:08 -0700735EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
736 NativeWindowType window,
737 const EGLint *attrib_list)
738{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700739 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700740 clearError();
741
Jesse Hallb29e5e82012-04-04 16:53:42 -0700742 egl_connection_t* cnx = NULL;
743 egl_display_ptr dp = validate_display_connection(dpy, cnx);
744 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800745 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700746
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800747 if (!window) {
748 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
749 }
750
751 int value = 0;
752 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
753 if (!value) {
754 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
755 }
756
Andy McFaddend566ce32014-01-07 15:54:17 -0800757 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800758 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800759 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
760 "failed (%#x) (already connected to another API?)",
761 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700762 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700763 }
764
Mathias Agopian95921562017-02-24 14:31:31 -0800765 EGLint format;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600766 getNativePixelFormat(iDpy, cnx, config, format);
767
768 // now select correct colorspace and dataspace based on user's attribute list
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600769 EGLint colorSpace;
770 android_dataspace dataSpace;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600771 if (!getColorSpaceAttribute(dp, window, attrib_list, colorSpace, dataSpace)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600772 ALOGE("error invalid colorspace: %d", colorSpace);
773 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700774 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800775
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600776 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700777 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600778 // Had to modify the attribute list due to use of color space.
779 // Use modified list from here on.
780 attrib_list = strippedAttribList.data();
781 }
782
Jesse Hallc2e41222013-08-08 13:40:22 -0700783 if (format != 0) {
784 int err = native_window_set_buffers_format(window, format);
785 if (err != 0) {
786 ALOGE("error setting native window pixel format: %s (%d)",
787 strerror(-err), err);
788 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
789 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
790 }
791 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700792
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800793 if (dataSpace != 0) {
794 int err = native_window_set_buffers_data_space(window, dataSpace);
795 if (err != 0) {
796 ALOGE("error setting native window pixel dataSpace: %s (%d)",
797 strerror(-err), err);
798 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
799 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
800 }
801 }
802
Jamie Gennis59769462011-11-19 18:04:43 -0800803 // the EGL spec requires that a new EGLSurface default to swap interval
804 // 1, so explicitly set that on the window here.
805 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
806 anw->setSwapInterval(anw, 1);
807
Mathias Agopian518ec112011-05-13 16:21:08 -0700808 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800809 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600811 egl_surface_t* s =
812 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700813
814 if (setSurfaceMetadata(s, window, origAttribList)) {
815 return s;
816 }
817 eglDestroySurface(dpy, s);
Mathias Agopian518ec112011-05-13 16:21:08 -0700818 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700819
820 // EGLSurface creation failed
821 native_window_set_buffers_format(window, 0);
822 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700823 }
824 return EGL_NO_SURFACE;
825}
826
827EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
828 NativePixmapType pixmap,
829 const EGLint *attrib_list)
830{
831 clearError();
832
Jesse Hallb29e5e82012-04-04 16:53:42 -0700833 egl_connection_t* cnx = NULL;
834 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600835 EGLint colorSpace;
836 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700837 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600838 // now select a corresponding sRGB format if needed
839 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
840 ALOGE("error invalid colorspace: %d", colorSpace);
841 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
842 }
843
Mathias Agopian518ec112011-05-13 16:21:08 -0700844 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800845 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700846 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600847 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700848 return s;
849 }
850 }
851 return EGL_NO_SURFACE;
852}
853
854EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
855 const EGLint *attrib_list)
856{
857 clearError();
858
Jesse Hallb29e5e82012-04-04 16:53:42 -0700859 egl_connection_t* cnx = NULL;
860 egl_display_ptr dp = validate_display_connection(dpy, cnx);
861 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600862 EGLDisplay iDpy = dp->disp.dpy;
863 EGLint format;
864 getNativePixelFormat(iDpy, cnx, config, format);
865
866 // now select correct colorspace and dataspace based on user's attribute list
867 EGLint colorSpace;
868 android_dataspace dataSpace;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600869 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
870 ALOGE("error invalid colorspace: %d", colorSpace);
871 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
872 }
873
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600874 // Pbuffers are not displayed so we don't need to store the
875 // colorspace. We do need to filter out color spaces the
876 // driver doesn't know how to process.
877 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700878 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600879 // Had to modify the attribute list due to use of color space.
880 // Use modified list from here on.
881 attrib_list = strippedAttribList.data();
882 }
883
Mathias Agopian518ec112011-05-13 16:21:08 -0700884 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800885 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700886 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600887 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700888 return s;
889 }
890 }
891 return EGL_NO_SURFACE;
892}
Jesse Hall47743382013-02-08 11:13:46 -0800893
Mathias Agopian518ec112011-05-13 16:21:08 -0700894EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
895{
896 clearError();
897
Jesse Hallb29e5e82012-04-04 16:53:42 -0700898 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700899 if (!dp) return EGL_FALSE;
900
Jesse Hallb29e5e82012-04-04 16:53:42 -0700901 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700902 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800903 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700904
905 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800906 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700907 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700908 _s.terminate();
909 }
910 return result;
911}
912
913EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
914 EGLint attribute, EGLint *value)
915{
916 clearError();
917
Jesse Hallb29e5e82012-04-04 16:53:42 -0700918 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700919 if (!dp) return EGL_FALSE;
920
Jesse Hallb29e5e82012-04-04 16:53:42 -0700921 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700922 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800923 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700924
Mathias Agopian518ec112011-05-13 16:21:08 -0700925 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700926 if (s->getColorSpaceAttribute(attribute, value)) {
927 return EGL_TRUE;
928 } else if (s->getSmpte2086Attribute(attribute, value)) {
929 return EGL_TRUE;
930 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600931 return EGL_TRUE;
932 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700933 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700934}
935
Jamie Gennise8696a42012-01-15 18:54:57 -0800936void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800937 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800938 clearError();
939
Jesse Hallb29e5e82012-04-04 16:53:42 -0700940 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800941 if (!dp) {
942 return;
943 }
944
Jesse Hallb29e5e82012-04-04 16:53:42 -0700945 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800946 if (!_s.get()) {
947 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800948 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800949}
950
Mathias Agopian518ec112011-05-13 16:21:08 -0700951// ----------------------------------------------------------------------------
952// Contexts
953// ----------------------------------------------------------------------------
954
955EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
956 EGLContext share_list, const EGLint *attrib_list)
957{
958 clearError();
959
Jesse Hallb29e5e82012-04-04 16:53:42 -0700960 egl_connection_t* cnx = NULL;
961 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700962 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700963 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700964 if (!ContextRef(dp.get(), share_list).get()) {
965 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
966 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700967 egl_context_t* const c = get_context(share_list);
968 share_list = c->context;
969 }
970 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800971 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700972 if (context != EGL_NO_CONTEXT) {
973 // figure out if it's a GLESv1 or GLESv2
974 int version = 0;
975 if (attrib_list) {
976 while (*attrib_list != EGL_NONE) {
977 GLint attr = *attrib_list++;
978 GLint value = *attrib_list++;
979 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
980 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800981 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800982 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800983 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700984 }
985 }
986 };
987 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700988 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
989 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700990 return c;
991 }
992 }
993 return EGL_NO_CONTEXT;
994}
995
996EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
997{
998 clearError();
999
Jesse Hallb29e5e82012-04-04 16:53:42 -07001000 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001001 if (!dp)
1002 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001003
Jesse Hallb29e5e82012-04-04 16:53:42 -07001004 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001005 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001006 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -08001007
Mathias Agopian518ec112011-05-13 16:21:08 -07001008 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -08001009 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 if (result == EGL_TRUE) {
1011 _c.terminate();
1012 }
1013 return result;
1014}
1015
Mathias Agopian518ec112011-05-13 16:21:08 -07001016EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1017 EGLSurface read, EGLContext ctx)
1018{
1019 clearError();
1020
Jesse Hallb29e5e82012-04-04 16:53:42 -07001021 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -08001022 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001023
Mathias Agopian5b287a62011-05-16 18:58:55 -07001024 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
1025 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
1026 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -07001027 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
1028 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001029 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001030 }
1031
1032 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -07001033 ContextRef _c(dp.get(), ctx);
1034 SurfaceRef _d(dp.get(), draw);
1035 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001036
1037 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -07001038 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001039 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -08001040 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001041 }
1042
1043 // these are the underlying implementation's object
1044 EGLContext impl_ctx = EGL_NO_CONTEXT;
1045 EGLSurface impl_draw = EGL_NO_SURFACE;
1046 EGLSurface impl_read = EGL_NO_SURFACE;
1047
1048 // these are our objects structs passed in
1049 egl_context_t * c = NULL;
1050 egl_surface_t const * d = NULL;
1051 egl_surface_t const * r = NULL;
1052
1053 // these are the current objects structs
1054 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001055
Mathias Agopian518ec112011-05-13 16:21:08 -07001056 if (ctx != EGL_NO_CONTEXT) {
1057 c = get_context(ctx);
1058 impl_ctx = c->context;
1059 } else {
1060 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001061 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1062 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001063 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001064 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001065 if (cur_c == NULL) {
1066 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001067 // not an error, there is just no current context.
1068 return EGL_TRUE;
1069 }
1070 }
1071
1072 // retrieve the underlying implementation's draw EGLSurface
1073 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001074 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001075 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001076 impl_draw = d->surface;
1077 }
1078
1079 // retrieve the underlying implementation's read EGLSurface
1080 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001081 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001082 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001083 impl_read = r->surface;
1084 }
1085
Mathias Agopian518ec112011-05-13 16:21:08 -07001086
Jesse Hallb29e5e82012-04-04 16:53:42 -07001087 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001088 draw, read, ctx,
1089 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001090
1091 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001092 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001093 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1094 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001095 _c.acquire();
1096 _r.acquire();
1097 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001098 } else {
1099 setGLHooksThreadSpecific(&gHooksNoContext);
1100 egl_tls_t::setContext(EGL_NO_CONTEXT);
1101 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001102 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001103
Mike Stroyan47e741b2017-06-01 13:56:18 -06001104 if (cur_c != NULL) {
1105 // Force return to current context for drivers that cannot handle errors
1106 EGLBoolean restore_result = EGL_FALSE;
1107 // get a reference to the old current objects
1108 ContextRef _c2(dp.get(), cur_c);
1109 SurfaceRef _d2(dp.get(), cur_c->draw);
1110 SurfaceRef _r2(dp.get(), cur_c->read);
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001111
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001112 c = cur_c;
1113 impl_ctx = c->context;
1114 impl_draw = EGL_NO_SURFACE;
1115 if (cur_c->draw != EGL_NO_SURFACE) {
1116 d = get_surface(cur_c->draw);
1117 impl_draw = d->surface;
1118 }
1119 impl_read = EGL_NO_SURFACE;
1120 if (cur_c->read != EGL_NO_SURFACE) {
1121 r = get_surface(cur_c->read);
1122 impl_read = r->surface;
1123 }
1124 restore_result = dp->makeCurrent(c, cur_c,
1125 cur_c->draw, cur_c->read, cur_c->context,
1126 impl_draw, impl_read, impl_ctx);
Mike Stroyan47e741b2017-06-01 13:56:18 -06001127 if (restore_result == EGL_TRUE) {
1128 _c2.acquire();
1129 _r2.acquire();
1130 _d2.acquire();
1131 } else {
1132 ALOGE("Could not restore original EGL context");
1133 }
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001134 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001135 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001136 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001137 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001138 }
1139 return result;
1140}
1141
1142
1143EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1144 EGLint attribute, EGLint *value)
1145{
1146 clearError();
1147
Jesse Hallb29e5e82012-04-04 16:53:42 -07001148 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001149 if (!dp) return EGL_FALSE;
1150
Jesse Hallb29e5e82012-04-04 16:53:42 -07001151 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001152 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001153
Mathias Agopian518ec112011-05-13 16:21:08 -07001154 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001155 return c->cnx->egl.eglQueryContext(
1156 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001157
Mathias Agopian518ec112011-05-13 16:21:08 -07001158}
1159
1160EGLContext eglGetCurrentContext(void)
1161{
1162 // could be called before eglInitialize(), but we wouldn't have a context
1163 // then, and this function would correctly return EGL_NO_CONTEXT.
1164
1165 clearError();
1166
1167 EGLContext ctx = getContext();
1168 return ctx;
1169}
1170
1171EGLSurface eglGetCurrentSurface(EGLint readdraw)
1172{
1173 // could be called before eglInitialize(), but we wouldn't have a context
1174 // then, and this function would correctly return EGL_NO_SURFACE.
1175
1176 clearError();
1177
1178 EGLContext ctx = getContext();
1179 if (ctx) {
1180 egl_context_t const * const c = get_context(ctx);
1181 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1182 switch (readdraw) {
1183 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001184 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001185 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1186 }
1187 }
1188 return EGL_NO_SURFACE;
1189}
1190
1191EGLDisplay eglGetCurrentDisplay(void)
1192{
1193 // could be called before eglInitialize(), but we wouldn't have a context
1194 // then, and this function would correctly return EGL_NO_DISPLAY.
1195
1196 clearError();
1197
1198 EGLContext ctx = getContext();
1199 if (ctx) {
1200 egl_context_t const * const c = get_context(ctx);
1201 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1202 return c->dpy;
1203 }
1204 return EGL_NO_DISPLAY;
1205}
1206
1207EGLBoolean eglWaitGL(void)
1208{
Mathias Agopian518ec112011-05-13 16:21:08 -07001209 clearError();
1210
Mathias Agopianada798b2012-02-13 17:09:30 -08001211 egl_connection_t* const cnx = &gEGLImpl;
1212 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001213 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001214
1215 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001216}
1217
1218EGLBoolean eglWaitNative(EGLint engine)
1219{
Mathias Agopian518ec112011-05-13 16:21:08 -07001220 clearError();
1221
Mathias Agopianada798b2012-02-13 17:09:30 -08001222 egl_connection_t* const cnx = &gEGLImpl;
1223 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001224 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001225
1226 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001227}
1228
1229EGLint eglGetError(void)
1230{
Mathias Agopianada798b2012-02-13 17:09:30 -08001231 EGLint err = EGL_SUCCESS;
1232 egl_connection_t* const cnx = &gEGLImpl;
1233 if (cnx->dso) {
1234 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001235 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001236 if (err == EGL_SUCCESS) {
1237 err = egl_tls_t::getError();
1238 }
1239 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001240}
1241
Michael Chockc0ec5e22014-01-27 08:14:33 -08001242static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001243 const char* procname) {
1244 const egl_connection_t* cnx = &gEGLImpl;
1245 void* proc = NULL;
1246
Michael Chockc0ec5e22014-01-27 08:14:33 -08001247 proc = dlsym(cnx->libEgl, procname);
1248 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1249
Jesse Hallc07b5202013-07-04 12:08:16 -07001250 proc = dlsym(cnx->libGles2, procname);
1251 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1252
1253 proc = dlsym(cnx->libGles1, procname);
1254 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1255
1256 return NULL;
1257}
1258
Mathias Agopian518ec112011-05-13 16:21:08 -07001259__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1260{
1261 // eglGetProcAddress() could be the very first function called
1262 // in which case we must make sure we've initialized ourselves, this
1263 // happens the first time egl_get_display() is called.
1264
1265 clearError();
1266
1267 if (egl_init_drivers() == EGL_FALSE) {
1268 setError(EGL_BAD_PARAMETER, NULL);
1269 return NULL;
1270 }
1271
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001272 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001273 return NULL;
1274 }
1275
Mathias Agopian518ec112011-05-13 16:21:08 -07001276 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001277 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 if (addr) return addr;
1279
Michael Chockc0ec5e22014-01-27 08:14:33 -08001280 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001281 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001282
Mathias Agopian518ec112011-05-13 16:21:08 -07001283 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1284 pthread_mutex_lock(&sExtensionMapMutex);
1285
1286 /*
1287 * Since eglGetProcAddress() is not associated to anything, it needs
1288 * to return a function pointer that "works" regardless of what
1289 * the current context is.
1290 *
1291 * For this reason, we return a "forwarder", a small stub that takes
1292 * care of calling the function associated with the context
1293 * currently bound.
1294 *
1295 * We first look for extensions we've already resolved, if we're seeing
1296 * this extension for the first time, we go through all our
1297 * implementations and call eglGetProcAddress() and record the
1298 * result in the appropriate implementation hooks and return the
1299 * address of the forwarder corresponding to that hook set.
1300 *
1301 */
1302
Mathias Agopian65421432017-03-08 11:49:05 -08001303 const std::string name(procname);
1304
1305 auto& extentionMap = sGLExtentionMap;
1306 auto pos = extentionMap.find(name);
1307 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001308 const int slot = sGLExtentionSlot;
1309
Steve Blocke6f43dd2012-01-06 19:20:56 +00001310 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001311 "no more slots for eglGetProcAddress(\"%s\")",
1312 procname);
1313
1314 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1315 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001316
1317 egl_connection_t* const cnx = &gEGLImpl;
1318 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001319 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001320 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001321 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1322 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001323 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001324 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001325 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001326
Mathias Agopian518ec112011-05-13 16:21:08 -07001327 if (found) {
1328 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001329 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001330 sGLExtentionSlot++;
1331 }
1332 }
1333
1334 pthread_mutex_unlock(&sExtensionMapMutex);
1335 return addr;
1336}
1337
Mathias Agopian65421432017-03-08 11:49:05 -08001338class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001339public:
1340
1341 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001342 static FrameCompletionThread thread;
1343
1344 char name[64];
1345
1346 std::lock_guard<std::mutex> lock(thread.mMutex);
1347 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1348 ATRACE_NAME(name);
1349
1350 thread.mQueue.push_back(sync);
1351 thread.mCondition.notify_one();
1352 thread.mFramesQueued++;
1353 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001354 }
1355
1356private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001357
Mathias Agopian65421432017-03-08 11:49:05 -08001358 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1359 std::thread thread(&FrameCompletionThread::loop, this);
1360 thread.detach();
1361 }
1362
1363#pragma clang diagnostic push
1364#pragma clang diagnostic ignored "-Wmissing-noreturn"
1365 void loop() {
1366 while (true) {
1367 threadLoop();
1368 }
1369 }
1370#pragma clang diagnostic pop
1371
1372 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001373 EGLSyncKHR sync;
1374 uint32_t frameNum;
1375 {
Mathias Agopian65421432017-03-08 11:49:05 -08001376 std::unique_lock<std::mutex> lock(mMutex);
1377 while (mQueue.empty()) {
1378 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001379 }
1380 sync = mQueue[0];
1381 frameNum = mFramesCompleted;
1382 }
1383 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1384 {
Mathias Agopian65421432017-03-08 11:49:05 -08001385 char name[64];
1386 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1387 ATRACE_NAME(name);
1388
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001389 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1390 if (result == EGL_FALSE) {
1391 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1392 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1393 ALOGE("FrameCompletion: timeout waiting for fence");
1394 }
1395 eglDestroySyncKHR(dpy, sync);
1396 }
1397 {
Mathias Agopian65421432017-03-08 11:49:05 -08001398 std::lock_guard<std::mutex> lock(mMutex);
1399 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001400 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001401 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001402 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001403 }
1404
1405 uint32_t mFramesQueued;
1406 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001407 std::deque<EGLSyncKHR> mQueue;
1408 std::condition_variable mCondition;
1409 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001410};
1411
Dan Stozaa894d082015-02-19 15:27:36 -08001412EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1413 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001414{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001415 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001416 clearError();
1417
Jesse Hallb29e5e82012-04-04 16:53:42 -07001418 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001419 if (!dp) return EGL_FALSE;
1420
Jesse Hallb29e5e82012-04-04 16:53:42 -07001421 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001422 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001423 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001424
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001426
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001427 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1428 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1429 if (sync != EGL_NO_SYNC_KHR) {
1430 FrameCompletionThread::queueSync(sync);
1431 }
1432 }
1433
Mathias Agopian7db993a2012-03-25 00:49:46 -07001434 if (CC_UNLIKELY(dp->finishOnSwap)) {
1435 uint32_t pixel;
1436 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1437 if (c) {
1438 // glReadPixels() ensures that the frame is complete
1439 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1440 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1441 }
1442 }
1443
Dan Stozaa894d082015-02-19 15:27:36 -08001444 if (n_rects == 0) {
1445 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1446 }
1447
Mathias Agopian65421432017-03-08 11:49:05 -08001448 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001449 for (int r = 0; r < n_rects; ++r) {
1450 int offset = r * 4;
1451 int x = rects[offset];
1452 int y = rects[offset + 1];
1453 int width = rects[offset + 2];
1454 int height = rects[offset + 3];
1455 android_native_rect_t androidRect;
1456 androidRect.left = x;
1457 androidRect.top = y + height;
1458 androidRect.right = x + width;
1459 androidRect.bottom = y;
1460 androidRects.push_back(androidRect);
1461 }
Mathias Agopian65421432017-03-08 11:49:05 -08001462 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001463
1464 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1465 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1466 rects, n_rects);
1467 } else {
1468 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1469 }
1470}
1471
1472EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1473{
1474 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001475}
1476
1477EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1478 NativePixmapType target)
1479{
1480 clearError();
1481
Jesse Hallb29e5e82012-04-04 16:53:42 -07001482 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001483 if (!dp) return EGL_FALSE;
1484
Jesse Hallb29e5e82012-04-04 16:53:42 -07001485 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001486 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001487 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001488
Mathias Agopian518ec112011-05-13 16:21:08 -07001489 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001490 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001491}
1492
1493const char* eglQueryString(EGLDisplay dpy, EGLint name)
1494{
1495 clearError();
1496
Chia-I Wue57d1352016-08-15 16:10:02 +08001497 // Generate an error quietly when client extensions (as defined by
1498 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1499 // validate_display to generate the error as validate_display would log
1500 // the error, which can be misleading.
1501 //
1502 // If we want to support EGL_EXT_client_extensions later, we can return
1503 // the client extension string here instead.
1504 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001505 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001506
Jesse Hallb29e5e82012-04-04 16:53:42 -07001507 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001508 if (!dp) return (const char *) NULL;
1509
1510 switch (name) {
1511 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001512 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001513 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001514 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001515 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001516 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001517 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001518 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001519 default:
1520 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001521 }
1522 return setError(EGL_BAD_PARAMETER, (const char *)0);
1523}
1524
Jiyong Park00b15b82017-08-10 20:30:56 +09001525extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001526{
1527 clearError();
1528
1529 const egl_display_ptr dp = validate_display(dpy);
1530 if (!dp) return (const char *) NULL;
1531
1532 switch (name) {
1533 case EGL_VENDOR:
1534 return dp->disp.queryString.vendor;
1535 case EGL_VERSION:
1536 return dp->disp.queryString.version;
1537 case EGL_EXTENSIONS:
1538 return dp->disp.queryString.extensions;
1539 case EGL_CLIENT_APIS:
1540 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001541 default:
1542 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001543 }
1544 return setError(EGL_BAD_PARAMETER, (const char *)0);
1545}
Mathias Agopian518ec112011-05-13 16:21:08 -07001546
1547// ----------------------------------------------------------------------------
1548// EGL 1.1
1549// ----------------------------------------------------------------------------
1550
1551EGLBoolean eglSurfaceAttrib(
1552 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1553{
1554 clearError();
1555
Jesse Hallb29e5e82012-04-04 16:53:42 -07001556 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001557 if (!dp) return EGL_FALSE;
1558
Jesse Hallb29e5e82012-04-04 16:53:42 -07001559 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001560 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001561 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001562
Pablo Ceballosc18be292016-05-31 14:55:42 -07001563 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001564
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001565 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001566 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001567 setError(EGL_BAD_SURFACE, EGL_FALSE);
1568 }
Mathias Agopian65421432017-03-08 11:49:05 -08001569 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1570 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001571 }
1572
Pablo Ceballosc18be292016-05-31 14:55:42 -07001573 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001574 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001575 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001576 }
Mathias Agopian65421432017-03-08 11:49:05 -08001577 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1578 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001579 }
1580
Mathias Agopian518ec112011-05-13 16:21:08 -07001581 if (s->cnx->egl.eglSurfaceAttrib) {
1582 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001583 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001584 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001585 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001586}
1587
1588EGLBoolean eglBindTexImage(
1589 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1590{
1591 clearError();
1592
Jesse Hallb29e5e82012-04-04 16:53:42 -07001593 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001594 if (!dp) return EGL_FALSE;
1595
Jesse Hallb29e5e82012-04-04 16:53:42 -07001596 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001597 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001598 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001599
Mathias Agopian518ec112011-05-13 16:21:08 -07001600 egl_surface_t const * const s = get_surface(surface);
1601 if (s->cnx->egl.eglBindTexImage) {
1602 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001603 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001604 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001605 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001606}
1607
1608EGLBoolean eglReleaseTexImage(
1609 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1610{
1611 clearError();
1612
Jesse Hallb29e5e82012-04-04 16:53:42 -07001613 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001614 if (!dp) return EGL_FALSE;
1615
Jesse Hallb29e5e82012-04-04 16:53:42 -07001616 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001617 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001618 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001619
Mathias Agopian518ec112011-05-13 16:21:08 -07001620 egl_surface_t const * const s = get_surface(surface);
1621 if (s->cnx->egl.eglReleaseTexImage) {
1622 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001623 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001624 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001625 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001626}
1627
1628EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1629{
1630 clearError();
1631
Jesse Hallb29e5e82012-04-04 16:53:42 -07001632 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001633 if (!dp) return EGL_FALSE;
1634
1635 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001636 egl_connection_t* const cnx = &gEGLImpl;
1637 if (cnx->dso && cnx->egl.eglSwapInterval) {
1638 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001639 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001640
Mathias Agopian518ec112011-05-13 16:21:08 -07001641 return res;
1642}
1643
1644
1645// ----------------------------------------------------------------------------
1646// EGL 1.2
1647// ----------------------------------------------------------------------------
1648
1649EGLBoolean eglWaitClient(void)
1650{
1651 clearError();
1652
Mathias Agopianada798b2012-02-13 17:09:30 -08001653 egl_connection_t* const cnx = &gEGLImpl;
1654 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001655 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001656
1657 EGLBoolean res;
1658 if (cnx->egl.eglWaitClient) {
1659 res = cnx->egl.eglWaitClient();
1660 } else {
1661 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001662 }
1663 return res;
1664}
1665
1666EGLBoolean eglBindAPI(EGLenum api)
1667{
1668 clearError();
1669
1670 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001671 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001672 }
1673
1674 // bind this API on all EGLs
1675 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001676 egl_connection_t* const cnx = &gEGLImpl;
1677 if (cnx->dso && cnx->egl.eglBindAPI) {
1678 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001679 }
1680 return res;
1681}
1682
1683EGLenum eglQueryAPI(void)
1684{
1685 clearError();
1686
1687 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001688 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001689 }
1690
Mathias Agopianada798b2012-02-13 17:09:30 -08001691 egl_connection_t* const cnx = &gEGLImpl;
1692 if (cnx->dso && cnx->egl.eglQueryAPI) {
1693 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001694 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001695
Mathias Agopian518ec112011-05-13 16:21:08 -07001696 // or, it can only be OpenGL ES
1697 return EGL_OPENGL_ES_API;
1698}
1699
1700EGLBoolean eglReleaseThread(void)
1701{
1702 clearError();
1703
Mathias Agopianada798b2012-02-13 17:09:30 -08001704 egl_connection_t* const cnx = &gEGLImpl;
1705 if (cnx->dso && cnx->egl.eglReleaseThread) {
1706 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001707 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301708
1709 // If there is context bound to the thread, release it
1710 egl_display_t::loseCurrent(get_context(getContext()));
1711
Mathias Agopian518ec112011-05-13 16:21:08 -07001712 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001713 return EGL_TRUE;
1714}
1715
1716EGLSurface eglCreatePbufferFromClientBuffer(
1717 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1718 EGLConfig config, const EGLint *attrib_list)
1719{
1720 clearError();
1721
Jesse Hallb29e5e82012-04-04 16:53:42 -07001722 egl_connection_t* cnx = NULL;
1723 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1724 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001725 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1726 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001727 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001728 }
1729 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1730}
1731
1732// ----------------------------------------------------------------------------
1733// EGL_EGLEXT_VERSION 3
1734// ----------------------------------------------------------------------------
1735
1736EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1737 const EGLint *attrib_list)
1738{
1739 clearError();
1740
Jesse Hallb29e5e82012-04-04 16:53:42 -07001741 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001742 if (!dp) return EGL_FALSE;
1743
Jesse Hallb29e5e82012-04-04 16:53:42 -07001744 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001745 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001746 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001747
1748 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001749 if (s->cnx->egl.eglLockSurfaceKHR) {
1750 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001751 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001752 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001753 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001754}
1755
1756EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1757{
1758 clearError();
1759
Jesse Hallb29e5e82012-04-04 16:53:42 -07001760 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001761 if (!dp) return EGL_FALSE;
1762
Jesse Hallb29e5e82012-04-04 16:53:42 -07001763 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001764 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001765 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001766
1767 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001768 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001769 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001770 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001771 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001772}
1773
1774EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1775 EGLClientBuffer buffer, const EGLint *attrib_list)
1776{
1777 clearError();
1778
Jesse Hallb29e5e82012-04-04 16:53:42 -07001779 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001780 if (!dp) return EGL_NO_IMAGE_KHR;
1781
Jesse Hallb29e5e82012-04-04 16:53:42 -07001782 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001783 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001784
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001785 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1786 egl_connection_t* const cnx = &gEGLImpl;
1787 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1788 result = cnx->egl.eglCreateImageKHR(
1789 dp->disp.dpy,
1790 c ? c->context : EGL_NO_CONTEXT,
1791 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001792 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001793 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001794}
1795
1796EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1797{
1798 clearError();
1799
Jesse Hallb29e5e82012-04-04 16:53:42 -07001800 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001801 if (!dp) return EGL_FALSE;
1802
Steven Holte646a5c52012-06-04 20:02:11 -07001803 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001804 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001805 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001806 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001807 }
Steven Holte646a5c52012-06-04 20:02:11 -07001808 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001809}
1810
1811// ----------------------------------------------------------------------------
1812// EGL_EGLEXT_VERSION 5
1813// ----------------------------------------------------------------------------
1814
1815
1816EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1817{
1818 clearError();
1819
Jesse Hallb29e5e82012-04-04 16:53:42 -07001820 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001821 if (!dp) return EGL_NO_SYNC_KHR;
1822
Mathias Agopian518ec112011-05-13 16:21:08 -07001823 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001824 egl_connection_t* const cnx = &gEGLImpl;
1825 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1826 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001827 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001828 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001829}
1830
1831EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1832{
1833 clearError();
1834
Jesse Hallb29e5e82012-04-04 16:53:42 -07001835 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001836 if (!dp) return EGL_FALSE;
1837
Mathias Agopian518ec112011-05-13 16:21:08 -07001838 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001839 egl_connection_t* const cnx = &gEGLImpl;
1840 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1841 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001842 }
1843 return result;
1844}
1845
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001846EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1847 clearError();
1848
1849 const egl_display_ptr dp = validate_display(dpy);
1850 if (!dp) return EGL_FALSE;
1851
1852 EGLBoolean result = EGL_FALSE;
1853 egl_connection_t* const cnx = &gEGLImpl;
1854 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1855 result = cnx->egl.eglSignalSyncKHR(
1856 dp->disp.dpy, sync, mode);
1857 }
1858 return result;
1859}
1860
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001861EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1862 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001863{
1864 clearError();
1865
Jesse Hallb29e5e82012-04-04 16:53:42 -07001866 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001867 if (!dp) return EGL_FALSE;
1868
Mathias Agopian737b8962017-02-24 14:32:05 -08001869 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001870 egl_connection_t* const cnx = &gEGLImpl;
1871 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1872 result = cnx->egl.eglClientWaitSyncKHR(
1873 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001874 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001875 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001876}
1877
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001878EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1879 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001880{
1881 clearError();
1882
Jesse Hallb29e5e82012-04-04 16:53:42 -07001883 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001884 if (!dp) return EGL_FALSE;
1885
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001886 EGLBoolean result = EGL_FALSE;
1887 egl_connection_t* const cnx = &gEGLImpl;
1888 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1889 result = cnx->egl.eglGetSyncAttribKHR(
1890 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001891 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001892 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001893}
1894
Season Li000d88f2015-07-01 11:39:40 -07001895EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1896{
1897 clearError();
1898
1899 const egl_display_ptr dp = validate_display(dpy);
1900 if (!dp) return EGL_NO_STREAM_KHR;
1901
1902 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1903 egl_connection_t* const cnx = &gEGLImpl;
1904 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1905 result = cnx->egl.eglCreateStreamKHR(
1906 dp->disp.dpy, attrib_list);
1907 }
1908 return result;
1909}
1910
1911EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1912{
1913 clearError();
1914
1915 const egl_display_ptr dp = validate_display(dpy);
1916 if (!dp) return EGL_FALSE;
1917
1918 EGLBoolean result = EGL_FALSE;
1919 egl_connection_t* const cnx = &gEGLImpl;
1920 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1921 result = cnx->egl.eglDestroyStreamKHR(
1922 dp->disp.dpy, stream);
1923 }
1924 return result;
1925}
1926
1927EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1928 EGLenum attribute, EGLint value)
1929{
1930 clearError();
1931
1932 const egl_display_ptr dp = validate_display(dpy);
1933 if (!dp) return EGL_FALSE;
1934
1935 EGLBoolean result = EGL_FALSE;
1936 egl_connection_t* const cnx = &gEGLImpl;
1937 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1938 result = cnx->egl.eglStreamAttribKHR(
1939 dp->disp.dpy, stream, attribute, value);
1940 }
1941 return result;
1942}
1943
1944EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1945 EGLenum attribute, EGLint *value)
1946{
1947 clearError();
1948
1949 const egl_display_ptr dp = validate_display(dpy);
1950 if (!dp) return EGL_FALSE;
1951
1952 EGLBoolean result = EGL_FALSE;
1953 egl_connection_t* const cnx = &gEGLImpl;
1954 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1955 result = cnx->egl.eglQueryStreamKHR(
1956 dp->disp.dpy, stream, attribute, value);
1957 }
1958 return result;
1959}
1960
1961EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1962 EGLenum attribute, EGLuint64KHR *value)
1963{
1964 clearError();
1965
1966 const egl_display_ptr dp = validate_display(dpy);
1967 if (!dp) return EGL_FALSE;
1968
1969 EGLBoolean result = EGL_FALSE;
1970 egl_connection_t* const cnx = &gEGLImpl;
1971 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1972 result = cnx->egl.eglQueryStreamu64KHR(
1973 dp->disp.dpy, stream, attribute, value);
1974 }
1975 return result;
1976}
1977
1978EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1979 EGLenum attribute, EGLTimeKHR *value)
1980{
1981 clearError();
1982
1983 const egl_display_ptr dp = validate_display(dpy);
1984 if (!dp) return EGL_FALSE;
1985
1986 EGLBoolean result = EGL_FALSE;
1987 egl_connection_t* const cnx = &gEGLImpl;
1988 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1989 result = cnx->egl.eglQueryStreamTimeKHR(
1990 dp->disp.dpy, stream, attribute, value);
1991 }
1992 return result;
1993}
1994
1995EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1996 EGLStreamKHR stream, const EGLint *attrib_list)
1997{
1998 clearError();
1999
2000 egl_display_ptr dp = validate_display(dpy);
2001 if (!dp) return EGL_NO_SURFACE;
2002
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06002003 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
2004 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
2005 // TODO: Probably need to update EGL_KHR_stream_producer_eglsurface to
2006 // indicate support for EGL_GL_COLORSPACE_KHR.
2007 // now select a corresponding sRGB format if needed
2008 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
2009 ALOGE("error invalid colorspace: %d", colorSpace);
2010 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
2011 }
2012
Season Li000d88f2015-07-01 11:39:40 -07002013 egl_connection_t* const cnx = &gEGLImpl;
2014 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
2015 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
2016 dp->disp.dpy, config, stream, attrib_list);
2017 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06002018 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Season Li000d88f2015-07-01 11:39:40 -07002019 return s;
2020 }
2021 }
2022 return EGL_NO_SURFACE;
2023}
2024
2025EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
2026 EGLStreamKHR stream)
2027{
2028 clearError();
2029
2030 const egl_display_ptr dp = validate_display(dpy);
2031 if (!dp) return EGL_FALSE;
2032
2033 EGLBoolean result = EGL_FALSE;
2034 egl_connection_t* const cnx = &gEGLImpl;
2035 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
2036 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
2037 dp->disp.dpy, stream);
2038 }
2039 return result;
2040}
2041
2042EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
2043 EGLStreamKHR stream)
2044{
2045 clearError();
2046
2047 const egl_display_ptr dp = validate_display(dpy);
2048 if (!dp) return EGL_FALSE;
2049
2050 EGLBoolean result = EGL_FALSE;
2051 egl_connection_t* const cnx = &gEGLImpl;
2052 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
2053 result = cnx->egl.eglStreamConsumerAcquireKHR(
2054 dp->disp.dpy, stream);
2055 }
2056 return result;
2057}
2058
2059EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2060 EGLStreamKHR stream)
2061{
2062 clearError();
2063
2064 const egl_display_ptr dp = validate_display(dpy);
2065 if (!dp) return EGL_FALSE;
2066
2067 EGLBoolean result = EGL_FALSE;
2068 egl_connection_t* const cnx = &gEGLImpl;
2069 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2070 result = cnx->egl.eglStreamConsumerReleaseKHR(
2071 dp->disp.dpy, stream);
2072 }
2073 return result;
2074}
2075
2076EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2077 EGLDisplay dpy, EGLStreamKHR stream)
2078{
2079 clearError();
2080
2081 const egl_display_ptr dp = validate_display(dpy);
2082 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2083
2084 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2085 egl_connection_t* const cnx = &gEGLImpl;
2086 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2087 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2088 dp->disp.dpy, stream);
2089 }
2090 return result;
2091}
2092
2093EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2094 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2095{
2096 clearError();
2097
2098 const egl_display_ptr dp = validate_display(dpy);
2099 if (!dp) return EGL_NO_STREAM_KHR;
2100
2101 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2102 egl_connection_t* const cnx = &gEGLImpl;
2103 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2104 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2105 dp->disp.dpy, file_descriptor);
2106 }
2107 return result;
2108}
2109
Mathias Agopian518ec112011-05-13 16:21:08 -07002110// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002111// EGL_EGLEXT_VERSION 15
2112// ----------------------------------------------------------------------------
2113
2114EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2115 clearError();
2116 const egl_display_ptr dp = validate_display(dpy);
2117 if (!dp) return EGL_FALSE;
2118 EGLint result = EGL_FALSE;
2119 egl_connection_t* const cnx = &gEGLImpl;
2120 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2121 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2122 }
2123 return result;
2124}
2125
2126// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002127// ANDROID extensions
2128// ----------------------------------------------------------------------------
2129
Jamie Gennis331841b2012-09-06 14:52:00 -07002130EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2131{
2132 clearError();
2133
2134 const egl_display_ptr dp = validate_display(dpy);
2135 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2136
2137 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2138 egl_connection_t* const cnx = &gEGLImpl;
2139 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2140 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2141 }
2142 return result;
2143}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002144
Andy McFadden72841452013-03-01 16:25:32 -08002145EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2146 EGLnsecsANDROID time)
2147{
2148 clearError();
2149
2150 const egl_display_ptr dp = validate_display(dpy);
2151 if (!dp) {
2152 return EGL_FALSE;
2153 }
2154
2155 SurfaceRef _s(dp.get(), surface);
2156 if (!_s.get()) {
2157 setError(EGL_BAD_SURFACE, EGL_FALSE);
2158 return EGL_FALSE;
2159 }
2160
2161 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002162 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002163
2164 return EGL_TRUE;
2165}
2166
Craig Donner60761072017-01-27 12:30:44 -08002167EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2168 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002169 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2170 // this function cannot be implemented when this libEGL is built for
2171 // vendors.
2172#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002173 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002174 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002175#else
2176 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2177#endif
Craig Donner60761072017-01-27 12:30:44 -08002178}
2179
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002180// ----------------------------------------------------------------------------
2181// NVIDIA extensions
2182// ----------------------------------------------------------------------------
2183EGLuint64NV eglGetSystemTimeFrequencyNV()
2184{
2185 clearError();
2186
2187 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002188 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002189 }
2190
2191 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002192 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002193
Mathias Agopianada798b2012-02-13 17:09:30 -08002194 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2195 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002196 }
2197
Mathias Agopian737b8962017-02-24 14:32:05 -08002198 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002199}
2200
2201EGLuint64NV eglGetSystemTimeNV()
2202{
2203 clearError();
2204
2205 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002206 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002207 }
2208
2209 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002210 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002211
Mathias Agopianada798b2012-02-13 17:09:30 -08002212 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2213 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002214 }
2215
Mathias Agopian737b8962017-02-24 14:32:05 -08002216 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002217}
Dan Stozaa894d082015-02-19 15:27:36 -08002218
2219// ----------------------------------------------------------------------------
2220// Partial update extension
2221// ----------------------------------------------------------------------------
2222EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2223 EGLint *rects, EGLint n_rects)
2224{
2225 clearError();
2226
2227 const egl_display_ptr dp = validate_display(dpy);
2228 if (!dp) {
2229 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2230 return EGL_FALSE;
2231 }
2232
2233 SurfaceRef _s(dp.get(), surface);
2234 if (!_s.get()) {
2235 setError(EGL_BAD_SURFACE, EGL_FALSE);
2236 return EGL_FALSE;
2237 }
2238
2239 egl_surface_t const * const s = get_surface(surface);
2240 if (s->cnx->egl.eglSetDamageRegionKHR) {
2241 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2242 rects, n_rects);
2243 }
2244
2245 return EGL_FALSE;
2246}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002247
Brian Anderson1049d1d2016-12-16 17:25:57 -08002248EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2249 EGLuint64KHR *frameId) {
2250 clearError();
2251
2252 const egl_display_ptr dp = validate_display(dpy);
2253 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002254 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002255 }
2256
2257 SurfaceRef _s(dp.get(), surface);
2258 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002259 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002260 }
2261
2262 egl_surface_t const * const s = get_surface(surface);
2263
Mathias Agopian65421432017-03-08 11:49:05 -08002264 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002265 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002266 }
2267
2268 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002269 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002270
Mathias Agopian65421432017-03-08 11:49:05 -08002271 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002272 // This should not happen. Return an error that is not in the spec
2273 // so it's obvious something is very wrong.
2274 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002275 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002276 }
2277
2278 *frameId = nextFrameId;
2279 return EGL_TRUE;
2280}
2281
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002282EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2283 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2284{
2285 clearError();
2286
2287 const egl_display_ptr dp = validate_display(dpy);
2288 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002289 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002290 }
2291
2292 SurfaceRef _s(dp.get(), surface);
2293 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002294 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002295 }
2296
2297 egl_surface_t const * const s = get_surface(surface);
2298
Mathias Agopian65421432017-03-08 11:49:05 -08002299 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002300 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002301 }
2302
2303 nsecs_t* compositeDeadline = nullptr;
2304 nsecs_t* compositeInterval = nullptr;
2305 nsecs_t* compositeToPresentLatency = nullptr;
2306
2307 for (int i = 0; i < numTimestamps; i++) {
2308 switch (names[i]) {
2309 case EGL_COMPOSITE_DEADLINE_ANDROID:
2310 compositeDeadline = &values[i];
2311 break;
2312 case EGL_COMPOSITE_INTERVAL_ANDROID:
2313 compositeInterval = &values[i];
2314 break;
2315 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2316 compositeToPresentLatency = &values[i];
2317 break;
2318 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002319 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002320 }
2321 }
2322
Mathias Agopian65421432017-03-08 11:49:05 -08002323 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002324 compositeDeadline, compositeInterval, compositeToPresentLatency);
2325
2326 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002327 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002328 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002329 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002330 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002331 default:
2332 // This should not happen. Return an error that is not in the spec
2333 // so it's obvious something is very wrong.
2334 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002335 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002336 }
2337}
2338
2339EGLBoolean eglGetCompositorTimingSupportedANDROID(
2340 EGLDisplay dpy, EGLSurface surface, EGLint name)
2341{
2342 clearError();
2343
2344 const egl_display_ptr dp = validate_display(dpy);
2345 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002346 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002347 }
2348
2349 SurfaceRef _s(dp.get(), surface);
2350 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002351 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002352 }
2353
2354 egl_surface_t const * const s = get_surface(surface);
2355
Mathias Agopian65421432017-03-08 11:49:05 -08002356 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002357 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002358 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002359 }
2360
2361 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002362 case EGL_COMPOSITE_DEADLINE_ANDROID:
2363 case EGL_COMPOSITE_INTERVAL_ANDROID:
2364 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2365 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002366 default:
2367 return EGL_FALSE;
2368 }
2369}
2370
Pablo Ceballosc18be292016-05-31 14:55:42 -07002371EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002372 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002373 EGLnsecsANDROID *values)
2374{
2375 clearError();
2376
2377 const egl_display_ptr dp = validate_display(dpy);
2378 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002379 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002380 }
2381
2382 SurfaceRef _s(dp.get(), surface);
2383 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002384 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002385 }
2386
2387 egl_surface_t const * const s = get_surface(surface);
2388
Mathias Agopian65421432017-03-08 11:49:05 -08002389 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002390 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002391 }
2392
Brian Andersondbd0ea82016-07-22 09:38:59 -07002393 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002394 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002395 nsecs_t* latchTime = nullptr;
2396 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002397 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002398 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002399 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002400 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002401 nsecs_t* releaseTime = nullptr;
2402
2403 for (int i = 0; i < numTimestamps; i++) {
2404 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002405 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2406 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002407 break;
2408 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2409 acquireTime = &values[i];
2410 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002411 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2412 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002413 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002414 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2415 firstRefreshStartTime = &values[i];
2416 break;
2417 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2418 lastRefreshStartTime = &values[i];
2419 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002420 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2421 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002422 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002423 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2424 displayPresentTime = &values[i];
2425 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002426 case EGL_DEQUEUE_READY_TIME_ANDROID:
2427 dequeueReadyTime = &values[i];
2428 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002429 case EGL_READS_DONE_TIME_ANDROID:
2430 releaseTime = &values[i];
2431 break;
2432 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002433 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002434 }
2435 }
2436
Mathias Agopian65421432017-03-08 11:49:05 -08002437 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002438 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002439 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002440 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002441
Brian Anderson069b3652016-07-22 10:32:47 -07002442 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002443 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002444 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002445 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002446 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002447 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002448 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002449 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002450 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2451 default:
2452 // This should not happen. Return an error that is not in the spec
2453 // so it's obvious something is very wrong.
2454 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2455 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002456 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002457}
2458
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002459EGLBoolean eglGetFrameTimestampSupportedANDROID(
2460 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002461{
2462 clearError();
2463
2464 const egl_display_ptr dp = validate_display(dpy);
2465 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002466 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002467 }
2468
2469 SurfaceRef _s(dp.get(), surface);
2470 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002471 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002472 }
2473
2474 egl_surface_t const * const s = get_surface(surface);
2475
Mathias Agopian65421432017-03-08 11:49:05 -08002476 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002477 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002478 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002479 }
2480
2481 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002482 case EGL_COMPOSITE_DEADLINE_ANDROID:
2483 case EGL_COMPOSITE_INTERVAL_ANDROID:
2484 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002485 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002486 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002487 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2488 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2489 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002490 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002491 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002492 case EGL_READS_DONE_TIME_ANDROID:
2493 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002494 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2495 int value = 0;
2496 window->query(window,
2497 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2498 return value == 0 ? EGL_FALSE : EGL_TRUE;
2499 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002500 default:
2501 return EGL_FALSE;
2502 }
2503}