blob: f3a9ad8b88f064b147072e5f2dcf571158eb0f63 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
Craig Donner60761072017-01-27 12:30:44 -080029#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080030#include <private/android/AHardwareBufferHelpers.h>
31
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Mathias Agopian65421432017-03-08 11:49:05 -080036#include <condition_variable>
37#include <deque>
38#include <mutex>
39#include <unordered_map>
40#include <string>
41#include <thread>
Mathias Agopian518ec112011-05-13 16:21:08 -070042
Mathias Agopian39c24a22013-04-04 23:17:56 -070043#include "../egl_impl.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070044
45#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070046#include "egl_object.h"
47#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080048#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070049
50using namespace android;
51
52// ----------------------------------------------------------------------------
53
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070054namespace android {
55
Mathias Agopian65421432017-03-08 11:49:05 -080056using nsecs_t = int64_t;
57
Mathias Agopian518ec112011-05-13 16:21:08 -070058struct extention_map_t {
59 const char* name;
60 __eglMustCastToProperFunctionPointerType address;
61};
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063/*
Jesse Hall21558da2013-08-06 15:31:22 -070064 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070065 *
Jesse Hall21558da2013-08-06 15:31:22 -070066 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
67 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070068 *
Jesse Hall21558da2013-08-06 15:31:22 -070069 * The rest (gExtensionString) depend on support in the EGL driver, and are
70 * only available if the driver supports them. However, some of these must be
71 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080072 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070073 * the mandatory extensions are present and may not function properly if some
74 * are missing.
75 *
76 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070077 */
Mathias Agopian737b8962017-02-24 14:32:05 -080078
Mathias Agopian311b4792017-02-28 15:00:49 -080079extern char const * const gBuiltinExtensionString;
80extern char const * const gExtensionString;
Mathias Agopian737b8962017-02-24 14:32:05 -080081
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -060082// clang-format off
Mathias Agopian311b4792017-02-28 15:00:49 -080083char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070084 "EGL_KHR_get_all_proc_addresses "
85 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080086 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080087 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080088 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070089 "EGL_ANDROID_get_frame_timestamps "
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -070090 "EGL_EXT_surface_SMPTE2086_metadata "
91 "EGL_EXT_surface_CTA861_3_metadata "
Jesse Hall21558da2013-08-06 15:31:22 -070092 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080093
94char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095 "EGL_KHR_image " // mandatory
96 "EGL_KHR_image_base " // mandatory
Krzysztof KosiƄski4245cba2018-03-30 16:21:39 -070097 "EGL_KHR_image_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070098 "EGL_KHR_image_pixmap "
99 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700100 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700101 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700102 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_cubemap_image "
104 "EGL_KHR_gl_renderbuffer_image "
105 "EGL_KHR_reusable_sync "
106 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700107 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700108 "EGL_KHR_config_attribs "
109 "EGL_KHR_surfaceless_context "
110 "EGL_KHR_stream "
111 "EGL_KHR_stream_fifo "
112 "EGL_KHR_stream_producer_eglsurface "
113 "EGL_KHR_stream_consumer_gltexture "
114 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700115 "EGL_EXT_create_context_robustness "
116 "EGL_NV_system_time "
117 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700118 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700119 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800120 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700121 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800122 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700123 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700124 "EGL_KHR_mutable_render_buffer "
Mika IsojÀrvif37864b2016-04-15 11:58:56 -0700125 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700126 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000127 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800128 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700129 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600130// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700131
132// extensions not exposed to applications but used by the ANDROID system
133// "EGL_ANDROID_blob_cache " // strongly recommended
134// "EGL_IMG_hibernate_process " // optional
135// "EGL_ANDROID_native_fence_sync " // strongly recommended
136// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700137// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700138
139/*
140 * EGL Extensions entry-points exposed to 3rd party applications
141 * (keep in sync with gExtensionString above)
142 *
143 */
144static const extention_map_t sExtensionMap[] = {
145 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700146 { "eglLockSurfaceKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
148 { "eglUnlockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700150
151 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700152 { "eglCreateImageKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
154 { "eglDestroyImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700156
157 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
158 { "eglCreateSyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
160 { "eglDestroySyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
162 { "eglClientWaitSyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
164 { "eglSignalSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
166 { "eglGetSyncAttribKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
168
169 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800170 { "eglGetSystemTimeFrequencyNV",
171 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
172 { "eglGetSystemTimeNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700174
Mathias Agopian2bb71682013-03-27 17:32:41 -0700175 // EGL_KHR_wait_sync
176 { "eglWaitSyncKHR",
177 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700178
179 // EGL_ANDROID_presentation_time
180 { "eglPresentationTimeANDROID",
181 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800182
183 // EGL_KHR_swap_buffers_with_damage
184 { "eglSwapBuffersWithDamageKHR",
185 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
186
Craig Donner60761072017-01-27 12:30:44 -0800187 // EGL_ANDROID_get_native_client_buffer
188 { "eglGetNativeClientBufferANDROID",
189 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
190
Dan Stozaa894d082015-02-19 15:27:36 -0800191 // EGL_KHR_partial_update
192 { "eglSetDamageRegionKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700194
195 { "eglCreateStreamKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
197 { "eglDestroyStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
199 { "eglStreamAttribKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
201 { "eglQueryStreamKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
203 { "eglQueryStreamu64KHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
205 { "eglQueryStreamTimeKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
207 { "eglCreateStreamProducerSurfaceKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
209 { "eglStreamConsumerGLTextureExternalKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
211 { "eglStreamConsumerAcquireKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
213 { "eglStreamConsumerReleaseKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
215 { "eglGetStreamFileDescriptorKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
217 { "eglCreateStreamFromFileDescriptorKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700219
220 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800221 { "eglGetNextFrameIdANDROID",
222 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800223 { "eglGetCompositorTimingANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
225 { "eglGetCompositorTimingSupportedANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700227 { "eglGetFrameTimestampsANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800229 { "eglGetFrameTimestampSupportedANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700231
232 // EGL_ANDROID_native_fence_sync
233 { "eglDupNativeFenceFDANDROID",
234 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700235};
236
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700237/*
238 * These extensions entry-points should not be exposed to applications.
239 * They're used internally by the Android EGL layer.
240 */
241#define FILTER_EXTENSIONS(procname) \
242 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
243 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700244 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700245
Mathias Agopian518ec112011-05-13 16:21:08 -0700246// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800247static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
248
Mathias Agopian518ec112011-05-13 16:21:08 -0700249static int sGLExtentionSlot = 0;
250static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
251
252static void(*findProcAddress(const char* name,
253 const extention_map_t* map, size_t n))() {
254 for (uint32_t i=0 ; i<n ; i++) {
255 if (!strcmp(name, map[i].name)) {
256 return map[i].address;
257 }
258 }
259 return NULL;
260}
261
262// ----------------------------------------------------------------------------
263
Mathias Agopian518ec112011-05-13 16:21:08 -0700264extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
265extern EGLBoolean egl_init_drivers();
266extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700267extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700268
Mathias Agopian518ec112011-05-13 16:21:08 -0700269} // namespace android;
270
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700271
Mathias Agopian518ec112011-05-13 16:21:08 -0700272// ----------------------------------------------------------------------------
273
274static inline void clearError() { egl_tls_t::clearError(); }
275static inline EGLContext getContext() { return egl_tls_t::getContext(); }
276
277// ----------------------------------------------------------------------------
278
279EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
280{
Jesse Hall1508ae62017-01-19 17:43:26 -0800281 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700282 clearError();
283
Dan Stozac3289c42014-01-17 11:38:34 -0800284 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700285 if (index >= NUM_DISPLAYS) {
286 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
287 }
288
289 if (egl_init_drivers() == EGL_FALSE) {
290 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
291 }
292
293 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
294 return dpy;
295}
296
297// ----------------------------------------------------------------------------
298// Initialization
299// ----------------------------------------------------------------------------
300
301EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
302{
303 clearError();
304
Jesse Hallb29e5e82012-04-04 16:53:42 -0700305 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800306 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700307
308 EGLBoolean res = dp->initialize(major, minor);
309
310 return res;
311}
312
313EGLBoolean eglTerminate(EGLDisplay dpy)
314{
315 // NOTE: don't unload the drivers b/c some APIs can be called
316 // after eglTerminate() has been called. eglTerminate() only
317 // terminates an EGLDisplay, not a EGL itself.
318
319 clearError();
320
Jesse Hallb29e5e82012-04-04 16:53:42 -0700321 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800322 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700323
324 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800325
Mathias Agopian518ec112011-05-13 16:21:08 -0700326 return res;
327}
328
329// ----------------------------------------------------------------------------
330// configuration
331// ----------------------------------------------------------------------------
332
333EGLBoolean eglGetConfigs( EGLDisplay dpy,
334 EGLConfig *configs,
335 EGLint config_size, EGLint *num_config)
336{
337 clearError();
338
Jesse Hallb29e5e82012-04-04 16:53:42 -0700339 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700340 if (!dp) return EGL_FALSE;
341
Mathias Agopian7773c432012-02-13 20:06:08 -0800342 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800343 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700344 }
345
Mathias Agopian7773c432012-02-13 20:06:08 -0800346 EGLBoolean res = EGL_FALSE;
347 *num_config = 0;
348
349 egl_connection_t* const cnx = &gEGLImpl;
350 if (cnx->dso) {
351 res = cnx->egl.eglGetConfigs(
352 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700353 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800354
355 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700356}
357
358EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
359 EGLConfig *configs, EGLint config_size,
360 EGLint *num_config)
361{
362 clearError();
363
Jesse Hallb29e5e82012-04-04 16:53:42 -0700364 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700365 if (!dp) return EGL_FALSE;
366
367 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800368 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700369 }
370
Mathias Agopian518ec112011-05-13 16:21:08 -0700371 EGLBoolean res = EGL_FALSE;
372 *num_config = 0;
373
Mathias Agopianada798b2012-02-13 17:09:30 -0800374 egl_connection_t* const cnx = &gEGLImpl;
375 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700376 if (attrib_list) {
377 char value[PROPERTY_VALUE_MAX];
378 property_get("debug.egl.force_msaa", value, "false");
379
380 if (!strcmp(value, "true")) {
381 size_t attribCount = 0;
382 EGLint attrib = attrib_list[0];
383
384 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700385 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700386 const EGLint *attribRendererable = NULL;
387 const EGLint *attribCaveat = NULL;
388
389 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700390 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700391 while (attrib != EGL_NONE) {
392 attrib = attrib_list[attribCount];
393 switch (attrib) {
394 case EGL_RENDERABLE_TYPE:
395 attribRendererable = &attrib_list[attribCount];
396 break;
397 case EGL_CONFIG_CAVEAT:
398 attribCaveat = &attrib_list[attribCount];
399 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800400 default:
401 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700402 }
403 attribCount++;
404 }
405
406 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
407 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800408
Romain Guy1cffc802012-10-15 18:13:05 -0700409 // Insert 2 extra attributes to force-enable MSAA 4x
410 EGLint aaAttribs[attribCount + 4];
411 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
412 aaAttribs[1] = 1;
413 aaAttribs[2] = EGL_SAMPLES;
414 aaAttribs[3] = 4;
415
416 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
417
418 EGLint numConfigAA;
419 EGLBoolean resAA = cnx->egl.eglChooseConfig(
420 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
421
422 if (resAA == EGL_TRUE && numConfigAA > 0) {
423 ALOGD("Enabling MSAA 4x");
424 *num_config = numConfigAA;
425 return resAA;
426 }
427 }
428 }
429 }
430
Mathias Agopian7773c432012-02-13 20:06:08 -0800431 res = cnx->egl.eglChooseConfig(
432 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700433 }
434 return res;
435}
436
437EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
438 EGLint attribute, EGLint *value)
439{
440 clearError();
441
Jesse Hallb29e5e82012-04-04 16:53:42 -0700442 egl_connection_t* cnx = NULL;
443 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
444 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800445
Mathias Agopian518ec112011-05-13 16:21:08 -0700446 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800447 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700448}
449
450// ----------------------------------------------------------------------------
451// surfaces
452// ----------------------------------------------------------------------------
453
Jesse Hallc2e41222013-08-08 13:40:22 -0700454// Turn linear formats into corresponding sRGB formats when colorspace is
455// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
456// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800457// the modification isn't possible, the original dataSpace is returned.
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600458static android_dataspace modifyBufferDataspace(android_dataspace dataSpace,
459 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800461 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700462 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800463 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600464 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
465 return HAL_DATASPACE_DISPLAY_P3;
466 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
467 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600468 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
469 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600470 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
471 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700472 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT) {
473 return HAL_DATASPACE_BT2020_LINEAR;
474 } else if (colorspace == EGL_GL_COLORSPACE_BT2020_PQ_EXT) {
475 return HAL_DATASPACE_BT2020_PQ;
Jesse Hallc2e41222013-08-08 13:40:22 -0700476 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800477 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700478}
479
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700480// stripAttributes is used by eglCreateWindowSurface, eglCreatePbufferSurface
481// and eglCreatePixmapSurface to clean up color space related Window parameters
482// that a driver does not advertise support for.
483// Return true if stripped_attrib_list has stripped contents.
484
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700485static EGLBoolean stripAttributes(egl_display_ptr dp, const EGLint* attrib_list,
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -0800486 EGLint format,
487 std::vector<EGLint>& stripped_attrib_list) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600488 std::vector<EGLint> allowedColorSpaces;
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700489 bool haveColorSpaceSupport = dp->haveExtension("EGL_KHR_gl_colorspace");
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600490 switch (format) {
491 case HAL_PIXEL_FORMAT_RGBA_8888:
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700492 if (haveColorSpaceSupport) {
493 // Spec says:
494 // [fn1] Only OpenGL and OpenGL ES contexts which support sRGB
495 // rendering must respect requests for EGL_GL_COLORSPACE_SRGB_KHR, and
496 // only to sRGB formats supported by the context (normally just SRGB8)
497 // Older versions not supporting sRGB rendering will ignore this
498 // surface attribute.
499 //
500 // We support sRGB and pixel format is SRGB8, so allow
501 // the EGL_GL_COLORSPACE_SRGB_KHR and
502 // EGL_GL_COLORSPACE_LINEAR_KHR
503 // colorspaces to be specified.
504
505 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
506 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
507 }
508 if (findExtension(dp->disp.queryString.extensions,
509 "EGL_EXT_gl_colorspace_display_p3_linear")) {
510 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT);
511 }
512 if (findExtension(dp->disp.queryString.extensions,
513 "EGL_EXT_gl_colorspace_display_p3")) {
514 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_DISPLAY_P3_EXT);
515 }
516 if (findExtension(dp->disp.queryString.extensions,
517 "EGL_EXT_gl_colorspace_bt2020_linear")) {
518 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_LINEAR_EXT);
519 }
520 if (findExtension(dp->disp.queryString.extensions,
521 "EGL_EXT_gl_colorspace_bt2020_pq")) {
522 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_BT2020_PQ_EXT);
523 }
524 if (findExtension(dp->disp.queryString.extensions,
525 "EGL_EXT_gl_colorspace_scrgb_linear")) {
526 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT);
527 }
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600528 break;
529
530 case HAL_PIXEL_FORMAT_RGBA_FP16:
531 case HAL_PIXEL_FORMAT_RGBA_1010102:
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700532 case HAL_PIXEL_FORMAT_RGB_565:
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600533 // Future: if driver supports XXXX extension, we can pass down that colorspace
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700534 default:
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600535 break;
536 }
537
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700538 if (!attrib_list) return false;
539
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600540 bool stripped = false;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700541 for (const EGLint* attr = attrib_list; attr[0] != EGL_NONE; attr += 2) {
542 switch (attr[0]) {
543 case EGL_GL_COLORSPACE_KHR: {
544 EGLint colorSpace = attr[1];
545 bool found = false;
546 // Verify that color space is allowed
547 for (auto it : allowedColorSpaces) {
548 if (colorSpace == it) {
549 found = true;
550 }
551 }
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700552 if (found) {
553 // Found supported attribute
554 stripped_attrib_list.push_back(attr[0]);
555 stripped_attrib_list.push_back(attr[1]);
556 } else if (!haveColorSpaceSupport) {
557 // Device does not support colorspace extension
558 // pass on the attribute and let downstream
559 // components validate like normal
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700560 stripped_attrib_list.push_back(attr[0]);
561 stripped_attrib_list.push_back(attr[1]);
Courtney Goeltzenleuchter52de2fd2018-02-01 09:51:58 -0700562 } else {
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700563 // Found supported attribute that driver does not
564 // support, strip it.
Courtney Goeltzenleuchter52de2fd2018-02-01 09:51:58 -0700565 stripped = true;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600566 }
567 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700568 break;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700569 default:
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600570 stripped_attrib_list.push_back(attr[0]);
571 stripped_attrib_list.push_back(attr[1]);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700572 break;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600573 }
574 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700575
576 // Make sure there is at least one attribute
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600577 if (stripped) {
578 stripped_attrib_list.push_back(EGL_NONE);
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600579 }
580 return stripped;
581}
582
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600583static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, NativeWindowType window,
584 const EGLint* attrib_list, EGLint& colorSpace,
585 android_dataspace& dataSpace) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600586 colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
587 dataSpace = HAL_DATASPACE_UNKNOWN;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600588
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600589 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
590 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
591 if (*attr == EGL_GL_COLORSPACE_KHR) {
592 colorSpace = attr[1];
593 bool found = false;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600594 bool verify = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600595 // Verify that color space is allowed
596 if (colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
597 colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600598 // SRGB and LINEAR are always supported when EGL_KHR_gl_colorspace
599 // is available, so no need to verify.
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600600 found = true;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600601 verify = false;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700602 } else if (colorSpace == EGL_GL_COLORSPACE_BT2020_LINEAR_EXT &&
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600603 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
604 found = true;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700605 } else if (colorSpace == EGL_GL_COLORSPACE_BT2020_PQ_EXT &&
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600606 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
607 found = true;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600608 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_EXT &&
609 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb")) {
610 found = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600611 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
612 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
613 found = true;
614 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
615 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
616 found = true;
617 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
618 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
619 found = true;
620 }
621 if (!found) {
622 return false;
623 }
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600624 if (verify && window) {
625 bool wide_color_support = true;
626 // Ordinarily we'd put a call to native_window_get_wide_color_support
627 // at the beginning of the function so that we'll have the
628 // result when needed elsewhere in the function.
629 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
630 // SurfaceFlinger is required to answer the call below we would
631 // end up in a deadlock situation. By moving the call to only happen
632 // if the application has specifically asked for wide-color we avoid
633 // the deadlock with SurfaceFlinger since it will not ask for a
634 // wide-color surface.
635 int err = native_window_get_wide_color_support(window, &wide_color_support);
636
637 if (err) {
638 ALOGE("getColorSpaceAttribute: invalid window (win=%p) "
639 "failed (%#x) (already connected to another API?)",
640 window, err);
641 return false;
642 }
643 if (!wide_color_support) {
644 // Application has asked for a wide-color colorspace but
645 // wide-color support isn't available on the display the window is on.
646 return false;
647 }
648 }
Courtney Goeltzenleuchter1d4f7a22017-05-05 13:30:25 -0600649 // Only change the dataSpace from default if the application
650 // has explicitly set the color space with a EGL_GL_COLORSPACE_KHR attribute.
651 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600652 }
653 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600654 }
655 return true;
656}
657
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600658static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
659 EGLint& colorSpace, android_dataspace& dataSpace) {
660 return getColorSpaceAttribute(dp, NULL, attrib_list, colorSpace, dataSpace);
661}
662
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600663void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config, EGLint& format) {
664 // Set the native window's buffers format to match what this config requests.
665 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
666 // of our native format. So if sRGB gamma is requested, we have to
667 // modify the EGLconfig's format before setting the native window's
668 // format.
669
670 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
671 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
672
673 EGLint a = 0;
674 EGLint r, g, b;
675 r = g = b = 0;
676 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
677 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
678 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
679 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
680 EGLint colorDepth = r + g + b;
681
682 // Today, the driver only understands sRGB and linear on 888X
683 // formats. Strip other colorspaces from the attribute list and
684 // only use them to set the dataspace via
685 // native_window_set_buffers_dataspace
686 // if pixel format is RGBX 8888
687 // TBD: Can test for future extensions that indicate that driver
688 // handles requested color space and we can let it through.
689 // allow SRGB and LINEAR. All others need to be stripped.
690 // else if 565, 4444
691 // TBD: Can we assume these are supported if 8888 is?
692 // else if FP16 or 1010102
693 // strip colorspace from attribs.
694 // endif
695 if (a == 0) {
696 if (colorDepth <= 16) {
697 format = HAL_PIXEL_FORMAT_RGB_565;
698 } else {
699 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
700 if (colorDepth > 24) {
701 format = HAL_PIXEL_FORMAT_RGBA_1010102;
702 } else {
703 format = HAL_PIXEL_FORMAT_RGBX_8888;
704 }
705 } else {
706 format = HAL_PIXEL_FORMAT_RGBA_FP16;
707 }
708 }
709 } else {
710 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
711 if (colorDepth > 24) {
712 format = HAL_PIXEL_FORMAT_RGBA_1010102;
713 } else {
714 format = HAL_PIXEL_FORMAT_RGBA_8888;
715 }
716 } else {
717 format = HAL_PIXEL_FORMAT_RGBA_FP16;
718 }
719 }
720}
721
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700722EGLBoolean sendSurfaceMetadata(egl_surface_t* s) {
723 android_smpte2086_metadata smpteMetadata;
724 if (s->getSmpte2086Metadata(smpteMetadata)) {
725 int err =
726 native_window_set_buffers_smpte2086_metadata(s->getNativeWindow(), &smpteMetadata);
727 s->resetSmpte2086Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700728 if (err != 0) {
729 ALOGE("error setting native window smpte2086 metadata: %s (%d)",
730 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700731 return EGL_FALSE;
732 }
733 }
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -0700734 android_cta861_3_metadata cta8613Metadata;
735 if (s->getCta8613Metadata(cta8613Metadata)) {
736 int err =
737 native_window_set_buffers_cta861_3_metadata(s->getNativeWindow(), &cta8613Metadata);
738 s->resetCta8613Metadata();
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700739 if (err != 0) {
740 ALOGE("error setting native window CTS 861.3 metadata: %s (%d)",
741 strerror(-err), err);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700742 return EGL_FALSE;
743 }
744 }
745 return EGL_TRUE;
746}
747
Mathias Agopian518ec112011-05-13 16:21:08 -0700748EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
749 NativeWindowType window,
750 const EGLint *attrib_list)
751{
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700752 const EGLint *origAttribList = attrib_list;
Mathias Agopian518ec112011-05-13 16:21:08 -0700753 clearError();
754
Jesse Hallb29e5e82012-04-04 16:53:42 -0700755 egl_connection_t* cnx = NULL;
756 egl_display_ptr dp = validate_display_connection(dpy, cnx);
757 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800758 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700759
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800760 if (!window) {
761 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
762 }
763
764 int value = 0;
765 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
766 if (!value) {
767 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
768 }
769
Andy McFaddend566ce32014-01-07 15:54:17 -0800770 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800771 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800772 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
773 "failed (%#x) (already connected to another API?)",
774 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700775 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700776 }
777
Mathias Agopian95921562017-02-24 14:31:31 -0800778 EGLint format;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600779 getNativePixelFormat(iDpy, cnx, config, format);
780
781 // now select correct colorspace and dataspace based on user's attribute list
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600782 EGLint colorSpace;
783 android_dataspace dataSpace;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600784 if (!getColorSpaceAttribute(dp, window, attrib_list, colorSpace, dataSpace)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600785 ALOGE("error invalid colorspace: %d", colorSpace);
786 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700787 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800788
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600789 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700790 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600791 // Had to modify the attribute list due to use of color space.
792 // Use modified list from here on.
793 attrib_list = strippedAttribList.data();
794 }
795
Jesse Hallc2e41222013-08-08 13:40:22 -0700796 if (format != 0) {
797 int err = native_window_set_buffers_format(window, format);
798 if (err != 0) {
799 ALOGE("error setting native window pixel format: %s (%d)",
800 strerror(-err), err);
801 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
802 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
803 }
804 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700805
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800806 if (dataSpace != 0) {
807 int err = native_window_set_buffers_data_space(window, dataSpace);
808 if (err != 0) {
809 ALOGE("error setting native window pixel dataSpace: %s (%d)",
810 strerror(-err), err);
811 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
812 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
813 }
814 }
815
Jamie Gennis59769462011-11-19 18:04:43 -0800816 // the EGL spec requires that a new EGLSurface default to swap interval
817 // 1, so explicitly set that on the window here.
818 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
819 anw->setSwapInterval(anw, 1);
820
Mathias Agopian518ec112011-05-13 16:21:08 -0700821 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800822 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700823 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600824 egl_surface_t* s =
825 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Courtney Goeltzenleuchtera1e59f12018-03-05 08:19:25 -0700826 return s;
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700828
829 // EGLSurface creation failed
830 native_window_set_buffers_format(window, 0);
831 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700832 }
833 return EGL_NO_SURFACE;
834}
835
836EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
837 NativePixmapType pixmap,
838 const EGLint *attrib_list)
839{
840 clearError();
841
Jesse Hallb29e5e82012-04-04 16:53:42 -0700842 egl_connection_t* cnx = NULL;
843 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600844 EGLint colorSpace;
845 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700846 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600847 // now select a corresponding sRGB format if needed
848 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
849 ALOGE("error invalid colorspace: %d", colorSpace);
850 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
851 }
852
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800854 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700855 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600856 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700857 return s;
858 }
859 }
860 return EGL_NO_SURFACE;
861}
862
863EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
864 const EGLint *attrib_list)
865{
866 clearError();
867
Jesse Hallb29e5e82012-04-04 16:53:42 -0700868 egl_connection_t* cnx = NULL;
869 egl_display_ptr dp = validate_display_connection(dpy, cnx);
870 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600871 EGLDisplay iDpy = dp->disp.dpy;
872 EGLint format;
873 getNativePixelFormat(iDpy, cnx, config, format);
874
875 // now select correct colorspace and dataspace based on user's attribute list
876 EGLint colorSpace;
877 android_dataspace dataSpace;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600878 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
879 ALOGE("error invalid colorspace: %d", colorSpace);
880 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
881 }
882
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600883 // Pbuffers are not displayed so we don't need to store the
884 // colorspace. We do need to filter out color spaces the
885 // driver doesn't know how to process.
886 std::vector<EGLint> strippedAttribList;
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700887 if (stripAttributes(dp, attrib_list, format, strippedAttribList)) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600888 // Had to modify the attribute list due to use of color space.
889 // Use modified list from here on.
890 attrib_list = strippedAttribList.data();
891 }
892
Mathias Agopian518ec112011-05-13 16:21:08 -0700893 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800894 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700895 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600896 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700897 return s;
898 }
899 }
900 return EGL_NO_SURFACE;
901}
Jesse Hall47743382013-02-08 11:13:46 -0800902
Mathias Agopian518ec112011-05-13 16:21:08 -0700903EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
904{
905 clearError();
906
Jesse Hallb29e5e82012-04-04 16:53:42 -0700907 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700908 if (!dp) return EGL_FALSE;
909
Jesse Hallb29e5e82012-04-04 16:53:42 -0700910 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700911 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800912 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700913
914 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800915 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700916 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700917 _s.terminate();
918 }
919 return result;
920}
921
922EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
923 EGLint attribute, EGLint *value)
924{
925 clearError();
926
Jesse Hallb29e5e82012-04-04 16:53:42 -0700927 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700928 if (!dp) return EGL_FALSE;
929
Jesse Hallb29e5e82012-04-04 16:53:42 -0700930 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700931 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800932 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700933
Mathias Agopian518ec112011-05-13 16:21:08 -0700934 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700935 if (s->getColorSpaceAttribute(attribute, value)) {
936 return EGL_TRUE;
937 } else if (s->getSmpte2086Attribute(attribute, value)) {
938 return EGL_TRUE;
939 } else if (s->getCta8613Attribute(attribute, value)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600940 return EGL_TRUE;
941 }
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700942 return s->cnx->egl.eglQuerySurface(dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700943}
944
Jamie Gennise8696a42012-01-15 18:54:57 -0800945void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800946 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800947 clearError();
948
Jesse Hallb29e5e82012-04-04 16:53:42 -0700949 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800950 if (!dp) {
951 return;
952 }
953
Jesse Hallb29e5e82012-04-04 16:53:42 -0700954 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800955 if (!_s.get()) {
956 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800957 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800958}
959
Mathias Agopian518ec112011-05-13 16:21:08 -0700960// ----------------------------------------------------------------------------
961// Contexts
962// ----------------------------------------------------------------------------
963
964EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
965 EGLContext share_list, const EGLint *attrib_list)
966{
967 clearError();
968
Jesse Hallb29e5e82012-04-04 16:53:42 -0700969 egl_connection_t* cnx = NULL;
970 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700971 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700972 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700973 if (!ContextRef(dp.get(), share_list).get()) {
974 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
975 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700976 egl_context_t* const c = get_context(share_list);
977 share_list = c->context;
978 }
979 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800980 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700981 if (context != EGL_NO_CONTEXT) {
982 // figure out if it's a GLESv1 or GLESv2
983 int version = 0;
984 if (attrib_list) {
985 while (*attrib_list != EGL_NONE) {
986 GLint attr = *attrib_list++;
987 GLint value = *attrib_list++;
988 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
989 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800990 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800991 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800992 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700993 }
994 }
995 };
996 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700997 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
998 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700999 return c;
1000 }
1001 }
1002 return EGL_NO_CONTEXT;
1003}
1004
1005EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1006{
1007 clearError();
1008
Jesse Hallb29e5e82012-04-04 16:53:42 -07001009 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001010 if (!dp)
1011 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001012
Jesse Hallb29e5e82012-04-04 16:53:42 -07001013 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001014 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001015 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -08001016
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -08001018 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 if (result == EGL_TRUE) {
1020 _c.terminate();
1021 }
1022 return result;
1023}
1024
Mathias Agopian518ec112011-05-13 16:21:08 -07001025EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
1026 EGLSurface read, EGLContext ctx)
1027{
1028 clearError();
1029
Jesse Hallb29e5e82012-04-04 16:53:42 -07001030 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -08001031 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001032
Mathias Agopian5b287a62011-05-16 18:58:55 -07001033 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
1034 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
1035 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -07001036 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
1037 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001038 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001039 }
1040
1041 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -07001042 ContextRef _c(dp.get(), ctx);
1043 SurfaceRef _d(dp.get(), draw);
1044 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001045
1046 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -07001047 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001048 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -08001049 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001050 }
1051
1052 // these are the underlying implementation's object
1053 EGLContext impl_ctx = EGL_NO_CONTEXT;
1054 EGLSurface impl_draw = EGL_NO_SURFACE;
1055 EGLSurface impl_read = EGL_NO_SURFACE;
1056
1057 // these are our objects structs passed in
1058 egl_context_t * c = NULL;
1059 egl_surface_t const * d = NULL;
1060 egl_surface_t const * r = NULL;
1061
1062 // these are the current objects structs
1063 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -08001064
Mathias Agopian518ec112011-05-13 16:21:08 -07001065 if (ctx != EGL_NO_CONTEXT) {
1066 c = get_context(ctx);
1067 impl_ctx = c->context;
1068 } else {
1069 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -07001070 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
1071 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -08001072 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -07001073 }
Mathias Agopian518ec112011-05-13 16:21:08 -07001074 if (cur_c == NULL) {
1075 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -07001076 // not an error, there is just no current context.
1077 return EGL_TRUE;
1078 }
1079 }
1080
1081 // retrieve the underlying implementation's draw EGLSurface
1082 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001083 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001084 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001085 impl_draw = d->surface;
1086 }
1087
1088 // retrieve the underlying implementation's read EGLSurface
1089 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001090 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001091 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001092 impl_read = r->surface;
1093 }
1094
Mathias Agopian518ec112011-05-13 16:21:08 -07001095
Jesse Hallb29e5e82012-04-04 16:53:42 -07001096 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001097 draw, read, ctx,
1098 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001099
1100 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001101 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001102 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1103 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001104 _c.acquire();
1105 _r.acquire();
1106 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001107 } else {
1108 setGLHooksThreadSpecific(&gHooksNoContext);
1109 egl_tls_t::setContext(EGL_NO_CONTEXT);
1110 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001111 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001112
Mike Stroyan47e741b2017-06-01 13:56:18 -06001113 if (cur_c != NULL) {
1114 // Force return to current context for drivers that cannot handle errors
1115 EGLBoolean restore_result = EGL_FALSE;
1116 // get a reference to the old current objects
1117 ContextRef _c2(dp.get(), cur_c);
1118 SurfaceRef _d2(dp.get(), cur_c->draw);
1119 SurfaceRef _r2(dp.get(), cur_c->read);
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001120
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001121 c = cur_c;
1122 impl_ctx = c->context;
1123 impl_draw = EGL_NO_SURFACE;
1124 if (cur_c->draw != EGL_NO_SURFACE) {
1125 d = get_surface(cur_c->draw);
1126 impl_draw = d->surface;
1127 }
1128 impl_read = EGL_NO_SURFACE;
1129 if (cur_c->read != EGL_NO_SURFACE) {
1130 r = get_surface(cur_c->read);
1131 impl_read = r->surface;
1132 }
1133 restore_result = dp->makeCurrent(c, cur_c,
1134 cur_c->draw, cur_c->read, cur_c->context,
1135 impl_draw, impl_read, impl_ctx);
Mike Stroyan47e741b2017-06-01 13:56:18 -06001136 if (restore_result == EGL_TRUE) {
1137 _c2.acquire();
1138 _r2.acquire();
1139 _d2.acquire();
1140 } else {
1141 ALOGE("Could not restore original EGL context");
1142 }
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001143 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001144 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001145 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001146 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001147 }
1148 return result;
1149}
1150
1151
1152EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1153 EGLint attribute, EGLint *value)
1154{
1155 clearError();
1156
Jesse Hallb29e5e82012-04-04 16:53:42 -07001157 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001158 if (!dp) return EGL_FALSE;
1159
Jesse Hallb29e5e82012-04-04 16:53:42 -07001160 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001161 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001162
Mathias Agopian518ec112011-05-13 16:21:08 -07001163 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001164 return c->cnx->egl.eglQueryContext(
1165 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001166
Mathias Agopian518ec112011-05-13 16:21:08 -07001167}
1168
1169EGLContext eglGetCurrentContext(void)
1170{
1171 // could be called before eglInitialize(), but we wouldn't have a context
1172 // then, and this function would correctly return EGL_NO_CONTEXT.
1173
1174 clearError();
1175
1176 EGLContext ctx = getContext();
1177 return ctx;
1178}
1179
1180EGLSurface eglGetCurrentSurface(EGLint readdraw)
1181{
1182 // could be called before eglInitialize(), but we wouldn't have a context
1183 // then, and this function would correctly return EGL_NO_SURFACE.
1184
1185 clearError();
1186
1187 EGLContext ctx = getContext();
1188 if (ctx) {
1189 egl_context_t const * const c = get_context(ctx);
1190 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1191 switch (readdraw) {
1192 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001193 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001194 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1195 }
1196 }
1197 return EGL_NO_SURFACE;
1198}
1199
1200EGLDisplay eglGetCurrentDisplay(void)
1201{
1202 // could be called before eglInitialize(), but we wouldn't have a context
1203 // then, and this function would correctly return EGL_NO_DISPLAY.
1204
1205 clearError();
1206
1207 EGLContext ctx = getContext();
1208 if (ctx) {
1209 egl_context_t const * const c = get_context(ctx);
1210 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1211 return c->dpy;
1212 }
1213 return EGL_NO_DISPLAY;
1214}
1215
1216EGLBoolean eglWaitGL(void)
1217{
Mathias Agopian518ec112011-05-13 16:21:08 -07001218 clearError();
1219
Mathias Agopianada798b2012-02-13 17:09:30 -08001220 egl_connection_t* const cnx = &gEGLImpl;
1221 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001222 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001223
1224 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001225}
1226
1227EGLBoolean eglWaitNative(EGLint engine)
1228{
Mathias Agopian518ec112011-05-13 16:21:08 -07001229 clearError();
1230
Mathias Agopianada798b2012-02-13 17:09:30 -08001231 egl_connection_t* const cnx = &gEGLImpl;
1232 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001233 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001234
1235 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001236}
1237
1238EGLint eglGetError(void)
1239{
Mathias Agopianada798b2012-02-13 17:09:30 -08001240 EGLint err = EGL_SUCCESS;
1241 egl_connection_t* const cnx = &gEGLImpl;
1242 if (cnx->dso) {
1243 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001244 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001245 if (err == EGL_SUCCESS) {
1246 err = egl_tls_t::getError();
1247 }
1248 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001249}
1250
Michael Chockc0ec5e22014-01-27 08:14:33 -08001251static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001252 const char* procname) {
1253 const egl_connection_t* cnx = &gEGLImpl;
1254 void* proc = NULL;
1255
Michael Chockc0ec5e22014-01-27 08:14:33 -08001256 proc = dlsym(cnx->libEgl, procname);
1257 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1258
Jesse Hallc07b5202013-07-04 12:08:16 -07001259 proc = dlsym(cnx->libGles2, procname);
1260 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1261
1262 proc = dlsym(cnx->libGles1, procname);
1263 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1264
1265 return NULL;
1266}
1267
Mathias Agopian518ec112011-05-13 16:21:08 -07001268__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1269{
1270 // eglGetProcAddress() could be the very first function called
1271 // in which case we must make sure we've initialized ourselves, this
1272 // happens the first time egl_get_display() is called.
1273
1274 clearError();
1275
1276 if (egl_init_drivers() == EGL_FALSE) {
1277 setError(EGL_BAD_PARAMETER, NULL);
1278 return NULL;
1279 }
1280
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001281 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001282 return NULL;
1283 }
1284
Mathias Agopian518ec112011-05-13 16:21:08 -07001285 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001286 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001287 if (addr) return addr;
1288
Michael Chockc0ec5e22014-01-27 08:14:33 -08001289 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001290 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001291
Mathias Agopian518ec112011-05-13 16:21:08 -07001292 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1293 pthread_mutex_lock(&sExtensionMapMutex);
1294
1295 /*
1296 * Since eglGetProcAddress() is not associated to anything, it needs
1297 * to return a function pointer that "works" regardless of what
1298 * the current context is.
1299 *
1300 * For this reason, we return a "forwarder", a small stub that takes
1301 * care of calling the function associated with the context
1302 * currently bound.
1303 *
1304 * We first look for extensions we've already resolved, if we're seeing
1305 * this extension for the first time, we go through all our
1306 * implementations and call eglGetProcAddress() and record the
1307 * result in the appropriate implementation hooks and return the
1308 * address of the forwarder corresponding to that hook set.
1309 *
1310 */
1311
Mathias Agopian65421432017-03-08 11:49:05 -08001312 const std::string name(procname);
1313
1314 auto& extentionMap = sGLExtentionMap;
1315 auto pos = extentionMap.find(name);
1316 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001317 const int slot = sGLExtentionSlot;
1318
Steve Blocke6f43dd2012-01-06 19:20:56 +00001319 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001320 "no more slots for eglGetProcAddress(\"%s\")",
1321 procname);
1322
1323 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1324 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001325
1326 egl_connection_t* const cnx = &gEGLImpl;
1327 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001328 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001329 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001330 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1331 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001332 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001333 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001334 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001335
Mathias Agopian518ec112011-05-13 16:21:08 -07001336 if (found) {
1337 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001338 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001339 sGLExtentionSlot++;
1340 }
1341 }
1342
1343 pthread_mutex_unlock(&sExtensionMapMutex);
1344 return addr;
1345}
1346
Mathias Agopian65421432017-03-08 11:49:05 -08001347class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001348public:
1349
1350 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001351 static FrameCompletionThread thread;
1352
1353 char name[64];
1354
1355 std::lock_guard<std::mutex> lock(thread.mMutex);
1356 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1357 ATRACE_NAME(name);
1358
1359 thread.mQueue.push_back(sync);
1360 thread.mCondition.notify_one();
1361 thread.mFramesQueued++;
1362 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001363 }
1364
1365private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001366
Mathias Agopian65421432017-03-08 11:49:05 -08001367 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1368 std::thread thread(&FrameCompletionThread::loop, this);
1369 thread.detach();
1370 }
1371
1372#pragma clang diagnostic push
1373#pragma clang diagnostic ignored "-Wmissing-noreturn"
1374 void loop() {
1375 while (true) {
1376 threadLoop();
1377 }
1378 }
1379#pragma clang diagnostic pop
1380
1381 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001382 EGLSyncKHR sync;
1383 uint32_t frameNum;
1384 {
Mathias Agopian65421432017-03-08 11:49:05 -08001385 std::unique_lock<std::mutex> lock(mMutex);
1386 while (mQueue.empty()) {
1387 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001388 }
1389 sync = mQueue[0];
1390 frameNum = mFramesCompleted;
1391 }
1392 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1393 {
Mathias Agopian65421432017-03-08 11:49:05 -08001394 char name[64];
1395 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1396 ATRACE_NAME(name);
1397
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001398 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1399 if (result == EGL_FALSE) {
1400 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1401 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1402 ALOGE("FrameCompletion: timeout waiting for fence");
1403 }
1404 eglDestroySyncKHR(dpy, sync);
1405 }
1406 {
Mathias Agopian65421432017-03-08 11:49:05 -08001407 std::lock_guard<std::mutex> lock(mMutex);
1408 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001409 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001410 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001411 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001412 }
1413
1414 uint32_t mFramesQueued;
1415 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001416 std::deque<EGLSyncKHR> mQueue;
1417 std::condition_variable mCondition;
1418 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001419};
1420
Dan Stozaa894d082015-02-19 15:27:36 -08001421EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1422 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001423{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001424 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 clearError();
1426
Jesse Hallb29e5e82012-04-04 16:53:42 -07001427 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001428 if (!dp) return EGL_FALSE;
1429
Jesse Hallb29e5e82012-04-04 16:53:42 -07001430 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001431 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001432 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001433
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001434 egl_surface_t* const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001435
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001436 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1437 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1438 if (sync != EGL_NO_SYNC_KHR) {
1439 FrameCompletionThread::queueSync(sync);
1440 }
1441 }
1442
Mathias Agopian7db993a2012-03-25 00:49:46 -07001443 if (CC_UNLIKELY(dp->finishOnSwap)) {
1444 uint32_t pixel;
1445 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1446 if (c) {
1447 // glReadPixels() ensures that the frame is complete
1448 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1449 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1450 }
1451 }
1452
Courtney Goeltzenleuchter936799c2018-03-02 16:47:08 -07001453 if (!sendSurfaceMetadata(s)) {
1454 native_window_api_disconnect(s->getNativeWindow(), NATIVE_WINDOW_API_EGL);
1455 return setError(EGL_BAD_NATIVE_WINDOW, (EGLBoolean)EGL_FALSE);
1456 }
1457
Dan Stozaa894d082015-02-19 15:27:36 -08001458 if (n_rects == 0) {
1459 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1460 }
1461
Mathias Agopian65421432017-03-08 11:49:05 -08001462 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001463 for (int r = 0; r < n_rects; ++r) {
1464 int offset = r * 4;
1465 int x = rects[offset];
1466 int y = rects[offset + 1];
1467 int width = rects[offset + 2];
1468 int height = rects[offset + 3];
1469 android_native_rect_t androidRect;
1470 androidRect.left = x;
1471 androidRect.top = y + height;
1472 androidRect.right = x + width;
1473 androidRect.bottom = y;
1474 androidRects.push_back(androidRect);
1475 }
Mathias Agopian65421432017-03-08 11:49:05 -08001476 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001477
1478 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1479 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1480 rects, n_rects);
1481 } else {
1482 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1483 }
1484}
1485
1486EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1487{
1488 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001489}
1490
1491EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1492 NativePixmapType target)
1493{
1494 clearError();
1495
Jesse Hallb29e5e82012-04-04 16:53:42 -07001496 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001497 if (!dp) return EGL_FALSE;
1498
Jesse Hallb29e5e82012-04-04 16:53:42 -07001499 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001500 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001501 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001502
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001504 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505}
1506
1507const char* eglQueryString(EGLDisplay dpy, EGLint name)
1508{
1509 clearError();
1510
Chia-I Wue57d1352016-08-15 16:10:02 +08001511 // Generate an error quietly when client extensions (as defined by
1512 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1513 // validate_display to generate the error as validate_display would log
1514 // the error, which can be misleading.
1515 //
1516 // If we want to support EGL_EXT_client_extensions later, we can return
1517 // the client extension string here instead.
1518 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001519 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001520
Jesse Hallb29e5e82012-04-04 16:53:42 -07001521 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001522 if (!dp) return (const char *) NULL;
1523
1524 switch (name) {
1525 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001526 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001527 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001528 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001529 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001530 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001531 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001532 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001533 default:
1534 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001535 }
1536 return setError(EGL_BAD_PARAMETER, (const char *)0);
1537}
1538
Jiyong Park00b15b82017-08-10 20:30:56 +09001539extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001540{
1541 clearError();
1542
1543 const egl_display_ptr dp = validate_display(dpy);
1544 if (!dp) return (const char *) NULL;
1545
1546 switch (name) {
1547 case EGL_VENDOR:
1548 return dp->disp.queryString.vendor;
1549 case EGL_VERSION:
1550 return dp->disp.queryString.version;
1551 case EGL_EXTENSIONS:
1552 return dp->disp.queryString.extensions;
1553 case EGL_CLIENT_APIS:
1554 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001555 default:
1556 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001557 }
1558 return setError(EGL_BAD_PARAMETER, (const char *)0);
1559}
Mathias Agopian518ec112011-05-13 16:21:08 -07001560
1561// ----------------------------------------------------------------------------
1562// EGL 1.1
1563// ----------------------------------------------------------------------------
1564
1565EGLBoolean eglSurfaceAttrib(
1566 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1567{
1568 clearError();
1569
Jesse Hallb29e5e82012-04-04 16:53:42 -07001570 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001571 if (!dp) return EGL_FALSE;
1572
Jesse Hallb29e5e82012-04-04 16:53:42 -07001573 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001574 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001575 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001576
Pablo Ceballosc18be292016-05-31 14:55:42 -07001577 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001578
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001579 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001580 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001581 setError(EGL_BAD_SURFACE, EGL_FALSE);
1582 }
Mathias Agopian65421432017-03-08 11:49:05 -08001583 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1584 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001585 }
1586
Pablo Ceballosc18be292016-05-31 14:55:42 -07001587 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001588 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001589 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001590 }
Mathias Agopian65421432017-03-08 11:49:05 -08001591 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1592 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001593 }
1594
Courtney Goeltzenleuchter786ab882018-01-26 13:37:33 -08001595 if (s->setSmpte2086Attribute(attribute, value)) {
1596 return EGL_TRUE;
1597 } else if (s->setCta8613Attribute(attribute, value)) {
1598 return EGL_TRUE;
1599 } else if (s->cnx->egl.eglSurfaceAttrib) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001600 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001601 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001602 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001603 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001604}
1605
1606EGLBoolean eglBindTexImage(
1607 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1608{
1609 clearError();
1610
Jesse Hallb29e5e82012-04-04 16:53:42 -07001611 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001612 if (!dp) return EGL_FALSE;
1613
Jesse Hallb29e5e82012-04-04 16:53:42 -07001614 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001615 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001616 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001617
Mathias Agopian518ec112011-05-13 16:21:08 -07001618 egl_surface_t const * const s = get_surface(surface);
1619 if (s->cnx->egl.eglBindTexImage) {
1620 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001621 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001622 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001623 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001624}
1625
1626EGLBoolean eglReleaseTexImage(
1627 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1628{
1629 clearError();
1630
Jesse Hallb29e5e82012-04-04 16:53:42 -07001631 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001632 if (!dp) return EGL_FALSE;
1633
Jesse Hallb29e5e82012-04-04 16:53:42 -07001634 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001635 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001636 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001637
Mathias Agopian518ec112011-05-13 16:21:08 -07001638 egl_surface_t const * const s = get_surface(surface);
1639 if (s->cnx->egl.eglReleaseTexImage) {
1640 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001641 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001642 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001643 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001644}
1645
1646EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1647{
1648 clearError();
1649
Jesse Hallb29e5e82012-04-04 16:53:42 -07001650 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001651 if (!dp) return EGL_FALSE;
1652
1653 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001654 egl_connection_t* const cnx = &gEGLImpl;
1655 if (cnx->dso && cnx->egl.eglSwapInterval) {
1656 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001657 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001658
Mathias Agopian518ec112011-05-13 16:21:08 -07001659 return res;
1660}
1661
1662
1663// ----------------------------------------------------------------------------
1664// EGL 1.2
1665// ----------------------------------------------------------------------------
1666
1667EGLBoolean eglWaitClient(void)
1668{
1669 clearError();
1670
Mathias Agopianada798b2012-02-13 17:09:30 -08001671 egl_connection_t* const cnx = &gEGLImpl;
1672 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001673 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001674
1675 EGLBoolean res;
1676 if (cnx->egl.eglWaitClient) {
1677 res = cnx->egl.eglWaitClient();
1678 } else {
1679 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001680 }
1681 return res;
1682}
1683
1684EGLBoolean eglBindAPI(EGLenum api)
1685{
1686 clearError();
1687
1688 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001689 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001690 }
1691
1692 // bind this API on all EGLs
1693 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001694 egl_connection_t* const cnx = &gEGLImpl;
1695 if (cnx->dso && cnx->egl.eglBindAPI) {
1696 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001697 }
1698 return res;
1699}
1700
1701EGLenum eglQueryAPI(void)
1702{
1703 clearError();
1704
1705 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001706 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001707 }
1708
Mathias Agopianada798b2012-02-13 17:09:30 -08001709 egl_connection_t* const cnx = &gEGLImpl;
1710 if (cnx->dso && cnx->egl.eglQueryAPI) {
1711 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001712 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001713
Mathias Agopian518ec112011-05-13 16:21:08 -07001714 // or, it can only be OpenGL ES
1715 return EGL_OPENGL_ES_API;
1716}
1717
1718EGLBoolean eglReleaseThread(void)
1719{
1720 clearError();
1721
Mathias Agopianada798b2012-02-13 17:09:30 -08001722 egl_connection_t* const cnx = &gEGLImpl;
1723 if (cnx->dso && cnx->egl.eglReleaseThread) {
1724 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001725 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301726
1727 // If there is context bound to the thread, release it
1728 egl_display_t::loseCurrent(get_context(getContext()));
1729
Mathias Agopian518ec112011-05-13 16:21:08 -07001730 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001731 return EGL_TRUE;
1732}
1733
1734EGLSurface eglCreatePbufferFromClientBuffer(
1735 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1736 EGLConfig config, const EGLint *attrib_list)
1737{
1738 clearError();
1739
Jesse Hallb29e5e82012-04-04 16:53:42 -07001740 egl_connection_t* cnx = NULL;
1741 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1742 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001743 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1744 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001745 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001746 }
1747 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1748}
1749
1750// ----------------------------------------------------------------------------
1751// EGL_EGLEXT_VERSION 3
1752// ----------------------------------------------------------------------------
1753
1754EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1755 const EGLint *attrib_list)
1756{
1757 clearError();
1758
Jesse Hallb29e5e82012-04-04 16:53:42 -07001759 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001760 if (!dp) return EGL_FALSE;
1761
Jesse Hallb29e5e82012-04-04 16:53:42 -07001762 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001763 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001764 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001765
1766 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001767 if (s->cnx->egl.eglLockSurfaceKHR) {
1768 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001769 dp->disp.dpy, s->surface, attrib_list);
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
1774EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1775{
1776 clearError();
1777
Jesse Hallb29e5e82012-04-04 16:53:42 -07001778 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001779 if (!dp) return EGL_FALSE;
1780
Jesse Hallb29e5e82012-04-04 16:53:42 -07001781 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001782 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001783 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001784
1785 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001786 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001787 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001788 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001789 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001790}
1791
1792EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1793 EGLClientBuffer buffer, const EGLint *attrib_list)
1794{
1795 clearError();
1796
Jesse Hallb29e5e82012-04-04 16:53:42 -07001797 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001798 if (!dp) return EGL_NO_IMAGE_KHR;
1799
Jesse Hallb29e5e82012-04-04 16:53:42 -07001800 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001801 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001802
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001803 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1804 egl_connection_t* const cnx = &gEGLImpl;
1805 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1806 result = cnx->egl.eglCreateImageKHR(
1807 dp->disp.dpy,
1808 c ? c->context : EGL_NO_CONTEXT,
1809 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001810 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001811 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001812}
1813
1814EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1815{
1816 clearError();
1817
Jesse Hallb29e5e82012-04-04 16:53:42 -07001818 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001819 if (!dp) return EGL_FALSE;
1820
Steven Holte646a5c52012-06-04 20:02:11 -07001821 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001822 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001823 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001824 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001825 }
Steven Holte646a5c52012-06-04 20:02:11 -07001826 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001827}
1828
1829// ----------------------------------------------------------------------------
1830// EGL_EGLEXT_VERSION 5
1831// ----------------------------------------------------------------------------
1832
1833
1834EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1835{
1836 clearError();
1837
Jesse Hallb29e5e82012-04-04 16:53:42 -07001838 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001839 if (!dp) return EGL_NO_SYNC_KHR;
1840
Mathias Agopian518ec112011-05-13 16:21:08 -07001841 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001842 egl_connection_t* const cnx = &gEGLImpl;
1843 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1844 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001845 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001846 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001847}
1848
1849EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1850{
1851 clearError();
1852
Jesse Hallb29e5e82012-04-04 16:53:42 -07001853 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001854 if (!dp) return EGL_FALSE;
1855
Mathias Agopian518ec112011-05-13 16:21:08 -07001856 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001857 egl_connection_t* const cnx = &gEGLImpl;
1858 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1859 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001860 }
1861 return result;
1862}
1863
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001864EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1865 clearError();
1866
1867 const egl_display_ptr dp = validate_display(dpy);
1868 if (!dp) return EGL_FALSE;
1869
1870 EGLBoolean result = EGL_FALSE;
1871 egl_connection_t* const cnx = &gEGLImpl;
1872 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1873 result = cnx->egl.eglSignalSyncKHR(
1874 dp->disp.dpy, sync, mode);
1875 }
1876 return result;
1877}
1878
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001879EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1880 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001881{
1882 clearError();
1883
Jesse Hallb29e5e82012-04-04 16:53:42 -07001884 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001885 if (!dp) return EGL_FALSE;
1886
Mathias Agopian737b8962017-02-24 14:32:05 -08001887 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001888 egl_connection_t* const cnx = &gEGLImpl;
1889 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1890 result = cnx->egl.eglClientWaitSyncKHR(
1891 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001892 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001893 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001894}
1895
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001896EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1897 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001898{
1899 clearError();
1900
Jesse Hallb29e5e82012-04-04 16:53:42 -07001901 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001902 if (!dp) return EGL_FALSE;
1903
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001904 EGLBoolean result = EGL_FALSE;
1905 egl_connection_t* const cnx = &gEGLImpl;
1906 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1907 result = cnx->egl.eglGetSyncAttribKHR(
1908 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001909 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001910 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001911}
1912
Season Li000d88f2015-07-01 11:39:40 -07001913EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1914{
1915 clearError();
1916
1917 const egl_display_ptr dp = validate_display(dpy);
1918 if (!dp) return EGL_NO_STREAM_KHR;
1919
1920 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1921 egl_connection_t* const cnx = &gEGLImpl;
1922 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1923 result = cnx->egl.eglCreateStreamKHR(
1924 dp->disp.dpy, attrib_list);
1925 }
1926 return result;
1927}
1928
1929EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1930{
1931 clearError();
1932
1933 const egl_display_ptr dp = validate_display(dpy);
1934 if (!dp) return EGL_FALSE;
1935
1936 EGLBoolean result = EGL_FALSE;
1937 egl_connection_t* const cnx = &gEGLImpl;
1938 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1939 result = cnx->egl.eglDestroyStreamKHR(
1940 dp->disp.dpy, stream);
1941 }
1942 return result;
1943}
1944
1945EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1946 EGLenum attribute, EGLint value)
1947{
1948 clearError();
1949
1950 const egl_display_ptr dp = validate_display(dpy);
1951 if (!dp) return EGL_FALSE;
1952
1953 EGLBoolean result = EGL_FALSE;
1954 egl_connection_t* const cnx = &gEGLImpl;
1955 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1956 result = cnx->egl.eglStreamAttribKHR(
1957 dp->disp.dpy, stream, attribute, value);
1958 }
1959 return result;
1960}
1961
1962EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1963 EGLenum attribute, EGLint *value)
1964{
1965 clearError();
1966
1967 const egl_display_ptr dp = validate_display(dpy);
1968 if (!dp) return EGL_FALSE;
1969
1970 EGLBoolean result = EGL_FALSE;
1971 egl_connection_t* const cnx = &gEGLImpl;
1972 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1973 result = cnx->egl.eglQueryStreamKHR(
1974 dp->disp.dpy, stream, attribute, value);
1975 }
1976 return result;
1977}
1978
1979EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1980 EGLenum attribute, EGLuint64KHR *value)
1981{
1982 clearError();
1983
1984 const egl_display_ptr dp = validate_display(dpy);
1985 if (!dp) return EGL_FALSE;
1986
1987 EGLBoolean result = EGL_FALSE;
1988 egl_connection_t* const cnx = &gEGLImpl;
1989 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1990 result = cnx->egl.eglQueryStreamu64KHR(
1991 dp->disp.dpy, stream, attribute, value);
1992 }
1993 return result;
1994}
1995
1996EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1997 EGLenum attribute, EGLTimeKHR *value)
1998{
1999 clearError();
2000
2001 const egl_display_ptr dp = validate_display(dpy);
2002 if (!dp) return EGL_FALSE;
2003
2004 EGLBoolean result = EGL_FALSE;
2005 egl_connection_t* const cnx = &gEGLImpl;
2006 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
2007 result = cnx->egl.eglQueryStreamTimeKHR(
2008 dp->disp.dpy, stream, attribute, value);
2009 }
2010 return result;
2011}
2012
2013EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
2014 EGLStreamKHR stream, const EGLint *attrib_list)
2015{
2016 clearError();
2017
2018 egl_display_ptr dp = validate_display(dpy);
2019 if (!dp) return EGL_NO_SURFACE;
2020
2021 egl_connection_t* const cnx = &gEGLImpl;
2022 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
2023 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
2024 dp->disp.dpy, config, stream, attrib_list);
2025 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchterb1d70ec2018-02-08 18:09:33 -07002026 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface,
2027 EGL_GL_COLORSPACE_LINEAR_KHR, cnx);
Season Li000d88f2015-07-01 11:39:40 -07002028 return s;
2029 }
2030 }
2031 return EGL_NO_SURFACE;
2032}
2033
2034EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
2035 EGLStreamKHR stream)
2036{
2037 clearError();
2038
2039 const egl_display_ptr dp = validate_display(dpy);
2040 if (!dp) return EGL_FALSE;
2041
2042 EGLBoolean result = EGL_FALSE;
2043 egl_connection_t* const cnx = &gEGLImpl;
2044 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
2045 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
2046 dp->disp.dpy, stream);
2047 }
2048 return result;
2049}
2050
2051EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
2052 EGLStreamKHR stream)
2053{
2054 clearError();
2055
2056 const egl_display_ptr dp = validate_display(dpy);
2057 if (!dp) return EGL_FALSE;
2058
2059 EGLBoolean result = EGL_FALSE;
2060 egl_connection_t* const cnx = &gEGLImpl;
2061 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
2062 result = cnx->egl.eglStreamConsumerAcquireKHR(
2063 dp->disp.dpy, stream);
2064 }
2065 return result;
2066}
2067
2068EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
2069 EGLStreamKHR stream)
2070{
2071 clearError();
2072
2073 const egl_display_ptr dp = validate_display(dpy);
2074 if (!dp) return EGL_FALSE;
2075
2076 EGLBoolean result = EGL_FALSE;
2077 egl_connection_t* const cnx = &gEGLImpl;
2078 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
2079 result = cnx->egl.eglStreamConsumerReleaseKHR(
2080 dp->disp.dpy, stream);
2081 }
2082 return result;
2083}
2084
2085EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2086 EGLDisplay dpy, EGLStreamKHR stream)
2087{
2088 clearError();
2089
2090 const egl_display_ptr dp = validate_display(dpy);
2091 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2092
2093 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2094 egl_connection_t* const cnx = &gEGLImpl;
2095 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2096 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2097 dp->disp.dpy, stream);
2098 }
2099 return result;
2100}
2101
2102EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2103 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2104{
2105 clearError();
2106
2107 const egl_display_ptr dp = validate_display(dpy);
2108 if (!dp) return EGL_NO_STREAM_KHR;
2109
2110 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2111 egl_connection_t* const cnx = &gEGLImpl;
2112 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2113 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2114 dp->disp.dpy, file_descriptor);
2115 }
2116 return result;
2117}
2118
Mathias Agopian518ec112011-05-13 16:21:08 -07002119// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002120// EGL_EGLEXT_VERSION 15
2121// ----------------------------------------------------------------------------
2122
2123EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2124 clearError();
2125 const egl_display_ptr dp = validate_display(dpy);
2126 if (!dp) return EGL_FALSE;
2127 EGLint result = EGL_FALSE;
2128 egl_connection_t* const cnx = &gEGLImpl;
2129 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2130 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2131 }
2132 return result;
2133}
2134
2135// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002136// ANDROID extensions
2137// ----------------------------------------------------------------------------
2138
Jamie Gennis331841b2012-09-06 14:52:00 -07002139EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2140{
2141 clearError();
2142
2143 const egl_display_ptr dp = validate_display(dpy);
2144 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2145
2146 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2147 egl_connection_t* const cnx = &gEGLImpl;
2148 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2149 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2150 }
2151 return result;
2152}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002153
Andy McFadden72841452013-03-01 16:25:32 -08002154EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2155 EGLnsecsANDROID time)
2156{
2157 clearError();
2158
2159 const egl_display_ptr dp = validate_display(dpy);
2160 if (!dp) {
2161 return EGL_FALSE;
2162 }
2163
2164 SurfaceRef _s(dp.get(), surface);
2165 if (!_s.get()) {
2166 setError(EGL_BAD_SURFACE, EGL_FALSE);
2167 return EGL_FALSE;
2168 }
2169
2170 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002171 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002172
2173 return EGL_TRUE;
2174}
2175
Craig Donner60761072017-01-27 12:30:44 -08002176EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2177 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002178 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2179 // this function cannot be implemented when this libEGL is built for
2180 // vendors.
2181#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002182 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002183 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002184#else
2185 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2186#endif
Craig Donner60761072017-01-27 12:30:44 -08002187}
2188
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002189// ----------------------------------------------------------------------------
2190// NVIDIA extensions
2191// ----------------------------------------------------------------------------
2192EGLuint64NV eglGetSystemTimeFrequencyNV()
2193{
2194 clearError();
2195
2196 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002197 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002198 }
2199
2200 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002201 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002202
Mathias Agopianada798b2012-02-13 17:09:30 -08002203 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2204 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002205 }
2206
Mathias Agopian737b8962017-02-24 14:32:05 -08002207 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002208}
2209
2210EGLuint64NV eglGetSystemTimeNV()
2211{
2212 clearError();
2213
2214 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002215 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002216 }
2217
2218 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002219 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002220
Mathias Agopianada798b2012-02-13 17:09:30 -08002221 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2222 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002223 }
2224
Mathias Agopian737b8962017-02-24 14:32:05 -08002225 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002226}
Dan Stozaa894d082015-02-19 15:27:36 -08002227
2228// ----------------------------------------------------------------------------
2229// Partial update extension
2230// ----------------------------------------------------------------------------
2231EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2232 EGLint *rects, EGLint n_rects)
2233{
2234 clearError();
2235
2236 const egl_display_ptr dp = validate_display(dpy);
2237 if (!dp) {
2238 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2239 return EGL_FALSE;
2240 }
2241
2242 SurfaceRef _s(dp.get(), surface);
2243 if (!_s.get()) {
2244 setError(EGL_BAD_SURFACE, EGL_FALSE);
2245 return EGL_FALSE;
2246 }
2247
2248 egl_surface_t const * const s = get_surface(surface);
2249 if (s->cnx->egl.eglSetDamageRegionKHR) {
2250 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2251 rects, n_rects);
2252 }
2253
2254 return EGL_FALSE;
2255}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002256
Brian Anderson1049d1d2016-12-16 17:25:57 -08002257EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2258 EGLuint64KHR *frameId) {
2259 clearError();
2260
2261 const egl_display_ptr dp = validate_display(dpy);
2262 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002263 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002264 }
2265
2266 SurfaceRef _s(dp.get(), surface);
2267 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002268 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002269 }
2270
2271 egl_surface_t const * const s = get_surface(surface);
2272
Mathias Agopian65421432017-03-08 11:49:05 -08002273 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002274 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002275 }
2276
2277 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002278 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002279
Mathias Agopian65421432017-03-08 11:49:05 -08002280 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002281 // This should not happen. Return an error that is not in the spec
2282 // so it's obvious something is very wrong.
2283 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002284 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002285 }
2286
2287 *frameId = nextFrameId;
2288 return EGL_TRUE;
2289}
2290
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002291EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2292 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2293{
2294 clearError();
2295
2296 const egl_display_ptr dp = validate_display(dpy);
2297 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002298 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002299 }
2300
2301 SurfaceRef _s(dp.get(), surface);
2302 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002303 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002304 }
2305
2306 egl_surface_t const * const s = get_surface(surface);
2307
Mathias Agopian65421432017-03-08 11:49:05 -08002308 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002309 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002310 }
2311
2312 nsecs_t* compositeDeadline = nullptr;
2313 nsecs_t* compositeInterval = nullptr;
2314 nsecs_t* compositeToPresentLatency = nullptr;
2315
2316 for (int i = 0; i < numTimestamps; i++) {
2317 switch (names[i]) {
2318 case EGL_COMPOSITE_DEADLINE_ANDROID:
2319 compositeDeadline = &values[i];
2320 break;
2321 case EGL_COMPOSITE_INTERVAL_ANDROID:
2322 compositeInterval = &values[i];
2323 break;
2324 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2325 compositeToPresentLatency = &values[i];
2326 break;
2327 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002328 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002329 }
2330 }
2331
Mathias Agopian65421432017-03-08 11:49:05 -08002332 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002333 compositeDeadline, compositeInterval, compositeToPresentLatency);
2334
2335 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002336 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002337 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002338 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002339 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002340 default:
2341 // This should not happen. Return an error that is not in the spec
2342 // so it's obvious something is very wrong.
2343 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002344 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002345 }
2346}
2347
2348EGLBoolean eglGetCompositorTimingSupportedANDROID(
2349 EGLDisplay dpy, EGLSurface surface, EGLint name)
2350{
2351 clearError();
2352
2353 const egl_display_ptr dp = validate_display(dpy);
2354 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002355 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002356 }
2357
2358 SurfaceRef _s(dp.get(), surface);
2359 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002360 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002361 }
2362
2363 egl_surface_t const * const s = get_surface(surface);
2364
Mathias Agopian65421432017-03-08 11:49:05 -08002365 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002366 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002367 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002368 }
2369
2370 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002371 case EGL_COMPOSITE_DEADLINE_ANDROID:
2372 case EGL_COMPOSITE_INTERVAL_ANDROID:
2373 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2374 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002375 default:
2376 return EGL_FALSE;
2377 }
2378}
2379
Pablo Ceballosc18be292016-05-31 14:55:42 -07002380EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002381 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002382 EGLnsecsANDROID *values)
2383{
2384 clearError();
2385
2386 const egl_display_ptr dp = validate_display(dpy);
2387 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002388 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002389 }
2390
2391 SurfaceRef _s(dp.get(), surface);
2392 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002393 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002394 }
2395
2396 egl_surface_t const * const s = get_surface(surface);
2397
Mathias Agopian65421432017-03-08 11:49:05 -08002398 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002399 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002400 }
2401
Brian Andersondbd0ea82016-07-22 09:38:59 -07002402 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002403 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002404 nsecs_t* latchTime = nullptr;
2405 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002406 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002407 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002408 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002409 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002410 nsecs_t* releaseTime = nullptr;
2411
2412 for (int i = 0; i < numTimestamps; i++) {
2413 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002414 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2415 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002416 break;
2417 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2418 acquireTime = &values[i];
2419 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002420 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2421 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002422 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002423 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2424 firstRefreshStartTime = &values[i];
2425 break;
2426 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2427 lastRefreshStartTime = &values[i];
2428 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002429 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2430 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002431 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002432 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2433 displayPresentTime = &values[i];
2434 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002435 case EGL_DEQUEUE_READY_TIME_ANDROID:
2436 dequeueReadyTime = &values[i];
2437 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002438 case EGL_READS_DONE_TIME_ANDROID:
2439 releaseTime = &values[i];
2440 break;
2441 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002442 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002443 }
2444 }
2445
Mathias Agopian65421432017-03-08 11:49:05 -08002446 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002447 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002448 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002449 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002450
Brian Anderson069b3652016-07-22 10:32:47 -07002451 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002452 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002453 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002454 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002455 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002456 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002457 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002458 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002459 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2460 default:
2461 // This should not happen. Return an error that is not in the spec
2462 // so it's obvious something is very wrong.
2463 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2464 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002465 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002466}
2467
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002468EGLBoolean eglGetFrameTimestampSupportedANDROID(
2469 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002470{
2471 clearError();
2472
2473 const egl_display_ptr dp = validate_display(dpy);
2474 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002475 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002476 }
2477
2478 SurfaceRef _s(dp.get(), surface);
2479 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002480 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002481 }
2482
2483 egl_surface_t const * const s = get_surface(surface);
2484
Mathias Agopian65421432017-03-08 11:49:05 -08002485 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002486 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002487 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002488 }
2489
2490 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002491 case EGL_COMPOSITE_DEADLINE_ANDROID:
2492 case EGL_COMPOSITE_INTERVAL_ANDROID:
2493 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002494 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002495 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002496 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2497 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2498 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002499 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002500 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002501 case EGL_READS_DONE_TIME_ANDROID:
2502 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002503 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2504 int value = 0;
2505 window->query(window,
2506 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2507 return value == 0 ? EGL_FALSE : EGL_TRUE;
2508 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002509 default:
2510 return EGL_FALSE;
2511 }
2512}