blob: 242217763b7b4a96feb7fb5fa8bb91350c9ee119 [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#include <system/window.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
Craig Donner60761072017-01-27 12:30:44 -080030#include <android/hardware_buffer.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070031#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070033#include <cutils/memory.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070034#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070035#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070036
Craig Donner68671532016-07-18 10:19:54 -070037#include <gui/ISurfaceComposer.h>
38
Craig Donner05249fc2016-01-15 19:33:55 -080039#include <ui/GraphicBuffer.h>
40
Mathias Agopian518ec112011-05-13 16:21:08 -070041#include <utils/KeyedVector.h>
42#include <utils/SortedVector.h>
43#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080044#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070045
Craig Donner68671532016-07-18 10:19:54 -070046#include "binder/Binder.h"
47#include "binder/Parcel.h"
48#include "binder/IServiceManager.h"
49
Mathias Agopian39c24a22013-04-04 23:17:56 -070050#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070051#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070052
53#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070054#include "egl_object.h"
55#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080056#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070057
58using namespace android;
59
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070060#define ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS 0
61
Mathias Agopian518ec112011-05-13 16:21:08 -070062// ----------------------------------------------------------------------------
63
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070064namespace android {
65
Mathias Agopian518ec112011-05-13 16:21:08 -070066struct extention_map_t {
67 const char* name;
68 __eglMustCastToProperFunctionPointerType address;
69};
70
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070071/*
Jesse Hall21558da2013-08-06 15:31:22 -070072 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070073 *
Jesse Hall21558da2013-08-06 15:31:22 -070074 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
75 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070076 *
Jesse Hall21558da2013-08-06 15:31:22 -070077 * The rest (gExtensionString) depend on support in the EGL driver, and are
78 * only available if the driver supports them. However, some of these must be
79 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080080 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070081 * the mandatory extensions are present and may not function properly if some
82 * are missing.
83 *
84 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070085 */
Jesse Hall21558da2013-08-06 15:31:22 -070086extern char const * const gBuiltinExtensionString =
87 "EGL_KHR_get_all_proc_addresses "
88 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080089 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080090 "EGL_ANDROID_create_native_client_buffer "
Craig Donner60761072017-01-27 12:30:44 -080091 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080092 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070093#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -070094 "EGL_ANDROID_get_frame_timestamps "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070095#endif
Jesse Hall21558da2013-08-06 15:31:22 -070096 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097extern char const * const gExtensionString =
98 "EGL_KHR_image " // mandatory
99 "EGL_KHR_image_base " // mandatory
100 "EGL_KHR_image_pixmap "
101 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700102 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700104 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700105 "EGL_KHR_gl_texture_cubemap_image "
106 "EGL_KHR_gl_renderbuffer_image "
107 "EGL_KHR_reusable_sync "
108 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700109 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700110 "EGL_KHR_config_attribs "
111 "EGL_KHR_surfaceless_context "
112 "EGL_KHR_stream "
113 "EGL_KHR_stream_fifo "
114 "EGL_KHR_stream_producer_eglsurface "
115 "EGL_KHR_stream_consumer_gltexture "
116 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700117 "EGL_EXT_create_context_robustness "
118 "EGL_NV_system_time "
119 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700120 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700121 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800122 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700123 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800124 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700125 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700126 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700127 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700128 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000129 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700130 ;
131
132// extensions not exposed to applications but used by the ANDROID system
133// "EGL_ANDROID_blob_cache " // strongly recommended
134// "EGL_IMG_hibernate_process " // optional
135// "EGL_ANDROID_native_fence_sync " // strongly recommended
136// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700137// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700138
139/*
140 * EGL Extensions entry-points exposed to 3rd party applications
141 * (keep in sync with gExtensionString above)
142 *
143 */
144static const extention_map_t sExtensionMap[] = {
145 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700146 { "eglLockSurfaceKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
148 { "eglUnlockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700150
151 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700152 { "eglCreateImageKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
154 { "eglDestroyImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700156
157 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
158 { "eglCreateSyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
160 { "eglDestroySyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
162 { "eglClientWaitSyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
164 { "eglSignalSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
166 { "eglGetSyncAttribKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
168
169 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800170 { "eglGetSystemTimeFrequencyNV",
171 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
172 { "eglGetSystemTimeNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700174
Mathias Agopian2bb71682013-03-27 17:32:41 -0700175 // EGL_KHR_wait_sync
176 { "eglWaitSyncKHR",
177 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700178
179 // EGL_ANDROID_presentation_time
180 { "eglPresentationTimeANDROID",
181 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800182
183 // EGL_KHR_swap_buffers_with_damage
184 { "eglSwapBuffersWithDamageKHR",
185 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
186
Craig Donner60761072017-01-27 12:30:44 -0800187 // EGL_ANDROID_create_native_client_buffer
Craig Donner05249fc2016-01-15 19:33:55 -0800188 { "eglCreateNativeClientBufferANDROID",
189 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
190
Craig Donner60761072017-01-27 12:30:44 -0800191 // EGL_ANDROID_get_native_client_buffer
192 { "eglGetNativeClientBufferANDROID",
193 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
194
Dan Stozaa894d082015-02-19 15:27:36 -0800195 // EGL_KHR_partial_update
196 { "eglSetDamageRegionKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700198
199 { "eglCreateStreamKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
201 { "eglDestroyStreamKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
203 { "eglStreamAttribKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
205 { "eglQueryStreamKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
207 { "eglQueryStreamu64KHR",
208 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
209 { "eglQueryStreamTimeKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
211 { "eglCreateStreamProducerSurfaceKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
213 { "eglStreamConsumerGLTextureExternalKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
215 { "eglStreamConsumerAcquireKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
217 { "eglStreamConsumerReleaseKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
219 { "eglGetStreamFileDescriptorKHR",
220 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
221 { "eglCreateStreamFromFileDescriptorKHR",
222 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700223
224 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800225 { "eglGetNextFrameIdANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700227 { "eglGetFrameTimestampsANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
229 { "eglQueryTimestampSupportedANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglQueryTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700231};
232
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700233/*
234 * These extensions entry-points should not be exposed to applications.
235 * They're used internally by the Android EGL layer.
236 */
237#define FILTER_EXTENSIONS(procname) \
238 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
239 !strcmp((procname), "eglHibernateProcessIMG") || \
240 !strcmp((procname), "eglAwakenProcessIMG") || \
241 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
242
243
244
Mathias Agopian518ec112011-05-13 16:21:08 -0700245// accesses protected by sExtensionMapMutex
246static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
247static int sGLExtentionSlot = 0;
248static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
249
250static void(*findProcAddress(const char* name,
251 const extention_map_t* map, size_t n))() {
252 for (uint32_t i=0 ; i<n ; i++) {
253 if (!strcmp(name, map[i].name)) {
254 return map[i].address;
255 }
256 }
257 return NULL;
258}
259
260// ----------------------------------------------------------------------------
261
Mathias Agopian518ec112011-05-13 16:21:08 -0700262extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
263extern EGLBoolean egl_init_drivers();
264extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700265extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700266
Mathias Agopian518ec112011-05-13 16:21:08 -0700267} // namespace android;
268
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700269
Mathias Agopian518ec112011-05-13 16:21:08 -0700270// ----------------------------------------------------------------------------
271
272static inline void clearError() { egl_tls_t::clearError(); }
273static inline EGLContext getContext() { return egl_tls_t::getContext(); }
274
275// ----------------------------------------------------------------------------
276
277EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
278{
Jesse Hall1508ae62017-01-19 17:43:26 -0800279 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700280 clearError();
281
Dan Stozac3289c42014-01-17 11:38:34 -0800282 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700283 if (index >= NUM_DISPLAYS) {
284 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
285 }
286
287 if (egl_init_drivers() == EGL_FALSE) {
288 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
289 }
290
291 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
292 return dpy;
293}
294
295// ----------------------------------------------------------------------------
296// Initialization
297// ----------------------------------------------------------------------------
298
299EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
300{
301 clearError();
302
Jesse Hallb29e5e82012-04-04 16:53:42 -0700303 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700304 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
305
306 EGLBoolean res = dp->initialize(major, minor);
307
308 return res;
309}
310
311EGLBoolean eglTerminate(EGLDisplay dpy)
312{
313 // NOTE: don't unload the drivers b/c some APIs can be called
314 // after eglTerminate() has been called. eglTerminate() only
315 // terminates an EGLDisplay, not a EGL itself.
316
317 clearError();
318
Jesse Hallb29e5e82012-04-04 16:53:42 -0700319 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700320 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
321
322 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800323
Mathias Agopian518ec112011-05-13 16:21:08 -0700324 return res;
325}
326
327// ----------------------------------------------------------------------------
328// configuration
329// ----------------------------------------------------------------------------
330
331EGLBoolean eglGetConfigs( EGLDisplay dpy,
332 EGLConfig *configs,
333 EGLint config_size, EGLint *num_config)
334{
335 clearError();
336
Jesse Hallb29e5e82012-04-04 16:53:42 -0700337 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700338 if (!dp) return EGL_FALSE;
339
Mathias Agopian7773c432012-02-13 20:06:08 -0800340 if (num_config==0) {
341 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700342 }
343
Mathias Agopian7773c432012-02-13 20:06:08 -0800344 EGLBoolean res = EGL_FALSE;
345 *num_config = 0;
346
347 egl_connection_t* const cnx = &gEGLImpl;
348 if (cnx->dso) {
349 res = cnx->egl.eglGetConfigs(
350 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700351 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800352
353 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700354}
355
356EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
357 EGLConfig *configs, EGLint config_size,
358 EGLint *num_config)
359{
360 clearError();
361
Jesse Hallb29e5e82012-04-04 16:53:42 -0700362 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700363 if (!dp) return EGL_FALSE;
364
365 if (num_config==0) {
366 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
367 }
368
Mathias Agopian518ec112011-05-13 16:21:08 -0700369 EGLBoolean res = EGL_FALSE;
370 *num_config = 0;
371
Mathias Agopianada798b2012-02-13 17:09:30 -0800372 egl_connection_t* const cnx = &gEGLImpl;
373 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700374 if (attrib_list) {
375 char value[PROPERTY_VALUE_MAX];
376 property_get("debug.egl.force_msaa", value, "false");
377
378 if (!strcmp(value, "true")) {
379 size_t attribCount = 0;
380 EGLint attrib = attrib_list[0];
381
382 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700383 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700384 const EGLint *attribRendererable = NULL;
385 const EGLint *attribCaveat = NULL;
386
387 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700388 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700389 while (attrib != EGL_NONE) {
390 attrib = attrib_list[attribCount];
391 switch (attrib) {
392 case EGL_RENDERABLE_TYPE:
393 attribRendererable = &attrib_list[attribCount];
394 break;
395 case EGL_CONFIG_CAVEAT:
396 attribCaveat = &attrib_list[attribCount];
397 break;
398 }
399 attribCount++;
400 }
401
402 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
403 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800404
Romain Guy1cffc802012-10-15 18:13:05 -0700405 // Insert 2 extra attributes to force-enable MSAA 4x
406 EGLint aaAttribs[attribCount + 4];
407 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
408 aaAttribs[1] = 1;
409 aaAttribs[2] = EGL_SAMPLES;
410 aaAttribs[3] = 4;
411
412 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
413
414 EGLint numConfigAA;
415 EGLBoolean resAA = cnx->egl.eglChooseConfig(
416 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
417
418 if (resAA == EGL_TRUE && numConfigAA > 0) {
419 ALOGD("Enabling MSAA 4x");
420 *num_config = numConfigAA;
421 return resAA;
422 }
423 }
424 }
425 }
426
Mathias Agopian7773c432012-02-13 20:06:08 -0800427 res = cnx->egl.eglChooseConfig(
428 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700429 }
430 return res;
431}
432
433EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
434 EGLint attribute, EGLint *value)
435{
436 clearError();
437
Jesse Hallb29e5e82012-04-04 16:53:42 -0700438 egl_connection_t* cnx = NULL;
439 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
440 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800441
Mathias Agopian518ec112011-05-13 16:21:08 -0700442 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800443 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700444}
445
446// ----------------------------------------------------------------------------
447// surfaces
448// ----------------------------------------------------------------------------
449
Jesse Hallc2e41222013-08-08 13:40:22 -0700450// Turn linear formats into corresponding sRGB formats when colorspace is
451// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
452// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800453// the modification isn't possible, the original dataSpace is returned.
454static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
455 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700456 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800457 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700458 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800459 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700460 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800461 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700462}
463
Mathias Agopian518ec112011-05-13 16:21:08 -0700464EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
465 NativeWindowType window,
466 const EGLint *attrib_list)
467{
468 clearError();
469
Jesse Hallb29e5e82012-04-04 16:53:42 -0700470 egl_connection_t* cnx = NULL;
471 egl_display_ptr dp = validate_display_connection(dpy, cnx);
472 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800473 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700474
Andy McFaddend566ce32014-01-07 15:54:17 -0800475 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
476 if (result != OK) {
477 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
478 "failed (%#x) (already connected to another API?)",
479 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700480 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700481 }
482
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700483 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700484 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
485 // of our native format. So if sRGB gamma is requested, we have to
486 // modify the EGLconfig's format before setting the native window's
487 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800488
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700489 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
490 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
491 &componentType);
492
493 // by default, just pick appropriate RGBA
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700494 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700495 if (dp->haveExtension("EGL_EXT_pixel_format_float") &&
496 (componentType == EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT)) {
497 format = HAL_PIXEL_FORMAT_RGBA_FP16;
498 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800499 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700500
501 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700502 EGLint r, g, b;
503 r = g = b = 0;
504 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
505 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
506 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700507 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700508 EGLint colorDepth = r + g + b;
509
510 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700511 if (colorDepth <= 16) {
512 format = HAL_PIXEL_FORMAT_RGB_565;
513 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700514 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
515 if (colorDepth > 24) {
516 format = HAL_PIXEL_FORMAT_RGBA_1010102;
517 } else {
518 format = HAL_PIXEL_FORMAT_RGBX_8888;
519 }
520 } else {
521 format = HAL_PIXEL_FORMAT_RGBA_FP16;
522 }
523 }
524 } else {
525 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
526 if (colorDepth > 24) {
527 format = HAL_PIXEL_FORMAT_RGBA_1010102;
528 } else {
529 format = HAL_PIXEL_FORMAT_RGBA_8888;
530 }
531 } else {
532 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700533 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700534 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700535
536 // now select a corresponding sRGB format if needed
537 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
538 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
539 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530540 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700541 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700542 }
543 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800544
Jesse Hallc2e41222013-08-08 13:40:22 -0700545 if (format != 0) {
546 int err = native_window_set_buffers_format(window, format);
547 if (err != 0) {
548 ALOGE("error setting native window pixel format: %s (%d)",
549 strerror(-err), err);
550 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
551 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
552 }
553 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700554
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800555 if (dataSpace != 0) {
556 int err = native_window_set_buffers_data_space(window, dataSpace);
557 if (err != 0) {
558 ALOGE("error setting native window pixel dataSpace: %s (%d)",
559 strerror(-err), err);
560 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
561 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
562 }
563 }
564
Jamie Gennis59769462011-11-19 18:04:43 -0800565 // the EGL spec requires that a new EGLSurface default to swap interval
566 // 1, so explicitly set that on the window here.
567 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
568 anw->setSwapInterval(anw, 1);
569
Mathias Agopian518ec112011-05-13 16:21:08 -0700570 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800571 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700572 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700573 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
574 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 return s;
576 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700577
578 // EGLSurface creation failed
579 native_window_set_buffers_format(window, 0);
580 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700581 }
582 return EGL_NO_SURFACE;
583}
584
585EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
586 NativePixmapType pixmap,
587 const EGLint *attrib_list)
588{
589 clearError();
590
Jesse Hallb29e5e82012-04-04 16:53:42 -0700591 egl_connection_t* cnx = NULL;
592 egl_display_ptr dp = validate_display_connection(dpy, cnx);
593 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700594 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800595 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700596 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700597 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
598 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700599 return s;
600 }
601 }
602 return EGL_NO_SURFACE;
603}
604
605EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
606 const EGLint *attrib_list)
607{
608 clearError();
609
Jesse Hallb29e5e82012-04-04 16:53:42 -0700610 egl_connection_t* cnx = NULL;
611 egl_display_ptr dp = validate_display_connection(dpy, cnx);
612 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700613 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800614 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700615 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700616 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
617 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700618 return s;
619 }
620 }
621 return EGL_NO_SURFACE;
622}
Jesse Hall47743382013-02-08 11:13:46 -0800623
Mathias Agopian518ec112011-05-13 16:21:08 -0700624EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
625{
626 clearError();
627
Jesse Hallb29e5e82012-04-04 16:53:42 -0700628 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700629 if (!dp) return EGL_FALSE;
630
Jesse Hallb29e5e82012-04-04 16:53:42 -0700631 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700632 if (!_s.get())
633 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700634
635 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800636 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700637 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700638 _s.terminate();
639 }
640 return result;
641}
642
643EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
644 EGLint attribute, EGLint *value)
645{
646 clearError();
647
Jesse Hallb29e5e82012-04-04 16:53:42 -0700648 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700649 if (!dp) return EGL_FALSE;
650
Jesse Hallb29e5e82012-04-04 16:53:42 -0700651 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700652 if (!_s.get())
653 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700654
Mathias Agopian518ec112011-05-13 16:21:08 -0700655 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800656 return s->cnx->egl.eglQuerySurface(
657 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700658}
659
Jamie Gennise8696a42012-01-15 18:54:57 -0800660void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800661 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800662 clearError();
663
Jesse Hallb29e5e82012-04-04 16:53:42 -0700664 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800665 if (!dp) {
666 return;
667 }
668
Jesse Hallb29e5e82012-04-04 16:53:42 -0700669 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800670 if (!_s.get()) {
671 setError(EGL_BAD_SURFACE, EGL_FALSE);
672 return;
673 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800674}
675
Mathias Agopian518ec112011-05-13 16:21:08 -0700676// ----------------------------------------------------------------------------
677// Contexts
678// ----------------------------------------------------------------------------
679
680EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
681 EGLContext share_list, const EGLint *attrib_list)
682{
683 clearError();
684
Jesse Hallb29e5e82012-04-04 16:53:42 -0700685 egl_connection_t* cnx = NULL;
686 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700687 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700688 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700689 if (!ContextRef(dp.get(), share_list).get()) {
690 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
691 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700692 egl_context_t* const c = get_context(share_list);
693 share_list = c->context;
694 }
695 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800696 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700697 if (context != EGL_NO_CONTEXT) {
698 // figure out if it's a GLESv1 or GLESv2
699 int version = 0;
700 if (attrib_list) {
701 while (*attrib_list != EGL_NONE) {
702 GLint attr = *attrib_list++;
703 GLint value = *attrib_list++;
704 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
705 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800706 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800707 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800708 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700709 }
710 }
711 };
712 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700713 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
714 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700715 return c;
716 }
717 }
718 return EGL_NO_CONTEXT;
719}
720
721EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
722{
723 clearError();
724
Jesse Hallb29e5e82012-04-04 16:53:42 -0700725 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700726 if (!dp)
727 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700728
Jesse Hallb29e5e82012-04-04 16:53:42 -0700729 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700730 if (!_c.get())
731 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800732
Mathias Agopian518ec112011-05-13 16:21:08 -0700733 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800734 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700735 if (result == EGL_TRUE) {
736 _c.terminate();
737 }
738 return result;
739}
740
Mathias Agopian518ec112011-05-13 16:21:08 -0700741EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
742 EGLSurface read, EGLContext ctx)
743{
744 clearError();
745
Jesse Hallb29e5e82012-04-04 16:53:42 -0700746 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700747 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
748
Mathias Agopian5b287a62011-05-16 18:58:55 -0700749 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
750 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
751 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700752 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
753 (draw != EGL_NO_SURFACE) ) {
754 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
755 }
756
757 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700758 ContextRef _c(dp.get(), ctx);
759 SurfaceRef _d(dp.get(), draw);
760 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700761
762 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700763 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700764 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700765 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700766 }
767
768 // these are the underlying implementation's object
769 EGLContext impl_ctx = EGL_NO_CONTEXT;
770 EGLSurface impl_draw = EGL_NO_SURFACE;
771 EGLSurface impl_read = EGL_NO_SURFACE;
772
773 // these are our objects structs passed in
774 egl_context_t * c = NULL;
775 egl_surface_t const * d = NULL;
776 egl_surface_t const * r = NULL;
777
778 // these are the current objects structs
779 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800780
Mathias Agopian518ec112011-05-13 16:21:08 -0700781 if (ctx != EGL_NO_CONTEXT) {
782 c = get_context(ctx);
783 impl_ctx = c->context;
784 } else {
785 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700786 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
787 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
788 return setError(EGL_BAD_MATCH, EGL_FALSE);
789 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700790 if (cur_c == NULL) {
791 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700792 // not an error, there is just no current context.
793 return EGL_TRUE;
794 }
795 }
796
797 // retrieve the underlying implementation's draw EGLSurface
798 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700799 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700800 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700801 impl_draw = d->surface;
802 }
803
804 // retrieve the underlying implementation's read EGLSurface
805 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700806 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700807 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700808 impl_read = r->surface;
809 }
810
Mathias Agopian518ec112011-05-13 16:21:08 -0700811
Jesse Hallb29e5e82012-04-04 16:53:42 -0700812 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800813 draw, read, ctx,
814 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700815
816 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800817 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700818 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
819 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700820 _c.acquire();
821 _r.acquire();
822 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700823 } else {
824 setGLHooksThreadSpecific(&gHooksNoContext);
825 egl_tls_t::setContext(EGL_NO_CONTEXT);
826 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700827 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000828 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700829 egl_connection_t* const cnx = &gEGLImpl;
830 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700831 }
832 return result;
833}
834
835
836EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
837 EGLint attribute, EGLint *value)
838{
839 clearError();
840
Jesse Hallb29e5e82012-04-04 16:53:42 -0700841 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700842 if (!dp) return EGL_FALSE;
843
Jesse Hallb29e5e82012-04-04 16:53:42 -0700844 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700845 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
846
Mathias Agopian518ec112011-05-13 16:21:08 -0700847 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800848 return c->cnx->egl.eglQueryContext(
849 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700850
Mathias Agopian518ec112011-05-13 16:21:08 -0700851}
852
853EGLContext eglGetCurrentContext(void)
854{
855 // could be called before eglInitialize(), but we wouldn't have a context
856 // then, and this function would correctly return EGL_NO_CONTEXT.
857
858 clearError();
859
860 EGLContext ctx = getContext();
861 return ctx;
862}
863
864EGLSurface eglGetCurrentSurface(EGLint readdraw)
865{
866 // could be called before eglInitialize(), but we wouldn't have a context
867 // then, and this function would correctly return EGL_NO_SURFACE.
868
869 clearError();
870
871 EGLContext ctx = getContext();
872 if (ctx) {
873 egl_context_t const * const c = get_context(ctx);
874 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
875 switch (readdraw) {
876 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800877 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700878 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
879 }
880 }
881 return EGL_NO_SURFACE;
882}
883
884EGLDisplay eglGetCurrentDisplay(void)
885{
886 // could be called before eglInitialize(), but we wouldn't have a context
887 // then, and this function would correctly return EGL_NO_DISPLAY.
888
889 clearError();
890
891 EGLContext ctx = getContext();
892 if (ctx) {
893 egl_context_t const * const c = get_context(ctx);
894 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
895 return c->dpy;
896 }
897 return EGL_NO_DISPLAY;
898}
899
900EGLBoolean eglWaitGL(void)
901{
Mathias Agopian518ec112011-05-13 16:21:08 -0700902 clearError();
903
Mathias Agopianada798b2012-02-13 17:09:30 -0800904 egl_connection_t* const cnx = &gEGLImpl;
905 if (!cnx->dso)
906 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
907
908 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700909}
910
911EGLBoolean eglWaitNative(EGLint engine)
912{
Mathias Agopian518ec112011-05-13 16:21:08 -0700913 clearError();
914
Mathias Agopianada798b2012-02-13 17:09:30 -0800915 egl_connection_t* const cnx = &gEGLImpl;
916 if (!cnx->dso)
917 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
918
919 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700920}
921
922EGLint eglGetError(void)
923{
Mathias Agopianada798b2012-02-13 17:09:30 -0800924 EGLint err = EGL_SUCCESS;
925 egl_connection_t* const cnx = &gEGLImpl;
926 if (cnx->dso) {
927 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700928 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800929 if (err == EGL_SUCCESS) {
930 err = egl_tls_t::getError();
931 }
932 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700933}
934
Michael Chockc0ec5e22014-01-27 08:14:33 -0800935static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700936 const char* procname) {
937 const egl_connection_t* cnx = &gEGLImpl;
938 void* proc = NULL;
939
Michael Chockc0ec5e22014-01-27 08:14:33 -0800940 proc = dlsym(cnx->libEgl, procname);
941 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
942
Jesse Hallc07b5202013-07-04 12:08:16 -0700943 proc = dlsym(cnx->libGles2, procname);
944 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
945
946 proc = dlsym(cnx->libGles1, procname);
947 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
948
949 return NULL;
950}
951
Mathias Agopian518ec112011-05-13 16:21:08 -0700952__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
953{
954 // eglGetProcAddress() could be the very first function called
955 // in which case we must make sure we've initialized ourselves, this
956 // happens the first time egl_get_display() is called.
957
958 clearError();
959
960 if (egl_init_drivers() == EGL_FALSE) {
961 setError(EGL_BAD_PARAMETER, NULL);
962 return NULL;
963 }
964
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700965 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700966 return NULL;
967 }
968
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700970 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700971 if (addr) return addr;
972
Michael Chockc0ec5e22014-01-27 08:14:33 -0800973 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700974 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700975
Mathias Agopian518ec112011-05-13 16:21:08 -0700976 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
977 pthread_mutex_lock(&sExtensionMapMutex);
978
979 /*
980 * Since eglGetProcAddress() is not associated to anything, it needs
981 * to return a function pointer that "works" regardless of what
982 * the current context is.
983 *
984 * For this reason, we return a "forwarder", a small stub that takes
985 * care of calling the function associated with the context
986 * currently bound.
987 *
988 * We first look for extensions we've already resolved, if we're seeing
989 * this extension for the first time, we go through all our
990 * implementations and call eglGetProcAddress() and record the
991 * result in the appropriate implementation hooks and return the
992 * address of the forwarder corresponding to that hook set.
993 *
994 */
995
996 const String8 name(procname);
997 addr = sGLExtentionMap.valueFor(name);
998 const int slot = sGLExtentionSlot;
999
Steve Blocke6f43dd2012-01-06 19:20:56 +00001000 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001001 "no more slots for eglGetProcAddress(\"%s\")",
1002 procname);
1003
1004 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1005 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001006
1007 egl_connection_t* const cnx = &gEGLImpl;
1008 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001009 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001010 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001011 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1012 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001013 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001014 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001015 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001016
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 if (found) {
1018 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 sGLExtentionMap.add(name, addr);
1020 sGLExtentionSlot++;
1021 }
1022 }
1023
1024 pthread_mutex_unlock(&sExtensionMapMutex);
1025 return addr;
1026}
1027
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001028class FrameCompletionThread : public Thread {
1029public:
1030
1031 static void queueSync(EGLSyncKHR sync) {
1032 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1033 static bool running = false;
1034 if (!running) {
1035 thread->run("GPUFrameCompletion");
1036 running = true;
1037 }
1038 {
1039 Mutex::Autolock lock(thread->mMutex);
1040 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1041 thread->mFramesQueued).string());
1042 thread->mQueue.push_back(sync);
1043 thread->mCondition.signal();
1044 thread->mFramesQueued++;
1045 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1046 }
1047 }
1048
1049private:
1050 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1051
1052 virtual bool threadLoop() {
1053 EGLSyncKHR sync;
1054 uint32_t frameNum;
1055 {
1056 Mutex::Autolock lock(mMutex);
1057 while (mQueue.isEmpty()) {
1058 mCondition.wait(mMutex);
1059 }
1060 sync = mQueue[0];
1061 frameNum = mFramesCompleted;
1062 }
1063 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1064 {
1065 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1066 frameNum).string());
1067 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1068 if (result == EGL_FALSE) {
1069 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1070 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1071 ALOGE("FrameCompletion: timeout waiting for fence");
1072 }
1073 eglDestroySyncKHR(dpy, sync);
1074 }
1075 {
1076 Mutex::Autolock lock(mMutex);
1077 mQueue.removeAt(0);
1078 mFramesCompleted++;
1079 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1080 }
1081 return true;
1082 }
1083
1084 uint32_t mFramesQueued;
1085 uint32_t mFramesCompleted;
1086 Vector<EGLSyncKHR> mQueue;
1087 Condition mCondition;
1088 Mutex mMutex;
1089};
1090
Dan Stozaa894d082015-02-19 15:27:36 -08001091EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1092 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001093{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001094 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001095 clearError();
1096
Jesse Hallb29e5e82012-04-04 16:53:42 -07001097 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001098 if (!dp) return EGL_FALSE;
1099
Jesse Hallb29e5e82012-04-04 16:53:42 -07001100 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001101 if (!_s.get())
1102 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001103
Mathias Agopian518ec112011-05-13 16:21:08 -07001104 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001105
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001106 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1107 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1108 if (sync != EGL_NO_SYNC_KHR) {
1109 FrameCompletionThread::queueSync(sync);
1110 }
1111 }
1112
Mathias Agopian7db993a2012-03-25 00:49:46 -07001113 if (CC_UNLIKELY(dp->finishOnSwap)) {
1114 uint32_t pixel;
1115 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1116 if (c) {
1117 // glReadPixels() ensures that the frame is complete
1118 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1119 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1120 }
1121 }
1122
Dan Stozaa894d082015-02-19 15:27:36 -08001123 if (n_rects == 0) {
1124 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1125 }
1126
1127 Vector<android_native_rect_t> androidRects;
1128 for (int r = 0; r < n_rects; ++r) {
1129 int offset = r * 4;
1130 int x = rects[offset];
1131 int y = rects[offset + 1];
1132 int width = rects[offset + 2];
1133 int height = rects[offset + 3];
1134 android_native_rect_t androidRect;
1135 androidRect.left = x;
1136 androidRect.top = y + height;
1137 androidRect.right = x + width;
1138 androidRect.bottom = y;
1139 androidRects.push_back(androidRect);
1140 }
1141 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1142 androidRects.size());
1143
1144 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1145 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1146 rects, n_rects);
1147 } else {
1148 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1149 }
1150}
1151
1152EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1153{
1154 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001155}
1156
1157EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1158 NativePixmapType target)
1159{
1160 clearError();
1161
Jesse Hallb29e5e82012-04-04 16:53:42 -07001162 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001163 if (!dp) return EGL_FALSE;
1164
Jesse Hallb29e5e82012-04-04 16:53:42 -07001165 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001166 if (!_s.get())
1167 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001168
Mathias Agopian518ec112011-05-13 16:21:08 -07001169 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001170 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001171}
1172
1173const char* eglQueryString(EGLDisplay dpy, EGLint name)
1174{
1175 clearError();
1176
Chia-I Wue57d1352016-08-15 16:10:02 +08001177 // Generate an error quietly when client extensions (as defined by
1178 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1179 // validate_display to generate the error as validate_display would log
1180 // the error, which can be misleading.
1181 //
1182 // If we want to support EGL_EXT_client_extensions later, we can return
1183 // the client extension string here instead.
1184 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1185 return setErrorQuiet(EGL_BAD_DISPLAY, nullptr);
1186
Jesse Hallb29e5e82012-04-04 16:53:42 -07001187 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001188 if (!dp) return (const char *) NULL;
1189
1190 switch (name) {
1191 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001192 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001193 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001194 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001195 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001196 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001197 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001198 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001199 }
1200 return setError(EGL_BAD_PARAMETER, (const char *)0);
1201}
1202
Mathias Agopianca088332013-03-28 17:44:13 -07001203EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1204{
1205 clearError();
1206
1207 const egl_display_ptr dp = validate_display(dpy);
1208 if (!dp) return (const char *) NULL;
1209
1210 switch (name) {
1211 case EGL_VENDOR:
1212 return dp->disp.queryString.vendor;
1213 case EGL_VERSION:
1214 return dp->disp.queryString.version;
1215 case EGL_EXTENSIONS:
1216 return dp->disp.queryString.extensions;
1217 case EGL_CLIENT_APIS:
1218 return dp->disp.queryString.clientApi;
1219 }
1220 return setError(EGL_BAD_PARAMETER, (const char *)0);
1221}
Mathias Agopian518ec112011-05-13 16:21:08 -07001222
1223// ----------------------------------------------------------------------------
1224// EGL 1.1
1225// ----------------------------------------------------------------------------
1226
1227EGLBoolean eglSurfaceAttrib(
1228 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1229{
1230 clearError();
1231
Jesse Hallb29e5e82012-04-04 16:53:42 -07001232 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001233 if (!dp) return EGL_FALSE;
1234
Jesse Hallb29e5e82012-04-04 16:53:42 -07001235 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001236 if (!_s.get())
1237 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001238
Pablo Ceballosc18be292016-05-31 14:55:42 -07001239 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001240
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001241 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001242 if (!s->win.get()) {
1243 setError(EGL_BAD_SURFACE, EGL_FALSE);
1244 }
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001245 int err = native_window_set_auto_refresh(s->win.get(),
1246 value ? true : false);
1247 return (err == NO_ERROR) ? EGL_TRUE :
1248 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001249 }
1250
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001251#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001252 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001253 if (!s->win.get()) {
1254 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1255 }
Brian Anderson069b3652016-07-22 10:32:47 -07001256 int err = native_window_enable_frame_timestamps(
1257 s->win.get(), value ? true : false);
1258 return (err == NO_ERROR) ? EGL_TRUE :
1259 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001260 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001261#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001262
Mathias Agopian518ec112011-05-13 16:21:08 -07001263 if (s->cnx->egl.eglSurfaceAttrib) {
1264 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001265 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001266 }
1267 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1268}
1269
1270EGLBoolean eglBindTexImage(
1271 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1272{
1273 clearError();
1274
Jesse Hallb29e5e82012-04-04 16:53:42 -07001275 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001276 if (!dp) return EGL_FALSE;
1277
Jesse Hallb29e5e82012-04-04 16:53:42 -07001278 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001279 if (!_s.get())
1280 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001281
Mathias Agopian518ec112011-05-13 16:21:08 -07001282 egl_surface_t const * const s = get_surface(surface);
1283 if (s->cnx->egl.eglBindTexImage) {
1284 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001285 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001286 }
1287 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1288}
1289
1290EGLBoolean eglReleaseTexImage(
1291 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1292{
1293 clearError();
1294
Jesse Hallb29e5e82012-04-04 16:53:42 -07001295 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001296 if (!dp) return EGL_FALSE;
1297
Jesse Hallb29e5e82012-04-04 16:53:42 -07001298 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001299 if (!_s.get())
1300 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001301
Mathias Agopian518ec112011-05-13 16:21:08 -07001302 egl_surface_t const * const s = get_surface(surface);
1303 if (s->cnx->egl.eglReleaseTexImage) {
1304 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001305 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001306 }
1307 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1308}
1309
1310EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1311{
1312 clearError();
1313
Jesse Hallb29e5e82012-04-04 16:53:42 -07001314 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001315 if (!dp) return EGL_FALSE;
1316
1317 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001318 egl_connection_t* const cnx = &gEGLImpl;
1319 if (cnx->dso && cnx->egl.eglSwapInterval) {
1320 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001321 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001322
Mathias Agopian518ec112011-05-13 16:21:08 -07001323 return res;
1324}
1325
1326
1327// ----------------------------------------------------------------------------
1328// EGL 1.2
1329// ----------------------------------------------------------------------------
1330
1331EGLBoolean eglWaitClient(void)
1332{
1333 clearError();
1334
Mathias Agopianada798b2012-02-13 17:09:30 -08001335 egl_connection_t* const cnx = &gEGLImpl;
1336 if (!cnx->dso)
1337 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1338
1339 EGLBoolean res;
1340 if (cnx->egl.eglWaitClient) {
1341 res = cnx->egl.eglWaitClient();
1342 } else {
1343 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001344 }
1345 return res;
1346}
1347
1348EGLBoolean eglBindAPI(EGLenum api)
1349{
1350 clearError();
1351
1352 if (egl_init_drivers() == EGL_FALSE) {
1353 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1354 }
1355
1356 // bind this API on all EGLs
1357 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001358 egl_connection_t* const cnx = &gEGLImpl;
1359 if (cnx->dso && cnx->egl.eglBindAPI) {
1360 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001361 }
1362 return res;
1363}
1364
1365EGLenum eglQueryAPI(void)
1366{
1367 clearError();
1368
1369 if (egl_init_drivers() == EGL_FALSE) {
1370 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1371 }
1372
Mathias Agopianada798b2012-02-13 17:09:30 -08001373 egl_connection_t* const cnx = &gEGLImpl;
1374 if (cnx->dso && cnx->egl.eglQueryAPI) {
1375 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001376 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001377
Mathias Agopian518ec112011-05-13 16:21:08 -07001378 // or, it can only be OpenGL ES
1379 return EGL_OPENGL_ES_API;
1380}
1381
1382EGLBoolean eglReleaseThread(void)
1383{
1384 clearError();
1385
Mathias Agopianada798b2012-02-13 17:09:30 -08001386 egl_connection_t* const cnx = &gEGLImpl;
1387 if (cnx->dso && cnx->egl.eglReleaseThread) {
1388 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001389 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301390
1391 // If there is context bound to the thread, release it
1392 egl_display_t::loseCurrent(get_context(getContext()));
1393
Mathias Agopian518ec112011-05-13 16:21:08 -07001394 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001395 return EGL_TRUE;
1396}
1397
1398EGLSurface eglCreatePbufferFromClientBuffer(
1399 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1400 EGLConfig config, const EGLint *attrib_list)
1401{
1402 clearError();
1403
Jesse Hallb29e5e82012-04-04 16:53:42 -07001404 egl_connection_t* cnx = NULL;
1405 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1406 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001407 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1408 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001409 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001410 }
1411 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1412}
1413
1414// ----------------------------------------------------------------------------
1415// EGL_EGLEXT_VERSION 3
1416// ----------------------------------------------------------------------------
1417
1418EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1419 const EGLint *attrib_list)
1420{
1421 clearError();
1422
Jesse Hallb29e5e82012-04-04 16:53:42 -07001423 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001424 if (!dp) return EGL_FALSE;
1425
Jesse Hallb29e5e82012-04-04 16:53:42 -07001426 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001427 if (!_s.get())
1428 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001429
1430 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001431 if (s->cnx->egl.eglLockSurfaceKHR) {
1432 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001433 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001434 }
1435 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1436}
1437
1438EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1439{
1440 clearError();
1441
Jesse Hallb29e5e82012-04-04 16:53:42 -07001442 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001443 if (!dp) return EGL_FALSE;
1444
Jesse Hallb29e5e82012-04-04 16:53:42 -07001445 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001446 if (!_s.get())
1447 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001448
1449 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001451 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001452 }
1453 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1454}
1455
1456EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1457 EGLClientBuffer buffer, const EGLint *attrib_list)
1458{
1459 clearError();
1460
Jesse Hallb29e5e82012-04-04 16:53:42 -07001461 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001462 if (!dp) return EGL_NO_IMAGE_KHR;
1463
Jesse Hallb29e5e82012-04-04 16:53:42 -07001464 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001465 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001466
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001467 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1468 egl_connection_t* const cnx = &gEGLImpl;
1469 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1470 result = cnx->egl.eglCreateImageKHR(
1471 dp->disp.dpy,
1472 c ? c->context : EGL_NO_CONTEXT,
1473 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001474 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001475 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001476}
1477
1478EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1479{
1480 clearError();
1481
Jesse Hallb29e5e82012-04-04 16:53:42 -07001482 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001483 if (!dp) return EGL_FALSE;
1484
Steven Holte646a5c52012-06-04 20:02:11 -07001485 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001486 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001487 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001488 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001489 }
Steven Holte646a5c52012-06-04 20:02:11 -07001490 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001491}
1492
1493// ----------------------------------------------------------------------------
1494// EGL_EGLEXT_VERSION 5
1495// ----------------------------------------------------------------------------
1496
1497
1498EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1499{
1500 clearError();
1501
Jesse Hallb29e5e82012-04-04 16:53:42 -07001502 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 if (!dp) return EGL_NO_SYNC_KHR;
1504
Mathias Agopian518ec112011-05-13 16:21:08 -07001505 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001506 egl_connection_t* const cnx = &gEGLImpl;
1507 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1508 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001509 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001510 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001511}
1512
1513EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1514{
1515 clearError();
1516
Jesse Hallb29e5e82012-04-04 16:53:42 -07001517 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001518 if (!dp) return EGL_FALSE;
1519
Mathias Agopian518ec112011-05-13 16:21:08 -07001520 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001521 egl_connection_t* const cnx = &gEGLImpl;
1522 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1523 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001524 }
1525 return result;
1526}
1527
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001528EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1529 clearError();
1530
1531 const egl_display_ptr dp = validate_display(dpy);
1532 if (!dp) return EGL_FALSE;
1533
1534 EGLBoolean result = EGL_FALSE;
1535 egl_connection_t* const cnx = &gEGLImpl;
1536 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1537 result = cnx->egl.eglSignalSyncKHR(
1538 dp->disp.dpy, sync, mode);
1539 }
1540 return result;
1541}
1542
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001543EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1544 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001545{
1546 clearError();
1547
Jesse Hallb29e5e82012-04-04 16:53:42 -07001548 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001549 if (!dp) return EGL_FALSE;
1550
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001551 EGLBoolean result = EGL_FALSE;
1552 egl_connection_t* const cnx = &gEGLImpl;
1553 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1554 result = cnx->egl.eglClientWaitSyncKHR(
1555 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001556 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001557 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001558}
1559
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001560EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1561 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001562{
1563 clearError();
1564
Jesse Hallb29e5e82012-04-04 16:53:42 -07001565 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001566 if (!dp) return EGL_FALSE;
1567
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001568 EGLBoolean result = EGL_FALSE;
1569 egl_connection_t* const cnx = &gEGLImpl;
1570 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1571 result = cnx->egl.eglGetSyncAttribKHR(
1572 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001573 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001574 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001575}
1576
Season Li000d88f2015-07-01 11:39:40 -07001577EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1578{
1579 clearError();
1580
1581 const egl_display_ptr dp = validate_display(dpy);
1582 if (!dp) return EGL_NO_STREAM_KHR;
1583
1584 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1585 egl_connection_t* const cnx = &gEGLImpl;
1586 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1587 result = cnx->egl.eglCreateStreamKHR(
1588 dp->disp.dpy, attrib_list);
1589 }
1590 return result;
1591}
1592
1593EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1594{
1595 clearError();
1596
1597 const egl_display_ptr dp = validate_display(dpy);
1598 if (!dp) return EGL_FALSE;
1599
1600 EGLBoolean result = EGL_FALSE;
1601 egl_connection_t* const cnx = &gEGLImpl;
1602 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1603 result = cnx->egl.eglDestroyStreamKHR(
1604 dp->disp.dpy, stream);
1605 }
1606 return result;
1607}
1608
1609EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1610 EGLenum attribute, EGLint value)
1611{
1612 clearError();
1613
1614 const egl_display_ptr dp = validate_display(dpy);
1615 if (!dp) return EGL_FALSE;
1616
1617 EGLBoolean result = EGL_FALSE;
1618 egl_connection_t* const cnx = &gEGLImpl;
1619 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1620 result = cnx->egl.eglStreamAttribKHR(
1621 dp->disp.dpy, stream, attribute, value);
1622 }
1623 return result;
1624}
1625
1626EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1627 EGLenum attribute, EGLint *value)
1628{
1629 clearError();
1630
1631 const egl_display_ptr dp = validate_display(dpy);
1632 if (!dp) return EGL_FALSE;
1633
1634 EGLBoolean result = EGL_FALSE;
1635 egl_connection_t* const cnx = &gEGLImpl;
1636 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1637 result = cnx->egl.eglQueryStreamKHR(
1638 dp->disp.dpy, stream, attribute, value);
1639 }
1640 return result;
1641}
1642
1643EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1644 EGLenum attribute, EGLuint64KHR *value)
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.eglQueryStreamu64KHR) {
1654 result = cnx->egl.eglQueryStreamu64KHR(
1655 dp->disp.dpy, stream, attribute, value);
1656 }
1657 return result;
1658}
1659
1660EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1661 EGLenum attribute, EGLTimeKHR *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.eglQueryStreamTimeKHR) {
1671 result = cnx->egl.eglQueryStreamTimeKHR(
1672 dp->disp.dpy, stream, attribute, value);
1673 }
1674 return result;
1675}
1676
1677EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1678 EGLStreamKHR stream, const EGLint *attrib_list)
1679{
1680 clearError();
1681
1682 egl_display_ptr dp = validate_display(dpy);
1683 if (!dp) return EGL_NO_SURFACE;
1684
1685 egl_connection_t* const cnx = &gEGLImpl;
1686 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1687 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1688 dp->disp.dpy, config, stream, attrib_list);
1689 if (surface != EGL_NO_SURFACE) {
1690 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1691 surface, cnx);
1692 return s;
1693 }
1694 }
1695 return EGL_NO_SURFACE;
1696}
1697
1698EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1699 EGLStreamKHR stream)
1700{
1701 clearError();
1702
1703 const egl_display_ptr dp = validate_display(dpy);
1704 if (!dp) return EGL_FALSE;
1705
1706 EGLBoolean result = EGL_FALSE;
1707 egl_connection_t* const cnx = &gEGLImpl;
1708 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1709 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1710 dp->disp.dpy, stream);
1711 }
1712 return result;
1713}
1714
1715EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1716 EGLStreamKHR stream)
1717{
1718 clearError();
1719
1720 const egl_display_ptr dp = validate_display(dpy);
1721 if (!dp) return EGL_FALSE;
1722
1723 EGLBoolean result = EGL_FALSE;
1724 egl_connection_t* const cnx = &gEGLImpl;
1725 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1726 result = cnx->egl.eglStreamConsumerAcquireKHR(
1727 dp->disp.dpy, stream);
1728 }
1729 return result;
1730}
1731
1732EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1733 EGLStreamKHR stream)
1734{
1735 clearError();
1736
1737 const egl_display_ptr dp = validate_display(dpy);
1738 if (!dp) return EGL_FALSE;
1739
1740 EGLBoolean result = EGL_FALSE;
1741 egl_connection_t* const cnx = &gEGLImpl;
1742 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1743 result = cnx->egl.eglStreamConsumerReleaseKHR(
1744 dp->disp.dpy, stream);
1745 }
1746 return result;
1747}
1748
1749EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1750 EGLDisplay dpy, EGLStreamKHR stream)
1751{
1752 clearError();
1753
1754 const egl_display_ptr dp = validate_display(dpy);
1755 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1756
1757 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1758 egl_connection_t* const cnx = &gEGLImpl;
1759 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1760 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1761 dp->disp.dpy, stream);
1762 }
1763 return result;
1764}
1765
1766EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1767 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1768{
1769 clearError();
1770
1771 const egl_display_ptr dp = validate_display(dpy);
1772 if (!dp) return EGL_NO_STREAM_KHR;
1773
1774 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1775 egl_connection_t* const cnx = &gEGLImpl;
1776 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1777 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1778 dp->disp.dpy, file_descriptor);
1779 }
1780 return result;
1781}
1782
Mathias Agopian518ec112011-05-13 16:21:08 -07001783// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001784// EGL_EGLEXT_VERSION 15
1785// ----------------------------------------------------------------------------
1786
1787EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1788 clearError();
1789 const egl_display_ptr dp = validate_display(dpy);
1790 if (!dp) return EGL_FALSE;
1791 EGLint result = EGL_FALSE;
1792 egl_connection_t* const cnx = &gEGLImpl;
1793 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1794 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1795 }
1796 return result;
1797}
1798
1799// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001800// ANDROID extensions
1801// ----------------------------------------------------------------------------
1802
Jamie Gennis331841b2012-09-06 14:52:00 -07001803EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1804{
1805 clearError();
1806
1807 const egl_display_ptr dp = validate_display(dpy);
1808 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1809
1810 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1811 egl_connection_t* const cnx = &gEGLImpl;
1812 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1813 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1814 }
1815 return result;
1816}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001817
Andy McFadden72841452013-03-01 16:25:32 -08001818EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1819 EGLnsecsANDROID time)
1820{
1821 clearError();
1822
1823 const egl_display_ptr dp = validate_display(dpy);
1824 if (!dp) {
1825 return EGL_FALSE;
1826 }
1827
1828 SurfaceRef _s(dp.get(), surface);
1829 if (!_s.get()) {
1830 setError(EGL_BAD_SURFACE, EGL_FALSE);
1831 return EGL_FALSE;
1832 }
1833
1834 egl_surface_t const * const s = get_surface(surface);
1835 native_window_set_buffers_timestamp(s->win.get(), time);
1836
1837 return EGL_TRUE;
1838}
1839
Craig Donner05249fc2016-01-15 19:33:55 -08001840EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1841{
1842 clearError();
1843
Craig Donnere96a3252017-02-02 12:13:34 -08001844 uint64_t producerUsage = 0;
1845 uint64_t consumerUsage = 0;
Craig Donner05249fc2016-01-15 19:33:55 -08001846 uint32_t width = 0;
1847 uint32_t height = 0;
1848 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001849 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001850 uint32_t red_size = 0;
1851 uint32_t green_size = 0;
1852 uint32_t blue_size = 0;
1853 uint32_t alpha_size = 0;
1854
Craig Donnerb40504a2016-06-03 17:54:25 -07001855#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001856 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001857 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001858 target = value; \
1859 } else { \
1860 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1861 } \
1862 break
1863
1864 if (attrib_list) {
1865 while (*attrib_list != EGL_NONE) {
1866 GLint attr = *attrib_list++;
1867 GLint value = *attrib_list++;
1868 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001869 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1870 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1871 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1872 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1873 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1874 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001875 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001876 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1877 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001878 producerUsage |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001879 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001880 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001881 producerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
Craig Donner05249fc2016-01-15 19:33:55 -08001882 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001883 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001884 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
Craig Donner05249fc2016-01-15 19:33:55 -08001885 }
Craig Donner05249fc2016-01-15 19:33:55 -08001886 break;
1887 default:
1888 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1889 }
1890 }
1891 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001892#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001893
1894 // Validate format.
1895 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1896 if (alpha_size == 8) {
1897 format = HAL_PIXEL_FORMAT_RGBA_8888;
1898 } else {
1899 format = HAL_PIXEL_FORMAT_RGB_888;
1900 }
1901 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1902 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001903 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001904 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001905 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001906 red_size, green_size, blue_size, alpha_size);
1907 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1908 }
1909
Craig Donner68671532016-07-18 10:19:54 -07001910#define CHECK_ERROR_CONDITION(message) \
1911 if (err != NO_ERROR) { \
1912 ALOGE(message); \
1913 goto error_condition; \
1914 }
1915
1916 // The holder is used to destroy the buffer if an error occurs.
1917 GraphicBuffer* gBuffer = new GraphicBuffer();
1918 sp<IServiceManager> sm = defaultServiceManager();
1919 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1920 sp<IBinder> allocator;
1921 Parcel sc_data, sc_reply, data, reply;
1922 status_t err = NO_ERROR;
1923 if (sm == NULL) {
1924 ALOGE("Unable to connect to ServiceManager");
1925 goto error_condition;
1926 }
1927
1928 // Obtain an allocator.
1929 if (surfaceFlinger == NULL) {
1930 ALOGE("Unable to connect to SurfaceFlinger");
1931 goto error_condition;
1932 }
1933 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1934 err = surfaceFlinger->transact(
1935 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1936 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1937 allocator = sc_reply.readStrongBinder();
1938
1939 if (allocator == NULL) {
1940 ALOGE("Unable to obtain an ISurfaceComposer");
1941 goto error_condition;
1942 }
1943 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1944 err = data.writeUint32(width);
1945 CHECK_ERROR_CONDITION("Unable to write width");
1946 err = data.writeUint32(height);
1947 CHECK_ERROR_CONDITION("Unable to write height");
1948 err = data.writeInt32(static_cast<int32_t>(format));
1949 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001950 err = data.writeUint32(layer_count);
1951 CHECK_ERROR_CONDITION("Unable to write layer count");
Craig Donnere96a3252017-02-02 12:13:34 -08001952 err = data.writeUint64(producerUsage);
1953 CHECK_ERROR_CONDITION("Unable to write producer usage");
1954 err = data.writeUint64(consumerUsage);
1955 CHECK_ERROR_CONDITION("Unable to write consumer usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001956 err = data.writeUtf8AsUtf16(
1957 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1958 std::to_string(getpid()) + ']');
1959 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001960 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1961 &reply);
1962 CHECK_ERROR_CONDITION(
1963 "Unable to request buffer allocation from surface composer");
1964 err = reply.readInt32();
1965 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1966 err = reply.read(*gBuffer);
1967 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1968
1969 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001970 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001971 ALOGE("Unable to create native buffer "
Craig Donnere96a3252017-02-02 12:13:34 -08001972 "{ w=%u, h=%u, f=%u, pu=%" PRIx64 " cu=%" PRIx64 ", lc=%u} %#x",
1973 width, height, format, producerUsage, consumerUsage,
1974 layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001975 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001976 }
Craig Donnere96a3252017-02-02 12:13:34 -08001977 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, pu=%" PRIx64
1978 " cu=%" PRIx64 ", lc=%u}",
1979 gBuffer, width, height, format, producerUsage, consumerUsage,
1980 layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001981 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001982
1983#undef CHECK_ERROR_CONDITION
1984
1985error_condition:
1986 // Delete the buffer.
1987 sp<GraphicBuffer> holder(gBuffer);
1988 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001989}
1990
Craig Donner60761072017-01-27 12:30:44 -08001991EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1992 clearError();
1993
1994 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1995
1996 // FIXME: remove this dangerous reinterpret_cast.
1997 const GraphicBuffer* graphicBuffer =
1998 reinterpret_cast<const GraphicBuffer*>(buffer);
1999 return static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
2000}
2001
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002002// ----------------------------------------------------------------------------
2003// NVIDIA extensions
2004// ----------------------------------------------------------------------------
2005EGLuint64NV eglGetSystemTimeFrequencyNV()
2006{
2007 clearError();
2008
2009 if (egl_init_drivers() == EGL_FALSE) {
2010 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2011 }
2012
2013 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002014 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002015
Mathias Agopianada798b2012-02-13 17:09:30 -08002016 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2017 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002018 }
2019
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07002020 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002021}
2022
2023EGLuint64NV eglGetSystemTimeNV()
2024{
2025 clearError();
2026
2027 if (egl_init_drivers() == EGL_FALSE) {
2028 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2029 }
2030
2031 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002032 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002033
Mathias Agopianada798b2012-02-13 17:09:30 -08002034 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2035 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002036 }
2037
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07002038 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002039}
Dan Stozaa894d082015-02-19 15:27:36 -08002040
2041// ----------------------------------------------------------------------------
2042// Partial update extension
2043// ----------------------------------------------------------------------------
2044EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2045 EGLint *rects, EGLint n_rects)
2046{
2047 clearError();
2048
2049 const egl_display_ptr dp = validate_display(dpy);
2050 if (!dp) {
2051 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2052 return EGL_FALSE;
2053 }
2054
2055 SurfaceRef _s(dp.get(), surface);
2056 if (!_s.get()) {
2057 setError(EGL_BAD_SURFACE, EGL_FALSE);
2058 return EGL_FALSE;
2059 }
2060
2061 egl_surface_t const * const s = get_surface(surface);
2062 if (s->cnx->egl.eglSetDamageRegionKHR) {
2063 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2064 rects, n_rects);
2065 }
2066
2067 return EGL_FALSE;
2068}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002069
Brian Anderson1049d1d2016-12-16 17:25:57 -08002070EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2071 EGLuint64KHR *frameId) {
2072 clearError();
2073
2074 const egl_display_ptr dp = validate_display(dpy);
2075 if (!dp) {
2076 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2077 }
2078
2079 SurfaceRef _s(dp.get(), surface);
2080 if (!_s.get()) {
2081 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2082 }
2083
2084 egl_surface_t const * const s = get_surface(surface);
2085
2086 if (!s->win.get()) {
2087 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2088 }
2089
2090 uint64_t nextFrameId = 0;
2091 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2092
2093 if (ret != NO_ERROR) {
2094 // This should not happen. Return an error that is not in the spec
2095 // so it's obvious something is very wrong.
2096 ALOGE("eglGetNextFrameId: Unexpected error.");
2097 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
2098 }
2099
2100 *frameId = nextFrameId;
2101 return EGL_TRUE;
2102}
2103
Pablo Ceballosc18be292016-05-31 14:55:42 -07002104EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002105 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002106 EGLnsecsANDROID *values)
2107{
2108 clearError();
2109
2110 const egl_display_ptr dp = validate_display(dpy);
2111 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002112 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002113 }
2114
2115 SurfaceRef _s(dp.get(), surface);
2116 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002117 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002118 }
2119
2120 egl_surface_t const * const s = get_surface(surface);
2121
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002122 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002123 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002124 }
2125
Brian Andersondbd0ea82016-07-22 09:38:59 -07002126 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002127 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002128 nsecs_t* latchTime = nullptr;
2129 nsecs_t* firstRefreshStartTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002130 nsecs_t* GLCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002131 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002132 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002133 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002134 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002135 nsecs_t* releaseTime = nullptr;
2136
2137 for (int i = 0; i < numTimestamps; i++) {
2138 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002139 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2140 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002141 break;
2142 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2143 acquireTime = &values[i];
2144 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002145 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2146 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002147 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002148 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2149 firstRefreshStartTime = &values[i];
2150 break;
2151 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2152 lastRefreshStartTime = &values[i];
2153 break;
2154 case EGL_FIRST_COMPOSITION_FINISHED_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002155 GLCompositionDoneTime = &values[i];
2156 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002157 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2158 displayPresentTime = &values[i];
2159 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002160 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2161 displayRetireTime = &values[i];
2162 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002163 case EGL_DEQUEUE_READY_TIME_ANDROID:
2164 dequeueReadyTime = &values[i];
2165 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002166 case EGL_READS_DONE_TIME_ANDROID:
2167 releaseTime = &values[i];
2168 break;
2169 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002170 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002171 }
2172 }
2173
Brian Anderson1049d1d2016-12-16 17:25:57 -08002174 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002175 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
2176 lastRefreshStartTime, GLCompositionDoneTime, displayPresentTime,
2177 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002178
Brian Anderson069b3652016-07-22 10:32:47 -07002179 switch (ret) {
2180 case NO_ERROR:
2181 return EGL_TRUE;
2182 case NAME_NOT_FOUND:
2183 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002184 case INVALID_OPERATION:
2185 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002186 case BAD_VALUE:
2187 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2188 default:
2189 // This should not happen. Return an error that is not in the spec
2190 // so it's obvious something is very wrong.
Brian Anderson1049d1d2016-12-16 17:25:57 -08002191 ALOGE("eglGetFrameTimestamps: Unexpected error.");
Brian Anderson069b3652016-07-22 10:32:47 -07002192 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002193 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002194}
2195
2196EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2197 EGLint timestamp)
2198{
2199 clearError();
2200
2201 const egl_display_ptr dp = validate_display(dpy);
2202 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002203 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002204 }
2205
2206 SurfaceRef _s(dp.get(), surface);
2207 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002208 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2209 }
2210
2211 egl_surface_t const * const s = get_surface(surface);
2212
2213 ANativeWindow* window = s->win.get();
2214 if (!window) {
2215 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002216 }
2217
2218 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002219#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Brian Andersondbd0ea82016-07-22 09:38:59 -07002220 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002221 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002222 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2223 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2224 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2225 case EGL_FIRST_COMPOSITION_FINISHED_TIME_ANDROID:
2226 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002227 case EGL_READS_DONE_TIME_ANDROID:
2228 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002229 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2230 int value = 0;
2231 window->query(window,
2232 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2233 return value == 0 ? EGL_FALSE : EGL_TRUE;
2234 }
2235 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2236 int value = 0;
2237 window->query(window,
2238 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2239 return value == 0 ? EGL_FALSE : EGL_TRUE;
2240 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002241#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002242 default:
2243 return EGL_FALSE;
2244 }
2245}