blob: aed27c8f58877c47fe03e3ae3dc2da0597e06c01 [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 "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800129 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700130 ;
131
132// extensions not exposed to applications but used by the ANDROID system
133// "EGL_ANDROID_blob_cache " // strongly recommended
134// "EGL_IMG_hibernate_process " // optional
135// "EGL_ANDROID_native_fence_sync " // strongly recommended
136// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700137// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700138
139/*
140 * EGL Extensions entry-points exposed to 3rd party applications
141 * (keep in sync with gExtensionString above)
142 *
143 */
144static const extention_map_t sExtensionMap[] = {
145 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700146 { "eglLockSurfaceKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
148 { "eglUnlockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700150
151 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700152 { "eglCreateImageKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
154 { "eglDestroyImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700156
157 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
158 { "eglCreateSyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
160 { "eglDestroySyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
162 { "eglClientWaitSyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
164 { "eglSignalSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
166 { "eglGetSyncAttribKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
168
169 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800170 { "eglGetSystemTimeFrequencyNV",
171 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
172 { "eglGetSystemTimeNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700174
Mathias Agopian2bb71682013-03-27 17:32:41 -0700175 // EGL_KHR_wait_sync
176 { "eglWaitSyncKHR",
177 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700178
179 // EGL_ANDROID_presentation_time
180 { "eglPresentationTimeANDROID",
181 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800182
183 // EGL_KHR_swap_buffers_with_damage
184 { "eglSwapBuffersWithDamageKHR",
185 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
186
Craig Donner60761072017-01-27 12:30:44 -0800187 // EGL_ANDROID_create_native_client_buffer
Craig Donner05249fc2016-01-15 19:33:55 -0800188 { "eglCreateNativeClientBufferANDROID",
189 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
190
Craig Donner60761072017-01-27 12:30:44 -0800191 // EGL_ANDROID_get_native_client_buffer
192 { "eglGetNativeClientBufferANDROID",
193 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
194
Dan Stozaa894d082015-02-19 15:27:36 -0800195 // EGL_KHR_partial_update
196 { "eglSetDamageRegionKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700198
199 { "eglCreateStreamKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
201 { "eglDestroyStreamKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
203 { "eglStreamAttribKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
205 { "eglQueryStreamKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
207 { "eglQueryStreamu64KHR",
208 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
209 { "eglQueryStreamTimeKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
211 { "eglCreateStreamProducerSurfaceKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
213 { "eglStreamConsumerGLTextureExternalKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
215 { "eglStreamConsumerAcquireKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
217 { "eglStreamConsumerReleaseKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
219 { "eglGetStreamFileDescriptorKHR",
220 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
221 { "eglCreateStreamFromFileDescriptorKHR",
222 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700223
224 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800225 { "eglGetNextFrameIdANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800227 { "eglGetCompositorTimingANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
229 { "eglGetCompositorTimingSupportedANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700231 { "eglGetFrameTimestampsANDROID",
232 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800233 { "eglGetFrameTimestampSupportedANDROID",
234 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700235};
236
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700237/*
238 * These extensions entry-points should not be exposed to applications.
239 * They're used internally by the Android EGL layer.
240 */
241#define FILTER_EXTENSIONS(procname) \
242 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
243 !strcmp((procname), "eglHibernateProcessIMG") || \
244 !strcmp((procname), "eglAwakenProcessIMG") || \
245 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
246
247
248
Mathias Agopian518ec112011-05-13 16:21:08 -0700249// accesses protected by sExtensionMapMutex
250static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
251static int sGLExtentionSlot = 0;
252static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
253
254static void(*findProcAddress(const char* name,
255 const extention_map_t* map, size_t n))() {
256 for (uint32_t i=0 ; i<n ; i++) {
257 if (!strcmp(name, map[i].name)) {
258 return map[i].address;
259 }
260 }
261 return NULL;
262}
263
264// ----------------------------------------------------------------------------
265
Mathias Agopian518ec112011-05-13 16:21:08 -0700266extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
267extern EGLBoolean egl_init_drivers();
268extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700269extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271} // namespace android;
272
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700273
Mathias Agopian518ec112011-05-13 16:21:08 -0700274// ----------------------------------------------------------------------------
275
276static inline void clearError() { egl_tls_t::clearError(); }
277static inline EGLContext getContext() { return egl_tls_t::getContext(); }
278
279// ----------------------------------------------------------------------------
280
281EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
282{
Jesse Hall1508ae62017-01-19 17:43:26 -0800283 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 clearError();
285
Dan Stozac3289c42014-01-17 11:38:34 -0800286 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700287 if (index >= NUM_DISPLAYS) {
288 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
289 }
290
291 if (egl_init_drivers() == EGL_FALSE) {
292 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
293 }
294
295 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
296 return dpy;
297}
298
299// ----------------------------------------------------------------------------
300// Initialization
301// ----------------------------------------------------------------------------
302
303EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
304{
305 clearError();
306
Jesse Hallb29e5e82012-04-04 16:53:42 -0700307 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800308 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700309
310 EGLBoolean res = dp->initialize(major, minor);
311
312 return res;
313}
314
315EGLBoolean eglTerminate(EGLDisplay dpy)
316{
317 // NOTE: don't unload the drivers b/c some APIs can be called
318 // after eglTerminate() has been called. eglTerminate() only
319 // terminates an EGLDisplay, not a EGL itself.
320
321 clearError();
322
Jesse Hallb29e5e82012-04-04 16:53:42 -0700323 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800324 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700325
326 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800327
Mathias Agopian518ec112011-05-13 16:21:08 -0700328 return res;
329}
330
331// ----------------------------------------------------------------------------
332// configuration
333// ----------------------------------------------------------------------------
334
335EGLBoolean eglGetConfigs( EGLDisplay dpy,
336 EGLConfig *configs,
337 EGLint config_size, EGLint *num_config)
338{
339 clearError();
340
Jesse Hallb29e5e82012-04-04 16:53:42 -0700341 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700342 if (!dp) return EGL_FALSE;
343
Mathias Agopian7773c432012-02-13 20:06:08 -0800344 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800345 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700346 }
347
Mathias Agopian7773c432012-02-13 20:06:08 -0800348 EGLBoolean res = EGL_FALSE;
349 *num_config = 0;
350
351 egl_connection_t* const cnx = &gEGLImpl;
352 if (cnx->dso) {
353 res = cnx->egl.eglGetConfigs(
354 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700355 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800356
357 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700358}
359
360EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
361 EGLConfig *configs, EGLint config_size,
362 EGLint *num_config)
363{
364 clearError();
365
Jesse Hallb29e5e82012-04-04 16:53:42 -0700366 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700367 if (!dp) return EGL_FALSE;
368
369 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800370 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700371 }
372
Mathias Agopian518ec112011-05-13 16:21:08 -0700373 EGLBoolean res = EGL_FALSE;
374 *num_config = 0;
375
Mathias Agopianada798b2012-02-13 17:09:30 -0800376 egl_connection_t* const cnx = &gEGLImpl;
377 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700378 if (attrib_list) {
379 char value[PROPERTY_VALUE_MAX];
380 property_get("debug.egl.force_msaa", value, "false");
381
382 if (!strcmp(value, "true")) {
383 size_t attribCount = 0;
384 EGLint attrib = attrib_list[0];
385
386 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700387 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700388 const EGLint *attribRendererable = NULL;
389 const EGLint *attribCaveat = NULL;
390
391 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700392 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700393 while (attrib != EGL_NONE) {
394 attrib = attrib_list[attribCount];
395 switch (attrib) {
396 case EGL_RENDERABLE_TYPE:
397 attribRendererable = &attrib_list[attribCount];
398 break;
399 case EGL_CONFIG_CAVEAT:
400 attribCaveat = &attrib_list[attribCount];
401 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800402 default:
403 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700404 }
405 attribCount++;
406 }
407
408 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
409 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800410
Romain Guy1cffc802012-10-15 18:13:05 -0700411 // Insert 2 extra attributes to force-enable MSAA 4x
412 EGLint aaAttribs[attribCount + 4];
413 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
414 aaAttribs[1] = 1;
415 aaAttribs[2] = EGL_SAMPLES;
416 aaAttribs[3] = 4;
417
418 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
419
420 EGLint numConfigAA;
421 EGLBoolean resAA = cnx->egl.eglChooseConfig(
422 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
423
424 if (resAA == EGL_TRUE && numConfigAA > 0) {
425 ALOGD("Enabling MSAA 4x");
426 *num_config = numConfigAA;
427 return resAA;
428 }
429 }
430 }
431 }
432
Mathias Agopian7773c432012-02-13 20:06:08 -0800433 res = cnx->egl.eglChooseConfig(
434 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700435 }
436 return res;
437}
438
439EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
440 EGLint attribute, EGLint *value)
441{
442 clearError();
443
Jesse Hallb29e5e82012-04-04 16:53:42 -0700444 egl_connection_t* cnx = NULL;
445 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
446 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800447
Mathias Agopian518ec112011-05-13 16:21:08 -0700448 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800449 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700450}
451
452// ----------------------------------------------------------------------------
453// surfaces
454// ----------------------------------------------------------------------------
455
Jesse Hallc2e41222013-08-08 13:40:22 -0700456// Turn linear formats into corresponding sRGB formats when colorspace is
457// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
458// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800459// the modification isn't possible, the original dataSpace is returned.
460static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
461 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700462 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800463 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700464 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800465 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700466 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800467 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700468}
469
Mathias Agopian518ec112011-05-13 16:21:08 -0700470EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
471 NativeWindowType window,
472 const EGLint *attrib_list)
473{
474 clearError();
475
Jesse Hallb29e5e82012-04-04 16:53:42 -0700476 egl_connection_t* cnx = NULL;
477 egl_display_ptr dp = validate_display_connection(dpy, cnx);
478 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800479 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700480
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800481 if (!window) {
482 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
483 }
484
485 int value = 0;
486 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
487 if (!value) {
488 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
489 }
490
Andy McFaddend566ce32014-01-07 15:54:17 -0800491 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
492 if (result != OK) {
493 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
494 "failed (%#x) (already connected to another API?)",
495 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700496 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700497 }
498
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700499 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700500 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
501 // of our native format. So if sRGB gamma is requested, we have to
502 // modify the EGLconfig's format before setting the native window's
503 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800504
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700505 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
506 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
507 &componentType);
508
Mathias Agopian95921562017-02-24 14:31:31 -0800509 EGLint format;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800510 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700511 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700512 EGLint r, g, b;
513 r = g = b = 0;
514 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
515 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
516 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700517 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700518 EGLint colorDepth = r + g + b;
519
520 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700521 if (colorDepth <= 16) {
522 format = HAL_PIXEL_FORMAT_RGB_565;
523 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700524 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_RGBX_8888;
529 }
530 } else {
531 format = HAL_PIXEL_FORMAT_RGBA_FP16;
532 }
533 }
534 } else {
535 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
536 if (colorDepth > 24) {
537 format = HAL_PIXEL_FORMAT_RGBA_1010102;
538 } else {
539 format = HAL_PIXEL_FORMAT_RGBA_8888;
540 }
541 } else {
542 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700543 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700544 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700545
546 // now select a corresponding sRGB format if needed
547 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
548 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
549 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530550 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700551 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700552 }
553 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800554
Jesse Hallc2e41222013-08-08 13:40:22 -0700555 if (format != 0) {
556 int err = native_window_set_buffers_format(window, format);
557 if (err != 0) {
558 ALOGE("error setting native window pixel format: %s (%d)",
559 strerror(-err), err);
560 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
561 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
562 }
563 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700564
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800565 if (dataSpace != 0) {
566 int err = native_window_set_buffers_data_space(window, dataSpace);
567 if (err != 0) {
568 ALOGE("error setting native window pixel dataSpace: %s (%d)",
569 strerror(-err), err);
570 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
571 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
572 }
573 }
574
Jamie Gennis59769462011-11-19 18:04:43 -0800575 // the EGL spec requires that a new EGLSurface default to swap interval
576 // 1, so explicitly set that on the window here.
577 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
578 anw->setSwapInterval(anw, 1);
579
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800581 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700582 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700583 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
584 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700585 return s;
586 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700587
588 // EGLSurface creation failed
589 native_window_set_buffers_format(window, 0);
590 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700591 }
592 return EGL_NO_SURFACE;
593}
594
595EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
596 NativePixmapType pixmap,
597 const EGLint *attrib_list)
598{
599 clearError();
600
Jesse Hallb29e5e82012-04-04 16:53:42 -0700601 egl_connection_t* cnx = NULL;
602 egl_display_ptr dp = validate_display_connection(dpy, cnx);
603 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700604 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800605 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700606 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700607 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
608 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700609 return s;
610 }
611 }
612 return EGL_NO_SURFACE;
613}
614
615EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
616 const EGLint *attrib_list)
617{
618 clearError();
619
Jesse Hallb29e5e82012-04-04 16:53:42 -0700620 egl_connection_t* cnx = NULL;
621 egl_display_ptr dp = validate_display_connection(dpy, cnx);
622 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700623 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800624 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700625 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700626 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
627 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700628 return s;
629 }
630 }
631 return EGL_NO_SURFACE;
632}
Jesse Hall47743382013-02-08 11:13:46 -0800633
Mathias Agopian518ec112011-05-13 16:21:08 -0700634EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
635{
636 clearError();
637
Jesse Hallb29e5e82012-04-04 16:53:42 -0700638 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700639 if (!dp) return EGL_FALSE;
640
Jesse Hallb29e5e82012-04-04 16:53:42 -0700641 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700642 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800643 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700644
645 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800646 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700647 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700648 _s.terminate();
649 }
650 return result;
651}
652
653EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
654 EGLint attribute, EGLint *value)
655{
656 clearError();
657
Jesse Hallb29e5e82012-04-04 16:53:42 -0700658 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700659 if (!dp) return EGL_FALSE;
660
Jesse Hallb29e5e82012-04-04 16:53:42 -0700661 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700662 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800663 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700664
Mathias Agopian518ec112011-05-13 16:21:08 -0700665 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800666 return s->cnx->egl.eglQuerySurface(
667 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700668}
669
Jamie Gennise8696a42012-01-15 18:54:57 -0800670void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800671 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800672 clearError();
673
Jesse Hallb29e5e82012-04-04 16:53:42 -0700674 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800675 if (!dp) {
676 return;
677 }
678
Jesse Hallb29e5e82012-04-04 16:53:42 -0700679 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800680 if (!_s.get()) {
681 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800682 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800683}
684
Mathias Agopian518ec112011-05-13 16:21:08 -0700685// ----------------------------------------------------------------------------
686// Contexts
687// ----------------------------------------------------------------------------
688
689EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
690 EGLContext share_list, const EGLint *attrib_list)
691{
692 clearError();
693
Jesse Hallb29e5e82012-04-04 16:53:42 -0700694 egl_connection_t* cnx = NULL;
695 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700696 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700697 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700698 if (!ContextRef(dp.get(), share_list).get()) {
699 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
700 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700701 egl_context_t* const c = get_context(share_list);
702 share_list = c->context;
703 }
704 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800705 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700706 if (context != EGL_NO_CONTEXT) {
707 // figure out if it's a GLESv1 or GLESv2
708 int version = 0;
709 if (attrib_list) {
710 while (*attrib_list != EGL_NONE) {
711 GLint attr = *attrib_list++;
712 GLint value = *attrib_list++;
713 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
714 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800715 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800716 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800717 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700718 }
719 }
720 };
721 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700722 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
723 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700724 return c;
725 }
726 }
727 return EGL_NO_CONTEXT;
728}
729
730EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
731{
732 clearError();
733
Jesse Hallb29e5e82012-04-04 16:53:42 -0700734 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700735 if (!dp)
736 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700737
Jesse Hallb29e5e82012-04-04 16:53:42 -0700738 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700739 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800740 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800741
Mathias Agopian518ec112011-05-13 16:21:08 -0700742 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800743 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700744 if (result == EGL_TRUE) {
745 _c.terminate();
746 }
747 return result;
748}
749
Mathias Agopian518ec112011-05-13 16:21:08 -0700750EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
751 EGLSurface read, EGLContext ctx)
752{
753 clearError();
754
Jesse Hallb29e5e82012-04-04 16:53:42 -0700755 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800756 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700757
Mathias Agopian5b287a62011-05-16 18:58:55 -0700758 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
759 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
760 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700761 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
762 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800763 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700764 }
765
766 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700767 ContextRef _c(dp.get(), ctx);
768 SurfaceRef _d(dp.get(), draw);
769 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700770
771 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700772 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700773 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800774 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700775 }
776
777 // these are the underlying implementation's object
778 EGLContext impl_ctx = EGL_NO_CONTEXT;
779 EGLSurface impl_draw = EGL_NO_SURFACE;
780 EGLSurface impl_read = EGL_NO_SURFACE;
781
782 // these are our objects structs passed in
783 egl_context_t * c = NULL;
784 egl_surface_t const * d = NULL;
785 egl_surface_t const * r = NULL;
786
787 // these are the current objects structs
788 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800789
Mathias Agopian518ec112011-05-13 16:21:08 -0700790 if (ctx != EGL_NO_CONTEXT) {
791 c = get_context(ctx);
792 impl_ctx = c->context;
793 } else {
794 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700795 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
796 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800797 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700798 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 if (cur_c == NULL) {
800 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700801 // not an error, there is just no current context.
802 return EGL_TRUE;
803 }
804 }
805
806 // retrieve the underlying implementation's draw EGLSurface
807 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800808 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700809 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 impl_draw = d->surface;
811 }
812
813 // retrieve the underlying implementation's read EGLSurface
814 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800815 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700816 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700817 impl_read = r->surface;
818 }
819
Mathias Agopian518ec112011-05-13 16:21:08 -0700820
Jesse Hallb29e5e82012-04-04 16:53:42 -0700821 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800822 draw, read, ctx,
823 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700824
825 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800826 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
828 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700829 _c.acquire();
830 _r.acquire();
831 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700832 } else {
833 setGLHooksThreadSpecific(&gHooksNoContext);
834 egl_tls_t::setContext(EGL_NO_CONTEXT);
835 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700836 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000837 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700838 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -0800839 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700840 }
841 return result;
842}
843
844
845EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
846 EGLint attribute, EGLint *value)
847{
848 clearError();
849
Jesse Hallb29e5e82012-04-04 16:53:42 -0700850 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700851 if (!dp) return EGL_FALSE;
852
Jesse Hallb29e5e82012-04-04 16:53:42 -0700853 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -0800854 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700855
Mathias Agopian518ec112011-05-13 16:21:08 -0700856 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800857 return c->cnx->egl.eglQueryContext(
858 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700859
Mathias Agopian518ec112011-05-13 16:21:08 -0700860}
861
862EGLContext eglGetCurrentContext(void)
863{
864 // could be called before eglInitialize(), but we wouldn't have a context
865 // then, and this function would correctly return EGL_NO_CONTEXT.
866
867 clearError();
868
869 EGLContext ctx = getContext();
870 return ctx;
871}
872
873EGLSurface eglGetCurrentSurface(EGLint readdraw)
874{
875 // could be called before eglInitialize(), but we wouldn't have a context
876 // then, and this function would correctly return EGL_NO_SURFACE.
877
878 clearError();
879
880 EGLContext ctx = getContext();
881 if (ctx) {
882 egl_context_t const * const c = get_context(ctx);
883 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
884 switch (readdraw) {
885 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800886 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700887 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
888 }
889 }
890 return EGL_NO_SURFACE;
891}
892
893EGLDisplay eglGetCurrentDisplay(void)
894{
895 // could be called before eglInitialize(), but we wouldn't have a context
896 // then, and this function would correctly return EGL_NO_DISPLAY.
897
898 clearError();
899
900 EGLContext ctx = getContext();
901 if (ctx) {
902 egl_context_t const * const c = get_context(ctx);
903 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
904 return c->dpy;
905 }
906 return EGL_NO_DISPLAY;
907}
908
909EGLBoolean eglWaitGL(void)
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.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700918}
919
920EGLBoolean eglWaitNative(EGLint engine)
921{
Mathias Agopian518ec112011-05-13 16:21:08 -0700922 clearError();
923
Mathias Agopianada798b2012-02-13 17:09:30 -0800924 egl_connection_t* const cnx = &gEGLImpl;
925 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800926 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800927
928 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700929}
930
931EGLint eglGetError(void)
932{
Mathias Agopianada798b2012-02-13 17:09:30 -0800933 EGLint err = EGL_SUCCESS;
934 egl_connection_t* const cnx = &gEGLImpl;
935 if (cnx->dso) {
936 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700937 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800938 if (err == EGL_SUCCESS) {
939 err = egl_tls_t::getError();
940 }
941 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700942}
943
Michael Chockc0ec5e22014-01-27 08:14:33 -0800944static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700945 const char* procname) {
946 const egl_connection_t* cnx = &gEGLImpl;
947 void* proc = NULL;
948
Michael Chockc0ec5e22014-01-27 08:14:33 -0800949 proc = dlsym(cnx->libEgl, procname);
950 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
951
Jesse Hallc07b5202013-07-04 12:08:16 -0700952 proc = dlsym(cnx->libGles2, procname);
953 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
954
955 proc = dlsym(cnx->libGles1, procname);
956 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
957
958 return NULL;
959}
960
Mathias Agopian518ec112011-05-13 16:21:08 -0700961__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
962{
963 // eglGetProcAddress() could be the very first function called
964 // in which case we must make sure we've initialized ourselves, this
965 // happens the first time egl_get_display() is called.
966
967 clearError();
968
969 if (egl_init_drivers() == EGL_FALSE) {
970 setError(EGL_BAD_PARAMETER, NULL);
971 return NULL;
972 }
973
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700974 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700975 return NULL;
976 }
977
Mathias Agopian518ec112011-05-13 16:21:08 -0700978 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700979 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700980 if (addr) return addr;
981
Michael Chockc0ec5e22014-01-27 08:14:33 -0800982 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700983 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700984
Mathias Agopian518ec112011-05-13 16:21:08 -0700985 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
986 pthread_mutex_lock(&sExtensionMapMutex);
987
988 /*
989 * Since eglGetProcAddress() is not associated to anything, it needs
990 * to return a function pointer that "works" regardless of what
991 * the current context is.
992 *
993 * For this reason, we return a "forwarder", a small stub that takes
994 * care of calling the function associated with the context
995 * currently bound.
996 *
997 * We first look for extensions we've already resolved, if we're seeing
998 * this extension for the first time, we go through all our
999 * implementations and call eglGetProcAddress() and record the
1000 * result in the appropriate implementation hooks and return the
1001 * address of the forwarder corresponding to that hook set.
1002 *
1003 */
1004
1005 const String8 name(procname);
1006 addr = sGLExtentionMap.valueFor(name);
1007 const int slot = sGLExtentionSlot;
1008
Steve Blocke6f43dd2012-01-06 19:20:56 +00001009 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001010 "no more slots for eglGetProcAddress(\"%s\")",
1011 procname);
1012
1013 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1014 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001015
1016 egl_connection_t* const cnx = &gEGLImpl;
1017 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001018 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001019 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001020 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1021 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001022 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001023 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001024 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001025
Mathias Agopian518ec112011-05-13 16:21:08 -07001026 if (found) {
1027 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001028 sGLExtentionMap.add(name, addr);
1029 sGLExtentionSlot++;
1030 }
1031 }
1032
1033 pthread_mutex_unlock(&sExtensionMapMutex);
1034 return addr;
1035}
1036
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001037class FrameCompletionThread : public Thread {
1038public:
1039
1040 static void queueSync(EGLSyncKHR sync) {
1041 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1042 static bool running = false;
1043 if (!running) {
1044 thread->run("GPUFrameCompletion");
1045 running = true;
1046 }
1047 {
1048 Mutex::Autolock lock(thread->mMutex);
1049 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1050 thread->mFramesQueued).string());
1051 thread->mQueue.push_back(sync);
1052 thread->mCondition.signal();
1053 thread->mFramesQueued++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001054 ATRACE_INT("GPU Frames Outstanding", int32_t(thread->mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001055 }
1056 }
1057
1058private:
1059 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1060
1061 virtual bool threadLoop() {
1062 EGLSyncKHR sync;
1063 uint32_t frameNum;
1064 {
1065 Mutex::Autolock lock(mMutex);
1066 while (mQueue.isEmpty()) {
1067 mCondition.wait(mMutex);
1068 }
1069 sync = mQueue[0];
1070 frameNum = mFramesCompleted;
1071 }
1072 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1073 {
1074 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1075 frameNum).string());
1076 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1077 if (result == EGL_FALSE) {
1078 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1079 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1080 ALOGE("FrameCompletion: timeout waiting for fence");
1081 }
1082 eglDestroySyncKHR(dpy, sync);
1083 }
1084 {
1085 Mutex::Autolock lock(mMutex);
1086 mQueue.removeAt(0);
1087 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001088 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001089 }
1090 return true;
1091 }
1092
1093 uint32_t mFramesQueued;
1094 uint32_t mFramesCompleted;
1095 Vector<EGLSyncKHR> mQueue;
1096 Condition mCondition;
1097 Mutex mMutex;
1098};
1099
Dan Stozaa894d082015-02-19 15:27:36 -08001100EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1101 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001102{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001103 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001104 clearError();
1105
Jesse Hallb29e5e82012-04-04 16:53:42 -07001106 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001107 if (!dp) return EGL_FALSE;
1108
Jesse Hallb29e5e82012-04-04 16:53:42 -07001109 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001110 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001111 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001112
Mathias Agopian518ec112011-05-13 16:21:08 -07001113 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001114
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001115 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1116 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1117 if (sync != EGL_NO_SYNC_KHR) {
1118 FrameCompletionThread::queueSync(sync);
1119 }
1120 }
1121
Mathias Agopian7db993a2012-03-25 00:49:46 -07001122 if (CC_UNLIKELY(dp->finishOnSwap)) {
1123 uint32_t pixel;
1124 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1125 if (c) {
1126 // glReadPixels() ensures that the frame is complete
1127 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1128 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1129 }
1130 }
1131
Dan Stozaa894d082015-02-19 15:27:36 -08001132 if (n_rects == 0) {
1133 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1134 }
1135
1136 Vector<android_native_rect_t> androidRects;
1137 for (int r = 0; r < n_rects; ++r) {
1138 int offset = r * 4;
1139 int x = rects[offset];
1140 int y = rects[offset + 1];
1141 int width = rects[offset + 2];
1142 int height = rects[offset + 3];
1143 android_native_rect_t androidRect;
1144 androidRect.left = x;
1145 androidRect.top = y + height;
1146 androidRect.right = x + width;
1147 androidRect.bottom = y;
1148 androidRects.push_back(androidRect);
1149 }
1150 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1151 androidRects.size());
1152
1153 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1154 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1155 rects, n_rects);
1156 } else {
1157 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1158 }
1159}
1160
1161EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1162{
1163 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001164}
1165
1166EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1167 NativePixmapType target)
1168{
1169 clearError();
1170
Jesse Hallb29e5e82012-04-04 16:53:42 -07001171 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001172 if (!dp) return EGL_FALSE;
1173
Jesse Hallb29e5e82012-04-04 16:53:42 -07001174 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001175 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001176 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001177
Mathias Agopian518ec112011-05-13 16:21:08 -07001178 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001179 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001180}
1181
1182const char* eglQueryString(EGLDisplay dpy, EGLint name)
1183{
1184 clearError();
1185
Chia-I Wue57d1352016-08-15 16:10:02 +08001186 // Generate an error quietly when client extensions (as defined by
1187 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1188 // validate_display to generate the error as validate_display would log
1189 // the error, which can be misleading.
1190 //
1191 // If we want to support EGL_EXT_client_extensions later, we can return
1192 // the client extension string here instead.
1193 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001194 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001195
Jesse Hallb29e5e82012-04-04 16:53:42 -07001196 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001197 if (!dp) return (const char *) NULL;
1198
1199 switch (name) {
1200 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001201 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001202 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001203 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001204 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001205 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001206 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001207 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001208 default:
1209 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001210 }
1211 return setError(EGL_BAD_PARAMETER, (const char *)0);
1212}
1213
Mathias Agopianca088332013-03-28 17:44:13 -07001214EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1215{
1216 clearError();
1217
1218 const egl_display_ptr dp = validate_display(dpy);
1219 if (!dp) return (const char *) NULL;
1220
1221 switch (name) {
1222 case EGL_VENDOR:
1223 return dp->disp.queryString.vendor;
1224 case EGL_VERSION:
1225 return dp->disp.queryString.version;
1226 case EGL_EXTENSIONS:
1227 return dp->disp.queryString.extensions;
1228 case EGL_CLIENT_APIS:
1229 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001230 default:
1231 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001232 }
1233 return setError(EGL_BAD_PARAMETER, (const char *)0);
1234}
Mathias Agopian518ec112011-05-13 16:21:08 -07001235
1236// ----------------------------------------------------------------------------
1237// EGL 1.1
1238// ----------------------------------------------------------------------------
1239
1240EGLBoolean eglSurfaceAttrib(
1241 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1242{
1243 clearError();
1244
Jesse Hallb29e5e82012-04-04 16:53:42 -07001245 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001246 if (!dp) return EGL_FALSE;
1247
Jesse Hallb29e5e82012-04-04 16:53:42 -07001248 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001249 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001250 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001251
Pablo Ceballosc18be292016-05-31 14:55:42 -07001252 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001253
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001254 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001255 if (!s->win.get()) {
1256 setError(EGL_BAD_SURFACE, EGL_FALSE);
1257 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001258 int err = native_window_set_auto_refresh(s->win.get(), value ? true : false);
1259 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001260 }
1261
Pablo Ceballosc18be292016-05-31 14:55:42 -07001262 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001263 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001264 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001265 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001266 int err = native_window_enable_frame_timestamps(s->win.get(), value ? true : false);
1267 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001268 }
1269
Mathias Agopian518ec112011-05-13 16:21:08 -07001270 if (s->cnx->egl.eglSurfaceAttrib) {
1271 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001272 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001273 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001274 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001275}
1276
1277EGLBoolean eglBindTexImage(
1278 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1279{
1280 clearError();
1281
Jesse Hallb29e5e82012-04-04 16:53:42 -07001282 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001283 if (!dp) return EGL_FALSE;
1284
Jesse Hallb29e5e82012-04-04 16:53:42 -07001285 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001286 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001287 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001288
Mathias Agopian518ec112011-05-13 16:21:08 -07001289 egl_surface_t const * const s = get_surface(surface);
1290 if (s->cnx->egl.eglBindTexImage) {
1291 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001292 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001293 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001294 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001295}
1296
1297EGLBoolean eglReleaseTexImage(
1298 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1299{
1300 clearError();
1301
Jesse Hallb29e5e82012-04-04 16:53:42 -07001302 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001303 if (!dp) return EGL_FALSE;
1304
Jesse Hallb29e5e82012-04-04 16:53:42 -07001305 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001306 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001307 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001308
Mathias Agopian518ec112011-05-13 16:21:08 -07001309 egl_surface_t const * const s = get_surface(surface);
1310 if (s->cnx->egl.eglReleaseTexImage) {
1311 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001312 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001313 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001314 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001315}
1316
1317EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1318{
1319 clearError();
1320
Jesse Hallb29e5e82012-04-04 16:53:42 -07001321 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001322 if (!dp) return EGL_FALSE;
1323
1324 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001325 egl_connection_t* const cnx = &gEGLImpl;
1326 if (cnx->dso && cnx->egl.eglSwapInterval) {
1327 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001328 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001329
Mathias Agopian518ec112011-05-13 16:21:08 -07001330 return res;
1331}
1332
1333
1334// ----------------------------------------------------------------------------
1335// EGL 1.2
1336// ----------------------------------------------------------------------------
1337
1338EGLBoolean eglWaitClient(void)
1339{
1340 clearError();
1341
Mathias Agopianada798b2012-02-13 17:09:30 -08001342 egl_connection_t* const cnx = &gEGLImpl;
1343 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001344 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001345
1346 EGLBoolean res;
1347 if (cnx->egl.eglWaitClient) {
1348 res = cnx->egl.eglWaitClient();
1349 } else {
1350 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001351 }
1352 return res;
1353}
1354
1355EGLBoolean eglBindAPI(EGLenum api)
1356{
1357 clearError();
1358
1359 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001360 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001361 }
1362
1363 // bind this API on all EGLs
1364 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001365 egl_connection_t* const cnx = &gEGLImpl;
1366 if (cnx->dso && cnx->egl.eglBindAPI) {
1367 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001368 }
1369 return res;
1370}
1371
1372EGLenum eglQueryAPI(void)
1373{
1374 clearError();
1375
1376 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001377 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001378 }
1379
Mathias Agopianada798b2012-02-13 17:09:30 -08001380 egl_connection_t* const cnx = &gEGLImpl;
1381 if (cnx->dso && cnx->egl.eglQueryAPI) {
1382 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001383 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001384
Mathias Agopian518ec112011-05-13 16:21:08 -07001385 // or, it can only be OpenGL ES
1386 return EGL_OPENGL_ES_API;
1387}
1388
1389EGLBoolean eglReleaseThread(void)
1390{
1391 clearError();
1392
Mathias Agopianada798b2012-02-13 17:09:30 -08001393 egl_connection_t* const cnx = &gEGLImpl;
1394 if (cnx->dso && cnx->egl.eglReleaseThread) {
1395 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001396 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301397
1398 // If there is context bound to the thread, release it
1399 egl_display_t::loseCurrent(get_context(getContext()));
1400
Mathias Agopian518ec112011-05-13 16:21:08 -07001401 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001402 return EGL_TRUE;
1403}
1404
1405EGLSurface eglCreatePbufferFromClientBuffer(
1406 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1407 EGLConfig config, const EGLint *attrib_list)
1408{
1409 clearError();
1410
Jesse Hallb29e5e82012-04-04 16:53:42 -07001411 egl_connection_t* cnx = NULL;
1412 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1413 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001414 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1415 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001416 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001417 }
1418 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1419}
1420
1421// ----------------------------------------------------------------------------
1422// EGL_EGLEXT_VERSION 3
1423// ----------------------------------------------------------------------------
1424
1425EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1426 const EGLint *attrib_list)
1427{
1428 clearError();
1429
Jesse Hallb29e5e82012-04-04 16:53:42 -07001430 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001431 if (!dp) return EGL_FALSE;
1432
Jesse Hallb29e5e82012-04-04 16:53:42 -07001433 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001434 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001435 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001436
1437 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001438 if (s->cnx->egl.eglLockSurfaceKHR) {
1439 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001440 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001441 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001442 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001443}
1444
1445EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1446{
1447 clearError();
1448
Jesse Hallb29e5e82012-04-04 16:53:42 -07001449 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450 if (!dp) return EGL_FALSE;
1451
Jesse Hallb29e5e82012-04-04 16:53:42 -07001452 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001453 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001454 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001455
1456 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001457 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001458 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001459 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001460 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001461}
1462
1463EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1464 EGLClientBuffer buffer, const EGLint *attrib_list)
1465{
1466 clearError();
1467
Jesse Hallb29e5e82012-04-04 16:53:42 -07001468 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001469 if (!dp) return EGL_NO_IMAGE_KHR;
1470
Jesse Hallb29e5e82012-04-04 16:53:42 -07001471 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001472 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001473
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001474 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1475 egl_connection_t* const cnx = &gEGLImpl;
1476 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1477 result = cnx->egl.eglCreateImageKHR(
1478 dp->disp.dpy,
1479 c ? c->context : EGL_NO_CONTEXT,
1480 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001481 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001482 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001483}
1484
1485EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1486{
1487 clearError();
1488
Jesse Hallb29e5e82012-04-04 16:53:42 -07001489 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001490 if (!dp) return EGL_FALSE;
1491
Steven Holte646a5c52012-06-04 20:02:11 -07001492 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001493 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001494 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001495 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001496 }
Steven Holte646a5c52012-06-04 20:02:11 -07001497 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001498}
1499
1500// ----------------------------------------------------------------------------
1501// EGL_EGLEXT_VERSION 5
1502// ----------------------------------------------------------------------------
1503
1504
1505EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1506{
1507 clearError();
1508
Jesse Hallb29e5e82012-04-04 16:53:42 -07001509 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001510 if (!dp) return EGL_NO_SYNC_KHR;
1511
Mathias Agopian518ec112011-05-13 16:21:08 -07001512 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001513 egl_connection_t* const cnx = &gEGLImpl;
1514 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1515 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001517 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001518}
1519
1520EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1521{
1522 clearError();
1523
Jesse Hallb29e5e82012-04-04 16:53:42 -07001524 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001525 if (!dp) return EGL_FALSE;
1526
Mathias Agopian518ec112011-05-13 16:21:08 -07001527 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001528 egl_connection_t* const cnx = &gEGLImpl;
1529 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1530 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001531 }
1532 return result;
1533}
1534
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001535EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1536 clearError();
1537
1538 const egl_display_ptr dp = validate_display(dpy);
1539 if (!dp) return EGL_FALSE;
1540
1541 EGLBoolean result = EGL_FALSE;
1542 egl_connection_t* const cnx = &gEGLImpl;
1543 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1544 result = cnx->egl.eglSignalSyncKHR(
1545 dp->disp.dpy, sync, mode);
1546 }
1547 return result;
1548}
1549
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001550EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1551 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001552{
1553 clearError();
1554
Jesse Hallb29e5e82012-04-04 16:53:42 -07001555 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001556 if (!dp) return EGL_FALSE;
1557
Mathias Agopian737b8962017-02-24 14:32:05 -08001558 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001559 egl_connection_t* const cnx = &gEGLImpl;
1560 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1561 result = cnx->egl.eglClientWaitSyncKHR(
1562 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001563 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001564 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001565}
1566
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001567EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1568 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001569{
1570 clearError();
1571
Jesse Hallb29e5e82012-04-04 16:53:42 -07001572 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001573 if (!dp) return EGL_FALSE;
1574
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001575 EGLBoolean result = EGL_FALSE;
1576 egl_connection_t* const cnx = &gEGLImpl;
1577 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1578 result = cnx->egl.eglGetSyncAttribKHR(
1579 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001580 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001581 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001582}
1583
Season Li000d88f2015-07-01 11:39:40 -07001584EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1585{
1586 clearError();
1587
1588 const egl_display_ptr dp = validate_display(dpy);
1589 if (!dp) return EGL_NO_STREAM_KHR;
1590
1591 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1592 egl_connection_t* const cnx = &gEGLImpl;
1593 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1594 result = cnx->egl.eglCreateStreamKHR(
1595 dp->disp.dpy, attrib_list);
1596 }
1597 return result;
1598}
1599
1600EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1601{
1602 clearError();
1603
1604 const egl_display_ptr dp = validate_display(dpy);
1605 if (!dp) return EGL_FALSE;
1606
1607 EGLBoolean result = EGL_FALSE;
1608 egl_connection_t* const cnx = &gEGLImpl;
1609 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1610 result = cnx->egl.eglDestroyStreamKHR(
1611 dp->disp.dpy, stream);
1612 }
1613 return result;
1614}
1615
1616EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1617 EGLenum attribute, EGLint value)
1618{
1619 clearError();
1620
1621 const egl_display_ptr dp = validate_display(dpy);
1622 if (!dp) return EGL_FALSE;
1623
1624 EGLBoolean result = EGL_FALSE;
1625 egl_connection_t* const cnx = &gEGLImpl;
1626 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1627 result = cnx->egl.eglStreamAttribKHR(
1628 dp->disp.dpy, stream, attribute, value);
1629 }
1630 return result;
1631}
1632
1633EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1634 EGLenum attribute, EGLint *value)
1635{
1636 clearError();
1637
1638 const egl_display_ptr dp = validate_display(dpy);
1639 if (!dp) return EGL_FALSE;
1640
1641 EGLBoolean result = EGL_FALSE;
1642 egl_connection_t* const cnx = &gEGLImpl;
1643 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1644 result = cnx->egl.eglQueryStreamKHR(
1645 dp->disp.dpy, stream, attribute, value);
1646 }
1647 return result;
1648}
1649
1650EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1651 EGLenum attribute, EGLuint64KHR *value)
1652{
1653 clearError();
1654
1655 const egl_display_ptr dp = validate_display(dpy);
1656 if (!dp) return EGL_FALSE;
1657
1658 EGLBoolean result = EGL_FALSE;
1659 egl_connection_t* const cnx = &gEGLImpl;
1660 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1661 result = cnx->egl.eglQueryStreamu64KHR(
1662 dp->disp.dpy, stream, attribute, value);
1663 }
1664 return result;
1665}
1666
1667EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1668 EGLenum attribute, EGLTimeKHR *value)
1669{
1670 clearError();
1671
1672 const egl_display_ptr dp = validate_display(dpy);
1673 if (!dp) return EGL_FALSE;
1674
1675 EGLBoolean result = EGL_FALSE;
1676 egl_connection_t* const cnx = &gEGLImpl;
1677 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1678 result = cnx->egl.eglQueryStreamTimeKHR(
1679 dp->disp.dpy, stream, attribute, value);
1680 }
1681 return result;
1682}
1683
1684EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1685 EGLStreamKHR stream, const EGLint *attrib_list)
1686{
1687 clearError();
1688
1689 egl_display_ptr dp = validate_display(dpy);
1690 if (!dp) return EGL_NO_SURFACE;
1691
1692 egl_connection_t* const cnx = &gEGLImpl;
1693 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1694 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1695 dp->disp.dpy, config, stream, attrib_list);
1696 if (surface != EGL_NO_SURFACE) {
1697 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1698 surface, cnx);
1699 return s;
1700 }
1701 }
1702 return EGL_NO_SURFACE;
1703}
1704
1705EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1706 EGLStreamKHR stream)
1707{
1708 clearError();
1709
1710 const egl_display_ptr dp = validate_display(dpy);
1711 if (!dp) return EGL_FALSE;
1712
1713 EGLBoolean result = EGL_FALSE;
1714 egl_connection_t* const cnx = &gEGLImpl;
1715 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1716 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1717 dp->disp.dpy, stream);
1718 }
1719 return result;
1720}
1721
1722EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1723 EGLStreamKHR stream)
1724{
1725 clearError();
1726
1727 const egl_display_ptr dp = validate_display(dpy);
1728 if (!dp) return EGL_FALSE;
1729
1730 EGLBoolean result = EGL_FALSE;
1731 egl_connection_t* const cnx = &gEGLImpl;
1732 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1733 result = cnx->egl.eglStreamConsumerAcquireKHR(
1734 dp->disp.dpy, stream);
1735 }
1736 return result;
1737}
1738
1739EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1740 EGLStreamKHR stream)
1741{
1742 clearError();
1743
1744 const egl_display_ptr dp = validate_display(dpy);
1745 if (!dp) return EGL_FALSE;
1746
1747 EGLBoolean result = EGL_FALSE;
1748 egl_connection_t* const cnx = &gEGLImpl;
1749 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1750 result = cnx->egl.eglStreamConsumerReleaseKHR(
1751 dp->disp.dpy, stream);
1752 }
1753 return result;
1754}
1755
1756EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1757 EGLDisplay dpy, EGLStreamKHR stream)
1758{
1759 clearError();
1760
1761 const egl_display_ptr dp = validate_display(dpy);
1762 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1763
1764 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1765 egl_connection_t* const cnx = &gEGLImpl;
1766 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1767 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1768 dp->disp.dpy, stream);
1769 }
1770 return result;
1771}
1772
1773EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1774 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1775{
1776 clearError();
1777
1778 const egl_display_ptr dp = validate_display(dpy);
1779 if (!dp) return EGL_NO_STREAM_KHR;
1780
1781 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1782 egl_connection_t* const cnx = &gEGLImpl;
1783 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1784 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1785 dp->disp.dpy, file_descriptor);
1786 }
1787 return result;
1788}
1789
Mathias Agopian518ec112011-05-13 16:21:08 -07001790// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001791// EGL_EGLEXT_VERSION 15
1792// ----------------------------------------------------------------------------
1793
1794EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1795 clearError();
1796 const egl_display_ptr dp = validate_display(dpy);
1797 if (!dp) return EGL_FALSE;
1798 EGLint result = EGL_FALSE;
1799 egl_connection_t* const cnx = &gEGLImpl;
1800 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1801 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1802 }
1803 return result;
1804}
1805
1806// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001807// ANDROID extensions
1808// ----------------------------------------------------------------------------
1809
Jamie Gennis331841b2012-09-06 14:52:00 -07001810EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1811{
1812 clearError();
1813
1814 const egl_display_ptr dp = validate_display(dpy);
1815 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1816
1817 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1818 egl_connection_t* const cnx = &gEGLImpl;
1819 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1820 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1821 }
1822 return result;
1823}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001824
Andy McFadden72841452013-03-01 16:25:32 -08001825EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1826 EGLnsecsANDROID time)
1827{
1828 clearError();
1829
1830 const egl_display_ptr dp = validate_display(dpy);
1831 if (!dp) {
1832 return EGL_FALSE;
1833 }
1834
1835 SurfaceRef _s(dp.get(), surface);
1836 if (!_s.get()) {
1837 setError(EGL_BAD_SURFACE, EGL_FALSE);
1838 return EGL_FALSE;
1839 }
1840
1841 egl_surface_t const * const s = get_surface(surface);
1842 native_window_set_buffers_timestamp(s->win.get(), time);
1843
1844 return EGL_TRUE;
1845}
1846
Craig Donner05249fc2016-01-15 19:33:55 -08001847EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1848{
1849 clearError();
1850
Craig Donnere96a3252017-02-02 12:13:34 -08001851 uint64_t producerUsage = 0;
1852 uint64_t consumerUsage = 0;
Craig Donner05249fc2016-01-15 19:33:55 -08001853 uint32_t width = 0;
1854 uint32_t height = 0;
1855 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001856 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001857 uint32_t red_size = 0;
1858 uint32_t green_size = 0;
1859 uint32_t blue_size = 0;
1860 uint32_t alpha_size = 0;
1861
Craig Donnerb40504a2016-06-03 17:54:25 -07001862#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001863 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001864 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001865 target = value; \
1866 } else { \
1867 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1868 } \
1869 break
1870
1871 if (attrib_list) {
1872 while (*attrib_list != EGL_NONE) {
1873 GLint attr = *attrib_list++;
1874 GLint value = *attrib_list++;
1875 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001876 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1877 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1878 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1879 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1880 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1881 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001882 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001883 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1884 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001885 producerUsage |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001886 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001887 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001888 producerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
Craig Donner05249fc2016-01-15 19:33:55 -08001889 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001890 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001891 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
Craig Donner05249fc2016-01-15 19:33:55 -08001892 }
Craig Donner05249fc2016-01-15 19:33:55 -08001893 break;
1894 default:
1895 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1896 }
1897 }
1898 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001899#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001900
1901 // Validate format.
1902 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1903 if (alpha_size == 8) {
1904 format = HAL_PIXEL_FORMAT_RGBA_8888;
1905 } else {
1906 format = HAL_PIXEL_FORMAT_RGB_888;
1907 }
1908 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1909 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001910 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001911 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001912 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001913 red_size, green_size, blue_size, alpha_size);
1914 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1915 }
1916
Craig Donner68671532016-07-18 10:19:54 -07001917#define CHECK_ERROR_CONDITION(message) \
1918 if (err != NO_ERROR) { \
1919 ALOGE(message); \
1920 goto error_condition; \
1921 }
1922
1923 // The holder is used to destroy the buffer if an error occurs.
1924 GraphicBuffer* gBuffer = new GraphicBuffer();
1925 sp<IServiceManager> sm = defaultServiceManager();
1926 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1927 sp<IBinder> allocator;
1928 Parcel sc_data, sc_reply, data, reply;
1929 status_t err = NO_ERROR;
1930 if (sm == NULL) {
1931 ALOGE("Unable to connect to ServiceManager");
1932 goto error_condition;
1933 }
1934
1935 // Obtain an allocator.
1936 if (surfaceFlinger == NULL) {
1937 ALOGE("Unable to connect to SurfaceFlinger");
1938 goto error_condition;
1939 }
1940 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1941 err = surfaceFlinger->transact(
1942 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1943 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1944 allocator = sc_reply.readStrongBinder();
1945
1946 if (allocator == NULL) {
1947 ALOGE("Unable to obtain an ISurfaceComposer");
1948 goto error_condition;
1949 }
1950 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1951 err = data.writeUint32(width);
1952 CHECK_ERROR_CONDITION("Unable to write width");
1953 err = data.writeUint32(height);
1954 CHECK_ERROR_CONDITION("Unable to write height");
1955 err = data.writeInt32(static_cast<int32_t>(format));
1956 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001957 err = data.writeUint32(layer_count);
1958 CHECK_ERROR_CONDITION("Unable to write layer count");
Craig Donnere96a3252017-02-02 12:13:34 -08001959 err = data.writeUint64(producerUsage);
1960 CHECK_ERROR_CONDITION("Unable to write producer usage");
1961 err = data.writeUint64(consumerUsage);
1962 CHECK_ERROR_CONDITION("Unable to write consumer usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001963 err = data.writeUtf8AsUtf16(
1964 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1965 std::to_string(getpid()) + ']');
1966 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001967 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1968 &reply);
1969 CHECK_ERROR_CONDITION(
1970 "Unable to request buffer allocation from surface composer");
1971 err = reply.readInt32();
1972 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1973 err = reply.read(*gBuffer);
1974 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1975
1976 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001977 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001978 ALOGE("Unable to create native buffer "
Craig Donnere96a3252017-02-02 12:13:34 -08001979 "{ w=%u, h=%u, f=%u, pu=%" PRIx64 " cu=%" PRIx64 ", lc=%u} %#x",
1980 width, height, format, producerUsage, consumerUsage,
1981 layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001982 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001983 }
Craig Donnere96a3252017-02-02 12:13:34 -08001984 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, pu=%" PRIx64
1985 " cu=%" PRIx64 ", lc=%u}",
1986 gBuffer, width, height, format, producerUsage, consumerUsage,
1987 layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001988 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001989
1990#undef CHECK_ERROR_CONDITION
1991
1992error_condition:
1993 // Delete the buffer.
1994 sp<GraphicBuffer> holder(gBuffer);
1995 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001996}
1997
Craig Donner60761072017-01-27 12:30:44 -08001998EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1999 clearError();
2000
2001 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2002
Mathias Agopian89ed4c82017-02-09 18:48:34 -08002003 const GraphicBuffer* graphicBuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
Craig Donner60761072017-01-27 12:30:44 -08002004 return static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
2005}
2006
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002007// ----------------------------------------------------------------------------
2008// NVIDIA extensions
2009// ----------------------------------------------------------------------------
2010EGLuint64NV eglGetSystemTimeFrequencyNV()
2011{
2012 clearError();
2013
2014 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002015 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002016 }
2017
2018 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002019 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002020
Mathias Agopianada798b2012-02-13 17:09:30 -08002021 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2022 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002023 }
2024
Mathias Agopian737b8962017-02-24 14:32:05 -08002025 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002026}
2027
2028EGLuint64NV eglGetSystemTimeNV()
2029{
2030 clearError();
2031
2032 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002033 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002034 }
2035
2036 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002037 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002038
Mathias Agopianada798b2012-02-13 17:09:30 -08002039 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2040 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002041 }
2042
Mathias Agopian737b8962017-02-24 14:32:05 -08002043 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002044}
Dan Stozaa894d082015-02-19 15:27:36 -08002045
2046// ----------------------------------------------------------------------------
2047// Partial update extension
2048// ----------------------------------------------------------------------------
2049EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2050 EGLint *rects, EGLint n_rects)
2051{
2052 clearError();
2053
2054 const egl_display_ptr dp = validate_display(dpy);
2055 if (!dp) {
2056 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2057 return EGL_FALSE;
2058 }
2059
2060 SurfaceRef _s(dp.get(), surface);
2061 if (!_s.get()) {
2062 setError(EGL_BAD_SURFACE, EGL_FALSE);
2063 return EGL_FALSE;
2064 }
2065
2066 egl_surface_t const * const s = get_surface(surface);
2067 if (s->cnx->egl.eglSetDamageRegionKHR) {
2068 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2069 rects, n_rects);
2070 }
2071
2072 return EGL_FALSE;
2073}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002074
Brian Anderson1049d1d2016-12-16 17:25:57 -08002075EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2076 EGLuint64KHR *frameId) {
2077 clearError();
2078
2079 const egl_display_ptr dp = validate_display(dpy);
2080 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002081 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002082 }
2083
2084 SurfaceRef _s(dp.get(), surface);
2085 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002086 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002087 }
2088
2089 egl_surface_t const * const s = get_surface(surface);
2090
2091 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002092 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002093 }
2094
2095 uint64_t nextFrameId = 0;
2096 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2097
2098 if (ret != NO_ERROR) {
2099 // This should not happen. Return an error that is not in the spec
2100 // so it's obvious something is very wrong.
2101 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002102 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002103 }
2104
2105 *frameId = nextFrameId;
2106 return EGL_TRUE;
2107}
2108
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002109EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2110 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2111{
2112 clearError();
2113
2114 const egl_display_ptr dp = validate_display(dpy);
2115 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002116 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002117 }
2118
2119 SurfaceRef _s(dp.get(), surface);
2120 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002121 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002122 }
2123
2124 egl_surface_t const * const s = get_surface(surface);
2125
2126 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002127 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002128 }
2129
2130 nsecs_t* compositeDeadline = nullptr;
2131 nsecs_t* compositeInterval = nullptr;
2132 nsecs_t* compositeToPresentLatency = nullptr;
2133
2134 for (int i = 0; i < numTimestamps; i++) {
2135 switch (names[i]) {
2136 case EGL_COMPOSITE_DEADLINE_ANDROID:
2137 compositeDeadline = &values[i];
2138 break;
2139 case EGL_COMPOSITE_INTERVAL_ANDROID:
2140 compositeInterval = &values[i];
2141 break;
2142 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2143 compositeToPresentLatency = &values[i];
2144 break;
2145 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002146 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002147 }
2148 }
2149
2150 status_t ret = native_window_get_compositor_timing(s->win.get(),
2151 compositeDeadline, compositeInterval, compositeToPresentLatency);
2152
2153 switch (ret) {
2154 case NO_ERROR:
2155 return EGL_TRUE;
2156 case INVALID_OPERATION:
Mathias Agopian737b8962017-02-24 14:32:05 -08002157 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002158 default:
2159 // This should not happen. Return an error that is not in the spec
2160 // so it's obvious something is very wrong.
2161 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002162 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002163 }
2164}
2165
2166EGLBoolean eglGetCompositorTimingSupportedANDROID(
2167 EGLDisplay dpy, EGLSurface surface, EGLint name)
2168{
2169 clearError();
2170
2171 const egl_display_ptr dp = validate_display(dpy);
2172 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002173 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002174 }
2175
2176 SurfaceRef _s(dp.get(), surface);
2177 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002178 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002179 }
2180
2181 egl_surface_t const * const s = get_surface(surface);
2182
2183 ANativeWindow* window = s->win.get();
2184 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002185 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002186 }
2187
2188 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002189 case EGL_COMPOSITE_DEADLINE_ANDROID:
2190 case EGL_COMPOSITE_INTERVAL_ANDROID:
2191 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2192 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002193 default:
2194 return EGL_FALSE;
2195 }
2196}
2197
Pablo Ceballosc18be292016-05-31 14:55:42 -07002198EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002199 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002200 EGLnsecsANDROID *values)
2201{
2202 clearError();
2203
2204 const egl_display_ptr dp = validate_display(dpy);
2205 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002206 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002207 }
2208
2209 SurfaceRef _s(dp.get(), surface);
2210 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002211 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002212 }
2213
2214 egl_surface_t const * const s = get_surface(surface);
2215
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002216 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002217 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002218 }
2219
Brian Andersondbd0ea82016-07-22 09:38:59 -07002220 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002221 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002222 nsecs_t* latchTime = nullptr;
2223 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002224 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002225 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002226 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002227 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002228 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002229 nsecs_t* releaseTime = nullptr;
2230
2231 for (int i = 0; i < numTimestamps; i++) {
2232 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002233 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2234 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002235 break;
2236 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2237 acquireTime = &values[i];
2238 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002239 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2240 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002241 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002242 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2243 firstRefreshStartTime = &values[i];
2244 break;
2245 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2246 lastRefreshStartTime = &values[i];
2247 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002248 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2249 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002250 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002251 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2252 displayPresentTime = &values[i];
2253 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002254 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2255 displayRetireTime = &values[i];
2256 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002257 case EGL_DEQUEUE_READY_TIME_ANDROID:
2258 dequeueReadyTime = &values[i];
2259 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002260 case EGL_READS_DONE_TIME_ANDROID:
2261 releaseTime = &values[i];
2262 break;
2263 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002264 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002265 }
2266 }
2267
Brian Anderson1049d1d2016-12-16 17:25:57 -08002268 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002269 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002270 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002271 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002272
Brian Anderson069b3652016-07-22 10:32:47 -07002273 switch (ret) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002274 case NO_ERROR:
2275 return EGL_TRUE;
2276 case NAME_NOT_FOUND:
2277 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
2278 case INVALID_OPERATION:
2279 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2280 case BAD_VALUE:
2281 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2282 default:
2283 // This should not happen. Return an error that is not in the spec
2284 // so it's obvious something is very wrong.
2285 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2286 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002287 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002288}
2289
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002290EGLBoolean eglGetFrameTimestampSupportedANDROID(
2291 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002292{
2293 clearError();
2294
2295 const egl_display_ptr dp = validate_display(dpy);
2296 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002297 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002298 }
2299
2300 SurfaceRef _s(dp.get(), surface);
2301 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002302 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002303 }
2304
2305 egl_surface_t const * const s = get_surface(surface);
2306
2307 ANativeWindow* window = s->win.get();
2308 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002309 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002310 }
2311
2312 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002313 case EGL_COMPOSITE_DEADLINE_ANDROID:
2314 case EGL_COMPOSITE_INTERVAL_ANDROID:
2315 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002316 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002317 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002318 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2319 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2320 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002321 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002322 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002323 case EGL_READS_DONE_TIME_ANDROID:
2324 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002325 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2326 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002327 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002328 return value == 0 ? EGL_FALSE : EGL_TRUE;
2329 }
2330 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2331 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002332 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002333 return value == 0 ? EGL_FALSE : EGL_TRUE;
2334 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002335 default:
2336 return EGL_FALSE;
2337 }
2338}