blob: 7c4bdf91b2aa4a679b31196c1de3a5e3e5fbaaf7 [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 }
508 }
509 }
510 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
511 }
512 return true;
513}
514
Mathias Agopian518ec112011-05-13 16:21:08 -0700515EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
516 NativeWindowType window,
517 const EGLint *attrib_list)
518{
519 clearError();
520
Jesse Hallb29e5e82012-04-04 16:53:42 -0700521 egl_connection_t* cnx = NULL;
522 egl_display_ptr dp = validate_display_connection(dpy, cnx);
523 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800524 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700525
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800526 if (!window) {
527 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
528 }
529
530 int value = 0;
531 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
532 if (!value) {
533 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
534 }
535
Andy McFaddend566ce32014-01-07 15:54:17 -0800536 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800537 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800538 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
539 "failed (%#x) (already connected to another API?)",
540 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700541 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700542 }
543
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700544 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700545 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
546 // of our native format. So if sRGB gamma is requested, we have to
547 // modify the EGLconfig's format before setting the native window's
548 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800549
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700550 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
551 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
552 &componentType);
553
Mathias Agopian95921562017-02-24 14:31:31 -0800554 EGLint format;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600555 EGLint colorSpace;
556 android_dataspace dataSpace;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700557 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700558 EGLint r, g, b;
559 r = g = b = 0;
560 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
561 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
562 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700563 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700564 EGLint colorDepth = r + g + b;
565
566 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700567 if (colorDepth <= 16) {
568 format = HAL_PIXEL_FORMAT_RGB_565;
569 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700570 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
571 if (colorDepth > 24) {
572 format = HAL_PIXEL_FORMAT_RGBA_1010102;
573 } else {
574 format = HAL_PIXEL_FORMAT_RGBX_8888;
575 }
576 } else {
577 format = HAL_PIXEL_FORMAT_RGBA_FP16;
578 }
579 }
580 } else {
581 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
582 if (colorDepth > 24) {
583 format = HAL_PIXEL_FORMAT_RGBA_1010102;
584 } else {
585 format = HAL_PIXEL_FORMAT_RGBA_8888;
586 }
587 } else {
588 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700589 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700590 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700591
592 // now select a corresponding sRGB format if needed
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600593 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
594 ALOGE("error invalid colorspace: %d", colorSpace);
595 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700596 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800597
Jesse Hallc2e41222013-08-08 13:40:22 -0700598 if (format != 0) {
599 int err = native_window_set_buffers_format(window, format);
600 if (err != 0) {
601 ALOGE("error setting native window pixel format: %s (%d)",
602 strerror(-err), err);
603 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
604 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
605 }
606 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700607
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800608 if (dataSpace != 0) {
609 int err = native_window_set_buffers_data_space(window, dataSpace);
610 if (err != 0) {
611 ALOGE("error setting native window pixel dataSpace: %s (%d)",
612 strerror(-err), err);
613 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
614 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
615 }
616 }
617
Jamie Gennis59769462011-11-19 18:04:43 -0800618 // the EGL spec requires that a new EGLSurface default to swap interval
619 // 1, so explicitly set that on the window here.
620 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
621 anw->setSwapInterval(anw, 1);
622
Mathias Agopian518ec112011-05-13 16:21:08 -0700623 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800624 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700625 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600626 egl_surface_t* s =
627 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700628 return s;
629 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700630
631 // EGLSurface creation failed
632 native_window_set_buffers_format(window, 0);
633 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700634 }
635 return EGL_NO_SURFACE;
636}
637
638EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
639 NativePixmapType pixmap,
640 const EGLint *attrib_list)
641{
642 clearError();
643
Jesse Hallb29e5e82012-04-04 16:53:42 -0700644 egl_connection_t* cnx = NULL;
645 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600646 EGLint colorSpace;
647 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700648 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600649 // now select a corresponding sRGB format if needed
650 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
651 ALOGE("error invalid colorspace: %d", colorSpace);
652 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
653 }
654
Mathias Agopian518ec112011-05-13 16:21:08 -0700655 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800656 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700657 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600658 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700659 return s;
660 }
661 }
662 return EGL_NO_SURFACE;
663}
664
665EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
666 const EGLint *attrib_list)
667{
668 clearError();
669
Jesse Hallb29e5e82012-04-04 16:53:42 -0700670 egl_connection_t* cnx = NULL;
671 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600672 EGLint colorSpace;
673 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700674 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600675 // now select a corresponding sRGB format if needed
676 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
677 ALOGE("error invalid colorspace: %d", colorSpace);
678 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
679 }
680
Mathias Agopian518ec112011-05-13 16:21:08 -0700681 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800682 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700683 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600684 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700685 return s;
686 }
687 }
688 return EGL_NO_SURFACE;
689}
Jesse Hall47743382013-02-08 11:13:46 -0800690
Mathias Agopian518ec112011-05-13 16:21:08 -0700691EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
692{
693 clearError();
694
Jesse Hallb29e5e82012-04-04 16:53:42 -0700695 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700696 if (!dp) return EGL_FALSE;
697
Jesse Hallb29e5e82012-04-04 16:53:42 -0700698 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700699 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800700 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700701
702 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800703 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700704 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700705 _s.terminate();
706 }
707 return result;
708}
709
710EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
711 EGLint attribute, EGLint *value)
712{
713 clearError();
714
Jesse Hallb29e5e82012-04-04 16:53:42 -0700715 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700716 if (!dp) return EGL_FALSE;
717
Jesse Hallb29e5e82012-04-04 16:53:42 -0700718 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700719 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800720 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700721
Mathias Agopian518ec112011-05-13 16:21:08 -0700722 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600723 if (attribute == EGL_GL_COLORSPACE_KHR) {
724 *value = s->getColorSpace();
725 return EGL_TRUE;
726 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800727 return s->cnx->egl.eglQuerySurface(
728 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700729}
730
Jamie Gennise8696a42012-01-15 18:54:57 -0800731void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800732 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800733 clearError();
734
Jesse Hallb29e5e82012-04-04 16:53:42 -0700735 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800736 if (!dp) {
737 return;
738 }
739
Jesse Hallb29e5e82012-04-04 16:53:42 -0700740 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800741 if (!_s.get()) {
742 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800743 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800744}
745
Mathias Agopian518ec112011-05-13 16:21:08 -0700746// ----------------------------------------------------------------------------
747// Contexts
748// ----------------------------------------------------------------------------
749
750EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
751 EGLContext share_list, const EGLint *attrib_list)
752{
753 clearError();
754
Jesse Hallb29e5e82012-04-04 16:53:42 -0700755 egl_connection_t* cnx = NULL;
756 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700757 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700759 if (!ContextRef(dp.get(), share_list).get()) {
760 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
761 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700762 egl_context_t* const c = get_context(share_list);
763 share_list = c->context;
764 }
765 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800766 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 if (context != EGL_NO_CONTEXT) {
768 // figure out if it's a GLESv1 or GLESv2
769 int version = 0;
770 if (attrib_list) {
771 while (*attrib_list != EGL_NONE) {
772 GLint attr = *attrib_list++;
773 GLint value = *attrib_list++;
774 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
775 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800776 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800777 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800778 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700779 }
780 }
781 };
782 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700783 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
784 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700785 return c;
786 }
787 }
788 return EGL_NO_CONTEXT;
789}
790
791EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
792{
793 clearError();
794
Jesse Hallb29e5e82012-04-04 16:53:42 -0700795 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700796 if (!dp)
797 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700798
Jesse Hallb29e5e82012-04-04 16:53:42 -0700799 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700800 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800801 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800802
Mathias Agopian518ec112011-05-13 16:21:08 -0700803 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800804 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700805 if (result == EGL_TRUE) {
806 _c.terminate();
807 }
808 return result;
809}
810
Mathias Agopian518ec112011-05-13 16:21:08 -0700811EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
812 EGLSurface read, EGLContext ctx)
813{
814 clearError();
815
Jesse Hallb29e5e82012-04-04 16:53:42 -0700816 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800817 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700818
Mathias Agopian5b287a62011-05-16 18:58:55 -0700819 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
820 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
821 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700822 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
823 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800824 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700825 }
826
827 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700828 ContextRef _c(dp.get(), ctx);
829 SurfaceRef _d(dp.get(), draw);
830 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700831
832 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700833 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700834 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800835 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700836 }
837
838 // these are the underlying implementation's object
839 EGLContext impl_ctx = EGL_NO_CONTEXT;
840 EGLSurface impl_draw = EGL_NO_SURFACE;
841 EGLSurface impl_read = EGL_NO_SURFACE;
842
843 // these are our objects structs passed in
844 egl_context_t * c = NULL;
845 egl_surface_t const * d = NULL;
846 egl_surface_t const * r = NULL;
847
848 // these are the current objects structs
849 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800850
Mathias Agopian518ec112011-05-13 16:21:08 -0700851 if (ctx != EGL_NO_CONTEXT) {
852 c = get_context(ctx);
853 impl_ctx = c->context;
854 } else {
855 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700856 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
857 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800858 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700859 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700860 if (cur_c == NULL) {
861 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700862 // not an error, there is just no current context.
863 return EGL_TRUE;
864 }
865 }
866
867 // retrieve the underlying implementation's draw EGLSurface
868 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800869 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700870 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700871 impl_draw = d->surface;
872 }
873
874 // retrieve the underlying implementation's read EGLSurface
875 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800876 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700877 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700878 impl_read = r->surface;
879 }
880
Mathias Agopian518ec112011-05-13 16:21:08 -0700881
Jesse Hallb29e5e82012-04-04 16:53:42 -0700882 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800883 draw, read, ctx,
884 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700885
886 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800887 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700888 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
889 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700890 _c.acquire();
891 _r.acquire();
892 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700893 } else {
894 setGLHooksThreadSpecific(&gHooksNoContext);
895 egl_tls_t::setContext(EGL_NO_CONTEXT);
896 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700897 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000898 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700899 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -0800900 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700901 }
902 return result;
903}
904
905
906EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
907 EGLint attribute, EGLint *value)
908{
909 clearError();
910
Jesse Hallb29e5e82012-04-04 16:53:42 -0700911 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700912 if (!dp) return EGL_FALSE;
913
Jesse Hallb29e5e82012-04-04 16:53:42 -0700914 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -0800915 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700916
Mathias Agopian518ec112011-05-13 16:21:08 -0700917 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800918 return c->cnx->egl.eglQueryContext(
919 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700920
Mathias Agopian518ec112011-05-13 16:21:08 -0700921}
922
923EGLContext eglGetCurrentContext(void)
924{
925 // could be called before eglInitialize(), but we wouldn't have a context
926 // then, and this function would correctly return EGL_NO_CONTEXT.
927
928 clearError();
929
930 EGLContext ctx = getContext();
931 return ctx;
932}
933
934EGLSurface eglGetCurrentSurface(EGLint readdraw)
935{
936 // could be called before eglInitialize(), but we wouldn't have a context
937 // then, and this function would correctly return EGL_NO_SURFACE.
938
939 clearError();
940
941 EGLContext ctx = getContext();
942 if (ctx) {
943 egl_context_t const * const c = get_context(ctx);
944 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
945 switch (readdraw) {
946 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800947 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700948 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
949 }
950 }
951 return EGL_NO_SURFACE;
952}
953
954EGLDisplay eglGetCurrentDisplay(void)
955{
956 // could be called before eglInitialize(), but we wouldn't have a context
957 // then, and this function would correctly return EGL_NO_DISPLAY.
958
959 clearError();
960
961 EGLContext ctx = getContext();
962 if (ctx) {
963 egl_context_t const * const c = get_context(ctx);
964 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
965 return c->dpy;
966 }
967 return EGL_NO_DISPLAY;
968}
969
970EGLBoolean eglWaitGL(void)
971{
Mathias Agopian518ec112011-05-13 16:21:08 -0700972 clearError();
973
Mathias Agopianada798b2012-02-13 17:09:30 -0800974 egl_connection_t* const cnx = &gEGLImpl;
975 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800976 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800977
978 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700979}
980
981EGLBoolean eglWaitNative(EGLint engine)
982{
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 clearError();
984
Mathias Agopianada798b2012-02-13 17:09:30 -0800985 egl_connection_t* const cnx = &gEGLImpl;
986 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800987 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800988
989 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700990}
991
992EGLint eglGetError(void)
993{
Mathias Agopianada798b2012-02-13 17:09:30 -0800994 EGLint err = EGL_SUCCESS;
995 egl_connection_t* const cnx = &gEGLImpl;
996 if (cnx->dso) {
997 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700998 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800999 if (err == EGL_SUCCESS) {
1000 err = egl_tls_t::getError();
1001 }
1002 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001003}
1004
Michael Chockc0ec5e22014-01-27 08:14:33 -08001005static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001006 const char* procname) {
1007 const egl_connection_t* cnx = &gEGLImpl;
1008 void* proc = NULL;
1009
Michael Chockc0ec5e22014-01-27 08:14:33 -08001010 proc = dlsym(cnx->libEgl, procname);
1011 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1012
Jesse Hallc07b5202013-07-04 12:08:16 -07001013 proc = dlsym(cnx->libGles2, procname);
1014 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1015
1016 proc = dlsym(cnx->libGles1, procname);
1017 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1018
1019 return NULL;
1020}
1021
Mathias Agopian518ec112011-05-13 16:21:08 -07001022__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1023{
1024 // eglGetProcAddress() could be the very first function called
1025 // in which case we must make sure we've initialized ourselves, this
1026 // happens the first time egl_get_display() is called.
1027
1028 clearError();
1029
1030 if (egl_init_drivers() == EGL_FALSE) {
1031 setError(EGL_BAD_PARAMETER, NULL);
1032 return NULL;
1033 }
1034
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001035 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001036 return NULL;
1037 }
1038
Mathias Agopian518ec112011-05-13 16:21:08 -07001039 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001040 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001041 if (addr) return addr;
1042
Michael Chockc0ec5e22014-01-27 08:14:33 -08001043 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001044 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001045
Mathias Agopian518ec112011-05-13 16:21:08 -07001046 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1047 pthread_mutex_lock(&sExtensionMapMutex);
1048
1049 /*
1050 * Since eglGetProcAddress() is not associated to anything, it needs
1051 * to return a function pointer that "works" regardless of what
1052 * the current context is.
1053 *
1054 * For this reason, we return a "forwarder", a small stub that takes
1055 * care of calling the function associated with the context
1056 * currently bound.
1057 *
1058 * We first look for extensions we've already resolved, if we're seeing
1059 * this extension for the first time, we go through all our
1060 * implementations and call eglGetProcAddress() and record the
1061 * result in the appropriate implementation hooks and return the
1062 * address of the forwarder corresponding to that hook set.
1063 *
1064 */
1065
Mathias Agopian65421432017-03-08 11:49:05 -08001066 const std::string name(procname);
1067
1068 auto& extentionMap = sGLExtentionMap;
1069 auto pos = extentionMap.find(name);
1070 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001071 const int slot = sGLExtentionSlot;
1072
Steve Blocke6f43dd2012-01-06 19:20:56 +00001073 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001074 "no more slots for eglGetProcAddress(\"%s\")",
1075 procname);
1076
1077 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1078 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001079
1080 egl_connection_t* const cnx = &gEGLImpl;
1081 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001082 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001083 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001084 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1085 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001086 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001087 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001088 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001089
Mathias Agopian518ec112011-05-13 16:21:08 -07001090 if (found) {
1091 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001092 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001093 sGLExtentionSlot++;
1094 }
1095 }
1096
1097 pthread_mutex_unlock(&sExtensionMapMutex);
1098 return addr;
1099}
1100
Mathias Agopian65421432017-03-08 11:49:05 -08001101class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001102public:
1103
1104 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001105 static FrameCompletionThread thread;
1106
1107 char name[64];
1108
1109 std::lock_guard<std::mutex> lock(thread.mMutex);
1110 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1111 ATRACE_NAME(name);
1112
1113 thread.mQueue.push_back(sync);
1114 thread.mCondition.notify_one();
1115 thread.mFramesQueued++;
1116 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001117 }
1118
1119private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001120
Mathias Agopian65421432017-03-08 11:49:05 -08001121 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1122 std::thread thread(&FrameCompletionThread::loop, this);
1123 thread.detach();
1124 }
1125
1126#pragma clang diagnostic push
1127#pragma clang diagnostic ignored "-Wmissing-noreturn"
1128 void loop() {
1129 while (true) {
1130 threadLoop();
1131 }
1132 }
1133#pragma clang diagnostic pop
1134
1135 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001136 EGLSyncKHR sync;
1137 uint32_t frameNum;
1138 {
Mathias Agopian65421432017-03-08 11:49:05 -08001139 std::unique_lock<std::mutex> lock(mMutex);
1140 while (mQueue.empty()) {
1141 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001142 }
1143 sync = mQueue[0];
1144 frameNum = mFramesCompleted;
1145 }
1146 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1147 {
Mathias Agopian65421432017-03-08 11:49:05 -08001148 char name[64];
1149 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1150 ATRACE_NAME(name);
1151
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001152 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1153 if (result == EGL_FALSE) {
1154 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1155 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1156 ALOGE("FrameCompletion: timeout waiting for fence");
1157 }
1158 eglDestroySyncKHR(dpy, sync);
1159 }
1160 {
Mathias Agopian65421432017-03-08 11:49:05 -08001161 std::lock_guard<std::mutex> lock(mMutex);
1162 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001163 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001164 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001165 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001166 }
1167
1168 uint32_t mFramesQueued;
1169 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001170 std::deque<EGLSyncKHR> mQueue;
1171 std::condition_variable mCondition;
1172 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001173};
1174
Dan Stozaa894d082015-02-19 15:27:36 -08001175EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1176 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001177{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001178 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001179 clearError();
1180
Jesse Hallb29e5e82012-04-04 16:53:42 -07001181 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001182 if (!dp) return EGL_FALSE;
1183
Jesse Hallb29e5e82012-04-04 16:53:42 -07001184 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001185 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001186 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001187
Mathias Agopian518ec112011-05-13 16:21:08 -07001188 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001189
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001190 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1191 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1192 if (sync != EGL_NO_SYNC_KHR) {
1193 FrameCompletionThread::queueSync(sync);
1194 }
1195 }
1196
Mathias Agopian7db993a2012-03-25 00:49:46 -07001197 if (CC_UNLIKELY(dp->finishOnSwap)) {
1198 uint32_t pixel;
1199 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1200 if (c) {
1201 // glReadPixels() ensures that the frame is complete
1202 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1203 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1204 }
1205 }
1206
Dan Stozaa894d082015-02-19 15:27:36 -08001207 if (n_rects == 0) {
1208 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1209 }
1210
Mathias Agopian65421432017-03-08 11:49:05 -08001211 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001212 for (int r = 0; r < n_rects; ++r) {
1213 int offset = r * 4;
1214 int x = rects[offset];
1215 int y = rects[offset + 1];
1216 int width = rects[offset + 2];
1217 int height = rects[offset + 3];
1218 android_native_rect_t androidRect;
1219 androidRect.left = x;
1220 androidRect.top = y + height;
1221 androidRect.right = x + width;
1222 androidRect.bottom = y;
1223 androidRects.push_back(androidRect);
1224 }
Mathias Agopian65421432017-03-08 11:49:05 -08001225 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001226
1227 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1228 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1229 rects, n_rects);
1230 } else {
1231 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1232 }
1233}
1234
1235EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1236{
1237 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001238}
1239
1240EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1241 NativePixmapType target)
1242{
1243 clearError();
1244
Jesse Hallb29e5e82012-04-04 16:53:42 -07001245 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001246 if (!dp) return EGL_FALSE;
1247
Jesse Hallb29e5e82012-04-04 16:53:42 -07001248 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001249 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001250 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001251
Mathias Agopian518ec112011-05-13 16:21:08 -07001252 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001253 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001254}
1255
1256const char* eglQueryString(EGLDisplay dpy, EGLint name)
1257{
1258 clearError();
1259
Chia-I Wue57d1352016-08-15 16:10:02 +08001260 // Generate an error quietly when client extensions (as defined by
1261 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1262 // validate_display to generate the error as validate_display would log
1263 // the error, which can be misleading.
1264 //
1265 // If we want to support EGL_EXT_client_extensions later, we can return
1266 // the client extension string here instead.
1267 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001268 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001269
Jesse Hallb29e5e82012-04-04 16:53:42 -07001270 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001271 if (!dp) return (const char *) NULL;
1272
1273 switch (name) {
1274 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001275 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001276 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001277 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001279 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001280 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001281 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001282 default:
1283 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001284 }
1285 return setError(EGL_BAD_PARAMETER, (const char *)0);
1286}
1287
Mathias Agopianca088332013-03-28 17:44:13 -07001288EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1289{
1290 clearError();
1291
1292 const egl_display_ptr dp = validate_display(dpy);
1293 if (!dp) return (const char *) NULL;
1294
1295 switch (name) {
1296 case EGL_VENDOR:
1297 return dp->disp.queryString.vendor;
1298 case EGL_VERSION:
1299 return dp->disp.queryString.version;
1300 case EGL_EXTENSIONS:
1301 return dp->disp.queryString.extensions;
1302 case EGL_CLIENT_APIS:
1303 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001304 default:
1305 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001306 }
1307 return setError(EGL_BAD_PARAMETER, (const char *)0);
1308}
Mathias Agopian518ec112011-05-13 16:21:08 -07001309
1310// ----------------------------------------------------------------------------
1311// EGL 1.1
1312// ----------------------------------------------------------------------------
1313
1314EGLBoolean eglSurfaceAttrib(
1315 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1316{
1317 clearError();
1318
Jesse Hallb29e5e82012-04-04 16:53:42 -07001319 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001320 if (!dp) return EGL_FALSE;
1321
Jesse Hallb29e5e82012-04-04 16:53:42 -07001322 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001323 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001324 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001325
Pablo Ceballosc18be292016-05-31 14:55:42 -07001326 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001327
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001328 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001329 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001330 setError(EGL_BAD_SURFACE, EGL_FALSE);
1331 }
Mathias Agopian65421432017-03-08 11:49:05 -08001332 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1333 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001334 }
1335
Pablo Ceballosc18be292016-05-31 14:55:42 -07001336 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001337 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001338 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001339 }
Mathias Agopian65421432017-03-08 11:49:05 -08001340 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1341 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001342 }
1343
Mathias Agopian518ec112011-05-13 16:21:08 -07001344 if (s->cnx->egl.eglSurfaceAttrib) {
1345 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001346 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001347 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001348 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001349}
1350
1351EGLBoolean eglBindTexImage(
1352 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1353{
1354 clearError();
1355
Jesse Hallb29e5e82012-04-04 16:53:42 -07001356 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001357 if (!dp) return EGL_FALSE;
1358
Jesse Hallb29e5e82012-04-04 16:53:42 -07001359 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001360 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001361 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001362
Mathias Agopian518ec112011-05-13 16:21:08 -07001363 egl_surface_t const * const s = get_surface(surface);
1364 if (s->cnx->egl.eglBindTexImage) {
1365 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001366 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001367 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001368 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001369}
1370
1371EGLBoolean eglReleaseTexImage(
1372 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1373{
1374 clearError();
1375
Jesse Hallb29e5e82012-04-04 16:53:42 -07001376 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001377 if (!dp) return EGL_FALSE;
1378
Jesse Hallb29e5e82012-04-04 16:53:42 -07001379 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001380 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001381 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001382
Mathias Agopian518ec112011-05-13 16:21:08 -07001383 egl_surface_t const * const s = get_surface(surface);
1384 if (s->cnx->egl.eglReleaseTexImage) {
1385 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001386 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001387 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001388 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001389}
1390
1391EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1392{
1393 clearError();
1394
Jesse Hallb29e5e82012-04-04 16:53:42 -07001395 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001396 if (!dp) return EGL_FALSE;
1397
1398 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001399 egl_connection_t* const cnx = &gEGLImpl;
1400 if (cnx->dso && cnx->egl.eglSwapInterval) {
1401 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001402 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001403
Mathias Agopian518ec112011-05-13 16:21:08 -07001404 return res;
1405}
1406
1407
1408// ----------------------------------------------------------------------------
1409// EGL 1.2
1410// ----------------------------------------------------------------------------
1411
1412EGLBoolean eglWaitClient(void)
1413{
1414 clearError();
1415
Mathias Agopianada798b2012-02-13 17:09:30 -08001416 egl_connection_t* const cnx = &gEGLImpl;
1417 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001418 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001419
1420 EGLBoolean res;
1421 if (cnx->egl.eglWaitClient) {
1422 res = cnx->egl.eglWaitClient();
1423 } else {
1424 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 }
1426 return res;
1427}
1428
1429EGLBoolean eglBindAPI(EGLenum api)
1430{
1431 clearError();
1432
1433 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001434 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001435 }
1436
1437 // bind this API on all EGLs
1438 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001439 egl_connection_t* const cnx = &gEGLImpl;
1440 if (cnx->dso && cnx->egl.eglBindAPI) {
1441 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001442 }
1443 return res;
1444}
1445
1446EGLenum eglQueryAPI(void)
1447{
1448 clearError();
1449
1450 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001451 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001452 }
1453
Mathias Agopianada798b2012-02-13 17:09:30 -08001454 egl_connection_t* const cnx = &gEGLImpl;
1455 if (cnx->dso && cnx->egl.eglQueryAPI) {
1456 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001457 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001458
Mathias Agopian518ec112011-05-13 16:21:08 -07001459 // or, it can only be OpenGL ES
1460 return EGL_OPENGL_ES_API;
1461}
1462
1463EGLBoolean eglReleaseThread(void)
1464{
1465 clearError();
1466
Mathias Agopianada798b2012-02-13 17:09:30 -08001467 egl_connection_t* const cnx = &gEGLImpl;
1468 if (cnx->dso && cnx->egl.eglReleaseThread) {
1469 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001470 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301471
1472 // If there is context bound to the thread, release it
1473 egl_display_t::loseCurrent(get_context(getContext()));
1474
Mathias Agopian518ec112011-05-13 16:21:08 -07001475 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001476 return EGL_TRUE;
1477}
1478
1479EGLSurface eglCreatePbufferFromClientBuffer(
1480 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1481 EGLConfig config, const EGLint *attrib_list)
1482{
1483 clearError();
1484
Jesse Hallb29e5e82012-04-04 16:53:42 -07001485 egl_connection_t* cnx = NULL;
1486 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1487 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001488 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1489 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001490 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001491 }
1492 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1493}
1494
1495// ----------------------------------------------------------------------------
1496// EGL_EGLEXT_VERSION 3
1497// ----------------------------------------------------------------------------
1498
1499EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1500 const EGLint *attrib_list)
1501{
1502 clearError();
1503
Jesse Hallb29e5e82012-04-04 16:53:42 -07001504 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505 if (!dp) return EGL_FALSE;
1506
Jesse Hallb29e5e82012-04-04 16:53:42 -07001507 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001508 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001509 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001510
1511 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001512 if (s->cnx->egl.eglLockSurfaceKHR) {
1513 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001514 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001515 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001516 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001517}
1518
1519EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1520{
1521 clearError();
1522
Jesse Hallb29e5e82012-04-04 16:53:42 -07001523 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001524 if (!dp) return EGL_FALSE;
1525
Jesse Hallb29e5e82012-04-04 16:53:42 -07001526 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001527 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001528 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001529
1530 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001531 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001532 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001533 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001534 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001535}
1536
1537EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1538 EGLClientBuffer buffer, 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_NO_IMAGE_KHR;
1544
Jesse Hallb29e5e82012-04-04 16:53:42 -07001545 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001546 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001547
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001548 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1549 egl_connection_t* const cnx = &gEGLImpl;
1550 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1551 result = cnx->egl.eglCreateImageKHR(
1552 dp->disp.dpy,
1553 c ? c->context : EGL_NO_CONTEXT,
1554 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001555 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001556 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001557}
1558
1559EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1560{
1561 clearError();
1562
Jesse Hallb29e5e82012-04-04 16:53:42 -07001563 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001564 if (!dp) return EGL_FALSE;
1565
Steven Holte646a5c52012-06-04 20:02:11 -07001566 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001567 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001568 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001569 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001570 }
Steven Holte646a5c52012-06-04 20:02:11 -07001571 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001572}
1573
1574// ----------------------------------------------------------------------------
1575// EGL_EGLEXT_VERSION 5
1576// ----------------------------------------------------------------------------
1577
1578
1579EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1580{
1581 clearError();
1582
Jesse Hallb29e5e82012-04-04 16:53:42 -07001583 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001584 if (!dp) return EGL_NO_SYNC_KHR;
1585
Mathias Agopian518ec112011-05-13 16:21:08 -07001586 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001587 egl_connection_t* const cnx = &gEGLImpl;
1588 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1589 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001590 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001591 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001592}
1593
1594EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1595{
1596 clearError();
1597
Jesse Hallb29e5e82012-04-04 16:53:42 -07001598 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001599 if (!dp) return EGL_FALSE;
1600
Mathias Agopian518ec112011-05-13 16:21:08 -07001601 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001602 egl_connection_t* const cnx = &gEGLImpl;
1603 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1604 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001605 }
1606 return result;
1607}
1608
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001609EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1610 clearError();
1611
1612 const egl_display_ptr dp = validate_display(dpy);
1613 if (!dp) return EGL_FALSE;
1614
1615 EGLBoolean result = EGL_FALSE;
1616 egl_connection_t* const cnx = &gEGLImpl;
1617 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1618 result = cnx->egl.eglSignalSyncKHR(
1619 dp->disp.dpy, sync, mode);
1620 }
1621 return result;
1622}
1623
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001624EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1625 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001626{
1627 clearError();
1628
Jesse Hallb29e5e82012-04-04 16:53:42 -07001629 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001630 if (!dp) return EGL_FALSE;
1631
Mathias Agopian737b8962017-02-24 14:32:05 -08001632 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001633 egl_connection_t* const cnx = &gEGLImpl;
1634 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1635 result = cnx->egl.eglClientWaitSyncKHR(
1636 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001637 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001638 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001639}
1640
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001641EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1642 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001643{
1644 clearError();
1645
Jesse Hallb29e5e82012-04-04 16:53:42 -07001646 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001647 if (!dp) return EGL_FALSE;
1648
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001649 EGLBoolean result = EGL_FALSE;
1650 egl_connection_t* const cnx = &gEGLImpl;
1651 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1652 result = cnx->egl.eglGetSyncAttribKHR(
1653 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001654 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001655 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001656}
1657
Season Li000d88f2015-07-01 11:39:40 -07001658EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1659{
1660 clearError();
1661
1662 const egl_display_ptr dp = validate_display(dpy);
1663 if (!dp) return EGL_NO_STREAM_KHR;
1664
1665 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1666 egl_connection_t* const cnx = &gEGLImpl;
1667 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1668 result = cnx->egl.eglCreateStreamKHR(
1669 dp->disp.dpy, attrib_list);
1670 }
1671 return result;
1672}
1673
1674EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1675{
1676 clearError();
1677
1678 const egl_display_ptr dp = validate_display(dpy);
1679 if (!dp) return EGL_FALSE;
1680
1681 EGLBoolean result = EGL_FALSE;
1682 egl_connection_t* const cnx = &gEGLImpl;
1683 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1684 result = cnx->egl.eglDestroyStreamKHR(
1685 dp->disp.dpy, stream);
1686 }
1687 return result;
1688}
1689
1690EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1691 EGLenum attribute, EGLint value)
1692{
1693 clearError();
1694
1695 const egl_display_ptr dp = validate_display(dpy);
1696 if (!dp) return EGL_FALSE;
1697
1698 EGLBoolean result = EGL_FALSE;
1699 egl_connection_t* const cnx = &gEGLImpl;
1700 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1701 result = cnx->egl.eglStreamAttribKHR(
1702 dp->disp.dpy, stream, attribute, value);
1703 }
1704 return result;
1705}
1706
1707EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1708 EGLenum attribute, EGLint *value)
1709{
1710 clearError();
1711
1712 const egl_display_ptr dp = validate_display(dpy);
1713 if (!dp) return EGL_FALSE;
1714
1715 EGLBoolean result = EGL_FALSE;
1716 egl_connection_t* const cnx = &gEGLImpl;
1717 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1718 result = cnx->egl.eglQueryStreamKHR(
1719 dp->disp.dpy, stream, attribute, value);
1720 }
1721 return result;
1722}
1723
1724EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1725 EGLenum attribute, EGLuint64KHR *value)
1726{
1727 clearError();
1728
1729 const egl_display_ptr dp = validate_display(dpy);
1730 if (!dp) return EGL_FALSE;
1731
1732 EGLBoolean result = EGL_FALSE;
1733 egl_connection_t* const cnx = &gEGLImpl;
1734 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1735 result = cnx->egl.eglQueryStreamu64KHR(
1736 dp->disp.dpy, stream, attribute, value);
1737 }
1738 return result;
1739}
1740
1741EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1742 EGLenum attribute, EGLTimeKHR *value)
1743{
1744 clearError();
1745
1746 const egl_display_ptr dp = validate_display(dpy);
1747 if (!dp) return EGL_FALSE;
1748
1749 EGLBoolean result = EGL_FALSE;
1750 egl_connection_t* const cnx = &gEGLImpl;
1751 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1752 result = cnx->egl.eglQueryStreamTimeKHR(
1753 dp->disp.dpy, stream, attribute, value);
1754 }
1755 return result;
1756}
1757
1758EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1759 EGLStreamKHR stream, const EGLint *attrib_list)
1760{
1761 clearError();
1762
1763 egl_display_ptr dp = validate_display(dpy);
1764 if (!dp) return EGL_NO_SURFACE;
1765
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06001766 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
1767 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
1768 // TODO: Probably need to update EGL_KHR_stream_producer_eglsurface to
1769 // indicate support for EGL_GL_COLORSPACE_KHR.
1770 // now select a corresponding sRGB format if needed
1771 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
1772 ALOGE("error invalid colorspace: %d", colorSpace);
1773 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1774 }
1775
Season Li000d88f2015-07-01 11:39:40 -07001776 egl_connection_t* const cnx = &gEGLImpl;
1777 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1778 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1779 dp->disp.dpy, config, stream, attrib_list);
1780 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06001781 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001782 return s;
1783 }
1784 }
1785 return EGL_NO_SURFACE;
1786}
1787
1788EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1789 EGLStreamKHR stream)
1790{
1791 clearError();
1792
1793 const egl_display_ptr dp = validate_display(dpy);
1794 if (!dp) return EGL_FALSE;
1795
1796 EGLBoolean result = EGL_FALSE;
1797 egl_connection_t* const cnx = &gEGLImpl;
1798 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1799 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1800 dp->disp.dpy, stream);
1801 }
1802 return result;
1803}
1804
1805EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1806 EGLStreamKHR stream)
1807{
1808 clearError();
1809
1810 const egl_display_ptr dp = validate_display(dpy);
1811 if (!dp) return EGL_FALSE;
1812
1813 EGLBoolean result = EGL_FALSE;
1814 egl_connection_t* const cnx = &gEGLImpl;
1815 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1816 result = cnx->egl.eglStreamConsumerAcquireKHR(
1817 dp->disp.dpy, stream);
1818 }
1819 return result;
1820}
1821
1822EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1823 EGLStreamKHR stream)
1824{
1825 clearError();
1826
1827 const egl_display_ptr dp = validate_display(dpy);
1828 if (!dp) return EGL_FALSE;
1829
1830 EGLBoolean result = EGL_FALSE;
1831 egl_connection_t* const cnx = &gEGLImpl;
1832 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1833 result = cnx->egl.eglStreamConsumerReleaseKHR(
1834 dp->disp.dpy, stream);
1835 }
1836 return result;
1837}
1838
1839EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1840 EGLDisplay dpy, EGLStreamKHR stream)
1841{
1842 clearError();
1843
1844 const egl_display_ptr dp = validate_display(dpy);
1845 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1846
1847 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1848 egl_connection_t* const cnx = &gEGLImpl;
1849 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1850 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1851 dp->disp.dpy, stream);
1852 }
1853 return result;
1854}
1855
1856EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1857 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1858{
1859 clearError();
1860
1861 const egl_display_ptr dp = validate_display(dpy);
1862 if (!dp) return EGL_NO_STREAM_KHR;
1863
1864 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1865 egl_connection_t* const cnx = &gEGLImpl;
1866 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1867 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1868 dp->disp.dpy, file_descriptor);
1869 }
1870 return result;
1871}
1872
Mathias Agopian518ec112011-05-13 16:21:08 -07001873// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001874// EGL_EGLEXT_VERSION 15
1875// ----------------------------------------------------------------------------
1876
1877EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1878 clearError();
1879 const egl_display_ptr dp = validate_display(dpy);
1880 if (!dp) return EGL_FALSE;
1881 EGLint result = EGL_FALSE;
1882 egl_connection_t* const cnx = &gEGLImpl;
1883 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1884 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1885 }
1886 return result;
1887}
1888
1889// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001890// ANDROID extensions
1891// ----------------------------------------------------------------------------
1892
Jamie Gennis331841b2012-09-06 14:52:00 -07001893EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1894{
1895 clearError();
1896
1897 const egl_display_ptr dp = validate_display(dpy);
1898 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1899
1900 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1901 egl_connection_t* const cnx = &gEGLImpl;
1902 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1903 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1904 }
1905 return result;
1906}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001907
Andy McFadden72841452013-03-01 16:25:32 -08001908EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1909 EGLnsecsANDROID time)
1910{
1911 clearError();
1912
1913 const egl_display_ptr dp = validate_display(dpy);
1914 if (!dp) {
1915 return EGL_FALSE;
1916 }
1917
1918 SurfaceRef _s(dp.get(), surface);
1919 if (!_s.get()) {
1920 setError(EGL_BAD_SURFACE, EGL_FALSE);
1921 return EGL_FALSE;
1922 }
1923
1924 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08001925 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08001926
1927 return EGL_TRUE;
1928}
1929
Craig Donner60761072017-01-27 12:30:44 -08001930EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1931 clearError();
Craig Donner60761072017-01-27 12:30:44 -08001932 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08001933 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Craig Donner60761072017-01-27 12:30:44 -08001934}
1935
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001936// ----------------------------------------------------------------------------
1937// NVIDIA extensions
1938// ----------------------------------------------------------------------------
1939EGLuint64NV eglGetSystemTimeFrequencyNV()
1940{
1941 clearError();
1942
1943 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001944 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001945 }
1946
1947 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001948 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001949
Mathias Agopianada798b2012-02-13 17:09:30 -08001950 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1951 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001952 }
1953
Mathias Agopian737b8962017-02-24 14:32:05 -08001954 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001955}
1956
1957EGLuint64NV eglGetSystemTimeNV()
1958{
1959 clearError();
1960
1961 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001962 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001963 }
1964
1965 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001966 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001967
Mathias Agopianada798b2012-02-13 17:09:30 -08001968 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1969 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001970 }
1971
Mathias Agopian737b8962017-02-24 14:32:05 -08001972 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001973}
Dan Stozaa894d082015-02-19 15:27:36 -08001974
1975// ----------------------------------------------------------------------------
1976// Partial update extension
1977// ----------------------------------------------------------------------------
1978EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1979 EGLint *rects, EGLint n_rects)
1980{
1981 clearError();
1982
1983 const egl_display_ptr dp = validate_display(dpy);
1984 if (!dp) {
1985 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1986 return EGL_FALSE;
1987 }
1988
1989 SurfaceRef _s(dp.get(), surface);
1990 if (!_s.get()) {
1991 setError(EGL_BAD_SURFACE, EGL_FALSE);
1992 return EGL_FALSE;
1993 }
1994
1995 egl_surface_t const * const s = get_surface(surface);
1996 if (s->cnx->egl.eglSetDamageRegionKHR) {
1997 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1998 rects, n_rects);
1999 }
2000
2001 return EGL_FALSE;
2002}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002003
Brian Anderson1049d1d2016-12-16 17:25:57 -08002004EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2005 EGLuint64KHR *frameId) {
2006 clearError();
2007
2008 const egl_display_ptr dp = validate_display(dpy);
2009 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002010 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002011 }
2012
2013 SurfaceRef _s(dp.get(), surface);
2014 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002015 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002016 }
2017
2018 egl_surface_t const * const s = get_surface(surface);
2019
Mathias Agopian65421432017-03-08 11:49:05 -08002020 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002021 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002022 }
2023
2024 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002025 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002026
Mathias Agopian65421432017-03-08 11:49:05 -08002027 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002028 // This should not happen. Return an error that is not in the spec
2029 // so it's obvious something is very wrong.
2030 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002031 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002032 }
2033
2034 *frameId = nextFrameId;
2035 return EGL_TRUE;
2036}
2037
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002038EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2039 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2040{
2041 clearError();
2042
2043 const egl_display_ptr dp = validate_display(dpy);
2044 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002045 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002046 }
2047
2048 SurfaceRef _s(dp.get(), surface);
2049 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002050 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002051 }
2052
2053 egl_surface_t const * const s = get_surface(surface);
2054
Mathias Agopian65421432017-03-08 11:49:05 -08002055 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002056 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002057 }
2058
2059 nsecs_t* compositeDeadline = nullptr;
2060 nsecs_t* compositeInterval = nullptr;
2061 nsecs_t* compositeToPresentLatency = nullptr;
2062
2063 for (int i = 0; i < numTimestamps; i++) {
2064 switch (names[i]) {
2065 case EGL_COMPOSITE_DEADLINE_ANDROID:
2066 compositeDeadline = &values[i];
2067 break;
2068 case EGL_COMPOSITE_INTERVAL_ANDROID:
2069 compositeInterval = &values[i];
2070 break;
2071 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2072 compositeToPresentLatency = &values[i];
2073 break;
2074 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002075 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002076 }
2077 }
2078
Mathias Agopian65421432017-03-08 11:49:05 -08002079 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002080 compositeDeadline, compositeInterval, compositeToPresentLatency);
2081
2082 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002083 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002084 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002085 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002086 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002087 default:
2088 // This should not happen. Return an error that is not in the spec
2089 // so it's obvious something is very wrong.
2090 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002091 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002092 }
2093}
2094
2095EGLBoolean eglGetCompositorTimingSupportedANDROID(
2096 EGLDisplay dpy, EGLSurface surface, EGLint name)
2097{
2098 clearError();
2099
2100 const egl_display_ptr dp = validate_display(dpy);
2101 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002102 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002103 }
2104
2105 SurfaceRef _s(dp.get(), surface);
2106 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002107 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002108 }
2109
2110 egl_surface_t const * const s = get_surface(surface);
2111
Mathias Agopian65421432017-03-08 11:49:05 -08002112 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002113 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002114 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002115 }
2116
2117 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002118 case EGL_COMPOSITE_DEADLINE_ANDROID:
2119 case EGL_COMPOSITE_INTERVAL_ANDROID:
2120 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2121 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002122 default:
2123 return EGL_FALSE;
2124 }
2125}
2126
Pablo Ceballosc18be292016-05-31 14:55:42 -07002127EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002128 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002129 EGLnsecsANDROID *values)
2130{
2131 clearError();
2132
2133 const egl_display_ptr dp = validate_display(dpy);
2134 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002135 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002136 }
2137
2138 SurfaceRef _s(dp.get(), surface);
2139 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002140 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002141 }
2142
2143 egl_surface_t const * const s = get_surface(surface);
2144
Mathias Agopian65421432017-03-08 11:49:05 -08002145 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002146 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002147 }
2148
Brian Andersondbd0ea82016-07-22 09:38:59 -07002149 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002150 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002151 nsecs_t* latchTime = nullptr;
2152 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002153 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002154 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002155 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002156 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002157 nsecs_t* releaseTime = nullptr;
2158
2159 for (int i = 0; i < numTimestamps; i++) {
2160 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002161 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2162 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002163 break;
2164 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2165 acquireTime = &values[i];
2166 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002167 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2168 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002169 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002170 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2171 firstRefreshStartTime = &values[i];
2172 break;
2173 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2174 lastRefreshStartTime = &values[i];
2175 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002176 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2177 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002178 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002179 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2180 displayPresentTime = &values[i];
2181 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002182 case EGL_DEQUEUE_READY_TIME_ANDROID:
2183 dequeueReadyTime = &values[i];
2184 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002185 case EGL_READS_DONE_TIME_ANDROID:
2186 releaseTime = &values[i];
2187 break;
2188 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002189 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002190 }
2191 }
2192
Mathias Agopian65421432017-03-08 11:49:05 -08002193 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002194 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002195 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002196 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002197
Brian Anderson069b3652016-07-22 10:32:47 -07002198 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002199 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002200 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002201 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002202 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002203 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002204 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002205 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002206 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2207 default:
2208 // This should not happen. Return an error that is not in the spec
2209 // so it's obvious something is very wrong.
2210 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2211 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002212 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002213}
2214
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002215EGLBoolean eglGetFrameTimestampSupportedANDROID(
2216 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002217{
2218 clearError();
2219
2220 const egl_display_ptr dp = validate_display(dpy);
2221 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002222 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002223 }
2224
2225 SurfaceRef _s(dp.get(), surface);
2226 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002227 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002228 }
2229
2230 egl_surface_t const * const s = get_surface(surface);
2231
Mathias Agopian65421432017-03-08 11:49:05 -08002232 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002233 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002234 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002235 }
2236
2237 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002238 case EGL_COMPOSITE_DEADLINE_ANDROID:
2239 case EGL_COMPOSITE_INTERVAL_ANDROID:
2240 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002241 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002242 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002243 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2244 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2245 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002246 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002247 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002248 case EGL_READS_DONE_TIME_ANDROID:
2249 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002250 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2251 int value = 0;
2252 window->query(window,
2253 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2254 return value == 0 ? EGL_FALSE : EGL_TRUE;
2255 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002256 default:
2257 return EGL_FALSE;
2258 }
2259}