blob: 7a0cce48b2868ffd11bd9b9413f528d1d2ad35cc [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 Donner05249fc2016-01-15 19:33:55 -080036#include <ui/GraphicBuffer.h>
37
Mathias Agopian518ec112011-05-13 16:21:08 -070038#include <utils/KeyedVector.h>
39#include <utils/SortedVector.h>
40#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080041#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070042
Mathias Agopian39c24a22013-04-04 23:17:56 -070043#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070044#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070045
46#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070047#include "egl_object.h"
48#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080049#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070050
51using namespace android;
52
Jesse Halla2ba4282013-09-14 21:00:14 -070053// This extension has not been ratified yet, so can't be shipped.
54// Implementation is incomplete and untested.
55#define ENABLE_EGL_KHR_GL_COLORSPACE 0
56
Mathias Agopian518ec112011-05-13 16:21:08 -070057// ----------------------------------------------------------------------------
58
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070059namespace android {
60
Mathias Agopian518ec112011-05-13 16:21:08 -070061struct extention_map_t {
62 const char* name;
63 __eglMustCastToProperFunctionPointerType address;
64};
65
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070066/*
Jesse Hall21558da2013-08-06 15:31:22 -070067 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070068 *
Jesse Hall21558da2013-08-06 15:31:22 -070069 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
70 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070071 *
Jesse Hall21558da2013-08-06 15:31:22 -070072 * The rest (gExtensionString) depend on support in the EGL driver, and are
73 * only available if the driver supports them. However, some of these must be
74 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080075 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070076 * the mandatory extensions are present and may not function properly if some
77 * are missing.
78 *
79 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070080 */
Jesse Hall21558da2013-08-06 15:31:22 -070081extern char const * const gBuiltinExtensionString =
82 "EGL_KHR_get_all_proc_addresses "
83 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080084 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080085 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080086 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070087 "EGL_ANDROID_get_frame_timestamps "
Jesse Hall21558da2013-08-06 15:31:22 -070088 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070089extern char const * const gExtensionString =
90 "EGL_KHR_image " // mandatory
91 "EGL_KHR_image_base " // mandatory
92 "EGL_KHR_image_pixmap "
93 "EGL_KHR_lock_surface "
Jesse Halla2ba4282013-09-14 21:00:14 -070094#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
Jesse Hallc2e41222013-08-08 13:40:22 -070095 "EGL_KHR_gl_colorspace "
Jesse Halla2ba4282013-09-14 21:00:14 -070096#endif
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070098 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070099 "EGL_KHR_gl_texture_cubemap_image "
100 "EGL_KHR_gl_renderbuffer_image "
101 "EGL_KHR_reusable_sync "
102 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700103 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700104 "EGL_KHR_config_attribs "
105 "EGL_KHR_surfaceless_context "
106 "EGL_KHR_stream "
107 "EGL_KHR_stream_fifo "
108 "EGL_KHR_stream_producer_eglsurface "
109 "EGL_KHR_stream_consumer_gltexture "
110 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700111 "EGL_EXT_create_context_robustness "
112 "EGL_NV_system_time "
113 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700114 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700115 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800116 "EGL_KHR_partial_update " // strongly recommended
117 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700118 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700119 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700120 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700121 "EGL_EXT_protected_content "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700122 ;
123
124// extensions not exposed to applications but used by the ANDROID system
125// "EGL_ANDROID_blob_cache " // strongly recommended
126// "EGL_IMG_hibernate_process " // optional
127// "EGL_ANDROID_native_fence_sync " // strongly recommended
128// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700129// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700130
131/*
132 * EGL Extensions entry-points exposed to 3rd party applications
133 * (keep in sync with gExtensionString above)
134 *
135 */
136static const extention_map_t sExtensionMap[] = {
137 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700138 { "eglLockSurfaceKHR",
139 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
140 { "eglUnlockSurfaceKHR",
141 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700142
143 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700144 { "eglCreateImageKHR",
145 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
146 { "eglDestroyImageKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700148
149 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
150 { "eglCreateSyncKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
152 { "eglDestroySyncKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
154 { "eglClientWaitSyncKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
156 { "eglSignalSyncKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
158 { "eglGetSyncAttribKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
160
161 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800162 { "eglGetSystemTimeFrequencyNV",
163 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
164 { "eglGetSystemTimeNV",
165 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700166
Mathias Agopian2bb71682013-03-27 17:32:41 -0700167 // EGL_KHR_wait_sync
168 { "eglWaitSyncKHR",
169 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700170
171 // EGL_ANDROID_presentation_time
172 { "eglPresentationTimeANDROID",
173 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800174
175 // EGL_KHR_swap_buffers_with_damage
176 { "eglSwapBuffersWithDamageKHR",
177 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
178
Craig Donner05249fc2016-01-15 19:33:55 -0800179 // EGL_ANDROID_native_client_buffer
180 { "eglCreateNativeClientBufferANDROID",
181 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
182
Dan Stozaa894d082015-02-19 15:27:36 -0800183 // EGL_KHR_partial_update
184 { "eglSetDamageRegionKHR",
185 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700186
187 { "eglCreateStreamKHR",
188 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
189 { "eglDestroyStreamKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
191 { "eglStreamAttribKHR",
192 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
193 { "eglQueryStreamKHR",
194 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
195 { "eglQueryStreamu64KHR",
196 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
197 { "eglQueryStreamTimeKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
199 { "eglCreateStreamProducerSurfaceKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
201 { "eglStreamConsumerGLTextureExternalKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
203 { "eglStreamConsumerAcquireKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
205 { "eglStreamConsumerReleaseKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
207 { "eglGetStreamFileDescriptorKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
209 { "eglCreateStreamFromFileDescriptorKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700211
212 // EGL_ANDROID_get_frame_timestamps
213 { "eglGetFrameTimestampsANDROID",
214 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
215 { "eglQueryTimestampSupportedANDROID",
216 (__eglMustCastToProperFunctionPointerType)&eglQueryTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700217};
218
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700219/*
220 * These extensions entry-points should not be exposed to applications.
221 * They're used internally by the Android EGL layer.
222 */
223#define FILTER_EXTENSIONS(procname) \
224 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
225 !strcmp((procname), "eglHibernateProcessIMG") || \
226 !strcmp((procname), "eglAwakenProcessIMG") || \
227 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
228
229
230
Mathias Agopian518ec112011-05-13 16:21:08 -0700231// accesses protected by sExtensionMapMutex
232static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
233static int sGLExtentionSlot = 0;
234static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
235
236static void(*findProcAddress(const char* name,
237 const extention_map_t* map, size_t n))() {
238 for (uint32_t i=0 ; i<n ; i++) {
239 if (!strcmp(name, map[i].name)) {
240 return map[i].address;
241 }
242 }
243 return NULL;
244}
245
246// ----------------------------------------------------------------------------
247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
249extern EGLBoolean egl_init_drivers();
250extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700251extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700252
Mathias Agopian518ec112011-05-13 16:21:08 -0700253} // namespace android;
254
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700255
Mathias Agopian518ec112011-05-13 16:21:08 -0700256// ----------------------------------------------------------------------------
257
258static inline void clearError() { egl_tls_t::clearError(); }
259static inline EGLContext getContext() { return egl_tls_t::getContext(); }
260
261// ----------------------------------------------------------------------------
262
263EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
264{
265 clearError();
266
Dan Stozac3289c42014-01-17 11:38:34 -0800267 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700268 if (index >= NUM_DISPLAYS) {
269 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
270 }
271
272 if (egl_init_drivers() == EGL_FALSE) {
273 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
274 }
275
276 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
277 return dpy;
278}
279
280// ----------------------------------------------------------------------------
281// Initialization
282// ----------------------------------------------------------------------------
283
284EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
285{
286 clearError();
287
Jesse Hallb29e5e82012-04-04 16:53:42 -0700288 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700289 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
290
291 EGLBoolean res = dp->initialize(major, minor);
292
293 return res;
294}
295
296EGLBoolean eglTerminate(EGLDisplay dpy)
297{
298 // NOTE: don't unload the drivers b/c some APIs can be called
299 // after eglTerminate() has been called. eglTerminate() only
300 // terminates an EGLDisplay, not a EGL itself.
301
302 clearError();
303
Jesse Hallb29e5e82012-04-04 16:53:42 -0700304 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700305 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
306
307 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800308
Mathias Agopian518ec112011-05-13 16:21:08 -0700309 return res;
310}
311
312// ----------------------------------------------------------------------------
313// configuration
314// ----------------------------------------------------------------------------
315
316EGLBoolean eglGetConfigs( EGLDisplay dpy,
317 EGLConfig *configs,
318 EGLint config_size, EGLint *num_config)
319{
320 clearError();
321
Jesse Hallb29e5e82012-04-04 16:53:42 -0700322 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700323 if (!dp) return EGL_FALSE;
324
Mathias Agopian7773c432012-02-13 20:06:08 -0800325 if (num_config==0) {
326 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700327 }
328
Mathias Agopian7773c432012-02-13 20:06:08 -0800329 EGLBoolean res = EGL_FALSE;
330 *num_config = 0;
331
332 egl_connection_t* const cnx = &gEGLImpl;
333 if (cnx->dso) {
334 res = cnx->egl.eglGetConfigs(
335 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700336 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800337
338 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700339}
340
341EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
342 EGLConfig *configs, EGLint config_size,
343 EGLint *num_config)
344{
345 clearError();
346
Jesse Hallb29e5e82012-04-04 16:53:42 -0700347 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700348 if (!dp) return EGL_FALSE;
349
350 if (num_config==0) {
351 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
352 }
353
Mathias Agopian518ec112011-05-13 16:21:08 -0700354 EGLBoolean res = EGL_FALSE;
355 *num_config = 0;
356
Mathias Agopianada798b2012-02-13 17:09:30 -0800357 egl_connection_t* const cnx = &gEGLImpl;
358 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700359 if (attrib_list) {
360 char value[PROPERTY_VALUE_MAX];
361 property_get("debug.egl.force_msaa", value, "false");
362
363 if (!strcmp(value, "true")) {
364 size_t attribCount = 0;
365 EGLint attrib = attrib_list[0];
366
367 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700368 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700369 const EGLint *attribRendererable = NULL;
370 const EGLint *attribCaveat = NULL;
371
372 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700373 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700374 while (attrib != EGL_NONE) {
375 attrib = attrib_list[attribCount];
376 switch (attrib) {
377 case EGL_RENDERABLE_TYPE:
378 attribRendererable = &attrib_list[attribCount];
379 break;
380 case EGL_CONFIG_CAVEAT:
381 attribCaveat = &attrib_list[attribCount];
382 break;
383 }
384 attribCount++;
385 }
386
387 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
388 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800389
Romain Guy1cffc802012-10-15 18:13:05 -0700390 // Insert 2 extra attributes to force-enable MSAA 4x
391 EGLint aaAttribs[attribCount + 4];
392 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
393 aaAttribs[1] = 1;
394 aaAttribs[2] = EGL_SAMPLES;
395 aaAttribs[3] = 4;
396
397 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
398
399 EGLint numConfigAA;
400 EGLBoolean resAA = cnx->egl.eglChooseConfig(
401 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
402
403 if (resAA == EGL_TRUE && numConfigAA > 0) {
404 ALOGD("Enabling MSAA 4x");
405 *num_config = numConfigAA;
406 return resAA;
407 }
408 }
409 }
410 }
411
Mathias Agopian7773c432012-02-13 20:06:08 -0800412 res = cnx->egl.eglChooseConfig(
413 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700414 }
415 return res;
416}
417
418EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
419 EGLint attribute, EGLint *value)
420{
421 clearError();
422
Jesse Hallb29e5e82012-04-04 16:53:42 -0700423 egl_connection_t* cnx = NULL;
424 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
425 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800426
Mathias Agopian518ec112011-05-13 16:21:08 -0700427 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800428 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700429}
430
431// ----------------------------------------------------------------------------
432// surfaces
433// ----------------------------------------------------------------------------
434
Jesse Halla2ba4282013-09-14 21:00:14 -0700435// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
Jesse Hallc2e41222013-08-08 13:40:22 -0700436// been added to the Khronos egl.h.
437#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE
438#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB
439#define EGL_GL_COLORSPACE_LINEAR_KHR EGL_VG_COLORSPACE_LINEAR
440
441// 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
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) {
Jesse Halla2ba4282013-09-14 21:00:14 -0700507 if (ENABLE_EGL_KHR_GL_COLORSPACE) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800508 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jesse Halla2ba4282013-09-14 21:00:14 -0700509 } else {
510 // Normally we'd pass through unhandled attributes to
511 // the driver. But in case the driver implements this
512 // extension but we're disabling it, we want to prevent
513 // it getting through -- support will be broken without
514 // our help.
515 ALOGE("sRGB window surfaces not supported");
516 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
517 }
Jamie Gennisbee205f2011-07-01 13:12:07 -0700518 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700519 }
520 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800521
Jesse Hallc2e41222013-08-08 13:40:22 -0700522 if (format != 0) {
523 int err = native_window_set_buffers_format(window, format);
524 if (err != 0) {
525 ALOGE("error setting native window pixel format: %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 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700531
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800532 if (dataSpace != 0) {
533 int err = native_window_set_buffers_data_space(window, dataSpace);
534 if (err != 0) {
535 ALOGE("error setting native window pixel dataSpace: %s (%d)",
536 strerror(-err), err);
537 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
538 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
539 }
540 }
541
Jamie Gennis59769462011-11-19 18:04:43 -0800542 // the EGL spec requires that a new EGLSurface default to swap interval
543 // 1, so explicitly set that on the window here.
544 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
545 anw->setSwapInterval(anw, 1);
546
Mathias Agopian518ec112011-05-13 16:21:08 -0700547 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800548 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700549 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700550 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
551 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700552 return s;
553 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700554
555 // EGLSurface creation failed
556 native_window_set_buffers_format(window, 0);
557 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700558 }
559 return EGL_NO_SURFACE;
560}
561
562EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
563 NativePixmapType pixmap,
564 const EGLint *attrib_list)
565{
566 clearError();
567
Jesse Hallb29e5e82012-04-04 16:53:42 -0700568 egl_connection_t* cnx = NULL;
569 egl_display_ptr dp = validate_display_connection(dpy, cnx);
570 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700571 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800572 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700573 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700574 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
575 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700576 return s;
577 }
578 }
579 return EGL_NO_SURFACE;
580}
581
582EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
583 const EGLint *attrib_list)
584{
585 clearError();
586
Jesse Hallb29e5e82012-04-04 16:53:42 -0700587 egl_connection_t* cnx = NULL;
588 egl_display_ptr dp = validate_display_connection(dpy, cnx);
589 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700590 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800591 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700592 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700593 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
594 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700595 return s;
596 }
597 }
598 return EGL_NO_SURFACE;
599}
Jesse Hall47743382013-02-08 11:13:46 -0800600
Mathias Agopian518ec112011-05-13 16:21:08 -0700601EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
602{
603 clearError();
604
Jesse Hallb29e5e82012-04-04 16:53:42 -0700605 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700606 if (!dp) return EGL_FALSE;
607
Jesse Hallb29e5e82012-04-04 16:53:42 -0700608 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700609 if (!_s.get())
610 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700611
612 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800613 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700614 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700615 _s.terminate();
616 }
617 return result;
618}
619
620EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
621 EGLint attribute, EGLint *value)
622{
623 clearError();
624
Jesse Hallb29e5e82012-04-04 16:53:42 -0700625 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700626 if (!dp) return EGL_FALSE;
627
Jesse Hallb29e5e82012-04-04 16:53:42 -0700628 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700629 if (!_s.get())
630 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700631
Mathias Agopian518ec112011-05-13 16:21:08 -0700632 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800633 return s->cnx->egl.eglQuerySurface(
634 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700635}
636
Jamie Gennise8696a42012-01-15 18:54:57 -0800637void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800638 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800639 clearError();
640
Jesse Hallb29e5e82012-04-04 16:53:42 -0700641 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800642 if (!dp) {
643 return;
644 }
645
Jesse Hallb29e5e82012-04-04 16:53:42 -0700646 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800647 if (!_s.get()) {
648 setError(EGL_BAD_SURFACE, EGL_FALSE);
649 return;
650 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800651}
652
Mathias Agopian518ec112011-05-13 16:21:08 -0700653// ----------------------------------------------------------------------------
654// Contexts
655// ----------------------------------------------------------------------------
656
657EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
658 EGLContext share_list, const EGLint *attrib_list)
659{
660 clearError();
661
Jesse Hallb29e5e82012-04-04 16:53:42 -0700662 egl_connection_t* cnx = NULL;
663 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700664 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700665 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700666 if (!ContextRef(dp.get(), share_list).get()) {
667 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
668 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700669 egl_context_t* const c = get_context(share_list);
670 share_list = c->context;
671 }
672 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800673 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700674 if (context != EGL_NO_CONTEXT) {
675 // figure out if it's a GLESv1 or GLESv2
676 int version = 0;
677 if (attrib_list) {
678 while (*attrib_list != EGL_NONE) {
679 GLint attr = *attrib_list++;
680 GLint value = *attrib_list++;
681 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
682 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800683 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800684 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800685 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700686 }
687 }
688 };
689 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700690 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
691 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700692 return c;
693 }
694 }
695 return EGL_NO_CONTEXT;
696}
697
698EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
699{
700 clearError();
701
Jesse Hallb29e5e82012-04-04 16:53:42 -0700702 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700703 if (!dp)
704 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700705
Jesse Hallb29e5e82012-04-04 16:53:42 -0700706 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700707 if (!_c.get())
708 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800709
Mathias Agopian518ec112011-05-13 16:21:08 -0700710 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800711 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700712 if (result == EGL_TRUE) {
713 _c.terminate();
714 }
715 return result;
716}
717
Mathias Agopian518ec112011-05-13 16:21:08 -0700718EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
719 EGLSurface read, EGLContext ctx)
720{
721 clearError();
722
Jesse Hallb29e5e82012-04-04 16:53:42 -0700723 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700724 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
725
Mathias Agopian5b287a62011-05-16 18:58:55 -0700726 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
727 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
728 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700729 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
730 (draw != EGL_NO_SURFACE) ) {
731 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
732 }
733
734 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700735 ContextRef _c(dp.get(), ctx);
736 SurfaceRef _d(dp.get(), draw);
737 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700738
739 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700740 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700741 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700742 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700743 }
744
745 // these are the underlying implementation's object
746 EGLContext impl_ctx = EGL_NO_CONTEXT;
747 EGLSurface impl_draw = EGL_NO_SURFACE;
748 EGLSurface impl_read = EGL_NO_SURFACE;
749
750 // these are our objects structs passed in
751 egl_context_t * c = NULL;
752 egl_surface_t const * d = NULL;
753 egl_surface_t const * r = NULL;
754
755 // these are the current objects structs
756 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800757
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 if (ctx != EGL_NO_CONTEXT) {
759 c = get_context(ctx);
760 impl_ctx = c->context;
761 } else {
762 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700763 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
764 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
765 return setError(EGL_BAD_MATCH, EGL_FALSE);
766 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 if (cur_c == NULL) {
768 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 // not an error, there is just no current context.
770 return EGL_TRUE;
771 }
772 }
773
774 // retrieve the underlying implementation's draw EGLSurface
775 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700776 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700777 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700778 impl_draw = d->surface;
779 }
780
781 // retrieve the underlying implementation's read EGLSurface
782 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700783 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700784 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700785 impl_read = r->surface;
786 }
787
Mathias Agopian518ec112011-05-13 16:21:08 -0700788
Jesse Hallb29e5e82012-04-04 16:53:42 -0700789 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800790 draw, read, ctx,
791 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700792
793 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800794 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700795 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
796 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700797 _c.acquire();
798 _r.acquire();
799 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700800 } else {
801 setGLHooksThreadSpecific(&gHooksNoContext);
802 egl_tls_t::setContext(EGL_NO_CONTEXT);
803 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700804 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000805 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700806 egl_connection_t* const cnx = &gEGLImpl;
807 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700808 }
809 return result;
810}
811
812
813EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
814 EGLint attribute, EGLint *value)
815{
816 clearError();
817
Jesse Hallb29e5e82012-04-04 16:53:42 -0700818 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700819 if (!dp) return EGL_FALSE;
820
Jesse Hallb29e5e82012-04-04 16:53:42 -0700821 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700822 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
823
Mathias Agopian518ec112011-05-13 16:21:08 -0700824 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800825 return c->cnx->egl.eglQueryContext(
826 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700827
Mathias Agopian518ec112011-05-13 16:21:08 -0700828}
829
830EGLContext eglGetCurrentContext(void)
831{
832 // could be called before eglInitialize(), but we wouldn't have a context
833 // then, and this function would correctly return EGL_NO_CONTEXT.
834
835 clearError();
836
837 EGLContext ctx = getContext();
838 return ctx;
839}
840
841EGLSurface eglGetCurrentSurface(EGLint readdraw)
842{
843 // could be called before eglInitialize(), but we wouldn't have a context
844 // then, and this function would correctly return EGL_NO_SURFACE.
845
846 clearError();
847
848 EGLContext ctx = getContext();
849 if (ctx) {
850 egl_context_t const * const c = get_context(ctx);
851 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
852 switch (readdraw) {
853 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800854 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700855 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
856 }
857 }
858 return EGL_NO_SURFACE;
859}
860
861EGLDisplay eglGetCurrentDisplay(void)
862{
863 // could be called before eglInitialize(), but we wouldn't have a context
864 // then, and this function would correctly return EGL_NO_DISPLAY.
865
866 clearError();
867
868 EGLContext ctx = getContext();
869 if (ctx) {
870 egl_context_t const * const c = get_context(ctx);
871 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
872 return c->dpy;
873 }
874 return EGL_NO_DISPLAY;
875}
876
877EGLBoolean eglWaitGL(void)
878{
Mathias Agopian518ec112011-05-13 16:21:08 -0700879 clearError();
880
Mathias Agopianada798b2012-02-13 17:09:30 -0800881 egl_connection_t* const cnx = &gEGLImpl;
882 if (!cnx->dso)
883 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
884
885 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700886}
887
888EGLBoolean eglWaitNative(EGLint engine)
889{
Mathias Agopian518ec112011-05-13 16:21:08 -0700890 clearError();
891
Mathias Agopianada798b2012-02-13 17:09:30 -0800892 egl_connection_t* const cnx = &gEGLImpl;
893 if (!cnx->dso)
894 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
895
896 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700897}
898
899EGLint eglGetError(void)
900{
Mathias Agopianada798b2012-02-13 17:09:30 -0800901 EGLint err = EGL_SUCCESS;
902 egl_connection_t* const cnx = &gEGLImpl;
903 if (cnx->dso) {
904 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700905 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800906 if (err == EGL_SUCCESS) {
907 err = egl_tls_t::getError();
908 }
909 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700910}
911
Michael Chockc0ec5e22014-01-27 08:14:33 -0800912static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700913 const char* procname) {
914 const egl_connection_t* cnx = &gEGLImpl;
915 void* proc = NULL;
916
Michael Chockc0ec5e22014-01-27 08:14:33 -0800917 proc = dlsym(cnx->libEgl, procname);
918 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
919
Jesse Hallc07b5202013-07-04 12:08:16 -0700920 proc = dlsym(cnx->libGles2, procname);
921 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
922
923 proc = dlsym(cnx->libGles1, procname);
924 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
925
926 return NULL;
927}
928
Mathias Agopian518ec112011-05-13 16:21:08 -0700929__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
930{
931 // eglGetProcAddress() could be the very first function called
932 // in which case we must make sure we've initialized ourselves, this
933 // happens the first time egl_get_display() is called.
934
935 clearError();
936
937 if (egl_init_drivers() == EGL_FALSE) {
938 setError(EGL_BAD_PARAMETER, NULL);
939 return NULL;
940 }
941
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700942 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700943 return NULL;
944 }
945
Mathias Agopian518ec112011-05-13 16:21:08 -0700946 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700947 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700948 if (addr) return addr;
949
Michael Chockc0ec5e22014-01-27 08:14:33 -0800950 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700951 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700952
Mathias Agopian518ec112011-05-13 16:21:08 -0700953 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
954 pthread_mutex_lock(&sExtensionMapMutex);
955
956 /*
957 * Since eglGetProcAddress() is not associated to anything, it needs
958 * to return a function pointer that "works" regardless of what
959 * the current context is.
960 *
961 * For this reason, we return a "forwarder", a small stub that takes
962 * care of calling the function associated with the context
963 * currently bound.
964 *
965 * We first look for extensions we've already resolved, if we're seeing
966 * this extension for the first time, we go through all our
967 * implementations and call eglGetProcAddress() and record the
968 * result in the appropriate implementation hooks and return the
969 * address of the forwarder corresponding to that hook set.
970 *
971 */
972
973 const String8 name(procname);
974 addr = sGLExtentionMap.valueFor(name);
975 const int slot = sGLExtentionSlot;
976
Steve Blocke6f43dd2012-01-06 19:20:56 +0000977 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700978 "no more slots for eglGetProcAddress(\"%s\")",
979 procname);
980
981 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
982 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800983
984 egl_connection_t* const cnx = &gEGLImpl;
985 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800986 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800987 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800988 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
989 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800990 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800991 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700992 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800993
Mathias Agopian518ec112011-05-13 16:21:08 -0700994 if (found) {
995 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700996 sGLExtentionMap.add(name, addr);
997 sGLExtentionSlot++;
998 }
999 }
1000
1001 pthread_mutex_unlock(&sExtensionMapMutex);
1002 return addr;
1003}
1004
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001005class FrameCompletionThread : public Thread {
1006public:
1007
1008 static void queueSync(EGLSyncKHR sync) {
1009 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1010 static bool running = false;
1011 if (!running) {
1012 thread->run("GPUFrameCompletion");
1013 running = true;
1014 }
1015 {
1016 Mutex::Autolock lock(thread->mMutex);
1017 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1018 thread->mFramesQueued).string());
1019 thread->mQueue.push_back(sync);
1020 thread->mCondition.signal();
1021 thread->mFramesQueued++;
1022 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1023 }
1024 }
1025
1026private:
1027 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1028
1029 virtual bool threadLoop() {
1030 EGLSyncKHR sync;
1031 uint32_t frameNum;
1032 {
1033 Mutex::Autolock lock(mMutex);
1034 while (mQueue.isEmpty()) {
1035 mCondition.wait(mMutex);
1036 }
1037 sync = mQueue[0];
1038 frameNum = mFramesCompleted;
1039 }
1040 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1041 {
1042 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1043 frameNum).string());
1044 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1045 if (result == EGL_FALSE) {
1046 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1047 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1048 ALOGE("FrameCompletion: timeout waiting for fence");
1049 }
1050 eglDestroySyncKHR(dpy, sync);
1051 }
1052 {
1053 Mutex::Autolock lock(mMutex);
1054 mQueue.removeAt(0);
1055 mFramesCompleted++;
1056 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1057 }
1058 return true;
1059 }
1060
1061 uint32_t mFramesQueued;
1062 uint32_t mFramesCompleted;
1063 Vector<EGLSyncKHR> mQueue;
1064 Condition mCondition;
1065 Mutex mMutex;
1066};
1067
Dan Stozaa894d082015-02-19 15:27:36 -08001068EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1069 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001070{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001071 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001072 clearError();
1073
Jesse Hallb29e5e82012-04-04 16:53:42 -07001074 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001075 if (!dp) return EGL_FALSE;
1076
Jesse Hallb29e5e82012-04-04 16:53:42 -07001077 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001078 if (!_s.get())
1079 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001080
Mathias Agopian518ec112011-05-13 16:21:08 -07001081 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001082
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001083 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1084 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1085 if (sync != EGL_NO_SYNC_KHR) {
1086 FrameCompletionThread::queueSync(sync);
1087 }
1088 }
1089
Mathias Agopian7db993a2012-03-25 00:49:46 -07001090 if (CC_UNLIKELY(dp->finishOnSwap)) {
1091 uint32_t pixel;
1092 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1093 if (c) {
1094 // glReadPixels() ensures that the frame is complete
1095 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1096 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1097 }
1098 }
1099
Dan Stozaa894d082015-02-19 15:27:36 -08001100 if (n_rects == 0) {
1101 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1102 }
1103
1104 Vector<android_native_rect_t> androidRects;
1105 for (int r = 0; r < n_rects; ++r) {
1106 int offset = r * 4;
1107 int x = rects[offset];
1108 int y = rects[offset + 1];
1109 int width = rects[offset + 2];
1110 int height = rects[offset + 3];
1111 android_native_rect_t androidRect;
1112 androidRect.left = x;
1113 androidRect.top = y + height;
1114 androidRect.right = x + width;
1115 androidRect.bottom = y;
1116 androidRects.push_back(androidRect);
1117 }
1118 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1119 androidRects.size());
1120
1121 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1122 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1123 rects, n_rects);
1124 } else {
1125 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1126 }
1127}
1128
1129EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1130{
1131 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001132}
1133
1134EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1135 NativePixmapType target)
1136{
1137 clearError();
1138
Jesse Hallb29e5e82012-04-04 16:53:42 -07001139 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001140 if (!dp) return EGL_FALSE;
1141
Jesse Hallb29e5e82012-04-04 16:53:42 -07001142 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001143 if (!_s.get())
1144 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001145
Mathias Agopian518ec112011-05-13 16:21:08 -07001146 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001147 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001148}
1149
1150const char* eglQueryString(EGLDisplay dpy, EGLint name)
1151{
1152 clearError();
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) {
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001209 int err = native_window_set_auto_refresh(s->win.get(),
1210 value ? true : false);
1211 return (err == NO_ERROR) ? EGL_TRUE :
1212 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001213 }
1214
Pablo Ceballosc18be292016-05-31 14:55:42 -07001215 if (attribute == EGL_TIMESTAMPS_ANDROID) {
1216 s->enableTimestamps = value;
1217 return EGL_TRUE;
1218 }
1219
Mathias Agopian518ec112011-05-13 16:21:08 -07001220 if (s->cnx->egl.eglSurfaceAttrib) {
1221 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001222 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001223 }
1224 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1225}
1226
1227EGLBoolean eglBindTexImage(
1228 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1229{
1230 clearError();
1231
Jesse Hallb29e5e82012-04-04 16:53:42 -07001232 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001233 if (!dp) return EGL_FALSE;
1234
Jesse Hallb29e5e82012-04-04 16:53:42 -07001235 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001236 if (!_s.get())
1237 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001238
Mathias Agopian518ec112011-05-13 16:21:08 -07001239 egl_surface_t const * const s = get_surface(surface);
1240 if (s->cnx->egl.eglBindTexImage) {
1241 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001242 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001243 }
1244 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1245}
1246
1247EGLBoolean eglReleaseTexImage(
1248 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1249{
1250 clearError();
1251
Jesse Hallb29e5e82012-04-04 16:53:42 -07001252 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001253 if (!dp) return EGL_FALSE;
1254
Jesse Hallb29e5e82012-04-04 16:53:42 -07001255 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001256 if (!_s.get())
1257 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001258
Mathias Agopian518ec112011-05-13 16:21:08 -07001259 egl_surface_t const * const s = get_surface(surface);
1260 if (s->cnx->egl.eglReleaseTexImage) {
1261 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001262 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001263 }
1264 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1265}
1266
1267EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1268{
1269 clearError();
1270
Jesse Hallb29e5e82012-04-04 16:53:42 -07001271 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 if (!dp) return EGL_FALSE;
1273
1274 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001275 egl_connection_t* const cnx = &gEGLImpl;
1276 if (cnx->dso && cnx->egl.eglSwapInterval) {
1277 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001278 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001279
Mathias Agopian518ec112011-05-13 16:21:08 -07001280 return res;
1281}
1282
1283
1284// ----------------------------------------------------------------------------
1285// EGL 1.2
1286// ----------------------------------------------------------------------------
1287
1288EGLBoolean eglWaitClient(void)
1289{
1290 clearError();
1291
Mathias Agopianada798b2012-02-13 17:09:30 -08001292 egl_connection_t* const cnx = &gEGLImpl;
1293 if (!cnx->dso)
1294 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1295
1296 EGLBoolean res;
1297 if (cnx->egl.eglWaitClient) {
1298 res = cnx->egl.eglWaitClient();
1299 } else {
1300 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001301 }
1302 return res;
1303}
1304
1305EGLBoolean eglBindAPI(EGLenum api)
1306{
1307 clearError();
1308
1309 if (egl_init_drivers() == EGL_FALSE) {
1310 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1311 }
1312
1313 // bind this API on all EGLs
1314 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001315 egl_connection_t* const cnx = &gEGLImpl;
1316 if (cnx->dso && cnx->egl.eglBindAPI) {
1317 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001318 }
1319 return res;
1320}
1321
1322EGLenum eglQueryAPI(void)
1323{
1324 clearError();
1325
1326 if (egl_init_drivers() == EGL_FALSE) {
1327 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1328 }
1329
Mathias Agopianada798b2012-02-13 17:09:30 -08001330 egl_connection_t* const cnx = &gEGLImpl;
1331 if (cnx->dso && cnx->egl.eglQueryAPI) {
1332 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001333 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001334
Mathias Agopian518ec112011-05-13 16:21:08 -07001335 // or, it can only be OpenGL ES
1336 return EGL_OPENGL_ES_API;
1337}
1338
1339EGLBoolean eglReleaseThread(void)
1340{
1341 clearError();
1342
1343 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001344 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001345
Mathias Agopianada798b2012-02-13 17:09:30 -08001346 egl_connection_t* const cnx = &gEGLImpl;
1347 if (cnx->dso && cnx->egl.eglReleaseThread) {
1348 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001349 }
1350 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001351 return EGL_TRUE;
1352}
1353
1354EGLSurface eglCreatePbufferFromClientBuffer(
1355 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1356 EGLConfig config, const EGLint *attrib_list)
1357{
1358 clearError();
1359
Jesse Hallb29e5e82012-04-04 16:53:42 -07001360 egl_connection_t* cnx = NULL;
1361 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1362 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001363 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1364 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001365 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001366 }
1367 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1368}
1369
1370// ----------------------------------------------------------------------------
1371// EGL_EGLEXT_VERSION 3
1372// ----------------------------------------------------------------------------
1373
1374EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1375 const EGLint *attrib_list)
1376{
1377 clearError();
1378
Jesse Hallb29e5e82012-04-04 16:53:42 -07001379 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001380 if (!dp) return EGL_FALSE;
1381
Jesse Hallb29e5e82012-04-04 16:53:42 -07001382 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001383 if (!_s.get())
1384 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001385
1386 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001387 if (s->cnx->egl.eglLockSurfaceKHR) {
1388 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001389 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001390 }
1391 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1392}
1393
1394EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1395{
1396 clearError();
1397
Jesse Hallb29e5e82012-04-04 16:53:42 -07001398 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001399 if (!dp) return EGL_FALSE;
1400
Jesse Hallb29e5e82012-04-04 16:53:42 -07001401 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001402 if (!_s.get())
1403 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001404
1405 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001406 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001407 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001408 }
1409 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1410}
1411
1412EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1413 EGLClientBuffer buffer, const EGLint *attrib_list)
1414{
1415 clearError();
1416
Jesse Hallb29e5e82012-04-04 16:53:42 -07001417 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001418 if (!dp) return EGL_NO_IMAGE_KHR;
1419
Jesse Hallb29e5e82012-04-04 16:53:42 -07001420 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001421 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001422
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001423 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1424 egl_connection_t* const cnx = &gEGLImpl;
1425 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1426 result = cnx->egl.eglCreateImageKHR(
1427 dp->disp.dpy,
1428 c ? c->context : EGL_NO_CONTEXT,
1429 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001431 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001432}
1433
1434EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1435{
1436 clearError();
1437
Jesse Hallb29e5e82012-04-04 16:53:42 -07001438 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001439 if (!dp) return EGL_FALSE;
1440
Steven Holte646a5c52012-06-04 20:02:11 -07001441 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001442 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001443 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001444 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001445 }
Steven Holte646a5c52012-06-04 20:02:11 -07001446 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001447}
1448
1449// ----------------------------------------------------------------------------
1450// EGL_EGLEXT_VERSION 5
1451// ----------------------------------------------------------------------------
1452
1453
1454EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1455{
1456 clearError();
1457
Jesse Hallb29e5e82012-04-04 16:53:42 -07001458 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001459 if (!dp) return EGL_NO_SYNC_KHR;
1460
Mathias Agopian518ec112011-05-13 16:21:08 -07001461 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001462 egl_connection_t* const cnx = &gEGLImpl;
1463 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1464 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001465 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001466 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001467}
1468
1469EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1470{
1471 clearError();
1472
Jesse Hallb29e5e82012-04-04 16:53:42 -07001473 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001474 if (!dp) return EGL_FALSE;
1475
Mathias Agopian518ec112011-05-13 16:21:08 -07001476 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001477 egl_connection_t* const cnx = &gEGLImpl;
1478 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1479 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001480 }
1481 return result;
1482}
1483
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001484EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1485 clearError();
1486
1487 const egl_display_ptr dp = validate_display(dpy);
1488 if (!dp) return EGL_FALSE;
1489
1490 EGLBoolean result = EGL_FALSE;
1491 egl_connection_t* const cnx = &gEGLImpl;
1492 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1493 result = cnx->egl.eglSignalSyncKHR(
1494 dp->disp.dpy, sync, mode);
1495 }
1496 return result;
1497}
1498
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001499EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1500 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001501{
1502 clearError();
1503
Jesse Hallb29e5e82012-04-04 16:53:42 -07001504 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001505 if (!dp) return EGL_FALSE;
1506
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001507 EGLBoolean result = EGL_FALSE;
1508 egl_connection_t* const cnx = &gEGLImpl;
1509 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1510 result = cnx->egl.eglClientWaitSyncKHR(
1511 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001512 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001513 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001514}
1515
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001516EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1517 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001518{
1519 clearError();
1520
Jesse Hallb29e5e82012-04-04 16:53:42 -07001521 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001522 if (!dp) return EGL_FALSE;
1523
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001524 EGLBoolean result = EGL_FALSE;
1525 egl_connection_t* const cnx = &gEGLImpl;
1526 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1527 result = cnx->egl.eglGetSyncAttribKHR(
1528 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001529 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001530 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001531}
1532
Season Li000d88f2015-07-01 11:39:40 -07001533EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1534{
1535 clearError();
1536
1537 const egl_display_ptr dp = validate_display(dpy);
1538 if (!dp) return EGL_NO_STREAM_KHR;
1539
1540 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1541 egl_connection_t* const cnx = &gEGLImpl;
1542 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1543 result = cnx->egl.eglCreateStreamKHR(
1544 dp->disp.dpy, attrib_list);
1545 }
1546 return result;
1547}
1548
1549EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1550{
1551 clearError();
1552
1553 const egl_display_ptr dp = validate_display(dpy);
1554 if (!dp) return EGL_FALSE;
1555
1556 EGLBoolean result = EGL_FALSE;
1557 egl_connection_t* const cnx = &gEGLImpl;
1558 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1559 result = cnx->egl.eglDestroyStreamKHR(
1560 dp->disp.dpy, stream);
1561 }
1562 return result;
1563}
1564
1565EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1566 EGLenum attribute, EGLint value)
1567{
1568 clearError();
1569
1570 const egl_display_ptr dp = validate_display(dpy);
1571 if (!dp) return EGL_FALSE;
1572
1573 EGLBoolean result = EGL_FALSE;
1574 egl_connection_t* const cnx = &gEGLImpl;
1575 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1576 result = cnx->egl.eglStreamAttribKHR(
1577 dp->disp.dpy, stream, attribute, value);
1578 }
1579 return result;
1580}
1581
1582EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1583 EGLenum attribute, EGLint *value)
1584{
1585 clearError();
1586
1587 const egl_display_ptr dp = validate_display(dpy);
1588 if (!dp) return EGL_FALSE;
1589
1590 EGLBoolean result = EGL_FALSE;
1591 egl_connection_t* const cnx = &gEGLImpl;
1592 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1593 result = cnx->egl.eglQueryStreamKHR(
1594 dp->disp.dpy, stream, attribute, value);
1595 }
1596 return result;
1597}
1598
1599EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1600 EGLenum attribute, EGLuint64KHR *value)
1601{
1602 clearError();
1603
1604 const egl_display_ptr dp = validate_display(dpy);
1605 if (!dp) return EGL_FALSE;
1606
1607 EGLBoolean result = EGL_FALSE;
1608 egl_connection_t* const cnx = &gEGLImpl;
1609 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1610 result = cnx->egl.eglQueryStreamu64KHR(
1611 dp->disp.dpy, stream, attribute, value);
1612 }
1613 return result;
1614}
1615
1616EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1617 EGLenum attribute, EGLTimeKHR *value)
1618{
1619 clearError();
1620
1621 const egl_display_ptr dp = validate_display(dpy);
1622 if (!dp) return EGL_FALSE;
1623
1624 EGLBoolean result = EGL_FALSE;
1625 egl_connection_t* const cnx = &gEGLImpl;
1626 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1627 result = cnx->egl.eglQueryStreamTimeKHR(
1628 dp->disp.dpy, stream, attribute, value);
1629 }
1630 return result;
1631}
1632
1633EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1634 EGLStreamKHR stream, const EGLint *attrib_list)
1635{
1636 clearError();
1637
1638 egl_display_ptr dp = validate_display(dpy);
1639 if (!dp) return EGL_NO_SURFACE;
1640
1641 egl_connection_t* const cnx = &gEGLImpl;
1642 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1643 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1644 dp->disp.dpy, config, stream, attrib_list);
1645 if (surface != EGL_NO_SURFACE) {
1646 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1647 surface, cnx);
1648 return s;
1649 }
1650 }
1651 return EGL_NO_SURFACE;
1652}
1653
1654EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1655 EGLStreamKHR stream)
1656{
1657 clearError();
1658
1659 const egl_display_ptr dp = validate_display(dpy);
1660 if (!dp) return EGL_FALSE;
1661
1662 EGLBoolean result = EGL_FALSE;
1663 egl_connection_t* const cnx = &gEGLImpl;
1664 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1665 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1666 dp->disp.dpy, stream);
1667 }
1668 return result;
1669}
1670
1671EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1672 EGLStreamKHR stream)
1673{
1674 clearError();
1675
1676 const egl_display_ptr dp = validate_display(dpy);
1677 if (!dp) return EGL_FALSE;
1678
1679 EGLBoolean result = EGL_FALSE;
1680 egl_connection_t* const cnx = &gEGLImpl;
1681 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1682 result = cnx->egl.eglStreamConsumerAcquireKHR(
1683 dp->disp.dpy, stream);
1684 }
1685 return result;
1686}
1687
1688EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1689 EGLStreamKHR stream)
1690{
1691 clearError();
1692
1693 const egl_display_ptr dp = validate_display(dpy);
1694 if (!dp) return EGL_FALSE;
1695
1696 EGLBoolean result = EGL_FALSE;
1697 egl_connection_t* const cnx = &gEGLImpl;
1698 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1699 result = cnx->egl.eglStreamConsumerReleaseKHR(
1700 dp->disp.dpy, stream);
1701 }
1702 return result;
1703}
1704
1705EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1706 EGLDisplay dpy, EGLStreamKHR stream)
1707{
1708 clearError();
1709
1710 const egl_display_ptr dp = validate_display(dpy);
1711 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1712
1713 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1714 egl_connection_t* const cnx = &gEGLImpl;
1715 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1716 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1717 dp->disp.dpy, stream);
1718 }
1719 return result;
1720}
1721
1722EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1723 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1724{
1725 clearError();
1726
1727 const egl_display_ptr dp = validate_display(dpy);
1728 if (!dp) return EGL_NO_STREAM_KHR;
1729
1730 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1731 egl_connection_t* const cnx = &gEGLImpl;
1732 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1733 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1734 dp->disp.dpy, file_descriptor);
1735 }
1736 return result;
1737}
1738
Mathias Agopian518ec112011-05-13 16:21:08 -07001739// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001740// EGL_EGLEXT_VERSION 15
1741// ----------------------------------------------------------------------------
1742
1743EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1744 clearError();
1745 const egl_display_ptr dp = validate_display(dpy);
1746 if (!dp) return EGL_FALSE;
1747 EGLint result = EGL_FALSE;
1748 egl_connection_t* const cnx = &gEGLImpl;
1749 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1750 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1751 }
1752 return result;
1753}
1754
1755// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001756// ANDROID extensions
1757// ----------------------------------------------------------------------------
1758
Jamie Gennis331841b2012-09-06 14:52:00 -07001759EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1760{
1761 clearError();
1762
1763 const egl_display_ptr dp = validate_display(dpy);
1764 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1765
1766 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1767 egl_connection_t* const cnx = &gEGLImpl;
1768 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1769 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1770 }
1771 return result;
1772}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001773
Andy McFadden72841452013-03-01 16:25:32 -08001774EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1775 EGLnsecsANDROID time)
1776{
1777 clearError();
1778
1779 const egl_display_ptr dp = validate_display(dpy);
1780 if (!dp) {
1781 return EGL_FALSE;
1782 }
1783
1784 SurfaceRef _s(dp.get(), surface);
1785 if (!_s.get()) {
1786 setError(EGL_BAD_SURFACE, EGL_FALSE);
1787 return EGL_FALSE;
1788 }
1789
1790 egl_surface_t const * const s = get_surface(surface);
1791 native_window_set_buffers_timestamp(s->win.get(), time);
1792
1793 return EGL_TRUE;
1794}
1795
Craig Donner05249fc2016-01-15 19:33:55 -08001796EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1797{
1798 clearError();
1799
1800 int usage = 0;
1801 uint32_t width = 0;
1802 uint32_t height = 0;
1803 uint32_t format = 0;
1804 uint32_t red_size = 0;
1805 uint32_t green_size = 0;
1806 uint32_t blue_size = 0;
1807 uint32_t alpha_size = 0;
1808
Craig Donnerb40504a2016-06-03 17:54:25 -07001809#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001810 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001811 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001812 target = value; \
1813 } else { \
1814 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1815 } \
1816 break
1817
1818 if (attrib_list) {
1819 while (*attrib_list != EGL_NONE) {
1820 GLint attr = *attrib_list++;
1821 GLint value = *attrib_list++;
1822 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001823 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1824 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1825 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1826 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1827 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1828 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner05249fc2016-01-15 19:33:55 -08001829 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1830 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1831 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001832 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001833 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001834 usage |= GRALLOC_USAGE_HW_RENDER;
1835 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001836 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001837 usage |= GRALLOC_USAGE_HW_TEXTURE;
1838 }
1839 // The buffer must be used for either a texture or a
1840 // renderbuffer.
Craig Donner8cfae6d2016-04-12 16:54:03 -07001841 if ((value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) &&
1842 (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID)) {
Craig Donner05249fc2016-01-15 19:33:55 -08001843 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1844 }
1845 break;
1846 default:
1847 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1848 }
1849 }
1850 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001851#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001852
1853 // Validate format.
1854 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1855 if (alpha_size == 8) {
1856 format = HAL_PIXEL_FORMAT_RGBA_8888;
1857 } else {
1858 format = HAL_PIXEL_FORMAT_RGB_888;
1859 }
1860 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1861 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001862 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001863 } else {
1864 ALOGE("Invalid native pixel format { r=%d, g=%d, b=%d, a=%d }",
1865 red_size, green_size, blue_size, alpha_size);
1866 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1867 }
1868
1869 GraphicBuffer* gBuffer = new GraphicBuffer(width, height, format, usage);
1870 const status_t err = gBuffer->initCheck();
1871 if (err != NO_ERROR) {
1872 ALOGE("Unable to create native buffer { w=%d, h=%d, f=%d, u=%#x }: %#x",
1873 width, height, format, usage, err);
1874 // Destroy the buffer.
1875 sp<GraphicBuffer> holder(gBuffer);
1876 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
1877 }
1878 ALOGD("Created new native buffer %p { w=%d, h=%d, f=%d, u=%#x }",
1879 gBuffer, width, height, format, usage);
1880 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
1881}
1882
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001883// ----------------------------------------------------------------------------
1884// NVIDIA extensions
1885// ----------------------------------------------------------------------------
1886EGLuint64NV eglGetSystemTimeFrequencyNV()
1887{
1888 clearError();
1889
1890 if (egl_init_drivers() == EGL_FALSE) {
1891 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1892 }
1893
1894 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001895 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001896
Mathias Agopianada798b2012-02-13 17:09:30 -08001897 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1898 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001899 }
1900
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001901 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001902}
1903
1904EGLuint64NV eglGetSystemTimeNV()
1905{
1906 clearError();
1907
1908 if (egl_init_drivers() == EGL_FALSE) {
1909 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1910 }
1911
1912 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001913 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001914
Mathias Agopianada798b2012-02-13 17:09:30 -08001915 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1916 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001917 }
1918
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001919 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001920}
Dan Stozaa894d082015-02-19 15:27:36 -08001921
1922// ----------------------------------------------------------------------------
1923// Partial update extension
1924// ----------------------------------------------------------------------------
1925EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1926 EGLint *rects, EGLint n_rects)
1927{
1928 clearError();
1929
1930 const egl_display_ptr dp = validate_display(dpy);
1931 if (!dp) {
1932 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1933 return EGL_FALSE;
1934 }
1935
1936 SurfaceRef _s(dp.get(), surface);
1937 if (!_s.get()) {
1938 setError(EGL_BAD_SURFACE, EGL_FALSE);
1939 return EGL_FALSE;
1940 }
1941
1942 egl_surface_t const * const s = get_surface(surface);
1943 if (s->cnx->egl.eglSetDamageRegionKHR) {
1944 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1945 rects, n_rects);
1946 }
1947
1948 return EGL_FALSE;
1949}
Pablo Ceballosc18be292016-05-31 14:55:42 -07001950
1951EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
1952 EGLint framesAgo, EGLint numTimestamps, const EGLint *timestamps,
1953 EGLnsecsANDROID *values)
1954{
1955 clearError();
1956
1957 const egl_display_ptr dp = validate_display(dpy);
1958 if (!dp) {
1959 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1960 return EGL_FALSE;
1961 }
1962
1963 SurfaceRef _s(dp.get(), surface);
1964 if (!_s.get()) {
1965 setError(EGL_BAD_SURFACE, EGL_FALSE);
1966 return EGL_FALSE;
1967 }
1968
1969 egl_surface_t const * const s = get_surface(surface);
1970
1971 if (!s->enableTimestamps) {
1972 setError(EGL_BAD_SURFACE, EGL_FALSE);
1973 return EGL_FALSE;
1974 }
1975
1976 nsecs_t* postedTime = nullptr;
1977 nsecs_t* acquireTime = nullptr;
1978 nsecs_t* refreshStartTime = nullptr;
1979 nsecs_t* GLCompositionDoneTime = nullptr;
1980 nsecs_t* displayRetireTime = nullptr;
1981 nsecs_t* releaseTime = nullptr;
1982
1983 for (int i = 0; i < numTimestamps; i++) {
1984 switch (timestamps[i]) {
1985 case EGL_QUEUE_TIME_ANDROID:
1986 postedTime = &values[i];
1987 break;
1988 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
1989 acquireTime = &values[i];
1990 break;
1991 case EGL_COMPOSITION_START_TIME_ANDROID:
1992 refreshStartTime = &values[i];
1993 break;
1994 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
1995 GLCompositionDoneTime = &values[i];
1996 break;
1997 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
1998 displayRetireTime = &values[i];
1999 break;
2000 case EGL_READS_DONE_TIME_ANDROID:
2001 releaseTime = &values[i];
2002 break;
2003 default:
2004 setError(EGL_BAD_PARAMETER, EGL_FALSE);
2005 return EGL_FALSE;
2006 }
2007 }
2008
2009 status_t ret = native_window_get_frame_timestamps(s->win.get(), framesAgo,
2010 postedTime, acquireTime, refreshStartTime, GLCompositionDoneTime,
2011 displayRetireTime, releaseTime);
2012
2013 if (ret != NO_ERROR) {
2014 setError(EGL_BAD_ACCESS, EGL_FALSE);
2015 return EGL_FALSE;
2016 }
2017
2018 return EGL_TRUE;
2019}
2020
2021EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2022 EGLint timestamp)
2023{
2024 clearError();
2025
2026 const egl_display_ptr dp = validate_display(dpy);
2027 if (!dp) {
2028 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2029 return EGL_FALSE;
2030 }
2031
2032 SurfaceRef _s(dp.get(), surface);
2033 if (!_s.get()) {
2034 setError(EGL_BAD_SURFACE, EGL_FALSE);
2035 return EGL_FALSE;
2036 }
2037
2038 switch (timestamp) {
2039 case EGL_QUEUE_TIME_ANDROID:
2040 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2041 case EGL_COMPOSITION_START_TIME_ANDROID:
2042 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
2043 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2044 case EGL_READS_DONE_TIME_ANDROID:
2045 return EGL_TRUE;
2046 default:
2047 return EGL_FALSE;
2048 }
2049}