blob: e52713f8f695dbd837591cc52b6d33f2d9a53955 [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
Andy McFaddend566ce32014-01-07 15:54:17 -0800481 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
482 if (result != OK) {
483 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
484 "failed (%#x) (already connected to another API?)",
485 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700486 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700487 }
488
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700489 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700490 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
491 // of our native format. So if sRGB gamma is requested, we have to
492 // modify the EGLconfig's format before setting the native window's
493 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800494
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700495 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
496 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
497 &componentType);
498
Mathias Agopian95921562017-02-24 14:31:31 -0800499 EGLint format;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800500 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700501 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700502 EGLint r, g, b;
503 r = g = b = 0;
504 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
505 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
506 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700507 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700508 EGLint colorDepth = r + g + b;
509
510 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700511 if (colorDepth <= 16) {
512 format = HAL_PIXEL_FORMAT_RGB_565;
513 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700514 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
515 if (colorDepth > 24) {
516 format = HAL_PIXEL_FORMAT_RGBA_1010102;
517 } else {
518 format = HAL_PIXEL_FORMAT_RGBX_8888;
519 }
520 } else {
521 format = HAL_PIXEL_FORMAT_RGBA_FP16;
522 }
523 }
524 } else {
525 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
526 if (colorDepth > 24) {
527 format = HAL_PIXEL_FORMAT_RGBA_1010102;
528 } else {
529 format = HAL_PIXEL_FORMAT_RGBA_8888;
530 }
531 } else {
532 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700533 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700534 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700535
536 // now select a corresponding sRGB format if needed
537 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
538 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
539 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530540 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700541 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700542 }
543 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800544
Jesse Hallc2e41222013-08-08 13:40:22 -0700545 if (format != 0) {
546 int err = native_window_set_buffers_format(window, format);
547 if (err != 0) {
548 ALOGE("error setting native window pixel format: %s (%d)",
549 strerror(-err), err);
550 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
551 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
552 }
553 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700554
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800555 if (dataSpace != 0) {
556 int err = native_window_set_buffers_data_space(window, dataSpace);
557 if (err != 0) {
558 ALOGE("error setting native window pixel dataSpace: %s (%d)",
559 strerror(-err), err);
560 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
561 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
562 }
563 }
564
Jamie Gennis59769462011-11-19 18:04:43 -0800565 // the EGL spec requires that a new EGLSurface default to swap interval
566 // 1, so explicitly set that on the window here.
567 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
568 anw->setSwapInterval(anw, 1);
569
Mathias Agopian518ec112011-05-13 16:21:08 -0700570 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800571 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700572 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700573 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
574 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 return s;
576 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700577
578 // EGLSurface creation failed
579 native_window_set_buffers_format(window, 0);
580 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700581 }
582 return EGL_NO_SURFACE;
583}
584
585EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
586 NativePixmapType pixmap,
587 const EGLint *attrib_list)
588{
589 clearError();
590
Jesse Hallb29e5e82012-04-04 16:53:42 -0700591 egl_connection_t* cnx = NULL;
592 egl_display_ptr dp = validate_display_connection(dpy, cnx);
593 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700594 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800595 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700596 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700597 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
598 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700599 return s;
600 }
601 }
602 return EGL_NO_SURFACE;
603}
604
605EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
606 const EGLint *attrib_list)
607{
608 clearError();
609
Jesse Hallb29e5e82012-04-04 16:53:42 -0700610 egl_connection_t* cnx = NULL;
611 egl_display_ptr dp = validate_display_connection(dpy, cnx);
612 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700613 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800614 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700615 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700616 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
617 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700618 return s;
619 }
620 }
621 return EGL_NO_SURFACE;
622}
Jesse Hall47743382013-02-08 11:13:46 -0800623
Mathias Agopian518ec112011-05-13 16:21:08 -0700624EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
625{
626 clearError();
627
Jesse Hallb29e5e82012-04-04 16:53:42 -0700628 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700629 if (!dp) return EGL_FALSE;
630
Jesse Hallb29e5e82012-04-04 16:53:42 -0700631 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700632 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800633 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700634
635 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800636 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700637 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700638 _s.terminate();
639 }
640 return result;
641}
642
643EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
644 EGLint attribute, EGLint *value)
645{
646 clearError();
647
Jesse Hallb29e5e82012-04-04 16:53:42 -0700648 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700649 if (!dp) return EGL_FALSE;
650
Jesse Hallb29e5e82012-04-04 16:53:42 -0700651 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700652 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800653 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700654
Mathias Agopian518ec112011-05-13 16:21:08 -0700655 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800656 return s->cnx->egl.eglQuerySurface(
657 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700658}
659
Jamie Gennise8696a42012-01-15 18:54:57 -0800660void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800661 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800662 clearError();
663
Jesse Hallb29e5e82012-04-04 16:53:42 -0700664 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800665 if (!dp) {
666 return;
667 }
668
Jesse Hallb29e5e82012-04-04 16:53:42 -0700669 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800670 if (!_s.get()) {
671 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800672 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800673}
674
Mathias Agopian518ec112011-05-13 16:21:08 -0700675// ----------------------------------------------------------------------------
676// Contexts
677// ----------------------------------------------------------------------------
678
679EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
680 EGLContext share_list, const EGLint *attrib_list)
681{
682 clearError();
683
Jesse Hallb29e5e82012-04-04 16:53:42 -0700684 egl_connection_t* cnx = NULL;
685 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700686 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700687 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700688 if (!ContextRef(dp.get(), share_list).get()) {
689 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
690 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700691 egl_context_t* const c = get_context(share_list);
692 share_list = c->context;
693 }
694 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800695 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700696 if (context != EGL_NO_CONTEXT) {
697 // figure out if it's a GLESv1 or GLESv2
698 int version = 0;
699 if (attrib_list) {
700 while (*attrib_list != EGL_NONE) {
701 GLint attr = *attrib_list++;
702 GLint value = *attrib_list++;
703 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
704 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800705 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800706 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800707 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700708 }
709 }
710 };
711 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700712 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
713 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700714 return c;
715 }
716 }
717 return EGL_NO_CONTEXT;
718}
719
720EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
721{
722 clearError();
723
Jesse Hallb29e5e82012-04-04 16:53:42 -0700724 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700725 if (!dp)
726 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700727
Jesse Hallb29e5e82012-04-04 16:53:42 -0700728 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700729 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800730 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800731
Mathias Agopian518ec112011-05-13 16:21:08 -0700732 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800733 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700734 if (result == EGL_TRUE) {
735 _c.terminate();
736 }
737 return result;
738}
739
Mathias Agopian518ec112011-05-13 16:21:08 -0700740EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
741 EGLSurface read, EGLContext ctx)
742{
743 clearError();
744
Jesse Hallb29e5e82012-04-04 16:53:42 -0700745 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800746 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700747
Mathias Agopian5b287a62011-05-16 18:58:55 -0700748 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
749 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
750 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700751 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
752 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800753 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700754 }
755
756 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700757 ContextRef _c(dp.get(), ctx);
758 SurfaceRef _d(dp.get(), draw);
759 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700760
761 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700762 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700763 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800764 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700765 }
766
767 // these are the underlying implementation's object
768 EGLContext impl_ctx = EGL_NO_CONTEXT;
769 EGLSurface impl_draw = EGL_NO_SURFACE;
770 EGLSurface impl_read = EGL_NO_SURFACE;
771
772 // these are our objects structs passed in
773 egl_context_t * c = NULL;
774 egl_surface_t const * d = NULL;
775 egl_surface_t const * r = NULL;
776
777 // these are the current objects structs
778 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800779
Mathias Agopian518ec112011-05-13 16:21:08 -0700780 if (ctx != EGL_NO_CONTEXT) {
781 c = get_context(ctx);
782 impl_ctx = c->context;
783 } else {
784 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700785 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
786 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800787 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700788 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700789 if (cur_c == NULL) {
790 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700791 // not an error, there is just no current context.
792 return EGL_TRUE;
793 }
794 }
795
796 // retrieve the underlying implementation's draw EGLSurface
797 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800798 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700800 impl_draw = d->surface;
801 }
802
803 // retrieve the underlying implementation's read EGLSurface
804 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800805 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700806 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700807 impl_read = r->surface;
808 }
809
Mathias Agopian518ec112011-05-13 16:21:08 -0700810
Jesse Hallb29e5e82012-04-04 16:53:42 -0700811 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800812 draw, read, ctx,
813 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700814
815 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800816 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700817 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
818 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700819 _c.acquire();
820 _r.acquire();
821 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700822 } else {
823 setGLHooksThreadSpecific(&gHooksNoContext);
824 egl_tls_t::setContext(EGL_NO_CONTEXT);
825 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700826 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000827 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700828 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -0800829 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700830 }
831 return result;
832}
833
834
835EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
836 EGLint attribute, EGLint *value)
837{
838 clearError();
839
Jesse Hallb29e5e82012-04-04 16:53:42 -0700840 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700841 if (!dp) return EGL_FALSE;
842
Jesse Hallb29e5e82012-04-04 16:53:42 -0700843 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -0800844 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700845
Mathias Agopian518ec112011-05-13 16:21:08 -0700846 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800847 return c->cnx->egl.eglQueryContext(
848 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700849
Mathias Agopian518ec112011-05-13 16:21:08 -0700850}
851
852EGLContext eglGetCurrentContext(void)
853{
854 // could be called before eglInitialize(), but we wouldn't have a context
855 // then, and this function would correctly return EGL_NO_CONTEXT.
856
857 clearError();
858
859 EGLContext ctx = getContext();
860 return ctx;
861}
862
863EGLSurface eglGetCurrentSurface(EGLint readdraw)
864{
865 // could be called before eglInitialize(), but we wouldn't have a context
866 // then, and this function would correctly return EGL_NO_SURFACE.
867
868 clearError();
869
870 EGLContext ctx = getContext();
871 if (ctx) {
872 egl_context_t const * const c = get_context(ctx);
873 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
874 switch (readdraw) {
875 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800876 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700877 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
878 }
879 }
880 return EGL_NO_SURFACE;
881}
882
883EGLDisplay eglGetCurrentDisplay(void)
884{
885 // could be called before eglInitialize(), but we wouldn't have a context
886 // then, and this function would correctly return EGL_NO_DISPLAY.
887
888 clearError();
889
890 EGLContext ctx = getContext();
891 if (ctx) {
892 egl_context_t const * const c = get_context(ctx);
893 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
894 return c->dpy;
895 }
896 return EGL_NO_DISPLAY;
897}
898
899EGLBoolean eglWaitGL(void)
900{
Mathias Agopian518ec112011-05-13 16:21:08 -0700901 clearError();
902
Mathias Agopianada798b2012-02-13 17:09:30 -0800903 egl_connection_t* const cnx = &gEGLImpl;
904 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800905 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800906
907 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700908}
909
910EGLBoolean eglWaitNative(EGLint engine)
911{
Mathias Agopian518ec112011-05-13 16:21:08 -0700912 clearError();
913
Mathias Agopianada798b2012-02-13 17:09:30 -0800914 egl_connection_t* const cnx = &gEGLImpl;
915 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800916 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800917
918 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700919}
920
921EGLint eglGetError(void)
922{
Mathias Agopianada798b2012-02-13 17:09:30 -0800923 EGLint err = EGL_SUCCESS;
924 egl_connection_t* const cnx = &gEGLImpl;
925 if (cnx->dso) {
926 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700927 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800928 if (err == EGL_SUCCESS) {
929 err = egl_tls_t::getError();
930 }
931 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700932}
933
Michael Chockc0ec5e22014-01-27 08:14:33 -0800934static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700935 const char* procname) {
936 const egl_connection_t* cnx = &gEGLImpl;
937 void* proc = NULL;
938
Michael Chockc0ec5e22014-01-27 08:14:33 -0800939 proc = dlsym(cnx->libEgl, procname);
940 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
941
Jesse Hallc07b5202013-07-04 12:08:16 -0700942 proc = dlsym(cnx->libGles2, procname);
943 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
944
945 proc = dlsym(cnx->libGles1, procname);
946 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
947
948 return NULL;
949}
950
Mathias Agopian518ec112011-05-13 16:21:08 -0700951__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
952{
953 // eglGetProcAddress() could be the very first function called
954 // in which case we must make sure we've initialized ourselves, this
955 // happens the first time egl_get_display() is called.
956
957 clearError();
958
959 if (egl_init_drivers() == EGL_FALSE) {
960 setError(EGL_BAD_PARAMETER, NULL);
961 return NULL;
962 }
963
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700964 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700965 return NULL;
966 }
967
Mathias Agopian518ec112011-05-13 16:21:08 -0700968 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700969 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700970 if (addr) return addr;
971
Michael Chockc0ec5e22014-01-27 08:14:33 -0800972 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700973 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700974
Mathias Agopian518ec112011-05-13 16:21:08 -0700975 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
976 pthread_mutex_lock(&sExtensionMapMutex);
977
978 /*
979 * Since eglGetProcAddress() is not associated to anything, it needs
980 * to return a function pointer that "works" regardless of what
981 * the current context is.
982 *
983 * For this reason, we return a "forwarder", a small stub that takes
984 * care of calling the function associated with the context
985 * currently bound.
986 *
987 * We first look for extensions we've already resolved, if we're seeing
988 * this extension for the first time, we go through all our
989 * implementations and call eglGetProcAddress() and record the
990 * result in the appropriate implementation hooks and return the
991 * address of the forwarder corresponding to that hook set.
992 *
993 */
994
995 const String8 name(procname);
996 addr = sGLExtentionMap.valueFor(name);
997 const int slot = sGLExtentionSlot;
998
Steve Blocke6f43dd2012-01-06 19:20:56 +0000999 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001000 "no more slots for eglGetProcAddress(\"%s\")",
1001 procname);
1002
1003 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1004 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001005
1006 egl_connection_t* const cnx = &gEGLImpl;
1007 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001008 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001009 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001010 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1011 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001012 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001013 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001014 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001015
Mathias Agopian518ec112011-05-13 16:21:08 -07001016 if (found) {
1017 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001018 sGLExtentionMap.add(name, addr);
1019 sGLExtentionSlot++;
1020 }
1021 }
1022
1023 pthread_mutex_unlock(&sExtensionMapMutex);
1024 return addr;
1025}
1026
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001027class FrameCompletionThread : public Thread {
1028public:
1029
1030 static void queueSync(EGLSyncKHR sync) {
1031 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1032 static bool running = false;
1033 if (!running) {
1034 thread->run("GPUFrameCompletion");
1035 running = true;
1036 }
1037 {
1038 Mutex::Autolock lock(thread->mMutex);
1039 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1040 thread->mFramesQueued).string());
1041 thread->mQueue.push_back(sync);
1042 thread->mCondition.signal();
1043 thread->mFramesQueued++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001044 ATRACE_INT("GPU Frames Outstanding", int32_t(thread->mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001045 }
1046 }
1047
1048private:
1049 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1050
1051 virtual bool threadLoop() {
1052 EGLSyncKHR sync;
1053 uint32_t frameNum;
1054 {
1055 Mutex::Autolock lock(mMutex);
1056 while (mQueue.isEmpty()) {
1057 mCondition.wait(mMutex);
1058 }
1059 sync = mQueue[0];
1060 frameNum = mFramesCompleted;
1061 }
1062 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1063 {
1064 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1065 frameNum).string());
1066 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1067 if (result == EGL_FALSE) {
1068 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1069 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1070 ALOGE("FrameCompletion: timeout waiting for fence");
1071 }
1072 eglDestroySyncKHR(dpy, sync);
1073 }
1074 {
1075 Mutex::Autolock lock(mMutex);
1076 mQueue.removeAt(0);
1077 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001078 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001079 }
1080 return true;
1081 }
1082
1083 uint32_t mFramesQueued;
1084 uint32_t mFramesCompleted;
1085 Vector<EGLSyncKHR> mQueue;
1086 Condition mCondition;
1087 Mutex mMutex;
1088};
1089
Dan Stozaa894d082015-02-19 15:27:36 -08001090EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1091 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001092{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001093 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001094 clearError();
1095
Jesse Hallb29e5e82012-04-04 16:53:42 -07001096 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001097 if (!dp) return EGL_FALSE;
1098
Jesse Hallb29e5e82012-04-04 16:53:42 -07001099 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001100 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001101 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001102
Mathias Agopian518ec112011-05-13 16:21:08 -07001103 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001104
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001105 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1106 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1107 if (sync != EGL_NO_SYNC_KHR) {
1108 FrameCompletionThread::queueSync(sync);
1109 }
1110 }
1111
Mathias Agopian7db993a2012-03-25 00:49:46 -07001112 if (CC_UNLIKELY(dp->finishOnSwap)) {
1113 uint32_t pixel;
1114 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1115 if (c) {
1116 // glReadPixels() ensures that the frame is complete
1117 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1118 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1119 }
1120 }
1121
Dan Stozaa894d082015-02-19 15:27:36 -08001122 if (n_rects == 0) {
1123 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1124 }
1125
1126 Vector<android_native_rect_t> androidRects;
1127 for (int r = 0; r < n_rects; ++r) {
1128 int offset = r * 4;
1129 int x = rects[offset];
1130 int y = rects[offset + 1];
1131 int width = rects[offset + 2];
1132 int height = rects[offset + 3];
1133 android_native_rect_t androidRect;
1134 androidRect.left = x;
1135 androidRect.top = y + height;
1136 androidRect.right = x + width;
1137 androidRect.bottom = y;
1138 androidRects.push_back(androidRect);
1139 }
1140 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1141 androidRects.size());
1142
1143 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1144 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1145 rects, n_rects);
1146 } else {
1147 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1148 }
1149}
1150
1151EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1152{
1153 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001154}
1155
1156EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1157 NativePixmapType target)
1158{
1159 clearError();
1160
Jesse Hallb29e5e82012-04-04 16:53:42 -07001161 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001162 if (!dp) return EGL_FALSE;
1163
Jesse Hallb29e5e82012-04-04 16:53:42 -07001164 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001165 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001166 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001167
Mathias Agopian518ec112011-05-13 16:21:08 -07001168 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001169 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001170}
1171
1172const char* eglQueryString(EGLDisplay dpy, EGLint name)
1173{
1174 clearError();
1175
Chia-I Wue57d1352016-08-15 16:10:02 +08001176 // Generate an error quietly when client extensions (as defined by
1177 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1178 // validate_display to generate the error as validate_display would log
1179 // the error, which can be misleading.
1180 //
1181 // If we want to support EGL_EXT_client_extensions later, we can return
1182 // the client extension string here instead.
1183 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001184 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001185
Jesse Hallb29e5e82012-04-04 16:53:42 -07001186 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001187 if (!dp) return (const char *) NULL;
1188
1189 switch (name) {
1190 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001191 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001192 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001193 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001194 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001195 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001196 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001197 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001198 default:
1199 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001200 }
1201 return setError(EGL_BAD_PARAMETER, (const char *)0);
1202}
1203
Mathias Agopianca088332013-03-28 17:44:13 -07001204EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1205{
1206 clearError();
1207
1208 const egl_display_ptr dp = validate_display(dpy);
1209 if (!dp) return (const char *) NULL;
1210
1211 switch (name) {
1212 case EGL_VENDOR:
1213 return dp->disp.queryString.vendor;
1214 case EGL_VERSION:
1215 return dp->disp.queryString.version;
1216 case EGL_EXTENSIONS:
1217 return dp->disp.queryString.extensions;
1218 case EGL_CLIENT_APIS:
1219 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001220 default:
1221 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001222 }
1223 return setError(EGL_BAD_PARAMETER, (const char *)0);
1224}
Mathias Agopian518ec112011-05-13 16:21:08 -07001225
1226// ----------------------------------------------------------------------------
1227// EGL 1.1
1228// ----------------------------------------------------------------------------
1229
1230EGLBoolean eglSurfaceAttrib(
1231 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1232{
1233 clearError();
1234
Jesse Hallb29e5e82012-04-04 16:53:42 -07001235 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001236 if (!dp) return EGL_FALSE;
1237
Jesse Hallb29e5e82012-04-04 16:53:42 -07001238 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001239 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001240 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001241
Pablo Ceballosc18be292016-05-31 14:55:42 -07001242 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001243
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001244 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001245 if (!s->win.get()) {
1246 setError(EGL_BAD_SURFACE, EGL_FALSE);
1247 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001248 int err = native_window_set_auto_refresh(s->win.get(), value ? true : false);
1249 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001250 }
1251
Pablo Ceballosc18be292016-05-31 14:55:42 -07001252 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001253 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001254 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001255 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001256 int err = native_window_enable_frame_timestamps(s->win.get(), value ? true : false);
1257 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001258 }
1259
Mathias Agopian518ec112011-05-13 16:21:08 -07001260 if (s->cnx->egl.eglSurfaceAttrib) {
1261 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001262 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001263 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001264 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001265}
1266
1267EGLBoolean eglBindTexImage(
1268 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1269{
1270 clearError();
1271
Jesse Hallb29e5e82012-04-04 16:53:42 -07001272 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001273 if (!dp) return EGL_FALSE;
1274
Jesse Hallb29e5e82012-04-04 16:53:42 -07001275 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001276 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001277 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001278
Mathias Agopian518ec112011-05-13 16:21:08 -07001279 egl_surface_t const * const s = get_surface(surface);
1280 if (s->cnx->egl.eglBindTexImage) {
1281 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001282 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001283 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001284 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001285}
1286
1287EGLBoolean eglReleaseTexImage(
1288 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1289{
1290 clearError();
1291
Jesse Hallb29e5e82012-04-04 16:53:42 -07001292 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001293 if (!dp) return EGL_FALSE;
1294
Jesse Hallb29e5e82012-04-04 16:53:42 -07001295 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001296 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001297 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001298
Mathias Agopian518ec112011-05-13 16:21:08 -07001299 egl_surface_t const * const s = get_surface(surface);
1300 if (s->cnx->egl.eglReleaseTexImage) {
1301 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001302 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001303 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001304 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001305}
1306
1307EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1308{
1309 clearError();
1310
Jesse Hallb29e5e82012-04-04 16:53:42 -07001311 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001312 if (!dp) return EGL_FALSE;
1313
1314 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001315 egl_connection_t* const cnx = &gEGLImpl;
1316 if (cnx->dso && cnx->egl.eglSwapInterval) {
1317 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001318 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001319
Mathias Agopian518ec112011-05-13 16:21:08 -07001320 return res;
1321}
1322
1323
1324// ----------------------------------------------------------------------------
1325// EGL 1.2
1326// ----------------------------------------------------------------------------
1327
1328EGLBoolean eglWaitClient(void)
1329{
1330 clearError();
1331
Mathias Agopianada798b2012-02-13 17:09:30 -08001332 egl_connection_t* const cnx = &gEGLImpl;
1333 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001334 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001335
1336 EGLBoolean res;
1337 if (cnx->egl.eglWaitClient) {
1338 res = cnx->egl.eglWaitClient();
1339 } else {
1340 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001341 }
1342 return res;
1343}
1344
1345EGLBoolean eglBindAPI(EGLenum api)
1346{
1347 clearError();
1348
1349 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001350 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001351 }
1352
1353 // bind this API on all EGLs
1354 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001355 egl_connection_t* const cnx = &gEGLImpl;
1356 if (cnx->dso && cnx->egl.eglBindAPI) {
1357 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001358 }
1359 return res;
1360}
1361
1362EGLenum eglQueryAPI(void)
1363{
1364 clearError();
1365
1366 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001367 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001368 }
1369
Mathias Agopianada798b2012-02-13 17:09:30 -08001370 egl_connection_t* const cnx = &gEGLImpl;
1371 if (cnx->dso && cnx->egl.eglQueryAPI) {
1372 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001373 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001374
Mathias Agopian518ec112011-05-13 16:21:08 -07001375 // or, it can only be OpenGL ES
1376 return EGL_OPENGL_ES_API;
1377}
1378
1379EGLBoolean eglReleaseThread(void)
1380{
1381 clearError();
1382
Mathias Agopianada798b2012-02-13 17:09:30 -08001383 egl_connection_t* const cnx = &gEGLImpl;
1384 if (cnx->dso && cnx->egl.eglReleaseThread) {
1385 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001386 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301387
1388 // If there is context bound to the thread, release it
1389 egl_display_t::loseCurrent(get_context(getContext()));
1390
Mathias Agopian518ec112011-05-13 16:21:08 -07001391 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001392 return EGL_TRUE;
1393}
1394
1395EGLSurface eglCreatePbufferFromClientBuffer(
1396 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1397 EGLConfig config, const EGLint *attrib_list)
1398{
1399 clearError();
1400
Jesse Hallb29e5e82012-04-04 16:53:42 -07001401 egl_connection_t* cnx = NULL;
1402 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1403 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001404 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1405 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001406 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001407 }
1408 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1409}
1410
1411// ----------------------------------------------------------------------------
1412// EGL_EGLEXT_VERSION 3
1413// ----------------------------------------------------------------------------
1414
1415EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1416 const EGLint *attrib_list)
1417{
1418 clearError();
1419
Jesse Hallb29e5e82012-04-04 16:53:42 -07001420 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001421 if (!dp) return EGL_FALSE;
1422
Jesse Hallb29e5e82012-04-04 16:53:42 -07001423 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001424 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001425 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001426
1427 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001428 if (s->cnx->egl.eglLockSurfaceKHR) {
1429 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001430 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001431 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001432 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001433}
1434
1435EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1436{
1437 clearError();
1438
Jesse Hallb29e5e82012-04-04 16:53:42 -07001439 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001440 if (!dp) return EGL_FALSE;
1441
Jesse Hallb29e5e82012-04-04 16:53:42 -07001442 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001443 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001444 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001445
1446 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001447 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001448 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001449 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001450 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001451}
1452
1453EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1454 EGLClientBuffer buffer, const EGLint *attrib_list)
1455{
1456 clearError();
1457
Jesse Hallb29e5e82012-04-04 16:53:42 -07001458 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001459 if (!dp) return EGL_NO_IMAGE_KHR;
1460
Jesse Hallb29e5e82012-04-04 16:53:42 -07001461 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001462 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001463
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001464 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1465 egl_connection_t* const cnx = &gEGLImpl;
1466 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1467 result = cnx->egl.eglCreateImageKHR(
1468 dp->disp.dpy,
1469 c ? c->context : EGL_NO_CONTEXT,
1470 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001471 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001472 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001473}
1474
1475EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1476{
1477 clearError();
1478
Jesse Hallb29e5e82012-04-04 16:53:42 -07001479 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001480 if (!dp) return EGL_FALSE;
1481
Steven Holte646a5c52012-06-04 20:02:11 -07001482 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001483 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001484 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001485 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001486 }
Steven Holte646a5c52012-06-04 20:02:11 -07001487 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001488}
1489
1490// ----------------------------------------------------------------------------
1491// EGL_EGLEXT_VERSION 5
1492// ----------------------------------------------------------------------------
1493
1494
1495EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1496{
1497 clearError();
1498
Jesse Hallb29e5e82012-04-04 16:53:42 -07001499 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001500 if (!dp) return EGL_NO_SYNC_KHR;
1501
Mathias Agopian518ec112011-05-13 16:21:08 -07001502 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001503 egl_connection_t* const cnx = &gEGLImpl;
1504 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1505 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001506 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001507 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001508}
1509
1510EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1511{
1512 clearError();
1513
Jesse Hallb29e5e82012-04-04 16:53:42 -07001514 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001515 if (!dp) return EGL_FALSE;
1516
Mathias Agopian518ec112011-05-13 16:21:08 -07001517 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001518 egl_connection_t* const cnx = &gEGLImpl;
1519 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1520 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001521 }
1522 return result;
1523}
1524
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001525EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1526 clearError();
1527
1528 const egl_display_ptr dp = validate_display(dpy);
1529 if (!dp) return EGL_FALSE;
1530
1531 EGLBoolean result = EGL_FALSE;
1532 egl_connection_t* const cnx = &gEGLImpl;
1533 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1534 result = cnx->egl.eglSignalSyncKHR(
1535 dp->disp.dpy, sync, mode);
1536 }
1537 return result;
1538}
1539
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001540EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1541 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001542{
1543 clearError();
1544
Jesse Hallb29e5e82012-04-04 16:53:42 -07001545 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001546 if (!dp) return EGL_FALSE;
1547
Mathias Agopian737b8962017-02-24 14:32:05 -08001548 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001549 egl_connection_t* const cnx = &gEGLImpl;
1550 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1551 result = cnx->egl.eglClientWaitSyncKHR(
1552 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001553 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001554 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001555}
1556
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001557EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1558 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001559{
1560 clearError();
1561
Jesse Hallb29e5e82012-04-04 16:53:42 -07001562 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001563 if (!dp) return EGL_FALSE;
1564
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001565 EGLBoolean result = EGL_FALSE;
1566 egl_connection_t* const cnx = &gEGLImpl;
1567 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1568 result = cnx->egl.eglGetSyncAttribKHR(
1569 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001570 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001571 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001572}
1573
Season Li000d88f2015-07-01 11:39:40 -07001574EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1575{
1576 clearError();
1577
1578 const egl_display_ptr dp = validate_display(dpy);
1579 if (!dp) return EGL_NO_STREAM_KHR;
1580
1581 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1582 egl_connection_t* const cnx = &gEGLImpl;
1583 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1584 result = cnx->egl.eglCreateStreamKHR(
1585 dp->disp.dpy, attrib_list);
1586 }
1587 return result;
1588}
1589
1590EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1591{
1592 clearError();
1593
1594 const egl_display_ptr dp = validate_display(dpy);
1595 if (!dp) return EGL_FALSE;
1596
1597 EGLBoolean result = EGL_FALSE;
1598 egl_connection_t* const cnx = &gEGLImpl;
1599 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1600 result = cnx->egl.eglDestroyStreamKHR(
1601 dp->disp.dpy, stream);
1602 }
1603 return result;
1604}
1605
1606EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1607 EGLenum attribute, EGLint value)
1608{
1609 clearError();
1610
1611 const egl_display_ptr dp = validate_display(dpy);
1612 if (!dp) return EGL_FALSE;
1613
1614 EGLBoolean result = EGL_FALSE;
1615 egl_connection_t* const cnx = &gEGLImpl;
1616 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1617 result = cnx->egl.eglStreamAttribKHR(
1618 dp->disp.dpy, stream, attribute, value);
1619 }
1620 return result;
1621}
1622
1623EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1624 EGLenum attribute, EGLint *value)
1625{
1626 clearError();
1627
1628 const egl_display_ptr dp = validate_display(dpy);
1629 if (!dp) return EGL_FALSE;
1630
1631 EGLBoolean result = EGL_FALSE;
1632 egl_connection_t* const cnx = &gEGLImpl;
1633 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1634 result = cnx->egl.eglQueryStreamKHR(
1635 dp->disp.dpy, stream, attribute, value);
1636 }
1637 return result;
1638}
1639
1640EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1641 EGLenum attribute, EGLuint64KHR *value)
1642{
1643 clearError();
1644
1645 const egl_display_ptr dp = validate_display(dpy);
1646 if (!dp) return EGL_FALSE;
1647
1648 EGLBoolean result = EGL_FALSE;
1649 egl_connection_t* const cnx = &gEGLImpl;
1650 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1651 result = cnx->egl.eglQueryStreamu64KHR(
1652 dp->disp.dpy, stream, attribute, value);
1653 }
1654 return result;
1655}
1656
1657EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1658 EGLenum attribute, EGLTimeKHR *value)
1659{
1660 clearError();
1661
1662 const egl_display_ptr dp = validate_display(dpy);
1663 if (!dp) return EGL_FALSE;
1664
1665 EGLBoolean result = EGL_FALSE;
1666 egl_connection_t* const cnx = &gEGLImpl;
1667 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1668 result = cnx->egl.eglQueryStreamTimeKHR(
1669 dp->disp.dpy, stream, attribute, value);
1670 }
1671 return result;
1672}
1673
1674EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1675 EGLStreamKHR stream, const EGLint *attrib_list)
1676{
1677 clearError();
1678
1679 egl_display_ptr dp = validate_display(dpy);
1680 if (!dp) return EGL_NO_SURFACE;
1681
1682 egl_connection_t* const cnx = &gEGLImpl;
1683 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1684 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1685 dp->disp.dpy, config, stream, attrib_list);
1686 if (surface != EGL_NO_SURFACE) {
1687 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1688 surface, cnx);
1689 return s;
1690 }
1691 }
1692 return EGL_NO_SURFACE;
1693}
1694
1695EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1696 EGLStreamKHR stream)
1697{
1698 clearError();
1699
1700 const egl_display_ptr dp = validate_display(dpy);
1701 if (!dp) return EGL_FALSE;
1702
1703 EGLBoolean result = EGL_FALSE;
1704 egl_connection_t* const cnx = &gEGLImpl;
1705 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1706 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1707 dp->disp.dpy, stream);
1708 }
1709 return result;
1710}
1711
1712EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1713 EGLStreamKHR stream)
1714{
1715 clearError();
1716
1717 const egl_display_ptr dp = validate_display(dpy);
1718 if (!dp) return EGL_FALSE;
1719
1720 EGLBoolean result = EGL_FALSE;
1721 egl_connection_t* const cnx = &gEGLImpl;
1722 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1723 result = cnx->egl.eglStreamConsumerAcquireKHR(
1724 dp->disp.dpy, stream);
1725 }
1726 return result;
1727}
1728
1729EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1730 EGLStreamKHR stream)
1731{
1732 clearError();
1733
1734 const egl_display_ptr dp = validate_display(dpy);
1735 if (!dp) return EGL_FALSE;
1736
1737 EGLBoolean result = EGL_FALSE;
1738 egl_connection_t* const cnx = &gEGLImpl;
1739 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1740 result = cnx->egl.eglStreamConsumerReleaseKHR(
1741 dp->disp.dpy, stream);
1742 }
1743 return result;
1744}
1745
1746EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1747 EGLDisplay dpy, EGLStreamKHR stream)
1748{
1749 clearError();
1750
1751 const egl_display_ptr dp = validate_display(dpy);
1752 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1753
1754 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1755 egl_connection_t* const cnx = &gEGLImpl;
1756 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1757 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1758 dp->disp.dpy, stream);
1759 }
1760 return result;
1761}
1762
1763EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1764 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1765{
1766 clearError();
1767
1768 const egl_display_ptr dp = validate_display(dpy);
1769 if (!dp) return EGL_NO_STREAM_KHR;
1770
1771 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1772 egl_connection_t* const cnx = &gEGLImpl;
1773 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1774 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1775 dp->disp.dpy, file_descriptor);
1776 }
1777 return result;
1778}
1779
Mathias Agopian518ec112011-05-13 16:21:08 -07001780// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001781// EGL_EGLEXT_VERSION 15
1782// ----------------------------------------------------------------------------
1783
1784EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1785 clearError();
1786 const egl_display_ptr dp = validate_display(dpy);
1787 if (!dp) return EGL_FALSE;
1788 EGLint result = EGL_FALSE;
1789 egl_connection_t* const cnx = &gEGLImpl;
1790 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1791 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1792 }
1793 return result;
1794}
1795
1796// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001797// ANDROID extensions
1798// ----------------------------------------------------------------------------
1799
Jamie Gennis331841b2012-09-06 14:52:00 -07001800EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1801{
1802 clearError();
1803
1804 const egl_display_ptr dp = validate_display(dpy);
1805 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1806
1807 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1808 egl_connection_t* const cnx = &gEGLImpl;
1809 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1810 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1811 }
1812 return result;
1813}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001814
Andy McFadden72841452013-03-01 16:25:32 -08001815EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1816 EGLnsecsANDROID time)
1817{
1818 clearError();
1819
1820 const egl_display_ptr dp = validate_display(dpy);
1821 if (!dp) {
1822 return EGL_FALSE;
1823 }
1824
1825 SurfaceRef _s(dp.get(), surface);
1826 if (!_s.get()) {
1827 setError(EGL_BAD_SURFACE, EGL_FALSE);
1828 return EGL_FALSE;
1829 }
1830
1831 egl_surface_t const * const s = get_surface(surface);
1832 native_window_set_buffers_timestamp(s->win.get(), time);
1833
1834 return EGL_TRUE;
1835}
1836
Craig Donner05249fc2016-01-15 19:33:55 -08001837EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1838{
1839 clearError();
1840
Craig Donnere96a3252017-02-02 12:13:34 -08001841 uint64_t producerUsage = 0;
1842 uint64_t consumerUsage = 0;
Craig Donner05249fc2016-01-15 19:33:55 -08001843 uint32_t width = 0;
1844 uint32_t height = 0;
1845 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001846 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001847 uint32_t red_size = 0;
1848 uint32_t green_size = 0;
1849 uint32_t blue_size = 0;
1850 uint32_t alpha_size = 0;
1851
Craig Donnerb40504a2016-06-03 17:54:25 -07001852#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001853 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001854 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001855 target = value; \
1856 } else { \
1857 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1858 } \
1859 break
1860
1861 if (attrib_list) {
1862 while (*attrib_list != EGL_NONE) {
1863 GLint attr = *attrib_list++;
1864 GLint value = *attrib_list++;
1865 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001866 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1867 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1868 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1869 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1870 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1871 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001872 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001873 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1874 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001875 producerUsage |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001876 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001877 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001878 producerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
Craig Donner05249fc2016-01-15 19:33:55 -08001879 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001880 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001881 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
Craig Donner05249fc2016-01-15 19:33:55 -08001882 }
Craig Donner05249fc2016-01-15 19:33:55 -08001883 break;
1884 default:
1885 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1886 }
1887 }
1888 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001889#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001890
1891 // Validate format.
1892 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1893 if (alpha_size == 8) {
1894 format = HAL_PIXEL_FORMAT_RGBA_8888;
1895 } else {
1896 format = HAL_PIXEL_FORMAT_RGB_888;
1897 }
1898 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1899 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001900 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001901 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001902 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001903 red_size, green_size, blue_size, alpha_size);
1904 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1905 }
1906
Craig Donner68671532016-07-18 10:19:54 -07001907#define CHECK_ERROR_CONDITION(message) \
1908 if (err != NO_ERROR) { \
1909 ALOGE(message); \
1910 goto error_condition; \
1911 }
1912
1913 // The holder is used to destroy the buffer if an error occurs.
1914 GraphicBuffer* gBuffer = new GraphicBuffer();
1915 sp<IServiceManager> sm = defaultServiceManager();
1916 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1917 sp<IBinder> allocator;
1918 Parcel sc_data, sc_reply, data, reply;
1919 status_t err = NO_ERROR;
1920 if (sm == NULL) {
1921 ALOGE("Unable to connect to ServiceManager");
1922 goto error_condition;
1923 }
1924
1925 // Obtain an allocator.
1926 if (surfaceFlinger == NULL) {
1927 ALOGE("Unable to connect to SurfaceFlinger");
1928 goto error_condition;
1929 }
1930 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1931 err = surfaceFlinger->transact(
1932 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1933 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1934 allocator = sc_reply.readStrongBinder();
1935
1936 if (allocator == NULL) {
1937 ALOGE("Unable to obtain an ISurfaceComposer");
1938 goto error_condition;
1939 }
1940 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1941 err = data.writeUint32(width);
1942 CHECK_ERROR_CONDITION("Unable to write width");
1943 err = data.writeUint32(height);
1944 CHECK_ERROR_CONDITION("Unable to write height");
1945 err = data.writeInt32(static_cast<int32_t>(format));
1946 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001947 err = data.writeUint32(layer_count);
1948 CHECK_ERROR_CONDITION("Unable to write layer count");
Craig Donnere96a3252017-02-02 12:13:34 -08001949 err = data.writeUint64(producerUsage);
1950 CHECK_ERROR_CONDITION("Unable to write producer usage");
1951 err = data.writeUint64(consumerUsage);
1952 CHECK_ERROR_CONDITION("Unable to write consumer usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001953 err = data.writeUtf8AsUtf16(
1954 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1955 std::to_string(getpid()) + ']');
1956 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001957 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1958 &reply);
1959 CHECK_ERROR_CONDITION(
1960 "Unable to request buffer allocation from surface composer");
1961 err = reply.readInt32();
1962 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1963 err = reply.read(*gBuffer);
1964 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1965
1966 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001967 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001968 ALOGE("Unable to create native buffer "
Craig Donnere96a3252017-02-02 12:13:34 -08001969 "{ w=%u, h=%u, f=%u, pu=%" PRIx64 " cu=%" PRIx64 ", lc=%u} %#x",
1970 width, height, format, producerUsage, consumerUsage,
1971 layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001972 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001973 }
Craig Donnere96a3252017-02-02 12:13:34 -08001974 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, pu=%" PRIx64
1975 " cu=%" PRIx64 ", lc=%u}",
1976 gBuffer, width, height, format, producerUsage, consumerUsage,
1977 layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001978 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001979
1980#undef CHECK_ERROR_CONDITION
1981
1982error_condition:
1983 // Delete the buffer.
1984 sp<GraphicBuffer> holder(gBuffer);
1985 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001986}
1987
Craig Donner60761072017-01-27 12:30:44 -08001988EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1989 clearError();
1990
1991 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1992
Mathias Agopian89ed4c82017-02-09 18:48:34 -08001993 const GraphicBuffer* graphicBuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
Craig Donner60761072017-01-27 12:30:44 -08001994 return static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
1995}
1996
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001997// ----------------------------------------------------------------------------
1998// NVIDIA extensions
1999// ----------------------------------------------------------------------------
2000EGLuint64NV eglGetSystemTimeFrequencyNV()
2001{
2002 clearError();
2003
2004 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002005 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002006 }
2007
2008 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002009 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002010
Mathias Agopianada798b2012-02-13 17:09:30 -08002011 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2012 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002013 }
2014
Mathias Agopian737b8962017-02-24 14:32:05 -08002015 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002016}
2017
2018EGLuint64NV eglGetSystemTimeNV()
2019{
2020 clearError();
2021
2022 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002023 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002024 }
2025
2026 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002027 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002028
Mathias Agopianada798b2012-02-13 17:09:30 -08002029 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2030 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002031 }
2032
Mathias Agopian737b8962017-02-24 14:32:05 -08002033 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002034}
Dan Stozaa894d082015-02-19 15:27:36 -08002035
2036// ----------------------------------------------------------------------------
2037// Partial update extension
2038// ----------------------------------------------------------------------------
2039EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2040 EGLint *rects, EGLint n_rects)
2041{
2042 clearError();
2043
2044 const egl_display_ptr dp = validate_display(dpy);
2045 if (!dp) {
2046 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2047 return EGL_FALSE;
2048 }
2049
2050 SurfaceRef _s(dp.get(), surface);
2051 if (!_s.get()) {
2052 setError(EGL_BAD_SURFACE, EGL_FALSE);
2053 return EGL_FALSE;
2054 }
2055
2056 egl_surface_t const * const s = get_surface(surface);
2057 if (s->cnx->egl.eglSetDamageRegionKHR) {
2058 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2059 rects, n_rects);
2060 }
2061
2062 return EGL_FALSE;
2063}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002064
Brian Anderson1049d1d2016-12-16 17:25:57 -08002065EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2066 EGLuint64KHR *frameId) {
2067 clearError();
2068
2069 const egl_display_ptr dp = validate_display(dpy);
2070 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002071 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002072 }
2073
2074 SurfaceRef _s(dp.get(), surface);
2075 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002076 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002077 }
2078
2079 egl_surface_t const * const s = get_surface(surface);
2080
2081 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002082 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002083 }
2084
2085 uint64_t nextFrameId = 0;
2086 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2087
2088 if (ret != NO_ERROR) {
2089 // This should not happen. Return an error that is not in the spec
2090 // so it's obvious something is very wrong.
2091 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002092 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002093 }
2094
2095 *frameId = nextFrameId;
2096 return EGL_TRUE;
2097}
2098
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002099EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2100 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2101{
2102 clearError();
2103
2104 const egl_display_ptr dp = validate_display(dpy);
2105 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002106 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002107 }
2108
2109 SurfaceRef _s(dp.get(), surface);
2110 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002111 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002112 }
2113
2114 egl_surface_t const * const s = get_surface(surface);
2115
2116 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002117 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002118 }
2119
2120 nsecs_t* compositeDeadline = nullptr;
2121 nsecs_t* compositeInterval = nullptr;
2122 nsecs_t* compositeToPresentLatency = nullptr;
2123
2124 for (int i = 0; i < numTimestamps; i++) {
2125 switch (names[i]) {
2126 case EGL_COMPOSITE_DEADLINE_ANDROID:
2127 compositeDeadline = &values[i];
2128 break;
2129 case EGL_COMPOSITE_INTERVAL_ANDROID:
2130 compositeInterval = &values[i];
2131 break;
2132 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2133 compositeToPresentLatency = &values[i];
2134 break;
2135 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002136 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002137 }
2138 }
2139
2140 status_t ret = native_window_get_compositor_timing(s->win.get(),
2141 compositeDeadline, compositeInterval, compositeToPresentLatency);
2142
2143 switch (ret) {
2144 case NO_ERROR:
2145 return EGL_TRUE;
2146 case INVALID_OPERATION:
Mathias Agopian737b8962017-02-24 14:32:05 -08002147 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002148 default:
2149 // This should not happen. Return an error that is not in the spec
2150 // so it's obvious something is very wrong.
2151 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002152 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002153 }
2154}
2155
2156EGLBoolean eglGetCompositorTimingSupportedANDROID(
2157 EGLDisplay dpy, EGLSurface surface, EGLint name)
2158{
2159 clearError();
2160
2161 const egl_display_ptr dp = validate_display(dpy);
2162 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002163 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002164 }
2165
2166 SurfaceRef _s(dp.get(), surface);
2167 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002168 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002169 }
2170
2171 egl_surface_t const * const s = get_surface(surface);
2172
2173 ANativeWindow* window = s->win.get();
2174 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002175 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002176 }
2177
2178 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002179 case EGL_COMPOSITE_DEADLINE_ANDROID:
2180 case EGL_COMPOSITE_INTERVAL_ANDROID:
2181 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2182 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002183 default:
2184 return EGL_FALSE;
2185 }
2186}
2187
Pablo Ceballosc18be292016-05-31 14:55:42 -07002188EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002189 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002190 EGLnsecsANDROID *values)
2191{
2192 clearError();
2193
2194 const egl_display_ptr dp = validate_display(dpy);
2195 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002196 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002197 }
2198
2199 SurfaceRef _s(dp.get(), surface);
2200 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002201 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002202 }
2203
2204 egl_surface_t const * const s = get_surface(surface);
2205
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002206 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002207 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002208 }
2209
Brian Andersondbd0ea82016-07-22 09:38:59 -07002210 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002211 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002212 nsecs_t* latchTime = nullptr;
2213 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002214 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002215 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002216 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002217 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002218 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002219 nsecs_t* releaseTime = nullptr;
2220
2221 for (int i = 0; i < numTimestamps; i++) {
2222 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002223 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2224 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002225 break;
2226 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2227 acquireTime = &values[i];
2228 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002229 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2230 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002231 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002232 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2233 firstRefreshStartTime = &values[i];
2234 break;
2235 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2236 lastRefreshStartTime = &values[i];
2237 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002238 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2239 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002240 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002241 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2242 displayPresentTime = &values[i];
2243 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002244 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2245 displayRetireTime = &values[i];
2246 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002247 case EGL_DEQUEUE_READY_TIME_ANDROID:
2248 dequeueReadyTime = &values[i];
2249 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002250 case EGL_READS_DONE_TIME_ANDROID:
2251 releaseTime = &values[i];
2252 break;
2253 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002254 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002255 }
2256 }
2257
Brian Anderson1049d1d2016-12-16 17:25:57 -08002258 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002259 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002260 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002261 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002262
Brian Anderson069b3652016-07-22 10:32:47 -07002263 switch (ret) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002264 case NO_ERROR:
2265 return EGL_TRUE;
2266 case NAME_NOT_FOUND:
2267 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
2268 case INVALID_OPERATION:
2269 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2270 case BAD_VALUE:
2271 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2272 default:
2273 // This should not happen. Return an error that is not in the spec
2274 // so it's obvious something is very wrong.
2275 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2276 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002277 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002278}
2279
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002280EGLBoolean eglGetFrameTimestampSupportedANDROID(
2281 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002282{
2283 clearError();
2284
2285 const egl_display_ptr dp = validate_display(dpy);
2286 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002287 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002288 }
2289
2290 SurfaceRef _s(dp.get(), surface);
2291 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002292 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002293 }
2294
2295 egl_surface_t const * const s = get_surface(surface);
2296
2297 ANativeWindow* window = s->win.get();
2298 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002299 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002300 }
2301
2302 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002303 case EGL_COMPOSITE_DEADLINE_ANDROID:
2304 case EGL_COMPOSITE_INTERVAL_ANDROID:
2305 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002306 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002307 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002308 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2309 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2310 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002311 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002312 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002313 case EGL_READS_DONE_TIME_ANDROID:
2314 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002315 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2316 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002317 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002318 return value == 0 ? EGL_FALSE : EGL_TRUE;
2319 }
2320 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2321 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002322 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002323 return value == 0 ? EGL_FALSE : EGL_TRUE;
2324 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002325 default:
2326 return EGL_FALSE;
2327 }
2328}