blob: 80a9f4082e1d0cb81cc0caf40625b24cc80ad688 [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
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070053#define ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS 0
54
Mathias Agopian518ec112011-05-13 16:21:08 -070055// ----------------------------------------------------------------------------
56
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070057namespace android {
58
Mathias Agopian518ec112011-05-13 16:21:08 -070059struct extention_map_t {
60 const char* name;
61 __eglMustCastToProperFunctionPointerType address;
62};
63
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070064/*
Jesse Hall21558da2013-08-06 15:31:22 -070065 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070066 *
Jesse Hall21558da2013-08-06 15:31:22 -070067 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
68 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070069 *
Jesse Hall21558da2013-08-06 15:31:22 -070070 * The rest (gExtensionString) depend on support in the EGL driver, and are
71 * only available if the driver supports them. However, some of these must be
72 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080073 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070074 * the mandatory extensions are present and may not function properly if some
75 * are missing.
76 *
77 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070078 */
Jesse Hall21558da2013-08-06 15:31:22 -070079extern char const * const gBuiltinExtensionString =
80 "EGL_KHR_get_all_proc_addresses "
81 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080082 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080083 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080084 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070085#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -070086 "EGL_ANDROID_get_frame_timestamps "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070087#endif
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 Hallc2e41222013-08-08 13:40:22 -070094 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070096 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_gl_texture_cubemap_image "
98 "EGL_KHR_gl_renderbuffer_image "
99 "EGL_KHR_reusable_sync "
100 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700101 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700102 "EGL_KHR_config_attribs "
103 "EGL_KHR_surfaceless_context "
104 "EGL_KHR_stream "
105 "EGL_KHR_stream_fifo "
106 "EGL_KHR_stream_producer_eglsurface "
107 "EGL_KHR_stream_consumer_gltexture "
108 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700109 "EGL_EXT_create_context_robustness "
110 "EGL_NV_system_time "
111 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700112 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700113 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800114 "EGL_KHR_partial_update " // strongly recommended
115 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700116 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700117 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700118 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700119 "EGL_EXT_protected_content "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700120 ;
121
122// extensions not exposed to applications but used by the ANDROID system
123// "EGL_ANDROID_blob_cache " // strongly recommended
124// "EGL_IMG_hibernate_process " // optional
125// "EGL_ANDROID_native_fence_sync " // strongly recommended
126// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700127// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700128
129/*
130 * EGL Extensions entry-points exposed to 3rd party applications
131 * (keep in sync with gExtensionString above)
132 *
133 */
134static const extention_map_t sExtensionMap[] = {
135 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700136 { "eglLockSurfaceKHR",
137 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
138 { "eglUnlockSurfaceKHR",
139 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700140
141 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700142 { "eglCreateImageKHR",
143 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
144 { "eglDestroyImageKHR",
145 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700146
147 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
148 { "eglCreateSyncKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
150 { "eglDestroySyncKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
152 { "eglClientWaitSyncKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
154 { "eglSignalSyncKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
156 { "eglGetSyncAttribKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
158
159 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800160 { "eglGetSystemTimeFrequencyNV",
161 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
162 { "eglGetSystemTimeNV",
163 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700164
Mathias Agopian2bb71682013-03-27 17:32:41 -0700165 // EGL_KHR_wait_sync
166 { "eglWaitSyncKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700168
169 // EGL_ANDROID_presentation_time
170 { "eglPresentationTimeANDROID",
171 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800172
173 // EGL_KHR_swap_buffers_with_damage
174 { "eglSwapBuffersWithDamageKHR",
175 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
176
Craig Donner05249fc2016-01-15 19:33:55 -0800177 // EGL_ANDROID_native_client_buffer
178 { "eglCreateNativeClientBufferANDROID",
179 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
180
Dan Stozaa894d082015-02-19 15:27:36 -0800181 // EGL_KHR_partial_update
182 { "eglSetDamageRegionKHR",
183 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700184
185 { "eglCreateStreamKHR",
186 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
187 { "eglDestroyStreamKHR",
188 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
189 { "eglStreamAttribKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
191 { "eglQueryStreamKHR",
192 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
193 { "eglQueryStreamu64KHR",
194 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
195 { "eglQueryStreamTimeKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
197 { "eglCreateStreamProducerSurfaceKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
199 { "eglStreamConsumerGLTextureExternalKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
201 { "eglStreamConsumerAcquireKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
203 { "eglStreamConsumerReleaseKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
205 { "eglGetStreamFileDescriptorKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
207 { "eglCreateStreamFromFileDescriptorKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700209
210 // EGL_ANDROID_get_frame_timestamps
211 { "eglGetFrameTimestampsANDROID",
212 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
213 { "eglQueryTimestampSupportedANDROID",
214 (__eglMustCastToProperFunctionPointerType)&eglQueryTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700215};
216
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700217/*
218 * These extensions entry-points should not be exposed to applications.
219 * They're used internally by the Android EGL layer.
220 */
221#define FILTER_EXTENSIONS(procname) \
222 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
223 !strcmp((procname), "eglHibernateProcessIMG") || \
224 !strcmp((procname), "eglAwakenProcessIMG") || \
225 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
226
227
228
Mathias Agopian518ec112011-05-13 16:21:08 -0700229// accesses protected by sExtensionMapMutex
230static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
231static int sGLExtentionSlot = 0;
232static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
233
234static void(*findProcAddress(const char* name,
235 const extention_map_t* map, size_t n))() {
236 for (uint32_t i=0 ; i<n ; i++) {
237 if (!strcmp(name, map[i].name)) {
238 return map[i].address;
239 }
240 }
241 return NULL;
242}
243
244// ----------------------------------------------------------------------------
245
Mathias Agopian518ec112011-05-13 16:21:08 -0700246extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
247extern EGLBoolean egl_init_drivers();
248extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700249extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700250
Mathias Agopian518ec112011-05-13 16:21:08 -0700251} // namespace android;
252
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700253
Mathias Agopian518ec112011-05-13 16:21:08 -0700254// ----------------------------------------------------------------------------
255
256static inline void clearError() { egl_tls_t::clearError(); }
257static inline EGLContext getContext() { return egl_tls_t::getContext(); }
258
259// ----------------------------------------------------------------------------
260
261EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
262{
263 clearError();
264
Dan Stozac3289c42014-01-17 11:38:34 -0800265 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700266 if (index >= NUM_DISPLAYS) {
267 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
268 }
269
270 if (egl_init_drivers() == EGL_FALSE) {
271 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
272 }
273
274 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
275 return dpy;
276}
277
278// ----------------------------------------------------------------------------
279// Initialization
280// ----------------------------------------------------------------------------
281
282EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
283{
284 clearError();
285
Jesse Hallb29e5e82012-04-04 16:53:42 -0700286 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700287 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
288
289 EGLBoolean res = dp->initialize(major, minor);
290
291 return res;
292}
293
294EGLBoolean eglTerminate(EGLDisplay dpy)
295{
296 // NOTE: don't unload the drivers b/c some APIs can be called
297 // after eglTerminate() has been called. eglTerminate() only
298 // terminates an EGLDisplay, not a EGL itself.
299
300 clearError();
301
Jesse Hallb29e5e82012-04-04 16:53:42 -0700302 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700303 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
304
305 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800306
Mathias Agopian518ec112011-05-13 16:21:08 -0700307 return res;
308}
309
310// ----------------------------------------------------------------------------
311// configuration
312// ----------------------------------------------------------------------------
313
314EGLBoolean eglGetConfigs( EGLDisplay dpy,
315 EGLConfig *configs,
316 EGLint config_size, EGLint *num_config)
317{
318 clearError();
319
Jesse Hallb29e5e82012-04-04 16:53:42 -0700320 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700321 if (!dp) return EGL_FALSE;
322
Mathias Agopian7773c432012-02-13 20:06:08 -0800323 if (num_config==0) {
324 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700325 }
326
Mathias Agopian7773c432012-02-13 20:06:08 -0800327 EGLBoolean res = EGL_FALSE;
328 *num_config = 0;
329
330 egl_connection_t* const cnx = &gEGLImpl;
331 if (cnx->dso) {
332 res = cnx->egl.eglGetConfigs(
333 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700334 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800335
336 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700337}
338
339EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
340 EGLConfig *configs, EGLint config_size,
341 EGLint *num_config)
342{
343 clearError();
344
Jesse Hallb29e5e82012-04-04 16:53:42 -0700345 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700346 if (!dp) return EGL_FALSE;
347
348 if (num_config==0) {
349 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
350 }
351
Mathias Agopian518ec112011-05-13 16:21:08 -0700352 EGLBoolean res = EGL_FALSE;
353 *num_config = 0;
354
Mathias Agopianada798b2012-02-13 17:09:30 -0800355 egl_connection_t* const cnx = &gEGLImpl;
356 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700357 if (attrib_list) {
358 char value[PROPERTY_VALUE_MAX];
359 property_get("debug.egl.force_msaa", value, "false");
360
361 if (!strcmp(value, "true")) {
362 size_t attribCount = 0;
363 EGLint attrib = attrib_list[0];
364
365 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700366 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700367 const EGLint *attribRendererable = NULL;
368 const EGLint *attribCaveat = NULL;
369
370 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700371 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700372 while (attrib != EGL_NONE) {
373 attrib = attrib_list[attribCount];
374 switch (attrib) {
375 case EGL_RENDERABLE_TYPE:
376 attribRendererable = &attrib_list[attribCount];
377 break;
378 case EGL_CONFIG_CAVEAT:
379 attribCaveat = &attrib_list[attribCount];
380 break;
381 }
382 attribCount++;
383 }
384
385 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
386 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800387
Romain Guy1cffc802012-10-15 18:13:05 -0700388 // Insert 2 extra attributes to force-enable MSAA 4x
389 EGLint aaAttribs[attribCount + 4];
390 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
391 aaAttribs[1] = 1;
392 aaAttribs[2] = EGL_SAMPLES;
393 aaAttribs[3] = 4;
394
395 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
396
397 EGLint numConfigAA;
398 EGLBoolean resAA = cnx->egl.eglChooseConfig(
399 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
400
401 if (resAA == EGL_TRUE && numConfigAA > 0) {
402 ALOGD("Enabling MSAA 4x");
403 *num_config = numConfigAA;
404 return resAA;
405 }
406 }
407 }
408 }
409
Mathias Agopian7773c432012-02-13 20:06:08 -0800410 res = cnx->egl.eglChooseConfig(
411 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700412 }
413 return res;
414}
415
416EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
417 EGLint attribute, EGLint *value)
418{
419 clearError();
420
Jesse Hallb29e5e82012-04-04 16:53:42 -0700421 egl_connection_t* cnx = NULL;
422 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
423 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800424
Mathias Agopian518ec112011-05-13 16:21:08 -0700425 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800426 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700427}
428
429// ----------------------------------------------------------------------------
430// surfaces
431// ----------------------------------------------------------------------------
432
Jesse Hallc2e41222013-08-08 13:40:22 -0700433// Turn linear formats into corresponding sRGB formats when colorspace is
434// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
435// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800436// the modification isn't possible, the original dataSpace is returned.
437static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
438 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700439 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800440 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700441 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800442 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700443 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800444 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700445}
446
Mathias Agopian518ec112011-05-13 16:21:08 -0700447EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
448 NativeWindowType window,
449 const EGLint *attrib_list)
450{
451 clearError();
452
Jesse Hallb29e5e82012-04-04 16:53:42 -0700453 egl_connection_t* cnx = NULL;
454 egl_display_ptr dp = validate_display_connection(dpy, cnx);
455 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800456 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700457
Andy McFaddend566ce32014-01-07 15:54:17 -0800458 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
459 if (result != OK) {
460 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
461 "failed (%#x) (already connected to another API?)",
462 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700463 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700464 }
465
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700466 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700467 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
468 // of our native format. So if sRGB gamma is requested, we have to
469 // modify the EGLconfig's format before setting the native window's
470 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800471
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700472 // by default, just pick RGBA_8888
473 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800474 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700475
476 EGLint a = 0;
477 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
478 if (a > 0) {
479 // alpha-channel requested, there's really only one suitable format
480 format = HAL_PIXEL_FORMAT_RGBA_8888;
481 } else {
482 EGLint r, g, b;
483 r = g = b = 0;
484 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
485 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
486 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
487 EGLint colorDepth = r + g + b;
488 if (colorDepth <= 16) {
489 format = HAL_PIXEL_FORMAT_RGB_565;
490 } else {
491 format = HAL_PIXEL_FORMAT_RGBX_8888;
492 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700493 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700494
495 // now select a corresponding sRGB format if needed
496 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
497 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
498 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530499 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700500 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700501 }
502 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800503
Jesse Hallc2e41222013-08-08 13:40:22 -0700504 if (format != 0) {
505 int err = native_window_set_buffers_format(window, format);
506 if (err != 0) {
507 ALOGE("error setting native window pixel format: %s (%d)",
508 strerror(-err), err);
509 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
510 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
511 }
512 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700513
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800514 if (dataSpace != 0) {
515 int err = native_window_set_buffers_data_space(window, dataSpace);
516 if (err != 0) {
517 ALOGE("error setting native window pixel dataSpace: %s (%d)",
518 strerror(-err), err);
519 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
520 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
521 }
522 }
523
Jamie Gennis59769462011-11-19 18:04:43 -0800524 // the EGL spec requires that a new EGLSurface default to swap interval
525 // 1, so explicitly set that on the window here.
526 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
527 anw->setSwapInterval(anw, 1);
528
Mathias Agopian518ec112011-05-13 16:21:08 -0700529 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800530 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700531 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700532 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
533 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700534 return s;
535 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700536
537 // EGLSurface creation failed
538 native_window_set_buffers_format(window, 0);
539 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700540 }
541 return EGL_NO_SURFACE;
542}
543
544EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
545 NativePixmapType pixmap,
546 const EGLint *attrib_list)
547{
548 clearError();
549
Jesse Hallb29e5e82012-04-04 16:53:42 -0700550 egl_connection_t* cnx = NULL;
551 egl_display_ptr dp = validate_display_connection(dpy, cnx);
552 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700553 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800554 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700555 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700556 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
557 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700558 return s;
559 }
560 }
561 return EGL_NO_SURFACE;
562}
563
564EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
565 const EGLint *attrib_list)
566{
567 clearError();
568
Jesse Hallb29e5e82012-04-04 16:53:42 -0700569 egl_connection_t* cnx = NULL;
570 egl_display_ptr dp = validate_display_connection(dpy, cnx);
571 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700572 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800573 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700574 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700575 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
576 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700577 return s;
578 }
579 }
580 return EGL_NO_SURFACE;
581}
Jesse Hall47743382013-02-08 11:13:46 -0800582
Mathias Agopian518ec112011-05-13 16:21:08 -0700583EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
584{
585 clearError();
586
Jesse Hallb29e5e82012-04-04 16:53:42 -0700587 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700588 if (!dp) return EGL_FALSE;
589
Jesse Hallb29e5e82012-04-04 16:53:42 -0700590 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700591 if (!_s.get())
592 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700593
594 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800595 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700596 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700597 _s.terminate();
598 }
599 return result;
600}
601
602EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
603 EGLint attribute, EGLint *value)
604{
605 clearError();
606
Jesse Hallb29e5e82012-04-04 16:53:42 -0700607 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700608 if (!dp) return EGL_FALSE;
609
Jesse Hallb29e5e82012-04-04 16:53:42 -0700610 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700611 if (!_s.get())
612 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700613
Mathias Agopian518ec112011-05-13 16:21:08 -0700614 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800615 return s->cnx->egl.eglQuerySurface(
616 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700617}
618
Jamie Gennise8696a42012-01-15 18:54:57 -0800619void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800620 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800621 clearError();
622
Jesse Hallb29e5e82012-04-04 16:53:42 -0700623 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800624 if (!dp) {
625 return;
626 }
627
Jesse Hallb29e5e82012-04-04 16:53:42 -0700628 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800629 if (!_s.get()) {
630 setError(EGL_BAD_SURFACE, EGL_FALSE);
631 return;
632 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800633}
634
Mathias Agopian518ec112011-05-13 16:21:08 -0700635// ----------------------------------------------------------------------------
636// Contexts
637// ----------------------------------------------------------------------------
638
639EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
640 EGLContext share_list, const EGLint *attrib_list)
641{
642 clearError();
643
Jesse Hallb29e5e82012-04-04 16:53:42 -0700644 egl_connection_t* cnx = NULL;
645 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700646 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700647 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700648 if (!ContextRef(dp.get(), share_list).get()) {
649 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
650 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700651 egl_context_t* const c = get_context(share_list);
652 share_list = c->context;
653 }
654 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800655 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700656 if (context != EGL_NO_CONTEXT) {
657 // figure out if it's a GLESv1 or GLESv2
658 int version = 0;
659 if (attrib_list) {
660 while (*attrib_list != EGL_NONE) {
661 GLint attr = *attrib_list++;
662 GLint value = *attrib_list++;
663 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
664 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800665 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800666 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800667 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700668 }
669 }
670 };
671 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700672 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
673 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700674 return c;
675 }
676 }
677 return EGL_NO_CONTEXT;
678}
679
680EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
681{
682 clearError();
683
Jesse Hallb29e5e82012-04-04 16:53:42 -0700684 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700685 if (!dp)
686 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700687
Jesse Hallb29e5e82012-04-04 16:53:42 -0700688 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700689 if (!_c.get())
690 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800691
Mathias Agopian518ec112011-05-13 16:21:08 -0700692 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800693 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700694 if (result == EGL_TRUE) {
695 _c.terminate();
696 }
697 return result;
698}
699
Mathias Agopian518ec112011-05-13 16:21:08 -0700700EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
701 EGLSurface read, EGLContext ctx)
702{
703 clearError();
704
Jesse Hallb29e5e82012-04-04 16:53:42 -0700705 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700706 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
707
Mathias Agopian5b287a62011-05-16 18:58:55 -0700708 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
709 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
710 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700711 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
712 (draw != EGL_NO_SURFACE) ) {
713 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
714 }
715
716 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700717 ContextRef _c(dp.get(), ctx);
718 SurfaceRef _d(dp.get(), draw);
719 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700720
721 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700722 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700723 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700724 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700725 }
726
727 // these are the underlying implementation's object
728 EGLContext impl_ctx = EGL_NO_CONTEXT;
729 EGLSurface impl_draw = EGL_NO_SURFACE;
730 EGLSurface impl_read = EGL_NO_SURFACE;
731
732 // these are our objects structs passed in
733 egl_context_t * c = NULL;
734 egl_surface_t const * d = NULL;
735 egl_surface_t const * r = NULL;
736
737 // these are the current objects structs
738 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800739
Mathias Agopian518ec112011-05-13 16:21:08 -0700740 if (ctx != EGL_NO_CONTEXT) {
741 c = get_context(ctx);
742 impl_ctx = c->context;
743 } else {
744 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700745 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
746 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
747 return setError(EGL_BAD_MATCH, EGL_FALSE);
748 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700749 if (cur_c == NULL) {
750 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700751 // not an error, there is just no current context.
752 return EGL_TRUE;
753 }
754 }
755
756 // retrieve the underlying implementation's draw EGLSurface
757 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700758 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700759 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700760 impl_draw = d->surface;
761 }
762
763 // retrieve the underlying implementation's read EGLSurface
764 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700765 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700766 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 impl_read = r->surface;
768 }
769
Mathias Agopian518ec112011-05-13 16:21:08 -0700770
Jesse Hallb29e5e82012-04-04 16:53:42 -0700771 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800772 draw, read, ctx,
773 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700774
775 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800776 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700777 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
778 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700779 _c.acquire();
780 _r.acquire();
781 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700782 } else {
783 setGLHooksThreadSpecific(&gHooksNoContext);
784 egl_tls_t::setContext(EGL_NO_CONTEXT);
785 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700786 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000787 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700788 egl_connection_t* const cnx = &gEGLImpl;
789 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700790 }
791 return result;
792}
793
794
795EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
796 EGLint attribute, EGLint *value)
797{
798 clearError();
799
Jesse Hallb29e5e82012-04-04 16:53:42 -0700800 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700801 if (!dp) return EGL_FALSE;
802
Jesse Hallb29e5e82012-04-04 16:53:42 -0700803 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700804 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
805
Mathias Agopian518ec112011-05-13 16:21:08 -0700806 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800807 return c->cnx->egl.eglQueryContext(
808 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700809
Mathias Agopian518ec112011-05-13 16:21:08 -0700810}
811
812EGLContext eglGetCurrentContext(void)
813{
814 // could be called before eglInitialize(), but we wouldn't have a context
815 // then, and this function would correctly return EGL_NO_CONTEXT.
816
817 clearError();
818
819 EGLContext ctx = getContext();
820 return ctx;
821}
822
823EGLSurface eglGetCurrentSurface(EGLint readdraw)
824{
825 // could be called before eglInitialize(), but we wouldn't have a context
826 // then, and this function would correctly return EGL_NO_SURFACE.
827
828 clearError();
829
830 EGLContext ctx = getContext();
831 if (ctx) {
832 egl_context_t const * const c = get_context(ctx);
833 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
834 switch (readdraw) {
835 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800836 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700837 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
838 }
839 }
840 return EGL_NO_SURFACE;
841}
842
843EGLDisplay eglGetCurrentDisplay(void)
844{
845 // could be called before eglInitialize(), but we wouldn't have a context
846 // then, and this function would correctly return EGL_NO_DISPLAY.
847
848 clearError();
849
850 EGLContext ctx = getContext();
851 if (ctx) {
852 egl_context_t const * const c = get_context(ctx);
853 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
854 return c->dpy;
855 }
856 return EGL_NO_DISPLAY;
857}
858
859EGLBoolean eglWaitGL(void)
860{
Mathias Agopian518ec112011-05-13 16:21:08 -0700861 clearError();
862
Mathias Agopianada798b2012-02-13 17:09:30 -0800863 egl_connection_t* const cnx = &gEGLImpl;
864 if (!cnx->dso)
865 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
866
867 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700868}
869
870EGLBoolean eglWaitNative(EGLint engine)
871{
Mathias Agopian518ec112011-05-13 16:21:08 -0700872 clearError();
873
Mathias Agopianada798b2012-02-13 17:09:30 -0800874 egl_connection_t* const cnx = &gEGLImpl;
875 if (!cnx->dso)
876 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
877
878 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700879}
880
881EGLint eglGetError(void)
882{
Mathias Agopianada798b2012-02-13 17:09:30 -0800883 EGLint err = EGL_SUCCESS;
884 egl_connection_t* const cnx = &gEGLImpl;
885 if (cnx->dso) {
886 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700887 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800888 if (err == EGL_SUCCESS) {
889 err = egl_tls_t::getError();
890 }
891 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700892}
893
Michael Chockc0ec5e22014-01-27 08:14:33 -0800894static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700895 const char* procname) {
896 const egl_connection_t* cnx = &gEGLImpl;
897 void* proc = NULL;
898
Michael Chockc0ec5e22014-01-27 08:14:33 -0800899 proc = dlsym(cnx->libEgl, procname);
900 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
901
Jesse Hallc07b5202013-07-04 12:08:16 -0700902 proc = dlsym(cnx->libGles2, procname);
903 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
904
905 proc = dlsym(cnx->libGles1, procname);
906 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
907
908 return NULL;
909}
910
Mathias Agopian518ec112011-05-13 16:21:08 -0700911__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
912{
913 // eglGetProcAddress() could be the very first function called
914 // in which case we must make sure we've initialized ourselves, this
915 // happens the first time egl_get_display() is called.
916
917 clearError();
918
919 if (egl_init_drivers() == EGL_FALSE) {
920 setError(EGL_BAD_PARAMETER, NULL);
921 return NULL;
922 }
923
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700924 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700925 return NULL;
926 }
927
Mathias Agopian518ec112011-05-13 16:21:08 -0700928 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700929 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700930 if (addr) return addr;
931
Michael Chockc0ec5e22014-01-27 08:14:33 -0800932 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700933 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700934
Mathias Agopian518ec112011-05-13 16:21:08 -0700935 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
936 pthread_mutex_lock(&sExtensionMapMutex);
937
938 /*
939 * Since eglGetProcAddress() is not associated to anything, it needs
940 * to return a function pointer that "works" regardless of what
941 * the current context is.
942 *
943 * For this reason, we return a "forwarder", a small stub that takes
944 * care of calling the function associated with the context
945 * currently bound.
946 *
947 * We first look for extensions we've already resolved, if we're seeing
948 * this extension for the first time, we go through all our
949 * implementations and call eglGetProcAddress() and record the
950 * result in the appropriate implementation hooks and return the
951 * address of the forwarder corresponding to that hook set.
952 *
953 */
954
955 const String8 name(procname);
956 addr = sGLExtentionMap.valueFor(name);
957 const int slot = sGLExtentionSlot;
958
Steve Blocke6f43dd2012-01-06 19:20:56 +0000959 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700960 "no more slots for eglGetProcAddress(\"%s\")",
961 procname);
962
963 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
964 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800965
966 egl_connection_t* const cnx = &gEGLImpl;
967 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800968 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800969 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800970 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
971 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800972 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800973 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700974 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800975
Mathias Agopian518ec112011-05-13 16:21:08 -0700976 if (found) {
977 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700978 sGLExtentionMap.add(name, addr);
979 sGLExtentionSlot++;
980 }
981 }
982
983 pthread_mutex_unlock(&sExtensionMapMutex);
984 return addr;
985}
986
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700987class FrameCompletionThread : public Thread {
988public:
989
990 static void queueSync(EGLSyncKHR sync) {
991 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
992 static bool running = false;
993 if (!running) {
994 thread->run("GPUFrameCompletion");
995 running = true;
996 }
997 {
998 Mutex::Autolock lock(thread->mMutex);
999 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1000 thread->mFramesQueued).string());
1001 thread->mQueue.push_back(sync);
1002 thread->mCondition.signal();
1003 thread->mFramesQueued++;
1004 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1005 }
1006 }
1007
1008private:
1009 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1010
1011 virtual bool threadLoop() {
1012 EGLSyncKHR sync;
1013 uint32_t frameNum;
1014 {
1015 Mutex::Autolock lock(mMutex);
1016 while (mQueue.isEmpty()) {
1017 mCondition.wait(mMutex);
1018 }
1019 sync = mQueue[0];
1020 frameNum = mFramesCompleted;
1021 }
1022 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1023 {
1024 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1025 frameNum).string());
1026 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1027 if (result == EGL_FALSE) {
1028 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1029 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1030 ALOGE("FrameCompletion: timeout waiting for fence");
1031 }
1032 eglDestroySyncKHR(dpy, sync);
1033 }
1034 {
1035 Mutex::Autolock lock(mMutex);
1036 mQueue.removeAt(0);
1037 mFramesCompleted++;
1038 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1039 }
1040 return true;
1041 }
1042
1043 uint32_t mFramesQueued;
1044 uint32_t mFramesCompleted;
1045 Vector<EGLSyncKHR> mQueue;
1046 Condition mCondition;
1047 Mutex mMutex;
1048};
1049
Dan Stozaa894d082015-02-19 15:27:36 -08001050EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1051 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001052{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001053 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001054 clearError();
1055
Jesse Hallb29e5e82012-04-04 16:53:42 -07001056 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001057 if (!dp) return EGL_FALSE;
1058
Jesse Hallb29e5e82012-04-04 16:53:42 -07001059 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001060 if (!_s.get())
1061 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001062
Mathias Agopian518ec112011-05-13 16:21:08 -07001063 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001064
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001065 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1066 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1067 if (sync != EGL_NO_SYNC_KHR) {
1068 FrameCompletionThread::queueSync(sync);
1069 }
1070 }
1071
Mathias Agopian7db993a2012-03-25 00:49:46 -07001072 if (CC_UNLIKELY(dp->finishOnSwap)) {
1073 uint32_t pixel;
1074 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1075 if (c) {
1076 // glReadPixels() ensures that the frame is complete
1077 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1078 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1079 }
1080 }
1081
Dan Stozaa894d082015-02-19 15:27:36 -08001082 if (n_rects == 0) {
1083 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1084 }
1085
1086 Vector<android_native_rect_t> androidRects;
1087 for (int r = 0; r < n_rects; ++r) {
1088 int offset = r * 4;
1089 int x = rects[offset];
1090 int y = rects[offset + 1];
1091 int width = rects[offset + 2];
1092 int height = rects[offset + 3];
1093 android_native_rect_t androidRect;
1094 androidRect.left = x;
1095 androidRect.top = y + height;
1096 androidRect.right = x + width;
1097 androidRect.bottom = y;
1098 androidRects.push_back(androidRect);
1099 }
1100 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1101 androidRects.size());
1102
1103 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1104 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1105 rects, n_rects);
1106 } else {
1107 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1108 }
1109}
1110
1111EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1112{
1113 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001114}
1115
1116EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1117 NativePixmapType target)
1118{
1119 clearError();
1120
Jesse Hallb29e5e82012-04-04 16:53:42 -07001121 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001122 if (!dp) return EGL_FALSE;
1123
Jesse Hallb29e5e82012-04-04 16:53:42 -07001124 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001125 if (!_s.get())
1126 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001127
Mathias Agopian518ec112011-05-13 16:21:08 -07001128 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001129 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001130}
1131
1132const char* eglQueryString(EGLDisplay dpy, EGLint name)
1133{
1134 clearError();
1135
Chia-I Wue57d1352016-08-15 16:10:02 +08001136 // Generate an error quietly when client extensions (as defined by
1137 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1138 // validate_display to generate the error as validate_display would log
1139 // the error, which can be misleading.
1140 //
1141 // If we want to support EGL_EXT_client_extensions later, we can return
1142 // the client extension string here instead.
1143 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1144 return setErrorQuiet(EGL_BAD_DISPLAY, nullptr);
1145
Jesse Hallb29e5e82012-04-04 16:53:42 -07001146 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001147 if (!dp) return (const char *) NULL;
1148
1149 switch (name) {
1150 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001151 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001152 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001153 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001154 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001155 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001156 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001157 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001158 }
1159 return setError(EGL_BAD_PARAMETER, (const char *)0);
1160}
1161
Mathias Agopianca088332013-03-28 17:44:13 -07001162EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1163{
1164 clearError();
1165
1166 const egl_display_ptr dp = validate_display(dpy);
1167 if (!dp) return (const char *) NULL;
1168
1169 switch (name) {
1170 case EGL_VENDOR:
1171 return dp->disp.queryString.vendor;
1172 case EGL_VERSION:
1173 return dp->disp.queryString.version;
1174 case EGL_EXTENSIONS:
1175 return dp->disp.queryString.extensions;
1176 case EGL_CLIENT_APIS:
1177 return dp->disp.queryString.clientApi;
1178 }
1179 return setError(EGL_BAD_PARAMETER, (const char *)0);
1180}
Mathias Agopian518ec112011-05-13 16:21:08 -07001181
1182// ----------------------------------------------------------------------------
1183// EGL 1.1
1184// ----------------------------------------------------------------------------
1185
1186EGLBoolean eglSurfaceAttrib(
1187 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1188{
1189 clearError();
1190
Jesse Hallb29e5e82012-04-04 16:53:42 -07001191 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001192 if (!dp) return EGL_FALSE;
1193
Jesse Hallb29e5e82012-04-04 16:53:42 -07001194 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001195 if (!_s.get())
1196 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001197
Pablo Ceballosc18be292016-05-31 14:55:42 -07001198 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001199
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001200 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001201 int err = native_window_set_auto_refresh(s->win.get(),
1202 value ? true : false);
1203 return (err == NO_ERROR) ? EGL_TRUE :
1204 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001205 }
1206
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001207#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001208 if (attribute == EGL_TIMESTAMPS_ANDROID) {
1209 s->enableTimestamps = value;
1210 return EGL_TRUE;
1211 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001212#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001213
Mathias Agopian518ec112011-05-13 16:21:08 -07001214 if (s->cnx->egl.eglSurfaceAttrib) {
1215 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001216 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001217 }
1218 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1219}
1220
1221EGLBoolean eglBindTexImage(
1222 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1223{
1224 clearError();
1225
Jesse Hallb29e5e82012-04-04 16:53:42 -07001226 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001227 if (!dp) return EGL_FALSE;
1228
Jesse Hallb29e5e82012-04-04 16:53:42 -07001229 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001230 if (!_s.get())
1231 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001232
Mathias Agopian518ec112011-05-13 16:21:08 -07001233 egl_surface_t const * const s = get_surface(surface);
1234 if (s->cnx->egl.eglBindTexImage) {
1235 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001236 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001237 }
1238 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1239}
1240
1241EGLBoolean eglReleaseTexImage(
1242 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1243{
1244 clearError();
1245
Jesse Hallb29e5e82012-04-04 16:53:42 -07001246 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001247 if (!dp) return EGL_FALSE;
1248
Jesse Hallb29e5e82012-04-04 16:53:42 -07001249 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001250 if (!_s.get())
1251 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001252
Mathias Agopian518ec112011-05-13 16:21:08 -07001253 egl_surface_t const * const s = get_surface(surface);
1254 if (s->cnx->egl.eglReleaseTexImage) {
1255 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001256 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001257 }
1258 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1259}
1260
1261EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1262{
1263 clearError();
1264
Jesse Hallb29e5e82012-04-04 16:53:42 -07001265 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001266 if (!dp) return EGL_FALSE;
1267
1268 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001269 egl_connection_t* const cnx = &gEGLImpl;
1270 if (cnx->dso && cnx->egl.eglSwapInterval) {
1271 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001272 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001273
Mathias Agopian518ec112011-05-13 16:21:08 -07001274 return res;
1275}
1276
1277
1278// ----------------------------------------------------------------------------
1279// EGL 1.2
1280// ----------------------------------------------------------------------------
1281
1282EGLBoolean eglWaitClient(void)
1283{
1284 clearError();
1285
Mathias Agopianada798b2012-02-13 17:09:30 -08001286 egl_connection_t* const cnx = &gEGLImpl;
1287 if (!cnx->dso)
1288 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1289
1290 EGLBoolean res;
1291 if (cnx->egl.eglWaitClient) {
1292 res = cnx->egl.eglWaitClient();
1293 } else {
1294 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001295 }
1296 return res;
1297}
1298
1299EGLBoolean eglBindAPI(EGLenum api)
1300{
1301 clearError();
1302
1303 if (egl_init_drivers() == EGL_FALSE) {
1304 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1305 }
1306
1307 // bind this API on all EGLs
1308 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001309 egl_connection_t* const cnx = &gEGLImpl;
1310 if (cnx->dso && cnx->egl.eglBindAPI) {
1311 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001312 }
1313 return res;
1314}
1315
1316EGLenum eglQueryAPI(void)
1317{
1318 clearError();
1319
1320 if (egl_init_drivers() == EGL_FALSE) {
1321 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1322 }
1323
Mathias Agopianada798b2012-02-13 17:09:30 -08001324 egl_connection_t* const cnx = &gEGLImpl;
1325 if (cnx->dso && cnx->egl.eglQueryAPI) {
1326 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001327 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001328
Mathias Agopian518ec112011-05-13 16:21:08 -07001329 // or, it can only be OpenGL ES
1330 return EGL_OPENGL_ES_API;
1331}
1332
1333EGLBoolean eglReleaseThread(void)
1334{
1335 clearError();
1336
Mathias Agopianada798b2012-02-13 17:09:30 -08001337 egl_connection_t* const cnx = &gEGLImpl;
1338 if (cnx->dso && cnx->egl.eglReleaseThread) {
1339 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001340 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301341
1342 // If there is context bound to the thread, release it
1343 egl_display_t::loseCurrent(get_context(getContext()));
1344
Mathias Agopian518ec112011-05-13 16:21:08 -07001345 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001346 return EGL_TRUE;
1347}
1348
1349EGLSurface eglCreatePbufferFromClientBuffer(
1350 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1351 EGLConfig config, const EGLint *attrib_list)
1352{
1353 clearError();
1354
Jesse Hallb29e5e82012-04-04 16:53:42 -07001355 egl_connection_t* cnx = NULL;
1356 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1357 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001358 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1359 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001360 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001361 }
1362 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1363}
1364
1365// ----------------------------------------------------------------------------
1366// EGL_EGLEXT_VERSION 3
1367// ----------------------------------------------------------------------------
1368
1369EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1370 const EGLint *attrib_list)
1371{
1372 clearError();
1373
Jesse Hallb29e5e82012-04-04 16:53:42 -07001374 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001375 if (!dp) return EGL_FALSE;
1376
Jesse Hallb29e5e82012-04-04 16:53:42 -07001377 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001378 if (!_s.get())
1379 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001380
1381 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001382 if (s->cnx->egl.eglLockSurfaceKHR) {
1383 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001384 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001385 }
1386 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1387}
1388
1389EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1390{
1391 clearError();
1392
Jesse Hallb29e5e82012-04-04 16:53:42 -07001393 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001394 if (!dp) return EGL_FALSE;
1395
Jesse Hallb29e5e82012-04-04 16:53:42 -07001396 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001397 if (!_s.get())
1398 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001399
1400 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001401 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001402 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001403 }
1404 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1405}
1406
1407EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1408 EGLClientBuffer buffer, const EGLint *attrib_list)
1409{
1410 clearError();
1411
Jesse Hallb29e5e82012-04-04 16:53:42 -07001412 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001413 if (!dp) return EGL_NO_IMAGE_KHR;
1414
Jesse Hallb29e5e82012-04-04 16:53:42 -07001415 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001416 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001417
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001418 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1419 egl_connection_t* const cnx = &gEGLImpl;
1420 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1421 result = cnx->egl.eglCreateImageKHR(
1422 dp->disp.dpy,
1423 c ? c->context : EGL_NO_CONTEXT,
1424 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001425 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001426 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001427}
1428
1429EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1430{
1431 clearError();
1432
Jesse Hallb29e5e82012-04-04 16:53:42 -07001433 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001434 if (!dp) return EGL_FALSE;
1435
Steven Holte646a5c52012-06-04 20:02:11 -07001436 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001437 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001438 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001439 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001440 }
Steven Holte646a5c52012-06-04 20:02:11 -07001441 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001442}
1443
1444// ----------------------------------------------------------------------------
1445// EGL_EGLEXT_VERSION 5
1446// ----------------------------------------------------------------------------
1447
1448
1449EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1450{
1451 clearError();
1452
Jesse Hallb29e5e82012-04-04 16:53:42 -07001453 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001454 if (!dp) return EGL_NO_SYNC_KHR;
1455
Mathias Agopian518ec112011-05-13 16:21:08 -07001456 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001457 egl_connection_t* const cnx = &gEGLImpl;
1458 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1459 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001460 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001461 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001462}
1463
1464EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1465{
1466 clearError();
1467
Jesse Hallb29e5e82012-04-04 16:53:42 -07001468 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001469 if (!dp) return EGL_FALSE;
1470
Mathias Agopian518ec112011-05-13 16:21:08 -07001471 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001472 egl_connection_t* const cnx = &gEGLImpl;
1473 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1474 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001475 }
1476 return result;
1477}
1478
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001479EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1480 clearError();
1481
1482 const egl_display_ptr dp = validate_display(dpy);
1483 if (!dp) return EGL_FALSE;
1484
1485 EGLBoolean result = EGL_FALSE;
1486 egl_connection_t* const cnx = &gEGLImpl;
1487 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1488 result = cnx->egl.eglSignalSyncKHR(
1489 dp->disp.dpy, sync, mode);
1490 }
1491 return result;
1492}
1493
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001494EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1495 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001496{
1497 clearError();
1498
Jesse Hallb29e5e82012-04-04 16:53:42 -07001499 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001500 if (!dp) return EGL_FALSE;
1501
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001502 EGLBoolean result = EGL_FALSE;
1503 egl_connection_t* const cnx = &gEGLImpl;
1504 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1505 result = cnx->egl.eglClientWaitSyncKHR(
1506 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001507 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001508 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001509}
1510
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001511EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1512 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001513{
1514 clearError();
1515
Jesse Hallb29e5e82012-04-04 16:53:42 -07001516 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001517 if (!dp) return EGL_FALSE;
1518
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001519 EGLBoolean result = EGL_FALSE;
1520 egl_connection_t* const cnx = &gEGLImpl;
1521 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1522 result = cnx->egl.eglGetSyncAttribKHR(
1523 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001524 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001525 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001526}
1527
Season Li000d88f2015-07-01 11:39:40 -07001528EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1529{
1530 clearError();
1531
1532 const egl_display_ptr dp = validate_display(dpy);
1533 if (!dp) return EGL_NO_STREAM_KHR;
1534
1535 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1536 egl_connection_t* const cnx = &gEGLImpl;
1537 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1538 result = cnx->egl.eglCreateStreamKHR(
1539 dp->disp.dpy, attrib_list);
1540 }
1541 return result;
1542}
1543
1544EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1545{
1546 clearError();
1547
1548 const egl_display_ptr dp = validate_display(dpy);
1549 if (!dp) return EGL_FALSE;
1550
1551 EGLBoolean result = EGL_FALSE;
1552 egl_connection_t* const cnx = &gEGLImpl;
1553 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1554 result = cnx->egl.eglDestroyStreamKHR(
1555 dp->disp.dpy, stream);
1556 }
1557 return result;
1558}
1559
1560EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1561 EGLenum attribute, EGLint value)
1562{
1563 clearError();
1564
1565 const egl_display_ptr dp = validate_display(dpy);
1566 if (!dp) return EGL_FALSE;
1567
1568 EGLBoolean result = EGL_FALSE;
1569 egl_connection_t* const cnx = &gEGLImpl;
1570 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1571 result = cnx->egl.eglStreamAttribKHR(
1572 dp->disp.dpy, stream, attribute, value);
1573 }
1574 return result;
1575}
1576
1577EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1578 EGLenum attribute, EGLint *value)
1579{
1580 clearError();
1581
1582 const egl_display_ptr dp = validate_display(dpy);
1583 if (!dp) return EGL_FALSE;
1584
1585 EGLBoolean result = EGL_FALSE;
1586 egl_connection_t* const cnx = &gEGLImpl;
1587 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1588 result = cnx->egl.eglQueryStreamKHR(
1589 dp->disp.dpy, stream, attribute, value);
1590 }
1591 return result;
1592}
1593
1594EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1595 EGLenum attribute, EGLuint64KHR *value)
1596{
1597 clearError();
1598
1599 const egl_display_ptr dp = validate_display(dpy);
1600 if (!dp) return EGL_FALSE;
1601
1602 EGLBoolean result = EGL_FALSE;
1603 egl_connection_t* const cnx = &gEGLImpl;
1604 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1605 result = cnx->egl.eglQueryStreamu64KHR(
1606 dp->disp.dpy, stream, attribute, value);
1607 }
1608 return result;
1609}
1610
1611EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1612 EGLenum attribute, EGLTimeKHR *value)
1613{
1614 clearError();
1615
1616 const egl_display_ptr dp = validate_display(dpy);
1617 if (!dp) return EGL_FALSE;
1618
1619 EGLBoolean result = EGL_FALSE;
1620 egl_connection_t* const cnx = &gEGLImpl;
1621 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1622 result = cnx->egl.eglQueryStreamTimeKHR(
1623 dp->disp.dpy, stream, attribute, value);
1624 }
1625 return result;
1626}
1627
1628EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1629 EGLStreamKHR stream, const EGLint *attrib_list)
1630{
1631 clearError();
1632
1633 egl_display_ptr dp = validate_display(dpy);
1634 if (!dp) return EGL_NO_SURFACE;
1635
1636 egl_connection_t* const cnx = &gEGLImpl;
1637 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1638 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1639 dp->disp.dpy, config, stream, attrib_list);
1640 if (surface != EGL_NO_SURFACE) {
1641 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1642 surface, cnx);
1643 return s;
1644 }
1645 }
1646 return EGL_NO_SURFACE;
1647}
1648
1649EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1650 EGLStreamKHR stream)
1651{
1652 clearError();
1653
1654 const egl_display_ptr dp = validate_display(dpy);
1655 if (!dp) return EGL_FALSE;
1656
1657 EGLBoolean result = EGL_FALSE;
1658 egl_connection_t* const cnx = &gEGLImpl;
1659 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1660 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1661 dp->disp.dpy, stream);
1662 }
1663 return result;
1664}
1665
1666EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1667 EGLStreamKHR stream)
1668{
1669 clearError();
1670
1671 const egl_display_ptr dp = validate_display(dpy);
1672 if (!dp) return EGL_FALSE;
1673
1674 EGLBoolean result = EGL_FALSE;
1675 egl_connection_t* const cnx = &gEGLImpl;
1676 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1677 result = cnx->egl.eglStreamConsumerAcquireKHR(
1678 dp->disp.dpy, stream);
1679 }
1680 return result;
1681}
1682
1683EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1684 EGLStreamKHR stream)
1685{
1686 clearError();
1687
1688 const egl_display_ptr dp = validate_display(dpy);
1689 if (!dp) return EGL_FALSE;
1690
1691 EGLBoolean result = EGL_FALSE;
1692 egl_connection_t* const cnx = &gEGLImpl;
1693 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1694 result = cnx->egl.eglStreamConsumerReleaseKHR(
1695 dp->disp.dpy, stream);
1696 }
1697 return result;
1698}
1699
1700EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1701 EGLDisplay dpy, EGLStreamKHR stream)
1702{
1703 clearError();
1704
1705 const egl_display_ptr dp = validate_display(dpy);
1706 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1707
1708 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1709 egl_connection_t* const cnx = &gEGLImpl;
1710 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1711 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1712 dp->disp.dpy, stream);
1713 }
1714 return result;
1715}
1716
1717EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1718 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1719{
1720 clearError();
1721
1722 const egl_display_ptr dp = validate_display(dpy);
1723 if (!dp) return EGL_NO_STREAM_KHR;
1724
1725 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1726 egl_connection_t* const cnx = &gEGLImpl;
1727 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1728 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1729 dp->disp.dpy, file_descriptor);
1730 }
1731 return result;
1732}
1733
Mathias Agopian518ec112011-05-13 16:21:08 -07001734// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001735// EGL_EGLEXT_VERSION 15
1736// ----------------------------------------------------------------------------
1737
1738EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1739 clearError();
1740 const egl_display_ptr dp = validate_display(dpy);
1741 if (!dp) return EGL_FALSE;
1742 EGLint result = EGL_FALSE;
1743 egl_connection_t* const cnx = &gEGLImpl;
1744 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1745 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1746 }
1747 return result;
1748}
1749
1750// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001751// ANDROID extensions
1752// ----------------------------------------------------------------------------
1753
Jamie Gennis331841b2012-09-06 14:52:00 -07001754EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1755{
1756 clearError();
1757
1758 const egl_display_ptr dp = validate_display(dpy);
1759 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1760
1761 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1762 egl_connection_t* const cnx = &gEGLImpl;
1763 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1764 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1765 }
1766 return result;
1767}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001768
Andy McFadden72841452013-03-01 16:25:32 -08001769EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1770 EGLnsecsANDROID time)
1771{
1772 clearError();
1773
1774 const egl_display_ptr dp = validate_display(dpy);
1775 if (!dp) {
1776 return EGL_FALSE;
1777 }
1778
1779 SurfaceRef _s(dp.get(), surface);
1780 if (!_s.get()) {
1781 setError(EGL_BAD_SURFACE, EGL_FALSE);
1782 return EGL_FALSE;
1783 }
1784
1785 egl_surface_t const * const s = get_surface(surface);
1786 native_window_set_buffers_timestamp(s->win.get(), time);
1787
1788 return EGL_TRUE;
1789}
1790
Craig Donner05249fc2016-01-15 19:33:55 -08001791EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1792{
1793 clearError();
1794
1795 int usage = 0;
1796 uint32_t width = 0;
1797 uint32_t height = 0;
1798 uint32_t format = 0;
1799 uint32_t red_size = 0;
1800 uint32_t green_size = 0;
1801 uint32_t blue_size = 0;
1802 uint32_t alpha_size = 0;
1803
Craig Donnerb40504a2016-06-03 17:54:25 -07001804#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001805 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001806 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001807 target = value; \
1808 } else { \
1809 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1810 } \
1811 break
1812
1813 if (attrib_list) {
1814 while (*attrib_list != EGL_NONE) {
1815 GLint attr = *attrib_list++;
1816 GLint value = *attrib_list++;
1817 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001818 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1819 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1820 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1821 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1822 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1823 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner05249fc2016-01-15 19:33:55 -08001824 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1825 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1826 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001827 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001828 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001829 usage |= GRALLOC_USAGE_HW_RENDER;
1830 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001831 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001832 usage |= GRALLOC_USAGE_HW_TEXTURE;
1833 }
1834 // The buffer must be used for either a texture or a
1835 // renderbuffer.
Craig Donner8cfae6d2016-04-12 16:54:03 -07001836 if ((value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) &&
1837 (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID)) {
Craig Donner05249fc2016-01-15 19:33:55 -08001838 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1839 }
1840 break;
1841 default:
1842 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1843 }
1844 }
1845 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001846#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001847
1848 // Validate format.
1849 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1850 if (alpha_size == 8) {
1851 format = HAL_PIXEL_FORMAT_RGBA_8888;
1852 } else {
1853 format = HAL_PIXEL_FORMAT_RGB_888;
1854 }
1855 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1856 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001857 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001858 } else {
1859 ALOGE("Invalid native pixel format { r=%d, g=%d, b=%d, a=%d }",
1860 red_size, green_size, blue_size, alpha_size);
1861 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1862 }
1863
1864 GraphicBuffer* gBuffer = new GraphicBuffer(width, height, format, usage);
1865 const status_t err = gBuffer->initCheck();
1866 if (err != NO_ERROR) {
1867 ALOGE("Unable to create native buffer { w=%d, h=%d, f=%d, u=%#x }: %#x",
1868 width, height, format, usage, err);
1869 // Destroy the buffer.
1870 sp<GraphicBuffer> holder(gBuffer);
1871 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
1872 }
1873 ALOGD("Created new native buffer %p { w=%d, h=%d, f=%d, u=%#x }",
1874 gBuffer, width, height, format, usage);
1875 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
1876}
1877
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001878// ----------------------------------------------------------------------------
1879// NVIDIA extensions
1880// ----------------------------------------------------------------------------
1881EGLuint64NV eglGetSystemTimeFrequencyNV()
1882{
1883 clearError();
1884
1885 if (egl_init_drivers() == EGL_FALSE) {
1886 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1887 }
1888
1889 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001890 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001891
Mathias Agopianada798b2012-02-13 17:09:30 -08001892 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1893 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001894 }
1895
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001896 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001897}
1898
1899EGLuint64NV eglGetSystemTimeNV()
1900{
1901 clearError();
1902
1903 if (egl_init_drivers() == EGL_FALSE) {
1904 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1905 }
1906
1907 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001908 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001909
Mathias Agopianada798b2012-02-13 17:09:30 -08001910 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1911 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001912 }
1913
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001914 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001915}
Dan Stozaa894d082015-02-19 15:27:36 -08001916
1917// ----------------------------------------------------------------------------
1918// Partial update extension
1919// ----------------------------------------------------------------------------
1920EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1921 EGLint *rects, EGLint n_rects)
1922{
1923 clearError();
1924
1925 const egl_display_ptr dp = validate_display(dpy);
1926 if (!dp) {
1927 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1928 return EGL_FALSE;
1929 }
1930
1931 SurfaceRef _s(dp.get(), surface);
1932 if (!_s.get()) {
1933 setError(EGL_BAD_SURFACE, EGL_FALSE);
1934 return EGL_FALSE;
1935 }
1936
1937 egl_surface_t const * const s = get_surface(surface);
1938 if (s->cnx->egl.eglSetDamageRegionKHR) {
1939 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1940 rects, n_rects);
1941 }
1942
1943 return EGL_FALSE;
1944}
Pablo Ceballosc18be292016-05-31 14:55:42 -07001945
1946EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
1947 EGLint framesAgo, EGLint numTimestamps, const EGLint *timestamps,
1948 EGLnsecsANDROID *values)
1949{
1950 clearError();
1951
1952 const egl_display_ptr dp = validate_display(dpy);
1953 if (!dp) {
1954 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1955 return EGL_FALSE;
1956 }
1957
1958 SurfaceRef _s(dp.get(), surface);
1959 if (!_s.get()) {
1960 setError(EGL_BAD_SURFACE, EGL_FALSE);
1961 return EGL_FALSE;
1962 }
1963
1964 egl_surface_t const * const s = get_surface(surface);
1965
1966 if (!s->enableTimestamps) {
1967 setError(EGL_BAD_SURFACE, EGL_FALSE);
1968 return EGL_FALSE;
1969 }
1970
1971 nsecs_t* postedTime = nullptr;
1972 nsecs_t* acquireTime = nullptr;
1973 nsecs_t* refreshStartTime = nullptr;
1974 nsecs_t* GLCompositionDoneTime = nullptr;
1975 nsecs_t* displayRetireTime = nullptr;
1976 nsecs_t* releaseTime = nullptr;
1977
1978 for (int i = 0; i < numTimestamps; i++) {
1979 switch (timestamps[i]) {
1980 case EGL_QUEUE_TIME_ANDROID:
1981 postedTime = &values[i];
1982 break;
1983 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
1984 acquireTime = &values[i];
1985 break;
1986 case EGL_COMPOSITION_START_TIME_ANDROID:
1987 refreshStartTime = &values[i];
1988 break;
1989 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
1990 GLCompositionDoneTime = &values[i];
1991 break;
1992 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
1993 displayRetireTime = &values[i];
1994 break;
1995 case EGL_READS_DONE_TIME_ANDROID:
1996 releaseTime = &values[i];
1997 break;
1998 default:
1999 setError(EGL_BAD_PARAMETER, EGL_FALSE);
2000 return EGL_FALSE;
2001 }
2002 }
2003
2004 status_t ret = native_window_get_frame_timestamps(s->win.get(), framesAgo,
2005 postedTime, acquireTime, refreshStartTime, GLCompositionDoneTime,
2006 displayRetireTime, releaseTime);
2007
2008 if (ret != NO_ERROR) {
2009 setError(EGL_BAD_ACCESS, EGL_FALSE);
2010 return EGL_FALSE;
2011 }
2012
2013 return EGL_TRUE;
2014}
2015
2016EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2017 EGLint timestamp)
2018{
2019 clearError();
2020
2021 const egl_display_ptr dp = validate_display(dpy);
2022 if (!dp) {
2023 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2024 return EGL_FALSE;
2025 }
2026
2027 SurfaceRef _s(dp.get(), surface);
2028 if (!_s.get()) {
2029 setError(EGL_BAD_SURFACE, EGL_FALSE);
2030 return EGL_FALSE;
2031 }
2032
2033 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002034#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07002035 case EGL_QUEUE_TIME_ANDROID:
2036 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2037 case EGL_COMPOSITION_START_TIME_ANDROID:
2038 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
2039 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2040 case EGL_READS_DONE_TIME_ANDROID:
2041 return EGL_TRUE;
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002042#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002043 default:
2044 return EGL_FALSE;
2045 }
2046}