blob: c434d0ebad698a879d9e4c3c5df337ea526132f8 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025#include <system/window.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
Craig Donner60761072017-01-27 12:30:44 -080030#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080031#include <private/android/AHardwareBufferHelpers.h>
32
Mathias Agopian518ec112011-05-13 16:21:08 -070033#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070034#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035#include <cutils/memory.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070036#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070037#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070038
Craig Donner68671532016-07-18 10:19:54 -070039#include <gui/ISurfaceComposer.h>
40
Craig Donner05249fc2016-01-15 19:33:55 -080041#include <ui/GraphicBuffer.h>
42
Mathias Agopian89ed4c82017-02-09 18:48:34 -080043
Mathias Agopian518ec112011-05-13 16:21:08 -070044#include <utils/KeyedVector.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070045#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080046#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070047
Craig Donner68671532016-07-18 10:19:54 -070048#include "binder/Binder.h"
49#include "binder/Parcel.h"
50#include "binder/IServiceManager.h"
51
Mathias Agopian39c24a22013-04-04 23:17:56 -070052#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070053#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070054
55#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070056#include "egl_object.h"
57#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080058#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070059
60using namespace android;
61
62// ----------------------------------------------------------------------------
63
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070064namespace android {
65
Mathias Agopian518ec112011-05-13 16:21:08 -070066struct extention_map_t {
67 const char* name;
68 __eglMustCastToProperFunctionPointerType address;
69};
70
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070071/*
Jesse Hall21558da2013-08-06 15:31:22 -070072 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070073 *
Jesse Hall21558da2013-08-06 15:31:22 -070074 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
75 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070076 *
Jesse Hall21558da2013-08-06 15:31:22 -070077 * The rest (gExtensionString) depend on support in the EGL driver, and are
78 * only available if the driver supports them. However, some of these must be
79 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080080 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070081 * the mandatory extensions are present and may not function properly if some
82 * are missing.
83 *
84 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070085 */
Mathias Agopian737b8962017-02-24 14:32:05 -080086
87// CLion mistakenly warns about the extern keyword below.
88#pragma clang diagnostic push
89#pragma clang diagnostic ignored "-Wextern-initializer"
90
Jesse Hall21558da2013-08-06 15:31:22 -070091extern char const * const gBuiltinExtensionString =
92 "EGL_KHR_get_all_proc_addresses "
93 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080094 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080095 "EGL_ANDROID_create_native_client_buffer "
Craig Donner60761072017-01-27 12:30:44 -080096 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080097 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070098 "EGL_ANDROID_get_frame_timestamps "
Jesse Hall21558da2013-08-06 15:31:22 -070099 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700100extern char const * const gExtensionString =
101 "EGL_KHR_image " // mandatory
102 "EGL_KHR_image_base " // mandatory
103 "EGL_KHR_image_pixmap "
104 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700105 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700106 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700107 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700108 "EGL_KHR_gl_texture_cubemap_image "
109 "EGL_KHR_gl_renderbuffer_image "
110 "EGL_KHR_reusable_sync "
111 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700112 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700113 "EGL_KHR_config_attribs "
114 "EGL_KHR_surfaceless_context "
115 "EGL_KHR_stream "
116 "EGL_KHR_stream_fifo "
117 "EGL_KHR_stream_producer_eglsurface "
118 "EGL_KHR_stream_consumer_gltexture "
119 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700120 "EGL_EXT_create_context_robustness "
121 "EGL_NV_system_time "
122 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700123 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700124 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800125 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700126 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800127 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700128 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700129 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700130 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700131 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000132 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700133 ;
134
135// extensions not exposed to applications but used by the ANDROID system
136// "EGL_ANDROID_blob_cache " // strongly recommended
137// "EGL_IMG_hibernate_process " // optional
138// "EGL_ANDROID_native_fence_sync " // strongly recommended
139// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700140// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700141
142/*
143 * EGL Extensions entry-points exposed to 3rd party applications
144 * (keep in sync with gExtensionString above)
145 *
146 */
147static const extention_map_t sExtensionMap[] = {
148 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700149 { "eglLockSurfaceKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
151 { "eglUnlockSurfaceKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700153
154 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700155 { "eglCreateImageKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
157 { "eglDestroyImageKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700159
160 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
161 { "eglCreateSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
163 { "eglDestroySyncKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
165 { "eglClientWaitSyncKHR",
166 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
167 { "eglSignalSyncKHR",
168 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
169 { "eglGetSyncAttribKHR",
170 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
171
172 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800173 { "eglGetSystemTimeFrequencyNV",
174 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
175 { "eglGetSystemTimeNV",
176 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700177
Mathias Agopian2bb71682013-03-27 17:32:41 -0700178 // EGL_KHR_wait_sync
179 { "eglWaitSyncKHR",
180 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700181
182 // EGL_ANDROID_presentation_time
183 { "eglPresentationTimeANDROID",
184 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800185
186 // EGL_KHR_swap_buffers_with_damage
187 { "eglSwapBuffersWithDamageKHR",
188 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
189
Craig Donner60761072017-01-27 12:30:44 -0800190 // EGL_ANDROID_create_native_client_buffer
Craig Donner05249fc2016-01-15 19:33:55 -0800191 { "eglCreateNativeClientBufferANDROID",
192 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
193
Craig Donner60761072017-01-27 12:30:44 -0800194 // EGL_ANDROID_get_native_client_buffer
195 { "eglGetNativeClientBufferANDROID",
196 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
197
Dan Stozaa894d082015-02-19 15:27:36 -0800198 // EGL_KHR_partial_update
199 { "eglSetDamageRegionKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700201
202 { "eglCreateStreamKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
204 { "eglDestroyStreamKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
206 { "eglStreamAttribKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
208 { "eglQueryStreamKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
210 { "eglQueryStreamu64KHR",
211 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
212 { "eglQueryStreamTimeKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
214 { "eglCreateStreamProducerSurfaceKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
216 { "eglStreamConsumerGLTextureExternalKHR",
217 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
218 { "eglStreamConsumerAcquireKHR",
219 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
220 { "eglStreamConsumerReleaseKHR",
221 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
222 { "eglGetStreamFileDescriptorKHR",
223 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
224 { "eglCreateStreamFromFileDescriptorKHR",
225 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700226
227 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800228 { "eglGetNextFrameIdANDROID",
229 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800230 { "eglGetCompositorTimingANDROID",
231 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
232 { "eglGetCompositorTimingSupportedANDROID",
233 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700234 { "eglGetFrameTimestampsANDROID",
235 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800236 { "eglGetFrameTimestampSupportedANDROID",
237 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700238};
239
Mathias Agopian737b8962017-02-24 14:32:05 -0800240#pragma clang diagnostic pop
241
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700242/*
243 * These extensions entry-points should not be exposed to applications.
244 * They're used internally by the Android EGL layer.
245 */
246#define FILTER_EXTENSIONS(procname) \
247 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
248 !strcmp((procname), "eglHibernateProcessIMG") || \
249 !strcmp((procname), "eglAwakenProcessIMG") || \
250 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
251
252
253
Mathias Agopian518ec112011-05-13 16:21:08 -0700254// accesses protected by sExtensionMapMutex
255static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
256static int sGLExtentionSlot = 0;
257static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
258
259static void(*findProcAddress(const char* name,
260 const extention_map_t* map, size_t n))() {
261 for (uint32_t i=0 ; i<n ; i++) {
262 if (!strcmp(name, map[i].name)) {
263 return map[i].address;
264 }
265 }
266 return NULL;
267}
268
269// ----------------------------------------------------------------------------
270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
272extern EGLBoolean egl_init_drivers();
273extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700274extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700275
Mathias Agopian518ec112011-05-13 16:21:08 -0700276} // namespace android;
277
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700278
Mathias Agopian518ec112011-05-13 16:21:08 -0700279// ----------------------------------------------------------------------------
280
281static inline void clearError() { egl_tls_t::clearError(); }
282static inline EGLContext getContext() { return egl_tls_t::getContext(); }
283
284// ----------------------------------------------------------------------------
285
286EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
287{
Jesse Hall1508ae62017-01-19 17:43:26 -0800288 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700289 clearError();
290
Dan Stozac3289c42014-01-17 11:38:34 -0800291 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700292 if (index >= NUM_DISPLAYS) {
293 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
294 }
295
296 if (egl_init_drivers() == EGL_FALSE) {
297 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
298 }
299
300 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
301 return dpy;
302}
303
304// ----------------------------------------------------------------------------
305// Initialization
306// ----------------------------------------------------------------------------
307
308EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
309{
310 clearError();
311
Jesse Hallb29e5e82012-04-04 16:53:42 -0700312 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800313 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700314
315 EGLBoolean res = dp->initialize(major, minor);
316
317 return res;
318}
319
320EGLBoolean eglTerminate(EGLDisplay dpy)
321{
322 // NOTE: don't unload the drivers b/c some APIs can be called
323 // after eglTerminate() has been called. eglTerminate() only
324 // terminates an EGLDisplay, not a EGL itself.
325
326 clearError();
327
Jesse Hallb29e5e82012-04-04 16:53:42 -0700328 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800329 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700330
331 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800332
Mathias Agopian518ec112011-05-13 16:21:08 -0700333 return res;
334}
335
336// ----------------------------------------------------------------------------
337// configuration
338// ----------------------------------------------------------------------------
339
340EGLBoolean eglGetConfigs( EGLDisplay dpy,
341 EGLConfig *configs,
342 EGLint config_size, EGLint *num_config)
343{
344 clearError();
345
Jesse Hallb29e5e82012-04-04 16:53:42 -0700346 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700347 if (!dp) return EGL_FALSE;
348
Mathias Agopian7773c432012-02-13 20:06:08 -0800349 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800350 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700351 }
352
Mathias Agopian7773c432012-02-13 20:06:08 -0800353 EGLBoolean res = EGL_FALSE;
354 *num_config = 0;
355
356 egl_connection_t* const cnx = &gEGLImpl;
357 if (cnx->dso) {
358 res = cnx->egl.eglGetConfigs(
359 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700360 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800361
362 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700363}
364
365EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
366 EGLConfig *configs, EGLint config_size,
367 EGLint *num_config)
368{
369 clearError();
370
Jesse Hallb29e5e82012-04-04 16:53:42 -0700371 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700372 if (!dp) return EGL_FALSE;
373
374 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800375 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700376 }
377
Mathias Agopian518ec112011-05-13 16:21:08 -0700378 EGLBoolean res = EGL_FALSE;
379 *num_config = 0;
380
Mathias Agopianada798b2012-02-13 17:09:30 -0800381 egl_connection_t* const cnx = &gEGLImpl;
382 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700383 if (attrib_list) {
384 char value[PROPERTY_VALUE_MAX];
385 property_get("debug.egl.force_msaa", value, "false");
386
387 if (!strcmp(value, "true")) {
388 size_t attribCount = 0;
389 EGLint attrib = attrib_list[0];
390
391 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700392 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700393 const EGLint *attribRendererable = NULL;
394 const EGLint *attribCaveat = NULL;
395
396 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700397 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700398 while (attrib != EGL_NONE) {
399 attrib = attrib_list[attribCount];
400 switch (attrib) {
401 case EGL_RENDERABLE_TYPE:
402 attribRendererable = &attrib_list[attribCount];
403 break;
404 case EGL_CONFIG_CAVEAT:
405 attribCaveat = &attrib_list[attribCount];
406 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800407 default:
408 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700409 }
410 attribCount++;
411 }
412
413 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
414 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800415
Romain Guy1cffc802012-10-15 18:13:05 -0700416 // Insert 2 extra attributes to force-enable MSAA 4x
417 EGLint aaAttribs[attribCount + 4];
418 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
419 aaAttribs[1] = 1;
420 aaAttribs[2] = EGL_SAMPLES;
421 aaAttribs[3] = 4;
422
423 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
424
425 EGLint numConfigAA;
426 EGLBoolean resAA = cnx->egl.eglChooseConfig(
427 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
428
429 if (resAA == EGL_TRUE && numConfigAA > 0) {
430 ALOGD("Enabling MSAA 4x");
431 *num_config = numConfigAA;
432 return resAA;
433 }
434 }
435 }
436 }
437
Mathias Agopian7773c432012-02-13 20:06:08 -0800438 res = cnx->egl.eglChooseConfig(
439 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700440 }
441 return res;
442}
443
444EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
445 EGLint attribute, EGLint *value)
446{
447 clearError();
448
Jesse Hallb29e5e82012-04-04 16:53:42 -0700449 egl_connection_t* cnx = NULL;
450 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
451 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800452
Mathias Agopian518ec112011-05-13 16:21:08 -0700453 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800454 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700455}
456
457// ----------------------------------------------------------------------------
458// surfaces
459// ----------------------------------------------------------------------------
460
Jesse Hallc2e41222013-08-08 13:40:22 -0700461// Turn linear formats into corresponding sRGB formats when colorspace is
462// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
463// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800464// the modification isn't possible, the original dataSpace is returned.
465static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
466 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700467 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800468 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700469 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800470 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700471 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800472 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700473}
474
Mathias Agopian518ec112011-05-13 16:21:08 -0700475EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
476 NativeWindowType window,
477 const EGLint *attrib_list)
478{
479 clearError();
480
Jesse Hallb29e5e82012-04-04 16:53:42 -0700481 egl_connection_t* cnx = NULL;
482 egl_display_ptr dp = validate_display_connection(dpy, cnx);
483 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800484 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700485
Andy McFaddend566ce32014-01-07 15:54:17 -0800486 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
487 if (result != OK) {
488 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
489 "failed (%#x) (already connected to another API?)",
490 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700491 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700492 }
493
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700494 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700495 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
496 // of our native format. So if sRGB gamma is requested, we have to
497 // modify the EGLconfig's format before setting the native window's
498 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800499
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700500 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
501 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
502 &componentType);
503
Mathias Agopian95921562017-02-24 14:31:31 -0800504 EGLint format;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800505 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700506 EGLint a = 0;
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700507 EGLint r, g, b;
508 r = g = b = 0;
509 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
510 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
511 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700512 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700513 EGLint colorDepth = r + g + b;
514
515 if (a == 0) {
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700516 if (colorDepth <= 16) {
517 format = HAL_PIXEL_FORMAT_RGB_565;
518 } else {
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700519 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
520 if (colorDepth > 24) {
521 format = HAL_PIXEL_FORMAT_RGBA_1010102;
522 } else {
523 format = HAL_PIXEL_FORMAT_RGBX_8888;
524 }
525 } else {
526 format = HAL_PIXEL_FORMAT_RGBA_FP16;
527 }
528 }
529 } else {
530 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
531 if (colorDepth > 24) {
532 format = HAL_PIXEL_FORMAT_RGBA_1010102;
533 } else {
534 format = HAL_PIXEL_FORMAT_RGBA_8888;
535 }
536 } else {
537 format = HAL_PIXEL_FORMAT_RGBA_FP16;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700538 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700539 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700540
541 // now select a corresponding sRGB format if needed
542 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
543 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
544 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530545 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700546 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700547 }
548 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800549
Jesse Hallc2e41222013-08-08 13:40:22 -0700550 if (format != 0) {
551 int err = native_window_set_buffers_format(window, format);
552 if (err != 0) {
553 ALOGE("error setting native window pixel format: %s (%d)",
554 strerror(-err), err);
555 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
556 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
557 }
558 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700559
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800560 if (dataSpace != 0) {
561 int err = native_window_set_buffers_data_space(window, dataSpace);
562 if (err != 0) {
563 ALOGE("error setting native window pixel dataSpace: %s (%d)",
564 strerror(-err), err);
565 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
566 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
567 }
568 }
569
Jamie Gennis59769462011-11-19 18:04:43 -0800570 // the EGL spec requires that a new EGLSurface default to swap interval
571 // 1, so explicitly set that on the window here.
572 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
573 anw->setSwapInterval(anw, 1);
574
Mathias Agopian518ec112011-05-13 16:21:08 -0700575 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800576 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700577 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700578 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
579 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 return s;
581 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700582
583 // EGLSurface creation failed
584 native_window_set_buffers_format(window, 0);
585 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700586 }
587 return EGL_NO_SURFACE;
588}
589
590EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
591 NativePixmapType pixmap,
592 const EGLint *attrib_list)
593{
594 clearError();
595
Jesse Hallb29e5e82012-04-04 16:53:42 -0700596 egl_connection_t* cnx = NULL;
597 egl_display_ptr dp = validate_display_connection(dpy, cnx);
598 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700599 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800600 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700601 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700602 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
603 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700604 return s;
605 }
606 }
607 return EGL_NO_SURFACE;
608}
609
610EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
611 const EGLint *attrib_list)
612{
613 clearError();
614
Jesse Hallb29e5e82012-04-04 16:53:42 -0700615 egl_connection_t* cnx = NULL;
616 egl_display_ptr dp = validate_display_connection(dpy, cnx);
617 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700618 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800619 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700620 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700621 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
622 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700623 return s;
624 }
625 }
626 return EGL_NO_SURFACE;
627}
Jesse Hall47743382013-02-08 11:13:46 -0800628
Mathias Agopian518ec112011-05-13 16:21:08 -0700629EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
630{
631 clearError();
632
Jesse Hallb29e5e82012-04-04 16:53:42 -0700633 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700634 if (!dp) return EGL_FALSE;
635
Jesse Hallb29e5e82012-04-04 16:53:42 -0700636 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700637 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800638 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700639
640 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800641 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700642 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700643 _s.terminate();
644 }
645 return result;
646}
647
648EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
649 EGLint attribute, EGLint *value)
650{
651 clearError();
652
Jesse Hallb29e5e82012-04-04 16:53:42 -0700653 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700654 if (!dp) return EGL_FALSE;
655
Jesse Hallb29e5e82012-04-04 16:53:42 -0700656 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700657 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800658 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700659
Mathias Agopian518ec112011-05-13 16:21:08 -0700660 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800661 return s->cnx->egl.eglQuerySurface(
662 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700663}
664
Jamie Gennise8696a42012-01-15 18:54:57 -0800665void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800666 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800667 clearError();
668
Jesse Hallb29e5e82012-04-04 16:53:42 -0700669 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800670 if (!dp) {
671 return;
672 }
673
Jesse Hallb29e5e82012-04-04 16:53:42 -0700674 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800675 if (!_s.get()) {
676 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800677 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800678}
679
Mathias Agopian518ec112011-05-13 16:21:08 -0700680// ----------------------------------------------------------------------------
681// Contexts
682// ----------------------------------------------------------------------------
683
684EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
685 EGLContext share_list, const EGLint *attrib_list)
686{
687 clearError();
688
Jesse Hallb29e5e82012-04-04 16:53:42 -0700689 egl_connection_t* cnx = NULL;
690 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700691 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700692 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700693 if (!ContextRef(dp.get(), share_list).get()) {
694 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
695 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700696 egl_context_t* const c = get_context(share_list);
697 share_list = c->context;
698 }
699 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800700 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700701 if (context != EGL_NO_CONTEXT) {
702 // figure out if it's a GLESv1 or GLESv2
703 int version = 0;
704 if (attrib_list) {
705 while (*attrib_list != EGL_NONE) {
706 GLint attr = *attrib_list++;
707 GLint value = *attrib_list++;
708 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
709 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800710 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800711 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800712 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 }
714 }
715 };
716 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700717 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
718 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700719 return c;
720 }
721 }
722 return EGL_NO_CONTEXT;
723}
724
725EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
726{
727 clearError();
728
Jesse Hallb29e5e82012-04-04 16:53:42 -0700729 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700730 if (!dp)
731 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700732
Jesse Hallb29e5e82012-04-04 16:53:42 -0700733 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700734 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800735 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800736
Mathias Agopian518ec112011-05-13 16:21:08 -0700737 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800738 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700739 if (result == EGL_TRUE) {
740 _c.terminate();
741 }
742 return result;
743}
744
Mathias Agopian518ec112011-05-13 16:21:08 -0700745EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
746 EGLSurface read, EGLContext ctx)
747{
748 clearError();
749
Jesse Hallb29e5e82012-04-04 16:53:42 -0700750 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800751 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700752
Mathias Agopian5b287a62011-05-16 18:58:55 -0700753 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
754 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
755 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
757 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800758 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700759 }
760
761 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700762 ContextRef _c(dp.get(), ctx);
763 SurfaceRef _d(dp.get(), draw);
764 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700765
766 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700767 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700768 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800769 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700770 }
771
772 // these are the underlying implementation's object
773 EGLContext impl_ctx = EGL_NO_CONTEXT;
774 EGLSurface impl_draw = EGL_NO_SURFACE;
775 EGLSurface impl_read = EGL_NO_SURFACE;
776
777 // these are our objects structs passed in
778 egl_context_t * c = NULL;
779 egl_surface_t const * d = NULL;
780 egl_surface_t const * r = NULL;
781
782 // these are the current objects structs
783 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800784
Mathias Agopian518ec112011-05-13 16:21:08 -0700785 if (ctx != EGL_NO_CONTEXT) {
786 c = get_context(ctx);
787 impl_ctx = c->context;
788 } else {
789 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700790 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
791 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800792 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700793 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700794 if (cur_c == NULL) {
795 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700796 // not an error, there is just no current context.
797 return EGL_TRUE;
798 }
799 }
800
801 // retrieve the underlying implementation's draw EGLSurface
802 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800803 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700804 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700805 impl_draw = d->surface;
806 }
807
808 // retrieve the underlying implementation's read EGLSurface
809 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800810 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700812 impl_read = r->surface;
813 }
814
Mathias Agopian518ec112011-05-13 16:21:08 -0700815
Jesse Hallb29e5e82012-04-04 16:53:42 -0700816 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800817 draw, read, ctx,
818 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700819
820 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800821 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700822 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
823 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700824 _c.acquire();
825 _r.acquire();
826 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700827 } else {
828 setGLHooksThreadSpecific(&gHooksNoContext);
829 egl_tls_t::setContext(EGL_NO_CONTEXT);
830 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700831 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000832 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700833 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -0800834 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700835 }
836 return result;
837}
838
839
840EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
841 EGLint attribute, EGLint *value)
842{
843 clearError();
844
Jesse Hallb29e5e82012-04-04 16:53:42 -0700845 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700846 if (!dp) return EGL_FALSE;
847
Jesse Hallb29e5e82012-04-04 16:53:42 -0700848 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -0800849 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700850
Mathias Agopian518ec112011-05-13 16:21:08 -0700851 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800852 return c->cnx->egl.eglQueryContext(
853 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700854
Mathias Agopian518ec112011-05-13 16:21:08 -0700855}
856
857EGLContext eglGetCurrentContext(void)
858{
859 // could be called before eglInitialize(), but we wouldn't have a context
860 // then, and this function would correctly return EGL_NO_CONTEXT.
861
862 clearError();
863
864 EGLContext ctx = getContext();
865 return ctx;
866}
867
868EGLSurface eglGetCurrentSurface(EGLint readdraw)
869{
870 // could be called before eglInitialize(), but we wouldn't have a context
871 // then, and this function would correctly return EGL_NO_SURFACE.
872
873 clearError();
874
875 EGLContext ctx = getContext();
876 if (ctx) {
877 egl_context_t const * const c = get_context(ctx);
878 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
879 switch (readdraw) {
880 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800881 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700882 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
883 }
884 }
885 return EGL_NO_SURFACE;
886}
887
888EGLDisplay eglGetCurrentDisplay(void)
889{
890 // could be called before eglInitialize(), but we wouldn't have a context
891 // then, and this function would correctly return EGL_NO_DISPLAY.
892
893 clearError();
894
895 EGLContext ctx = getContext();
896 if (ctx) {
897 egl_context_t const * const c = get_context(ctx);
898 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
899 return c->dpy;
900 }
901 return EGL_NO_DISPLAY;
902}
903
904EGLBoolean eglWaitGL(void)
905{
Mathias Agopian518ec112011-05-13 16:21:08 -0700906 clearError();
907
Mathias Agopianada798b2012-02-13 17:09:30 -0800908 egl_connection_t* const cnx = &gEGLImpl;
909 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800910 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800911
912 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700913}
914
915EGLBoolean eglWaitNative(EGLint engine)
916{
Mathias Agopian518ec112011-05-13 16:21:08 -0700917 clearError();
918
Mathias Agopianada798b2012-02-13 17:09:30 -0800919 egl_connection_t* const cnx = &gEGLImpl;
920 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -0800921 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -0800922
923 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700924}
925
926EGLint eglGetError(void)
927{
Mathias Agopianada798b2012-02-13 17:09:30 -0800928 EGLint err = EGL_SUCCESS;
929 egl_connection_t* const cnx = &gEGLImpl;
930 if (cnx->dso) {
931 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700932 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800933 if (err == EGL_SUCCESS) {
934 err = egl_tls_t::getError();
935 }
936 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700937}
938
Michael Chockc0ec5e22014-01-27 08:14:33 -0800939static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700940 const char* procname) {
941 const egl_connection_t* cnx = &gEGLImpl;
942 void* proc = NULL;
943
Michael Chockc0ec5e22014-01-27 08:14:33 -0800944 proc = dlsym(cnx->libEgl, procname);
945 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
946
Jesse Hallc07b5202013-07-04 12:08:16 -0700947 proc = dlsym(cnx->libGles2, procname);
948 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
949
950 proc = dlsym(cnx->libGles1, procname);
951 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
952
953 return NULL;
954}
955
Mathias Agopian518ec112011-05-13 16:21:08 -0700956__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
957{
958 // eglGetProcAddress() could be the very first function called
959 // in which case we must make sure we've initialized ourselves, this
960 // happens the first time egl_get_display() is called.
961
962 clearError();
963
964 if (egl_init_drivers() == EGL_FALSE) {
965 setError(EGL_BAD_PARAMETER, NULL);
966 return NULL;
967 }
968
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700969 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700970 return NULL;
971 }
972
Mathias Agopian518ec112011-05-13 16:21:08 -0700973 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700974 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700975 if (addr) return addr;
976
Michael Chockc0ec5e22014-01-27 08:14:33 -0800977 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700978 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700979
Mathias Agopian518ec112011-05-13 16:21:08 -0700980 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
981 pthread_mutex_lock(&sExtensionMapMutex);
982
983 /*
984 * Since eglGetProcAddress() is not associated to anything, it needs
985 * to return a function pointer that "works" regardless of what
986 * the current context is.
987 *
988 * For this reason, we return a "forwarder", a small stub that takes
989 * care of calling the function associated with the context
990 * currently bound.
991 *
992 * We first look for extensions we've already resolved, if we're seeing
993 * this extension for the first time, we go through all our
994 * implementations and call eglGetProcAddress() and record the
995 * result in the appropriate implementation hooks and return the
996 * address of the forwarder corresponding to that hook set.
997 *
998 */
999
1000 const String8 name(procname);
1001 addr = sGLExtentionMap.valueFor(name);
1002 const int slot = sGLExtentionSlot;
1003
Steve Blocke6f43dd2012-01-06 19:20:56 +00001004 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001005 "no more slots for eglGetProcAddress(\"%s\")",
1006 procname);
1007
1008 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1009 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001010
1011 egl_connection_t* const cnx = &gEGLImpl;
1012 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001013 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001014 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001015 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1016 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001017 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001018 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001019 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001020
Mathias Agopian518ec112011-05-13 16:21:08 -07001021 if (found) {
1022 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -07001023 sGLExtentionMap.add(name, addr);
1024 sGLExtentionSlot++;
1025 }
1026 }
1027
1028 pthread_mutex_unlock(&sExtensionMapMutex);
1029 return addr;
1030}
1031
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001032class FrameCompletionThread : public Thread {
1033public:
1034
1035 static void queueSync(EGLSyncKHR sync) {
1036 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1037 static bool running = false;
1038 if (!running) {
1039 thread->run("GPUFrameCompletion");
1040 running = true;
1041 }
1042 {
1043 Mutex::Autolock lock(thread->mMutex);
1044 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1045 thread->mFramesQueued).string());
1046 thread->mQueue.push_back(sync);
1047 thread->mCondition.signal();
1048 thread->mFramesQueued++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001049 ATRACE_INT("GPU Frames Outstanding", int32_t(thread->mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001050 }
1051 }
1052
1053private:
1054 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1055
1056 virtual bool threadLoop() {
1057 EGLSyncKHR sync;
1058 uint32_t frameNum;
1059 {
1060 Mutex::Autolock lock(mMutex);
1061 while (mQueue.isEmpty()) {
1062 mCondition.wait(mMutex);
1063 }
1064 sync = mQueue[0];
1065 frameNum = mFramesCompleted;
1066 }
1067 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1068 {
1069 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1070 frameNum).string());
1071 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1072 if (result == EGL_FALSE) {
1073 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1074 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1075 ALOGE("FrameCompletion: timeout waiting for fence");
1076 }
1077 eglDestroySyncKHR(dpy, sync);
1078 }
1079 {
1080 Mutex::Autolock lock(mMutex);
1081 mQueue.removeAt(0);
1082 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001083 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001084 }
1085 return true;
1086 }
1087
1088 uint32_t mFramesQueued;
1089 uint32_t mFramesCompleted;
1090 Vector<EGLSyncKHR> mQueue;
1091 Condition mCondition;
1092 Mutex mMutex;
1093};
1094
Dan Stozaa894d082015-02-19 15:27:36 -08001095EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1096 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001097{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001098 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001099 clearError();
1100
Jesse Hallb29e5e82012-04-04 16:53:42 -07001101 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001102 if (!dp) return EGL_FALSE;
1103
Jesse Hallb29e5e82012-04-04 16:53:42 -07001104 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001105 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001106 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001107
Mathias Agopian518ec112011-05-13 16:21:08 -07001108 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001109
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001110 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1111 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1112 if (sync != EGL_NO_SYNC_KHR) {
1113 FrameCompletionThread::queueSync(sync);
1114 }
1115 }
1116
Mathias Agopian7db993a2012-03-25 00:49:46 -07001117 if (CC_UNLIKELY(dp->finishOnSwap)) {
1118 uint32_t pixel;
1119 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1120 if (c) {
1121 // glReadPixels() ensures that the frame is complete
1122 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1123 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1124 }
1125 }
1126
Dan Stozaa894d082015-02-19 15:27:36 -08001127 if (n_rects == 0) {
1128 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1129 }
1130
1131 Vector<android_native_rect_t> androidRects;
1132 for (int r = 0; r < n_rects; ++r) {
1133 int offset = r * 4;
1134 int x = rects[offset];
1135 int y = rects[offset + 1];
1136 int width = rects[offset + 2];
1137 int height = rects[offset + 3];
1138 android_native_rect_t androidRect;
1139 androidRect.left = x;
1140 androidRect.top = y + height;
1141 androidRect.right = x + width;
1142 androidRect.bottom = y;
1143 androidRects.push_back(androidRect);
1144 }
1145 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1146 androidRects.size());
1147
1148 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1149 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1150 rects, n_rects);
1151 } else {
1152 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1153 }
1154}
1155
1156EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1157{
1158 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001159}
1160
1161EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1162 NativePixmapType target)
1163{
1164 clearError();
1165
Jesse Hallb29e5e82012-04-04 16:53:42 -07001166 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001167 if (!dp) return EGL_FALSE;
1168
Jesse Hallb29e5e82012-04-04 16:53:42 -07001169 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001170 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001171 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001172
Mathias Agopian518ec112011-05-13 16:21:08 -07001173 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001174 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001175}
1176
1177const char* eglQueryString(EGLDisplay dpy, EGLint name)
1178{
1179 clearError();
1180
Chia-I Wue57d1352016-08-15 16:10:02 +08001181 // Generate an error quietly when client extensions (as defined by
1182 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1183 // validate_display to generate the error as validate_display would log
1184 // the error, which can be misleading.
1185 //
1186 // If we want to support EGL_EXT_client_extensions later, we can return
1187 // the client extension string here instead.
1188 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001189 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001190
Jesse Hallb29e5e82012-04-04 16:53:42 -07001191 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001192 if (!dp) return (const char *) NULL;
1193
1194 switch (name) {
1195 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001196 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001197 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001198 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001199 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001200 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001201 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001202 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001203 default:
1204 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001205 }
1206 return setError(EGL_BAD_PARAMETER, (const char *)0);
1207}
1208
Mathias Agopianca088332013-03-28 17:44:13 -07001209EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1210{
1211 clearError();
1212
1213 const egl_display_ptr dp = validate_display(dpy);
1214 if (!dp) return (const char *) NULL;
1215
1216 switch (name) {
1217 case EGL_VENDOR:
1218 return dp->disp.queryString.vendor;
1219 case EGL_VERSION:
1220 return dp->disp.queryString.version;
1221 case EGL_EXTENSIONS:
1222 return dp->disp.queryString.extensions;
1223 case EGL_CLIENT_APIS:
1224 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001225 default:
1226 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001227 }
1228 return setError(EGL_BAD_PARAMETER, (const char *)0);
1229}
Mathias Agopian518ec112011-05-13 16:21:08 -07001230
1231// ----------------------------------------------------------------------------
1232// EGL 1.1
1233// ----------------------------------------------------------------------------
1234
1235EGLBoolean eglSurfaceAttrib(
1236 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1237{
1238 clearError();
1239
Jesse Hallb29e5e82012-04-04 16:53:42 -07001240 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001241 if (!dp) return EGL_FALSE;
1242
Jesse Hallb29e5e82012-04-04 16:53:42 -07001243 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001244 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001245 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001246
Pablo Ceballosc18be292016-05-31 14:55:42 -07001247 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001248
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001249 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001250 if (!s->win.get()) {
1251 setError(EGL_BAD_SURFACE, EGL_FALSE);
1252 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001253 int err = native_window_set_auto_refresh(s->win.get(), value ? true : false);
1254 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001255 }
1256
Pablo Ceballosc18be292016-05-31 14:55:42 -07001257 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001258 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001259 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001260 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001261 int err = native_window_enable_frame_timestamps(s->win.get(), value ? true : false);
1262 return (err == NO_ERROR) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001263 }
1264
Mathias Agopian518ec112011-05-13 16:21:08 -07001265 if (s->cnx->egl.eglSurfaceAttrib) {
1266 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001267 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001268 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001269 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001270}
1271
1272EGLBoolean eglBindTexImage(
1273 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1274{
1275 clearError();
1276
Jesse Hallb29e5e82012-04-04 16:53:42 -07001277 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 if (!dp) return EGL_FALSE;
1279
Jesse Hallb29e5e82012-04-04 16:53:42 -07001280 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001281 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001282 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001283
Mathias Agopian518ec112011-05-13 16:21:08 -07001284 egl_surface_t const * const s = get_surface(surface);
1285 if (s->cnx->egl.eglBindTexImage) {
1286 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001287 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001288 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001289 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001290}
1291
1292EGLBoolean eglReleaseTexImage(
1293 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1294{
1295 clearError();
1296
Jesse Hallb29e5e82012-04-04 16:53:42 -07001297 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001298 if (!dp) return EGL_FALSE;
1299
Jesse Hallb29e5e82012-04-04 16:53:42 -07001300 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001301 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001302 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001303
Mathias Agopian518ec112011-05-13 16:21:08 -07001304 egl_surface_t const * const s = get_surface(surface);
1305 if (s->cnx->egl.eglReleaseTexImage) {
1306 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001307 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001308 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001309 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001310}
1311
1312EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1313{
1314 clearError();
1315
Jesse Hallb29e5e82012-04-04 16:53:42 -07001316 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001317 if (!dp) return EGL_FALSE;
1318
1319 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001320 egl_connection_t* const cnx = &gEGLImpl;
1321 if (cnx->dso && cnx->egl.eglSwapInterval) {
1322 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001323 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001324
Mathias Agopian518ec112011-05-13 16:21:08 -07001325 return res;
1326}
1327
1328
1329// ----------------------------------------------------------------------------
1330// EGL 1.2
1331// ----------------------------------------------------------------------------
1332
1333EGLBoolean eglWaitClient(void)
1334{
1335 clearError();
1336
Mathias Agopianada798b2012-02-13 17:09:30 -08001337 egl_connection_t* const cnx = &gEGLImpl;
1338 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001339 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001340
1341 EGLBoolean res;
1342 if (cnx->egl.eglWaitClient) {
1343 res = cnx->egl.eglWaitClient();
1344 } else {
1345 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001346 }
1347 return res;
1348}
1349
1350EGLBoolean eglBindAPI(EGLenum api)
1351{
1352 clearError();
1353
1354 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001355 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001356 }
1357
1358 // bind this API on all EGLs
1359 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001360 egl_connection_t* const cnx = &gEGLImpl;
1361 if (cnx->dso && cnx->egl.eglBindAPI) {
1362 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001363 }
1364 return res;
1365}
1366
1367EGLenum eglQueryAPI(void)
1368{
1369 clearError();
1370
1371 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001372 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001373 }
1374
Mathias Agopianada798b2012-02-13 17:09:30 -08001375 egl_connection_t* const cnx = &gEGLImpl;
1376 if (cnx->dso && cnx->egl.eglQueryAPI) {
1377 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001378 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001379
Mathias Agopian518ec112011-05-13 16:21:08 -07001380 // or, it can only be OpenGL ES
1381 return EGL_OPENGL_ES_API;
1382}
1383
1384EGLBoolean eglReleaseThread(void)
1385{
1386 clearError();
1387
Mathias Agopianada798b2012-02-13 17:09:30 -08001388 egl_connection_t* const cnx = &gEGLImpl;
1389 if (cnx->dso && cnx->egl.eglReleaseThread) {
1390 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001391 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301392
1393 // If there is context bound to the thread, release it
1394 egl_display_t::loseCurrent(get_context(getContext()));
1395
Mathias Agopian518ec112011-05-13 16:21:08 -07001396 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001397 return EGL_TRUE;
1398}
1399
1400EGLSurface eglCreatePbufferFromClientBuffer(
1401 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1402 EGLConfig config, const EGLint *attrib_list)
1403{
1404 clearError();
1405
Jesse Hallb29e5e82012-04-04 16:53:42 -07001406 egl_connection_t* cnx = NULL;
1407 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1408 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001409 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1410 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001411 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001412 }
1413 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1414}
1415
1416// ----------------------------------------------------------------------------
1417// EGL_EGLEXT_VERSION 3
1418// ----------------------------------------------------------------------------
1419
1420EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1421 const EGLint *attrib_list)
1422{
1423 clearError();
1424
Jesse Hallb29e5e82012-04-04 16:53:42 -07001425 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001426 if (!dp) return EGL_FALSE;
1427
Jesse Hallb29e5e82012-04-04 16:53:42 -07001428 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001429 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001430 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001431
1432 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001433 if (s->cnx->egl.eglLockSurfaceKHR) {
1434 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001435 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001436 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001437 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001438}
1439
1440EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1441{
1442 clearError();
1443
Jesse Hallb29e5e82012-04-04 16:53:42 -07001444 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001445 if (!dp) return EGL_FALSE;
1446
Jesse Hallb29e5e82012-04-04 16:53:42 -07001447 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001448 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001449 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450
1451 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001452 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001453 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001454 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001455 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001456}
1457
1458EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1459 EGLClientBuffer buffer, const EGLint *attrib_list)
1460{
1461 clearError();
1462
Jesse Hallb29e5e82012-04-04 16:53:42 -07001463 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001464 if (!dp) return EGL_NO_IMAGE_KHR;
1465
Jesse Hallb29e5e82012-04-04 16:53:42 -07001466 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001467 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001468
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001469 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1470 egl_connection_t* const cnx = &gEGLImpl;
1471 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1472 result = cnx->egl.eglCreateImageKHR(
1473 dp->disp.dpy,
1474 c ? c->context : EGL_NO_CONTEXT,
1475 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001476 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001477 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001478}
1479
1480EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1481{
1482 clearError();
1483
Jesse Hallb29e5e82012-04-04 16:53:42 -07001484 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001485 if (!dp) return EGL_FALSE;
1486
Steven Holte646a5c52012-06-04 20:02:11 -07001487 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001488 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001489 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001490 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001491 }
Steven Holte646a5c52012-06-04 20:02:11 -07001492 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001493}
1494
1495// ----------------------------------------------------------------------------
1496// EGL_EGLEXT_VERSION 5
1497// ----------------------------------------------------------------------------
1498
1499
1500EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1501{
1502 clearError();
1503
Jesse Hallb29e5e82012-04-04 16:53:42 -07001504 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505 if (!dp) return EGL_NO_SYNC_KHR;
1506
Mathias Agopian518ec112011-05-13 16:21:08 -07001507 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001508 egl_connection_t* const cnx = &gEGLImpl;
1509 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1510 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001511 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001512 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001513}
1514
1515EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1516{
1517 clearError();
1518
Jesse Hallb29e5e82012-04-04 16:53:42 -07001519 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001520 if (!dp) return EGL_FALSE;
1521
Mathias Agopian518ec112011-05-13 16:21:08 -07001522 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001523 egl_connection_t* const cnx = &gEGLImpl;
1524 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1525 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001526 }
1527 return result;
1528}
1529
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001530EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1531 clearError();
1532
1533 const egl_display_ptr dp = validate_display(dpy);
1534 if (!dp) return EGL_FALSE;
1535
1536 EGLBoolean result = EGL_FALSE;
1537 egl_connection_t* const cnx = &gEGLImpl;
1538 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1539 result = cnx->egl.eglSignalSyncKHR(
1540 dp->disp.dpy, sync, mode);
1541 }
1542 return result;
1543}
1544
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001545EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1546 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001547{
1548 clearError();
1549
Jesse Hallb29e5e82012-04-04 16:53:42 -07001550 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001551 if (!dp) return EGL_FALSE;
1552
Mathias Agopian737b8962017-02-24 14:32:05 -08001553 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001554 egl_connection_t* const cnx = &gEGLImpl;
1555 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1556 result = cnx->egl.eglClientWaitSyncKHR(
1557 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001558 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001559 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001560}
1561
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001562EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1563 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001564{
1565 clearError();
1566
Jesse Hallb29e5e82012-04-04 16:53:42 -07001567 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001568 if (!dp) return EGL_FALSE;
1569
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001570 EGLBoolean result = EGL_FALSE;
1571 egl_connection_t* const cnx = &gEGLImpl;
1572 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1573 result = cnx->egl.eglGetSyncAttribKHR(
1574 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001575 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001576 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001577}
1578
Season Li000d88f2015-07-01 11:39:40 -07001579EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1580{
1581 clearError();
1582
1583 const egl_display_ptr dp = validate_display(dpy);
1584 if (!dp) return EGL_NO_STREAM_KHR;
1585
1586 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1587 egl_connection_t* const cnx = &gEGLImpl;
1588 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1589 result = cnx->egl.eglCreateStreamKHR(
1590 dp->disp.dpy, attrib_list);
1591 }
1592 return result;
1593}
1594
1595EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1596{
1597 clearError();
1598
1599 const egl_display_ptr dp = validate_display(dpy);
1600 if (!dp) return EGL_FALSE;
1601
1602 EGLBoolean result = EGL_FALSE;
1603 egl_connection_t* const cnx = &gEGLImpl;
1604 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1605 result = cnx->egl.eglDestroyStreamKHR(
1606 dp->disp.dpy, stream);
1607 }
1608 return result;
1609}
1610
1611EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1612 EGLenum attribute, EGLint value)
1613{
1614 clearError();
1615
1616 const egl_display_ptr dp = validate_display(dpy);
1617 if (!dp) return EGL_FALSE;
1618
1619 EGLBoolean result = EGL_FALSE;
1620 egl_connection_t* const cnx = &gEGLImpl;
1621 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1622 result = cnx->egl.eglStreamAttribKHR(
1623 dp->disp.dpy, stream, attribute, value);
1624 }
1625 return result;
1626}
1627
1628EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1629 EGLenum attribute, EGLint *value)
1630{
1631 clearError();
1632
1633 const egl_display_ptr dp = validate_display(dpy);
1634 if (!dp) return EGL_FALSE;
1635
1636 EGLBoolean result = EGL_FALSE;
1637 egl_connection_t* const cnx = &gEGLImpl;
1638 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1639 result = cnx->egl.eglQueryStreamKHR(
1640 dp->disp.dpy, stream, attribute, value);
1641 }
1642 return result;
1643}
1644
1645EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1646 EGLenum attribute, EGLuint64KHR *value)
1647{
1648 clearError();
1649
1650 const egl_display_ptr dp = validate_display(dpy);
1651 if (!dp) return EGL_FALSE;
1652
1653 EGLBoolean result = EGL_FALSE;
1654 egl_connection_t* const cnx = &gEGLImpl;
1655 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1656 result = cnx->egl.eglQueryStreamu64KHR(
1657 dp->disp.dpy, stream, attribute, value);
1658 }
1659 return result;
1660}
1661
1662EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1663 EGLenum attribute, EGLTimeKHR *value)
1664{
1665 clearError();
1666
1667 const egl_display_ptr dp = validate_display(dpy);
1668 if (!dp) return EGL_FALSE;
1669
1670 EGLBoolean result = EGL_FALSE;
1671 egl_connection_t* const cnx = &gEGLImpl;
1672 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1673 result = cnx->egl.eglQueryStreamTimeKHR(
1674 dp->disp.dpy, stream, attribute, value);
1675 }
1676 return result;
1677}
1678
1679EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1680 EGLStreamKHR stream, const EGLint *attrib_list)
1681{
1682 clearError();
1683
1684 egl_display_ptr dp = validate_display(dpy);
1685 if (!dp) return EGL_NO_SURFACE;
1686
1687 egl_connection_t* const cnx = &gEGLImpl;
1688 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1689 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1690 dp->disp.dpy, config, stream, attrib_list);
1691 if (surface != EGL_NO_SURFACE) {
1692 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1693 surface, cnx);
1694 return s;
1695 }
1696 }
1697 return EGL_NO_SURFACE;
1698}
1699
1700EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1701 EGLStreamKHR stream)
1702{
1703 clearError();
1704
1705 const egl_display_ptr dp = validate_display(dpy);
1706 if (!dp) return EGL_FALSE;
1707
1708 EGLBoolean result = EGL_FALSE;
1709 egl_connection_t* const cnx = &gEGLImpl;
1710 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1711 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1712 dp->disp.dpy, stream);
1713 }
1714 return result;
1715}
1716
1717EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1718 EGLStreamKHR stream)
1719{
1720 clearError();
1721
1722 const egl_display_ptr dp = validate_display(dpy);
1723 if (!dp) return EGL_FALSE;
1724
1725 EGLBoolean result = EGL_FALSE;
1726 egl_connection_t* const cnx = &gEGLImpl;
1727 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1728 result = cnx->egl.eglStreamConsumerAcquireKHR(
1729 dp->disp.dpy, stream);
1730 }
1731 return result;
1732}
1733
1734EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1735 EGLStreamKHR stream)
1736{
1737 clearError();
1738
1739 const egl_display_ptr dp = validate_display(dpy);
1740 if (!dp) return EGL_FALSE;
1741
1742 EGLBoolean result = EGL_FALSE;
1743 egl_connection_t* const cnx = &gEGLImpl;
1744 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1745 result = cnx->egl.eglStreamConsumerReleaseKHR(
1746 dp->disp.dpy, stream);
1747 }
1748 return result;
1749}
1750
1751EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1752 EGLDisplay dpy, EGLStreamKHR stream)
1753{
1754 clearError();
1755
1756 const egl_display_ptr dp = validate_display(dpy);
1757 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1758
1759 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1760 egl_connection_t* const cnx = &gEGLImpl;
1761 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1762 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1763 dp->disp.dpy, stream);
1764 }
1765 return result;
1766}
1767
1768EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1769 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1770{
1771 clearError();
1772
1773 const egl_display_ptr dp = validate_display(dpy);
1774 if (!dp) return EGL_NO_STREAM_KHR;
1775
1776 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1777 egl_connection_t* const cnx = &gEGLImpl;
1778 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1779 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1780 dp->disp.dpy, file_descriptor);
1781 }
1782 return result;
1783}
1784
Mathias Agopian518ec112011-05-13 16:21:08 -07001785// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001786// EGL_EGLEXT_VERSION 15
1787// ----------------------------------------------------------------------------
1788
1789EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1790 clearError();
1791 const egl_display_ptr dp = validate_display(dpy);
1792 if (!dp) return EGL_FALSE;
1793 EGLint result = EGL_FALSE;
1794 egl_connection_t* const cnx = &gEGLImpl;
1795 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1796 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1797 }
1798 return result;
1799}
1800
1801// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001802// ANDROID extensions
1803// ----------------------------------------------------------------------------
1804
Jamie Gennis331841b2012-09-06 14:52:00 -07001805EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1806{
1807 clearError();
1808
1809 const egl_display_ptr dp = validate_display(dpy);
1810 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1811
1812 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1813 egl_connection_t* const cnx = &gEGLImpl;
1814 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1815 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1816 }
1817 return result;
1818}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001819
Andy McFadden72841452013-03-01 16:25:32 -08001820EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1821 EGLnsecsANDROID time)
1822{
1823 clearError();
1824
1825 const egl_display_ptr dp = validate_display(dpy);
1826 if (!dp) {
1827 return EGL_FALSE;
1828 }
1829
1830 SurfaceRef _s(dp.get(), surface);
1831 if (!_s.get()) {
1832 setError(EGL_BAD_SURFACE, EGL_FALSE);
1833 return EGL_FALSE;
1834 }
1835
1836 egl_surface_t const * const s = get_surface(surface);
1837 native_window_set_buffers_timestamp(s->win.get(), time);
1838
1839 return EGL_TRUE;
1840}
1841
Craig Donner05249fc2016-01-15 19:33:55 -08001842EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1843{
1844 clearError();
1845
Craig Donnere96a3252017-02-02 12:13:34 -08001846 uint64_t producerUsage = 0;
1847 uint64_t consumerUsage = 0;
Craig Donner05249fc2016-01-15 19:33:55 -08001848 uint32_t width = 0;
1849 uint32_t height = 0;
1850 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001851 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001852 uint32_t red_size = 0;
1853 uint32_t green_size = 0;
1854 uint32_t blue_size = 0;
1855 uint32_t alpha_size = 0;
1856
Craig Donnerb40504a2016-06-03 17:54:25 -07001857#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001858 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001859 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001860 target = value; \
1861 } else { \
1862 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1863 } \
1864 break
1865
1866 if (attrib_list) {
1867 while (*attrib_list != EGL_NONE) {
1868 GLint attr = *attrib_list++;
1869 GLint value = *attrib_list++;
1870 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001871 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1872 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1873 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1874 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1875 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1876 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001877 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001878 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1879 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001880 producerUsage |= GRALLOC1_PRODUCER_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001881 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001882 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001883 producerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET;
Craig Donner05249fc2016-01-15 19:33:55 -08001884 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001885 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donnere96a3252017-02-02 12:13:34 -08001886 consumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE;
Craig Donner05249fc2016-01-15 19:33:55 -08001887 }
Craig Donner05249fc2016-01-15 19:33:55 -08001888 break;
1889 default:
1890 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1891 }
1892 }
1893 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001894#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001895
1896 // Validate format.
1897 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1898 if (alpha_size == 8) {
1899 format = HAL_PIXEL_FORMAT_RGBA_8888;
1900 } else {
1901 format = HAL_PIXEL_FORMAT_RGB_888;
1902 }
1903 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1904 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001905 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001906 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001907 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001908 red_size, green_size, blue_size, alpha_size);
1909 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1910 }
1911
Craig Donner68671532016-07-18 10:19:54 -07001912#define CHECK_ERROR_CONDITION(message) \
1913 if (err != NO_ERROR) { \
1914 ALOGE(message); \
1915 goto error_condition; \
1916 }
1917
1918 // The holder is used to destroy the buffer if an error occurs.
1919 GraphicBuffer* gBuffer = new GraphicBuffer();
1920 sp<IServiceManager> sm = defaultServiceManager();
1921 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1922 sp<IBinder> allocator;
1923 Parcel sc_data, sc_reply, data, reply;
1924 status_t err = NO_ERROR;
1925 if (sm == NULL) {
1926 ALOGE("Unable to connect to ServiceManager");
1927 goto error_condition;
1928 }
1929
1930 // Obtain an allocator.
1931 if (surfaceFlinger == NULL) {
1932 ALOGE("Unable to connect to SurfaceFlinger");
1933 goto error_condition;
1934 }
1935 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1936 err = surfaceFlinger->transact(
1937 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1938 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1939 allocator = sc_reply.readStrongBinder();
1940
1941 if (allocator == NULL) {
1942 ALOGE("Unable to obtain an ISurfaceComposer");
1943 goto error_condition;
1944 }
1945 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1946 err = data.writeUint32(width);
1947 CHECK_ERROR_CONDITION("Unable to write width");
1948 err = data.writeUint32(height);
1949 CHECK_ERROR_CONDITION("Unable to write height");
1950 err = data.writeInt32(static_cast<int32_t>(format));
1951 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001952 err = data.writeUint32(layer_count);
1953 CHECK_ERROR_CONDITION("Unable to write layer count");
Craig Donnere96a3252017-02-02 12:13:34 -08001954 err = data.writeUint64(producerUsage);
1955 CHECK_ERROR_CONDITION("Unable to write producer usage");
1956 err = data.writeUint64(consumerUsage);
1957 CHECK_ERROR_CONDITION("Unable to write consumer usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001958 err = data.writeUtf8AsUtf16(
1959 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1960 std::to_string(getpid()) + ']');
1961 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001962 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1963 &reply);
1964 CHECK_ERROR_CONDITION(
1965 "Unable to request buffer allocation from surface composer");
1966 err = reply.readInt32();
1967 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1968 err = reply.read(*gBuffer);
1969 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1970
1971 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001972 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001973 ALOGE("Unable to create native buffer "
Craig Donnere96a3252017-02-02 12:13:34 -08001974 "{ w=%u, h=%u, f=%u, pu=%" PRIx64 " cu=%" PRIx64 ", lc=%u} %#x",
1975 width, height, format, producerUsage, consumerUsage,
1976 layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001977 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001978 }
Craig Donnere96a3252017-02-02 12:13:34 -08001979 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, pu=%" PRIx64
1980 " cu=%" PRIx64 ", lc=%u}",
1981 gBuffer, width, height, format, producerUsage, consumerUsage,
1982 layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001983 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001984
1985#undef CHECK_ERROR_CONDITION
1986
1987error_condition:
1988 // Delete the buffer.
1989 sp<GraphicBuffer> holder(gBuffer);
1990 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001991}
1992
Craig Donner60761072017-01-27 12:30:44 -08001993EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1994 clearError();
1995
1996 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1997
Mathias Agopian89ed4c82017-02-09 18:48:34 -08001998 const GraphicBuffer* graphicBuffer = AHardwareBuffer_to_GraphicBuffer(buffer);
Craig Donner60761072017-01-27 12:30:44 -08001999 return static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
2000}
2001
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002002// ----------------------------------------------------------------------------
2003// NVIDIA extensions
2004// ----------------------------------------------------------------------------
2005EGLuint64NV eglGetSystemTimeFrequencyNV()
2006{
2007 clearError();
2008
2009 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002010 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002011 }
2012
2013 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002014 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002015
Mathias Agopianada798b2012-02-13 17:09:30 -08002016 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2017 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002018 }
2019
Mathias Agopian737b8962017-02-24 14:32:05 -08002020 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002021}
2022
2023EGLuint64NV eglGetSystemTimeNV()
2024{
2025 clearError();
2026
2027 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002028 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002029 }
2030
2031 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002032 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002033
Mathias Agopianada798b2012-02-13 17:09:30 -08002034 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2035 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002036 }
2037
Mathias Agopian737b8962017-02-24 14:32:05 -08002038 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002039}
Dan Stozaa894d082015-02-19 15:27:36 -08002040
2041// ----------------------------------------------------------------------------
2042// Partial update extension
2043// ----------------------------------------------------------------------------
2044EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2045 EGLint *rects, EGLint n_rects)
2046{
2047 clearError();
2048
2049 const egl_display_ptr dp = validate_display(dpy);
2050 if (!dp) {
2051 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2052 return EGL_FALSE;
2053 }
2054
2055 SurfaceRef _s(dp.get(), surface);
2056 if (!_s.get()) {
2057 setError(EGL_BAD_SURFACE, EGL_FALSE);
2058 return EGL_FALSE;
2059 }
2060
2061 egl_surface_t const * const s = get_surface(surface);
2062 if (s->cnx->egl.eglSetDamageRegionKHR) {
2063 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2064 rects, n_rects);
2065 }
2066
2067 return EGL_FALSE;
2068}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002069
Brian Anderson1049d1d2016-12-16 17:25:57 -08002070EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2071 EGLuint64KHR *frameId) {
2072 clearError();
2073
2074 const egl_display_ptr dp = validate_display(dpy);
2075 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002076 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002077 }
2078
2079 SurfaceRef _s(dp.get(), surface);
2080 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002081 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002082 }
2083
2084 egl_surface_t const * const s = get_surface(surface);
2085
2086 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002087 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002088 }
2089
2090 uint64_t nextFrameId = 0;
2091 status_t ret = native_window_get_next_frame_id(s->win.get(), &nextFrameId);
2092
2093 if (ret != NO_ERROR) {
2094 // This should not happen. Return an error that is not in the spec
2095 // so it's obvious something is very wrong.
2096 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002097 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002098 }
2099
2100 *frameId = nextFrameId;
2101 return EGL_TRUE;
2102}
2103
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002104EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2105 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2106{
2107 clearError();
2108
2109 const egl_display_ptr dp = validate_display(dpy);
2110 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002111 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002112 }
2113
2114 SurfaceRef _s(dp.get(), surface);
2115 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002116 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002117 }
2118
2119 egl_surface_t const * const s = get_surface(surface);
2120
2121 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002122 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002123 }
2124
2125 nsecs_t* compositeDeadline = nullptr;
2126 nsecs_t* compositeInterval = nullptr;
2127 nsecs_t* compositeToPresentLatency = nullptr;
2128
2129 for (int i = 0; i < numTimestamps; i++) {
2130 switch (names[i]) {
2131 case EGL_COMPOSITE_DEADLINE_ANDROID:
2132 compositeDeadline = &values[i];
2133 break;
2134 case EGL_COMPOSITE_INTERVAL_ANDROID:
2135 compositeInterval = &values[i];
2136 break;
2137 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2138 compositeToPresentLatency = &values[i];
2139 break;
2140 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002141 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002142 }
2143 }
2144
2145 status_t ret = native_window_get_compositor_timing(s->win.get(),
2146 compositeDeadline, compositeInterval, compositeToPresentLatency);
2147
2148 switch (ret) {
2149 case NO_ERROR:
2150 return EGL_TRUE;
2151 case INVALID_OPERATION:
Mathias Agopian737b8962017-02-24 14:32:05 -08002152 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002153 default:
2154 // This should not happen. Return an error that is not in the spec
2155 // so it's obvious something is very wrong.
2156 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002157 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002158 }
2159}
2160
2161EGLBoolean eglGetCompositorTimingSupportedANDROID(
2162 EGLDisplay dpy, EGLSurface surface, EGLint name)
2163{
2164 clearError();
2165
2166 const egl_display_ptr dp = validate_display(dpy);
2167 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002168 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002169 }
2170
2171 SurfaceRef _s(dp.get(), surface);
2172 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002173 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002174 }
2175
2176 egl_surface_t const * const s = get_surface(surface);
2177
2178 ANativeWindow* window = s->win.get();
2179 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002180 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002181 }
2182
2183 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002184 case EGL_COMPOSITE_DEADLINE_ANDROID:
2185 case EGL_COMPOSITE_INTERVAL_ANDROID:
2186 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2187 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002188 default:
2189 return EGL_FALSE;
2190 }
2191}
2192
Pablo Ceballosc18be292016-05-31 14:55:42 -07002193EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002194 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002195 EGLnsecsANDROID *values)
2196{
2197 clearError();
2198
2199 const egl_display_ptr dp = validate_display(dpy);
2200 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002201 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002202 }
2203
2204 SurfaceRef _s(dp.get(), surface);
2205 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002206 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002207 }
2208
2209 egl_surface_t const * const s = get_surface(surface);
2210
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002211 if (!s->win.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002212 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002213 }
2214
Brian Andersondbd0ea82016-07-22 09:38:59 -07002215 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002216 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002217 nsecs_t* latchTime = nullptr;
2218 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002219 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002220 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002221 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002222 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002223 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002224 nsecs_t* releaseTime = nullptr;
2225
2226 for (int i = 0; i < numTimestamps; i++) {
2227 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002228 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2229 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002230 break;
2231 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2232 acquireTime = &values[i];
2233 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002234 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2235 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002236 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002237 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2238 firstRefreshStartTime = &values[i];
2239 break;
2240 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2241 lastRefreshStartTime = &values[i];
2242 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002243 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2244 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002245 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002246 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2247 displayPresentTime = &values[i];
2248 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002249 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2250 displayRetireTime = &values[i];
2251 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002252 case EGL_DEQUEUE_READY_TIME_ANDROID:
2253 dequeueReadyTime = &values[i];
2254 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002255 case EGL_READS_DONE_TIME_ANDROID:
2256 releaseTime = &values[i];
2257 break;
2258 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002259 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002260 }
2261 }
2262
Brian Anderson1049d1d2016-12-16 17:25:57 -08002263 status_t ret = native_window_get_frame_timestamps(s->win.get(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002264 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002265 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002266 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002267
Brian Anderson069b3652016-07-22 10:32:47 -07002268 switch (ret) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002269 case NO_ERROR:
2270 return EGL_TRUE;
2271 case NAME_NOT_FOUND:
2272 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
2273 case INVALID_OPERATION:
2274 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2275 case BAD_VALUE:
2276 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2277 default:
2278 // This should not happen. Return an error that is not in the spec
2279 // so it's obvious something is very wrong.
2280 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2281 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002282 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002283}
2284
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002285EGLBoolean eglGetFrameTimestampSupportedANDROID(
2286 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002287{
2288 clearError();
2289
2290 const egl_display_ptr dp = validate_display(dpy);
2291 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002292 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002293 }
2294
2295 SurfaceRef _s(dp.get(), surface);
2296 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002297 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002298 }
2299
2300 egl_surface_t const * const s = get_surface(surface);
2301
2302 ANativeWindow* window = s->win.get();
2303 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002304 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002305 }
2306
2307 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002308 case EGL_COMPOSITE_DEADLINE_ANDROID:
2309 case EGL_COMPOSITE_INTERVAL_ANDROID:
2310 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002311 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002312 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002313 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2314 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2315 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002316 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002317 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002318 case EGL_READS_DONE_TIME_ANDROID:
2319 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002320 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2321 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002322 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002323 return value == 0 ? EGL_FALSE : EGL_TRUE;
2324 }
2325 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2326 int value = 0;
Mathias Agopian737b8962017-02-24 14:32:05 -08002327 window->query(window, NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
Brian Anderson069b3652016-07-22 10:32:47 -07002328 return value == 0 ? EGL_FALSE : EGL_TRUE;
2329 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002330 default:
2331 return EGL_FALSE;
2332 }
2333}