blob: f15c29d4ad84cff5655da7b130c9b020ae5865d4 [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
60// ----------------------------------------------------------------------------
61
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070062namespace android {
63
Mathias Agopian518ec112011-05-13 16:21:08 -070064struct extention_map_t {
65 const char* name;
66 __eglMustCastToProperFunctionPointerType address;
67};
68
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070069/*
Jesse Hall21558da2013-08-06 15:31:22 -070070 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070071 *
Jesse Hall21558da2013-08-06 15:31:22 -070072 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
73 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070074 *
Jesse Hall21558da2013-08-06 15:31:22 -070075 * The rest (gExtensionString) depend on support in the EGL driver, and are
76 * only available if the driver supports them. However, some of these must be
77 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080078 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070079 * the mandatory extensions are present and may not function properly if some
80 * are missing.
81 *
82 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070083 */
Jesse Hall21558da2013-08-06 15:31:22 -070084extern char const * const gBuiltinExtensionString =
85 "EGL_KHR_get_all_proc_addresses "
86 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080087 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080088 "EGL_ANDROID_create_native_client_buffer "
Craig Donner60761072017-01-27 12:30:44 -080089 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080090 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070091 "EGL_ANDROID_get_frame_timestamps "
Jesse Hall21558da2013-08-06 15:31:22 -070092 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070093extern char const * const gExtensionString =
94 "EGL_KHR_image " // mandatory
95 "EGL_KHR_image_base " // mandatory
96 "EGL_KHR_image_pixmap "
97 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -070098 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070099 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700100 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700101 "EGL_KHR_gl_texture_cubemap_image "
102 "EGL_KHR_gl_renderbuffer_image "
103 "EGL_KHR_reusable_sync "
104 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700105 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700106 "EGL_KHR_config_attribs "
107 "EGL_KHR_surfaceless_context "
108 "EGL_KHR_stream "
109 "EGL_KHR_stream_fifo "
110 "EGL_KHR_stream_producer_eglsurface "
111 "EGL_KHR_stream_consumer_gltexture "
112 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700113 "EGL_EXT_create_context_robustness "
114 "EGL_NV_system_time "
115 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700116 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700117 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800118 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700119 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800120 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700121 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700122 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700123 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700124 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000125 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700126 ;
127
128// extensions not exposed to applications but used by the ANDROID system
129// "EGL_ANDROID_blob_cache " // strongly recommended
130// "EGL_IMG_hibernate_process " // optional
131// "EGL_ANDROID_native_fence_sync " // strongly recommended
132// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700133// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700134
135/*
136 * EGL Extensions entry-points exposed to 3rd party applications
137 * (keep in sync with gExtensionString above)
138 *
139 */
140static const extention_map_t sExtensionMap[] = {
141 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700142 { "eglLockSurfaceKHR",
143 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
144 { "eglUnlockSurfaceKHR",
145 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700146
147 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700148 { "eglCreateImageKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
150 { "eglDestroyImageKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700152
153 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
154 { "eglCreateSyncKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
156 { "eglDestroySyncKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
158 { "eglClientWaitSyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
160 { "eglSignalSyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
162 { "eglGetSyncAttribKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
164
165 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800166 { "eglGetSystemTimeFrequencyNV",
167 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
168 { "eglGetSystemTimeNV",
169 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700170
Mathias Agopian2bb71682013-03-27 17:32:41 -0700171 // EGL_KHR_wait_sync
172 { "eglWaitSyncKHR",
173 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700174
175 // EGL_ANDROID_presentation_time
176 { "eglPresentationTimeANDROID",
177 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800178
179 // EGL_KHR_swap_buffers_with_damage
180 { "eglSwapBuffersWithDamageKHR",
181 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
182
Craig Donner60761072017-01-27 12:30:44 -0800183 // EGL_ANDROID_create_native_client_buffer
Craig Donner05249fc2016-01-15 19:33:55 -0800184 { "eglCreateNativeClientBufferANDROID",
185 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
186
Craig Donner60761072017-01-27 12:30:44 -0800187 // EGL_ANDROID_get_native_client_buffer
188 { "eglGetNativeClientBufferANDROID",
189 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
190
Dan Stozaa894d082015-02-19 15:27:36 -0800191 // EGL_KHR_partial_update
192 { "eglSetDamageRegionKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700194
195 { "eglCreateStreamKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
197 { "eglDestroyStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
199 { "eglStreamAttribKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
201 { "eglQueryStreamKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
203 { "eglQueryStreamu64KHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
205 { "eglQueryStreamTimeKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
207 { "eglCreateStreamProducerSurfaceKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
209 { "eglStreamConsumerGLTextureExternalKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
211 { "eglStreamConsumerAcquireKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
213 { "eglStreamConsumerReleaseKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
215 { "eglGetStreamFileDescriptorKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
217 { "eglCreateStreamFromFileDescriptorKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700219
220 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800221 { "eglGetNextFrameIdANDROID",
222 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800223 { "eglGetCompositorTimingANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
225 { "eglGetCompositorTimingSupportedANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700227 { "eglGetFrameTimestampsANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800229 { "eglGetFrameTimestampSupportedANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
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 Ceballosc18be292016-05-31 14:55:42 -07001251 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001252 if (!s->win.get()) {
1253 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1254 }
Brian Anderson069b3652016-07-22 10:32:47 -07001255 int err = native_window_enable_frame_timestamps(
1256 s->win.get(), value ? true : false);
1257 return (err == NO_ERROR) ? EGL_TRUE :
1258 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001259 }
1260
Mathias Agopian518ec112011-05-13 16:21:08 -07001261 if (s->cnx->egl.eglSurfaceAttrib) {
1262 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001263 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001264 }
1265 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1266}
1267
1268EGLBoolean eglBindTexImage(
1269 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1270{
1271 clearError();
1272
Jesse Hallb29e5e82012-04-04 16:53:42 -07001273 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001274 if (!dp) return EGL_FALSE;
1275
Jesse Hallb29e5e82012-04-04 16:53:42 -07001276 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001277 if (!_s.get())
1278 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001279
Mathias Agopian518ec112011-05-13 16:21:08 -07001280 egl_surface_t const * const s = get_surface(surface);
1281 if (s->cnx->egl.eglBindTexImage) {
1282 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001283 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001284 }
1285 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1286}
1287
1288EGLBoolean eglReleaseTexImage(
1289 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1290{
1291 clearError();
1292
Jesse Hallb29e5e82012-04-04 16:53:42 -07001293 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001294 if (!dp) return EGL_FALSE;
1295
Jesse Hallb29e5e82012-04-04 16:53:42 -07001296 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001297 if (!_s.get())
1298 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001299
Mathias Agopian518ec112011-05-13 16:21:08 -07001300 egl_surface_t const * const s = get_surface(surface);
1301 if (s->cnx->egl.eglReleaseTexImage) {
1302 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001303 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001304 }
1305 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1306}
1307
1308EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1309{
1310 clearError();
1311
Jesse Hallb29e5e82012-04-04 16:53:42 -07001312 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001313 if (!dp) return EGL_FALSE;
1314
1315 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001316 egl_connection_t* const cnx = &gEGLImpl;
1317 if (cnx->dso && cnx->egl.eglSwapInterval) {
1318 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001319 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001320
Mathias Agopian518ec112011-05-13 16:21:08 -07001321 return res;
1322}
1323
1324
1325// ----------------------------------------------------------------------------
1326// EGL 1.2
1327// ----------------------------------------------------------------------------
1328
1329EGLBoolean eglWaitClient(void)
1330{
1331 clearError();
1332
Mathias Agopianada798b2012-02-13 17:09:30 -08001333 egl_connection_t* const cnx = &gEGLImpl;
1334 if (!cnx->dso)
1335 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1336
1337 EGLBoolean res;
1338 if (cnx->egl.eglWaitClient) {
1339 res = cnx->egl.eglWaitClient();
1340 } else {
1341 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001342 }
1343 return res;
1344}
1345
1346EGLBoolean eglBindAPI(EGLenum api)
1347{
1348 clearError();
1349
1350 if (egl_init_drivers() == EGL_FALSE) {
1351 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1352 }
1353
1354 // bind this API on all EGLs
1355 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001356 egl_connection_t* const cnx = &gEGLImpl;
1357 if (cnx->dso && cnx->egl.eglBindAPI) {
1358 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001359 }
1360 return res;
1361}
1362
1363EGLenum eglQueryAPI(void)
1364{
1365 clearError();
1366
1367 if (egl_init_drivers() == EGL_FALSE) {
1368 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1369 }
1370
Mathias Agopianada798b2012-02-13 17:09:30 -08001371 egl_connection_t* const cnx = &gEGLImpl;
1372 if (cnx->dso && cnx->egl.eglQueryAPI) {
1373 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001374 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001375
Mathias Agopian518ec112011-05-13 16:21:08 -07001376 // or, it can only be OpenGL ES
1377 return EGL_OPENGL_ES_API;
1378}
1379
1380EGLBoolean eglReleaseThread(void)
1381{
1382 clearError();
1383
Mathias Agopianada798b2012-02-13 17:09:30 -08001384 egl_connection_t* const cnx = &gEGLImpl;
1385 if (cnx->dso && cnx->egl.eglReleaseThread) {
1386 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001387 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301388
1389 // If there is context bound to the thread, release it
1390 egl_display_t::loseCurrent(get_context(getContext()));
1391
Mathias Agopian518ec112011-05-13 16:21:08 -07001392 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001393 return EGL_TRUE;
1394}
1395
1396EGLSurface eglCreatePbufferFromClientBuffer(
1397 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1398 EGLConfig config, const EGLint *attrib_list)
1399{
1400 clearError();
1401
Jesse Hallb29e5e82012-04-04 16:53:42 -07001402 egl_connection_t* cnx = NULL;
1403 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1404 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001405 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1406 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001407 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001408 }
1409 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1410}
1411
1412// ----------------------------------------------------------------------------
1413// EGL_EGLEXT_VERSION 3
1414// ----------------------------------------------------------------------------
1415
1416EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1417 const EGLint *attrib_list)
1418{
1419 clearError();
1420
Jesse Hallb29e5e82012-04-04 16:53:42 -07001421 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001422 if (!dp) return EGL_FALSE;
1423
Jesse Hallb29e5e82012-04-04 16:53:42 -07001424 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001425 if (!_s.get())
1426 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001427
1428 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001429 if (s->cnx->egl.eglLockSurfaceKHR) {
1430 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001431 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001432 }
1433 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1434}
1435
1436EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1437{
1438 clearError();
1439
Jesse Hallb29e5e82012-04-04 16:53:42 -07001440 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001441 if (!dp) return EGL_FALSE;
1442
Jesse Hallb29e5e82012-04-04 16:53:42 -07001443 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001444 if (!_s.get())
1445 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001446
1447 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001448 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001449 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450 }
1451 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1452}
1453
1454EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1455 EGLClientBuffer buffer, const EGLint *attrib_list)
1456{
1457 clearError();
1458
Jesse Hallb29e5e82012-04-04 16:53:42 -07001459 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001460 if (!dp) return EGL_NO_IMAGE_KHR;
1461
Jesse Hallb29e5e82012-04-04 16:53:42 -07001462 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001463 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001464
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001465 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1466 egl_connection_t* const cnx = &gEGLImpl;
1467 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1468 result = cnx->egl.eglCreateImageKHR(
1469 dp->disp.dpy,
1470 c ? c->context : EGL_NO_CONTEXT,
1471 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001472 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001473 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001474}
1475
1476EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1477{
1478 clearError();
1479
Jesse Hallb29e5e82012-04-04 16:53:42 -07001480 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001481 if (!dp) return EGL_FALSE;
1482
Steven Holte646a5c52012-06-04 20:02:11 -07001483 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001484 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001485 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001486 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001487 }
Steven Holte646a5c52012-06-04 20:02:11 -07001488 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001489}
1490
1491// ----------------------------------------------------------------------------
1492// EGL_EGLEXT_VERSION 5
1493// ----------------------------------------------------------------------------
1494
1495
1496EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1497{
1498 clearError();
1499
Jesse Hallb29e5e82012-04-04 16:53:42 -07001500 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001501 if (!dp) return EGL_NO_SYNC_KHR;
1502
Mathias Agopian518ec112011-05-13 16:21:08 -07001503 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001504 egl_connection_t* const cnx = &gEGLImpl;
1505 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1506 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001507 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001508 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001509}
1510
1511EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1512{
1513 clearError();
1514
Jesse Hallb29e5e82012-04-04 16:53:42 -07001515 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 if (!dp) return EGL_FALSE;
1517
Mathias Agopian518ec112011-05-13 16:21:08 -07001518 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001519 egl_connection_t* const cnx = &gEGLImpl;
1520 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1521 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001522 }
1523 return result;
1524}
1525
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001526EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1527 clearError();
1528
1529 const egl_display_ptr dp = validate_display(dpy);
1530 if (!dp) return EGL_FALSE;
1531
1532 EGLBoolean result = EGL_FALSE;
1533 egl_connection_t* const cnx = &gEGLImpl;
1534 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1535 result = cnx->egl.eglSignalSyncKHR(
1536 dp->disp.dpy, sync, mode);
1537 }
1538 return result;
1539}
1540
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001541EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1542 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001543{
1544 clearError();
1545
Jesse Hallb29e5e82012-04-04 16:53:42 -07001546 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001547 if (!dp) return EGL_FALSE;
1548
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001549 EGLBoolean result = EGL_FALSE;
1550 egl_connection_t* const cnx = &gEGLImpl;
1551 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1552 result = cnx->egl.eglClientWaitSyncKHR(
1553 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001554 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001555 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001556}
1557
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001558EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1559 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001560{
1561 clearError();
1562
Jesse Hallb29e5e82012-04-04 16:53:42 -07001563 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001564 if (!dp) return EGL_FALSE;
1565
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001566 EGLBoolean result = EGL_FALSE;
1567 egl_connection_t* const cnx = &gEGLImpl;
1568 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1569 result = cnx->egl.eglGetSyncAttribKHR(
1570 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001571 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001572 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001573}
1574
Season Li000d88f2015-07-01 11:39:40 -07001575EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1576{
1577 clearError();
1578
1579 const egl_display_ptr dp = validate_display(dpy);
1580 if (!dp) return EGL_NO_STREAM_KHR;
1581
1582 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1583 egl_connection_t* const cnx = &gEGLImpl;
1584 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1585 result = cnx->egl.eglCreateStreamKHR(
1586 dp->disp.dpy, attrib_list);
1587 }
1588 return result;
1589}
1590
1591EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1592{
1593 clearError();
1594
1595 const egl_display_ptr dp = validate_display(dpy);
1596 if (!dp) return EGL_FALSE;
1597
1598 EGLBoolean result = EGL_FALSE;
1599 egl_connection_t* const cnx = &gEGLImpl;
1600 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1601 result = cnx->egl.eglDestroyStreamKHR(
1602 dp->disp.dpy, stream);
1603 }
1604 return result;
1605}
1606
1607EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1608 EGLenum attribute, EGLint value)
1609{
1610 clearError();
1611
1612 const egl_display_ptr dp = validate_display(dpy);
1613 if (!dp) return EGL_FALSE;
1614
1615 EGLBoolean result = EGL_FALSE;
1616 egl_connection_t* const cnx = &gEGLImpl;
1617 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1618 result = cnx->egl.eglStreamAttribKHR(
1619 dp->disp.dpy, stream, attribute, value);
1620 }
1621 return result;
1622}
1623
1624EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1625 EGLenum attribute, EGLint *value)
1626{
1627 clearError();
1628
1629 const egl_display_ptr dp = validate_display(dpy);
1630 if (!dp) return EGL_FALSE;
1631
1632 EGLBoolean result = EGL_FALSE;
1633 egl_connection_t* const cnx = &gEGLImpl;
1634 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1635 result = cnx->egl.eglQueryStreamKHR(
1636 dp->disp.dpy, stream, attribute, value);
1637 }
1638 return result;
1639}
1640
1641EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1642 EGLenum attribute, EGLuint64KHR *value)
1643{
1644 clearError();
1645
1646 const egl_display_ptr dp = validate_display(dpy);
1647 if (!dp) return EGL_FALSE;
1648
1649 EGLBoolean result = EGL_FALSE;
1650 egl_connection_t* const cnx = &gEGLImpl;
1651 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1652 result = cnx->egl.eglQueryStreamu64KHR(
1653 dp->disp.dpy, stream, attribute, value);
1654 }
1655 return result;
1656}
1657
1658EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1659 EGLenum attribute, EGLTimeKHR *value)
1660{
1661 clearError();
1662
1663 const egl_display_ptr dp = validate_display(dpy);
1664 if (!dp) return EGL_FALSE;
1665
1666 EGLBoolean result = EGL_FALSE;
1667 egl_connection_t* const cnx = &gEGLImpl;
1668 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1669 result = cnx->egl.eglQueryStreamTimeKHR(
1670 dp->disp.dpy, stream, attribute, value);
1671 }
1672 return result;
1673}
1674
1675EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1676 EGLStreamKHR stream, const EGLint *attrib_list)
1677{
1678 clearError();
1679
1680 egl_display_ptr dp = validate_display(dpy);
1681 if (!dp) return EGL_NO_SURFACE;
1682
1683 egl_connection_t* const cnx = &gEGLImpl;
1684 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1685 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1686 dp->disp.dpy, config, stream, attrib_list);
1687 if (surface != EGL_NO_SURFACE) {
1688 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1689 surface, cnx);
1690 return s;
1691 }
1692 }
1693 return EGL_NO_SURFACE;
1694}
1695
1696EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1697 EGLStreamKHR stream)
1698{
1699 clearError();
1700
1701 const egl_display_ptr dp = validate_display(dpy);
1702 if (!dp) return EGL_FALSE;
1703
1704 EGLBoolean result = EGL_FALSE;
1705 egl_connection_t* const cnx = &gEGLImpl;
1706 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1707 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1708 dp->disp.dpy, stream);
1709 }
1710 return result;
1711}
1712
1713EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1714 EGLStreamKHR stream)
1715{
1716 clearError();
1717
1718 const egl_display_ptr dp = validate_display(dpy);
1719 if (!dp) return EGL_FALSE;
1720
1721 EGLBoolean result = EGL_FALSE;
1722 egl_connection_t* const cnx = &gEGLImpl;
1723 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1724 result = cnx->egl.eglStreamConsumerAcquireKHR(
1725 dp->disp.dpy, stream);
1726 }
1727 return result;
1728}
1729
1730EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1731 EGLStreamKHR stream)
1732{
1733 clearError();
1734
1735 const egl_display_ptr dp = validate_display(dpy);
1736 if (!dp) return EGL_FALSE;
1737
1738 EGLBoolean result = EGL_FALSE;
1739 egl_connection_t* const cnx = &gEGLImpl;
1740 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1741 result = cnx->egl.eglStreamConsumerReleaseKHR(
1742 dp->disp.dpy, stream);
1743 }
1744 return result;
1745}
1746
1747EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1748 EGLDisplay dpy, EGLStreamKHR stream)
1749{
1750 clearError();
1751
1752 const egl_display_ptr dp = validate_display(dpy);
1753 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1754
1755 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1756 egl_connection_t* const cnx = &gEGLImpl;
1757 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1758 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1759 dp->disp.dpy, stream);
1760 }
1761 return result;
1762}
1763
1764EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1765 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1766{
1767 clearError();
1768
1769 const egl_display_ptr dp = validate_display(dpy);
1770 if (!dp) return EGL_NO_STREAM_KHR;
1771
1772 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1773 egl_connection_t* const cnx = &gEGLImpl;
1774 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1775 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1776 dp->disp.dpy, file_descriptor);
1777 }
1778 return result;
1779}
1780
Mathias Agopian518ec112011-05-13 16:21:08 -07001781// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001782// EGL_EGLEXT_VERSION 15
1783// ----------------------------------------------------------------------------
1784
1785EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1786 clearError();
1787 const egl_display_ptr dp = validate_display(dpy);
1788 if (!dp) return EGL_FALSE;
1789 EGLint result = EGL_FALSE;
1790 egl_connection_t* const cnx = &gEGLImpl;
1791 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1792 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1793 }
1794 return result;
1795}
1796
1797// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001798// ANDROID extensions
1799// ----------------------------------------------------------------------------
1800
Jamie Gennis331841b2012-09-06 14:52:00 -07001801EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1802{
1803 clearError();
1804
1805 const egl_display_ptr dp = validate_display(dpy);
1806 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1807
1808 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1809 egl_connection_t* const cnx = &gEGLImpl;
1810 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1811 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1812 }
1813 return result;
1814}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001815
Andy McFadden72841452013-03-01 16:25:32 -08001816EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1817 EGLnsecsANDROID time)
1818{
1819 clearError();
1820
1821 const egl_display_ptr dp = validate_display(dpy);
1822 if (!dp) {
1823 return EGL_FALSE;
1824 }
1825
1826 SurfaceRef _s(dp.get(), surface);
1827 if (!_s.get()) {
1828 setError(EGL_BAD_SURFACE, EGL_FALSE);
1829 return EGL_FALSE;
1830 }
1831
1832 egl_surface_t const * const s = get_surface(surface);
1833 native_window_set_buffers_timestamp(s->win.get(), time);
1834
1835 return EGL_TRUE;
1836}
1837
Craig Donner05249fc2016-01-15 19:33:55 -08001838EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1839{
1840 clearError();
1841
Craig Donnere96a3252017-02-02 12:13:34 -08001842 uint64_t producerUsage = 0;
1843 uint64_t consumerUsage = 0;
Craig Donner05249fc2016-01-15 19:33:55 -08001844 uint32_t width = 0;
1845 uint32_t height = 0;
1846 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001847 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001848 uint32_t red_size = 0;
1849 uint32_t green_size = 0;
1850 uint32_t blue_size = 0;
1851 uint32_t alpha_size = 0;
1852
Craig Donnerb40504a2016-06-03 17:54:25 -07001853#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001854 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001855 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001856 target = value; \
1857 } else { \
1858 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1859 } \
1860 break
1861
1862 if (attrib_list) {
1863 while (*attrib_list != EGL_NONE) {
1864 GLint attr = *attrib_list++;
1865 GLint value = *attrib_list++;
1866 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001867 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1868 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1869 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1870 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1871 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1872 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001873 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001874 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1875 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001876 producerUsage |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001877 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001878 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001879 producerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
Craig Donner05249fc2016-01-15 19:33:55 -08001880 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001881 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001882 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
Craig Donner05249fc2016-01-15 19:33:55 -08001883 }
Craig Donner05249fc2016-01-15 19:33:55 -08001884 break;
1885 default:
1886 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1887 }
1888 }
1889 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001890#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001891
1892 // Validate format.
1893 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1894 if (alpha_size == 8) {
1895 format = HAL_PIXEL_FORMAT_RGBA_8888;
1896 } else {
1897 format = HAL_PIXEL_FORMAT_RGB_888;
1898 }
1899 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1900 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001901 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001902 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001903 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001904 red_size, green_size, blue_size, alpha_size);
1905 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1906 }
1907
Craig Donner68671532016-07-18 10:19:54 -07001908#define CHECK_ERROR_CONDITION(message) \
1909 if (err != NO_ERROR) { \
1910 ALOGE(message); \
1911 goto error_condition; \
1912 }
1913
1914 // The holder is used to destroy the buffer if an error occurs.
1915 GraphicBuffer* gBuffer = new GraphicBuffer();
1916 sp<IServiceManager> sm = defaultServiceManager();
1917 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1918 sp<IBinder> allocator;
1919 Parcel sc_data, sc_reply, data, reply;
1920 status_t err = NO_ERROR;
1921 if (sm == NULL) {
1922 ALOGE("Unable to connect to ServiceManager");
1923 goto error_condition;
1924 }
1925
1926 // Obtain an allocator.
1927 if (surfaceFlinger == NULL) {
1928 ALOGE("Unable to connect to SurfaceFlinger");
1929 goto error_condition;
1930 }
1931 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1932 err = surfaceFlinger->transact(
1933 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1934 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1935 allocator = sc_reply.readStrongBinder();
1936
1937 if (allocator == NULL) {
1938 ALOGE("Unable to obtain an ISurfaceComposer");
1939 goto error_condition;
1940 }
1941 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1942 err = data.writeUint32(width);
1943 CHECK_ERROR_CONDITION("Unable to write width");
1944 err = data.writeUint32(height);
1945 CHECK_ERROR_CONDITION("Unable to write height");
1946 err = data.writeInt32(static_cast<int32_t>(format));
1947 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001948 err = data.writeUint32(layer_count);
1949 CHECK_ERROR_CONDITION("Unable to write layer count");
Craig Donnere96a3252017-02-02 12:13:34 -08001950 err = data.writeUint64(producerUsage);
1951 CHECK_ERROR_CONDITION("Unable to write producer usage");
1952 err = data.writeUint64(consumerUsage);
1953 CHECK_ERROR_CONDITION("Unable to write consumer usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001954 err = data.writeUtf8AsUtf16(
1955 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1956 std::to_string(getpid()) + ']');
1957 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001958 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1959 &reply);
1960 CHECK_ERROR_CONDITION(
1961 "Unable to request buffer allocation from surface composer");
1962 err = reply.readInt32();
1963 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1964 err = reply.read(*gBuffer);
1965 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1966
1967 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001968 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001969 ALOGE("Unable to create native buffer "
Craig Donnere96a3252017-02-02 12:13:34 -08001970 "{ w=%u, h=%u, f=%u, pu=%" PRIx64 " cu=%" PRIx64 ", lc=%u} %#x",
1971 width, height, format, producerUsage, consumerUsage,
1972 layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001973 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001974 }
Craig Donnere96a3252017-02-02 12:13:34 -08001975 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, pu=%" PRIx64
1976 " cu=%" PRIx64 ", lc=%u}",
1977 gBuffer, width, height, format, producerUsage, consumerUsage,
1978 layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001979 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001980
1981#undef CHECK_ERROR_CONDITION
1982
1983error_condition:
1984 // Delete the buffer.
1985 sp<GraphicBuffer> holder(gBuffer);
1986 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001987}
1988
Craig Donner60761072017-01-27 12:30:44 -08001989EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1990 clearError();
1991
1992 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1993
1994 // FIXME: remove this dangerous reinterpret_cast.
1995 const GraphicBuffer* graphicBuffer =
1996 reinterpret_cast<const GraphicBuffer*>(buffer);
1997 return static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
1998}
1999
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002000// ----------------------------------------------------------------------------
2001// NVIDIA extensions
2002// ----------------------------------------------------------------------------
2003EGLuint64NV eglGetSystemTimeFrequencyNV()
2004{
2005 clearError();
2006
2007 if (egl_init_drivers() == EGL_FALSE) {
2008 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2009 }
2010
2011 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002012 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002013
Mathias Agopianada798b2012-02-13 17:09:30 -08002014 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2015 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002016 }
2017
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07002018 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002019}
2020
2021EGLuint64NV eglGetSystemTimeNV()
2022{
2023 clearError();
2024
2025 if (egl_init_drivers() == EGL_FALSE) {
2026 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2027 }
2028
2029 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002030 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002031
Mathias Agopianada798b2012-02-13 17:09:30 -08002032 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2033 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002034 }
2035
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07002036 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002037}
Dan Stozaa894d082015-02-19 15:27:36 -08002038
2039// ----------------------------------------------------------------------------
2040// Partial update extension
2041// ----------------------------------------------------------------------------
2042EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2043 EGLint *rects, EGLint n_rects)
2044{
2045 clearError();
2046
2047 const egl_display_ptr dp = validate_display(dpy);
2048 if (!dp) {
2049 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2050 return EGL_FALSE;
2051 }
2052
2053 SurfaceRef _s(dp.get(), surface);
2054 if (!_s.get()) {
2055 setError(EGL_BAD_SURFACE, EGL_FALSE);
2056 return EGL_FALSE;
2057 }
2058
2059 egl_surface_t const * const s = get_surface(surface);
2060 if (s->cnx->egl.eglSetDamageRegionKHR) {
2061 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2062 rects, n_rects);
2063 }
2064
2065 return EGL_FALSE;
2066}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002067
Brian Anderson1049d1d2016-12-16 17:25:57 -08002068EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2069 EGLuint64KHR *frameId) {
2070 clearError();
2071
2072 const egl_display_ptr dp = validate_display(dpy);
2073 if (!dp) {
2074 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2075 }
2076
2077 SurfaceRef _s(dp.get(), surface);
2078 if (!_s.get()) {
2079 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2080 }
2081
2082 egl_surface_t const * const s = get_surface(surface);
2083
2084 if (!s->win.get()) {
2085 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2086 }
2087
2088 uint64_t nextFrameId = 0;
2089 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2090
2091 if (ret != NO_ERROR) {
2092 // This should not happen. Return an error that is not in the spec
2093 // so it's obvious something is very wrong.
2094 ALOGE("eglGetNextFrameId: Unexpected error.");
2095 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
2096 }
2097
2098 *frameId = nextFrameId;
2099 return EGL_TRUE;
2100}
2101
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002102EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2103 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2104{
2105 clearError();
2106
2107 const egl_display_ptr dp = validate_display(dpy);
2108 if (!dp) {
2109 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2110 }
2111
2112 SurfaceRef _s(dp.get(), surface);
2113 if (!_s.get()) {
2114 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2115 }
2116
2117 egl_surface_t const * const s = get_surface(surface);
2118
2119 if (!s->win.get()) {
2120 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2121 }
2122
2123 nsecs_t* compositeDeadline = nullptr;
2124 nsecs_t* compositeInterval = nullptr;
2125 nsecs_t* compositeToPresentLatency = nullptr;
2126
2127 for (int i = 0; i < numTimestamps; i++) {
2128 switch (names[i]) {
2129 case EGL_COMPOSITE_DEADLINE_ANDROID:
2130 compositeDeadline = &values[i];
2131 break;
2132 case EGL_COMPOSITE_INTERVAL_ANDROID:
2133 compositeInterval = &values[i];
2134 break;
2135 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2136 compositeToPresentLatency = &values[i];
2137 break;
2138 default:
2139 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2140 }
2141 }
2142
2143 status_t ret = native_window_get_compositor_timing(s->win.get(),
2144 compositeDeadline, compositeInterval, compositeToPresentLatency);
2145
2146 switch (ret) {
2147 case NO_ERROR:
2148 return EGL_TRUE;
2149 case INVALID_OPERATION:
2150 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2151 default:
2152 // This should not happen. Return an error that is not in the spec
2153 // so it's obvious something is very wrong.
2154 ALOGE("eglGetCompositorTiming: Unexpected error.");
2155 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
2156 }
2157}
2158
2159EGLBoolean eglGetCompositorTimingSupportedANDROID(
2160 EGLDisplay dpy, EGLSurface surface, EGLint name)
2161{
2162 clearError();
2163
2164 const egl_display_ptr dp = validate_display(dpy);
2165 if (!dp) {
2166 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
2167 }
2168
2169 SurfaceRef _s(dp.get(), surface);
2170 if (!_s.get()) {
2171 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2172 }
2173
2174 egl_surface_t const * const s = get_surface(surface);
2175
2176 ANativeWindow* window = s->win.get();
2177 if (!window) {
2178 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2179 }
2180
2181 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002182 case EGL_COMPOSITE_DEADLINE_ANDROID:
2183 case EGL_COMPOSITE_INTERVAL_ANDROID:
2184 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2185 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002186 default:
2187 return EGL_FALSE;
2188 }
2189}
2190
Pablo Ceballosc18be292016-05-31 14:55:42 -07002191EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002192 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002193 EGLnsecsANDROID *values)
2194{
2195 clearError();
2196
2197 const egl_display_ptr dp = validate_display(dpy);
2198 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002199 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002200 }
2201
2202 SurfaceRef _s(dp.get(), surface);
2203 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002204 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002205 }
2206
2207 egl_surface_t const * const s = get_surface(surface);
2208
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002209 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002210 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002211 }
2212
Brian Andersondbd0ea82016-07-22 09:38:59 -07002213 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002214 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002215 nsecs_t* latchTime = nullptr;
2216 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002217 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002218 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002219 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002220 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002221 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002222 nsecs_t* releaseTime = nullptr;
2223
2224 for (int i = 0; i < numTimestamps; i++) {
2225 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002226 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2227 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002228 break;
2229 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2230 acquireTime = &values[i];
2231 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002232 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2233 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002234 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002235 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2236 firstRefreshStartTime = &values[i];
2237 break;
2238 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2239 lastRefreshStartTime = &values[i];
2240 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002241 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2242 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002243 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002244 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2245 displayPresentTime = &values[i];
2246 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002247 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2248 displayRetireTime = &values[i];
2249 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002250 case EGL_DEQUEUE_READY_TIME_ANDROID:
2251 dequeueReadyTime = &values[i];
2252 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002253 case EGL_READS_DONE_TIME_ANDROID:
2254 releaseTime = &values[i];
2255 break;
2256 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002257 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002258 }
2259 }
2260
Brian Anderson1049d1d2016-12-16 17:25:57 -08002261 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002262 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002263 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002264 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002265
Brian Anderson069b3652016-07-22 10:32:47 -07002266 switch (ret) {
2267 case NO_ERROR:
2268 return EGL_TRUE;
2269 case NAME_NOT_FOUND:
2270 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002271 case INVALID_OPERATION:
2272 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002273 case BAD_VALUE:
2274 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2275 default:
2276 // This should not happen. Return an error that is not in the spec
2277 // so it's obvious something is very wrong.
Brian Anderson1049d1d2016-12-16 17:25:57 -08002278 ALOGE("eglGetFrameTimestamps: Unexpected error.");
Brian Anderson069b3652016-07-22 10:32:47 -07002279 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002280 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002281}
2282
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002283EGLBoolean eglGetFrameTimestampSupportedANDROID(
2284 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002285{
2286 clearError();
2287
2288 const egl_display_ptr dp = validate_display(dpy);
2289 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002290 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002291 }
2292
2293 SurfaceRef _s(dp.get(), surface);
2294 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002295 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2296 }
2297
2298 egl_surface_t const * const s = get_surface(surface);
2299
2300 ANativeWindow* window = s->win.get();
2301 if (!window) {
2302 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002303 }
2304
2305 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002306 case EGL_COMPOSITE_DEADLINE_ANDROID:
2307 case EGL_COMPOSITE_INTERVAL_ANDROID:
2308 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002309 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002310 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002311 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2312 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2313 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002314 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002315 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002316 case EGL_READS_DONE_TIME_ANDROID:
2317 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002318 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2319 int value = 0;
2320 window->query(window,
2321 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2322 return value == 0 ? EGL_FALSE : EGL_TRUE;
2323 }
2324 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2325 int value = 0;
2326 window->query(window,
2327 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2328 return value == 0 ? EGL_FALSE : EGL_TRUE;
2329 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002330 default:
2331 return EGL_FALSE;
2332 }
2333}