blob: 10f4e6613a484c25c8ff320d8cf4feae8dbb4e77 [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
Mathias Agopian311b4792017-02-28 15:00:49 -080082char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070083 "EGL_KHR_get_all_proc_addresses "
84 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080085 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080086 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080087 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070088 "EGL_ANDROID_get_frame_timestamps "
Jesse Hall21558da2013-08-06 15:31:22 -070089 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080090
91char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070092 "EGL_KHR_image " // mandatory
93 "EGL_KHR_image_base " // mandatory
94 "EGL_KHR_image_pixmap "
95 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -070096 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070098 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070099 "EGL_KHR_gl_texture_cubemap_image "
100 "EGL_KHR_gl_renderbuffer_image "
101 "EGL_KHR_reusable_sync "
102 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700103 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700104 "EGL_KHR_config_attribs "
105 "EGL_KHR_surfaceless_context "
106 "EGL_KHR_stream "
107 "EGL_KHR_stream_fifo "
108 "EGL_KHR_stream_producer_eglsurface "
109 "EGL_KHR_stream_consumer_gltexture "
110 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700111 "EGL_EXT_create_context_robustness "
112 "EGL_NV_system_time "
113 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700114 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700115 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800116 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700117 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800118 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700119 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700120 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700121 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700122 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000123 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800124 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700125 ;
126
127// extensions not exposed to applications but used by the ANDROID system
128// "EGL_ANDROID_blob_cache " // strongly recommended
129// "EGL_IMG_hibernate_process " // optional
130// "EGL_ANDROID_native_fence_sync " // strongly recommended
131// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700132// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700133
134/*
135 * EGL Extensions entry-points exposed to 3rd party applications
136 * (keep in sync with gExtensionString above)
137 *
138 */
139static const extention_map_t sExtensionMap[] = {
140 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700141 { "eglLockSurfaceKHR",
142 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
143 { "eglUnlockSurfaceKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700145
146 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700147 { "eglCreateImageKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
149 { "eglDestroyImageKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700151
152 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
153 { "eglCreateSyncKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
155 { "eglDestroySyncKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
157 { "eglClientWaitSyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
159 { "eglSignalSyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
161 { "eglGetSyncAttribKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
163
164 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800165 { "eglGetSystemTimeFrequencyNV",
166 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
167 { "eglGetSystemTimeNV",
168 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700169
Mathias Agopian2bb71682013-03-27 17:32:41 -0700170 // EGL_KHR_wait_sync
171 { "eglWaitSyncKHR",
172 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700173
174 // EGL_ANDROID_presentation_time
175 { "eglPresentationTimeANDROID",
176 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800177
178 // EGL_KHR_swap_buffers_with_damage
179 { "eglSwapBuffersWithDamageKHR",
180 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
181
Craig Donner60761072017-01-27 12:30:44 -0800182 // EGL_ANDROID_get_native_client_buffer
183 { "eglGetNativeClientBufferANDROID",
184 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
185
Dan Stozaa894d082015-02-19 15:27:36 -0800186 // EGL_KHR_partial_update
187 { "eglSetDamageRegionKHR",
188 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700189
190 { "eglCreateStreamKHR",
191 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
192 { "eglDestroyStreamKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
194 { "eglStreamAttribKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
196 { "eglQueryStreamKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
198 { "eglQueryStreamu64KHR",
199 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
200 { "eglQueryStreamTimeKHR",
201 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
202 { "eglCreateStreamProducerSurfaceKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
204 { "eglStreamConsumerGLTextureExternalKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
206 { "eglStreamConsumerAcquireKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
208 { "eglStreamConsumerReleaseKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
210 { "eglGetStreamFileDescriptorKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
212 { "eglCreateStreamFromFileDescriptorKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700214
215 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800216 { "eglGetNextFrameIdANDROID",
217 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800218 { "eglGetCompositorTimingANDROID",
219 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
220 { "eglGetCompositorTimingSupportedANDROID",
221 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700222 { "eglGetFrameTimestampsANDROID",
223 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800224 { "eglGetFrameTimestampSupportedANDROID",
225 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700226
227 // EGL_ANDROID_native_fence_sync
228 { "eglDupNativeFenceFDANDROID",
229 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700230};
231
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700232/*
233 * These extensions entry-points should not be exposed to applications.
234 * They're used internally by the Android EGL layer.
235 */
236#define FILTER_EXTENSIONS(procname) \
237 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
238 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700239 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700240
241
242
Mathias Agopian518ec112011-05-13 16:21:08 -0700243// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800244static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
245
Mathias Agopian518ec112011-05-13 16:21:08 -0700246static int sGLExtentionSlot = 0;
247static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
248
249static void(*findProcAddress(const char* name,
250 const extention_map_t* map, size_t n))() {
251 for (uint32_t i=0 ; i<n ; i++) {
252 if (!strcmp(name, map[i].name)) {
253 return map[i].address;
254 }
255 }
256 return NULL;
257}
258
259// ----------------------------------------------------------------------------
260
Mathias Agopian518ec112011-05-13 16:21:08 -0700261extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
262extern EGLBoolean egl_init_drivers();
263extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700264extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700265
Mathias Agopian518ec112011-05-13 16:21:08 -0700266} // namespace android;
267
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700268
Mathias Agopian518ec112011-05-13 16:21:08 -0700269// ----------------------------------------------------------------------------
270
271static inline void clearError() { egl_tls_t::clearError(); }
272static inline EGLContext getContext() { return egl_tls_t::getContext(); }
273
274// ----------------------------------------------------------------------------
275
276EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
277{
Jesse Hall1508ae62017-01-19 17:43:26 -0800278 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700279 clearError();
280
Dan Stozac3289c42014-01-17 11:38:34 -0800281 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700282 if (index >= NUM_DISPLAYS) {
283 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
284 }
285
286 if (egl_init_drivers() == EGL_FALSE) {
287 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
288 }
289
290 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
291 return dpy;
292}
293
294// ----------------------------------------------------------------------------
295// Initialization
296// ----------------------------------------------------------------------------
297
298EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
299{
300 clearError();
301
Jesse Hallb29e5e82012-04-04 16:53:42 -0700302 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800303 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700304
305 EGLBoolean res = dp->initialize(major, minor);
306
307 return res;
308}
309
310EGLBoolean eglTerminate(EGLDisplay dpy)
311{
312 // NOTE: don't unload the drivers b/c some APIs can be called
313 // after eglTerminate() has been called. eglTerminate() only
314 // terminates an EGLDisplay, not a EGL itself.
315
316 clearError();
317
Jesse Hallb29e5e82012-04-04 16:53:42 -0700318 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800319 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700320
321 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800322
Mathias Agopian518ec112011-05-13 16:21:08 -0700323 return res;
324}
325
326// ----------------------------------------------------------------------------
327// configuration
328// ----------------------------------------------------------------------------
329
330EGLBoolean eglGetConfigs( EGLDisplay dpy,
331 EGLConfig *configs,
332 EGLint config_size, EGLint *num_config)
333{
334 clearError();
335
Jesse Hallb29e5e82012-04-04 16:53:42 -0700336 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700337 if (!dp) return EGL_FALSE;
338
Mathias Agopian7773c432012-02-13 20:06:08 -0800339 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800340 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700341 }
342
Mathias Agopian7773c432012-02-13 20:06:08 -0800343 EGLBoolean res = EGL_FALSE;
344 *num_config = 0;
345
346 egl_connection_t* const cnx = &gEGLImpl;
347 if (cnx->dso) {
348 res = cnx->egl.eglGetConfigs(
349 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700350 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800351
352 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700353}
354
355EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
356 EGLConfig *configs, EGLint config_size,
357 EGLint *num_config)
358{
359 clearError();
360
Jesse Hallb29e5e82012-04-04 16:53:42 -0700361 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700362 if (!dp) return EGL_FALSE;
363
364 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800365 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700366 }
367
Mathias Agopian518ec112011-05-13 16:21:08 -0700368 EGLBoolean res = EGL_FALSE;
369 *num_config = 0;
370
Mathias Agopianada798b2012-02-13 17:09:30 -0800371 egl_connection_t* const cnx = &gEGLImpl;
372 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700373 if (attrib_list) {
374 char value[PROPERTY_VALUE_MAX];
375 property_get("debug.egl.force_msaa", value, "false");
376
377 if (!strcmp(value, "true")) {
378 size_t attribCount = 0;
379 EGLint attrib = attrib_list[0];
380
381 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700382 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700383 const EGLint *attribRendererable = NULL;
384 const EGLint *attribCaveat = NULL;
385
386 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700387 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700388 while (attrib != EGL_NONE) {
389 attrib = attrib_list[attribCount];
390 switch (attrib) {
391 case EGL_RENDERABLE_TYPE:
392 attribRendererable = &attrib_list[attribCount];
393 break;
394 case EGL_CONFIG_CAVEAT:
395 attribCaveat = &attrib_list[attribCount];
396 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800397 default:
398 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700399 }
400 attribCount++;
401 }
402
403 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
404 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800405
Romain Guy1cffc802012-10-15 18:13:05 -0700406 // Insert 2 extra attributes to force-enable MSAA 4x
407 EGLint aaAttribs[attribCount + 4];
408 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
409 aaAttribs[1] = 1;
410 aaAttribs[2] = EGL_SAMPLES;
411 aaAttribs[3] = 4;
412
413 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
414
415 EGLint numConfigAA;
416 EGLBoolean resAA = cnx->egl.eglChooseConfig(
417 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
418
419 if (resAA == EGL_TRUE && numConfigAA > 0) {
420 ALOGD("Enabling MSAA 4x");
421 *num_config = numConfigAA;
422 return resAA;
423 }
424 }
425 }
426 }
427
Mathias Agopian7773c432012-02-13 20:06:08 -0800428 res = cnx->egl.eglChooseConfig(
429 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700430 }
431 return res;
432}
433
434EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
435 EGLint attribute, EGLint *value)
436{
437 clearError();
438
Jesse Hallb29e5e82012-04-04 16:53:42 -0700439 egl_connection_t* cnx = NULL;
440 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
441 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800442
Mathias Agopian518ec112011-05-13 16:21:08 -0700443 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800444 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700445}
446
447// ----------------------------------------------------------------------------
448// surfaces
449// ----------------------------------------------------------------------------
450
Jesse Hallc2e41222013-08-08 13:40:22 -0700451// Turn linear formats into corresponding sRGB formats when colorspace is
452// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
453// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800454// the modification isn't possible, the original dataSpace is returned.
455static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
456 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700457 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800458 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700459 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800460 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700461 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800462 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700463}
464
Mathias Agopian518ec112011-05-13 16:21:08 -0700465EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
466 NativeWindowType window,
467 const EGLint *attrib_list)
468{
469 clearError();
470
Jesse Hallb29e5e82012-04-04 16:53:42 -0700471 egl_connection_t* cnx = NULL;
472 egl_display_ptr dp = validate_display_connection(dpy, cnx);
473 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800474 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700475
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800476 if (!window) {
477 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
478 }
479
480 int value = 0;
481 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
482 if (!value) {
483 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
484 }
485
Andy McFaddend566ce32014-01-07 15:54:17 -0800486 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800487 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800488 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
489 "failed (%#x) (already connected to another API?)",
490 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700491 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700492 }
493
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700494 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700495 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
496 // of our native format. So if sRGB gamma is requested, we have to
497 // modify the EGLconfig's format before setting the native window's
498 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800499
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700500 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
501 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
502 &componentType);
503
Mathias Agopian95921562017-02-24 14:31:31 -0800504 EGLint format;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800505 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700506 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700507 EGLint r, g, b;
508 r = g = b = 0;
509 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
510 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
511 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700512 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700513 EGLint colorDepth = r + g + b;
514
515 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700516 if (colorDepth <= 16) {
517 format = HAL_PIXEL_FORMAT_RGB_565;
518 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700519 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
520 if (colorDepth > 24) {
521 format = HAL_PIXEL_FORMAT_RGBA_1010102;
522 } else {
523 format = HAL_PIXEL_FORMAT_RGBX_8888;
524 }
525 } else {
526 format = HAL_PIXEL_FORMAT_RGBA_FP16;
527 }
528 }
529 } else {
530 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
531 if (colorDepth > 24) {
532 format = HAL_PIXEL_FORMAT_RGBA_1010102;
533 } else {
534 format = HAL_PIXEL_FORMAT_RGBA_8888;
535 }
536 } else {
537 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700538 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700539 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700540
541 // now select a corresponding sRGB format if needed
542 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
543 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
544 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530545 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700546 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700547 }
548 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800549
Jesse Hallc2e41222013-08-08 13:40:22 -0700550 if (format != 0) {
551 int err = native_window_set_buffers_format(window, format);
552 if (err != 0) {
553 ALOGE("error setting native window pixel format: %s (%d)",
554 strerror(-err), err);
555 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
556 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
557 }
558 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700559
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800560 if (dataSpace != 0) {
561 int err = native_window_set_buffers_data_space(window, dataSpace);
562 if (err != 0) {
563 ALOGE("error setting native window pixel dataSpace: %s (%d)",
564 strerror(-err), err);
565 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
566 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
567 }
568 }
569
Jamie Gennis59769462011-11-19 18:04:43 -0800570 // the EGL spec requires that a new EGLSurface default to swap interval
571 // 1, so explicitly set that on the window here.
572 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
573 anw->setSwapInterval(anw, 1);
574
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800576 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700577 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700578 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
579 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 return s;
581 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700582
583 // EGLSurface creation failed
584 native_window_set_buffers_format(window, 0);
585 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700586 }
587 return EGL_NO_SURFACE;
588}
589
590EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
591 NativePixmapType pixmap,
592 const EGLint *attrib_list)
593{
594 clearError();
595
Jesse Hallb29e5e82012-04-04 16:53:42 -0700596 egl_connection_t* cnx = NULL;
597 egl_display_ptr dp = validate_display_connection(dpy, cnx);
598 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700599 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800600 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700601 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700602 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
603 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700604 return s;
605 }
606 }
607 return EGL_NO_SURFACE;
608}
609
610EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
611 const EGLint *attrib_list)
612{
613 clearError();
614
Jesse Hallb29e5e82012-04-04 16:53:42 -0700615 egl_connection_t* cnx = NULL;
616 egl_display_ptr dp = validate_display_connection(dpy, cnx);
617 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700618 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800619 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700620 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700621 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
622 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700623 return s;
624 }
625 }
626 return EGL_NO_SURFACE;
627}
Jesse Hall47743382013-02-08 11:13:46 -0800628
Mathias Agopian518ec112011-05-13 16:21:08 -0700629EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
630{
631 clearError();
632
Jesse Hallb29e5e82012-04-04 16:53:42 -0700633 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700634 if (!dp) return EGL_FALSE;
635
Jesse Hallb29e5e82012-04-04 16:53:42 -0700636 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700637 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800638 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700639
640 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800641 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700642 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700643 _s.terminate();
644 }
645 return result;
646}
647
648EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
649 EGLint attribute, EGLint *value)
650{
651 clearError();
652
Jesse Hallb29e5e82012-04-04 16:53:42 -0700653 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700654 if (!dp) return EGL_FALSE;
655
Jesse Hallb29e5e82012-04-04 16:53:42 -0700656 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700657 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800658 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700659
Mathias Agopian518ec112011-05-13 16:21:08 -0700660 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800661 return s->cnx->egl.eglQuerySurface(
662 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700663}
664
Jamie Gennise8696a42012-01-15 18:54:57 -0800665void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800666 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800667 clearError();
668
Jesse Hallb29e5e82012-04-04 16:53:42 -0700669 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800670 if (!dp) {
671 return;
672 }
673
Jesse Hallb29e5e82012-04-04 16:53:42 -0700674 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800675 if (!_s.get()) {
676 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800677 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800678}
679
Mathias Agopian518ec112011-05-13 16:21:08 -0700680// ----------------------------------------------------------------------------
681// Contexts
682// ----------------------------------------------------------------------------
683
684EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
685 EGLContext share_list, const EGLint *attrib_list)
686{
687 clearError();
688
Jesse Hallb29e5e82012-04-04 16:53:42 -0700689 egl_connection_t* cnx = NULL;
690 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700691 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700692 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700693 if (!ContextRef(dp.get(), share_list).get()) {
694 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
695 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700696 egl_context_t* const c = get_context(share_list);
697 share_list = c->context;
698 }
699 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800700 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700701 if (context != EGL_NO_CONTEXT) {
702 // figure out if it's a GLESv1 or GLESv2
703 int version = 0;
704 if (attrib_list) {
705 while (*attrib_list != EGL_NONE) {
706 GLint attr = *attrib_list++;
707 GLint value = *attrib_list++;
708 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
709 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800710 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800711 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800712 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 }
714 }
715 };
716 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700717 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
718 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700719 return c;
720 }
721 }
722 return EGL_NO_CONTEXT;
723}
724
725EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
726{
727 clearError();
728
Jesse Hallb29e5e82012-04-04 16:53:42 -0700729 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700730 if (!dp)
731 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700732
Jesse Hallb29e5e82012-04-04 16:53:42 -0700733 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700734 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800735 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800736
Mathias Agopian518ec112011-05-13 16:21:08 -0700737 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800738 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700739 if (result == EGL_TRUE) {
740 _c.terminate();
741 }
742 return result;
743}
744
Mathias Agopian518ec112011-05-13 16:21:08 -0700745EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
746 EGLSurface read, EGLContext ctx)
747{
748 clearError();
749
Jesse Hallb29e5e82012-04-04 16:53:42 -0700750 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800751 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700752
Mathias Agopian5b287a62011-05-16 18:58:55 -0700753 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
754 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
755 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
757 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800758 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700759 }
760
761 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700762 ContextRef _c(dp.get(), ctx);
763 SurfaceRef _d(dp.get(), draw);
764 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700765
766 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700767 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700768 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800769 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700770 }
771
772 // these are the underlying implementation's object
773 EGLContext impl_ctx = EGL_NO_CONTEXT;
774 EGLSurface impl_draw = EGL_NO_SURFACE;
775 EGLSurface impl_read = EGL_NO_SURFACE;
776
777 // these are our objects structs passed in
778 egl_context_t * c = NULL;
779 egl_surface_t const * d = NULL;
780 egl_surface_t const * r = NULL;
781
782 // these are the current objects structs
783 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800784
Mathias Agopian518ec112011-05-13 16:21:08 -0700785 if (ctx != EGL_NO_CONTEXT) {
786 c = get_context(ctx);
787 impl_ctx = c->context;
788 } else {
789 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700790 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
791 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800792 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700793 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700794 if (cur_c == NULL) {
795 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700796 // not an error, there is just no current context.
797 return EGL_TRUE;
798 }
799 }
800
801 // retrieve the underlying implementation's draw EGLSurface
802 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800803 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700804 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700805 impl_draw = d->surface;
806 }
807
808 // retrieve the underlying implementation's read EGLSurface
809 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800810 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700812 impl_read = r->surface;
813 }
814
Mathias Agopian518ec112011-05-13 16:21:08 -0700815
Jesse Hallb29e5e82012-04-04 16:53:42 -0700816 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800817 draw, read, ctx,
818 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700819
820 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800821 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700822 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
823 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700824 _c.acquire();
825 _r.acquire();
826 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 } else {
828 setGLHooksThreadSpecific(&gHooksNoContext);
829 egl_tls_t::setContext(EGL_NO_CONTEXT);
830 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700831 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -0600832 // Force return to current context for drivers that cannot handle errors
833 EGLBoolean restore_result = EGL_FALSE;
834
835 // get a reference to the old current objects
836 ContextRef _c2(dp.get(), cur_c);
837 SurfaceRef _d2(dp.get(), cur_c->draw);
838 SurfaceRef _r2(dp.get(), cur_c->read);
839
840 if (cur_c == NULL) {
841 restore_result = dp->makeCurrent(c, cur_c,
842 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT,
843 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
844 } else {
845 c = cur_c;
846 impl_ctx = c->context;
847 impl_draw = EGL_NO_SURFACE;
848 if (cur_c->draw != EGL_NO_SURFACE) {
849 d = get_surface(cur_c->draw);
850 impl_draw = d->surface;
851 }
852 impl_read = EGL_NO_SURFACE;
853 if (cur_c->read != EGL_NO_SURFACE) {
854 r = get_surface(cur_c->read);
855 impl_read = r->surface;
856 }
857 restore_result = dp->makeCurrent(c, cur_c,
858 cur_c->draw, cur_c->read, cur_c->context,
859 impl_draw, impl_read, impl_ctx);
860 }
861 if (restore_result == EGL_TRUE) {
862 _c2.acquire();
863 _r2.acquire();
864 _d2.acquire();
865 } else {
866 ALOGE("Could not restore original EGL context");
867 }
Steve Blocke6f43dd2012-01-06 19:20:56 +0000868 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700869 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -0800870 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700871 }
872 return result;
873}
874
875
876EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
877 EGLint attribute, EGLint *value)
878{
879 clearError();
880
Jesse Hallb29e5e82012-04-04 16:53:42 -0700881 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700882 if (!dp) return EGL_FALSE;
883
Jesse Hallb29e5e82012-04-04 16:53:42 -0700884 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -0800885 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700886
Mathias Agopian518ec112011-05-13 16:21:08 -0700887 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800888 return c->cnx->egl.eglQueryContext(
889 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700890
Mathias Agopian518ec112011-05-13 16:21:08 -0700891}
892
893EGLContext eglGetCurrentContext(void)
894{
895 // could be called before eglInitialize(), but we wouldn't have a context
896 // then, and this function would correctly return EGL_NO_CONTEXT.
897
898 clearError();
899
900 EGLContext ctx = getContext();
901 return ctx;
902}
903
904EGLSurface eglGetCurrentSurface(EGLint readdraw)
905{
906 // could be called before eglInitialize(), but we wouldn't have a context
907 // then, and this function would correctly return EGL_NO_SURFACE.
908
909 clearError();
910
911 EGLContext ctx = getContext();
912 if (ctx) {
913 egl_context_t const * const c = get_context(ctx);
914 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
915 switch (readdraw) {
916 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800917 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700918 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
919 }
920 }
921 return EGL_NO_SURFACE;
922}
923
924EGLDisplay eglGetCurrentDisplay(void)
925{
926 // could be called before eglInitialize(), but we wouldn't have a context
927 // then, and this function would correctly return EGL_NO_DISPLAY.
928
929 clearError();
930
931 EGLContext ctx = getContext();
932 if (ctx) {
933 egl_context_t const * const c = get_context(ctx);
934 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
935 return c->dpy;
936 }
937 return EGL_NO_DISPLAY;
938}
939
940EGLBoolean eglWaitGL(void)
941{
Mathias Agopian518ec112011-05-13 16:21:08 -0700942 clearError();
943
Mathias Agopianada798b2012-02-13 17:09:30 -0800944 egl_connection_t* const cnx = &gEGLImpl;
945 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800946 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800947
948 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700949}
950
951EGLBoolean eglWaitNative(EGLint engine)
952{
Mathias Agopian518ec112011-05-13 16:21:08 -0700953 clearError();
954
Mathias Agopianada798b2012-02-13 17:09:30 -0800955 egl_connection_t* const cnx = &gEGLImpl;
956 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800957 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800958
959 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700960}
961
962EGLint eglGetError(void)
963{
Mathias Agopianada798b2012-02-13 17:09:30 -0800964 EGLint err = EGL_SUCCESS;
965 egl_connection_t* const cnx = &gEGLImpl;
966 if (cnx->dso) {
967 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700968 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800969 if (err == EGL_SUCCESS) {
970 err = egl_tls_t::getError();
971 }
972 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700973}
974
Michael Chockc0ec5e22014-01-27 08:14:33 -0800975static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700976 const char* procname) {
977 const egl_connection_t* cnx = &gEGLImpl;
978 void* proc = NULL;
979
Michael Chockc0ec5e22014-01-27 08:14:33 -0800980 proc = dlsym(cnx->libEgl, procname);
981 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
982
Jesse Hallc07b5202013-07-04 12:08:16 -0700983 proc = dlsym(cnx->libGles2, procname);
984 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
985
986 proc = dlsym(cnx->libGles1, procname);
987 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
988
989 return NULL;
990}
991
Mathias Agopian518ec112011-05-13 16:21:08 -0700992__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
993{
994 // eglGetProcAddress() could be the very first function called
995 // in which case we must make sure we've initialized ourselves, this
996 // happens the first time egl_get_display() is called.
997
998 clearError();
999
1000 if (egl_init_drivers() == EGL_FALSE) {
1001 setError(EGL_BAD_PARAMETER, NULL);
1002 return NULL;
1003 }
1004
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001005 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001006 return NULL;
1007 }
1008
Mathias Agopian518ec112011-05-13 16:21:08 -07001009 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001010 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001011 if (addr) return addr;
1012
Michael Chockc0ec5e22014-01-27 08:14:33 -08001013 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001014 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001015
Mathias Agopian518ec112011-05-13 16:21:08 -07001016 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1017 pthread_mutex_lock(&sExtensionMapMutex);
1018
1019 /*
1020 * Since eglGetProcAddress() is not associated to anything, it needs
1021 * to return a function pointer that "works" regardless of what
1022 * the current context is.
1023 *
1024 * For this reason, we return a "forwarder", a small stub that takes
1025 * care of calling the function associated with the context
1026 * currently bound.
1027 *
1028 * We first look for extensions we've already resolved, if we're seeing
1029 * this extension for the first time, we go through all our
1030 * implementations and call eglGetProcAddress() and record the
1031 * result in the appropriate implementation hooks and return the
1032 * address of the forwarder corresponding to that hook set.
1033 *
1034 */
1035
Mathias Agopian65421432017-03-08 11:49:05 -08001036 const std::string name(procname);
1037
1038 auto& extentionMap = sGLExtentionMap;
1039 auto pos = extentionMap.find(name);
1040 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001041 const int slot = sGLExtentionSlot;
1042
Steve Blocke6f43dd2012-01-06 19:20:56 +00001043 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001044 "no more slots for eglGetProcAddress(\"%s\")",
1045 procname);
1046
1047 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1048 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001049
1050 egl_connection_t* const cnx = &gEGLImpl;
1051 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001052 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001053 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001054 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1055 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001056 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001057 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001058 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001059
Mathias Agopian518ec112011-05-13 16:21:08 -07001060 if (found) {
1061 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001062 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001063 sGLExtentionSlot++;
1064 }
1065 }
1066
1067 pthread_mutex_unlock(&sExtensionMapMutex);
1068 return addr;
1069}
1070
Mathias Agopian65421432017-03-08 11:49:05 -08001071class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001072public:
1073
1074 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001075 static FrameCompletionThread thread;
1076
1077 char name[64];
1078
1079 std::lock_guard<std::mutex> lock(thread.mMutex);
1080 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1081 ATRACE_NAME(name);
1082
1083 thread.mQueue.push_back(sync);
1084 thread.mCondition.notify_one();
1085 thread.mFramesQueued++;
1086 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001087 }
1088
1089private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001090
Mathias Agopian65421432017-03-08 11:49:05 -08001091 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1092 std::thread thread(&FrameCompletionThread::loop, this);
1093 thread.detach();
1094 }
1095
1096#pragma clang diagnostic push
1097#pragma clang diagnostic ignored "-Wmissing-noreturn"
1098 void loop() {
1099 while (true) {
1100 threadLoop();
1101 }
1102 }
1103#pragma clang diagnostic pop
1104
1105 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001106 EGLSyncKHR sync;
1107 uint32_t frameNum;
1108 {
Mathias Agopian65421432017-03-08 11:49:05 -08001109 std::unique_lock<std::mutex> lock(mMutex);
1110 while (mQueue.empty()) {
1111 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001112 }
1113 sync = mQueue[0];
1114 frameNum = mFramesCompleted;
1115 }
1116 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1117 {
Mathias Agopian65421432017-03-08 11:49:05 -08001118 char name[64];
1119 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1120 ATRACE_NAME(name);
1121
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001122 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1123 if (result == EGL_FALSE) {
1124 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1125 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1126 ALOGE("FrameCompletion: timeout waiting for fence");
1127 }
1128 eglDestroySyncKHR(dpy, sync);
1129 }
1130 {
Mathias Agopian65421432017-03-08 11:49:05 -08001131 std::lock_guard<std::mutex> lock(mMutex);
1132 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001133 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001134 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001135 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001136 }
1137
1138 uint32_t mFramesQueued;
1139 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001140 std::deque<EGLSyncKHR> mQueue;
1141 std::condition_variable mCondition;
1142 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001143};
1144
Dan Stozaa894d082015-02-19 15:27:36 -08001145EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1146 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001147{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001148 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001149 clearError();
1150
Jesse Hallb29e5e82012-04-04 16:53:42 -07001151 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001152 if (!dp) return EGL_FALSE;
1153
Jesse Hallb29e5e82012-04-04 16:53:42 -07001154 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001155 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001156 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001157
Mathias Agopian518ec112011-05-13 16:21:08 -07001158 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001159
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001160 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1161 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1162 if (sync != EGL_NO_SYNC_KHR) {
1163 FrameCompletionThread::queueSync(sync);
1164 }
1165 }
1166
Mathias Agopian7db993a2012-03-25 00:49:46 -07001167 if (CC_UNLIKELY(dp->finishOnSwap)) {
1168 uint32_t pixel;
1169 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1170 if (c) {
1171 // glReadPixels() ensures that the frame is complete
1172 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1173 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1174 }
1175 }
1176
Dan Stozaa894d082015-02-19 15:27:36 -08001177 if (n_rects == 0) {
1178 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1179 }
1180
Mathias Agopian65421432017-03-08 11:49:05 -08001181 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001182 for (int r = 0; r < n_rects; ++r) {
1183 int offset = r * 4;
1184 int x = rects[offset];
1185 int y = rects[offset + 1];
1186 int width = rects[offset + 2];
1187 int height = rects[offset + 3];
1188 android_native_rect_t androidRect;
1189 androidRect.left = x;
1190 androidRect.top = y + height;
1191 androidRect.right = x + width;
1192 androidRect.bottom = y;
1193 androidRects.push_back(androidRect);
1194 }
Mathias Agopian65421432017-03-08 11:49:05 -08001195 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001196
1197 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1198 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1199 rects, n_rects);
1200 } else {
1201 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1202 }
1203}
1204
1205EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1206{
1207 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001208}
1209
1210EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1211 NativePixmapType target)
1212{
1213 clearError();
1214
Jesse Hallb29e5e82012-04-04 16:53:42 -07001215 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001216 if (!dp) return EGL_FALSE;
1217
Jesse Hallb29e5e82012-04-04 16:53:42 -07001218 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001219 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001220 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001221
Mathias Agopian518ec112011-05-13 16:21:08 -07001222 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001223 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001224}
1225
1226const char* eglQueryString(EGLDisplay dpy, EGLint name)
1227{
1228 clearError();
1229
Chia-I Wue57d1352016-08-15 16:10:02 +08001230 // Generate an error quietly when client extensions (as defined by
1231 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1232 // validate_display to generate the error as validate_display would log
1233 // the error, which can be misleading.
1234 //
1235 // If we want to support EGL_EXT_client_extensions later, we can return
1236 // the client extension string here instead.
1237 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001238 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001239
Jesse Hallb29e5e82012-04-04 16:53:42 -07001240 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001241 if (!dp) return (const char *) NULL;
1242
1243 switch (name) {
1244 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001245 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001246 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001247 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001248 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001249 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001250 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001251 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001252 default:
1253 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001254 }
1255 return setError(EGL_BAD_PARAMETER, (const char *)0);
1256}
1257
Mathias Agopianca088332013-03-28 17:44:13 -07001258EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1259{
1260 clearError();
1261
1262 const egl_display_ptr dp = validate_display(dpy);
1263 if (!dp) return (const char *) NULL;
1264
1265 switch (name) {
1266 case EGL_VENDOR:
1267 return dp->disp.queryString.vendor;
1268 case EGL_VERSION:
1269 return dp->disp.queryString.version;
1270 case EGL_EXTENSIONS:
1271 return dp->disp.queryString.extensions;
1272 case EGL_CLIENT_APIS:
1273 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001274 default:
1275 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001276 }
1277 return setError(EGL_BAD_PARAMETER, (const char *)0);
1278}
Mathias Agopian518ec112011-05-13 16:21:08 -07001279
1280// ----------------------------------------------------------------------------
1281// EGL 1.1
1282// ----------------------------------------------------------------------------
1283
1284EGLBoolean eglSurfaceAttrib(
1285 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1286{
1287 clearError();
1288
Jesse Hallb29e5e82012-04-04 16:53:42 -07001289 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001290 if (!dp) return EGL_FALSE;
1291
Jesse Hallb29e5e82012-04-04 16:53:42 -07001292 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001293 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001294 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001295
Pablo Ceballosc18be292016-05-31 14:55:42 -07001296 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001297
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001298 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001299 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001300 setError(EGL_BAD_SURFACE, EGL_FALSE);
1301 }
Mathias Agopian65421432017-03-08 11:49:05 -08001302 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1303 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001304 }
1305
Pablo Ceballosc18be292016-05-31 14:55:42 -07001306 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001307 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001308 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001309 }
Mathias Agopian65421432017-03-08 11:49:05 -08001310 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1311 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001312 }
1313
Mathias Agopian518ec112011-05-13 16:21:08 -07001314 if (s->cnx->egl.eglSurfaceAttrib) {
1315 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001316 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001317 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001318 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001319}
1320
1321EGLBoolean eglBindTexImage(
1322 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1323{
1324 clearError();
1325
Jesse Hallb29e5e82012-04-04 16:53:42 -07001326 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001327 if (!dp) return EGL_FALSE;
1328
Jesse Hallb29e5e82012-04-04 16:53:42 -07001329 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001330 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001331 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001332
Mathias Agopian518ec112011-05-13 16:21:08 -07001333 egl_surface_t const * const s = get_surface(surface);
1334 if (s->cnx->egl.eglBindTexImage) {
1335 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001336 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001337 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001338 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001339}
1340
1341EGLBoolean eglReleaseTexImage(
1342 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1343{
1344 clearError();
1345
Jesse Hallb29e5e82012-04-04 16:53:42 -07001346 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001347 if (!dp) return EGL_FALSE;
1348
Jesse Hallb29e5e82012-04-04 16:53:42 -07001349 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001350 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001351 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001352
Mathias Agopian518ec112011-05-13 16:21:08 -07001353 egl_surface_t const * const s = get_surface(surface);
1354 if (s->cnx->egl.eglReleaseTexImage) {
1355 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001356 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001357 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001358 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001359}
1360
1361EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1362{
1363 clearError();
1364
Jesse Hallb29e5e82012-04-04 16:53:42 -07001365 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001366 if (!dp) return EGL_FALSE;
1367
1368 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001369 egl_connection_t* const cnx = &gEGLImpl;
1370 if (cnx->dso && cnx->egl.eglSwapInterval) {
1371 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001372 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001373
Mathias Agopian518ec112011-05-13 16:21:08 -07001374 return res;
1375}
1376
1377
1378// ----------------------------------------------------------------------------
1379// EGL 1.2
1380// ----------------------------------------------------------------------------
1381
1382EGLBoolean eglWaitClient(void)
1383{
1384 clearError();
1385
Mathias Agopianada798b2012-02-13 17:09:30 -08001386 egl_connection_t* const cnx = &gEGLImpl;
1387 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001388 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001389
1390 EGLBoolean res;
1391 if (cnx->egl.eglWaitClient) {
1392 res = cnx->egl.eglWaitClient();
1393 } else {
1394 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001395 }
1396 return res;
1397}
1398
1399EGLBoolean eglBindAPI(EGLenum api)
1400{
1401 clearError();
1402
1403 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001404 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001405 }
1406
1407 // bind this API on all EGLs
1408 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001409 egl_connection_t* const cnx = &gEGLImpl;
1410 if (cnx->dso && cnx->egl.eglBindAPI) {
1411 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001412 }
1413 return res;
1414}
1415
1416EGLenum eglQueryAPI(void)
1417{
1418 clearError();
1419
1420 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001421 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001422 }
1423
Mathias Agopianada798b2012-02-13 17:09:30 -08001424 egl_connection_t* const cnx = &gEGLImpl;
1425 if (cnx->dso && cnx->egl.eglQueryAPI) {
1426 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001427 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001428
Mathias Agopian518ec112011-05-13 16:21:08 -07001429 // or, it can only be OpenGL ES
1430 return EGL_OPENGL_ES_API;
1431}
1432
1433EGLBoolean eglReleaseThread(void)
1434{
1435 clearError();
1436
Mathias Agopianada798b2012-02-13 17:09:30 -08001437 egl_connection_t* const cnx = &gEGLImpl;
1438 if (cnx->dso && cnx->egl.eglReleaseThread) {
1439 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001440 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301441
1442 // If there is context bound to the thread, release it
1443 egl_display_t::loseCurrent(get_context(getContext()));
1444
Mathias Agopian518ec112011-05-13 16:21:08 -07001445 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001446 return EGL_TRUE;
1447}
1448
1449EGLSurface eglCreatePbufferFromClientBuffer(
1450 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1451 EGLConfig config, const EGLint *attrib_list)
1452{
1453 clearError();
1454
Jesse Hallb29e5e82012-04-04 16:53:42 -07001455 egl_connection_t* cnx = NULL;
1456 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1457 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001458 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1459 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001460 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001461 }
1462 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1463}
1464
1465// ----------------------------------------------------------------------------
1466// EGL_EGLEXT_VERSION 3
1467// ----------------------------------------------------------------------------
1468
1469EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1470 const EGLint *attrib_list)
1471{
1472 clearError();
1473
Jesse Hallb29e5e82012-04-04 16:53:42 -07001474 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001475 if (!dp) return EGL_FALSE;
1476
Jesse Hallb29e5e82012-04-04 16:53:42 -07001477 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001478 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001479 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001480
1481 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001482 if (s->cnx->egl.eglLockSurfaceKHR) {
1483 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001484 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001485 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001486 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001487}
1488
1489EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1490{
1491 clearError();
1492
Jesse Hallb29e5e82012-04-04 16:53:42 -07001493 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001494 if (!dp) return EGL_FALSE;
1495
Jesse Hallb29e5e82012-04-04 16:53:42 -07001496 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001497 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001498 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001499
1500 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001501 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001502 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001504 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505}
1506
1507EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1508 EGLClientBuffer buffer, const EGLint *attrib_list)
1509{
1510 clearError();
1511
Jesse Hallb29e5e82012-04-04 16:53:42 -07001512 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001513 if (!dp) return EGL_NO_IMAGE_KHR;
1514
Jesse Hallb29e5e82012-04-04 16:53:42 -07001515 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001516 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001517
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001518 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1519 egl_connection_t* const cnx = &gEGLImpl;
1520 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1521 result = cnx->egl.eglCreateImageKHR(
1522 dp->disp.dpy,
1523 c ? c->context : EGL_NO_CONTEXT,
1524 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001525 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001526 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001527}
1528
1529EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1530{
1531 clearError();
1532
Jesse Hallb29e5e82012-04-04 16:53:42 -07001533 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001534 if (!dp) return EGL_FALSE;
1535
Steven Holte646a5c52012-06-04 20:02:11 -07001536 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001537 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001538 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001539 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001540 }
Steven Holte646a5c52012-06-04 20:02:11 -07001541 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001542}
1543
1544// ----------------------------------------------------------------------------
1545// EGL_EGLEXT_VERSION 5
1546// ----------------------------------------------------------------------------
1547
1548
1549EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1550{
1551 clearError();
1552
Jesse Hallb29e5e82012-04-04 16:53:42 -07001553 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001554 if (!dp) return EGL_NO_SYNC_KHR;
1555
Mathias Agopian518ec112011-05-13 16:21:08 -07001556 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001557 egl_connection_t* const cnx = &gEGLImpl;
1558 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1559 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001560 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001561 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001562}
1563
1564EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1565{
1566 clearError();
1567
Jesse Hallb29e5e82012-04-04 16:53:42 -07001568 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001569 if (!dp) return EGL_FALSE;
1570
Mathias Agopian518ec112011-05-13 16:21:08 -07001571 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001572 egl_connection_t* const cnx = &gEGLImpl;
1573 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1574 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001575 }
1576 return result;
1577}
1578
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001579EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1580 clearError();
1581
1582 const egl_display_ptr dp = validate_display(dpy);
1583 if (!dp) return EGL_FALSE;
1584
1585 EGLBoolean result = EGL_FALSE;
1586 egl_connection_t* const cnx = &gEGLImpl;
1587 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1588 result = cnx->egl.eglSignalSyncKHR(
1589 dp->disp.dpy, sync, mode);
1590 }
1591 return result;
1592}
1593
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001594EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1595 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001596{
1597 clearError();
1598
Jesse Hallb29e5e82012-04-04 16:53:42 -07001599 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001600 if (!dp) return EGL_FALSE;
1601
Mathias Agopian737b8962017-02-24 14:32:05 -08001602 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001603 egl_connection_t* const cnx = &gEGLImpl;
1604 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1605 result = cnx->egl.eglClientWaitSyncKHR(
1606 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001607 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001608 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001609}
1610
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001611EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1612 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001613{
1614 clearError();
1615
Jesse Hallb29e5e82012-04-04 16:53:42 -07001616 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001617 if (!dp) return EGL_FALSE;
1618
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001619 EGLBoolean result = EGL_FALSE;
1620 egl_connection_t* const cnx = &gEGLImpl;
1621 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1622 result = cnx->egl.eglGetSyncAttribKHR(
1623 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001624 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001625 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001626}
1627
Season Li000d88f2015-07-01 11:39:40 -07001628EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1629{
1630 clearError();
1631
1632 const egl_display_ptr dp = validate_display(dpy);
1633 if (!dp) return EGL_NO_STREAM_KHR;
1634
1635 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1636 egl_connection_t* const cnx = &gEGLImpl;
1637 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1638 result = cnx->egl.eglCreateStreamKHR(
1639 dp->disp.dpy, attrib_list);
1640 }
1641 return result;
1642}
1643
1644EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1645{
1646 clearError();
1647
1648 const egl_display_ptr dp = validate_display(dpy);
1649 if (!dp) return EGL_FALSE;
1650
1651 EGLBoolean result = EGL_FALSE;
1652 egl_connection_t* const cnx = &gEGLImpl;
1653 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1654 result = cnx->egl.eglDestroyStreamKHR(
1655 dp->disp.dpy, stream);
1656 }
1657 return result;
1658}
1659
1660EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1661 EGLenum attribute, EGLint value)
1662{
1663 clearError();
1664
1665 const egl_display_ptr dp = validate_display(dpy);
1666 if (!dp) return EGL_FALSE;
1667
1668 EGLBoolean result = EGL_FALSE;
1669 egl_connection_t* const cnx = &gEGLImpl;
1670 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1671 result = cnx->egl.eglStreamAttribKHR(
1672 dp->disp.dpy, stream, attribute, value);
1673 }
1674 return result;
1675}
1676
1677EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1678 EGLenum attribute, EGLint *value)
1679{
1680 clearError();
1681
1682 const egl_display_ptr dp = validate_display(dpy);
1683 if (!dp) return EGL_FALSE;
1684
1685 EGLBoolean result = EGL_FALSE;
1686 egl_connection_t* const cnx = &gEGLImpl;
1687 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1688 result = cnx->egl.eglQueryStreamKHR(
1689 dp->disp.dpy, stream, attribute, value);
1690 }
1691 return result;
1692}
1693
1694EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1695 EGLenum attribute, EGLuint64KHR *value)
1696{
1697 clearError();
1698
1699 const egl_display_ptr dp = validate_display(dpy);
1700 if (!dp) return EGL_FALSE;
1701
1702 EGLBoolean result = EGL_FALSE;
1703 egl_connection_t* const cnx = &gEGLImpl;
1704 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1705 result = cnx->egl.eglQueryStreamu64KHR(
1706 dp->disp.dpy, stream, attribute, value);
1707 }
1708 return result;
1709}
1710
1711EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1712 EGLenum attribute, EGLTimeKHR *value)
1713{
1714 clearError();
1715
1716 const egl_display_ptr dp = validate_display(dpy);
1717 if (!dp) return EGL_FALSE;
1718
1719 EGLBoolean result = EGL_FALSE;
1720 egl_connection_t* const cnx = &gEGLImpl;
1721 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1722 result = cnx->egl.eglQueryStreamTimeKHR(
1723 dp->disp.dpy, stream, attribute, value);
1724 }
1725 return result;
1726}
1727
1728EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1729 EGLStreamKHR stream, const EGLint *attrib_list)
1730{
1731 clearError();
1732
1733 egl_display_ptr dp = validate_display(dpy);
1734 if (!dp) return EGL_NO_SURFACE;
1735
1736 egl_connection_t* const cnx = &gEGLImpl;
1737 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1738 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1739 dp->disp.dpy, config, stream, attrib_list);
1740 if (surface != EGL_NO_SURFACE) {
1741 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1742 surface, cnx);
1743 return s;
1744 }
1745 }
1746 return EGL_NO_SURFACE;
1747}
1748
1749EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1750 EGLStreamKHR stream)
1751{
1752 clearError();
1753
1754 const egl_display_ptr dp = validate_display(dpy);
1755 if (!dp) return EGL_FALSE;
1756
1757 EGLBoolean result = EGL_FALSE;
1758 egl_connection_t* const cnx = &gEGLImpl;
1759 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1760 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1761 dp->disp.dpy, stream);
1762 }
1763 return result;
1764}
1765
1766EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1767 EGLStreamKHR stream)
1768{
1769 clearError();
1770
1771 const egl_display_ptr dp = validate_display(dpy);
1772 if (!dp) return EGL_FALSE;
1773
1774 EGLBoolean result = EGL_FALSE;
1775 egl_connection_t* const cnx = &gEGLImpl;
1776 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1777 result = cnx->egl.eglStreamConsumerAcquireKHR(
1778 dp->disp.dpy, stream);
1779 }
1780 return result;
1781}
1782
1783EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1784 EGLStreamKHR stream)
1785{
1786 clearError();
1787
1788 const egl_display_ptr dp = validate_display(dpy);
1789 if (!dp) return EGL_FALSE;
1790
1791 EGLBoolean result = EGL_FALSE;
1792 egl_connection_t* const cnx = &gEGLImpl;
1793 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1794 result = cnx->egl.eglStreamConsumerReleaseKHR(
1795 dp->disp.dpy, stream);
1796 }
1797 return result;
1798}
1799
1800EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1801 EGLDisplay dpy, EGLStreamKHR stream)
1802{
1803 clearError();
1804
1805 const egl_display_ptr dp = validate_display(dpy);
1806 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1807
1808 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1809 egl_connection_t* const cnx = &gEGLImpl;
1810 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1811 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1812 dp->disp.dpy, stream);
1813 }
1814 return result;
1815}
1816
1817EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1818 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1819{
1820 clearError();
1821
1822 const egl_display_ptr dp = validate_display(dpy);
1823 if (!dp) return EGL_NO_STREAM_KHR;
1824
1825 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1826 egl_connection_t* const cnx = &gEGLImpl;
1827 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1828 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1829 dp->disp.dpy, file_descriptor);
1830 }
1831 return result;
1832}
1833
Mathias Agopian518ec112011-05-13 16:21:08 -07001834// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001835// EGL_EGLEXT_VERSION 15
1836// ----------------------------------------------------------------------------
1837
1838EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1839 clearError();
1840 const egl_display_ptr dp = validate_display(dpy);
1841 if (!dp) return EGL_FALSE;
1842 EGLint result = EGL_FALSE;
1843 egl_connection_t* const cnx = &gEGLImpl;
1844 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1845 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1846 }
1847 return result;
1848}
1849
1850// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001851// ANDROID extensions
1852// ----------------------------------------------------------------------------
1853
Jamie Gennis331841b2012-09-06 14:52:00 -07001854EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1855{
1856 clearError();
1857
1858 const egl_display_ptr dp = validate_display(dpy);
1859 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1860
1861 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1862 egl_connection_t* const cnx = &gEGLImpl;
1863 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1864 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1865 }
1866 return result;
1867}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001868
Andy McFadden72841452013-03-01 16:25:32 -08001869EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1870 EGLnsecsANDROID time)
1871{
1872 clearError();
1873
1874 const egl_display_ptr dp = validate_display(dpy);
1875 if (!dp) {
1876 return EGL_FALSE;
1877 }
1878
1879 SurfaceRef _s(dp.get(), surface);
1880 if (!_s.get()) {
1881 setError(EGL_BAD_SURFACE, EGL_FALSE);
1882 return EGL_FALSE;
1883 }
1884
1885 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08001886 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08001887
1888 return EGL_TRUE;
1889}
1890
Craig Donner60761072017-01-27 12:30:44 -08001891EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1892 clearError();
Craig Donner60761072017-01-27 12:30:44 -08001893 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08001894 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Craig Donner60761072017-01-27 12:30:44 -08001895}
1896
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001897// ----------------------------------------------------------------------------
1898// NVIDIA extensions
1899// ----------------------------------------------------------------------------
1900EGLuint64NV eglGetSystemTimeFrequencyNV()
1901{
1902 clearError();
1903
1904 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001905 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001906 }
1907
1908 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001909 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001910
Mathias Agopianada798b2012-02-13 17:09:30 -08001911 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1912 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001913 }
1914
Mathias Agopian737b8962017-02-24 14:32:05 -08001915 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001916}
1917
1918EGLuint64NV eglGetSystemTimeNV()
1919{
1920 clearError();
1921
1922 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001923 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001924 }
1925
1926 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001927 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001928
Mathias Agopianada798b2012-02-13 17:09:30 -08001929 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1930 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001931 }
1932
Mathias Agopian737b8962017-02-24 14:32:05 -08001933 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001934}
Dan Stozaa894d082015-02-19 15:27:36 -08001935
1936// ----------------------------------------------------------------------------
1937// Partial update extension
1938// ----------------------------------------------------------------------------
1939EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1940 EGLint *rects, EGLint n_rects)
1941{
1942 clearError();
1943
1944 const egl_display_ptr dp = validate_display(dpy);
1945 if (!dp) {
1946 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1947 return EGL_FALSE;
1948 }
1949
1950 SurfaceRef _s(dp.get(), surface);
1951 if (!_s.get()) {
1952 setError(EGL_BAD_SURFACE, EGL_FALSE);
1953 return EGL_FALSE;
1954 }
1955
1956 egl_surface_t const * const s = get_surface(surface);
1957 if (s->cnx->egl.eglSetDamageRegionKHR) {
1958 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1959 rects, n_rects);
1960 }
1961
1962 return EGL_FALSE;
1963}
Pablo Ceballosc18be292016-05-31 14:55:42 -07001964
Brian Anderson1049d1d2016-12-16 17:25:57 -08001965EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
1966 EGLuint64KHR *frameId) {
1967 clearError();
1968
1969 const egl_display_ptr dp = validate_display(dpy);
1970 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001971 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001972 }
1973
1974 SurfaceRef _s(dp.get(), surface);
1975 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001976 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001977 }
1978
1979 egl_surface_t const * const s = get_surface(surface);
1980
Mathias Agopian65421432017-03-08 11:49:05 -08001981 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001982 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001983 }
1984
1985 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08001986 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001987
Mathias Agopian65421432017-03-08 11:49:05 -08001988 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08001989 // This should not happen. Return an error that is not in the spec
1990 // so it's obvious something is very wrong.
1991 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08001992 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08001993 }
1994
1995 *frameId = nextFrameId;
1996 return EGL_TRUE;
1997}
1998
Brian Anderson0a61b0c2016-12-07 14:55:56 -08001999EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2000 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2001{
2002 clearError();
2003
2004 const egl_display_ptr dp = validate_display(dpy);
2005 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002006 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002007 }
2008
2009 SurfaceRef _s(dp.get(), surface);
2010 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002011 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002012 }
2013
2014 egl_surface_t const * const s = get_surface(surface);
2015
Mathias Agopian65421432017-03-08 11:49:05 -08002016 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002017 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002018 }
2019
2020 nsecs_t* compositeDeadline = nullptr;
2021 nsecs_t* compositeInterval = nullptr;
2022 nsecs_t* compositeToPresentLatency = nullptr;
2023
2024 for (int i = 0; i < numTimestamps; i++) {
2025 switch (names[i]) {
2026 case EGL_COMPOSITE_DEADLINE_ANDROID:
2027 compositeDeadline = &values[i];
2028 break;
2029 case EGL_COMPOSITE_INTERVAL_ANDROID:
2030 compositeInterval = &values[i];
2031 break;
2032 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2033 compositeToPresentLatency = &values[i];
2034 break;
2035 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002036 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002037 }
2038 }
2039
Mathias Agopian65421432017-03-08 11:49:05 -08002040 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002041 compositeDeadline, compositeInterval, compositeToPresentLatency);
2042
2043 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002044 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002045 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002046 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002047 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002048 default:
2049 // This should not happen. Return an error that is not in the spec
2050 // so it's obvious something is very wrong.
2051 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002052 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002053 }
2054}
2055
2056EGLBoolean eglGetCompositorTimingSupportedANDROID(
2057 EGLDisplay dpy, EGLSurface surface, EGLint name)
2058{
2059 clearError();
2060
2061 const egl_display_ptr dp = validate_display(dpy);
2062 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002063 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002064 }
2065
2066 SurfaceRef _s(dp.get(), surface);
2067 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002068 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002069 }
2070
2071 egl_surface_t const * const s = get_surface(surface);
2072
Mathias Agopian65421432017-03-08 11:49:05 -08002073 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002074 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002075 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002076 }
2077
2078 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002079 case EGL_COMPOSITE_DEADLINE_ANDROID:
2080 case EGL_COMPOSITE_INTERVAL_ANDROID:
2081 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2082 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002083 default:
2084 return EGL_FALSE;
2085 }
2086}
2087
Pablo Ceballosc18be292016-05-31 14:55:42 -07002088EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002089 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002090 EGLnsecsANDROID *values)
2091{
2092 clearError();
2093
2094 const egl_display_ptr dp = validate_display(dpy);
2095 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002096 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002097 }
2098
2099 SurfaceRef _s(dp.get(), surface);
2100 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002101 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002102 }
2103
2104 egl_surface_t const * const s = get_surface(surface);
2105
Mathias Agopian65421432017-03-08 11:49:05 -08002106 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002107 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002108 }
2109
Brian Andersondbd0ea82016-07-22 09:38:59 -07002110 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002111 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002112 nsecs_t* latchTime = nullptr;
2113 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002114 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002115 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002116 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002117 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002118 nsecs_t* releaseTime = nullptr;
2119
2120 for (int i = 0; i < numTimestamps; i++) {
2121 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002122 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2123 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002124 break;
2125 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2126 acquireTime = &values[i];
2127 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002128 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2129 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002130 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002131 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2132 firstRefreshStartTime = &values[i];
2133 break;
2134 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2135 lastRefreshStartTime = &values[i];
2136 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002137 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2138 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002139 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002140 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2141 displayPresentTime = &values[i];
2142 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002143 case EGL_DEQUEUE_READY_TIME_ANDROID:
2144 dequeueReadyTime = &values[i];
2145 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002146 case EGL_READS_DONE_TIME_ANDROID:
2147 releaseTime = &values[i];
2148 break;
2149 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002150 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002151 }
2152 }
2153
Mathias Agopian65421432017-03-08 11:49:05 -08002154 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002155 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002156 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002157 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002158
Brian Anderson069b3652016-07-22 10:32:47 -07002159 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002160 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002161 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002162 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002163 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002164 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002165 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002166 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002167 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2168 default:
2169 // This should not happen. Return an error that is not in the spec
2170 // so it's obvious something is very wrong.
2171 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2172 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002173 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002174}
2175
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002176EGLBoolean eglGetFrameTimestampSupportedANDROID(
2177 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002178{
2179 clearError();
2180
2181 const egl_display_ptr dp = validate_display(dpy);
2182 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002183 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002184 }
2185
2186 SurfaceRef _s(dp.get(), surface);
2187 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002188 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002189 }
2190
2191 egl_surface_t const * const s = get_surface(surface);
2192
Mathias Agopian65421432017-03-08 11:49:05 -08002193 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002194 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002195 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002196 }
2197
2198 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002199 case EGL_COMPOSITE_DEADLINE_ANDROID:
2200 case EGL_COMPOSITE_INTERVAL_ANDROID:
2201 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002202 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002203 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002204 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2205 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2206 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002207 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002208 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002209 case EGL_READS_DONE_TIME_ANDROID:
2210 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002211 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2212 int value = 0;
2213 window->query(window,
2214 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2215 return value == 0 ? EGL_FALSE : EGL_TRUE;
2216 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002217 default:
2218 return EGL_FALSE;
2219 }
2220}