blob: 3359c6408c8fdaefa5797bd8cb5c92418f9fe88c [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
Craig Donner60761072017-01-27 12:30:44 -080029#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080030#include <private/android/AHardwareBufferHelpers.h>
31
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Craig Donner68671532016-07-18 10:19:54 -070036#include <gui/ISurfaceComposer.h>
37
Craig Donner05249fc2016-01-15 19:33:55 -080038#include <ui/GraphicBuffer.h>
39
Mathias Agopian89ed4c82017-02-09 18:48:34 -080040
Mathias Agopian518ec112011-05-13 16:21:08 -070041#include <utils/KeyedVector.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070042#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080044#include <utils/Thread.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 Agopian518ec112011-05-13 16:21:08 -070051
52#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070053#include "egl_object.h"
54#include "egl_tls.h"
55
56using namespace android;
57
58// ----------------------------------------------------------------------------
59
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070060namespace android {
61
Mathias Agopian518ec112011-05-13 16:21:08 -070062struct extention_map_t {
63 const char* name;
64 __eglMustCastToProperFunctionPointerType address;
65};
66
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070067/*
Jesse Hall21558da2013-08-06 15:31:22 -070068 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070069 *
Jesse Hall21558da2013-08-06 15:31:22 -070070 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
71 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070072 *
Jesse Hall21558da2013-08-06 15:31:22 -070073 * The rest (gExtensionString) depend on support in the EGL driver, and are
74 * only available if the driver supports them. However, some of these must be
75 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080076 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070077 * the mandatory extensions are present and may not function properly if some
78 * are missing.
79 *
80 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070081 */
Mathias Agopian737b8962017-02-24 14:32:05 -080082
Mathias Agopian311b4792017-02-28 15:00:49 -080083extern char const * const gBuiltinExtensionString;
84extern char const * const gExtensionString;
Mathias Agopian737b8962017-02-24 14:32:05 -080085
Mathias Agopian311b4792017-02-28 15:00:49 -080086char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070087 "EGL_KHR_get_all_proc_addresses "
88 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080089 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080090 "EGL_ANDROID_create_native_client_buffer "
Craig Donner60761072017-01-27 12:30:44 -080091 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080092 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070093 "EGL_ANDROID_get_frame_timestamps "
Jesse Hall21558da2013-08-06 15:31:22 -070094 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080095
96char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_image " // mandatory
98 "EGL_KHR_image_base " // mandatory
99 "EGL_KHR_image_pixmap "
100 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700101 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700102 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700103 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700104 "EGL_KHR_gl_texture_cubemap_image "
105 "EGL_KHR_gl_renderbuffer_image "
106 "EGL_KHR_reusable_sync "
107 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700108 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700109 "EGL_KHR_config_attribs "
110 "EGL_KHR_surfaceless_context "
111 "EGL_KHR_stream "
112 "EGL_KHR_stream_fifo "
113 "EGL_KHR_stream_producer_eglsurface "
114 "EGL_KHR_stream_consumer_gltexture "
115 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700116 "EGL_EXT_create_context_robustness "
117 "EGL_NV_system_time "
118 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700119 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700120 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800121 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700122 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800123 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700124 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700125 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700126 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700127 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000128 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700129 ;
130
131// extensions not exposed to applications but used by the ANDROID system
132// "EGL_ANDROID_blob_cache " // strongly recommended
133// "EGL_IMG_hibernate_process " // optional
134// "EGL_ANDROID_native_fence_sync " // strongly recommended
135// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700136// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700137
138/*
139 * EGL Extensions entry-points exposed to 3rd party applications
140 * (keep in sync with gExtensionString above)
141 *
142 */
143static const extention_map_t sExtensionMap[] = {
144 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700145 { "eglLockSurfaceKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
147 { "eglUnlockSurfaceKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700149
150 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700151 { "eglCreateImageKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
153 { "eglDestroyImageKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700155
156 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
157 { "eglCreateSyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
159 { "eglDestroySyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
161 { "eglClientWaitSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
163 { "eglSignalSyncKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
165 { "eglGetSyncAttribKHR",
166 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
167
168 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800169 { "eglGetSystemTimeFrequencyNV",
170 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
171 { "eglGetSystemTimeNV",
172 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700173
Mathias Agopian2bb71682013-03-27 17:32:41 -0700174 // EGL_KHR_wait_sync
175 { "eglWaitSyncKHR",
176 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700177
178 // EGL_ANDROID_presentation_time
179 { "eglPresentationTimeANDROID",
180 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800181
182 // EGL_KHR_swap_buffers_with_damage
183 { "eglSwapBuffersWithDamageKHR",
184 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
185
Craig Donner60761072017-01-27 12:30:44 -0800186 // EGL_ANDROID_create_native_client_buffer
Craig Donner05249fc2016-01-15 19:33:55 -0800187 { "eglCreateNativeClientBufferANDROID",
188 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
189
Craig Donner60761072017-01-27 12:30:44 -0800190 // EGL_ANDROID_get_native_client_buffer
191 { "eglGetNativeClientBufferANDROID",
192 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
193
Dan Stozaa894d082015-02-19 15:27:36 -0800194 // EGL_KHR_partial_update
195 { "eglSetDamageRegionKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700197
198 { "eglCreateStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
200 { "eglDestroyStreamKHR",
201 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
202 { "eglStreamAttribKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
204 { "eglQueryStreamKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
206 { "eglQueryStreamu64KHR",
207 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
208 { "eglQueryStreamTimeKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
210 { "eglCreateStreamProducerSurfaceKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
212 { "eglStreamConsumerGLTextureExternalKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
214 { "eglStreamConsumerAcquireKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
216 { "eglStreamConsumerReleaseKHR",
217 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
218 { "eglGetStreamFileDescriptorKHR",
219 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
220 { "eglCreateStreamFromFileDescriptorKHR",
221 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700222
223 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800224 { "eglGetNextFrameIdANDROID",
225 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800226 { "eglGetCompositorTimingANDROID",
227 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
228 { "eglGetCompositorTimingSupportedANDROID",
229 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700230 { "eglGetFrameTimestampsANDROID",
231 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800232 { "eglGetFrameTimestampSupportedANDROID",
233 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700234};
235
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700236/*
237 * These extensions entry-points should not be exposed to applications.
238 * They're used internally by the Android EGL layer.
239 */
240#define FILTER_EXTENSIONS(procname) \
241 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
242 !strcmp((procname), "eglHibernateProcessIMG") || \
243 !strcmp((procname), "eglAwakenProcessIMG") || \
244 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
245
246
247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248// accesses protected by sExtensionMapMutex
249static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
250static int sGLExtentionSlot = 0;
251static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
252
253static void(*findProcAddress(const char* name,
254 const extention_map_t* map, size_t n))() {
255 for (uint32_t i=0 ; i<n ; i++) {
256 if (!strcmp(name, map[i].name)) {
257 return map[i].address;
258 }
259 }
260 return NULL;
261}
262
263// ----------------------------------------------------------------------------
264
Mathias Agopian518ec112011-05-13 16:21:08 -0700265extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
266extern EGLBoolean egl_init_drivers();
267extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700268extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700269
Mathias Agopian518ec112011-05-13 16:21:08 -0700270} // namespace android;
271
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700272
Mathias Agopian518ec112011-05-13 16:21:08 -0700273// ----------------------------------------------------------------------------
274
275static inline void clearError() { egl_tls_t::clearError(); }
276static inline EGLContext getContext() { return egl_tls_t::getContext(); }
277
278// ----------------------------------------------------------------------------
279
280EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
281{
Jesse Hall1508ae62017-01-19 17:43:26 -0800282 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700283 clearError();
284
Dan Stozac3289c42014-01-17 11:38:34 -0800285 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700286 if (index >= NUM_DISPLAYS) {
287 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
288 }
289
290 if (egl_init_drivers() == EGL_FALSE) {
291 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
292 }
293
294 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
295 return dpy;
296}
297
298// ----------------------------------------------------------------------------
299// Initialization
300// ----------------------------------------------------------------------------
301
302EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
303{
304 clearError();
305
Jesse Hallb29e5e82012-04-04 16:53:42 -0700306 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800307 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700308
309 EGLBoolean res = dp->initialize(major, minor);
310
311 return res;
312}
313
314EGLBoolean eglTerminate(EGLDisplay dpy)
315{
316 // NOTE: don't unload the drivers b/c some APIs can be called
317 // after eglTerminate() has been called. eglTerminate() only
318 // terminates an EGLDisplay, not a EGL itself.
319
320 clearError();
321
Jesse Hallb29e5e82012-04-04 16:53:42 -0700322 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800323 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700324
325 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800326
Mathias Agopian518ec112011-05-13 16:21:08 -0700327 return res;
328}
329
330// ----------------------------------------------------------------------------
331// configuration
332// ----------------------------------------------------------------------------
333
334EGLBoolean eglGetConfigs( EGLDisplay dpy,
335 EGLConfig *configs,
336 EGLint config_size, EGLint *num_config)
337{
338 clearError();
339
Jesse Hallb29e5e82012-04-04 16:53:42 -0700340 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700341 if (!dp) return EGL_FALSE;
342
Mathias Agopian7773c432012-02-13 20:06:08 -0800343 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800344 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700345 }
346
Mathias Agopian7773c432012-02-13 20:06:08 -0800347 EGLBoolean res = EGL_FALSE;
348 *num_config = 0;
349
350 egl_connection_t* const cnx = &gEGLImpl;
351 if (cnx->dso) {
352 res = cnx->egl.eglGetConfigs(
353 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700354 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800355
356 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700357}
358
359EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
360 EGLConfig *configs, EGLint config_size,
361 EGLint *num_config)
362{
363 clearError();
364
Jesse Hallb29e5e82012-04-04 16:53:42 -0700365 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700366 if (!dp) return EGL_FALSE;
367
368 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800369 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700370 }
371
Mathias Agopian518ec112011-05-13 16:21:08 -0700372 EGLBoolean res = EGL_FALSE;
373 *num_config = 0;
374
Mathias Agopianada798b2012-02-13 17:09:30 -0800375 egl_connection_t* const cnx = &gEGLImpl;
376 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700377 if (attrib_list) {
378 char value[PROPERTY_VALUE_MAX];
379 property_get("debug.egl.force_msaa", value, "false");
380
381 if (!strcmp(value, "true")) {
382 size_t attribCount = 0;
383 EGLint attrib = attrib_list[0];
384
385 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700386 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700387 const EGLint *attribRendererable = NULL;
388 const EGLint *attribCaveat = NULL;
389
390 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700391 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700392 while (attrib != EGL_NONE) {
393 attrib = attrib_list[attribCount];
394 switch (attrib) {
395 case EGL_RENDERABLE_TYPE:
396 attribRendererable = &attrib_list[attribCount];
397 break;
398 case EGL_CONFIG_CAVEAT:
399 attribCaveat = &attrib_list[attribCount];
400 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800401 default:
402 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700403 }
404 attribCount++;
405 }
406
407 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
408 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800409
Romain Guy1cffc802012-10-15 18:13:05 -0700410 // Insert 2 extra attributes to force-enable MSAA 4x
411 EGLint aaAttribs[attribCount + 4];
412 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
413 aaAttribs[1] = 1;
414 aaAttribs[2] = EGL_SAMPLES;
415 aaAttribs[3] = 4;
416
417 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
418
419 EGLint numConfigAA;
420 EGLBoolean resAA = cnx->egl.eglChooseConfig(
421 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
422
423 if (resAA == EGL_TRUE && numConfigAA > 0) {
424 ALOGD("Enabling MSAA 4x");
425 *num_config = numConfigAA;
426 return resAA;
427 }
428 }
429 }
430 }
431
Mathias Agopian7773c432012-02-13 20:06:08 -0800432 res = cnx->egl.eglChooseConfig(
433 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700434 }
435 return res;
436}
437
438EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
439 EGLint attribute, EGLint *value)
440{
441 clearError();
442
Jesse Hallb29e5e82012-04-04 16:53:42 -0700443 egl_connection_t* cnx = NULL;
444 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
445 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800446
Mathias Agopian518ec112011-05-13 16:21:08 -0700447 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800448 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700449}
450
451// ----------------------------------------------------------------------------
452// surfaces
453// ----------------------------------------------------------------------------
454
Jesse Hallc2e41222013-08-08 13:40:22 -0700455// Turn linear formats into corresponding sRGB formats when colorspace is
456// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
457// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800458// the modification isn't possible, the original dataSpace is returned.
459static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
460 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700461 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800462 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700463 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800464 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700465 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800466 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700467}
468
Mathias Agopian518ec112011-05-13 16:21:08 -0700469EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
470 NativeWindowType window,
471 const EGLint *attrib_list)
472{
473 clearError();
474
Jesse Hallb29e5e82012-04-04 16:53:42 -0700475 egl_connection_t* cnx = NULL;
476 egl_display_ptr dp = validate_display_connection(dpy, cnx);
477 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800478 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700479
Andy McFaddend566ce32014-01-07 15:54:17 -0800480 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
481 if (result != OK) {
482 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
483 "failed (%#x) (already connected to another API?)",
484 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700485 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700486 }
487
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700488 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700489 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
490 // of our native format. So if sRGB gamma is requested, we have to
491 // modify the EGLconfig's format before setting the native window's
492 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800493
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700494 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
495 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
496 &componentType);
497
Mathias Agopian95921562017-02-24 14:31:31 -0800498 EGLint format;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800499 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700500 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700501 EGLint r, g, b;
502 r = g = b = 0;
503 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
504 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
505 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700506 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700507 EGLint colorDepth = r + g + b;
508
509 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700510 if (colorDepth <= 16) {
511 format = HAL_PIXEL_FORMAT_RGB_565;
512 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700513 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
514 if (colorDepth > 24) {
515 format = HAL_PIXEL_FORMAT_RGBA_1010102;
516 } else {
517 format = HAL_PIXEL_FORMAT_RGBX_8888;
518 }
519 } else {
520 format = HAL_PIXEL_FORMAT_RGBA_FP16;
521 }
522 }
523 } else {
524 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
525 if (colorDepth > 24) {
526 format = HAL_PIXEL_FORMAT_RGBA_1010102;
527 } else {
528 format = HAL_PIXEL_FORMAT_RGBA_8888;
529 }
530 } else {
531 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700532 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700533 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700534
535 // now select a corresponding sRGB format if needed
536 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
537 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
538 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530539 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700540 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700541 }
542 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800543
Jesse Hallc2e41222013-08-08 13:40:22 -0700544 if (format != 0) {
545 int err = native_window_set_buffers_format(window, format);
546 if (err != 0) {
547 ALOGE("error setting native window pixel format: %s (%d)",
548 strerror(-err), err);
549 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
550 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
551 }
552 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700553
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800554 if (dataSpace != 0) {
555 int err = native_window_set_buffers_data_space(window, dataSpace);
556 if (err != 0) {
557 ALOGE("error setting native window pixel dataSpace: %s (%d)",
558 strerror(-err), err);
559 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
560 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
561 }
562 }
563
Jamie Gennis59769462011-11-19 18:04:43 -0800564 // the EGL spec requires that a new EGLSurface default to swap interval
565 // 1, so explicitly set that on the window here.
566 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
567 anw->setSwapInterval(anw, 1);
568
Mathias Agopian518ec112011-05-13 16:21:08 -0700569 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800570 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700571 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700572 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
573 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700574 return s;
575 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700576
577 // EGLSurface creation failed
578 native_window_set_buffers_format(window, 0);
579 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 }
581 return EGL_NO_SURFACE;
582}
583
584EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
585 NativePixmapType pixmap,
586 const EGLint *attrib_list)
587{
588 clearError();
589
Jesse Hallb29e5e82012-04-04 16:53:42 -0700590 egl_connection_t* cnx = NULL;
591 egl_display_ptr dp = validate_display_connection(dpy, cnx);
592 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700593 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800594 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700595 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700596 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
597 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700598 return s;
599 }
600 }
601 return EGL_NO_SURFACE;
602}
603
604EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
605 const EGLint *attrib_list)
606{
607 clearError();
608
Jesse Hallb29e5e82012-04-04 16:53:42 -0700609 egl_connection_t* cnx = NULL;
610 egl_display_ptr dp = validate_display_connection(dpy, cnx);
611 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700612 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800613 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700614 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700615 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
616 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700617 return s;
618 }
619 }
620 return EGL_NO_SURFACE;
621}
Jesse Hall47743382013-02-08 11:13:46 -0800622
Mathias Agopian518ec112011-05-13 16:21:08 -0700623EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
624{
625 clearError();
626
Jesse Hallb29e5e82012-04-04 16:53:42 -0700627 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700628 if (!dp) return EGL_FALSE;
629
Jesse Hallb29e5e82012-04-04 16:53:42 -0700630 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700631 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800632 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700633
634 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800635 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700636 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700637 _s.terminate();
638 }
639 return result;
640}
641
642EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
643 EGLint attribute, EGLint *value)
644{
645 clearError();
646
Jesse Hallb29e5e82012-04-04 16:53:42 -0700647 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700648 if (!dp) return EGL_FALSE;
649
Jesse Hallb29e5e82012-04-04 16:53:42 -0700650 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700651 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800652 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700653
Mathias Agopian518ec112011-05-13 16:21:08 -0700654 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800655 return s->cnx->egl.eglQuerySurface(
656 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700657}
658
Jamie Gennise8696a42012-01-15 18:54:57 -0800659void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800660 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800661 clearError();
662
Jesse Hallb29e5e82012-04-04 16:53:42 -0700663 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800664 if (!dp) {
665 return;
666 }
667
Jesse Hallb29e5e82012-04-04 16:53:42 -0700668 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800669 if (!_s.get()) {
670 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800671 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800672}
673
Mathias Agopian518ec112011-05-13 16:21:08 -0700674// ----------------------------------------------------------------------------
675// Contexts
676// ----------------------------------------------------------------------------
677
678EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
679 EGLContext share_list, const EGLint *attrib_list)
680{
681 clearError();
682
Jesse Hallb29e5e82012-04-04 16:53:42 -0700683 egl_connection_t* cnx = NULL;
684 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700685 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700686 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700687 if (!ContextRef(dp.get(), share_list).get()) {
688 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
689 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700690 egl_context_t* const c = get_context(share_list);
691 share_list = c->context;
692 }
693 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800694 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700695 if (context != EGL_NO_CONTEXT) {
696 // figure out if it's a GLESv1 or GLESv2
697 int version = 0;
698 if (attrib_list) {
699 while (*attrib_list != EGL_NONE) {
700 GLint attr = *attrib_list++;
701 GLint value = *attrib_list++;
702 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
703 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800704 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800705 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800706 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700707 }
708 }
709 };
710 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700711 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
712 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 return c;
714 }
715 }
716 return EGL_NO_CONTEXT;
717}
718
719EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
720{
721 clearError();
722
Jesse Hallb29e5e82012-04-04 16:53:42 -0700723 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700724 if (!dp)
725 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700726
Jesse Hallb29e5e82012-04-04 16:53:42 -0700727 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700728 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800729 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800730
Mathias Agopian518ec112011-05-13 16:21:08 -0700731 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800732 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700733 if (result == EGL_TRUE) {
734 _c.terminate();
735 }
736 return result;
737}
738
Mathias Agopian518ec112011-05-13 16:21:08 -0700739EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
740 EGLSurface read, EGLContext ctx)
741{
742 clearError();
743
Jesse Hallb29e5e82012-04-04 16:53:42 -0700744 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800745 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700746
Mathias Agopian5b287a62011-05-16 18:58:55 -0700747 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
748 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
749 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700750 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
751 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800752 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700753 }
754
755 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700756 ContextRef _c(dp.get(), ctx);
757 SurfaceRef _d(dp.get(), draw);
758 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700759
760 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700761 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700762 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800763 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700764 }
765
766 // these are the underlying implementation's object
767 EGLContext impl_ctx = EGL_NO_CONTEXT;
768 EGLSurface impl_draw = EGL_NO_SURFACE;
769 EGLSurface impl_read = EGL_NO_SURFACE;
770
771 // these are our objects structs passed in
772 egl_context_t * c = NULL;
773 egl_surface_t const * d = NULL;
774 egl_surface_t const * r = NULL;
775
776 // these are the current objects structs
777 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800778
Mathias Agopian518ec112011-05-13 16:21:08 -0700779 if (ctx != EGL_NO_CONTEXT) {
780 c = get_context(ctx);
781 impl_ctx = c->context;
782 } else {
783 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700784 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
785 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800786 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700787 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700788 if (cur_c == NULL) {
789 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700790 // not an error, there is just no current context.
791 return EGL_TRUE;
792 }
793 }
794
795 // retrieve the underlying implementation's draw EGLSurface
796 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800797 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 impl_draw = d->surface;
800 }
801
802 // retrieve the underlying implementation's read EGLSurface
803 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800804 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700805 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700806 impl_read = r->surface;
807 }
808
Mathias Agopian518ec112011-05-13 16:21:08 -0700809
Jesse Hallb29e5e82012-04-04 16:53:42 -0700810 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800811 draw, read, ctx,
812 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700813
814 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800815 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700816 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
817 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700818 _c.acquire();
819 _r.acquire();
820 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700821 } else {
822 setGLHooksThreadSpecific(&gHooksNoContext);
823 egl_tls_t::setContext(EGL_NO_CONTEXT);
824 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700825 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000826 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700827 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -0800828 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700829 }
830 return result;
831}
832
833
834EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
835 EGLint attribute, EGLint *value)
836{
837 clearError();
838
Jesse Hallb29e5e82012-04-04 16:53:42 -0700839 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700840 if (!dp) return EGL_FALSE;
841
Jesse Hallb29e5e82012-04-04 16:53:42 -0700842 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -0800843 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700844
Mathias Agopian518ec112011-05-13 16:21:08 -0700845 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800846 return c->cnx->egl.eglQueryContext(
847 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700848
Mathias Agopian518ec112011-05-13 16:21:08 -0700849}
850
851EGLContext eglGetCurrentContext(void)
852{
853 // could be called before eglInitialize(), but we wouldn't have a context
854 // then, and this function would correctly return EGL_NO_CONTEXT.
855
856 clearError();
857
858 EGLContext ctx = getContext();
859 return ctx;
860}
861
862EGLSurface eglGetCurrentSurface(EGLint readdraw)
863{
864 // could be called before eglInitialize(), but we wouldn't have a context
865 // then, and this function would correctly return EGL_NO_SURFACE.
866
867 clearError();
868
869 EGLContext ctx = getContext();
870 if (ctx) {
871 egl_context_t const * const c = get_context(ctx);
872 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
873 switch (readdraw) {
874 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800875 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700876 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
877 }
878 }
879 return EGL_NO_SURFACE;
880}
881
882EGLDisplay eglGetCurrentDisplay(void)
883{
884 // could be called before eglInitialize(), but we wouldn't have a context
885 // then, and this function would correctly return EGL_NO_DISPLAY.
886
887 clearError();
888
889 EGLContext ctx = getContext();
890 if (ctx) {
891 egl_context_t const * const c = get_context(ctx);
892 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
893 return c->dpy;
894 }
895 return EGL_NO_DISPLAY;
896}
897
898EGLBoolean eglWaitGL(void)
899{
Mathias Agopian518ec112011-05-13 16:21:08 -0700900 clearError();
901
Mathias Agopianada798b2012-02-13 17:09:30 -0800902 egl_connection_t* const cnx = &gEGLImpl;
903 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800904 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800905
906 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700907}
908
909EGLBoolean eglWaitNative(EGLint engine)
910{
Mathias Agopian518ec112011-05-13 16:21:08 -0700911 clearError();
912
Mathias Agopianada798b2012-02-13 17:09:30 -0800913 egl_connection_t* const cnx = &gEGLImpl;
914 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800915 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800916
917 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700918}
919
920EGLint eglGetError(void)
921{
Mathias Agopianada798b2012-02-13 17:09:30 -0800922 EGLint err = EGL_SUCCESS;
923 egl_connection_t* const cnx = &gEGLImpl;
924 if (cnx->dso) {
925 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700926 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800927 if (err == EGL_SUCCESS) {
928 err = egl_tls_t::getError();
929 }
930 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700931}
932
Michael Chockc0ec5e22014-01-27 08:14:33 -0800933static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700934 const char* procname) {
935 const egl_connection_t* cnx = &gEGLImpl;
936 void* proc = NULL;
937
Michael Chockc0ec5e22014-01-27 08:14:33 -0800938 proc = dlsym(cnx->libEgl, procname);
939 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
940
Jesse Hallc07b5202013-07-04 12:08:16 -0700941 proc = dlsym(cnx->libGles2, procname);
942 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
943
944 proc = dlsym(cnx->libGles1, procname);
945 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
946
947 return NULL;
948}
949
Mathias Agopian518ec112011-05-13 16:21:08 -0700950__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
951{
952 // eglGetProcAddress() could be the very first function called
953 // in which case we must make sure we've initialized ourselves, this
954 // happens the first time egl_get_display() is called.
955
956 clearError();
957
958 if (egl_init_drivers() == EGL_FALSE) {
959 setError(EGL_BAD_PARAMETER, NULL);
960 return NULL;
961 }
962
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700963 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700964 return NULL;
965 }
966
Mathias Agopian518ec112011-05-13 16:21:08 -0700967 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700968 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 if (addr) return addr;
970
Michael Chockc0ec5e22014-01-27 08:14:33 -0800971 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700972 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700973
Mathias Agopian518ec112011-05-13 16:21:08 -0700974 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
975 pthread_mutex_lock(&sExtensionMapMutex);
976
977 /*
978 * Since eglGetProcAddress() is not associated to anything, it needs
979 * to return a function pointer that "works" regardless of what
980 * the current context is.
981 *
982 * For this reason, we return a "forwarder", a small stub that takes
983 * care of calling the function associated with the context
984 * currently bound.
985 *
986 * We first look for extensions we've already resolved, if we're seeing
987 * this extension for the first time, we go through all our
988 * implementations and call eglGetProcAddress() and record the
989 * result in the appropriate implementation hooks and return the
990 * address of the forwarder corresponding to that hook set.
991 *
992 */
993
994 const String8 name(procname);
995 addr = sGLExtentionMap.valueFor(name);
996 const int slot = sGLExtentionSlot;
997
Steve Blocke6f43dd2012-01-06 19:20:56 +0000998 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700999 "no more slots for eglGetProcAddress(\"%s\")",
1000 procname);
1001
1002 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1003 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001004
1005 egl_connection_t* const cnx = &gEGLImpl;
1006 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001007 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001008 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001009 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1010 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001011 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001012 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001013 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001014
Mathias Agopian518ec112011-05-13 16:21:08 -07001015 if (found) {
1016 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001017 sGLExtentionMap.add(name, addr);
1018 sGLExtentionSlot++;
1019 }
1020 }
1021
1022 pthread_mutex_unlock(&sExtensionMapMutex);
1023 return addr;
1024}
1025
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001026class FrameCompletionThread : public Thread {
1027public:
1028
1029 static void queueSync(EGLSyncKHR sync) {
1030 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1031 static bool running = false;
1032 if (!running) {
1033 thread->run("GPUFrameCompletion");
1034 running = true;
1035 }
1036 {
1037 Mutex::Autolock lock(thread->mMutex);
1038 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1039 thread->mFramesQueued).string());
1040 thread->mQueue.push_back(sync);
1041 thread->mCondition.signal();
1042 thread->mFramesQueued++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001043 ATRACE_INT("GPU Frames Outstanding", int32_t(thread->mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001044 }
1045 }
1046
1047private:
1048 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1049
1050 virtual bool threadLoop() {
1051 EGLSyncKHR sync;
1052 uint32_t frameNum;
1053 {
1054 Mutex::Autolock lock(mMutex);
1055 while (mQueue.isEmpty()) {
1056 mCondition.wait(mMutex);
1057 }
1058 sync = mQueue[0];
1059 frameNum = mFramesCompleted;
1060 }
1061 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1062 {
1063 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1064 frameNum).string());
1065 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1066 if (result == EGL_FALSE) {
1067 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1068 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1069 ALOGE("FrameCompletion: timeout waiting for fence");
1070 }
1071 eglDestroySyncKHR(dpy, sync);
1072 }
1073 {
1074 Mutex::Autolock lock(mMutex);
1075 mQueue.removeAt(0);
1076 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001077 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001078 }
1079 return true;
1080 }
1081
1082 uint32_t mFramesQueued;
1083 uint32_t mFramesCompleted;
1084 Vector<EGLSyncKHR> mQueue;
1085 Condition mCondition;
1086 Mutex mMutex;
1087};
1088
Dan Stozaa894d082015-02-19 15:27:36 -08001089EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1090 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001091{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001092 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001093 clearError();
1094
Jesse Hallb29e5e82012-04-04 16:53:42 -07001095 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001096 if (!dp) return EGL_FALSE;
1097
Jesse Hallb29e5e82012-04-04 16:53:42 -07001098 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001099 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001100 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001101
Mathias Agopian518ec112011-05-13 16:21:08 -07001102 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001103
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001104 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1105 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1106 if (sync != EGL_NO_SYNC_KHR) {
1107 FrameCompletionThread::queueSync(sync);
1108 }
1109 }
1110
Mathias Agopian7db993a2012-03-25 00:49:46 -07001111 if (CC_UNLIKELY(dp->finishOnSwap)) {
1112 uint32_t pixel;
1113 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1114 if (c) {
1115 // glReadPixels() ensures that the frame is complete
1116 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1117 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1118 }
1119 }
1120
Dan Stozaa894d082015-02-19 15:27:36 -08001121 if (n_rects == 0) {
1122 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1123 }
1124
1125 Vector<android_native_rect_t> androidRects;
1126 for (int r = 0; r < n_rects; ++r) {
1127 int offset = r * 4;
1128 int x = rects[offset];
1129 int y = rects[offset + 1];
1130 int width = rects[offset + 2];
1131 int height = rects[offset + 3];
1132 android_native_rect_t androidRect;
1133 androidRect.left = x;
1134 androidRect.top = y + height;
1135 androidRect.right = x + width;
1136 androidRect.bottom = y;
1137 androidRects.push_back(androidRect);
1138 }
1139 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1140 androidRects.size());
1141
1142 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1143 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1144 rects, n_rects);
1145 } else {
1146 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1147 }
1148}
1149
1150EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1151{
1152 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001153}
1154
1155EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1156 NativePixmapType target)
1157{
1158 clearError();
1159
Jesse Hallb29e5e82012-04-04 16:53:42 -07001160 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001161 if (!dp) return EGL_FALSE;
1162
Jesse Hallb29e5e82012-04-04 16:53:42 -07001163 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001164 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001165 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001166
Mathias Agopian518ec112011-05-13 16:21:08 -07001167 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001168 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001169}
1170
1171const char* eglQueryString(EGLDisplay dpy, EGLint name)
1172{
1173 clearError();
1174
Chia-I Wue57d1352016-08-15 16:10:02 +08001175 // Generate an error quietly when client extensions (as defined by
1176 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1177 // validate_display to generate the error as validate_display would log
1178 // the error, which can be misleading.
1179 //
1180 // If we want to support EGL_EXT_client_extensions later, we can return
1181 // the client extension string here instead.
1182 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001183 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001184
Jesse Hallb29e5e82012-04-04 16:53:42 -07001185 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001186 if (!dp) return (const char *) NULL;
1187
1188 switch (name) {
1189 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001190 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001191 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001192 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001193 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001194 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001195 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001196 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001197 default:
1198 break;
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;
Mathias Agopian737b8962017-02-24 14:32:05 -08001219 default:
1220 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001221 }
1222 return setError(EGL_BAD_PARAMETER, (const char *)0);
1223}
Mathias Agopian518ec112011-05-13 16:21:08 -07001224
1225// ----------------------------------------------------------------------------
1226// EGL 1.1
1227// ----------------------------------------------------------------------------
1228
1229EGLBoolean eglSurfaceAttrib(
1230 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1231{
1232 clearError();
1233
Jesse Hallb29e5e82012-04-04 16:53:42 -07001234 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001235 if (!dp) return EGL_FALSE;
1236
Jesse Hallb29e5e82012-04-04 16:53:42 -07001237 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001238 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001239 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001240
Pablo Ceballosc18be292016-05-31 14:55:42 -07001241 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001242
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001243 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001244 if (!s->win.get()) {
1245 setError(EGL_BAD_SURFACE, EGL_FALSE);
1246 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001247 int err = native_window_set_auto_refresh(s->win.get(), value ? true : false);
1248 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)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()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001253 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001254 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001255 int err = native_window_enable_frame_timestamps(s->win.get(), value ? true : false);
1256 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001257 }
1258
Mathias Agopian518ec112011-05-13 16:21:08 -07001259 if (s->cnx->egl.eglSurfaceAttrib) {
1260 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001261 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001262 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001263 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001264}
1265
1266EGLBoolean eglBindTexImage(
1267 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1268{
1269 clearError();
1270
Jesse Hallb29e5e82012-04-04 16:53:42 -07001271 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 if (!dp) return EGL_FALSE;
1273
Jesse Hallb29e5e82012-04-04 16:53:42 -07001274 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001275 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001276 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001277
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 egl_surface_t const * const s = get_surface(surface);
1279 if (s->cnx->egl.eglBindTexImage) {
1280 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001281 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001282 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001283 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001284}
1285
1286EGLBoolean eglReleaseTexImage(
1287 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1288{
1289 clearError();
1290
Jesse Hallb29e5e82012-04-04 16:53:42 -07001291 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001292 if (!dp) return EGL_FALSE;
1293
Jesse Hallb29e5e82012-04-04 16:53:42 -07001294 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001295 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001296 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001297
Mathias Agopian518ec112011-05-13 16:21:08 -07001298 egl_surface_t const * const s = get_surface(surface);
1299 if (s->cnx->egl.eglReleaseTexImage) {
1300 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001301 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001302 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001303 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001304}
1305
1306EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1307{
1308 clearError();
1309
Jesse Hallb29e5e82012-04-04 16:53:42 -07001310 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001311 if (!dp) return EGL_FALSE;
1312
1313 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001314 egl_connection_t* const cnx = &gEGLImpl;
1315 if (cnx->dso && cnx->egl.eglSwapInterval) {
1316 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001317 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001318
Mathias Agopian518ec112011-05-13 16:21:08 -07001319 return res;
1320}
1321
1322
1323// ----------------------------------------------------------------------------
1324// EGL 1.2
1325// ----------------------------------------------------------------------------
1326
1327EGLBoolean eglWaitClient(void)
1328{
1329 clearError();
1330
Mathias Agopianada798b2012-02-13 17:09:30 -08001331 egl_connection_t* const cnx = &gEGLImpl;
1332 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001333 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001334
1335 EGLBoolean res;
1336 if (cnx->egl.eglWaitClient) {
1337 res = cnx->egl.eglWaitClient();
1338 } else {
1339 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001340 }
1341 return res;
1342}
1343
1344EGLBoolean eglBindAPI(EGLenum api)
1345{
1346 clearError();
1347
1348 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001349 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001350 }
1351
1352 // bind this API on all EGLs
1353 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001354 egl_connection_t* const cnx = &gEGLImpl;
1355 if (cnx->dso && cnx->egl.eglBindAPI) {
1356 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001357 }
1358 return res;
1359}
1360
1361EGLenum eglQueryAPI(void)
1362{
1363 clearError();
1364
1365 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001366 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001367 }
1368
Mathias Agopianada798b2012-02-13 17:09:30 -08001369 egl_connection_t* const cnx = &gEGLImpl;
1370 if (cnx->dso && cnx->egl.eglQueryAPI) {
1371 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001372 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001373
Mathias Agopian518ec112011-05-13 16:21:08 -07001374 // or, it can only be OpenGL ES
1375 return EGL_OPENGL_ES_API;
1376}
1377
1378EGLBoolean eglReleaseThread(void)
1379{
1380 clearError();
1381
Mathias Agopianada798b2012-02-13 17:09:30 -08001382 egl_connection_t* const cnx = &gEGLImpl;
1383 if (cnx->dso && cnx->egl.eglReleaseThread) {
1384 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001385 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301386
1387 // If there is context bound to the thread, release it
1388 egl_display_t::loseCurrent(get_context(getContext()));
1389
Mathias Agopian518ec112011-05-13 16:21:08 -07001390 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001391 return EGL_TRUE;
1392}
1393
1394EGLSurface eglCreatePbufferFromClientBuffer(
1395 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1396 EGLConfig config, const EGLint *attrib_list)
1397{
1398 clearError();
1399
Jesse Hallb29e5e82012-04-04 16:53:42 -07001400 egl_connection_t* cnx = NULL;
1401 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1402 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001403 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1404 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001405 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001406 }
1407 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1408}
1409
1410// ----------------------------------------------------------------------------
1411// EGL_EGLEXT_VERSION 3
1412// ----------------------------------------------------------------------------
1413
1414EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1415 const EGLint *attrib_list)
1416{
1417 clearError();
1418
Jesse Hallb29e5e82012-04-04 16:53:42 -07001419 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001420 if (!dp) return EGL_FALSE;
1421
Jesse Hallb29e5e82012-04-04 16:53:42 -07001422 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001423 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001424 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001425
1426 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001427 if (s->cnx->egl.eglLockSurfaceKHR) {
1428 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001429 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001431 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001432}
1433
1434EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1435{
1436 clearError();
1437
Jesse Hallb29e5e82012-04-04 16:53:42 -07001438 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001439 if (!dp) return EGL_FALSE;
1440
Jesse Hallb29e5e82012-04-04 16:53:42 -07001441 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001442 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001443 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001444
1445 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001446 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001447 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001448 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001449 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450}
1451
1452EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1453 EGLClientBuffer buffer, const EGLint *attrib_list)
1454{
1455 clearError();
1456
Jesse Hallb29e5e82012-04-04 16:53:42 -07001457 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001458 if (!dp) return EGL_NO_IMAGE_KHR;
1459
Jesse Hallb29e5e82012-04-04 16:53:42 -07001460 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001461 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001462
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001463 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1464 egl_connection_t* const cnx = &gEGLImpl;
1465 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1466 result = cnx->egl.eglCreateImageKHR(
1467 dp->disp.dpy,
1468 c ? c->context : EGL_NO_CONTEXT,
1469 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001470 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001471 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001472}
1473
1474EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1475{
1476 clearError();
1477
Jesse Hallb29e5e82012-04-04 16:53:42 -07001478 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001479 if (!dp) return EGL_FALSE;
1480
Steven Holte646a5c52012-06-04 20:02:11 -07001481 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001482 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001483 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001484 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001485 }
Steven Holte646a5c52012-06-04 20:02:11 -07001486 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001487}
1488
1489// ----------------------------------------------------------------------------
1490// EGL_EGLEXT_VERSION 5
1491// ----------------------------------------------------------------------------
1492
1493
1494EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1495{
1496 clearError();
1497
Jesse Hallb29e5e82012-04-04 16:53:42 -07001498 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001499 if (!dp) return EGL_NO_SYNC_KHR;
1500
Mathias Agopian518ec112011-05-13 16:21:08 -07001501 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001502 egl_connection_t* const cnx = &gEGLImpl;
1503 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1504 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001506 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001507}
1508
1509EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1510{
1511 clearError();
1512
Jesse Hallb29e5e82012-04-04 16:53:42 -07001513 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001514 if (!dp) return EGL_FALSE;
1515
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001517 egl_connection_t* const cnx = &gEGLImpl;
1518 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1519 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001520 }
1521 return result;
1522}
1523
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001524EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1525 clearError();
1526
1527 const egl_display_ptr dp = validate_display(dpy);
1528 if (!dp) return EGL_FALSE;
1529
1530 EGLBoolean result = EGL_FALSE;
1531 egl_connection_t* const cnx = &gEGLImpl;
1532 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1533 result = cnx->egl.eglSignalSyncKHR(
1534 dp->disp.dpy, sync, mode);
1535 }
1536 return result;
1537}
1538
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001539EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1540 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001541{
1542 clearError();
1543
Jesse Hallb29e5e82012-04-04 16:53:42 -07001544 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001545 if (!dp) return EGL_FALSE;
1546
Mathias Agopian737b8962017-02-24 14:32:05 -08001547 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001548 egl_connection_t* const cnx = &gEGLImpl;
1549 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1550 result = cnx->egl.eglClientWaitSyncKHR(
1551 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001552 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001553 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001554}
1555
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001556EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1557 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001558{
1559 clearError();
1560
Jesse Hallb29e5e82012-04-04 16:53:42 -07001561 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001562 if (!dp) return EGL_FALSE;
1563
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001564 EGLBoolean result = EGL_FALSE;
1565 egl_connection_t* const cnx = &gEGLImpl;
1566 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1567 result = cnx->egl.eglGetSyncAttribKHR(
1568 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001569 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001570 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001571}
1572
Season Li000d88f2015-07-01 11:39:40 -07001573EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1574{
1575 clearError();
1576
1577 const egl_display_ptr dp = validate_display(dpy);
1578 if (!dp) return EGL_NO_STREAM_KHR;
1579
1580 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1581 egl_connection_t* const cnx = &gEGLImpl;
1582 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1583 result = cnx->egl.eglCreateStreamKHR(
1584 dp->disp.dpy, attrib_list);
1585 }
1586 return result;
1587}
1588
1589EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1590{
1591 clearError();
1592
1593 const egl_display_ptr dp = validate_display(dpy);
1594 if (!dp) return EGL_FALSE;
1595
1596 EGLBoolean result = EGL_FALSE;
1597 egl_connection_t* const cnx = &gEGLImpl;
1598 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1599 result = cnx->egl.eglDestroyStreamKHR(
1600 dp->disp.dpy, stream);
1601 }
1602 return result;
1603}
1604
1605EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1606 EGLenum attribute, EGLint value)
1607{
1608 clearError();
1609
1610 const egl_display_ptr dp = validate_display(dpy);
1611 if (!dp) return EGL_FALSE;
1612
1613 EGLBoolean result = EGL_FALSE;
1614 egl_connection_t* const cnx = &gEGLImpl;
1615 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1616 result = cnx->egl.eglStreamAttribKHR(
1617 dp->disp.dpy, stream, attribute, value);
1618 }
1619 return result;
1620}
1621
1622EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1623 EGLenum attribute, EGLint *value)
1624{
1625 clearError();
1626
1627 const egl_display_ptr dp = validate_display(dpy);
1628 if (!dp) return EGL_FALSE;
1629
1630 EGLBoolean result = EGL_FALSE;
1631 egl_connection_t* const cnx = &gEGLImpl;
1632 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1633 result = cnx->egl.eglQueryStreamKHR(
1634 dp->disp.dpy, stream, attribute, value);
1635 }
1636 return result;
1637}
1638
1639EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1640 EGLenum attribute, EGLuint64KHR *value)
1641{
1642 clearError();
1643
1644 const egl_display_ptr dp = validate_display(dpy);
1645 if (!dp) return EGL_FALSE;
1646
1647 EGLBoolean result = EGL_FALSE;
1648 egl_connection_t* const cnx = &gEGLImpl;
1649 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1650 result = cnx->egl.eglQueryStreamu64KHR(
1651 dp->disp.dpy, stream, attribute, value);
1652 }
1653 return result;
1654}
1655
1656EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1657 EGLenum attribute, EGLTimeKHR *value)
1658{
1659 clearError();
1660
1661 const egl_display_ptr dp = validate_display(dpy);
1662 if (!dp) return EGL_FALSE;
1663
1664 EGLBoolean result = EGL_FALSE;
1665 egl_connection_t* const cnx = &gEGLImpl;
1666 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1667 result = cnx->egl.eglQueryStreamTimeKHR(
1668 dp->disp.dpy, stream, attribute, value);
1669 }
1670 return result;
1671}
1672
1673EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1674 EGLStreamKHR stream, const EGLint *attrib_list)
1675{
1676 clearError();
1677
1678 egl_display_ptr dp = validate_display(dpy);
1679 if (!dp) return EGL_NO_SURFACE;
1680
1681 egl_connection_t* const cnx = &gEGLImpl;
1682 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1683 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1684 dp->disp.dpy, config, stream, attrib_list);
1685 if (surface != EGL_NO_SURFACE) {
1686 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1687 surface, cnx);
1688 return s;
1689 }
1690 }
1691 return EGL_NO_SURFACE;
1692}
1693
1694EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1695 EGLStreamKHR stream)
1696{
1697 clearError();
1698
1699 const egl_display_ptr dp = validate_display(dpy);
1700 if (!dp) return EGL_FALSE;
1701
1702 EGLBoolean result = EGL_FALSE;
1703 egl_connection_t* const cnx = &gEGLImpl;
1704 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1705 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1706 dp->disp.dpy, stream);
1707 }
1708 return result;
1709}
1710
1711EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1712 EGLStreamKHR stream)
1713{
1714 clearError();
1715
1716 const egl_display_ptr dp = validate_display(dpy);
1717 if (!dp) return EGL_FALSE;
1718
1719 EGLBoolean result = EGL_FALSE;
1720 egl_connection_t* const cnx = &gEGLImpl;
1721 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1722 result = cnx->egl.eglStreamConsumerAcquireKHR(
1723 dp->disp.dpy, stream);
1724 }
1725 return result;
1726}
1727
1728EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1729 EGLStreamKHR stream)
1730{
1731 clearError();
1732
1733 const egl_display_ptr dp = validate_display(dpy);
1734 if (!dp) return EGL_FALSE;
1735
1736 EGLBoolean result = EGL_FALSE;
1737 egl_connection_t* const cnx = &gEGLImpl;
1738 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1739 result = cnx->egl.eglStreamConsumerReleaseKHR(
1740 dp->disp.dpy, stream);
1741 }
1742 return result;
1743}
1744
1745EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1746 EGLDisplay dpy, EGLStreamKHR stream)
1747{
1748 clearError();
1749
1750 const egl_display_ptr dp = validate_display(dpy);
1751 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1752
1753 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1754 egl_connection_t* const cnx = &gEGLImpl;
1755 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1756 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1757 dp->disp.dpy, stream);
1758 }
1759 return result;
1760}
1761
1762EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1763 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1764{
1765 clearError();
1766
1767 const egl_display_ptr dp = validate_display(dpy);
1768 if (!dp) return EGL_NO_STREAM_KHR;
1769
1770 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1771 egl_connection_t* const cnx = &gEGLImpl;
1772 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1773 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1774 dp->disp.dpy, file_descriptor);
1775 }
1776 return result;
1777}
1778
Mathias Agopian518ec112011-05-13 16:21:08 -07001779// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001780// EGL_EGLEXT_VERSION 15
1781// ----------------------------------------------------------------------------
1782
1783EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1784 clearError();
1785 const egl_display_ptr dp = validate_display(dpy);
1786 if (!dp) return EGL_FALSE;
1787 EGLint result = EGL_FALSE;
1788 egl_connection_t* const cnx = &gEGLImpl;
1789 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1790 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1791 }
1792 return result;
1793}
1794
1795// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001796// ANDROID extensions
1797// ----------------------------------------------------------------------------
1798
Jamie Gennis331841b2012-09-06 14:52:00 -07001799EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1800{
1801 clearError();
1802
1803 const egl_display_ptr dp = validate_display(dpy);
1804 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1805
1806 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1807 egl_connection_t* const cnx = &gEGLImpl;
1808 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1809 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1810 }
1811 return result;
1812}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001813
Andy McFadden72841452013-03-01 16:25:32 -08001814EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1815 EGLnsecsANDROID time)
1816{
1817 clearError();
1818
1819 const egl_display_ptr dp = validate_display(dpy);
1820 if (!dp) {
1821 return EGL_FALSE;
1822 }
1823
1824 SurfaceRef _s(dp.get(), surface);
1825 if (!_s.get()) {
1826 setError(EGL_BAD_SURFACE, EGL_FALSE);
1827 return EGL_FALSE;
1828 }
1829
1830 egl_surface_t const * const s = get_surface(surface);
1831 native_window_set_buffers_timestamp(s->win.get(), time);
1832
1833 return EGL_TRUE;
1834}
1835
Craig Donner05249fc2016-01-15 19:33:55 -08001836EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1837{
1838 clearError();
1839
Craig Donnere96a3252017-02-02 12:13:34 -08001840 uint64_t producerUsage = 0;
1841 uint64_t consumerUsage = 0;
Craig Donner05249fc2016-01-15 19:33:55 -08001842 uint32_t width = 0;
1843 uint32_t height = 0;
1844 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001845 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001846 uint32_t red_size = 0;
1847 uint32_t green_size = 0;
1848 uint32_t blue_size = 0;
1849 uint32_t alpha_size = 0;
1850
Craig Donnerb40504a2016-06-03 17:54:25 -07001851#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001852 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001853 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001854 target = value; \
1855 } else { \
1856 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1857 } \
1858 break
1859
1860 if (attrib_list) {
1861 while (*attrib_list != EGL_NONE) {
1862 GLint attr = *attrib_list++;
1863 GLint value = *attrib_list++;
1864 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001865 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1866 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1867 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1868 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1869 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1870 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001871 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001872 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1873 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001874 producerUsage |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001875 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001876 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001877 producerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
Craig Donner05249fc2016-01-15 19:33:55 -08001878 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001879 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001880 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
Craig Donner05249fc2016-01-15 19:33:55 -08001881 }
Craig Donner05249fc2016-01-15 19:33:55 -08001882 break;
1883 default:
1884 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1885 }
1886 }
1887 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001888#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001889
1890 // Validate format.
1891 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1892 if (alpha_size == 8) {
1893 format = HAL_PIXEL_FORMAT_RGBA_8888;
1894 } else {
1895 format = HAL_PIXEL_FORMAT_RGB_888;
1896 }
1897 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1898 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001899 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001900 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001901 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001902 red_size, green_size, blue_size, alpha_size);
1903 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1904 }
1905
Craig Donner68671532016-07-18 10:19:54 -07001906#define CHECK_ERROR_CONDITION(message) \
1907 if (err != NO_ERROR) { \
1908 ALOGE(message); \
1909 goto error_condition; \
1910 }
1911
1912 // The holder is used to destroy the buffer if an error occurs.
1913 GraphicBuffer* gBuffer = new GraphicBuffer();
1914 sp<IServiceManager> sm = defaultServiceManager();
1915 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1916 sp<IBinder> allocator;
1917 Parcel sc_data, sc_reply, data, reply;
1918 status_t err = NO_ERROR;
1919 if (sm == NULL) {
1920 ALOGE("Unable to connect to ServiceManager");
1921 goto error_condition;
1922 }
1923
1924 // Obtain an allocator.
1925 if (surfaceFlinger == NULL) {
1926 ALOGE("Unable to connect to SurfaceFlinger");
1927 goto error_condition;
1928 }
1929 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1930 err = surfaceFlinger->transact(
1931 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1932 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1933 allocator = sc_reply.readStrongBinder();
1934
1935 if (allocator == NULL) {
1936 ALOGE("Unable to obtain an ISurfaceComposer");
1937 goto error_condition;
1938 }
1939 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1940 err = data.writeUint32(width);
1941 CHECK_ERROR_CONDITION("Unable to write width");
1942 err = data.writeUint32(height);
1943 CHECK_ERROR_CONDITION("Unable to write height");
1944 err = data.writeInt32(static_cast<int32_t>(format));
1945 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001946 err = data.writeUint32(layer_count);
1947 CHECK_ERROR_CONDITION("Unable to write layer count");
Craig Donnere96a3252017-02-02 12:13:34 -08001948 err = data.writeUint64(producerUsage);
1949 CHECK_ERROR_CONDITION("Unable to write producer usage");
1950 err = data.writeUint64(consumerUsage);
1951 CHECK_ERROR_CONDITION("Unable to write consumer usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001952 err = data.writeUtf8AsUtf16(
1953 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1954 std::to_string(getpid()) + ']');
1955 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001956 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1957 &reply);
1958 CHECK_ERROR_CONDITION(
1959 "Unable to request buffer allocation from surface composer");
1960 err = reply.readInt32();
1961 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1962 err = reply.read(*gBuffer);
1963 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1964
1965 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001966 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001967 ALOGE("Unable to create native buffer "
Craig Donnere96a3252017-02-02 12:13:34 -08001968 "{ w=%u, h=%u, f=%u, pu=%" PRIx64 " cu=%" PRIx64 ", lc=%u} %#x",
1969 width, height, format, producerUsage, consumerUsage,
1970 layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001971 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001972 }
Craig Donnere96a3252017-02-02 12:13:34 -08001973 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, pu=%" PRIx64
1974 " cu=%" PRIx64 ", lc=%u}",
1975 gBuffer, width, height, format, producerUsage, consumerUsage,
1976 layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001977 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001978
1979#undef CHECK_ERROR_CONDITION
1980
1981error_condition:
1982 // Delete the buffer.
1983 sp<GraphicBuffer> holder(gBuffer);
1984 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001985}
1986
Craig Donner60761072017-01-27 12:30:44 -08001987EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1988 clearError();
1989
1990 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1991
Mathias Agopian89ed4c82017-02-09 18:48:34 -08001992 const GraphicBuffer* graphicBuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
Craig Donner60761072017-01-27 12:30:44 -08001993 return static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
1994}
1995
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001996// ----------------------------------------------------------------------------
1997// NVIDIA extensions
1998// ----------------------------------------------------------------------------
1999EGLuint64NV eglGetSystemTimeFrequencyNV()
2000{
2001 clearError();
2002
2003 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002004 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002005 }
2006
2007 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002008 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002009
Mathias Agopianada798b2012-02-13 17:09:30 -08002010 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2011 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002012 }
2013
Mathias Agopian737b8962017-02-24 14:32:05 -08002014 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002015}
2016
2017EGLuint64NV eglGetSystemTimeNV()
2018{
2019 clearError();
2020
2021 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002022 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002023 }
2024
2025 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002026 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002027
Mathias Agopianada798b2012-02-13 17:09:30 -08002028 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2029 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002030 }
2031
Mathias Agopian737b8962017-02-24 14:32:05 -08002032 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002033}
Dan Stozaa894d082015-02-19 15:27:36 -08002034
2035// ----------------------------------------------------------------------------
2036// Partial update extension
2037// ----------------------------------------------------------------------------
2038EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2039 EGLint *rects, EGLint n_rects)
2040{
2041 clearError();
2042
2043 const egl_display_ptr dp = validate_display(dpy);
2044 if (!dp) {
2045 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2046 return EGL_FALSE;
2047 }
2048
2049 SurfaceRef _s(dp.get(), surface);
2050 if (!_s.get()) {
2051 setError(EGL_BAD_SURFACE, EGL_FALSE);
2052 return EGL_FALSE;
2053 }
2054
2055 egl_surface_t const * const s = get_surface(surface);
2056 if (s->cnx->egl.eglSetDamageRegionKHR) {
2057 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2058 rects, n_rects);
2059 }
2060
2061 return EGL_FALSE;
2062}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002063
Brian Anderson1049d1d2016-12-16 17:25:57 -08002064EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2065 EGLuint64KHR *frameId) {
2066 clearError();
2067
2068 const egl_display_ptr dp = validate_display(dpy);
2069 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002070 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002071 }
2072
2073 SurfaceRef _s(dp.get(), surface);
2074 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002075 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002076 }
2077
2078 egl_surface_t const * const s = get_surface(surface);
2079
2080 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002081 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002082 }
2083
2084 uint64_t nextFrameId = 0;
2085 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2086
2087 if (ret != NO_ERROR) {
2088 // This should not happen. Return an error that is not in the spec
2089 // so it's obvious something is very wrong.
2090 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002091 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002092 }
2093
2094 *frameId = nextFrameId;
2095 return EGL_TRUE;
2096}
2097
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002098EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2099 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2100{
2101 clearError();
2102
2103 const egl_display_ptr dp = validate_display(dpy);
2104 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002105 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002106 }
2107
2108 SurfaceRef _s(dp.get(), surface);
2109 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002110 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002111 }
2112
2113 egl_surface_t const * const s = get_surface(surface);
2114
2115 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002116 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002117 }
2118
2119 nsecs_t* compositeDeadline = nullptr;
2120 nsecs_t* compositeInterval = nullptr;
2121 nsecs_t* compositeToPresentLatency = nullptr;
2122
2123 for (int i = 0; i < numTimestamps; i++) {
2124 switch (names[i]) {
2125 case EGL_COMPOSITE_DEADLINE_ANDROID:
2126 compositeDeadline = &values[i];
2127 break;
2128 case EGL_COMPOSITE_INTERVAL_ANDROID:
2129 compositeInterval = &values[i];
2130 break;
2131 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2132 compositeToPresentLatency = &values[i];
2133 break;
2134 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002135 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002136 }
2137 }
2138
2139 status_t ret = native_window_get_compositor_timing(s->win.get(),
2140 compositeDeadline, compositeInterval, compositeToPresentLatency);
2141
2142 switch (ret) {
2143 case NO_ERROR:
2144 return EGL_TRUE;
2145 case INVALID_OPERATION:
Mathias Agopian737b8962017-02-24 14:32:05 -08002146 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002147 default:
2148 // This should not happen. Return an error that is not in the spec
2149 // so it's obvious something is very wrong.
2150 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002151 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002152 }
2153}
2154
2155EGLBoolean eglGetCompositorTimingSupportedANDROID(
2156 EGLDisplay dpy, EGLSurface surface, EGLint name)
2157{
2158 clearError();
2159
2160 const egl_display_ptr dp = validate_display(dpy);
2161 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002162 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002163 }
2164
2165 SurfaceRef _s(dp.get(), surface);
2166 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002167 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002168 }
2169
2170 egl_surface_t const * const s = get_surface(surface);
2171
2172 ANativeWindow* window = s->win.get();
2173 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002174 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002175 }
2176
2177 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002178 case EGL_COMPOSITE_DEADLINE_ANDROID:
2179 case EGL_COMPOSITE_INTERVAL_ANDROID:
2180 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2181 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002182 default:
2183 return EGL_FALSE;
2184 }
2185}
2186
Pablo Ceballosc18be292016-05-31 14:55:42 -07002187EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002188 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002189 EGLnsecsANDROID *values)
2190{
2191 clearError();
2192
2193 const egl_display_ptr dp = validate_display(dpy);
2194 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002195 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002196 }
2197
2198 SurfaceRef _s(dp.get(), surface);
2199 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002200 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002201 }
2202
2203 egl_surface_t const * const s = get_surface(surface);
2204
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002205 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002206 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002207 }
2208
Brian Andersondbd0ea82016-07-22 09:38:59 -07002209 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002210 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002211 nsecs_t* latchTime = nullptr;
2212 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002213 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002214 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002215 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002216 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002217 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002218 nsecs_t* releaseTime = nullptr;
2219
2220 for (int i = 0; i < numTimestamps; i++) {
2221 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002222 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2223 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002224 break;
2225 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2226 acquireTime = &values[i];
2227 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002228 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2229 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002230 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002231 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2232 firstRefreshStartTime = &values[i];
2233 break;
2234 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2235 lastRefreshStartTime = &values[i];
2236 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002237 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2238 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002239 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002240 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2241 displayPresentTime = &values[i];
2242 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002243 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2244 displayRetireTime = &values[i];
2245 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002246 case EGL_DEQUEUE_READY_TIME_ANDROID:
2247 dequeueReadyTime = &values[i];
2248 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002249 case EGL_READS_DONE_TIME_ANDROID:
2250 releaseTime = &values[i];
2251 break;
2252 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002253 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002254 }
2255 }
2256
Brian Anderson1049d1d2016-12-16 17:25:57 -08002257 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002258 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002259 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002260 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002261
Brian Anderson069b3652016-07-22 10:32:47 -07002262 switch (ret) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002263 case NO_ERROR:
2264 return EGL_TRUE;
2265 case NAME_NOT_FOUND:
2266 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
2267 case INVALID_OPERATION:
2268 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2269 case BAD_VALUE:
2270 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2271 default:
2272 // This should not happen. Return an error that is not in the spec
2273 // so it's obvious something is very wrong.
2274 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2275 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002276 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002277}
2278
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002279EGLBoolean eglGetFrameTimestampSupportedANDROID(
2280 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002281{
2282 clearError();
2283
2284 const egl_display_ptr dp = validate_display(dpy);
2285 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002286 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002287 }
2288
2289 SurfaceRef _s(dp.get(), surface);
2290 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002291 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002292 }
2293
2294 egl_surface_t const * const s = get_surface(surface);
2295
2296 ANativeWindow* window = s->win.get();
2297 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002298 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002299 }
2300
2301 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002302 case EGL_COMPOSITE_DEADLINE_ANDROID:
2303 case EGL_COMPOSITE_INTERVAL_ANDROID:
2304 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002305 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002306 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002307 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2308 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2309 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002310 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002311 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002312 case EGL_READS_DONE_TIME_ANDROID:
2313 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002314 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2315 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002316 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002317 return value == 0 ? EGL_FALSE : EGL_TRUE;
2318 }
2319 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2320 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002321 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002322 return value == 0 ? EGL_FALSE : EGL_TRUE;
2323 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002324 default:
2325 return EGL_FALSE;
2326 }
2327}