blob: 6328c9c1f3b49cd04c35ee8ca148f75fee2c9145 [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
Jesse Hallc07b5202013-07-04 12:08:16 -070019#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070020#include <ctype.h>
21#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
30#include <cutils/log.h>
31#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070033#include <cutils/properties.h>
34#include <cutils/memory.h>
35
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{
270 clearError();
271
Dan Stozac3289c42014-01-17 11:38:34 -0800272 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700273 if (index >= NUM_DISPLAYS) {
274 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
275 }
276
277 if (egl_init_drivers() == EGL_FALSE) {
278 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
279 }
280
281 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
282 return dpy;
283}
284
285// ----------------------------------------------------------------------------
286// Initialization
287// ----------------------------------------------------------------------------
288
289EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
290{
291 clearError();
292
Jesse Hallb29e5e82012-04-04 16:53:42 -0700293 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
295
296 EGLBoolean res = dp->initialize(major, minor);
297
298 return res;
299}
300
301EGLBoolean eglTerminate(EGLDisplay dpy)
302{
303 // NOTE: don't unload the drivers b/c some APIs can be called
304 // after eglTerminate() has been called. eglTerminate() only
305 // terminates an EGLDisplay, not a EGL itself.
306
307 clearError();
308
Jesse Hallb29e5e82012-04-04 16:53:42 -0700309 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700310 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
311
312 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800313
Mathias Agopian518ec112011-05-13 16:21:08 -0700314 return res;
315}
316
317// ----------------------------------------------------------------------------
318// configuration
319// ----------------------------------------------------------------------------
320
321EGLBoolean eglGetConfigs( EGLDisplay dpy,
322 EGLConfig *configs,
323 EGLint config_size, EGLint *num_config)
324{
325 clearError();
326
Jesse Hallb29e5e82012-04-04 16:53:42 -0700327 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700328 if (!dp) return EGL_FALSE;
329
Mathias Agopian7773c432012-02-13 20:06:08 -0800330 if (num_config==0) {
331 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700332 }
333
Mathias Agopian7773c432012-02-13 20:06:08 -0800334 EGLBoolean res = EGL_FALSE;
335 *num_config = 0;
336
337 egl_connection_t* const cnx = &gEGLImpl;
338 if (cnx->dso) {
339 res = cnx->egl.eglGetConfigs(
340 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700341 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800342
343 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700344}
345
346EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
347 EGLConfig *configs, EGLint config_size,
348 EGLint *num_config)
349{
350 clearError();
351
Jesse Hallb29e5e82012-04-04 16:53:42 -0700352 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700353 if (!dp) return EGL_FALSE;
354
355 if (num_config==0) {
356 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
357 }
358
Mathias Agopian518ec112011-05-13 16:21:08 -0700359 EGLBoolean res = EGL_FALSE;
360 *num_config = 0;
361
Mathias Agopianada798b2012-02-13 17:09:30 -0800362 egl_connection_t* const cnx = &gEGLImpl;
363 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700364 if (attrib_list) {
365 char value[PROPERTY_VALUE_MAX];
366 property_get("debug.egl.force_msaa", value, "false");
367
368 if (!strcmp(value, "true")) {
369 size_t attribCount = 0;
370 EGLint attrib = attrib_list[0];
371
372 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700373 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700374 const EGLint *attribRendererable = NULL;
375 const EGLint *attribCaveat = NULL;
376
377 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700378 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700379 while (attrib != EGL_NONE) {
380 attrib = attrib_list[attribCount];
381 switch (attrib) {
382 case EGL_RENDERABLE_TYPE:
383 attribRendererable = &attrib_list[attribCount];
384 break;
385 case EGL_CONFIG_CAVEAT:
386 attribCaveat = &attrib_list[attribCount];
387 break;
388 }
389 attribCount++;
390 }
391
392 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
393 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800394
Romain Guy1cffc802012-10-15 18:13:05 -0700395 // Insert 2 extra attributes to force-enable MSAA 4x
396 EGLint aaAttribs[attribCount + 4];
397 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
398 aaAttribs[1] = 1;
399 aaAttribs[2] = EGL_SAMPLES;
400 aaAttribs[3] = 4;
401
402 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
403
404 EGLint numConfigAA;
405 EGLBoolean resAA = cnx->egl.eglChooseConfig(
406 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
407
408 if (resAA == EGL_TRUE && numConfigAA > 0) {
409 ALOGD("Enabling MSAA 4x");
410 *num_config = numConfigAA;
411 return resAA;
412 }
413 }
414 }
415 }
416
Mathias Agopian7773c432012-02-13 20:06:08 -0800417 res = cnx->egl.eglChooseConfig(
418 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700419 }
420 return res;
421}
422
423EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
424 EGLint attribute, EGLint *value)
425{
426 clearError();
427
Jesse Hallb29e5e82012-04-04 16:53:42 -0700428 egl_connection_t* cnx = NULL;
429 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
430 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800431
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800433 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700434}
435
436// ----------------------------------------------------------------------------
437// surfaces
438// ----------------------------------------------------------------------------
439
Jesse Hallc2e41222013-08-08 13:40:22 -0700440// Turn linear formats into corresponding sRGB formats when colorspace is
441// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
442// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800443// the modification isn't possible, the original dataSpace is returned.
444static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
445 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700446 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800447 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700448 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800449 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700450 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800451 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700452}
453
Mathias Agopian518ec112011-05-13 16:21:08 -0700454EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
455 NativeWindowType window,
456 const EGLint *attrib_list)
457{
458 clearError();
459
Jesse Hallb29e5e82012-04-04 16:53:42 -0700460 egl_connection_t* cnx = NULL;
461 egl_display_ptr dp = validate_display_connection(dpy, cnx);
462 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800463 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700464
Andy McFaddend566ce32014-01-07 15:54:17 -0800465 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
466 if (result != OK) {
467 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
468 "failed (%#x) (already connected to another API?)",
469 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700470 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700471 }
472
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700473 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700474 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
475 // of our native format. So if sRGB gamma is requested, we have to
476 // modify the EGLconfig's format before setting the native window's
477 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800478
Romain Guyff415142016-12-13 16:51:25 -0800479 // TODO: Add support for HAL_PIXEL_FORMAT_RGBA_FP16
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700480 // by default, just pick RGBA_8888
481 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800482 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700483
484 EGLint a = 0;
485 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
486 if (a > 0) {
487 // alpha-channel requested, there's really only one suitable format
488 format = HAL_PIXEL_FORMAT_RGBA_8888;
489 } else {
490 EGLint r, g, b;
491 r = g = b = 0;
492 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
493 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
494 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
495 EGLint colorDepth = r + g + b;
496 if (colorDepth <= 16) {
497 format = HAL_PIXEL_FORMAT_RGB_565;
498 } else {
499 format = HAL_PIXEL_FORMAT_RGBX_8888;
500 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700501 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700502
503 // now select a corresponding sRGB format if needed
504 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
505 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
506 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530507 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700508 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700509 }
510 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800511
Jesse Hallc2e41222013-08-08 13:40:22 -0700512 if (format != 0) {
513 int err = native_window_set_buffers_format(window, format);
514 if (err != 0) {
515 ALOGE("error setting native window pixel format: %s (%d)",
516 strerror(-err), err);
517 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
518 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
519 }
520 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700521
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800522 if (dataSpace != 0) {
523 int err = native_window_set_buffers_data_space(window, dataSpace);
524 if (err != 0) {
525 ALOGE("error setting native window pixel dataSpace: %s (%d)",
526 strerror(-err), err);
527 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
528 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
529 }
530 }
531
Jamie Gennis59769462011-11-19 18:04:43 -0800532 // the EGL spec requires that a new EGLSurface default to swap interval
533 // 1, so explicitly set that on the window here.
534 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
535 anw->setSwapInterval(anw, 1);
536
Mathias Agopian518ec112011-05-13 16:21:08 -0700537 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800538 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700539 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700540 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
541 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700542 return s;
543 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700544
545 // EGLSurface creation failed
546 native_window_set_buffers_format(window, 0);
547 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700548 }
549 return EGL_NO_SURFACE;
550}
551
552EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
553 NativePixmapType pixmap,
554 const EGLint *attrib_list)
555{
556 clearError();
557
Jesse Hallb29e5e82012-04-04 16:53:42 -0700558 egl_connection_t* cnx = NULL;
559 egl_display_ptr dp = validate_display_connection(dpy, cnx);
560 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700561 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800562 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700563 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700564 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
565 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700566 return s;
567 }
568 }
569 return EGL_NO_SURFACE;
570}
571
572EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
573 const EGLint *attrib_list)
574{
575 clearError();
576
Jesse Hallb29e5e82012-04-04 16:53:42 -0700577 egl_connection_t* cnx = NULL;
578 egl_display_ptr dp = validate_display_connection(dpy, cnx);
579 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800581 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700582 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700583 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
584 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700585 return s;
586 }
587 }
588 return EGL_NO_SURFACE;
589}
Jesse Hall47743382013-02-08 11:13:46 -0800590
Mathias Agopian518ec112011-05-13 16:21:08 -0700591EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
592{
593 clearError();
594
Jesse Hallb29e5e82012-04-04 16:53:42 -0700595 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700596 if (!dp) return EGL_FALSE;
597
Jesse Hallb29e5e82012-04-04 16:53:42 -0700598 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700599 if (!_s.get())
600 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700601
602 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800603 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700604 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700605 _s.terminate();
606 }
607 return result;
608}
609
610EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
611 EGLint attribute, EGLint *value)
612{
613 clearError();
614
Jesse Hallb29e5e82012-04-04 16:53:42 -0700615 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700616 if (!dp) return EGL_FALSE;
617
Jesse Hallb29e5e82012-04-04 16:53:42 -0700618 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700619 if (!_s.get())
620 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700621
Mathias Agopian518ec112011-05-13 16:21:08 -0700622 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800623 return s->cnx->egl.eglQuerySurface(
624 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700625}
626
Jamie Gennise8696a42012-01-15 18:54:57 -0800627void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800628 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800629 clearError();
630
Jesse Hallb29e5e82012-04-04 16:53:42 -0700631 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800632 if (!dp) {
633 return;
634 }
635
Jesse Hallb29e5e82012-04-04 16:53:42 -0700636 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800637 if (!_s.get()) {
638 setError(EGL_BAD_SURFACE, EGL_FALSE);
639 return;
640 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800641}
642
Mathias Agopian518ec112011-05-13 16:21:08 -0700643// ----------------------------------------------------------------------------
644// Contexts
645// ----------------------------------------------------------------------------
646
647EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
648 EGLContext share_list, const EGLint *attrib_list)
649{
650 clearError();
651
Jesse Hallb29e5e82012-04-04 16:53:42 -0700652 egl_connection_t* cnx = NULL;
653 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700654 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700655 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700656 if (!ContextRef(dp.get(), share_list).get()) {
657 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
658 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700659 egl_context_t* const c = get_context(share_list);
660 share_list = c->context;
661 }
662 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800663 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700664 if (context != EGL_NO_CONTEXT) {
665 // figure out if it's a GLESv1 or GLESv2
666 int version = 0;
667 if (attrib_list) {
668 while (*attrib_list != EGL_NONE) {
669 GLint attr = *attrib_list++;
670 GLint value = *attrib_list++;
671 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
672 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800673 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800674 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800675 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700676 }
677 }
678 };
679 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700680 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
681 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700682 return c;
683 }
684 }
685 return EGL_NO_CONTEXT;
686}
687
688EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
689{
690 clearError();
691
Jesse Hallb29e5e82012-04-04 16:53:42 -0700692 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700693 if (!dp)
694 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700695
Jesse Hallb29e5e82012-04-04 16:53:42 -0700696 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700697 if (!_c.get())
698 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800699
Mathias Agopian518ec112011-05-13 16:21:08 -0700700 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800701 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700702 if (result == EGL_TRUE) {
703 _c.terminate();
704 }
705 return result;
706}
707
Mathias Agopian518ec112011-05-13 16:21:08 -0700708EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
709 EGLSurface read, EGLContext ctx)
710{
711 clearError();
712
Jesse Hallb29e5e82012-04-04 16:53:42 -0700713 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700714 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
715
Mathias Agopian5b287a62011-05-16 18:58:55 -0700716 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
717 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
718 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700719 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
720 (draw != EGL_NO_SURFACE) ) {
721 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
722 }
723
724 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700725 ContextRef _c(dp.get(), ctx);
726 SurfaceRef _d(dp.get(), draw);
727 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700728
729 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700730 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700731 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700732 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700733 }
734
735 // these are the underlying implementation's object
736 EGLContext impl_ctx = EGL_NO_CONTEXT;
737 EGLSurface impl_draw = EGL_NO_SURFACE;
738 EGLSurface impl_read = EGL_NO_SURFACE;
739
740 // these are our objects structs passed in
741 egl_context_t * c = NULL;
742 egl_surface_t const * d = NULL;
743 egl_surface_t const * r = NULL;
744
745 // these are the current objects structs
746 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800747
Mathias Agopian518ec112011-05-13 16:21:08 -0700748 if (ctx != EGL_NO_CONTEXT) {
749 c = get_context(ctx);
750 impl_ctx = c->context;
751 } else {
752 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700753 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
754 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
755 return setError(EGL_BAD_MATCH, EGL_FALSE);
756 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700757 if (cur_c == NULL) {
758 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700759 // not an error, there is just no current context.
760 return EGL_TRUE;
761 }
762 }
763
764 // retrieve the underlying implementation's draw EGLSurface
765 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700766 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700768 impl_draw = d->surface;
769 }
770
771 // retrieve the underlying implementation's read EGLSurface
772 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700773 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700774 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700775 impl_read = r->surface;
776 }
777
Mathias Agopian518ec112011-05-13 16:21:08 -0700778
Jesse Hallb29e5e82012-04-04 16:53:42 -0700779 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800780 draw, read, ctx,
781 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700782
783 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800784 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700785 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
786 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700787 _c.acquire();
788 _r.acquire();
789 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700790 } else {
791 setGLHooksThreadSpecific(&gHooksNoContext);
792 egl_tls_t::setContext(EGL_NO_CONTEXT);
793 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700794 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000795 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700796 egl_connection_t* const cnx = &gEGLImpl;
797 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700798 }
799 return result;
800}
801
802
803EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
804 EGLint attribute, EGLint *value)
805{
806 clearError();
807
Jesse Hallb29e5e82012-04-04 16:53:42 -0700808 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700809 if (!dp) return EGL_FALSE;
810
Jesse Hallb29e5e82012-04-04 16:53:42 -0700811 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700812 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
813
Mathias Agopian518ec112011-05-13 16:21:08 -0700814 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800815 return c->cnx->egl.eglQueryContext(
816 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700817
Mathias Agopian518ec112011-05-13 16:21:08 -0700818}
819
820EGLContext eglGetCurrentContext(void)
821{
822 // could be called before eglInitialize(), but we wouldn't have a context
823 // then, and this function would correctly return EGL_NO_CONTEXT.
824
825 clearError();
826
827 EGLContext ctx = getContext();
828 return ctx;
829}
830
831EGLSurface eglGetCurrentSurface(EGLint readdraw)
832{
833 // could be called before eglInitialize(), but we wouldn't have a context
834 // then, and this function would correctly return EGL_NO_SURFACE.
835
836 clearError();
837
838 EGLContext ctx = getContext();
839 if (ctx) {
840 egl_context_t const * const c = get_context(ctx);
841 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
842 switch (readdraw) {
843 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800844 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700845 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
846 }
847 }
848 return EGL_NO_SURFACE;
849}
850
851EGLDisplay eglGetCurrentDisplay(void)
852{
853 // could be called before eglInitialize(), but we wouldn't have a context
854 // then, and this function would correctly return EGL_NO_DISPLAY.
855
856 clearError();
857
858 EGLContext ctx = getContext();
859 if (ctx) {
860 egl_context_t const * const c = get_context(ctx);
861 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
862 return c->dpy;
863 }
864 return EGL_NO_DISPLAY;
865}
866
867EGLBoolean eglWaitGL(void)
868{
Mathias Agopian518ec112011-05-13 16:21:08 -0700869 clearError();
870
Mathias Agopianada798b2012-02-13 17:09:30 -0800871 egl_connection_t* const cnx = &gEGLImpl;
872 if (!cnx->dso)
873 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
874
875 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700876}
877
878EGLBoolean eglWaitNative(EGLint engine)
879{
Mathias Agopian518ec112011-05-13 16:21:08 -0700880 clearError();
881
Mathias Agopianada798b2012-02-13 17:09:30 -0800882 egl_connection_t* const cnx = &gEGLImpl;
883 if (!cnx->dso)
884 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
885
886 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700887}
888
889EGLint eglGetError(void)
890{
Mathias Agopianada798b2012-02-13 17:09:30 -0800891 EGLint err = EGL_SUCCESS;
892 egl_connection_t* const cnx = &gEGLImpl;
893 if (cnx->dso) {
894 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700895 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800896 if (err == EGL_SUCCESS) {
897 err = egl_tls_t::getError();
898 }
899 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700900}
901
Michael Chockc0ec5e22014-01-27 08:14:33 -0800902static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700903 const char* procname) {
904 const egl_connection_t* cnx = &gEGLImpl;
905 void* proc = NULL;
906
Michael Chockc0ec5e22014-01-27 08:14:33 -0800907 proc = dlsym(cnx->libEgl, procname);
908 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
909
Jesse Hallc07b5202013-07-04 12:08:16 -0700910 proc = dlsym(cnx->libGles2, procname);
911 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
912
913 proc = dlsym(cnx->libGles1, procname);
914 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
915
916 return NULL;
917}
918
Mathias Agopian518ec112011-05-13 16:21:08 -0700919__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
920{
921 // eglGetProcAddress() could be the very first function called
922 // in which case we must make sure we've initialized ourselves, this
923 // happens the first time egl_get_display() is called.
924
925 clearError();
926
927 if (egl_init_drivers() == EGL_FALSE) {
928 setError(EGL_BAD_PARAMETER, NULL);
929 return NULL;
930 }
931
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700932 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700933 return NULL;
934 }
935
Mathias Agopian518ec112011-05-13 16:21:08 -0700936 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700937 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700938 if (addr) return addr;
939
Michael Chockc0ec5e22014-01-27 08:14:33 -0800940 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700941 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700942
Mathias Agopian518ec112011-05-13 16:21:08 -0700943 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
944 pthread_mutex_lock(&sExtensionMapMutex);
945
946 /*
947 * Since eglGetProcAddress() is not associated to anything, it needs
948 * to return a function pointer that "works" regardless of what
949 * the current context is.
950 *
951 * For this reason, we return a "forwarder", a small stub that takes
952 * care of calling the function associated with the context
953 * currently bound.
954 *
955 * We first look for extensions we've already resolved, if we're seeing
956 * this extension for the first time, we go through all our
957 * implementations and call eglGetProcAddress() and record the
958 * result in the appropriate implementation hooks and return the
959 * address of the forwarder corresponding to that hook set.
960 *
961 */
962
963 const String8 name(procname);
964 addr = sGLExtentionMap.valueFor(name);
965 const int slot = sGLExtentionSlot;
966
Steve Blocke6f43dd2012-01-06 19:20:56 +0000967 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700968 "no more slots for eglGetProcAddress(\"%s\")",
969 procname);
970
971 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
972 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800973
974 egl_connection_t* const cnx = &gEGLImpl;
975 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800976 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800977 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800978 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
979 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800980 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800981 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700982 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800983
Mathias Agopian518ec112011-05-13 16:21:08 -0700984 if (found) {
985 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700986 sGLExtentionMap.add(name, addr);
987 sGLExtentionSlot++;
988 }
989 }
990
991 pthread_mutex_unlock(&sExtensionMapMutex);
992 return addr;
993}
994
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700995class FrameCompletionThread : public Thread {
996public:
997
998 static void queueSync(EGLSyncKHR sync) {
999 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1000 static bool running = false;
1001 if (!running) {
1002 thread->run("GPUFrameCompletion");
1003 running = true;
1004 }
1005 {
1006 Mutex::Autolock lock(thread->mMutex);
1007 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1008 thread->mFramesQueued).string());
1009 thread->mQueue.push_back(sync);
1010 thread->mCondition.signal();
1011 thread->mFramesQueued++;
1012 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1013 }
1014 }
1015
1016private:
1017 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1018
1019 virtual bool threadLoop() {
1020 EGLSyncKHR sync;
1021 uint32_t frameNum;
1022 {
1023 Mutex::Autolock lock(mMutex);
1024 while (mQueue.isEmpty()) {
1025 mCondition.wait(mMutex);
1026 }
1027 sync = mQueue[0];
1028 frameNum = mFramesCompleted;
1029 }
1030 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1031 {
1032 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1033 frameNum).string());
1034 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1035 if (result == EGL_FALSE) {
1036 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1037 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1038 ALOGE("FrameCompletion: timeout waiting for fence");
1039 }
1040 eglDestroySyncKHR(dpy, sync);
1041 }
1042 {
1043 Mutex::Autolock lock(mMutex);
1044 mQueue.removeAt(0);
1045 mFramesCompleted++;
1046 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1047 }
1048 return true;
1049 }
1050
1051 uint32_t mFramesQueued;
1052 uint32_t mFramesCompleted;
1053 Vector<EGLSyncKHR> mQueue;
1054 Condition mCondition;
1055 Mutex mMutex;
1056};
1057
Dan Stozaa894d082015-02-19 15:27:36 -08001058EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1059 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001060{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001061 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001062 clearError();
1063
Jesse Hallb29e5e82012-04-04 16:53:42 -07001064 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001065 if (!dp) return EGL_FALSE;
1066
Jesse Hallb29e5e82012-04-04 16:53:42 -07001067 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001068 if (!_s.get())
1069 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001070
Mathias Agopian518ec112011-05-13 16:21:08 -07001071 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001072
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001073 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1074 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1075 if (sync != EGL_NO_SYNC_KHR) {
1076 FrameCompletionThread::queueSync(sync);
1077 }
1078 }
1079
Mathias Agopian7db993a2012-03-25 00:49:46 -07001080 if (CC_UNLIKELY(dp->finishOnSwap)) {
1081 uint32_t pixel;
1082 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1083 if (c) {
1084 // glReadPixels() ensures that the frame is complete
1085 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1086 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1087 }
1088 }
1089
Dan Stozaa894d082015-02-19 15:27:36 -08001090 if (n_rects == 0) {
1091 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1092 }
1093
1094 Vector<android_native_rect_t> androidRects;
1095 for (int r = 0; r < n_rects; ++r) {
1096 int offset = r * 4;
1097 int x = rects[offset];
1098 int y = rects[offset + 1];
1099 int width = rects[offset + 2];
1100 int height = rects[offset + 3];
1101 android_native_rect_t androidRect;
1102 androidRect.left = x;
1103 androidRect.top = y + height;
1104 androidRect.right = x + width;
1105 androidRect.bottom = y;
1106 androidRects.push_back(androidRect);
1107 }
1108 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1109 androidRects.size());
1110
1111 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1112 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1113 rects, n_rects);
1114 } else {
1115 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1116 }
1117}
1118
1119EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1120{
1121 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001122}
1123
1124EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1125 NativePixmapType target)
1126{
1127 clearError();
1128
Jesse Hallb29e5e82012-04-04 16:53:42 -07001129 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001130 if (!dp) return EGL_FALSE;
1131
Jesse Hallb29e5e82012-04-04 16:53:42 -07001132 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001133 if (!_s.get())
1134 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001135
Mathias Agopian518ec112011-05-13 16:21:08 -07001136 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001137 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001138}
1139
1140const char* eglQueryString(EGLDisplay dpy, EGLint name)
1141{
1142 clearError();
1143
Chia-I Wue57d1352016-08-15 16:10:02 +08001144 // Generate an error quietly when client extensions (as defined by
1145 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1146 // validate_display to generate the error as validate_display would log
1147 // the error, which can be misleading.
1148 //
1149 // If we want to support EGL_EXT_client_extensions later, we can return
1150 // the client extension string here instead.
1151 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1152 return setErrorQuiet(EGL_BAD_DISPLAY, nullptr);
1153
Jesse Hallb29e5e82012-04-04 16:53:42 -07001154 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001155 if (!dp) return (const char *) NULL;
1156
1157 switch (name) {
1158 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001159 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001160 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001161 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001162 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001163 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001164 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001165 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001166 }
1167 return setError(EGL_BAD_PARAMETER, (const char *)0);
1168}
1169
Mathias Agopianca088332013-03-28 17:44:13 -07001170EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1171{
1172 clearError();
1173
1174 const egl_display_ptr dp = validate_display(dpy);
1175 if (!dp) return (const char *) NULL;
1176
1177 switch (name) {
1178 case EGL_VENDOR:
1179 return dp->disp.queryString.vendor;
1180 case EGL_VERSION:
1181 return dp->disp.queryString.version;
1182 case EGL_EXTENSIONS:
1183 return dp->disp.queryString.extensions;
1184 case EGL_CLIENT_APIS:
1185 return dp->disp.queryString.clientApi;
1186 }
1187 return setError(EGL_BAD_PARAMETER, (const char *)0);
1188}
Mathias Agopian518ec112011-05-13 16:21:08 -07001189
1190// ----------------------------------------------------------------------------
1191// EGL 1.1
1192// ----------------------------------------------------------------------------
1193
1194EGLBoolean eglSurfaceAttrib(
1195 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1196{
1197 clearError();
1198
Jesse Hallb29e5e82012-04-04 16:53:42 -07001199 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001200 if (!dp) return EGL_FALSE;
1201
Jesse Hallb29e5e82012-04-04 16:53:42 -07001202 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001203 if (!_s.get())
1204 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001205
Pablo Ceballosc18be292016-05-31 14:55:42 -07001206 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001207
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001208 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001209 if (!s->win.get()) {
1210 setError(EGL_BAD_SURFACE, EGL_FALSE);
1211 }
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001212 int err = native_window_set_auto_refresh(s->win.get(),
1213 value ? true : false);
1214 return (err == NO_ERROR) ? EGL_TRUE :
1215 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001216 }
1217
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001218#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001219 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001220 if (!s->win.get()) {
1221 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1222 }
Brian Anderson069b3652016-07-22 10:32:47 -07001223 int err = native_window_enable_frame_timestamps(
1224 s->win.get(), value ? true : false);
1225 return (err == NO_ERROR) ? EGL_TRUE :
1226 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001227 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001228#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001229
Mathias Agopian518ec112011-05-13 16:21:08 -07001230 if (s->cnx->egl.eglSurfaceAttrib) {
1231 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001232 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001233 }
1234 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1235}
1236
1237EGLBoolean eglBindTexImage(
1238 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1239{
1240 clearError();
1241
Jesse Hallb29e5e82012-04-04 16:53:42 -07001242 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001243 if (!dp) return EGL_FALSE;
1244
Jesse Hallb29e5e82012-04-04 16:53:42 -07001245 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001246 if (!_s.get())
1247 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001248
Mathias Agopian518ec112011-05-13 16:21:08 -07001249 egl_surface_t const * const s = get_surface(surface);
1250 if (s->cnx->egl.eglBindTexImage) {
1251 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001252 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001253 }
1254 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1255}
1256
1257EGLBoolean eglReleaseTexImage(
1258 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1259{
1260 clearError();
1261
Jesse Hallb29e5e82012-04-04 16:53:42 -07001262 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001263 if (!dp) return EGL_FALSE;
1264
Jesse Hallb29e5e82012-04-04 16:53:42 -07001265 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001266 if (!_s.get())
1267 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001268
Mathias Agopian518ec112011-05-13 16:21:08 -07001269 egl_surface_t const * const s = get_surface(surface);
1270 if (s->cnx->egl.eglReleaseTexImage) {
1271 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001272 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001273 }
1274 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1275}
1276
1277EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1278{
1279 clearError();
1280
Jesse Hallb29e5e82012-04-04 16:53:42 -07001281 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001282 if (!dp) return EGL_FALSE;
1283
1284 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001285 egl_connection_t* const cnx = &gEGLImpl;
1286 if (cnx->dso && cnx->egl.eglSwapInterval) {
1287 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001288 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001289
Mathias Agopian518ec112011-05-13 16:21:08 -07001290 return res;
1291}
1292
1293
1294// ----------------------------------------------------------------------------
1295// EGL 1.2
1296// ----------------------------------------------------------------------------
1297
1298EGLBoolean eglWaitClient(void)
1299{
1300 clearError();
1301
Mathias Agopianada798b2012-02-13 17:09:30 -08001302 egl_connection_t* const cnx = &gEGLImpl;
1303 if (!cnx->dso)
1304 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1305
1306 EGLBoolean res;
1307 if (cnx->egl.eglWaitClient) {
1308 res = cnx->egl.eglWaitClient();
1309 } else {
1310 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001311 }
1312 return res;
1313}
1314
1315EGLBoolean eglBindAPI(EGLenum api)
1316{
1317 clearError();
1318
1319 if (egl_init_drivers() == EGL_FALSE) {
1320 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1321 }
1322
1323 // bind this API on all EGLs
1324 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001325 egl_connection_t* const cnx = &gEGLImpl;
1326 if (cnx->dso && cnx->egl.eglBindAPI) {
1327 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001328 }
1329 return res;
1330}
1331
1332EGLenum eglQueryAPI(void)
1333{
1334 clearError();
1335
1336 if (egl_init_drivers() == EGL_FALSE) {
1337 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1338 }
1339
Mathias Agopianada798b2012-02-13 17:09:30 -08001340 egl_connection_t* const cnx = &gEGLImpl;
1341 if (cnx->dso && cnx->egl.eglQueryAPI) {
1342 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001343 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001344
Mathias Agopian518ec112011-05-13 16:21:08 -07001345 // or, it can only be OpenGL ES
1346 return EGL_OPENGL_ES_API;
1347}
1348
1349EGLBoolean eglReleaseThread(void)
1350{
1351 clearError();
1352
Mathias Agopianada798b2012-02-13 17:09:30 -08001353 egl_connection_t* const cnx = &gEGLImpl;
1354 if (cnx->dso && cnx->egl.eglReleaseThread) {
1355 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001356 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301357
1358 // If there is context bound to the thread, release it
1359 egl_display_t::loseCurrent(get_context(getContext()));
1360
Mathias Agopian518ec112011-05-13 16:21:08 -07001361 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001362 return EGL_TRUE;
1363}
1364
1365EGLSurface eglCreatePbufferFromClientBuffer(
1366 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1367 EGLConfig config, const EGLint *attrib_list)
1368{
1369 clearError();
1370
Jesse Hallb29e5e82012-04-04 16:53:42 -07001371 egl_connection_t* cnx = NULL;
1372 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1373 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001374 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1375 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001376 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001377 }
1378 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1379}
1380
1381// ----------------------------------------------------------------------------
1382// EGL_EGLEXT_VERSION 3
1383// ----------------------------------------------------------------------------
1384
1385EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1386 const EGLint *attrib_list)
1387{
1388 clearError();
1389
Jesse Hallb29e5e82012-04-04 16:53:42 -07001390 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001391 if (!dp) return EGL_FALSE;
1392
Jesse Hallb29e5e82012-04-04 16:53:42 -07001393 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001394 if (!_s.get())
1395 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001396
1397 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001398 if (s->cnx->egl.eglLockSurfaceKHR) {
1399 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001400 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001401 }
1402 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1403}
1404
1405EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1406{
1407 clearError();
1408
Jesse Hallb29e5e82012-04-04 16:53:42 -07001409 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001410 if (!dp) return EGL_FALSE;
1411
Jesse Hallb29e5e82012-04-04 16:53:42 -07001412 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001413 if (!_s.get())
1414 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001415
1416 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001417 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001418 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001419 }
1420 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1421}
1422
1423EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1424 EGLClientBuffer buffer, const EGLint *attrib_list)
1425{
1426 clearError();
1427
Jesse Hallb29e5e82012-04-04 16:53:42 -07001428 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001429 if (!dp) return EGL_NO_IMAGE_KHR;
1430
Jesse Hallb29e5e82012-04-04 16:53:42 -07001431 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001432 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001433
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001434 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1435 egl_connection_t* const cnx = &gEGLImpl;
1436 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1437 result = cnx->egl.eglCreateImageKHR(
1438 dp->disp.dpy,
1439 c ? c->context : EGL_NO_CONTEXT,
1440 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001441 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001442 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001443}
1444
1445EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1446{
1447 clearError();
1448
Jesse Hallb29e5e82012-04-04 16:53:42 -07001449 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450 if (!dp) return EGL_FALSE;
1451
Steven Holte646a5c52012-06-04 20:02:11 -07001452 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001453 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001454 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001455 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001456 }
Steven Holte646a5c52012-06-04 20:02:11 -07001457 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001458}
1459
1460// ----------------------------------------------------------------------------
1461// EGL_EGLEXT_VERSION 5
1462// ----------------------------------------------------------------------------
1463
1464
1465EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1466{
1467 clearError();
1468
Jesse Hallb29e5e82012-04-04 16:53:42 -07001469 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001470 if (!dp) return EGL_NO_SYNC_KHR;
1471
Mathias Agopian518ec112011-05-13 16:21:08 -07001472 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001473 egl_connection_t* const cnx = &gEGLImpl;
1474 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1475 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001476 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001477 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001478}
1479
1480EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1481{
1482 clearError();
1483
Jesse Hallb29e5e82012-04-04 16:53:42 -07001484 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001485 if (!dp) return EGL_FALSE;
1486
Mathias Agopian518ec112011-05-13 16:21:08 -07001487 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001488 egl_connection_t* const cnx = &gEGLImpl;
1489 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1490 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001491 }
1492 return result;
1493}
1494
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001495EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1496 clearError();
1497
1498 const egl_display_ptr dp = validate_display(dpy);
1499 if (!dp) return EGL_FALSE;
1500
1501 EGLBoolean result = EGL_FALSE;
1502 egl_connection_t* const cnx = &gEGLImpl;
1503 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1504 result = cnx->egl.eglSignalSyncKHR(
1505 dp->disp.dpy, sync, mode);
1506 }
1507 return result;
1508}
1509
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001510EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1511 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001512{
1513 clearError();
1514
Jesse Hallb29e5e82012-04-04 16:53:42 -07001515 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 if (!dp) return EGL_FALSE;
1517
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001518 EGLBoolean result = EGL_FALSE;
1519 egl_connection_t* const cnx = &gEGLImpl;
1520 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1521 result = cnx->egl.eglClientWaitSyncKHR(
1522 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001523 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001524 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001525}
1526
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001527EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1528 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001529{
1530 clearError();
1531
Jesse Hallb29e5e82012-04-04 16:53:42 -07001532 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001533 if (!dp) return EGL_FALSE;
1534
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001535 EGLBoolean result = EGL_FALSE;
1536 egl_connection_t* const cnx = &gEGLImpl;
1537 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1538 result = cnx->egl.eglGetSyncAttribKHR(
1539 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001540 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001541 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001542}
1543
Season Li000d88f2015-07-01 11:39:40 -07001544EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1545{
1546 clearError();
1547
1548 const egl_display_ptr dp = validate_display(dpy);
1549 if (!dp) return EGL_NO_STREAM_KHR;
1550
1551 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1552 egl_connection_t* const cnx = &gEGLImpl;
1553 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1554 result = cnx->egl.eglCreateStreamKHR(
1555 dp->disp.dpy, attrib_list);
1556 }
1557 return result;
1558}
1559
1560EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1561{
1562 clearError();
1563
1564 const egl_display_ptr dp = validate_display(dpy);
1565 if (!dp) return EGL_FALSE;
1566
1567 EGLBoolean result = EGL_FALSE;
1568 egl_connection_t* const cnx = &gEGLImpl;
1569 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1570 result = cnx->egl.eglDestroyStreamKHR(
1571 dp->disp.dpy, stream);
1572 }
1573 return result;
1574}
1575
1576EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1577 EGLenum attribute, EGLint value)
1578{
1579 clearError();
1580
1581 const egl_display_ptr dp = validate_display(dpy);
1582 if (!dp) return EGL_FALSE;
1583
1584 EGLBoolean result = EGL_FALSE;
1585 egl_connection_t* const cnx = &gEGLImpl;
1586 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1587 result = cnx->egl.eglStreamAttribKHR(
1588 dp->disp.dpy, stream, attribute, value);
1589 }
1590 return result;
1591}
1592
1593EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1594 EGLenum attribute, EGLint *value)
1595{
1596 clearError();
1597
1598 const egl_display_ptr dp = validate_display(dpy);
1599 if (!dp) return EGL_FALSE;
1600
1601 EGLBoolean result = EGL_FALSE;
1602 egl_connection_t* const cnx = &gEGLImpl;
1603 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1604 result = cnx->egl.eglQueryStreamKHR(
1605 dp->disp.dpy, stream, attribute, value);
1606 }
1607 return result;
1608}
1609
1610EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1611 EGLenum attribute, EGLuint64KHR *value)
1612{
1613 clearError();
1614
1615 const egl_display_ptr dp = validate_display(dpy);
1616 if (!dp) return EGL_FALSE;
1617
1618 EGLBoolean result = EGL_FALSE;
1619 egl_connection_t* const cnx = &gEGLImpl;
1620 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1621 result = cnx->egl.eglQueryStreamu64KHR(
1622 dp->disp.dpy, stream, attribute, value);
1623 }
1624 return result;
1625}
1626
1627EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1628 EGLenum attribute, EGLTimeKHR *value)
1629{
1630 clearError();
1631
1632 const egl_display_ptr dp = validate_display(dpy);
1633 if (!dp) return EGL_FALSE;
1634
1635 EGLBoolean result = EGL_FALSE;
1636 egl_connection_t* const cnx = &gEGLImpl;
1637 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1638 result = cnx->egl.eglQueryStreamTimeKHR(
1639 dp->disp.dpy, stream, attribute, value);
1640 }
1641 return result;
1642}
1643
1644EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1645 EGLStreamKHR stream, const EGLint *attrib_list)
1646{
1647 clearError();
1648
1649 egl_display_ptr dp = validate_display(dpy);
1650 if (!dp) return EGL_NO_SURFACE;
1651
1652 egl_connection_t* const cnx = &gEGLImpl;
1653 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1654 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1655 dp->disp.dpy, config, stream, attrib_list);
1656 if (surface != EGL_NO_SURFACE) {
1657 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1658 surface, cnx);
1659 return s;
1660 }
1661 }
1662 return EGL_NO_SURFACE;
1663}
1664
1665EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1666 EGLStreamKHR stream)
1667{
1668 clearError();
1669
1670 const egl_display_ptr dp = validate_display(dpy);
1671 if (!dp) return EGL_FALSE;
1672
1673 EGLBoolean result = EGL_FALSE;
1674 egl_connection_t* const cnx = &gEGLImpl;
1675 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1676 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1677 dp->disp.dpy, stream);
1678 }
1679 return result;
1680}
1681
1682EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1683 EGLStreamKHR stream)
1684{
1685 clearError();
1686
1687 const egl_display_ptr dp = validate_display(dpy);
1688 if (!dp) return EGL_FALSE;
1689
1690 EGLBoolean result = EGL_FALSE;
1691 egl_connection_t* const cnx = &gEGLImpl;
1692 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1693 result = cnx->egl.eglStreamConsumerAcquireKHR(
1694 dp->disp.dpy, stream);
1695 }
1696 return result;
1697}
1698
1699EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1700 EGLStreamKHR stream)
1701{
1702 clearError();
1703
1704 const egl_display_ptr dp = validate_display(dpy);
1705 if (!dp) return EGL_FALSE;
1706
1707 EGLBoolean result = EGL_FALSE;
1708 egl_connection_t* const cnx = &gEGLImpl;
1709 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1710 result = cnx->egl.eglStreamConsumerReleaseKHR(
1711 dp->disp.dpy, stream);
1712 }
1713 return result;
1714}
1715
1716EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1717 EGLDisplay dpy, EGLStreamKHR stream)
1718{
1719 clearError();
1720
1721 const egl_display_ptr dp = validate_display(dpy);
1722 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1723
1724 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1725 egl_connection_t* const cnx = &gEGLImpl;
1726 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1727 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1728 dp->disp.dpy, stream);
1729 }
1730 return result;
1731}
1732
1733EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1734 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1735{
1736 clearError();
1737
1738 const egl_display_ptr dp = validate_display(dpy);
1739 if (!dp) return EGL_NO_STREAM_KHR;
1740
1741 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1742 egl_connection_t* const cnx = &gEGLImpl;
1743 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1744 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1745 dp->disp.dpy, file_descriptor);
1746 }
1747 return result;
1748}
1749
Mathias Agopian518ec112011-05-13 16:21:08 -07001750// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001751// EGL_EGLEXT_VERSION 15
1752// ----------------------------------------------------------------------------
1753
1754EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1755 clearError();
1756 const egl_display_ptr dp = validate_display(dpy);
1757 if (!dp) return EGL_FALSE;
1758 EGLint result = EGL_FALSE;
1759 egl_connection_t* const cnx = &gEGLImpl;
1760 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1761 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1762 }
1763 return result;
1764}
1765
1766// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001767// ANDROID extensions
1768// ----------------------------------------------------------------------------
1769
Jamie Gennis331841b2012-09-06 14:52:00 -07001770EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1771{
1772 clearError();
1773
1774 const egl_display_ptr dp = validate_display(dpy);
1775 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1776
1777 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1778 egl_connection_t* const cnx = &gEGLImpl;
1779 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1780 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1781 }
1782 return result;
1783}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001784
Andy McFadden72841452013-03-01 16:25:32 -08001785EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1786 EGLnsecsANDROID time)
1787{
1788 clearError();
1789
1790 const egl_display_ptr dp = validate_display(dpy);
1791 if (!dp) {
1792 return EGL_FALSE;
1793 }
1794
1795 SurfaceRef _s(dp.get(), surface);
1796 if (!_s.get()) {
1797 setError(EGL_BAD_SURFACE, EGL_FALSE);
1798 return EGL_FALSE;
1799 }
1800
1801 egl_surface_t const * const s = get_surface(surface);
1802 native_window_set_buffers_timestamp(s->win.get(), time);
1803
1804 return EGL_TRUE;
1805}
1806
Craig Donner05249fc2016-01-15 19:33:55 -08001807EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1808{
1809 clearError();
1810
1811 int usage = 0;
1812 uint32_t width = 0;
1813 uint32_t height = 0;
1814 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001815 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001816 uint32_t red_size = 0;
1817 uint32_t green_size = 0;
1818 uint32_t blue_size = 0;
1819 uint32_t alpha_size = 0;
1820
Craig Donnerb40504a2016-06-03 17:54:25 -07001821#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001822 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001823 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001824 target = value; \
1825 } else { \
1826 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1827 } \
1828 break
1829
1830 if (attrib_list) {
1831 while (*attrib_list != EGL_NONE) {
1832 GLint attr = *attrib_list++;
1833 GLint value = *attrib_list++;
1834 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001835 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1836 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1837 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1838 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1839 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1840 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001841 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001842 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1843 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1844 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001845 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001846 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001847 usage |= GRALLOC_USAGE_HW_RENDER;
1848 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001849 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001850 usage |= GRALLOC_USAGE_HW_TEXTURE;
1851 }
Craig Donner05249fc2016-01-15 19:33:55 -08001852 break;
1853 default:
1854 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1855 }
1856 }
1857 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001858#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001859
1860 // Validate format.
1861 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1862 if (alpha_size == 8) {
1863 format = HAL_PIXEL_FORMAT_RGBA_8888;
1864 } else {
1865 format = HAL_PIXEL_FORMAT_RGB_888;
1866 }
1867 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1868 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001869 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001870 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001871 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001872 red_size, green_size, blue_size, alpha_size);
1873 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1874 }
1875
Craig Donner68671532016-07-18 10:19:54 -07001876#define CHECK_ERROR_CONDITION(message) \
1877 if (err != NO_ERROR) { \
1878 ALOGE(message); \
1879 goto error_condition; \
1880 }
1881
1882 // The holder is used to destroy the buffer if an error occurs.
1883 GraphicBuffer* gBuffer = new GraphicBuffer();
1884 sp<IServiceManager> sm = defaultServiceManager();
1885 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1886 sp<IBinder> allocator;
1887 Parcel sc_data, sc_reply, data, reply;
1888 status_t err = NO_ERROR;
1889 if (sm == NULL) {
1890 ALOGE("Unable to connect to ServiceManager");
1891 goto error_condition;
1892 }
1893
1894 // Obtain an allocator.
1895 if (surfaceFlinger == NULL) {
1896 ALOGE("Unable to connect to SurfaceFlinger");
1897 goto error_condition;
1898 }
1899 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1900 err = surfaceFlinger->transact(
1901 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1902 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1903 allocator = sc_reply.readStrongBinder();
1904
1905 if (allocator == NULL) {
1906 ALOGE("Unable to obtain an ISurfaceComposer");
1907 goto error_condition;
1908 }
1909 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1910 err = data.writeUint32(width);
1911 CHECK_ERROR_CONDITION("Unable to write width");
1912 err = data.writeUint32(height);
1913 CHECK_ERROR_CONDITION("Unable to write height");
1914 err = data.writeInt32(static_cast<int32_t>(format));
1915 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001916 err = data.writeUint32(layer_count);
1917 CHECK_ERROR_CONDITION("Unable to write layer count");
1918 err = data.writeUint32(usage);
Craig Donner68671532016-07-18 10:19:54 -07001919 CHECK_ERROR_CONDITION("Unable to write usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001920 err = data.writeUtf8AsUtf16(
1921 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1922 std::to_string(getpid()) + ']');
1923 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001924 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1925 &reply);
1926 CHECK_ERROR_CONDITION(
1927 "Unable to request buffer allocation from surface composer");
1928 err = reply.readInt32();
1929 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1930 err = reply.read(*gBuffer);
1931 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1932
1933 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001934 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001935 ALOGE("Unable to create native buffer "
1936 "{ w=%u, h=%u, f=%u, u=%#x, lc=%u}: %#x", width, height, format,
1937 usage, layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001938 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001939 }
Craig Donner6ebc46a2016-10-21 15:23:44 -07001940 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, u=%#x, lc=%u}",
1941 gBuffer, width, height, format, usage, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001942 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001943
1944#undef CHECK_ERROR_CONDITION
1945
1946error_condition:
1947 // Delete the buffer.
1948 sp<GraphicBuffer> holder(gBuffer);
1949 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001950}
1951
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001952// ----------------------------------------------------------------------------
1953// NVIDIA extensions
1954// ----------------------------------------------------------------------------
1955EGLuint64NV eglGetSystemTimeFrequencyNV()
1956{
1957 clearError();
1958
1959 if (egl_init_drivers() == EGL_FALSE) {
1960 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1961 }
1962
1963 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001964 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001965
Mathias Agopianada798b2012-02-13 17:09:30 -08001966 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1967 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001968 }
1969
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001970 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001971}
1972
1973EGLuint64NV eglGetSystemTimeNV()
1974{
1975 clearError();
1976
1977 if (egl_init_drivers() == EGL_FALSE) {
1978 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1979 }
1980
1981 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001982 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001983
Mathias Agopianada798b2012-02-13 17:09:30 -08001984 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1985 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001986 }
1987
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001988 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001989}
Dan Stozaa894d082015-02-19 15:27:36 -08001990
1991// ----------------------------------------------------------------------------
1992// Partial update extension
1993// ----------------------------------------------------------------------------
1994EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1995 EGLint *rects, EGLint n_rects)
1996{
1997 clearError();
1998
1999 const egl_display_ptr dp = validate_display(dpy);
2000 if (!dp) {
2001 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2002 return EGL_FALSE;
2003 }
2004
2005 SurfaceRef _s(dp.get(), surface);
2006 if (!_s.get()) {
2007 setError(EGL_BAD_SURFACE, EGL_FALSE);
2008 return EGL_FALSE;
2009 }
2010
2011 egl_surface_t const * const s = get_surface(surface);
2012 if (s->cnx->egl.eglSetDamageRegionKHR) {
2013 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2014 rects, n_rects);
2015 }
2016
2017 return EGL_FALSE;
2018}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002019
2020EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
2021 EGLint framesAgo, EGLint numTimestamps, const EGLint *timestamps,
2022 EGLnsecsANDROID *values)
2023{
2024 clearError();
2025
2026 const egl_display_ptr dp = validate_display(dpy);
2027 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002028 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002029 }
2030
2031 SurfaceRef _s(dp.get(), surface);
2032 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002033 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002034 }
2035
2036 egl_surface_t const * const s = get_surface(surface);
2037
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002038 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002039 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002040 }
2041
Brian Andersondbd0ea82016-07-22 09:38:59 -07002042 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002043 nsecs_t* acquireTime = nullptr;
2044 nsecs_t* refreshStartTime = nullptr;
2045 nsecs_t* GLCompositionDoneTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002046 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002047 nsecs_t* displayRetireTime = nullptr;
2048 nsecs_t* releaseTime = nullptr;
2049
2050 for (int i = 0; i < numTimestamps; i++) {
2051 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002052 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2053 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002054 break;
2055 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2056 acquireTime = &values[i];
2057 break;
2058 case EGL_COMPOSITION_START_TIME_ANDROID:
2059 refreshStartTime = &values[i];
2060 break;
2061 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
2062 GLCompositionDoneTime = &values[i];
2063 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002064 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2065 displayPresentTime = &values[i];
2066 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002067 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2068 displayRetireTime = &values[i];
2069 break;
2070 case EGL_READS_DONE_TIME_ANDROID:
2071 releaseTime = &values[i];
2072 break;
2073 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002074 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002075 }
2076 }
2077
2078 status_t ret = native_window_get_frame_timestamps(s->win.get(), framesAgo,
Brian Andersondbd0ea82016-07-22 09:38:59 -07002079 requestedPresentTime, acquireTime, refreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -07002080 GLCompositionDoneTime, displayPresentTime, displayRetireTime,
2081 releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002082
Brian Anderson069b3652016-07-22 10:32:47 -07002083 switch (ret) {
2084 case NO_ERROR:
2085 return EGL_TRUE;
2086 case NAME_NOT_FOUND:
2087 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002088 case INVALID_OPERATION:
2089 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002090 case BAD_VALUE:
2091 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2092 default:
2093 // This should not happen. Return an error that is not in the spec
2094 // so it's obvious something is very wrong.
2095 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002096 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002097}
2098
2099EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2100 EGLint timestamp)
2101{
2102 clearError();
2103
2104 const egl_display_ptr dp = validate_display(dpy);
2105 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002106 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002107 }
2108
2109 SurfaceRef _s(dp.get(), surface);
2110 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002111 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2112 }
2113
2114 egl_surface_t const * const s = get_surface(surface);
2115
2116 ANativeWindow* window = s->win.get();
2117 if (!window) {
2118 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002119 }
2120
2121 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002122#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Brian Andersondbd0ea82016-07-22 09:38:59 -07002123 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002124 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2125 case EGL_COMPOSITION_START_TIME_ANDROID:
2126 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002127 case EGL_READS_DONE_TIME_ANDROID:
2128 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002129 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2130 int value = 0;
2131 window->query(window,
2132 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2133 return value == 0 ? EGL_FALSE : EGL_TRUE;
2134 }
2135 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2136 int value = 0;
2137 window->query(window,
2138 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2139 return value == 0 ? EGL_FALSE : EGL_TRUE;
2140 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002141#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002142 default:
2143 return EGL_FALSE;
2144 }
2145}