blob: 6bff38ac3d4dd5daf7bbbc3bdd9006a720cc2824 [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 Goeltzenleuchter21b33cb2017-04-17 17:31:29 -060090 "EGL_EXT_gl_colorspace_scrgb_linear "
91 "EGL_EXT_gl_colorspace_display_p3_linear "
92 "EGL_EXT_gl_colorspace_display_p3 "
Jesse Hall21558da2013-08-06 15:31:22 -070093 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080094
95char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070096 "EGL_KHR_image " // mandatory
97 "EGL_KHR_image_base " // mandatory
98 "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
246
247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800249static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
250
Mathias Agopian518ec112011-05-13 16:21:08 -0700251static int sGLExtentionSlot = 0;
252static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
253
254static void(*findProcAddress(const char* name,
255 const extention_map_t* map, size_t n))() {
256 for (uint32_t i=0 ; i<n ; i++) {
257 if (!strcmp(name, map[i].name)) {
258 return map[i].address;
259 }
260 }
261 return NULL;
262}
263
264// ----------------------------------------------------------------------------
265
Mathias Agopian518ec112011-05-13 16:21:08 -0700266extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
267extern EGLBoolean egl_init_drivers();
268extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700269extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271} // namespace android;
272
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700273
Mathias Agopian518ec112011-05-13 16:21:08 -0700274// ----------------------------------------------------------------------------
275
276static inline void clearError() { egl_tls_t::clearError(); }
277static inline EGLContext getContext() { return egl_tls_t::getContext(); }
278
279// ----------------------------------------------------------------------------
280
281EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
282{
Jesse Hall1508ae62017-01-19 17:43:26 -0800283 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 clearError();
285
Dan Stozac3289c42014-01-17 11:38:34 -0800286 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700287 if (index >= NUM_DISPLAYS) {
288 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
289 }
290
291 if (egl_init_drivers() == EGL_FALSE) {
292 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
293 }
294
295 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
296 return dpy;
297}
298
299// ----------------------------------------------------------------------------
300// Initialization
301// ----------------------------------------------------------------------------
302
303EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
304{
305 clearError();
306
Jesse Hallb29e5e82012-04-04 16:53:42 -0700307 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800308 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700309
310 EGLBoolean res = dp->initialize(major, minor);
311
312 return res;
313}
314
315EGLBoolean eglTerminate(EGLDisplay dpy)
316{
317 // NOTE: don't unload the drivers b/c some APIs can be called
318 // after eglTerminate() has been called. eglTerminate() only
319 // terminates an EGLDisplay, not a EGL itself.
320
321 clearError();
322
Jesse Hallb29e5e82012-04-04 16:53:42 -0700323 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800324 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700325
326 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800327
Mathias Agopian518ec112011-05-13 16:21:08 -0700328 return res;
329}
330
331// ----------------------------------------------------------------------------
332// configuration
333// ----------------------------------------------------------------------------
334
335EGLBoolean eglGetConfigs( EGLDisplay dpy,
336 EGLConfig *configs,
337 EGLint config_size, EGLint *num_config)
338{
339 clearError();
340
Jesse Hallb29e5e82012-04-04 16:53:42 -0700341 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700342 if (!dp) return EGL_FALSE;
343
Mathias Agopian7773c432012-02-13 20:06:08 -0800344 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800345 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700346 }
347
Mathias Agopian7773c432012-02-13 20:06:08 -0800348 EGLBoolean res = EGL_FALSE;
349 *num_config = 0;
350
351 egl_connection_t* const cnx = &gEGLImpl;
352 if (cnx->dso) {
353 res = cnx->egl.eglGetConfigs(
354 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700355 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800356
357 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700358}
359
360EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
361 EGLConfig *configs, EGLint config_size,
362 EGLint *num_config)
363{
364 clearError();
365
Jesse Hallb29e5e82012-04-04 16:53:42 -0700366 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700367 if (!dp) return EGL_FALSE;
368
369 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800370 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700371 }
372
Mathias Agopian518ec112011-05-13 16:21:08 -0700373 EGLBoolean res = EGL_FALSE;
374 *num_config = 0;
375
Mathias Agopianada798b2012-02-13 17:09:30 -0800376 egl_connection_t* const cnx = &gEGLImpl;
377 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700378 if (attrib_list) {
379 char value[PROPERTY_VALUE_MAX];
380 property_get("debug.egl.force_msaa", value, "false");
381
382 if (!strcmp(value, "true")) {
383 size_t attribCount = 0;
384 EGLint attrib = attrib_list[0];
385
386 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700387 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700388 const EGLint *attribRendererable = NULL;
389 const EGLint *attribCaveat = NULL;
390
391 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700392 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700393 while (attrib != EGL_NONE) {
394 attrib = attrib_list[attribCount];
395 switch (attrib) {
396 case EGL_RENDERABLE_TYPE:
397 attribRendererable = &attrib_list[attribCount];
398 break;
399 case EGL_CONFIG_CAVEAT:
400 attribCaveat = &attrib_list[attribCount];
401 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800402 default:
403 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700404 }
405 attribCount++;
406 }
407
408 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
409 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800410
Romain Guy1cffc802012-10-15 18:13:05 -0700411 // Insert 2 extra attributes to force-enable MSAA 4x
412 EGLint aaAttribs[attribCount + 4];
413 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
414 aaAttribs[1] = 1;
415 aaAttribs[2] = EGL_SAMPLES;
416 aaAttribs[3] = 4;
417
418 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
419
420 EGLint numConfigAA;
421 EGLBoolean resAA = cnx->egl.eglChooseConfig(
422 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
423
424 if (resAA == EGL_TRUE && numConfigAA > 0) {
425 ALOGD("Enabling MSAA 4x");
426 *num_config = numConfigAA;
427 return resAA;
428 }
429 }
430 }
431 }
432
Mathias Agopian7773c432012-02-13 20:06:08 -0800433 res = cnx->egl.eglChooseConfig(
434 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700435 }
436 return res;
437}
438
439EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
440 EGLint attribute, EGLint *value)
441{
442 clearError();
443
Jesse Hallb29e5e82012-04-04 16:53:42 -0700444 egl_connection_t* cnx = NULL;
445 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
446 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800447
Mathias Agopian518ec112011-05-13 16:21:08 -0700448 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800449 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700450}
451
452// ----------------------------------------------------------------------------
453// surfaces
454// ----------------------------------------------------------------------------
455
Jesse Hallc2e41222013-08-08 13:40:22 -0700456// Turn linear formats into corresponding sRGB formats when colorspace is
457// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
458// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800459// the modification isn't possible, the original dataSpace is returned.
460static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
461 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700462 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800463 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700464 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800465 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600466 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
467 return HAL_DATASPACE_DISPLAY_P3;
468 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
469 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
470 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
471 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700472 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800473 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700474}
475
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600476static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
477 EGLint& colorSpace, android_dataspace& dataSpace) {
478 colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
479 dataSpace = HAL_DATASPACE_UNKNOWN;
480 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
481 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
482 if (*attr == EGL_GL_COLORSPACE_KHR) {
483 colorSpace = attr[1];
484 bool found = false;
485 // Verify that color space is allowed
486 if (colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
487 colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR) {
488 found = true;
489 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_linear &&
490 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
491 found = true;
492 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_pq &&
493 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
494 found = true;
495 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
496 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
497 found = true;
498 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
499 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
500 found = true;
501 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
502 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
503 found = true;
504 }
505 if (!found) {
506 return false;
507 }
Courtney Goeltzenleuchter1d4f7a22017-05-05 13:30:25 -0600508 // Only change the dataSpace from default if the application
509 // has explicitly set the color space with a EGL_GL_COLORSPACE_KHR attribute.
510 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600511 }
512 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600513 }
514 return true;
515}
516
Mathias Agopian518ec112011-05-13 16:21:08 -0700517EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
518 NativeWindowType window,
519 const EGLint *attrib_list)
520{
521 clearError();
522
Jesse Hallb29e5e82012-04-04 16:53:42 -0700523 egl_connection_t* cnx = NULL;
524 egl_display_ptr dp = validate_display_connection(dpy, cnx);
525 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800526 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700527
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800528 if (!window) {
529 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
530 }
531
532 int value = 0;
533 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
534 if (!value) {
535 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
536 }
537
Andy McFaddend566ce32014-01-07 15:54:17 -0800538 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800539 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800540 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
541 "failed (%#x) (already connected to another API?)",
542 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700543 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700544 }
545
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700546 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700547 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
548 // of our native format. So if sRGB gamma is requested, we have to
549 // modify the EGLconfig's format before setting the native window's
550 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800551
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700552 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
553 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
554 &componentType);
555
Mathias Agopian95921562017-02-24 14:31:31 -0800556 EGLint format;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600557 EGLint colorSpace;
558 android_dataspace dataSpace;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700559 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700560 EGLint r, g, b;
561 r = g = b = 0;
562 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
563 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
564 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700565 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700566 EGLint colorDepth = r + g + b;
567
568 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700569 if (colorDepth <= 16) {
570 format = HAL_PIXEL_FORMAT_RGB_565;
571 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700572 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
573 if (colorDepth > 24) {
574 format = HAL_PIXEL_FORMAT_RGBA_1010102;
575 } else {
576 format = HAL_PIXEL_FORMAT_RGBX_8888;
577 }
578 } else {
579 format = HAL_PIXEL_FORMAT_RGBA_FP16;
580 }
581 }
582 } else {
583 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
584 if (colorDepth > 24) {
585 format = HAL_PIXEL_FORMAT_RGBA_1010102;
586 } else {
587 format = HAL_PIXEL_FORMAT_RGBA_8888;
588 }
589 } else {
590 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700591 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700592 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700593
594 // now select a corresponding sRGB format if needed
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600595 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
596 ALOGE("error invalid colorspace: %d", colorSpace);
597 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700598 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800599
Jesse Hallc2e41222013-08-08 13:40:22 -0700600 if (format != 0) {
601 int err = native_window_set_buffers_format(window, format);
602 if (err != 0) {
603 ALOGE("error setting native window pixel format: %s (%d)",
604 strerror(-err), err);
605 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
606 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
607 }
608 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700609
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800610 if (dataSpace != 0) {
611 int err = native_window_set_buffers_data_space(window, dataSpace);
612 if (err != 0) {
613 ALOGE("error setting native window pixel dataSpace: %s (%d)",
614 strerror(-err), err);
615 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
616 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
617 }
618 }
619
Jamie Gennis59769462011-11-19 18:04:43 -0800620 // the EGL spec requires that a new EGLSurface default to swap interval
621 // 1, so explicitly set that on the window here.
622 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
623 anw->setSwapInterval(anw, 1);
624
Mathias Agopian518ec112011-05-13 16:21:08 -0700625 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800626 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700627 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600628 egl_surface_t* s =
629 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700630 return s;
631 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700632
633 // EGLSurface creation failed
634 native_window_set_buffers_format(window, 0);
635 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700636 }
637 return EGL_NO_SURFACE;
638}
639
640EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
641 NativePixmapType pixmap,
642 const EGLint *attrib_list)
643{
644 clearError();
645
Jesse Hallb29e5e82012-04-04 16:53:42 -0700646 egl_connection_t* cnx = NULL;
647 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600648 EGLint colorSpace;
649 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700650 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600651 // now select a corresponding sRGB format if needed
652 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
653 ALOGE("error invalid colorspace: %d", colorSpace);
654 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
655 }
656
Mathias Agopian518ec112011-05-13 16:21:08 -0700657 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800658 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700659 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600660 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700661 return s;
662 }
663 }
664 return EGL_NO_SURFACE;
665}
666
667EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
668 const EGLint *attrib_list)
669{
670 clearError();
671
Jesse Hallb29e5e82012-04-04 16:53:42 -0700672 egl_connection_t* cnx = NULL;
673 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600674 EGLint colorSpace;
675 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700676 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600677 // now select a corresponding sRGB format if needed
678 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
679 ALOGE("error invalid colorspace: %d", colorSpace);
680 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
681 }
682
Mathias Agopian518ec112011-05-13 16:21:08 -0700683 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800684 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700685 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600686 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700687 return s;
688 }
689 }
690 return EGL_NO_SURFACE;
691}
Jesse Hall47743382013-02-08 11:13:46 -0800692
Mathias Agopian518ec112011-05-13 16:21:08 -0700693EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
694{
695 clearError();
696
Jesse Hallb29e5e82012-04-04 16:53:42 -0700697 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700698 if (!dp) return EGL_FALSE;
699
Jesse Hallb29e5e82012-04-04 16:53:42 -0700700 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700701 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800702 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700703
704 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800705 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700706 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700707 _s.terminate();
708 }
709 return result;
710}
711
712EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
713 EGLint attribute, EGLint *value)
714{
715 clearError();
716
Jesse Hallb29e5e82012-04-04 16:53:42 -0700717 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700718 if (!dp) return EGL_FALSE;
719
Jesse Hallb29e5e82012-04-04 16:53:42 -0700720 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700721 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800722 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700723
Mathias Agopian518ec112011-05-13 16:21:08 -0700724 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600725 if (attribute == EGL_GL_COLORSPACE_KHR) {
726 *value = s->getColorSpace();
727 return EGL_TRUE;
728 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800729 return s->cnx->egl.eglQuerySurface(
730 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700731}
732
Jamie Gennise8696a42012-01-15 18:54:57 -0800733void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800734 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800735 clearError();
736
Jesse Hallb29e5e82012-04-04 16:53:42 -0700737 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800738 if (!dp) {
739 return;
740 }
741
Jesse Hallb29e5e82012-04-04 16:53:42 -0700742 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800743 if (!_s.get()) {
744 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800745 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800746}
747
Mathias Agopian518ec112011-05-13 16:21:08 -0700748// ----------------------------------------------------------------------------
749// Contexts
750// ----------------------------------------------------------------------------
751
752EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
753 EGLContext share_list, const EGLint *attrib_list)
754{
755 clearError();
756
Jesse Hallb29e5e82012-04-04 16:53:42 -0700757 egl_connection_t* cnx = NULL;
758 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700759 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700760 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700761 if (!ContextRef(dp.get(), share_list).get()) {
762 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
763 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700764 egl_context_t* const c = get_context(share_list);
765 share_list = c->context;
766 }
767 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800768 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 if (context != EGL_NO_CONTEXT) {
770 // figure out if it's a GLESv1 or GLESv2
771 int version = 0;
772 if (attrib_list) {
773 while (*attrib_list != EGL_NONE) {
774 GLint attr = *attrib_list++;
775 GLint value = *attrib_list++;
776 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
777 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800778 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800779 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800780 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700781 }
782 }
783 };
784 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700785 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
786 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700787 return c;
788 }
789 }
790 return EGL_NO_CONTEXT;
791}
792
793EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
794{
795 clearError();
796
Jesse Hallb29e5e82012-04-04 16:53:42 -0700797 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700798 if (!dp)
799 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700800
Jesse Hallb29e5e82012-04-04 16:53:42 -0700801 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700802 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800803 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800804
Mathias Agopian518ec112011-05-13 16:21:08 -0700805 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800806 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700807 if (result == EGL_TRUE) {
808 _c.terminate();
809 }
810 return result;
811}
812
Mathias Agopian518ec112011-05-13 16:21:08 -0700813EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
814 EGLSurface read, EGLContext ctx)
815{
816 clearError();
817
Jesse Hallb29e5e82012-04-04 16:53:42 -0700818 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800819 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700820
Mathias Agopian5b287a62011-05-16 18:58:55 -0700821 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
822 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
823 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700824 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
825 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800826 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 }
828
829 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700830 ContextRef _c(dp.get(), ctx);
831 SurfaceRef _d(dp.get(), draw);
832 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700833
834 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700835 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700836 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800837 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700838 }
839
840 // these are the underlying implementation's object
841 EGLContext impl_ctx = EGL_NO_CONTEXT;
842 EGLSurface impl_draw = EGL_NO_SURFACE;
843 EGLSurface impl_read = EGL_NO_SURFACE;
844
845 // these are our objects structs passed in
846 egl_context_t * c = NULL;
847 egl_surface_t const * d = NULL;
848 egl_surface_t const * r = NULL;
849
850 // these are the current objects structs
851 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800852
Mathias Agopian518ec112011-05-13 16:21:08 -0700853 if (ctx != EGL_NO_CONTEXT) {
854 c = get_context(ctx);
855 impl_ctx = c->context;
856 } else {
857 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700858 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
859 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800860 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700861 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700862 if (cur_c == NULL) {
863 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700864 // not an error, there is just no current context.
865 return EGL_TRUE;
866 }
867 }
868
869 // retrieve the underlying implementation's draw EGLSurface
870 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800871 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700872 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700873 impl_draw = d->surface;
874 }
875
876 // retrieve the underlying implementation's read EGLSurface
877 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800878 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700879 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700880 impl_read = r->surface;
881 }
882
Mathias Agopian518ec112011-05-13 16:21:08 -0700883
Jesse Hallb29e5e82012-04-04 16:53:42 -0700884 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800885 draw, read, ctx,
886 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700887
888 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800889 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700890 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
891 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700892 _c.acquire();
893 _r.acquire();
894 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700895 } else {
896 setGLHooksThreadSpecific(&gHooksNoContext);
897 egl_tls_t::setContext(EGL_NO_CONTEXT);
898 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700899 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -0600900 // Force return to current context for drivers that cannot handle errors
901 EGLBoolean restore_result = EGL_FALSE;
902
903 // get a reference to the old current objects
904 ContextRef _c2(dp.get(), cur_c);
905 SurfaceRef _d2(dp.get(), cur_c->draw);
906 SurfaceRef _r2(dp.get(), cur_c->read);
907
908 if (cur_c == NULL) {
909 restore_result = dp->makeCurrent(c, cur_c,
910 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT,
911 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
912 } else {
913 c = cur_c;
914 impl_ctx = c->context;
915 impl_draw = EGL_NO_SURFACE;
916 if (cur_c->draw != EGL_NO_SURFACE) {
917 d = get_surface(cur_c->draw);
918 impl_draw = d->surface;
919 }
920 impl_read = EGL_NO_SURFACE;
921 if (cur_c->read != EGL_NO_SURFACE) {
922 r = get_surface(cur_c->read);
923 impl_read = r->surface;
924 }
925 restore_result = dp->makeCurrent(c, cur_c,
926 cur_c->draw, cur_c->read, cur_c->context,
927 impl_draw, impl_read, impl_ctx);
928 }
929 if (restore_result == EGL_TRUE) {
930 _c2.acquire();
931 _r2.acquire();
932 _d2.acquire();
933 } else {
934 ALOGE("Could not restore original EGL context");
935 }
Steve Blocke6f43dd2012-01-06 19:20:56 +0000936 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700937 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -0800938 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700939 }
940 return result;
941}
942
943
944EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
945 EGLint attribute, EGLint *value)
946{
947 clearError();
948
Jesse Hallb29e5e82012-04-04 16:53:42 -0700949 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700950 if (!dp) return EGL_FALSE;
951
Jesse Hallb29e5e82012-04-04 16:53:42 -0700952 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -0800953 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700954
Mathias Agopian518ec112011-05-13 16:21:08 -0700955 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800956 return c->cnx->egl.eglQueryContext(
957 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700958
Mathias Agopian518ec112011-05-13 16:21:08 -0700959}
960
961EGLContext eglGetCurrentContext(void)
962{
963 // could be called before eglInitialize(), but we wouldn't have a context
964 // then, and this function would correctly return EGL_NO_CONTEXT.
965
966 clearError();
967
968 EGLContext ctx = getContext();
969 return ctx;
970}
971
972EGLSurface eglGetCurrentSurface(EGLint readdraw)
973{
974 // could be called before eglInitialize(), but we wouldn't have a context
975 // then, and this function would correctly return EGL_NO_SURFACE.
976
977 clearError();
978
979 EGLContext ctx = getContext();
980 if (ctx) {
981 egl_context_t const * const c = get_context(ctx);
982 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
983 switch (readdraw) {
984 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800985 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700986 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
987 }
988 }
989 return EGL_NO_SURFACE;
990}
991
992EGLDisplay eglGetCurrentDisplay(void)
993{
994 // could be called before eglInitialize(), but we wouldn't have a context
995 // then, and this function would correctly return EGL_NO_DISPLAY.
996
997 clearError();
998
999 EGLContext ctx = getContext();
1000 if (ctx) {
1001 egl_context_t const * const c = get_context(ctx);
1002 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1003 return c->dpy;
1004 }
1005 return EGL_NO_DISPLAY;
1006}
1007
1008EGLBoolean eglWaitGL(void)
1009{
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 clearError();
1011
Mathias Agopianada798b2012-02-13 17:09:30 -08001012 egl_connection_t* const cnx = &gEGLImpl;
1013 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001014 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001015
1016 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001017}
1018
1019EGLBoolean eglWaitNative(EGLint engine)
1020{
Mathias Agopian518ec112011-05-13 16:21:08 -07001021 clearError();
1022
Mathias Agopianada798b2012-02-13 17:09:30 -08001023 egl_connection_t* const cnx = &gEGLImpl;
1024 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001025 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001026
1027 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001028}
1029
1030EGLint eglGetError(void)
1031{
Mathias Agopianada798b2012-02-13 17:09:30 -08001032 EGLint err = EGL_SUCCESS;
1033 egl_connection_t* const cnx = &gEGLImpl;
1034 if (cnx->dso) {
1035 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001036 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001037 if (err == EGL_SUCCESS) {
1038 err = egl_tls_t::getError();
1039 }
1040 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001041}
1042
Michael Chockc0ec5e22014-01-27 08:14:33 -08001043static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001044 const char* procname) {
1045 const egl_connection_t* cnx = &gEGLImpl;
1046 void* proc = NULL;
1047
Michael Chockc0ec5e22014-01-27 08:14:33 -08001048 proc = dlsym(cnx->libEgl, procname);
1049 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1050
Jesse Hallc07b5202013-07-04 12:08:16 -07001051 proc = dlsym(cnx->libGles2, procname);
1052 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1053
1054 proc = dlsym(cnx->libGles1, procname);
1055 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1056
1057 return NULL;
1058}
1059
Mathias Agopian518ec112011-05-13 16:21:08 -07001060__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1061{
1062 // eglGetProcAddress() could be the very first function called
1063 // in which case we must make sure we've initialized ourselves, this
1064 // happens the first time egl_get_display() is called.
1065
1066 clearError();
1067
1068 if (egl_init_drivers() == EGL_FALSE) {
1069 setError(EGL_BAD_PARAMETER, NULL);
1070 return NULL;
1071 }
1072
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001073 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001074 return NULL;
1075 }
1076
Mathias Agopian518ec112011-05-13 16:21:08 -07001077 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001078 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001079 if (addr) return addr;
1080
Michael Chockc0ec5e22014-01-27 08:14:33 -08001081 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001082 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001083
Mathias Agopian518ec112011-05-13 16:21:08 -07001084 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1085 pthread_mutex_lock(&sExtensionMapMutex);
1086
1087 /*
1088 * Since eglGetProcAddress() is not associated to anything, it needs
1089 * to return a function pointer that "works" regardless of what
1090 * the current context is.
1091 *
1092 * For this reason, we return a "forwarder", a small stub that takes
1093 * care of calling the function associated with the context
1094 * currently bound.
1095 *
1096 * We first look for extensions we've already resolved, if we're seeing
1097 * this extension for the first time, we go through all our
1098 * implementations and call eglGetProcAddress() and record the
1099 * result in the appropriate implementation hooks and return the
1100 * address of the forwarder corresponding to that hook set.
1101 *
1102 */
1103
Mathias Agopian65421432017-03-08 11:49:05 -08001104 const std::string name(procname);
1105
1106 auto& extentionMap = sGLExtentionMap;
1107 auto pos = extentionMap.find(name);
1108 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001109 const int slot = sGLExtentionSlot;
1110
Steve Blocke6f43dd2012-01-06 19:20:56 +00001111 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001112 "no more slots for eglGetProcAddress(\"%s\")",
1113 procname);
1114
1115 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1116 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001117
1118 egl_connection_t* const cnx = &gEGLImpl;
1119 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001120 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001121 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001122 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1123 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001124 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001125 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001126 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001127
Mathias Agopian518ec112011-05-13 16:21:08 -07001128 if (found) {
1129 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001130 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001131 sGLExtentionSlot++;
1132 }
1133 }
1134
1135 pthread_mutex_unlock(&sExtensionMapMutex);
1136 return addr;
1137}
1138
Mathias Agopian65421432017-03-08 11:49:05 -08001139class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001140public:
1141
1142 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001143 static FrameCompletionThread thread;
1144
1145 char name[64];
1146
1147 std::lock_guard<std::mutex> lock(thread.mMutex);
1148 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1149 ATRACE_NAME(name);
1150
1151 thread.mQueue.push_back(sync);
1152 thread.mCondition.notify_one();
1153 thread.mFramesQueued++;
1154 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001155 }
1156
1157private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001158
Mathias Agopian65421432017-03-08 11:49:05 -08001159 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1160 std::thread thread(&FrameCompletionThread::loop, this);
1161 thread.detach();
1162 }
1163
1164#pragma clang diagnostic push
1165#pragma clang diagnostic ignored "-Wmissing-noreturn"
1166 void loop() {
1167 while (true) {
1168 threadLoop();
1169 }
1170 }
1171#pragma clang diagnostic pop
1172
1173 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001174 EGLSyncKHR sync;
1175 uint32_t frameNum;
1176 {
Mathias Agopian65421432017-03-08 11:49:05 -08001177 std::unique_lock<std::mutex> lock(mMutex);
1178 while (mQueue.empty()) {
1179 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001180 }
1181 sync = mQueue[0];
1182 frameNum = mFramesCompleted;
1183 }
1184 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1185 {
Mathias Agopian65421432017-03-08 11:49:05 -08001186 char name[64];
1187 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1188 ATRACE_NAME(name);
1189
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001190 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1191 if (result == EGL_FALSE) {
1192 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1193 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1194 ALOGE("FrameCompletion: timeout waiting for fence");
1195 }
1196 eglDestroySyncKHR(dpy, sync);
1197 }
1198 {
Mathias Agopian65421432017-03-08 11:49:05 -08001199 std::lock_guard<std::mutex> lock(mMutex);
1200 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001201 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001202 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001203 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001204 }
1205
1206 uint32_t mFramesQueued;
1207 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001208 std::deque<EGLSyncKHR> mQueue;
1209 std::condition_variable mCondition;
1210 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001211};
1212
Dan Stozaa894d082015-02-19 15:27:36 -08001213EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1214 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001215{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001216 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001217 clearError();
1218
Jesse Hallb29e5e82012-04-04 16:53:42 -07001219 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001220 if (!dp) return EGL_FALSE;
1221
Jesse Hallb29e5e82012-04-04 16:53:42 -07001222 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001223 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001224 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001225
Mathias Agopian518ec112011-05-13 16:21:08 -07001226 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001227
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001228 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1229 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1230 if (sync != EGL_NO_SYNC_KHR) {
1231 FrameCompletionThread::queueSync(sync);
1232 }
1233 }
1234
Mathias Agopian7db993a2012-03-25 00:49:46 -07001235 if (CC_UNLIKELY(dp->finishOnSwap)) {
1236 uint32_t pixel;
1237 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1238 if (c) {
1239 // glReadPixels() ensures that the frame is complete
1240 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1241 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1242 }
1243 }
1244
Dan Stozaa894d082015-02-19 15:27:36 -08001245 if (n_rects == 0) {
1246 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1247 }
1248
Mathias Agopian65421432017-03-08 11:49:05 -08001249 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001250 for (int r = 0; r < n_rects; ++r) {
1251 int offset = r * 4;
1252 int x = rects[offset];
1253 int y = rects[offset + 1];
1254 int width = rects[offset + 2];
1255 int height = rects[offset + 3];
1256 android_native_rect_t androidRect;
1257 androidRect.left = x;
1258 androidRect.top = y + height;
1259 androidRect.right = x + width;
1260 androidRect.bottom = y;
1261 androidRects.push_back(androidRect);
1262 }
Mathias Agopian65421432017-03-08 11:49:05 -08001263 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001264
1265 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1266 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1267 rects, n_rects);
1268 } else {
1269 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1270 }
1271}
1272
1273EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1274{
1275 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001276}
1277
1278EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1279 NativePixmapType target)
1280{
1281 clearError();
1282
Jesse Hallb29e5e82012-04-04 16:53:42 -07001283 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001284 if (!dp) return EGL_FALSE;
1285
Jesse Hallb29e5e82012-04-04 16:53:42 -07001286 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001287 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001288 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001289
Mathias Agopian518ec112011-05-13 16:21:08 -07001290 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001291 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001292}
1293
1294const char* eglQueryString(EGLDisplay dpy, EGLint name)
1295{
1296 clearError();
1297
Chia-I Wue57d1352016-08-15 16:10:02 +08001298 // Generate an error quietly when client extensions (as defined by
1299 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1300 // validate_display to generate the error as validate_display would log
1301 // the error, which can be misleading.
1302 //
1303 // If we want to support EGL_EXT_client_extensions later, we can return
1304 // the client extension string here instead.
1305 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001306 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001307
Jesse Hallb29e5e82012-04-04 16:53:42 -07001308 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001309 if (!dp) return (const char *) NULL;
1310
1311 switch (name) {
1312 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001313 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001314 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001315 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001316 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001317 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001318 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001319 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001320 default:
1321 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001322 }
1323 return setError(EGL_BAD_PARAMETER, (const char *)0);
1324}
1325
Mathias Agopianca088332013-03-28 17:44:13 -07001326EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1327{
1328 clearError();
1329
1330 const egl_display_ptr dp = validate_display(dpy);
1331 if (!dp) return (const char *) NULL;
1332
1333 switch (name) {
1334 case EGL_VENDOR:
1335 return dp->disp.queryString.vendor;
1336 case EGL_VERSION:
1337 return dp->disp.queryString.version;
1338 case EGL_EXTENSIONS:
1339 return dp->disp.queryString.extensions;
1340 case EGL_CLIENT_APIS:
1341 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001342 default:
1343 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001344 }
1345 return setError(EGL_BAD_PARAMETER, (const char *)0);
1346}
Mathias Agopian518ec112011-05-13 16:21:08 -07001347
1348// ----------------------------------------------------------------------------
1349// EGL 1.1
1350// ----------------------------------------------------------------------------
1351
1352EGLBoolean eglSurfaceAttrib(
1353 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1354{
1355 clearError();
1356
Jesse Hallb29e5e82012-04-04 16:53:42 -07001357 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001358 if (!dp) return EGL_FALSE;
1359
Jesse Hallb29e5e82012-04-04 16:53:42 -07001360 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001361 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001362 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001363
Pablo Ceballosc18be292016-05-31 14:55:42 -07001364 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001365
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001366 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001367 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001368 setError(EGL_BAD_SURFACE, EGL_FALSE);
1369 }
Mathias Agopian65421432017-03-08 11:49:05 -08001370 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1371 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001372 }
1373
Pablo Ceballosc18be292016-05-31 14:55:42 -07001374 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001375 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001376 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001377 }
Mathias Agopian65421432017-03-08 11:49:05 -08001378 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1379 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001380 }
1381
Mathias Agopian518ec112011-05-13 16:21:08 -07001382 if (s->cnx->egl.eglSurfaceAttrib) {
1383 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001384 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001385 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001386 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001387}
1388
1389EGLBoolean eglBindTexImage(
1390 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1391{
1392 clearError();
1393
Jesse Hallb29e5e82012-04-04 16:53:42 -07001394 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001395 if (!dp) return EGL_FALSE;
1396
Jesse Hallb29e5e82012-04-04 16:53:42 -07001397 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001398 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001399 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001400
Mathias Agopian518ec112011-05-13 16:21:08 -07001401 egl_surface_t const * const s = get_surface(surface);
1402 if (s->cnx->egl.eglBindTexImage) {
1403 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001404 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001405 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001406 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001407}
1408
1409EGLBoolean eglReleaseTexImage(
1410 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1411{
1412 clearError();
1413
Jesse Hallb29e5e82012-04-04 16:53:42 -07001414 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001415 if (!dp) return EGL_FALSE;
1416
Jesse Hallb29e5e82012-04-04 16:53:42 -07001417 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001418 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001419 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001420
Mathias Agopian518ec112011-05-13 16:21:08 -07001421 egl_surface_t const * const s = get_surface(surface);
1422 if (s->cnx->egl.eglReleaseTexImage) {
1423 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001424 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001426 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001427}
1428
1429EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1430{
1431 clearError();
1432
Jesse Hallb29e5e82012-04-04 16:53:42 -07001433 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001434 if (!dp) return EGL_FALSE;
1435
1436 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001437 egl_connection_t* const cnx = &gEGLImpl;
1438 if (cnx->dso && cnx->egl.eglSwapInterval) {
1439 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001440 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001441
Mathias Agopian518ec112011-05-13 16:21:08 -07001442 return res;
1443}
1444
1445
1446// ----------------------------------------------------------------------------
1447// EGL 1.2
1448// ----------------------------------------------------------------------------
1449
1450EGLBoolean eglWaitClient(void)
1451{
1452 clearError();
1453
Mathias Agopianada798b2012-02-13 17:09:30 -08001454 egl_connection_t* const cnx = &gEGLImpl;
1455 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001456 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001457
1458 EGLBoolean res;
1459 if (cnx->egl.eglWaitClient) {
1460 res = cnx->egl.eglWaitClient();
1461 } else {
1462 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001463 }
1464 return res;
1465}
1466
1467EGLBoolean eglBindAPI(EGLenum api)
1468{
1469 clearError();
1470
1471 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001472 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001473 }
1474
1475 // bind this API on all EGLs
1476 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001477 egl_connection_t* const cnx = &gEGLImpl;
1478 if (cnx->dso && cnx->egl.eglBindAPI) {
1479 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001480 }
1481 return res;
1482}
1483
1484EGLenum eglQueryAPI(void)
1485{
1486 clearError();
1487
1488 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001489 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001490 }
1491
Mathias Agopianada798b2012-02-13 17:09:30 -08001492 egl_connection_t* const cnx = &gEGLImpl;
1493 if (cnx->dso && cnx->egl.eglQueryAPI) {
1494 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001495 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001496
Mathias Agopian518ec112011-05-13 16:21:08 -07001497 // or, it can only be OpenGL ES
1498 return EGL_OPENGL_ES_API;
1499}
1500
1501EGLBoolean eglReleaseThread(void)
1502{
1503 clearError();
1504
Mathias Agopianada798b2012-02-13 17:09:30 -08001505 egl_connection_t* const cnx = &gEGLImpl;
1506 if (cnx->dso && cnx->egl.eglReleaseThread) {
1507 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001508 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301509
1510 // If there is context bound to the thread, release it
1511 egl_display_t::loseCurrent(get_context(getContext()));
1512
Mathias Agopian518ec112011-05-13 16:21:08 -07001513 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001514 return EGL_TRUE;
1515}
1516
1517EGLSurface eglCreatePbufferFromClientBuffer(
1518 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1519 EGLConfig config, const EGLint *attrib_list)
1520{
1521 clearError();
1522
Jesse Hallb29e5e82012-04-04 16:53:42 -07001523 egl_connection_t* cnx = NULL;
1524 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1525 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001526 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1527 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001528 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001529 }
1530 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1531}
1532
1533// ----------------------------------------------------------------------------
1534// EGL_EGLEXT_VERSION 3
1535// ----------------------------------------------------------------------------
1536
1537EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1538 const EGLint *attrib_list)
1539{
1540 clearError();
1541
Jesse Hallb29e5e82012-04-04 16:53:42 -07001542 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001543 if (!dp) return EGL_FALSE;
1544
Jesse Hallb29e5e82012-04-04 16:53:42 -07001545 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001546 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001547 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001548
1549 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001550 if (s->cnx->egl.eglLockSurfaceKHR) {
1551 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001552 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001553 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001554 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001555}
1556
1557EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1558{
1559 clearError();
1560
Jesse Hallb29e5e82012-04-04 16:53:42 -07001561 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001562 if (!dp) return EGL_FALSE;
1563
Jesse Hallb29e5e82012-04-04 16:53:42 -07001564 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001565 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001566 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001567
1568 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001569 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001570 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001571 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001572 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001573}
1574
1575EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1576 EGLClientBuffer buffer, const EGLint *attrib_list)
1577{
1578 clearError();
1579
Jesse Hallb29e5e82012-04-04 16:53:42 -07001580 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001581 if (!dp) return EGL_NO_IMAGE_KHR;
1582
Jesse Hallb29e5e82012-04-04 16:53:42 -07001583 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001584 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001585
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001586 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1587 egl_connection_t* const cnx = &gEGLImpl;
1588 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1589 result = cnx->egl.eglCreateImageKHR(
1590 dp->disp.dpy,
1591 c ? c->context : EGL_NO_CONTEXT,
1592 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001593 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001594 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001595}
1596
1597EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1598{
1599 clearError();
1600
Jesse Hallb29e5e82012-04-04 16:53:42 -07001601 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001602 if (!dp) return EGL_FALSE;
1603
Steven Holte646a5c52012-06-04 20:02:11 -07001604 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001605 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001606 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001607 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001608 }
Steven Holte646a5c52012-06-04 20:02:11 -07001609 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001610}
1611
1612// ----------------------------------------------------------------------------
1613// EGL_EGLEXT_VERSION 5
1614// ----------------------------------------------------------------------------
1615
1616
1617EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1618{
1619 clearError();
1620
Jesse Hallb29e5e82012-04-04 16:53:42 -07001621 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001622 if (!dp) return EGL_NO_SYNC_KHR;
1623
Mathias Agopian518ec112011-05-13 16:21:08 -07001624 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001625 egl_connection_t* const cnx = &gEGLImpl;
1626 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1627 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001628 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001629 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001630}
1631
1632EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1633{
1634 clearError();
1635
Jesse Hallb29e5e82012-04-04 16:53:42 -07001636 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001637 if (!dp) return EGL_FALSE;
1638
Mathias Agopian518ec112011-05-13 16:21:08 -07001639 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001640 egl_connection_t* const cnx = &gEGLImpl;
1641 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1642 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001643 }
1644 return result;
1645}
1646
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001647EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1648 clearError();
1649
1650 const egl_display_ptr dp = validate_display(dpy);
1651 if (!dp) return EGL_FALSE;
1652
1653 EGLBoolean result = EGL_FALSE;
1654 egl_connection_t* const cnx = &gEGLImpl;
1655 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1656 result = cnx->egl.eglSignalSyncKHR(
1657 dp->disp.dpy, sync, mode);
1658 }
1659 return result;
1660}
1661
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001662EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1663 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001664{
1665 clearError();
1666
Jesse Hallb29e5e82012-04-04 16:53:42 -07001667 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001668 if (!dp) return EGL_FALSE;
1669
Mathias Agopian737b8962017-02-24 14:32:05 -08001670 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001671 egl_connection_t* const cnx = &gEGLImpl;
1672 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1673 result = cnx->egl.eglClientWaitSyncKHR(
1674 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001675 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001676 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001677}
1678
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001679EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1680 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001681{
1682 clearError();
1683
Jesse Hallb29e5e82012-04-04 16:53:42 -07001684 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001685 if (!dp) return EGL_FALSE;
1686
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001687 EGLBoolean result = EGL_FALSE;
1688 egl_connection_t* const cnx = &gEGLImpl;
1689 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1690 result = cnx->egl.eglGetSyncAttribKHR(
1691 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001692 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001693 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001694}
1695
Season Li000d88f2015-07-01 11:39:40 -07001696EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1697{
1698 clearError();
1699
1700 const egl_display_ptr dp = validate_display(dpy);
1701 if (!dp) return EGL_NO_STREAM_KHR;
1702
1703 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1704 egl_connection_t* const cnx = &gEGLImpl;
1705 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1706 result = cnx->egl.eglCreateStreamKHR(
1707 dp->disp.dpy, attrib_list);
1708 }
1709 return result;
1710}
1711
1712EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1713{
1714 clearError();
1715
1716 const egl_display_ptr dp = validate_display(dpy);
1717 if (!dp) return EGL_FALSE;
1718
1719 EGLBoolean result = EGL_FALSE;
1720 egl_connection_t* const cnx = &gEGLImpl;
1721 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1722 result = cnx->egl.eglDestroyStreamKHR(
1723 dp->disp.dpy, stream);
1724 }
1725 return result;
1726}
1727
1728EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1729 EGLenum attribute, EGLint value)
1730{
1731 clearError();
1732
1733 const egl_display_ptr dp = validate_display(dpy);
1734 if (!dp) return EGL_FALSE;
1735
1736 EGLBoolean result = EGL_FALSE;
1737 egl_connection_t* const cnx = &gEGLImpl;
1738 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1739 result = cnx->egl.eglStreamAttribKHR(
1740 dp->disp.dpy, stream, attribute, value);
1741 }
1742 return result;
1743}
1744
1745EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1746 EGLenum attribute, EGLint *value)
1747{
1748 clearError();
1749
1750 const egl_display_ptr dp = validate_display(dpy);
1751 if (!dp) return EGL_FALSE;
1752
1753 EGLBoolean result = EGL_FALSE;
1754 egl_connection_t* const cnx = &gEGLImpl;
1755 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1756 result = cnx->egl.eglQueryStreamKHR(
1757 dp->disp.dpy, stream, attribute, value);
1758 }
1759 return result;
1760}
1761
1762EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1763 EGLenum attribute, EGLuint64KHR *value)
1764{
1765 clearError();
1766
1767 const egl_display_ptr dp = validate_display(dpy);
1768 if (!dp) return EGL_FALSE;
1769
1770 EGLBoolean result = EGL_FALSE;
1771 egl_connection_t* const cnx = &gEGLImpl;
1772 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1773 result = cnx->egl.eglQueryStreamu64KHR(
1774 dp->disp.dpy, stream, attribute, value);
1775 }
1776 return result;
1777}
1778
1779EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1780 EGLenum attribute, EGLTimeKHR *value)
1781{
1782 clearError();
1783
1784 const egl_display_ptr dp = validate_display(dpy);
1785 if (!dp) return EGL_FALSE;
1786
1787 EGLBoolean result = EGL_FALSE;
1788 egl_connection_t* const cnx = &gEGLImpl;
1789 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1790 result = cnx->egl.eglQueryStreamTimeKHR(
1791 dp->disp.dpy, stream, attribute, value);
1792 }
1793 return result;
1794}
1795
1796EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1797 EGLStreamKHR stream, const EGLint *attrib_list)
1798{
1799 clearError();
1800
1801 egl_display_ptr dp = validate_display(dpy);
1802 if (!dp) return EGL_NO_SURFACE;
1803
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06001804 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
1805 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
1806 // TODO: Probably need to update EGL_KHR_stream_producer_eglsurface to
1807 // indicate support for EGL_GL_COLORSPACE_KHR.
1808 // now select a corresponding sRGB format if needed
1809 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
1810 ALOGE("error invalid colorspace: %d", colorSpace);
1811 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1812 }
1813
Season Li000d88f2015-07-01 11:39:40 -07001814 egl_connection_t* const cnx = &gEGLImpl;
1815 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1816 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1817 dp->disp.dpy, config, stream, attrib_list);
1818 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06001819 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001820 return s;
1821 }
1822 }
1823 return EGL_NO_SURFACE;
1824}
1825
1826EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1827 EGLStreamKHR stream)
1828{
1829 clearError();
1830
1831 const egl_display_ptr dp = validate_display(dpy);
1832 if (!dp) return EGL_FALSE;
1833
1834 EGLBoolean result = EGL_FALSE;
1835 egl_connection_t* const cnx = &gEGLImpl;
1836 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1837 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1838 dp->disp.dpy, stream);
1839 }
1840 return result;
1841}
1842
1843EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1844 EGLStreamKHR stream)
1845{
1846 clearError();
1847
1848 const egl_display_ptr dp = validate_display(dpy);
1849 if (!dp) return EGL_FALSE;
1850
1851 EGLBoolean result = EGL_FALSE;
1852 egl_connection_t* const cnx = &gEGLImpl;
1853 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1854 result = cnx->egl.eglStreamConsumerAcquireKHR(
1855 dp->disp.dpy, stream);
1856 }
1857 return result;
1858}
1859
1860EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1861 EGLStreamKHR stream)
1862{
1863 clearError();
1864
1865 const egl_display_ptr dp = validate_display(dpy);
1866 if (!dp) return EGL_FALSE;
1867
1868 EGLBoolean result = EGL_FALSE;
1869 egl_connection_t* const cnx = &gEGLImpl;
1870 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1871 result = cnx->egl.eglStreamConsumerReleaseKHR(
1872 dp->disp.dpy, stream);
1873 }
1874 return result;
1875}
1876
1877EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1878 EGLDisplay dpy, EGLStreamKHR stream)
1879{
1880 clearError();
1881
1882 const egl_display_ptr dp = validate_display(dpy);
1883 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1884
1885 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1886 egl_connection_t* const cnx = &gEGLImpl;
1887 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1888 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1889 dp->disp.dpy, stream);
1890 }
1891 return result;
1892}
1893
1894EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1895 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1896{
1897 clearError();
1898
1899 const egl_display_ptr dp = validate_display(dpy);
1900 if (!dp) return EGL_NO_STREAM_KHR;
1901
1902 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1903 egl_connection_t* const cnx = &gEGLImpl;
1904 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1905 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1906 dp->disp.dpy, file_descriptor);
1907 }
1908 return result;
1909}
1910
Mathias Agopian518ec112011-05-13 16:21:08 -07001911// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001912// EGL_EGLEXT_VERSION 15
1913// ----------------------------------------------------------------------------
1914
1915EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1916 clearError();
1917 const egl_display_ptr dp = validate_display(dpy);
1918 if (!dp) return EGL_FALSE;
1919 EGLint result = EGL_FALSE;
1920 egl_connection_t* const cnx = &gEGLImpl;
1921 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1922 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1923 }
1924 return result;
1925}
1926
1927// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001928// ANDROID extensions
1929// ----------------------------------------------------------------------------
1930
Jamie Gennis331841b2012-09-06 14:52:00 -07001931EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1932{
1933 clearError();
1934
1935 const egl_display_ptr dp = validate_display(dpy);
1936 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1937
1938 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1939 egl_connection_t* const cnx = &gEGLImpl;
1940 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1941 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1942 }
1943 return result;
1944}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001945
Andy McFadden72841452013-03-01 16:25:32 -08001946EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1947 EGLnsecsANDROID time)
1948{
1949 clearError();
1950
1951 const egl_display_ptr dp = validate_display(dpy);
1952 if (!dp) {
1953 return EGL_FALSE;
1954 }
1955
1956 SurfaceRef _s(dp.get(), surface);
1957 if (!_s.get()) {
1958 setError(EGL_BAD_SURFACE, EGL_FALSE);
1959 return EGL_FALSE;
1960 }
1961
1962 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08001963 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08001964
1965 return EGL_TRUE;
1966}
1967
Craig Donner60761072017-01-27 12:30:44 -08001968EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1969 clearError();
Craig Donner60761072017-01-27 12:30:44 -08001970 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08001971 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Craig Donner60761072017-01-27 12:30:44 -08001972}
1973
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001974// ----------------------------------------------------------------------------
1975// NVIDIA extensions
1976// ----------------------------------------------------------------------------
1977EGLuint64NV eglGetSystemTimeFrequencyNV()
1978{
1979 clearError();
1980
1981 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001982 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001983 }
1984
1985 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001986 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001987
Mathias Agopianada798b2012-02-13 17:09:30 -08001988 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1989 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001990 }
1991
Mathias Agopian737b8962017-02-24 14:32:05 -08001992 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001993}
1994
1995EGLuint64NV eglGetSystemTimeNV()
1996{
1997 clearError();
1998
1999 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002000 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002001 }
2002
2003 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002004 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002005
Mathias Agopianada798b2012-02-13 17:09:30 -08002006 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2007 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002008 }
2009
Mathias Agopian737b8962017-02-24 14:32:05 -08002010 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002011}
Dan Stozaa894d082015-02-19 15:27:36 -08002012
2013// ----------------------------------------------------------------------------
2014// Partial update extension
2015// ----------------------------------------------------------------------------
2016EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2017 EGLint *rects, EGLint n_rects)
2018{
2019 clearError();
2020
2021 const egl_display_ptr dp = validate_display(dpy);
2022 if (!dp) {
2023 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2024 return EGL_FALSE;
2025 }
2026
2027 SurfaceRef _s(dp.get(), surface);
2028 if (!_s.get()) {
2029 setError(EGL_BAD_SURFACE, EGL_FALSE);
2030 return EGL_FALSE;
2031 }
2032
2033 egl_surface_t const * const s = get_surface(surface);
2034 if (s->cnx->egl.eglSetDamageRegionKHR) {
2035 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2036 rects, n_rects);
2037 }
2038
2039 return EGL_FALSE;
2040}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002041
Brian Anderson1049d1d2016-12-16 17:25:57 -08002042EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2043 EGLuint64KHR *frameId) {
2044 clearError();
2045
2046 const egl_display_ptr dp = validate_display(dpy);
2047 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002048 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002049 }
2050
2051 SurfaceRef _s(dp.get(), surface);
2052 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002053 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002054 }
2055
2056 egl_surface_t const * const s = get_surface(surface);
2057
Mathias Agopian65421432017-03-08 11:49:05 -08002058 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002059 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002060 }
2061
2062 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002063 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002064
Mathias Agopian65421432017-03-08 11:49:05 -08002065 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002066 // This should not happen. Return an error that is not in the spec
2067 // so it's obvious something is very wrong.
2068 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002069 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002070 }
2071
2072 *frameId = nextFrameId;
2073 return EGL_TRUE;
2074}
2075
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002076EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2077 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2078{
2079 clearError();
2080
2081 const egl_display_ptr dp = validate_display(dpy);
2082 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002083 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002084 }
2085
2086 SurfaceRef _s(dp.get(), surface);
2087 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002088 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002089 }
2090
2091 egl_surface_t const * const s = get_surface(surface);
2092
Mathias Agopian65421432017-03-08 11:49:05 -08002093 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002094 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002095 }
2096
2097 nsecs_t* compositeDeadline = nullptr;
2098 nsecs_t* compositeInterval = nullptr;
2099 nsecs_t* compositeToPresentLatency = nullptr;
2100
2101 for (int i = 0; i < numTimestamps; i++) {
2102 switch (names[i]) {
2103 case EGL_COMPOSITE_DEADLINE_ANDROID:
2104 compositeDeadline = &values[i];
2105 break;
2106 case EGL_COMPOSITE_INTERVAL_ANDROID:
2107 compositeInterval = &values[i];
2108 break;
2109 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2110 compositeToPresentLatency = &values[i];
2111 break;
2112 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002113 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002114 }
2115 }
2116
Mathias Agopian65421432017-03-08 11:49:05 -08002117 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002118 compositeDeadline, compositeInterval, compositeToPresentLatency);
2119
2120 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002121 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002122 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002123 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002124 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002125 default:
2126 // This should not happen. Return an error that is not in the spec
2127 // so it's obvious something is very wrong.
2128 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002129 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002130 }
2131}
2132
2133EGLBoolean eglGetCompositorTimingSupportedANDROID(
2134 EGLDisplay dpy, EGLSurface surface, EGLint name)
2135{
2136 clearError();
2137
2138 const egl_display_ptr dp = validate_display(dpy);
2139 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002140 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002141 }
2142
2143 SurfaceRef _s(dp.get(), surface);
2144 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002145 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002146 }
2147
2148 egl_surface_t const * const s = get_surface(surface);
2149
Mathias Agopian65421432017-03-08 11:49:05 -08002150 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002151 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002152 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002153 }
2154
2155 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002156 case EGL_COMPOSITE_DEADLINE_ANDROID:
2157 case EGL_COMPOSITE_INTERVAL_ANDROID:
2158 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2159 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002160 default:
2161 return EGL_FALSE;
2162 }
2163}
2164
Pablo Ceballosc18be292016-05-31 14:55:42 -07002165EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002166 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002167 EGLnsecsANDROID *values)
2168{
2169 clearError();
2170
2171 const egl_display_ptr dp = validate_display(dpy);
2172 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002173 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002174 }
2175
2176 SurfaceRef _s(dp.get(), surface);
2177 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002178 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002179 }
2180
2181 egl_surface_t const * const s = get_surface(surface);
2182
Mathias Agopian65421432017-03-08 11:49:05 -08002183 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002184 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002185 }
2186
Brian Andersondbd0ea82016-07-22 09:38:59 -07002187 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002188 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002189 nsecs_t* latchTime = nullptr;
2190 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002191 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002192 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002193 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002194 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002195 nsecs_t* releaseTime = nullptr;
2196
2197 for (int i = 0; i < numTimestamps; i++) {
2198 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002199 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2200 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002201 break;
2202 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2203 acquireTime = &values[i];
2204 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002205 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2206 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002207 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002208 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2209 firstRefreshStartTime = &values[i];
2210 break;
2211 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2212 lastRefreshStartTime = &values[i];
2213 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002214 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2215 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002216 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002217 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2218 displayPresentTime = &values[i];
2219 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002220 case EGL_DEQUEUE_READY_TIME_ANDROID:
2221 dequeueReadyTime = &values[i];
2222 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002223 case EGL_READS_DONE_TIME_ANDROID:
2224 releaseTime = &values[i];
2225 break;
2226 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002227 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002228 }
2229 }
2230
Mathias Agopian65421432017-03-08 11:49:05 -08002231 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002232 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002233 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002234 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002235
Brian Anderson069b3652016-07-22 10:32:47 -07002236 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002237 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002238 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002239 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002240 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002241 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002242 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002243 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002244 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2245 default:
2246 // This should not happen. Return an error that is not in the spec
2247 // so it's obvious something is very wrong.
2248 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2249 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002250 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002251}
2252
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002253EGLBoolean eglGetFrameTimestampSupportedANDROID(
2254 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002255{
2256 clearError();
2257
2258 const egl_display_ptr dp = validate_display(dpy);
2259 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002260 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002261 }
2262
2263 SurfaceRef _s(dp.get(), surface);
2264 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002265 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002266 }
2267
2268 egl_surface_t const * const s = get_surface(surface);
2269
Mathias Agopian65421432017-03-08 11:49:05 -08002270 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002271 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002272 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002273 }
2274
2275 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002276 case EGL_COMPOSITE_DEADLINE_ANDROID:
2277 case EGL_COMPOSITE_INTERVAL_ANDROID:
2278 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002279 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002280 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002281 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2282 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2283 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002284 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002285 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002286 case EGL_READS_DONE_TIME_ANDROID:
2287 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002288 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2289 int value = 0;
2290 window->query(window,
2291 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2292 return value == 0 ? EGL_FALSE : EGL_TRUE;
2293 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002294 default:
2295 return EGL_FALSE;
2296 }
2297}