blob: b38b4c2a5879b86e281fde74eeb3fe0639171d05 [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
24#include <hardware/gralloc.h>
25#include <system/window.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
Mathias Agopian518ec112011-05-13 16:21:08 -070030#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070031#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070032#include <cutils/memory.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Craig Donner68671532016-07-18 10:19:54 -070036#include <gui/ISurfaceComposer.h>
37
Craig Donner05249fc2016-01-15 19:33:55 -080038#include <ui/GraphicBuffer.h>
39
Mathias Agopian518ec112011-05-13 16:21:08 -070040#include <utils/KeyedVector.h>
41#include <utils/SortedVector.h>
42#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070044
Craig Donner68671532016-07-18 10:19:54 -070045#include "binder/Binder.h"
46#include "binder/Parcel.h"
47#include "binder/IServiceManager.h"
48
Mathias Agopian39c24a22013-04-04 23:17:56 -070049#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070050#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070051
52#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070053#include "egl_object.h"
54#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080055#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070056
57using namespace android;
58
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070059#define ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS 0
60
Mathias Agopian518ec112011-05-13 16:21:08 -070061// ----------------------------------------------------------------------------
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063namespace android {
64
Mathias Agopian518ec112011-05-13 16:21:08 -070065struct extention_map_t {
66 const char* name;
67 __eglMustCastToProperFunctionPointerType address;
68};
69
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070070/*
Jesse Hall21558da2013-08-06 15:31:22 -070071 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070072 *
Jesse Hall21558da2013-08-06 15:31:22 -070073 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
74 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070075 *
Jesse Hall21558da2013-08-06 15:31:22 -070076 * The rest (gExtensionString) depend on support in the EGL driver, and are
77 * only available if the driver supports them. However, some of these must be
78 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080079 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070080 * the mandatory extensions are present and may not function properly if some
81 * are missing.
82 *
83 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070084 */
Jesse Hall21558da2013-08-06 15:31:22 -070085extern char const * const gBuiltinExtensionString =
86 "EGL_KHR_get_all_proc_addresses "
87 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080088 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080089 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080090 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070091#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -070092 "EGL_ANDROID_get_frame_timestamps "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070093#endif
Jesse Hall21558da2013-08-06 15:31:22 -070094 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095extern char const * const gExtensionString =
96 "EGL_KHR_image " // mandatory
97 "EGL_KHR_image_base " // mandatory
98 "EGL_KHR_image_pixmap "
99 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700100 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700101 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700102 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_cubemap_image "
104 "EGL_KHR_gl_renderbuffer_image "
105 "EGL_KHR_reusable_sync "
106 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700107 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700108 "EGL_KHR_config_attribs "
109 "EGL_KHR_surfaceless_context "
110 "EGL_KHR_stream "
111 "EGL_KHR_stream_fifo "
112 "EGL_KHR_stream_producer_eglsurface "
113 "EGL_KHR_stream_consumer_gltexture "
114 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700115 "EGL_EXT_create_context_robustness "
116 "EGL_NV_system_time "
117 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700118 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700119 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800120 "EGL_KHR_partial_update " // strongly recommended
121 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700122 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700123 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700124 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700125 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000126 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700127 ;
128
129// extensions not exposed to applications but used by the ANDROID system
130// "EGL_ANDROID_blob_cache " // strongly recommended
131// "EGL_IMG_hibernate_process " // optional
132// "EGL_ANDROID_native_fence_sync " // strongly recommended
133// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700134// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700135
136/*
137 * EGL Extensions entry-points exposed to 3rd party applications
138 * (keep in sync with gExtensionString above)
139 *
140 */
141static const extention_map_t sExtensionMap[] = {
142 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700143 { "eglLockSurfaceKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
145 { "eglUnlockSurfaceKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700147
148 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700149 { "eglCreateImageKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
151 { "eglDestroyImageKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700153
154 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
155 { "eglCreateSyncKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
157 { "eglDestroySyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
159 { "eglClientWaitSyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
161 { "eglSignalSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
163 { "eglGetSyncAttribKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
165
166 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800167 { "eglGetSystemTimeFrequencyNV",
168 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
169 { "eglGetSystemTimeNV",
170 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700171
Mathias Agopian2bb71682013-03-27 17:32:41 -0700172 // EGL_KHR_wait_sync
173 { "eglWaitSyncKHR",
174 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700175
176 // EGL_ANDROID_presentation_time
177 { "eglPresentationTimeANDROID",
178 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800179
180 // EGL_KHR_swap_buffers_with_damage
181 { "eglSwapBuffersWithDamageKHR",
182 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
183
Craig Donner05249fc2016-01-15 19:33:55 -0800184 // EGL_ANDROID_native_client_buffer
185 { "eglCreateNativeClientBufferANDROID",
186 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
187
Dan Stozaa894d082015-02-19 15:27:36 -0800188 // EGL_KHR_partial_update
189 { "eglSetDamageRegionKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700191
192 { "eglCreateStreamKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
194 { "eglDestroyStreamKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
196 { "eglStreamAttribKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
198 { "eglQueryStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
200 { "eglQueryStreamu64KHR",
201 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
202 { "eglQueryStreamTimeKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
204 { "eglCreateStreamProducerSurfaceKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
206 { "eglStreamConsumerGLTextureExternalKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
208 { "eglStreamConsumerAcquireKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
210 { "eglStreamConsumerReleaseKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
212 { "eglGetStreamFileDescriptorKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
214 { "eglCreateStreamFromFileDescriptorKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700216
217 // EGL_ANDROID_get_frame_timestamps
218 { "eglGetFrameTimestampsANDROID",
219 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
220 { "eglQueryTimestampSupportedANDROID",
221 (__eglMustCastToProperFunctionPointerType)&eglQueryTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700222};
223
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700224/*
225 * These extensions entry-points should not be exposed to applications.
226 * They're used internally by the Android EGL layer.
227 */
228#define FILTER_EXTENSIONS(procname) \
229 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
230 !strcmp((procname), "eglHibernateProcessIMG") || \
231 !strcmp((procname), "eglAwakenProcessIMG") || \
232 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
233
234
235
Mathias Agopian518ec112011-05-13 16:21:08 -0700236// accesses protected by sExtensionMapMutex
237static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
238static int sGLExtentionSlot = 0;
239static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
240
241static void(*findProcAddress(const char* name,
242 const extention_map_t* map, size_t n))() {
243 for (uint32_t i=0 ; i<n ; i++) {
244 if (!strcmp(name, map[i].name)) {
245 return map[i].address;
246 }
247 }
248 return NULL;
249}
250
251// ----------------------------------------------------------------------------
252
Mathias Agopian518ec112011-05-13 16:21:08 -0700253extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
254extern EGLBoolean egl_init_drivers();
255extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700256extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700257
Mathias Agopian518ec112011-05-13 16:21:08 -0700258} // namespace android;
259
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700260
Mathias Agopian518ec112011-05-13 16:21:08 -0700261// ----------------------------------------------------------------------------
262
263static inline void clearError() { egl_tls_t::clearError(); }
264static inline EGLContext getContext() { return egl_tls_t::getContext(); }
265
266// ----------------------------------------------------------------------------
267
268EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
269{
Jesse Hall1508ae62017-01-19 17:43:26 -0800270 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700271 clearError();
272
Dan Stozac3289c42014-01-17 11:38:34 -0800273 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700274 if (index >= NUM_DISPLAYS) {
275 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
276 }
277
278 if (egl_init_drivers() == EGL_FALSE) {
279 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
280 }
281
282 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
283 return dpy;
284}
285
286// ----------------------------------------------------------------------------
287// Initialization
288// ----------------------------------------------------------------------------
289
290EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
291{
292 clearError();
293
Jesse Hallb29e5e82012-04-04 16:53:42 -0700294 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700295 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
296
297 EGLBoolean res = dp->initialize(major, minor);
298
299 return res;
300}
301
302EGLBoolean eglTerminate(EGLDisplay dpy)
303{
304 // NOTE: don't unload the drivers b/c some APIs can be called
305 // after eglTerminate() has been called. eglTerminate() only
306 // terminates an EGLDisplay, not a EGL itself.
307
308 clearError();
309
Jesse Hallb29e5e82012-04-04 16:53:42 -0700310 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700311 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
312
313 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800314
Mathias Agopian518ec112011-05-13 16:21:08 -0700315 return res;
316}
317
318// ----------------------------------------------------------------------------
319// configuration
320// ----------------------------------------------------------------------------
321
322EGLBoolean eglGetConfigs( EGLDisplay dpy,
323 EGLConfig *configs,
324 EGLint config_size, EGLint *num_config)
325{
326 clearError();
327
Jesse Hallb29e5e82012-04-04 16:53:42 -0700328 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700329 if (!dp) return EGL_FALSE;
330
Mathias Agopian7773c432012-02-13 20:06:08 -0800331 if (num_config==0) {
332 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700333 }
334
Mathias Agopian7773c432012-02-13 20:06:08 -0800335 EGLBoolean res = EGL_FALSE;
336 *num_config = 0;
337
338 egl_connection_t* const cnx = &gEGLImpl;
339 if (cnx->dso) {
340 res = cnx->egl.eglGetConfigs(
341 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700342 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800343
344 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700345}
346
347EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
348 EGLConfig *configs, EGLint config_size,
349 EGLint *num_config)
350{
351 clearError();
352
Jesse Hallb29e5e82012-04-04 16:53:42 -0700353 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700354 if (!dp) return EGL_FALSE;
355
356 if (num_config==0) {
357 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
358 }
359
Mathias Agopian518ec112011-05-13 16:21:08 -0700360 EGLBoolean res = EGL_FALSE;
361 *num_config = 0;
362
Mathias Agopianada798b2012-02-13 17:09:30 -0800363 egl_connection_t* const cnx = &gEGLImpl;
364 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700365 if (attrib_list) {
366 char value[PROPERTY_VALUE_MAX];
367 property_get("debug.egl.force_msaa", value, "false");
368
369 if (!strcmp(value, "true")) {
370 size_t attribCount = 0;
371 EGLint attrib = attrib_list[0];
372
373 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700374 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700375 const EGLint *attribRendererable = NULL;
376 const EGLint *attribCaveat = NULL;
377
378 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700379 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700380 while (attrib != EGL_NONE) {
381 attrib = attrib_list[attribCount];
382 switch (attrib) {
383 case EGL_RENDERABLE_TYPE:
384 attribRendererable = &attrib_list[attribCount];
385 break;
386 case EGL_CONFIG_CAVEAT:
387 attribCaveat = &attrib_list[attribCount];
388 break;
389 }
390 attribCount++;
391 }
392
393 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
394 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800395
Romain Guy1cffc802012-10-15 18:13:05 -0700396 // Insert 2 extra attributes to force-enable MSAA 4x
397 EGLint aaAttribs[attribCount + 4];
398 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
399 aaAttribs[1] = 1;
400 aaAttribs[2] = EGL_SAMPLES;
401 aaAttribs[3] = 4;
402
403 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
404
405 EGLint numConfigAA;
406 EGLBoolean resAA = cnx->egl.eglChooseConfig(
407 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
408
409 if (resAA == EGL_TRUE && numConfigAA > 0) {
410 ALOGD("Enabling MSAA 4x");
411 *num_config = numConfigAA;
412 return resAA;
413 }
414 }
415 }
416 }
417
Mathias Agopian7773c432012-02-13 20:06:08 -0800418 res = cnx->egl.eglChooseConfig(
419 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700420 }
421 return res;
422}
423
424EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
425 EGLint attribute, EGLint *value)
426{
427 clearError();
428
Jesse Hallb29e5e82012-04-04 16:53:42 -0700429 egl_connection_t* cnx = NULL;
430 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
431 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800432
Mathias Agopian518ec112011-05-13 16:21:08 -0700433 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800434 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700435}
436
437// ----------------------------------------------------------------------------
438// surfaces
439// ----------------------------------------------------------------------------
440
Jesse Hallc2e41222013-08-08 13:40:22 -0700441// Turn linear formats into corresponding sRGB formats when colorspace is
442// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
443// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800444// the modification isn't possible, the original dataSpace is returned.
445static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
446 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700447 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800448 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700449 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800450 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700451 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800452 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700453}
454
Mathias Agopian518ec112011-05-13 16:21:08 -0700455EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
456 NativeWindowType window,
457 const EGLint *attrib_list)
458{
459 clearError();
460
Jesse Hallb29e5e82012-04-04 16:53:42 -0700461 egl_connection_t* cnx = NULL;
462 egl_display_ptr dp = validate_display_connection(dpy, cnx);
463 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800464 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700465
Andy McFaddend566ce32014-01-07 15:54:17 -0800466 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
467 if (result != OK) {
468 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
469 "failed (%#x) (already connected to another API?)",
470 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700471 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700472 }
473
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700474 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700475 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
476 // of our native format. So if sRGB gamma is requested, we have to
477 // modify the EGLconfig's format before setting the native window's
478 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800479
Romain Guyff415142016-12-13 16:51:25 -0800480 // TODO: Add support for HAL_PIXEL_FORMAT_RGBA_FP16
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700481 // by default, just pick RGBA_8888
482 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800483 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700484
485 EGLint a = 0;
486 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
487 if (a > 0) {
488 // alpha-channel requested, there's really only one suitable format
489 format = HAL_PIXEL_FORMAT_RGBA_8888;
490 } else {
491 EGLint r, g, b;
492 r = g = b = 0;
493 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
494 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
495 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
496 EGLint colorDepth = r + g + b;
497 if (colorDepth <= 16) {
498 format = HAL_PIXEL_FORMAT_RGB_565;
499 } else {
500 format = HAL_PIXEL_FORMAT_RGBX_8888;
501 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700502 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700503
504 // now select a corresponding sRGB format if needed
505 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
506 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
507 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530508 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700509 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700510 }
511 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800512
Jesse Hallc2e41222013-08-08 13:40:22 -0700513 if (format != 0) {
514 int err = native_window_set_buffers_format(window, format);
515 if (err != 0) {
516 ALOGE("error setting native window pixel format: %s (%d)",
517 strerror(-err), err);
518 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
519 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
520 }
521 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700522
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800523 if (dataSpace != 0) {
524 int err = native_window_set_buffers_data_space(window, dataSpace);
525 if (err != 0) {
526 ALOGE("error setting native window pixel dataSpace: %s (%d)",
527 strerror(-err), err);
528 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
529 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
530 }
531 }
532
Jamie Gennis59769462011-11-19 18:04:43 -0800533 // the EGL spec requires that a new EGLSurface default to swap interval
534 // 1, so explicitly set that on the window here.
535 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
536 anw->setSwapInterval(anw, 1);
537
Mathias Agopian518ec112011-05-13 16:21:08 -0700538 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800539 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700540 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700541 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
542 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700543 return s;
544 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700545
546 // EGLSurface creation failed
547 native_window_set_buffers_format(window, 0);
548 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700549 }
550 return EGL_NO_SURFACE;
551}
552
553EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
554 NativePixmapType pixmap,
555 const EGLint *attrib_list)
556{
557 clearError();
558
Jesse Hallb29e5e82012-04-04 16:53:42 -0700559 egl_connection_t* cnx = NULL;
560 egl_display_ptr dp = validate_display_connection(dpy, cnx);
561 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700562 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800563 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700564 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700565 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
566 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700567 return s;
568 }
569 }
570 return EGL_NO_SURFACE;
571}
572
573EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
574 const EGLint *attrib_list)
575{
576 clearError();
577
Jesse Hallb29e5e82012-04-04 16:53:42 -0700578 egl_connection_t* cnx = NULL;
579 egl_display_ptr dp = validate_display_connection(dpy, cnx);
580 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700581 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800582 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700583 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700584 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
585 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700586 return s;
587 }
588 }
589 return EGL_NO_SURFACE;
590}
Jesse Hall47743382013-02-08 11:13:46 -0800591
Mathias Agopian518ec112011-05-13 16:21:08 -0700592EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
593{
594 clearError();
595
Jesse Hallb29e5e82012-04-04 16:53:42 -0700596 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700597 if (!dp) return EGL_FALSE;
598
Jesse Hallb29e5e82012-04-04 16:53:42 -0700599 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700600 if (!_s.get())
601 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700602
603 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800604 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700605 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700606 _s.terminate();
607 }
608 return result;
609}
610
611EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
612 EGLint attribute, EGLint *value)
613{
614 clearError();
615
Jesse Hallb29e5e82012-04-04 16:53:42 -0700616 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700617 if (!dp) return EGL_FALSE;
618
Jesse Hallb29e5e82012-04-04 16:53:42 -0700619 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700620 if (!_s.get())
621 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700622
Mathias Agopian518ec112011-05-13 16:21:08 -0700623 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800624 return s->cnx->egl.eglQuerySurface(
625 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700626}
627
Jamie Gennise8696a42012-01-15 18:54:57 -0800628void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800629 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800630 clearError();
631
Jesse Hallb29e5e82012-04-04 16:53:42 -0700632 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800633 if (!dp) {
634 return;
635 }
636
Jesse Hallb29e5e82012-04-04 16:53:42 -0700637 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800638 if (!_s.get()) {
639 setError(EGL_BAD_SURFACE, EGL_FALSE);
640 return;
641 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800642}
643
Mathias Agopian518ec112011-05-13 16:21:08 -0700644// ----------------------------------------------------------------------------
645// Contexts
646// ----------------------------------------------------------------------------
647
648EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
649 EGLContext share_list, const EGLint *attrib_list)
650{
651 clearError();
652
Jesse Hallb29e5e82012-04-04 16:53:42 -0700653 egl_connection_t* cnx = NULL;
654 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700655 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700656 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700657 if (!ContextRef(dp.get(), share_list).get()) {
658 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
659 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700660 egl_context_t* const c = get_context(share_list);
661 share_list = c->context;
662 }
663 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800664 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700665 if (context != EGL_NO_CONTEXT) {
666 // figure out if it's a GLESv1 or GLESv2
667 int version = 0;
668 if (attrib_list) {
669 while (*attrib_list != EGL_NONE) {
670 GLint attr = *attrib_list++;
671 GLint value = *attrib_list++;
672 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
673 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800674 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800675 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800676 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700677 }
678 }
679 };
680 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700681 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
682 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700683 return c;
684 }
685 }
686 return EGL_NO_CONTEXT;
687}
688
689EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
690{
691 clearError();
692
Jesse Hallb29e5e82012-04-04 16:53:42 -0700693 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700694 if (!dp)
695 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700696
Jesse Hallb29e5e82012-04-04 16:53:42 -0700697 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700698 if (!_c.get())
699 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800700
Mathias Agopian518ec112011-05-13 16:21:08 -0700701 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800702 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700703 if (result == EGL_TRUE) {
704 _c.terminate();
705 }
706 return result;
707}
708
Mathias Agopian518ec112011-05-13 16:21:08 -0700709EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
710 EGLSurface read, EGLContext ctx)
711{
712 clearError();
713
Jesse Hallb29e5e82012-04-04 16:53:42 -0700714 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700715 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
716
Mathias Agopian5b287a62011-05-16 18:58:55 -0700717 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
718 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
719 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700720 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
721 (draw != EGL_NO_SURFACE) ) {
722 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
723 }
724
725 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700726 ContextRef _c(dp.get(), ctx);
727 SurfaceRef _d(dp.get(), draw);
728 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700729
730 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700731 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700732 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700733 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700734 }
735
736 // these are the underlying implementation's object
737 EGLContext impl_ctx = EGL_NO_CONTEXT;
738 EGLSurface impl_draw = EGL_NO_SURFACE;
739 EGLSurface impl_read = EGL_NO_SURFACE;
740
741 // these are our objects structs passed in
742 egl_context_t * c = NULL;
743 egl_surface_t const * d = NULL;
744 egl_surface_t const * r = NULL;
745
746 // these are the current objects structs
747 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800748
Mathias Agopian518ec112011-05-13 16:21:08 -0700749 if (ctx != EGL_NO_CONTEXT) {
750 c = get_context(ctx);
751 impl_ctx = c->context;
752 } else {
753 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700754 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
755 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
756 return setError(EGL_BAD_MATCH, EGL_FALSE);
757 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 if (cur_c == NULL) {
759 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700760 // not an error, there is just no current context.
761 return EGL_TRUE;
762 }
763 }
764
765 // retrieve the underlying implementation's draw EGLSurface
766 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700767 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700768 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 impl_draw = d->surface;
770 }
771
772 // retrieve the underlying implementation's read EGLSurface
773 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700774 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700775 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700776 impl_read = r->surface;
777 }
778
Mathias Agopian518ec112011-05-13 16:21:08 -0700779
Jesse Hallb29e5e82012-04-04 16:53:42 -0700780 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800781 draw, read, ctx,
782 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700783
784 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800785 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700786 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
787 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700788 _c.acquire();
789 _r.acquire();
790 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700791 } else {
792 setGLHooksThreadSpecific(&gHooksNoContext);
793 egl_tls_t::setContext(EGL_NO_CONTEXT);
794 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700795 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000796 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700797 egl_connection_t* const cnx = &gEGLImpl;
798 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700799 }
800 return result;
801}
802
803
804EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
805 EGLint attribute, EGLint *value)
806{
807 clearError();
808
Jesse Hallb29e5e82012-04-04 16:53:42 -0700809 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 if (!dp) return EGL_FALSE;
811
Jesse Hallb29e5e82012-04-04 16:53:42 -0700812 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700813 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
814
Mathias Agopian518ec112011-05-13 16:21:08 -0700815 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800816 return c->cnx->egl.eglQueryContext(
817 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700818
Mathias Agopian518ec112011-05-13 16:21:08 -0700819}
820
821EGLContext eglGetCurrentContext(void)
822{
823 // could be called before eglInitialize(), but we wouldn't have a context
824 // then, and this function would correctly return EGL_NO_CONTEXT.
825
826 clearError();
827
828 EGLContext ctx = getContext();
829 return ctx;
830}
831
832EGLSurface eglGetCurrentSurface(EGLint readdraw)
833{
834 // could be called before eglInitialize(), but we wouldn't have a context
835 // then, and this function would correctly return EGL_NO_SURFACE.
836
837 clearError();
838
839 EGLContext ctx = getContext();
840 if (ctx) {
841 egl_context_t const * const c = get_context(ctx);
842 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
843 switch (readdraw) {
844 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800845 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700846 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
847 }
848 }
849 return EGL_NO_SURFACE;
850}
851
852EGLDisplay eglGetCurrentDisplay(void)
853{
854 // could be called before eglInitialize(), but we wouldn't have a context
855 // then, and this function would correctly return EGL_NO_DISPLAY.
856
857 clearError();
858
859 EGLContext ctx = getContext();
860 if (ctx) {
861 egl_context_t const * const c = get_context(ctx);
862 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
863 return c->dpy;
864 }
865 return EGL_NO_DISPLAY;
866}
867
868EGLBoolean eglWaitGL(void)
869{
Mathias Agopian518ec112011-05-13 16:21:08 -0700870 clearError();
871
Mathias Agopianada798b2012-02-13 17:09:30 -0800872 egl_connection_t* const cnx = &gEGLImpl;
873 if (!cnx->dso)
874 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
875
876 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700877}
878
879EGLBoolean eglWaitNative(EGLint engine)
880{
Mathias Agopian518ec112011-05-13 16:21:08 -0700881 clearError();
882
Mathias Agopianada798b2012-02-13 17:09:30 -0800883 egl_connection_t* const cnx = &gEGLImpl;
884 if (!cnx->dso)
885 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
886
887 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700888}
889
890EGLint eglGetError(void)
891{
Mathias Agopianada798b2012-02-13 17:09:30 -0800892 EGLint err = EGL_SUCCESS;
893 egl_connection_t* const cnx = &gEGLImpl;
894 if (cnx->dso) {
895 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700896 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800897 if (err == EGL_SUCCESS) {
898 err = egl_tls_t::getError();
899 }
900 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700901}
902
Michael Chockc0ec5e22014-01-27 08:14:33 -0800903static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700904 const char* procname) {
905 const egl_connection_t* cnx = &gEGLImpl;
906 void* proc = NULL;
907
Michael Chockc0ec5e22014-01-27 08:14:33 -0800908 proc = dlsym(cnx->libEgl, procname);
909 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
910
Jesse Hallc07b5202013-07-04 12:08:16 -0700911 proc = dlsym(cnx->libGles2, procname);
912 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
913
914 proc = dlsym(cnx->libGles1, procname);
915 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
916
917 return NULL;
918}
919
Mathias Agopian518ec112011-05-13 16:21:08 -0700920__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
921{
922 // eglGetProcAddress() could be the very first function called
923 // in which case we must make sure we've initialized ourselves, this
924 // happens the first time egl_get_display() is called.
925
926 clearError();
927
928 if (egl_init_drivers() == EGL_FALSE) {
929 setError(EGL_BAD_PARAMETER, NULL);
930 return NULL;
931 }
932
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700933 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700934 return NULL;
935 }
936
Mathias Agopian518ec112011-05-13 16:21:08 -0700937 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700938 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700939 if (addr) return addr;
940
Michael Chockc0ec5e22014-01-27 08:14:33 -0800941 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700942 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700943
Mathias Agopian518ec112011-05-13 16:21:08 -0700944 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
945 pthread_mutex_lock(&sExtensionMapMutex);
946
947 /*
948 * Since eglGetProcAddress() is not associated to anything, it needs
949 * to return a function pointer that "works" regardless of what
950 * the current context is.
951 *
952 * For this reason, we return a "forwarder", a small stub that takes
953 * care of calling the function associated with the context
954 * currently bound.
955 *
956 * We first look for extensions we've already resolved, if we're seeing
957 * this extension for the first time, we go through all our
958 * implementations and call eglGetProcAddress() and record the
959 * result in the appropriate implementation hooks and return the
960 * address of the forwarder corresponding to that hook set.
961 *
962 */
963
964 const String8 name(procname);
965 addr = sGLExtentionMap.valueFor(name);
966 const int slot = sGLExtentionSlot;
967
Steve Blocke6f43dd2012-01-06 19:20:56 +0000968 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 "no more slots for eglGetProcAddress(\"%s\")",
970 procname);
971
972 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
973 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800974
975 egl_connection_t* const cnx = &gEGLImpl;
976 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800977 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800978 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800979 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
980 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800981 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800982 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800984
Mathias Agopian518ec112011-05-13 16:21:08 -0700985 if (found) {
986 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700987 sGLExtentionMap.add(name, addr);
988 sGLExtentionSlot++;
989 }
990 }
991
992 pthread_mutex_unlock(&sExtensionMapMutex);
993 return addr;
994}
995
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700996class FrameCompletionThread : public Thread {
997public:
998
999 static void queueSync(EGLSyncKHR sync) {
1000 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1001 static bool running = false;
1002 if (!running) {
1003 thread->run("GPUFrameCompletion");
1004 running = true;
1005 }
1006 {
1007 Mutex::Autolock lock(thread->mMutex);
1008 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1009 thread->mFramesQueued).string());
1010 thread->mQueue.push_back(sync);
1011 thread->mCondition.signal();
1012 thread->mFramesQueued++;
1013 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1014 }
1015 }
1016
1017private:
1018 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1019
1020 virtual bool threadLoop() {
1021 EGLSyncKHR sync;
1022 uint32_t frameNum;
1023 {
1024 Mutex::Autolock lock(mMutex);
1025 while (mQueue.isEmpty()) {
1026 mCondition.wait(mMutex);
1027 }
1028 sync = mQueue[0];
1029 frameNum = mFramesCompleted;
1030 }
1031 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1032 {
1033 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1034 frameNum).string());
1035 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1036 if (result == EGL_FALSE) {
1037 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1038 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1039 ALOGE("FrameCompletion: timeout waiting for fence");
1040 }
1041 eglDestroySyncKHR(dpy, sync);
1042 }
1043 {
1044 Mutex::Autolock lock(mMutex);
1045 mQueue.removeAt(0);
1046 mFramesCompleted++;
1047 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1048 }
1049 return true;
1050 }
1051
1052 uint32_t mFramesQueued;
1053 uint32_t mFramesCompleted;
1054 Vector<EGLSyncKHR> mQueue;
1055 Condition mCondition;
1056 Mutex mMutex;
1057};
1058
Dan Stozaa894d082015-02-19 15:27:36 -08001059EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1060 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001061{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001062 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001063 clearError();
1064
Jesse Hallb29e5e82012-04-04 16:53:42 -07001065 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001066 if (!dp) return EGL_FALSE;
1067
Jesse Hallb29e5e82012-04-04 16:53:42 -07001068 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001069 if (!_s.get())
1070 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001071
Mathias Agopian518ec112011-05-13 16:21:08 -07001072 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001073
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001074 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1075 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1076 if (sync != EGL_NO_SYNC_KHR) {
1077 FrameCompletionThread::queueSync(sync);
1078 }
1079 }
1080
Mathias Agopian7db993a2012-03-25 00:49:46 -07001081 if (CC_UNLIKELY(dp->finishOnSwap)) {
1082 uint32_t pixel;
1083 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1084 if (c) {
1085 // glReadPixels() ensures that the frame is complete
1086 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1087 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1088 }
1089 }
1090
Dan Stozaa894d082015-02-19 15:27:36 -08001091 if (n_rects == 0) {
1092 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1093 }
1094
1095 Vector<android_native_rect_t> androidRects;
1096 for (int r = 0; r < n_rects; ++r) {
1097 int offset = r * 4;
1098 int x = rects[offset];
1099 int y = rects[offset + 1];
1100 int width = rects[offset + 2];
1101 int height = rects[offset + 3];
1102 android_native_rect_t androidRect;
1103 androidRect.left = x;
1104 androidRect.top = y + height;
1105 androidRect.right = x + width;
1106 androidRect.bottom = y;
1107 androidRects.push_back(androidRect);
1108 }
1109 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1110 androidRects.size());
1111
1112 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1113 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1114 rects, n_rects);
1115 } else {
1116 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1117 }
1118}
1119
1120EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1121{
1122 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001123}
1124
1125EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1126 NativePixmapType target)
1127{
1128 clearError();
1129
Jesse Hallb29e5e82012-04-04 16:53:42 -07001130 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001131 if (!dp) return EGL_FALSE;
1132
Jesse Hallb29e5e82012-04-04 16:53:42 -07001133 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001134 if (!_s.get())
1135 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001136
Mathias Agopian518ec112011-05-13 16:21:08 -07001137 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001138 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001139}
1140
1141const char* eglQueryString(EGLDisplay dpy, EGLint name)
1142{
1143 clearError();
1144
Chia-I Wue57d1352016-08-15 16:10:02 +08001145 // Generate an error quietly when client extensions (as defined by
1146 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1147 // validate_display to generate the error as validate_display would log
1148 // the error, which can be misleading.
1149 //
1150 // If we want to support EGL_EXT_client_extensions later, we can return
1151 // the client extension string here instead.
1152 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1153 return setErrorQuiet(EGL_BAD_DISPLAY, nullptr);
1154
Jesse Hallb29e5e82012-04-04 16:53:42 -07001155 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001156 if (!dp) return (const char *) NULL;
1157
1158 switch (name) {
1159 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001160 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001161 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001162 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001163 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001164 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001165 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001166 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001167 }
1168 return setError(EGL_BAD_PARAMETER, (const char *)0);
1169}
1170
Mathias Agopianca088332013-03-28 17:44:13 -07001171EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1172{
1173 clearError();
1174
1175 const egl_display_ptr dp = validate_display(dpy);
1176 if (!dp) return (const char *) NULL;
1177
1178 switch (name) {
1179 case EGL_VENDOR:
1180 return dp->disp.queryString.vendor;
1181 case EGL_VERSION:
1182 return dp->disp.queryString.version;
1183 case EGL_EXTENSIONS:
1184 return dp->disp.queryString.extensions;
1185 case EGL_CLIENT_APIS:
1186 return dp->disp.queryString.clientApi;
1187 }
1188 return setError(EGL_BAD_PARAMETER, (const char *)0);
1189}
Mathias Agopian518ec112011-05-13 16:21:08 -07001190
1191// ----------------------------------------------------------------------------
1192// EGL 1.1
1193// ----------------------------------------------------------------------------
1194
1195EGLBoolean eglSurfaceAttrib(
1196 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1197{
1198 clearError();
1199
Jesse Hallb29e5e82012-04-04 16:53:42 -07001200 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001201 if (!dp) return EGL_FALSE;
1202
Jesse Hallb29e5e82012-04-04 16:53:42 -07001203 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001204 if (!_s.get())
1205 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001206
Pablo Ceballosc18be292016-05-31 14:55:42 -07001207 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001208
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001209 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001210 if (!s->win.get()) {
1211 setError(EGL_BAD_SURFACE, EGL_FALSE);
1212 }
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001213 int err = native_window_set_auto_refresh(s->win.get(),
1214 value ? true : false);
1215 return (err == NO_ERROR) ? EGL_TRUE :
1216 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001217 }
1218
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001219#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001220 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001221 if (!s->win.get()) {
1222 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1223 }
Brian Anderson069b3652016-07-22 10:32:47 -07001224 int err = native_window_enable_frame_timestamps(
1225 s->win.get(), value ? true : false);
1226 return (err == NO_ERROR) ? EGL_TRUE :
1227 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001228 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001229#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001230
Mathias Agopian518ec112011-05-13 16:21:08 -07001231 if (s->cnx->egl.eglSurfaceAttrib) {
1232 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001233 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001234 }
1235 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1236}
1237
1238EGLBoolean eglBindTexImage(
1239 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1240{
1241 clearError();
1242
Jesse Hallb29e5e82012-04-04 16:53:42 -07001243 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001244 if (!dp) return EGL_FALSE;
1245
Jesse Hallb29e5e82012-04-04 16:53:42 -07001246 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001247 if (!_s.get())
1248 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001249
Mathias Agopian518ec112011-05-13 16:21:08 -07001250 egl_surface_t const * const s = get_surface(surface);
1251 if (s->cnx->egl.eglBindTexImage) {
1252 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001253 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001254 }
1255 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1256}
1257
1258EGLBoolean eglReleaseTexImage(
1259 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1260{
1261 clearError();
1262
Jesse Hallb29e5e82012-04-04 16:53:42 -07001263 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001264 if (!dp) return EGL_FALSE;
1265
Jesse Hallb29e5e82012-04-04 16:53:42 -07001266 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001267 if (!_s.get())
1268 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001269
Mathias Agopian518ec112011-05-13 16:21:08 -07001270 egl_surface_t const * const s = get_surface(surface);
1271 if (s->cnx->egl.eglReleaseTexImage) {
1272 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001273 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001274 }
1275 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1276}
1277
1278EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1279{
1280 clearError();
1281
Jesse Hallb29e5e82012-04-04 16:53:42 -07001282 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001283 if (!dp) return EGL_FALSE;
1284
1285 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001286 egl_connection_t* const cnx = &gEGLImpl;
1287 if (cnx->dso && cnx->egl.eglSwapInterval) {
1288 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001289 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001290
Mathias Agopian518ec112011-05-13 16:21:08 -07001291 return res;
1292}
1293
1294
1295// ----------------------------------------------------------------------------
1296// EGL 1.2
1297// ----------------------------------------------------------------------------
1298
1299EGLBoolean eglWaitClient(void)
1300{
1301 clearError();
1302
Mathias Agopianada798b2012-02-13 17:09:30 -08001303 egl_connection_t* const cnx = &gEGLImpl;
1304 if (!cnx->dso)
1305 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1306
1307 EGLBoolean res;
1308 if (cnx->egl.eglWaitClient) {
1309 res = cnx->egl.eglWaitClient();
1310 } else {
1311 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001312 }
1313 return res;
1314}
1315
1316EGLBoolean eglBindAPI(EGLenum api)
1317{
1318 clearError();
1319
1320 if (egl_init_drivers() == EGL_FALSE) {
1321 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1322 }
1323
1324 // bind this API on all EGLs
1325 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001326 egl_connection_t* const cnx = &gEGLImpl;
1327 if (cnx->dso && cnx->egl.eglBindAPI) {
1328 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001329 }
1330 return res;
1331}
1332
1333EGLenum eglQueryAPI(void)
1334{
1335 clearError();
1336
1337 if (egl_init_drivers() == EGL_FALSE) {
1338 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1339 }
1340
Mathias Agopianada798b2012-02-13 17:09:30 -08001341 egl_connection_t* const cnx = &gEGLImpl;
1342 if (cnx->dso && cnx->egl.eglQueryAPI) {
1343 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001344 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001345
Mathias Agopian518ec112011-05-13 16:21:08 -07001346 // or, it can only be OpenGL ES
1347 return EGL_OPENGL_ES_API;
1348}
1349
1350EGLBoolean eglReleaseThread(void)
1351{
1352 clearError();
1353
Mathias Agopianada798b2012-02-13 17:09:30 -08001354 egl_connection_t* const cnx = &gEGLImpl;
1355 if (cnx->dso && cnx->egl.eglReleaseThread) {
1356 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001357 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301358
1359 // If there is context bound to the thread, release it
1360 egl_display_t::loseCurrent(get_context(getContext()));
1361
Mathias Agopian518ec112011-05-13 16:21:08 -07001362 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001363 return EGL_TRUE;
1364}
1365
1366EGLSurface eglCreatePbufferFromClientBuffer(
1367 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1368 EGLConfig config, const EGLint *attrib_list)
1369{
1370 clearError();
1371
Jesse Hallb29e5e82012-04-04 16:53:42 -07001372 egl_connection_t* cnx = NULL;
1373 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1374 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001375 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1376 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001377 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001378 }
1379 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1380}
1381
1382// ----------------------------------------------------------------------------
1383// EGL_EGLEXT_VERSION 3
1384// ----------------------------------------------------------------------------
1385
1386EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1387 const EGLint *attrib_list)
1388{
1389 clearError();
1390
Jesse Hallb29e5e82012-04-04 16:53:42 -07001391 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001392 if (!dp) return EGL_FALSE;
1393
Jesse Hallb29e5e82012-04-04 16:53:42 -07001394 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001395 if (!_s.get())
1396 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001397
1398 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001399 if (s->cnx->egl.eglLockSurfaceKHR) {
1400 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001401 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001402 }
1403 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1404}
1405
1406EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1407{
1408 clearError();
1409
Jesse Hallb29e5e82012-04-04 16:53:42 -07001410 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001411 if (!dp) return EGL_FALSE;
1412
Jesse Hallb29e5e82012-04-04 16:53:42 -07001413 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001414 if (!_s.get())
1415 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001416
1417 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001418 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001419 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001420 }
1421 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1422}
1423
1424EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1425 EGLClientBuffer buffer, const EGLint *attrib_list)
1426{
1427 clearError();
1428
Jesse Hallb29e5e82012-04-04 16:53:42 -07001429 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 if (!dp) return EGL_NO_IMAGE_KHR;
1431
Jesse Hallb29e5e82012-04-04 16:53:42 -07001432 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001433 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001434
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001435 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1436 egl_connection_t* const cnx = &gEGLImpl;
1437 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1438 result = cnx->egl.eglCreateImageKHR(
1439 dp->disp.dpy,
1440 c ? c->context : EGL_NO_CONTEXT,
1441 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001442 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001443 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001444}
1445
1446EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1447{
1448 clearError();
1449
Jesse Hallb29e5e82012-04-04 16:53:42 -07001450 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001451 if (!dp) return EGL_FALSE;
1452
Steven Holte646a5c52012-06-04 20:02:11 -07001453 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001454 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001455 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001456 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001457 }
Steven Holte646a5c52012-06-04 20:02:11 -07001458 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001459}
1460
1461// ----------------------------------------------------------------------------
1462// EGL_EGLEXT_VERSION 5
1463// ----------------------------------------------------------------------------
1464
1465
1466EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1467{
1468 clearError();
1469
Jesse Hallb29e5e82012-04-04 16:53:42 -07001470 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001471 if (!dp) return EGL_NO_SYNC_KHR;
1472
Mathias Agopian518ec112011-05-13 16:21:08 -07001473 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001474 egl_connection_t* const cnx = &gEGLImpl;
1475 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1476 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001477 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001478 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001479}
1480
1481EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1482{
1483 clearError();
1484
Jesse Hallb29e5e82012-04-04 16:53:42 -07001485 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001486 if (!dp) return EGL_FALSE;
1487
Mathias Agopian518ec112011-05-13 16:21:08 -07001488 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001489 egl_connection_t* const cnx = &gEGLImpl;
1490 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1491 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001492 }
1493 return result;
1494}
1495
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001496EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1497 clearError();
1498
1499 const egl_display_ptr dp = validate_display(dpy);
1500 if (!dp) return EGL_FALSE;
1501
1502 EGLBoolean result = EGL_FALSE;
1503 egl_connection_t* const cnx = &gEGLImpl;
1504 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1505 result = cnx->egl.eglSignalSyncKHR(
1506 dp->disp.dpy, sync, mode);
1507 }
1508 return result;
1509}
1510
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001511EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1512 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001513{
1514 clearError();
1515
Jesse Hallb29e5e82012-04-04 16:53:42 -07001516 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001517 if (!dp) return EGL_FALSE;
1518
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001519 EGLBoolean result = EGL_FALSE;
1520 egl_connection_t* const cnx = &gEGLImpl;
1521 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1522 result = cnx->egl.eglClientWaitSyncKHR(
1523 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001524 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001525 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001526}
1527
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001528EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1529 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001530{
1531 clearError();
1532
Jesse Hallb29e5e82012-04-04 16:53:42 -07001533 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001534 if (!dp) return EGL_FALSE;
1535
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001536 EGLBoolean result = EGL_FALSE;
1537 egl_connection_t* const cnx = &gEGLImpl;
1538 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1539 result = cnx->egl.eglGetSyncAttribKHR(
1540 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001541 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001542 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001543}
1544
Season Li000d88f2015-07-01 11:39:40 -07001545EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1546{
1547 clearError();
1548
1549 const egl_display_ptr dp = validate_display(dpy);
1550 if (!dp) return EGL_NO_STREAM_KHR;
1551
1552 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1553 egl_connection_t* const cnx = &gEGLImpl;
1554 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1555 result = cnx->egl.eglCreateStreamKHR(
1556 dp->disp.dpy, attrib_list);
1557 }
1558 return result;
1559}
1560
1561EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1562{
1563 clearError();
1564
1565 const egl_display_ptr dp = validate_display(dpy);
1566 if (!dp) return EGL_FALSE;
1567
1568 EGLBoolean result = EGL_FALSE;
1569 egl_connection_t* const cnx = &gEGLImpl;
1570 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1571 result = cnx->egl.eglDestroyStreamKHR(
1572 dp->disp.dpy, stream);
1573 }
1574 return result;
1575}
1576
1577EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1578 EGLenum attribute, EGLint value)
1579{
1580 clearError();
1581
1582 const egl_display_ptr dp = validate_display(dpy);
1583 if (!dp) return EGL_FALSE;
1584
1585 EGLBoolean result = EGL_FALSE;
1586 egl_connection_t* const cnx = &gEGLImpl;
1587 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1588 result = cnx->egl.eglStreamAttribKHR(
1589 dp->disp.dpy, stream, attribute, value);
1590 }
1591 return result;
1592}
1593
1594EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1595 EGLenum attribute, EGLint *value)
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.eglQueryStreamKHR) {
1605 result = cnx->egl.eglQueryStreamKHR(
1606 dp->disp.dpy, stream, attribute, value);
1607 }
1608 return result;
1609}
1610
1611EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1612 EGLenum attribute, EGLuint64KHR *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.eglQueryStreamu64KHR) {
1622 result = cnx->egl.eglQueryStreamu64KHR(
1623 dp->disp.dpy, stream, attribute, value);
1624 }
1625 return result;
1626}
1627
1628EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1629 EGLenum attribute, EGLTimeKHR *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.eglQueryStreamTimeKHR) {
1639 result = cnx->egl.eglQueryStreamTimeKHR(
1640 dp->disp.dpy, stream, attribute, value);
1641 }
1642 return result;
1643}
1644
1645EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1646 EGLStreamKHR stream, const EGLint *attrib_list)
1647{
1648 clearError();
1649
1650 egl_display_ptr dp = validate_display(dpy);
1651 if (!dp) return EGL_NO_SURFACE;
1652
1653 egl_connection_t* const cnx = &gEGLImpl;
1654 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1655 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1656 dp->disp.dpy, config, stream, attrib_list);
1657 if (surface != EGL_NO_SURFACE) {
1658 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1659 surface, cnx);
1660 return s;
1661 }
1662 }
1663 return EGL_NO_SURFACE;
1664}
1665
1666EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1667 EGLStreamKHR stream)
1668{
1669 clearError();
1670
1671 const egl_display_ptr dp = validate_display(dpy);
1672 if (!dp) return EGL_FALSE;
1673
1674 EGLBoolean result = EGL_FALSE;
1675 egl_connection_t* const cnx = &gEGLImpl;
1676 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1677 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1678 dp->disp.dpy, stream);
1679 }
1680 return result;
1681}
1682
1683EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1684 EGLStreamKHR stream)
1685{
1686 clearError();
1687
1688 const egl_display_ptr dp = validate_display(dpy);
1689 if (!dp) return EGL_FALSE;
1690
1691 EGLBoolean result = EGL_FALSE;
1692 egl_connection_t* const cnx = &gEGLImpl;
1693 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1694 result = cnx->egl.eglStreamConsumerAcquireKHR(
1695 dp->disp.dpy, stream);
1696 }
1697 return result;
1698}
1699
1700EGLBoolean eglStreamConsumerReleaseKHR(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.eglStreamConsumerReleaseKHR) {
1711 result = cnx->egl.eglStreamConsumerReleaseKHR(
1712 dp->disp.dpy, stream);
1713 }
1714 return result;
1715}
1716
1717EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1718 EGLDisplay dpy, EGLStreamKHR stream)
1719{
1720 clearError();
1721
1722 const egl_display_ptr dp = validate_display(dpy);
1723 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1724
1725 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1726 egl_connection_t* const cnx = &gEGLImpl;
1727 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1728 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1729 dp->disp.dpy, stream);
1730 }
1731 return result;
1732}
1733
1734EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1735 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1736{
1737 clearError();
1738
1739 const egl_display_ptr dp = validate_display(dpy);
1740 if (!dp) return EGL_NO_STREAM_KHR;
1741
1742 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1743 egl_connection_t* const cnx = &gEGLImpl;
1744 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1745 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1746 dp->disp.dpy, file_descriptor);
1747 }
1748 return result;
1749}
1750
Mathias Agopian518ec112011-05-13 16:21:08 -07001751// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001752// EGL_EGLEXT_VERSION 15
1753// ----------------------------------------------------------------------------
1754
1755EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1756 clearError();
1757 const egl_display_ptr dp = validate_display(dpy);
1758 if (!dp) return EGL_FALSE;
1759 EGLint result = EGL_FALSE;
1760 egl_connection_t* const cnx = &gEGLImpl;
1761 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1762 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1763 }
1764 return result;
1765}
1766
1767// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001768// ANDROID extensions
1769// ----------------------------------------------------------------------------
1770
Jamie Gennis331841b2012-09-06 14:52:00 -07001771EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1772{
1773 clearError();
1774
1775 const egl_display_ptr dp = validate_display(dpy);
1776 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1777
1778 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1779 egl_connection_t* const cnx = &gEGLImpl;
1780 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1781 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1782 }
1783 return result;
1784}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001785
Andy McFadden72841452013-03-01 16:25:32 -08001786EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1787 EGLnsecsANDROID time)
1788{
1789 clearError();
1790
1791 const egl_display_ptr dp = validate_display(dpy);
1792 if (!dp) {
1793 return EGL_FALSE;
1794 }
1795
1796 SurfaceRef _s(dp.get(), surface);
1797 if (!_s.get()) {
1798 setError(EGL_BAD_SURFACE, EGL_FALSE);
1799 return EGL_FALSE;
1800 }
1801
1802 egl_surface_t const * const s = get_surface(surface);
1803 native_window_set_buffers_timestamp(s->win.get(), time);
1804
1805 return EGL_TRUE;
1806}
1807
Craig Donner05249fc2016-01-15 19:33:55 -08001808EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1809{
1810 clearError();
1811
1812 int usage = 0;
1813 uint32_t width = 0;
1814 uint32_t height = 0;
1815 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001816 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001817 uint32_t red_size = 0;
1818 uint32_t green_size = 0;
1819 uint32_t blue_size = 0;
1820 uint32_t alpha_size = 0;
1821
Craig Donnerb40504a2016-06-03 17:54:25 -07001822#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001823 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001824 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001825 target = value; \
1826 } else { \
1827 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1828 } \
1829 break
1830
1831 if (attrib_list) {
1832 while (*attrib_list != EGL_NONE) {
1833 GLint attr = *attrib_list++;
1834 GLint value = *attrib_list++;
1835 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001836 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1837 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1838 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1839 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1840 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1841 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001842 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001843 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1844 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1845 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001846 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001847 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001848 usage |= GRALLOC_USAGE_HW_RENDER;
1849 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001850 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001851 usage |= GRALLOC_USAGE_HW_TEXTURE;
1852 }
Craig Donner05249fc2016-01-15 19:33:55 -08001853 break;
1854 default:
1855 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1856 }
1857 }
1858 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001859#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001860
1861 // Validate format.
1862 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1863 if (alpha_size == 8) {
1864 format = HAL_PIXEL_FORMAT_RGBA_8888;
1865 } else {
1866 format = HAL_PIXEL_FORMAT_RGB_888;
1867 }
1868 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1869 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001870 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001871 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001872 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001873 red_size, green_size, blue_size, alpha_size);
1874 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1875 }
1876
Craig Donner68671532016-07-18 10:19:54 -07001877#define CHECK_ERROR_CONDITION(message) \
1878 if (err != NO_ERROR) { \
1879 ALOGE(message); \
1880 goto error_condition; \
1881 }
1882
1883 // The holder is used to destroy the buffer if an error occurs.
1884 GraphicBuffer* gBuffer = new GraphicBuffer();
1885 sp<IServiceManager> sm = defaultServiceManager();
1886 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1887 sp<IBinder> allocator;
1888 Parcel sc_data, sc_reply, data, reply;
1889 status_t err = NO_ERROR;
1890 if (sm == NULL) {
1891 ALOGE("Unable to connect to ServiceManager");
1892 goto error_condition;
1893 }
1894
1895 // Obtain an allocator.
1896 if (surfaceFlinger == NULL) {
1897 ALOGE("Unable to connect to SurfaceFlinger");
1898 goto error_condition;
1899 }
1900 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1901 err = surfaceFlinger->transact(
1902 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1903 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1904 allocator = sc_reply.readStrongBinder();
1905
1906 if (allocator == NULL) {
1907 ALOGE("Unable to obtain an ISurfaceComposer");
1908 goto error_condition;
1909 }
1910 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1911 err = data.writeUint32(width);
1912 CHECK_ERROR_CONDITION("Unable to write width");
1913 err = data.writeUint32(height);
1914 CHECK_ERROR_CONDITION("Unable to write height");
1915 err = data.writeInt32(static_cast<int32_t>(format));
1916 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001917 err = data.writeUint32(layer_count);
1918 CHECK_ERROR_CONDITION("Unable to write layer count");
1919 err = data.writeUint32(usage);
Craig Donner68671532016-07-18 10:19:54 -07001920 CHECK_ERROR_CONDITION("Unable to write usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001921 err = data.writeUtf8AsUtf16(
1922 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1923 std::to_string(getpid()) + ']');
1924 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001925 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1926 &reply);
1927 CHECK_ERROR_CONDITION(
1928 "Unable to request buffer allocation from surface composer");
1929 err = reply.readInt32();
1930 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1931 err = reply.read(*gBuffer);
1932 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1933
1934 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001935 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001936 ALOGE("Unable to create native buffer "
1937 "{ w=%u, h=%u, f=%u, u=%#x, lc=%u}: %#x", width, height, format,
1938 usage, layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001939 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001940 }
Craig Donner6ebc46a2016-10-21 15:23:44 -07001941 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, u=%#x, lc=%u}",
1942 gBuffer, width, height, format, usage, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001943 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001944
1945#undef CHECK_ERROR_CONDITION
1946
1947error_condition:
1948 // Delete the buffer.
1949 sp<GraphicBuffer> holder(gBuffer);
1950 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001951}
1952
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001953// ----------------------------------------------------------------------------
1954// NVIDIA extensions
1955// ----------------------------------------------------------------------------
1956EGLuint64NV eglGetSystemTimeFrequencyNV()
1957{
1958 clearError();
1959
1960 if (egl_init_drivers() == EGL_FALSE) {
1961 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1962 }
1963
1964 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001965 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001966
Mathias Agopianada798b2012-02-13 17:09:30 -08001967 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1968 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001969 }
1970
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001971 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001972}
1973
1974EGLuint64NV eglGetSystemTimeNV()
1975{
1976 clearError();
1977
1978 if (egl_init_drivers() == EGL_FALSE) {
1979 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1980 }
1981
1982 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001983 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001984
Mathias Agopianada798b2012-02-13 17:09:30 -08001985 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1986 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001987 }
1988
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001989 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001990}
Dan Stozaa894d082015-02-19 15:27:36 -08001991
1992// ----------------------------------------------------------------------------
1993// Partial update extension
1994// ----------------------------------------------------------------------------
1995EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1996 EGLint *rects, EGLint n_rects)
1997{
1998 clearError();
1999
2000 const egl_display_ptr dp = validate_display(dpy);
2001 if (!dp) {
2002 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2003 return EGL_FALSE;
2004 }
2005
2006 SurfaceRef _s(dp.get(), surface);
2007 if (!_s.get()) {
2008 setError(EGL_BAD_SURFACE, EGL_FALSE);
2009 return EGL_FALSE;
2010 }
2011
2012 egl_surface_t const * const s = get_surface(surface);
2013 if (s->cnx->egl.eglSetDamageRegionKHR) {
2014 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2015 rects, n_rects);
2016 }
2017
2018 return EGL_FALSE;
2019}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002020
2021EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
2022 EGLint framesAgo, EGLint numTimestamps, const EGLint *timestamps,
2023 EGLnsecsANDROID *values)
2024{
2025 clearError();
2026
2027 const egl_display_ptr dp = validate_display(dpy);
2028 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002029 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002030 }
2031
2032 SurfaceRef _s(dp.get(), surface);
2033 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002034 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002035 }
2036
2037 egl_surface_t const * const s = get_surface(surface);
2038
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002039 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002040 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002041 }
2042
Brian Andersondbd0ea82016-07-22 09:38:59 -07002043 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002044 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002045 nsecs_t* latchTime = nullptr;
2046 nsecs_t* firstRefreshStartTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002047 nsecs_t* GLCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002048 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002049 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002050 nsecs_t* displayRetireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002051 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002052 nsecs_t* releaseTime = nullptr;
2053
2054 for (int i = 0; i < numTimestamps; i++) {
2055 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002056 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2057 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002058 break;
2059 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2060 acquireTime = &values[i];
2061 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002062 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2063 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002064 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002065 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2066 firstRefreshStartTime = &values[i];
2067 break;
2068 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2069 lastRefreshStartTime = &values[i];
2070 break;
2071 case EGL_FIRST_COMPOSITION_FINISHED_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002072 GLCompositionDoneTime = &values[i];
2073 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002074 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2075 displayPresentTime = &values[i];
2076 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002077 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2078 displayRetireTime = &values[i];
2079 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002080 case EGL_DEQUEUE_READY_TIME_ANDROID:
2081 dequeueReadyTime = &values[i];
2082 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002083 case EGL_READS_DONE_TIME_ANDROID:
2084 releaseTime = &values[i];
2085 break;
2086 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002087 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002088 }
2089 }
2090
2091 status_t ret = native_window_get_frame_timestamps(s->win.get(), framesAgo,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002092 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
2093 lastRefreshStartTime, GLCompositionDoneTime, displayPresentTime,
2094 displayRetireTime, dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002095
Brian Anderson069b3652016-07-22 10:32:47 -07002096 switch (ret) {
2097 case NO_ERROR:
2098 return EGL_TRUE;
2099 case NAME_NOT_FOUND:
2100 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002101 case INVALID_OPERATION:
2102 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002103 case BAD_VALUE:
2104 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2105 default:
2106 // This should not happen. Return an error that is not in the spec
2107 // so it's obvious something is very wrong.
2108 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002109 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002110}
2111
2112EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2113 EGLint timestamp)
2114{
2115 clearError();
2116
2117 const egl_display_ptr dp = validate_display(dpy);
2118 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002119 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002120 }
2121
2122 SurfaceRef _s(dp.get(), surface);
2123 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002124 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2125 }
2126
2127 egl_surface_t const * const s = get_surface(surface);
2128
2129 ANativeWindow* window = s->win.get();
2130 if (!window) {
2131 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002132 }
2133
2134 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002135#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Brian Andersondbd0ea82016-07-22 09:38:59 -07002136 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002137 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002138 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2139 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2140 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2141 case EGL_FIRST_COMPOSITION_FINISHED_TIME_ANDROID:
2142 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002143 case EGL_READS_DONE_TIME_ANDROID:
2144 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002145 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2146 int value = 0;
2147 window->query(window,
2148 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2149 return value == 0 ? EGL_FALSE : EGL_TRUE;
2150 }
2151 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2152 int value = 0;
2153 window->query(window,
2154 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2155 return value == 0 ? EGL_FALSE : EGL_TRUE;
2156 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002157#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002158 default:
2159 return EGL_FALSE;
2160 }
2161}