blob: 613b63b28c48e4b9a7426ef43afe714e900a0bc6 [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 "
Jesse Hall21558da2013-08-06 15:31:22 -070087 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070088extern char const * const gExtensionString =
89 "EGL_KHR_image " // mandatory
90 "EGL_KHR_image_base " // mandatory
91 "EGL_KHR_image_pixmap "
92 "EGL_KHR_lock_surface "
Jesse Halla2ba4282013-09-14 21:00:14 -070093#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
Jesse Hallc2e41222013-08-08 13:40:22 -070094 "EGL_KHR_gl_colorspace "
Jesse Halla2ba4282013-09-14 21:00:14 -070095#endif
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070096 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070097 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070098 "EGL_KHR_gl_texture_cubemap_image "
99 "EGL_KHR_gl_renderbuffer_image "
100 "EGL_KHR_reusable_sync "
101 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700102 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700103 "EGL_KHR_config_attribs "
104 "EGL_KHR_surfaceless_context "
105 "EGL_KHR_stream "
106 "EGL_KHR_stream_fifo "
107 "EGL_KHR_stream_producer_eglsurface "
108 "EGL_KHR_stream_consumer_gltexture "
109 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700110 "EGL_EXT_create_context_robustness "
111 "EGL_NV_system_time "
112 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700113 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700114 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800115 "EGL_KHR_partial_update " // strongly recommended
116 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700117 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700118 "EGL_KHR_mutable_render_buffer "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700119 ;
120
121// extensions not exposed to applications but used by the ANDROID system
122// "EGL_ANDROID_blob_cache " // strongly recommended
123// "EGL_IMG_hibernate_process " // optional
124// "EGL_ANDROID_native_fence_sync " // strongly recommended
125// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700126// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700127
128/*
129 * EGL Extensions entry-points exposed to 3rd party applications
130 * (keep in sync with gExtensionString above)
131 *
132 */
133static const extention_map_t sExtensionMap[] = {
134 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700135 { "eglLockSurfaceKHR",
136 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
137 { "eglUnlockSurfaceKHR",
138 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700139
140 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700141 { "eglCreateImageKHR",
142 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
143 { "eglDestroyImageKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700145
146 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
147 { "eglCreateSyncKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
149 { "eglDestroySyncKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
151 { "eglClientWaitSyncKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
153 { "eglSignalSyncKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
155 { "eglGetSyncAttribKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
157
158 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800159 { "eglGetSystemTimeFrequencyNV",
160 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
161 { "eglGetSystemTimeNV",
162 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700163
Mathias Agopian2bb71682013-03-27 17:32:41 -0700164 // EGL_KHR_wait_sync
165 { "eglWaitSyncKHR",
166 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700167
168 // EGL_ANDROID_presentation_time
169 { "eglPresentationTimeANDROID",
170 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800171
172 // EGL_KHR_swap_buffers_with_damage
173 { "eglSwapBuffersWithDamageKHR",
174 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
175
Craig Donner05249fc2016-01-15 19:33:55 -0800176 // EGL_ANDROID_native_client_buffer
177 { "eglCreateNativeClientBufferANDROID",
178 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
179
Dan Stozaa894d082015-02-19 15:27:36 -0800180 // EGL_KHR_partial_update
181 { "eglSetDamageRegionKHR",
182 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700183
184 { "eglCreateStreamKHR",
185 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
186 { "eglDestroyStreamKHR",
187 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
188 { "eglStreamAttribKHR",
189 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
190 { "eglQueryStreamKHR",
191 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
192 { "eglQueryStreamu64KHR",
193 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
194 { "eglQueryStreamTimeKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
196 { "eglCreateStreamProducerSurfaceKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
198 { "eglStreamConsumerGLTextureExternalKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
200 { "eglStreamConsumerAcquireKHR",
201 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
202 { "eglStreamConsumerReleaseKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
204 { "eglGetStreamFileDescriptorKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
206 { "eglCreateStreamFromFileDescriptorKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Mathias Agopian518ec112011-05-13 16:21:08 -0700208};
209
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700210/*
211 * These extensions entry-points should not be exposed to applications.
212 * They're used internally by the Android EGL layer.
213 */
214#define FILTER_EXTENSIONS(procname) \
215 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
216 !strcmp((procname), "eglHibernateProcessIMG") || \
217 !strcmp((procname), "eglAwakenProcessIMG") || \
218 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
219
220
221
Mathias Agopian518ec112011-05-13 16:21:08 -0700222// accesses protected by sExtensionMapMutex
223static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
224static int sGLExtentionSlot = 0;
225static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
226
227static void(*findProcAddress(const char* name,
228 const extention_map_t* map, size_t n))() {
229 for (uint32_t i=0 ; i<n ; i++) {
230 if (!strcmp(name, map[i].name)) {
231 return map[i].address;
232 }
233 }
234 return NULL;
235}
236
237// ----------------------------------------------------------------------------
238
Mathias Agopian518ec112011-05-13 16:21:08 -0700239extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
240extern EGLBoolean egl_init_drivers();
241extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700242extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700243
Mathias Agopian518ec112011-05-13 16:21:08 -0700244} // namespace android;
245
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700246
Mathias Agopian518ec112011-05-13 16:21:08 -0700247// ----------------------------------------------------------------------------
248
249static inline void clearError() { egl_tls_t::clearError(); }
250static inline EGLContext getContext() { return egl_tls_t::getContext(); }
251
252// ----------------------------------------------------------------------------
253
254EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
255{
256 clearError();
257
Dan Stozac3289c42014-01-17 11:38:34 -0800258 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700259 if (index >= NUM_DISPLAYS) {
260 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
261 }
262
263 if (egl_init_drivers() == EGL_FALSE) {
264 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
265 }
266
267 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
268 return dpy;
269}
270
271// ----------------------------------------------------------------------------
272// Initialization
273// ----------------------------------------------------------------------------
274
275EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
276{
277 clearError();
278
Jesse Hallb29e5e82012-04-04 16:53:42 -0700279 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700280 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
281
282 EGLBoolean res = dp->initialize(major, minor);
283
284 return res;
285}
286
287EGLBoolean eglTerminate(EGLDisplay dpy)
288{
289 // NOTE: don't unload the drivers b/c some APIs can be called
290 // after eglTerminate() has been called. eglTerminate() only
291 // terminates an EGLDisplay, not a EGL itself.
292
293 clearError();
294
Jesse Hallb29e5e82012-04-04 16:53:42 -0700295 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700296 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
297
298 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800299
Mathias Agopian518ec112011-05-13 16:21:08 -0700300 return res;
301}
302
303// ----------------------------------------------------------------------------
304// configuration
305// ----------------------------------------------------------------------------
306
307EGLBoolean eglGetConfigs( EGLDisplay dpy,
308 EGLConfig *configs,
309 EGLint config_size, EGLint *num_config)
310{
311 clearError();
312
Jesse Hallb29e5e82012-04-04 16:53:42 -0700313 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700314 if (!dp) return EGL_FALSE;
315
Mathias Agopian7773c432012-02-13 20:06:08 -0800316 if (num_config==0) {
317 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700318 }
319
Mathias Agopian7773c432012-02-13 20:06:08 -0800320 EGLBoolean res = EGL_FALSE;
321 *num_config = 0;
322
323 egl_connection_t* const cnx = &gEGLImpl;
324 if (cnx->dso) {
325 res = cnx->egl.eglGetConfigs(
326 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700327 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800328
329 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700330}
331
332EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
333 EGLConfig *configs, EGLint config_size,
334 EGLint *num_config)
335{
336 clearError();
337
Jesse Hallb29e5e82012-04-04 16:53:42 -0700338 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700339 if (!dp) return EGL_FALSE;
340
341 if (num_config==0) {
342 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
343 }
344
Mathias Agopian518ec112011-05-13 16:21:08 -0700345 EGLBoolean res = EGL_FALSE;
346 *num_config = 0;
347
Mathias Agopianada798b2012-02-13 17:09:30 -0800348 egl_connection_t* const cnx = &gEGLImpl;
349 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700350 if (attrib_list) {
351 char value[PROPERTY_VALUE_MAX];
352 property_get("debug.egl.force_msaa", value, "false");
353
354 if (!strcmp(value, "true")) {
355 size_t attribCount = 0;
356 EGLint attrib = attrib_list[0];
357
358 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700359 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700360 const EGLint *attribRendererable = NULL;
361 const EGLint *attribCaveat = NULL;
362
363 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700364 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700365 while (attrib != EGL_NONE) {
366 attrib = attrib_list[attribCount];
367 switch (attrib) {
368 case EGL_RENDERABLE_TYPE:
369 attribRendererable = &attrib_list[attribCount];
370 break;
371 case EGL_CONFIG_CAVEAT:
372 attribCaveat = &attrib_list[attribCount];
373 break;
374 }
375 attribCount++;
376 }
377
378 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
379 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800380
Romain Guy1cffc802012-10-15 18:13:05 -0700381 // Insert 2 extra attributes to force-enable MSAA 4x
382 EGLint aaAttribs[attribCount + 4];
383 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
384 aaAttribs[1] = 1;
385 aaAttribs[2] = EGL_SAMPLES;
386 aaAttribs[3] = 4;
387
388 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
389
390 EGLint numConfigAA;
391 EGLBoolean resAA = cnx->egl.eglChooseConfig(
392 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
393
394 if (resAA == EGL_TRUE && numConfigAA > 0) {
395 ALOGD("Enabling MSAA 4x");
396 *num_config = numConfigAA;
397 return resAA;
398 }
399 }
400 }
401 }
402
Mathias Agopian7773c432012-02-13 20:06:08 -0800403 res = cnx->egl.eglChooseConfig(
404 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700405 }
406 return res;
407}
408
409EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
410 EGLint attribute, EGLint *value)
411{
412 clearError();
413
Jesse Hallb29e5e82012-04-04 16:53:42 -0700414 egl_connection_t* cnx = NULL;
415 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
416 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800417
Mathias Agopian518ec112011-05-13 16:21:08 -0700418 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800419 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700420}
421
422// ----------------------------------------------------------------------------
423// surfaces
424// ----------------------------------------------------------------------------
425
Jesse Halla2ba4282013-09-14 21:00:14 -0700426// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
Jesse Hallc2e41222013-08-08 13:40:22 -0700427// been added to the Khronos egl.h.
428#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE
429#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB
430#define EGL_GL_COLORSPACE_LINEAR_KHR EGL_VG_COLORSPACE_LINEAR
431
432// Turn linear formats into corresponding sRGB formats when colorspace is
433// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
434// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800435// the modification isn't possible, the original dataSpace is returned.
436static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
437 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700438 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800439 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700440 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800441 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700442 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800443 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700444}
445
Mathias Agopian518ec112011-05-13 16:21:08 -0700446EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
447 NativeWindowType window,
448 const EGLint *attrib_list)
449{
450 clearError();
451
Jesse Hallb29e5e82012-04-04 16:53:42 -0700452 egl_connection_t* cnx = NULL;
453 egl_display_ptr dp = validate_display_connection(dpy, cnx);
454 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800455 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700456
Andy McFaddend566ce32014-01-07 15:54:17 -0800457 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
458 if (result != OK) {
459 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
460 "failed (%#x) (already connected to another API?)",
461 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700462 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700463 }
464
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700465 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700466 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
467 // of our native format. So if sRGB gamma is requested, we have to
468 // modify the EGLconfig's format before setting the native window's
469 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800470
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700471 // by default, just pick RGBA_8888
472 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800473 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700474
475 EGLint a = 0;
476 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
477 if (a > 0) {
478 // alpha-channel requested, there's really only one suitable format
479 format = HAL_PIXEL_FORMAT_RGBA_8888;
480 } else {
481 EGLint r, g, b;
482 r = g = b = 0;
483 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
484 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
485 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
486 EGLint colorDepth = r + g + b;
487 if (colorDepth <= 16) {
488 format = HAL_PIXEL_FORMAT_RGB_565;
489 } else {
490 format = HAL_PIXEL_FORMAT_RGBX_8888;
491 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700492 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700493
494 // now select a corresponding sRGB format if needed
495 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
496 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
497 if (*attr == EGL_GL_COLORSPACE_KHR) {
Jesse Halla2ba4282013-09-14 21:00:14 -0700498 if (ENABLE_EGL_KHR_GL_COLORSPACE) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800499 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jesse Halla2ba4282013-09-14 21:00:14 -0700500 } else {
501 // Normally we'd pass through unhandled attributes to
502 // the driver. But in case the driver implements this
503 // extension but we're disabling it, we want to prevent
504 // it getting through -- support will be broken without
505 // our help.
506 ALOGE("sRGB window surfaces not supported");
507 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
508 }
Jamie Gennisbee205f2011-07-01 13:12:07 -0700509 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700510 }
511 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800512
Jesse Hallc2e41222013-08-08 13:40:22 -0700513 if (format != 0) {
514 int err = native_window_set_buffers_format(window, format);
515 if (err != 0) {
516 ALOGE("error setting native window pixel format: %s (%d)",
517 strerror(-err), err);
518 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
519 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
520 }
521 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700522
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800523 if (dataSpace != 0) {
524 int err = native_window_set_buffers_data_space(window, dataSpace);
525 if (err != 0) {
526 ALOGE("error setting native window pixel dataSpace: %s (%d)",
527 strerror(-err), err);
528 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
529 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
530 }
531 }
532
Jamie Gennis59769462011-11-19 18:04:43 -0800533 // the EGL spec requires that a new EGLSurface default to swap interval
534 // 1, so explicitly set that on the window here.
535 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
536 anw->setSwapInterval(anw, 1);
537
Mathias Agopian518ec112011-05-13 16:21:08 -0700538 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800539 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700540 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700541 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
542 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700543 return s;
544 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700545
546 // EGLSurface creation failed
547 native_window_set_buffers_format(window, 0);
548 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700549 }
550 return EGL_NO_SURFACE;
551}
552
553EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
554 NativePixmapType pixmap,
555 const EGLint *attrib_list)
556{
557 clearError();
558
Jesse Hallb29e5e82012-04-04 16:53:42 -0700559 egl_connection_t* cnx = NULL;
560 egl_display_ptr dp = validate_display_connection(dpy, cnx);
561 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700562 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800563 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700564 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700565 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
566 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700567 return s;
568 }
569 }
570 return EGL_NO_SURFACE;
571}
572
573EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
574 const EGLint *attrib_list)
575{
576 clearError();
577
Jesse Hallb29e5e82012-04-04 16:53:42 -0700578 egl_connection_t* cnx = NULL;
579 egl_display_ptr dp = validate_display_connection(dpy, cnx);
580 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700581 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800582 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700583 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700584 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
585 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700586 return s;
587 }
588 }
589 return EGL_NO_SURFACE;
590}
Jesse Hall47743382013-02-08 11:13:46 -0800591
Mathias Agopian518ec112011-05-13 16:21:08 -0700592EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
593{
594 clearError();
595
Jesse Hallb29e5e82012-04-04 16:53:42 -0700596 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700597 if (!dp) return EGL_FALSE;
598
Jesse Hallb29e5e82012-04-04 16:53:42 -0700599 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700600 if (!_s.get())
601 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700602
603 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800604 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700605 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700606 _s.terminate();
Pablo Ceballos541de492016-04-09 15:56:12 -0700607 dp->removeSurface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700608 }
609 return result;
610}
611
612EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
613 EGLint attribute, EGLint *value)
614{
615 clearError();
616
Jesse Hallb29e5e82012-04-04 16:53:42 -0700617 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700618 if (!dp) return EGL_FALSE;
619
Jesse Hallb29e5e82012-04-04 16:53:42 -0700620 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700621 if (!_s.get())
622 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700623
Mathias Agopian518ec112011-05-13 16:21:08 -0700624 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800625 return s->cnx->egl.eglQuerySurface(
626 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700627}
628
Jamie Gennise8696a42012-01-15 18:54:57 -0800629void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800630 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800631 clearError();
632
Jesse Hallb29e5e82012-04-04 16:53:42 -0700633 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800634 if (!dp) {
635 return;
636 }
637
Jesse Hallb29e5e82012-04-04 16:53:42 -0700638 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800639 if (!_s.get()) {
640 setError(EGL_BAD_SURFACE, EGL_FALSE);
641 return;
642 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800643}
644
Mathias Agopian518ec112011-05-13 16:21:08 -0700645// ----------------------------------------------------------------------------
646// Contexts
647// ----------------------------------------------------------------------------
648
649EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
650 EGLContext share_list, const EGLint *attrib_list)
651{
652 clearError();
653
Jesse Hallb29e5e82012-04-04 16:53:42 -0700654 egl_connection_t* cnx = NULL;
655 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700656 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700657 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700658 if (!ContextRef(dp.get(), share_list).get()) {
659 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
660 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700661 egl_context_t* const c = get_context(share_list);
662 share_list = c->context;
663 }
664 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800665 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700666 if (context != EGL_NO_CONTEXT) {
667 // figure out if it's a GLESv1 or GLESv2
668 int version = 0;
669 if (attrib_list) {
670 while (*attrib_list != EGL_NONE) {
671 GLint attr = *attrib_list++;
672 GLint value = *attrib_list++;
673 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
674 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800675 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800676 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800677 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700678 }
679 }
680 };
681 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700682 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
683 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700684 return c;
685 }
686 }
687 return EGL_NO_CONTEXT;
688}
689
690EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
691{
692 clearError();
693
Jesse Hallb29e5e82012-04-04 16:53:42 -0700694 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700695 if (!dp)
696 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700697
Jesse Hallb29e5e82012-04-04 16:53:42 -0700698 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700699 if (!_c.get())
700 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800701
Mathias Agopian518ec112011-05-13 16:21:08 -0700702 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800703 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700704 if (result == EGL_TRUE) {
705 _c.terminate();
706 }
707 return result;
708}
709
Mathias Agopian518ec112011-05-13 16:21:08 -0700710EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
711 EGLSurface read, EGLContext ctx)
712{
713 clearError();
714
Jesse Hallb29e5e82012-04-04 16:53:42 -0700715 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700716 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
717
Mathias Agopian5b287a62011-05-16 18:58:55 -0700718 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
719 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
720 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700721 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
722 (draw != EGL_NO_SURFACE) ) {
723 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
724 }
725
726 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700727 ContextRef _c(dp.get(), ctx);
728 SurfaceRef _d(dp.get(), draw);
729 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700730
731 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700732 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700733 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700734 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700735 }
736
737 // these are the underlying implementation's object
738 EGLContext impl_ctx = EGL_NO_CONTEXT;
739 EGLSurface impl_draw = EGL_NO_SURFACE;
740 EGLSurface impl_read = EGL_NO_SURFACE;
741
742 // these are our objects structs passed in
743 egl_context_t * c = NULL;
744 egl_surface_t const * d = NULL;
745 egl_surface_t const * r = NULL;
746
747 // these are the current objects structs
748 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800749
Mathias Agopian518ec112011-05-13 16:21:08 -0700750 if (ctx != EGL_NO_CONTEXT) {
751 c = get_context(ctx);
752 impl_ctx = c->context;
753 } else {
754 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700755 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
756 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
757 return setError(EGL_BAD_MATCH, EGL_FALSE);
758 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700759 if (cur_c == NULL) {
760 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700761 // not an error, there is just no current context.
762 return EGL_TRUE;
763 }
764 }
765
766 // retrieve the underlying implementation's draw EGLSurface
767 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700768 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700769 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700770 impl_draw = d->surface;
771 }
772
773 // retrieve the underlying implementation's read EGLSurface
774 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700775 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700776 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700777 impl_read = r->surface;
778 }
779
Mathias Agopian518ec112011-05-13 16:21:08 -0700780
Jesse Hallb29e5e82012-04-04 16:53:42 -0700781 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800782 draw, read, ctx,
783 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700784
785 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800786 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700787 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
788 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700789 _c.acquire();
790 _r.acquire();
791 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700792 } else {
793 setGLHooksThreadSpecific(&gHooksNoContext);
794 egl_tls_t::setContext(EGL_NO_CONTEXT);
795 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700796 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000797 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700798 egl_connection_t* const cnx = &gEGLImpl;
799 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700800 }
801 return result;
802}
803
804
805EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
806 EGLint attribute, EGLint *value)
807{
808 clearError();
809
Jesse Hallb29e5e82012-04-04 16:53:42 -0700810 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 if (!dp) return EGL_FALSE;
812
Jesse Hallb29e5e82012-04-04 16:53:42 -0700813 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700814 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
815
Mathias Agopian518ec112011-05-13 16:21:08 -0700816 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800817 return c->cnx->egl.eglQueryContext(
818 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700819
Mathias Agopian518ec112011-05-13 16:21:08 -0700820}
821
822EGLContext eglGetCurrentContext(void)
823{
824 // could be called before eglInitialize(), but we wouldn't have a context
825 // then, and this function would correctly return EGL_NO_CONTEXT.
826
827 clearError();
828
829 EGLContext ctx = getContext();
830 return ctx;
831}
832
833EGLSurface eglGetCurrentSurface(EGLint readdraw)
834{
835 // could be called before eglInitialize(), but we wouldn't have a context
836 // then, and this function would correctly return EGL_NO_SURFACE.
837
838 clearError();
839
840 EGLContext ctx = getContext();
841 if (ctx) {
842 egl_context_t const * const c = get_context(ctx);
843 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
844 switch (readdraw) {
845 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800846 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700847 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
848 }
849 }
850 return EGL_NO_SURFACE;
851}
852
853EGLDisplay eglGetCurrentDisplay(void)
854{
855 // could be called before eglInitialize(), but we wouldn't have a context
856 // then, and this function would correctly return EGL_NO_DISPLAY.
857
858 clearError();
859
860 EGLContext ctx = getContext();
861 if (ctx) {
862 egl_context_t const * const c = get_context(ctx);
863 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
864 return c->dpy;
865 }
866 return EGL_NO_DISPLAY;
867}
868
869EGLBoolean eglWaitGL(void)
870{
Mathias Agopian518ec112011-05-13 16:21:08 -0700871 clearError();
872
Mathias Agopianada798b2012-02-13 17:09:30 -0800873 egl_connection_t* const cnx = &gEGLImpl;
874 if (!cnx->dso)
875 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
876
877 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700878}
879
880EGLBoolean eglWaitNative(EGLint engine)
881{
Mathias Agopian518ec112011-05-13 16:21:08 -0700882 clearError();
883
Mathias Agopianada798b2012-02-13 17:09:30 -0800884 egl_connection_t* const cnx = &gEGLImpl;
885 if (!cnx->dso)
886 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
887
888 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700889}
890
891EGLint eglGetError(void)
892{
Mathias Agopianada798b2012-02-13 17:09:30 -0800893 EGLint err = EGL_SUCCESS;
894 egl_connection_t* const cnx = &gEGLImpl;
895 if (cnx->dso) {
896 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700897 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800898 if (err == EGL_SUCCESS) {
899 err = egl_tls_t::getError();
900 }
901 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700902}
903
Michael Chockc0ec5e22014-01-27 08:14:33 -0800904static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700905 const char* procname) {
906 const egl_connection_t* cnx = &gEGLImpl;
907 void* proc = NULL;
908
Michael Chockc0ec5e22014-01-27 08:14:33 -0800909 proc = dlsym(cnx->libEgl, procname);
910 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
911
Jesse Hallc07b5202013-07-04 12:08:16 -0700912 proc = dlsym(cnx->libGles2, procname);
913 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
914
915 proc = dlsym(cnx->libGles1, procname);
916 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
917
918 return NULL;
919}
920
Mathias Agopian518ec112011-05-13 16:21:08 -0700921__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
922{
923 // eglGetProcAddress() could be the very first function called
924 // in which case we must make sure we've initialized ourselves, this
925 // happens the first time egl_get_display() is called.
926
927 clearError();
928
929 if (egl_init_drivers() == EGL_FALSE) {
930 setError(EGL_BAD_PARAMETER, NULL);
931 return NULL;
932 }
933
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700934 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700935 return NULL;
936 }
937
Mathias Agopian518ec112011-05-13 16:21:08 -0700938 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700939 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700940 if (addr) return addr;
941
Michael Chockc0ec5e22014-01-27 08:14:33 -0800942 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700943 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700944
Mathias Agopian518ec112011-05-13 16:21:08 -0700945 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
946 pthread_mutex_lock(&sExtensionMapMutex);
947
948 /*
949 * Since eglGetProcAddress() is not associated to anything, it needs
950 * to return a function pointer that "works" regardless of what
951 * the current context is.
952 *
953 * For this reason, we return a "forwarder", a small stub that takes
954 * care of calling the function associated with the context
955 * currently bound.
956 *
957 * We first look for extensions we've already resolved, if we're seeing
958 * this extension for the first time, we go through all our
959 * implementations and call eglGetProcAddress() and record the
960 * result in the appropriate implementation hooks and return the
961 * address of the forwarder corresponding to that hook set.
962 *
963 */
964
965 const String8 name(procname);
966 addr = sGLExtentionMap.valueFor(name);
967 const int slot = sGLExtentionSlot;
968
Steve Blocke6f43dd2012-01-06 19:20:56 +0000969 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700970 "no more slots for eglGetProcAddress(\"%s\")",
971 procname);
972
973 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
974 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800975
976 egl_connection_t* const cnx = &gEGLImpl;
977 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800978 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800979 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800980 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
981 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800982 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800983 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700984 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800985
Mathias Agopian518ec112011-05-13 16:21:08 -0700986 if (found) {
987 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700988 sGLExtentionMap.add(name, addr);
989 sGLExtentionSlot++;
990 }
991 }
992
993 pthread_mutex_unlock(&sExtensionMapMutex);
994 return addr;
995}
996
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700997class FrameCompletionThread : public Thread {
998public:
999
1000 static void queueSync(EGLSyncKHR sync) {
1001 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1002 static bool running = false;
1003 if (!running) {
1004 thread->run("GPUFrameCompletion");
1005 running = true;
1006 }
1007 {
1008 Mutex::Autolock lock(thread->mMutex);
1009 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1010 thread->mFramesQueued).string());
1011 thread->mQueue.push_back(sync);
1012 thread->mCondition.signal();
1013 thread->mFramesQueued++;
1014 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1015 }
1016 }
1017
1018private:
1019 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1020
1021 virtual bool threadLoop() {
1022 EGLSyncKHR sync;
1023 uint32_t frameNum;
1024 {
1025 Mutex::Autolock lock(mMutex);
1026 while (mQueue.isEmpty()) {
1027 mCondition.wait(mMutex);
1028 }
1029 sync = mQueue[0];
1030 frameNum = mFramesCompleted;
1031 }
1032 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1033 {
1034 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1035 frameNum).string());
1036 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1037 if (result == EGL_FALSE) {
1038 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1039 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1040 ALOGE("FrameCompletion: timeout waiting for fence");
1041 }
1042 eglDestroySyncKHR(dpy, sync);
1043 }
1044 {
1045 Mutex::Autolock lock(mMutex);
1046 mQueue.removeAt(0);
1047 mFramesCompleted++;
1048 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1049 }
1050 return true;
1051 }
1052
1053 uint32_t mFramesQueued;
1054 uint32_t mFramesCompleted;
1055 Vector<EGLSyncKHR> mQueue;
1056 Condition mCondition;
1057 Mutex mMutex;
1058};
1059
Dan Stozaa894d082015-02-19 15:27:36 -08001060EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1061 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001062{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001063 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001064 clearError();
1065
Jesse Hallb29e5e82012-04-04 16:53:42 -07001066 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001067 if (!dp) return EGL_FALSE;
1068
Jesse Hallb29e5e82012-04-04 16:53:42 -07001069 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001070 if (!_s.get())
1071 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001072
Mathias Agopian518ec112011-05-13 16:21:08 -07001073 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001074
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001075 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1076 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1077 if (sync != EGL_NO_SYNC_KHR) {
1078 FrameCompletionThread::queueSync(sync);
1079 }
1080 }
1081
Mathias Agopian7db993a2012-03-25 00:49:46 -07001082 if (CC_UNLIKELY(dp->finishOnSwap)) {
1083 uint32_t pixel;
1084 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1085 if (c) {
1086 // glReadPixels() ensures that the frame is complete
1087 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1088 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1089 }
1090 }
1091
Dan Stozaa894d082015-02-19 15:27:36 -08001092 if (n_rects == 0) {
1093 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1094 }
1095
1096 Vector<android_native_rect_t> androidRects;
1097 for (int r = 0; r < n_rects; ++r) {
1098 int offset = r * 4;
1099 int x = rects[offset];
1100 int y = rects[offset + 1];
1101 int width = rects[offset + 2];
1102 int height = rects[offset + 3];
1103 android_native_rect_t androidRect;
1104 androidRect.left = x;
1105 androidRect.top = y + height;
1106 androidRect.right = x + width;
1107 androidRect.bottom = y;
1108 androidRects.push_back(androidRect);
1109 }
1110 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1111 androidRects.size());
1112
1113 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1114 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1115 rects, n_rects);
1116 } else {
1117 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1118 }
1119}
1120
1121EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1122{
1123 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001124}
1125
1126EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1127 NativePixmapType target)
1128{
1129 clearError();
1130
Jesse Hallb29e5e82012-04-04 16:53:42 -07001131 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001132 if (!dp) return EGL_FALSE;
1133
Jesse Hallb29e5e82012-04-04 16:53:42 -07001134 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001135 if (!_s.get())
1136 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001137
Mathias Agopian518ec112011-05-13 16:21:08 -07001138 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001139 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001140}
1141
1142const char* eglQueryString(EGLDisplay dpy, EGLint name)
1143{
1144 clearError();
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
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 egl_surface_t const * 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
Mathias Agopian518ec112011-05-13 16:21:08 -07001207 if (s->cnx->egl.eglSurfaceAttrib) {
1208 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001209 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001210 }
1211 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1212}
1213
1214EGLBoolean eglBindTexImage(
1215 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1216{
1217 clearError();
1218
Jesse Hallb29e5e82012-04-04 16:53:42 -07001219 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001220 if (!dp) return EGL_FALSE;
1221
Jesse Hallb29e5e82012-04-04 16:53:42 -07001222 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001223 if (!_s.get())
1224 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001225
Mathias Agopian518ec112011-05-13 16:21:08 -07001226 egl_surface_t const * const s = get_surface(surface);
1227 if (s->cnx->egl.eglBindTexImage) {
1228 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001229 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001230 }
1231 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1232}
1233
1234EGLBoolean eglReleaseTexImage(
1235 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1236{
1237 clearError();
1238
Jesse Hallb29e5e82012-04-04 16:53:42 -07001239 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001240 if (!dp) return EGL_FALSE;
1241
Jesse Hallb29e5e82012-04-04 16:53:42 -07001242 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001243 if (!_s.get())
1244 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001245
Mathias Agopian518ec112011-05-13 16:21:08 -07001246 egl_surface_t const * const s = get_surface(surface);
1247 if (s->cnx->egl.eglReleaseTexImage) {
1248 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001249 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001250 }
1251 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1252}
1253
1254EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1255{
1256 clearError();
1257
Jesse Hallb29e5e82012-04-04 16:53:42 -07001258 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001259 if (!dp) return EGL_FALSE;
1260
1261 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001262 egl_connection_t* const cnx = &gEGLImpl;
1263 if (cnx->dso && cnx->egl.eglSwapInterval) {
1264 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001265 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001266
Mathias Agopian518ec112011-05-13 16:21:08 -07001267 return res;
1268}
1269
1270
1271// ----------------------------------------------------------------------------
1272// EGL 1.2
1273// ----------------------------------------------------------------------------
1274
1275EGLBoolean eglWaitClient(void)
1276{
1277 clearError();
1278
Mathias Agopianada798b2012-02-13 17:09:30 -08001279 egl_connection_t* const cnx = &gEGLImpl;
1280 if (!cnx->dso)
1281 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1282
1283 EGLBoolean res;
1284 if (cnx->egl.eglWaitClient) {
1285 res = cnx->egl.eglWaitClient();
1286 } else {
1287 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001288 }
1289 return res;
1290}
1291
1292EGLBoolean eglBindAPI(EGLenum api)
1293{
1294 clearError();
1295
1296 if (egl_init_drivers() == EGL_FALSE) {
1297 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1298 }
1299
1300 // bind this API on all EGLs
1301 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001302 egl_connection_t* const cnx = &gEGLImpl;
1303 if (cnx->dso && cnx->egl.eglBindAPI) {
1304 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001305 }
1306 return res;
1307}
1308
1309EGLenum eglQueryAPI(void)
1310{
1311 clearError();
1312
1313 if (egl_init_drivers() == EGL_FALSE) {
1314 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1315 }
1316
Mathias Agopianada798b2012-02-13 17:09:30 -08001317 egl_connection_t* const cnx = &gEGLImpl;
1318 if (cnx->dso && cnx->egl.eglQueryAPI) {
1319 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001320 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001321
Mathias Agopian518ec112011-05-13 16:21:08 -07001322 // or, it can only be OpenGL ES
1323 return EGL_OPENGL_ES_API;
1324}
1325
1326EGLBoolean eglReleaseThread(void)
1327{
1328 clearError();
1329
1330 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001331 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001332
Mathias Agopianada798b2012-02-13 17:09:30 -08001333 egl_connection_t* const cnx = &gEGLImpl;
1334 if (cnx->dso && cnx->egl.eglReleaseThread) {
1335 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001336 }
1337 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001338 return EGL_TRUE;
1339}
1340
1341EGLSurface eglCreatePbufferFromClientBuffer(
1342 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1343 EGLConfig config, const EGLint *attrib_list)
1344{
1345 clearError();
1346
Jesse Hallb29e5e82012-04-04 16:53:42 -07001347 egl_connection_t* cnx = NULL;
1348 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1349 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001350 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1351 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001352 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001353 }
1354 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1355}
1356
1357// ----------------------------------------------------------------------------
1358// EGL_EGLEXT_VERSION 3
1359// ----------------------------------------------------------------------------
1360
1361EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1362 const EGLint *attrib_list)
1363{
1364 clearError();
1365
Jesse Hallb29e5e82012-04-04 16:53:42 -07001366 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001367 if (!dp) return EGL_FALSE;
1368
Jesse Hallb29e5e82012-04-04 16:53:42 -07001369 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001370 if (!_s.get())
1371 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001372
1373 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001374 if (s->cnx->egl.eglLockSurfaceKHR) {
1375 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001376 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001377 }
1378 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1379}
1380
1381EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1382{
1383 clearError();
1384
Jesse Hallb29e5e82012-04-04 16:53:42 -07001385 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001386 if (!dp) return EGL_FALSE;
1387
Jesse Hallb29e5e82012-04-04 16:53:42 -07001388 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001389 if (!_s.get())
1390 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001391
1392 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001393 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001394 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001395 }
1396 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1397}
1398
1399EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1400 EGLClientBuffer buffer, const EGLint *attrib_list)
1401{
1402 clearError();
1403
Jesse Hallb29e5e82012-04-04 16:53:42 -07001404 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001405 if (!dp) return EGL_NO_IMAGE_KHR;
1406
Jesse Hallb29e5e82012-04-04 16:53:42 -07001407 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001408 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001409
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001410 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1411 egl_connection_t* const cnx = &gEGLImpl;
1412 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1413 result = cnx->egl.eglCreateImageKHR(
1414 dp->disp.dpy,
1415 c ? c->context : EGL_NO_CONTEXT,
1416 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001417 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001418 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001419}
1420
1421EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1422{
1423 clearError();
1424
Jesse Hallb29e5e82012-04-04 16:53:42 -07001425 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001426 if (!dp) return EGL_FALSE;
1427
Steven Holte646a5c52012-06-04 20:02:11 -07001428 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001429 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001430 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001431 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001432 }
Steven Holte646a5c52012-06-04 20:02:11 -07001433 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001434}
1435
1436// ----------------------------------------------------------------------------
1437// EGL_EGLEXT_VERSION 5
1438// ----------------------------------------------------------------------------
1439
1440
1441EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1442{
1443 clearError();
1444
Jesse Hallb29e5e82012-04-04 16:53:42 -07001445 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001446 if (!dp) return EGL_NO_SYNC_KHR;
1447
Mathias Agopian518ec112011-05-13 16:21:08 -07001448 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001449 egl_connection_t* const cnx = &gEGLImpl;
1450 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1451 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001452 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001453 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001454}
1455
1456EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1457{
1458 clearError();
1459
Jesse Hallb29e5e82012-04-04 16:53:42 -07001460 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001461 if (!dp) return EGL_FALSE;
1462
Mathias Agopian518ec112011-05-13 16:21:08 -07001463 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001464 egl_connection_t* const cnx = &gEGLImpl;
1465 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1466 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001467 }
1468 return result;
1469}
1470
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001471EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1472 clearError();
1473
1474 const egl_display_ptr dp = validate_display(dpy);
1475 if (!dp) return EGL_FALSE;
1476
1477 EGLBoolean result = EGL_FALSE;
1478 egl_connection_t* const cnx = &gEGLImpl;
1479 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1480 result = cnx->egl.eglSignalSyncKHR(
1481 dp->disp.dpy, sync, mode);
1482 }
1483 return result;
1484}
1485
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001486EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1487 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001488{
1489 clearError();
1490
Jesse Hallb29e5e82012-04-04 16:53:42 -07001491 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001492 if (!dp) return EGL_FALSE;
1493
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001494 EGLBoolean result = EGL_FALSE;
1495 egl_connection_t* const cnx = &gEGLImpl;
1496 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1497 result = cnx->egl.eglClientWaitSyncKHR(
1498 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001499 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001500 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001501}
1502
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001503EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1504 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001505{
1506 clearError();
1507
Jesse Hallb29e5e82012-04-04 16:53:42 -07001508 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001509 if (!dp) return EGL_FALSE;
1510
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001511 EGLBoolean result = EGL_FALSE;
1512 egl_connection_t* const cnx = &gEGLImpl;
1513 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1514 result = cnx->egl.eglGetSyncAttribKHR(
1515 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001516 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001517 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001518}
1519
Season Li000d88f2015-07-01 11:39:40 -07001520EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1521{
1522 clearError();
1523
1524 const egl_display_ptr dp = validate_display(dpy);
1525 if (!dp) return EGL_NO_STREAM_KHR;
1526
1527 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1528 egl_connection_t* const cnx = &gEGLImpl;
1529 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1530 result = cnx->egl.eglCreateStreamKHR(
1531 dp->disp.dpy, attrib_list);
1532 }
1533 return result;
1534}
1535
1536EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1537{
1538 clearError();
1539
1540 const egl_display_ptr dp = validate_display(dpy);
1541 if (!dp) return EGL_FALSE;
1542
1543 EGLBoolean result = EGL_FALSE;
1544 egl_connection_t* const cnx = &gEGLImpl;
1545 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1546 result = cnx->egl.eglDestroyStreamKHR(
1547 dp->disp.dpy, stream);
1548 }
1549 return result;
1550}
1551
1552EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1553 EGLenum attribute, EGLint value)
1554{
1555 clearError();
1556
1557 const egl_display_ptr dp = validate_display(dpy);
1558 if (!dp) return EGL_FALSE;
1559
1560 EGLBoolean result = EGL_FALSE;
1561 egl_connection_t* const cnx = &gEGLImpl;
1562 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1563 result = cnx->egl.eglStreamAttribKHR(
1564 dp->disp.dpy, stream, attribute, value);
1565 }
1566 return result;
1567}
1568
1569EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1570 EGLenum attribute, EGLint *value)
1571{
1572 clearError();
1573
1574 const egl_display_ptr dp = validate_display(dpy);
1575 if (!dp) return EGL_FALSE;
1576
1577 EGLBoolean result = EGL_FALSE;
1578 egl_connection_t* const cnx = &gEGLImpl;
1579 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1580 result = cnx->egl.eglQueryStreamKHR(
1581 dp->disp.dpy, stream, attribute, value);
1582 }
1583 return result;
1584}
1585
1586EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1587 EGLenum attribute, EGLuint64KHR *value)
1588{
1589 clearError();
1590
1591 const egl_display_ptr dp = validate_display(dpy);
1592 if (!dp) return EGL_FALSE;
1593
1594 EGLBoolean result = EGL_FALSE;
1595 egl_connection_t* const cnx = &gEGLImpl;
1596 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1597 result = cnx->egl.eglQueryStreamu64KHR(
1598 dp->disp.dpy, stream, attribute, value);
1599 }
1600 return result;
1601}
1602
1603EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1604 EGLenum attribute, EGLTimeKHR *value)
1605{
1606 clearError();
1607
1608 const egl_display_ptr dp = validate_display(dpy);
1609 if (!dp) return EGL_FALSE;
1610
1611 EGLBoolean result = EGL_FALSE;
1612 egl_connection_t* const cnx = &gEGLImpl;
1613 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1614 result = cnx->egl.eglQueryStreamTimeKHR(
1615 dp->disp.dpy, stream, attribute, value);
1616 }
1617 return result;
1618}
1619
1620EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1621 EGLStreamKHR stream, const EGLint *attrib_list)
1622{
1623 clearError();
1624
1625 egl_display_ptr dp = validate_display(dpy);
1626 if (!dp) return EGL_NO_SURFACE;
1627
1628 egl_connection_t* const cnx = &gEGLImpl;
1629 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1630 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1631 dp->disp.dpy, config, stream, attrib_list);
1632 if (surface != EGL_NO_SURFACE) {
1633 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1634 surface, cnx);
1635 return s;
1636 }
1637 }
1638 return EGL_NO_SURFACE;
1639}
1640
1641EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1642 EGLStreamKHR stream)
1643{
1644 clearError();
1645
1646 const egl_display_ptr dp = validate_display(dpy);
1647 if (!dp) return EGL_FALSE;
1648
1649 EGLBoolean result = EGL_FALSE;
1650 egl_connection_t* const cnx = &gEGLImpl;
1651 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1652 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1653 dp->disp.dpy, stream);
1654 }
1655 return result;
1656}
1657
1658EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1659 EGLStreamKHR stream)
1660{
1661 clearError();
1662
1663 const egl_display_ptr dp = validate_display(dpy);
1664 if (!dp) return EGL_FALSE;
1665
1666 EGLBoolean result = EGL_FALSE;
1667 egl_connection_t* const cnx = &gEGLImpl;
1668 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1669 result = cnx->egl.eglStreamConsumerAcquireKHR(
1670 dp->disp.dpy, stream);
1671 }
1672 return result;
1673}
1674
1675EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1676 EGLStreamKHR stream)
1677{
1678 clearError();
1679
1680 const egl_display_ptr dp = validate_display(dpy);
1681 if (!dp) return EGL_FALSE;
1682
1683 EGLBoolean result = EGL_FALSE;
1684 egl_connection_t* const cnx = &gEGLImpl;
1685 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1686 result = cnx->egl.eglStreamConsumerReleaseKHR(
1687 dp->disp.dpy, stream);
1688 }
1689 return result;
1690}
1691
1692EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1693 EGLDisplay dpy, EGLStreamKHR stream)
1694{
1695 clearError();
1696
1697 const egl_display_ptr dp = validate_display(dpy);
1698 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1699
1700 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1701 egl_connection_t* const cnx = &gEGLImpl;
1702 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1703 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1704 dp->disp.dpy, stream);
1705 }
1706 return result;
1707}
1708
1709EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1710 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1711{
1712 clearError();
1713
1714 const egl_display_ptr dp = validate_display(dpy);
1715 if (!dp) return EGL_NO_STREAM_KHR;
1716
1717 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1718 egl_connection_t* const cnx = &gEGLImpl;
1719 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1720 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1721 dp->disp.dpy, file_descriptor);
1722 }
1723 return result;
1724}
1725
Mathias Agopian518ec112011-05-13 16:21:08 -07001726// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001727// EGL_EGLEXT_VERSION 15
1728// ----------------------------------------------------------------------------
1729
1730EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1731 clearError();
1732 const egl_display_ptr dp = validate_display(dpy);
1733 if (!dp) return EGL_FALSE;
1734 EGLint result = EGL_FALSE;
1735 egl_connection_t* const cnx = &gEGLImpl;
1736 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1737 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1738 }
1739 return result;
1740}
1741
1742// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001743// ANDROID extensions
1744// ----------------------------------------------------------------------------
1745
Jamie Gennis331841b2012-09-06 14:52:00 -07001746EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1747{
1748 clearError();
1749
1750 const egl_display_ptr dp = validate_display(dpy);
1751 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1752
1753 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1754 egl_connection_t* const cnx = &gEGLImpl;
1755 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1756 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1757 }
1758 return result;
1759}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001760
Andy McFadden72841452013-03-01 16:25:32 -08001761EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1762 EGLnsecsANDROID time)
1763{
1764 clearError();
1765
1766 const egl_display_ptr dp = validate_display(dpy);
1767 if (!dp) {
1768 return EGL_FALSE;
1769 }
1770
1771 SurfaceRef _s(dp.get(), surface);
1772 if (!_s.get()) {
1773 setError(EGL_BAD_SURFACE, EGL_FALSE);
1774 return EGL_FALSE;
1775 }
1776
1777 egl_surface_t const * const s = get_surface(surface);
1778 native_window_set_buffers_timestamp(s->win.get(), time);
1779
1780 return EGL_TRUE;
1781}
1782
Craig Donner05249fc2016-01-15 19:33:55 -08001783EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1784{
1785 clearError();
1786
1787 int usage = 0;
1788 uint32_t width = 0;
1789 uint32_t height = 0;
1790 uint32_t format = 0;
1791 uint32_t red_size = 0;
1792 uint32_t green_size = 0;
1793 uint32_t blue_size = 0;
1794 uint32_t alpha_size = 0;
1795
1796#define GET_POSITIVE_VALUE(case_name, target) \
1797 case case_name: \
1798 if (value > 0) { \
1799 target = value; \
1800 } else { \
1801 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1802 } \
1803 break
1804
1805 if (attrib_list) {
1806 while (*attrib_list != EGL_NONE) {
1807 GLint attr = *attrib_list++;
1808 GLint value = *attrib_list++;
1809 switch (attr) {
1810 GET_POSITIVE_VALUE(EGL_WIDTH, width);
1811 GET_POSITIVE_VALUE(EGL_HEIGHT, height);
1812 GET_POSITIVE_VALUE(EGL_RED_SIZE, red_size);
1813 GET_POSITIVE_VALUE(EGL_GREEN_SIZE, green_size);
1814 GET_POSITIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1815 GET_POSITIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
1816 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1817 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1818 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001819 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001820 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001821 usage |= GRALLOC_USAGE_HW_RENDER;
1822 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001823 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001824 usage |= GRALLOC_USAGE_HW_TEXTURE;
1825 }
1826 // The buffer must be used for either a texture or a
1827 // renderbuffer.
Craig Donner8cfae6d2016-04-12 16:54:03 -07001828 if ((value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) &&
1829 (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID)) {
Craig Donner05249fc2016-01-15 19:33:55 -08001830 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1831 }
1832 break;
1833 default:
1834 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1835 }
1836 }
1837 }
1838#undef GET_POSITIVE_VALUE
1839
1840 // Validate format.
1841 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1842 if (alpha_size == 8) {
1843 format = HAL_PIXEL_FORMAT_RGBA_8888;
1844 } else {
1845 format = HAL_PIXEL_FORMAT_RGB_888;
1846 }
1847 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1848 alpha_size == 0) {
1849 format == HAL_PIXEL_FORMAT_RGB_565;
1850 } else {
1851 ALOGE("Invalid native pixel format { r=%d, g=%d, b=%d, a=%d }",
1852 red_size, green_size, blue_size, alpha_size);
1853 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1854 }
1855
1856 GraphicBuffer* gBuffer = new GraphicBuffer(width, height, format, usage);
1857 const status_t err = gBuffer->initCheck();
1858 if (err != NO_ERROR) {
1859 ALOGE("Unable to create native buffer { w=%d, h=%d, f=%d, u=%#x }: %#x",
1860 width, height, format, usage, err);
1861 // Destroy the buffer.
1862 sp<GraphicBuffer> holder(gBuffer);
1863 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
1864 }
1865 ALOGD("Created new native buffer %p { w=%d, h=%d, f=%d, u=%#x }",
1866 gBuffer, width, height, format, usage);
1867 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
1868}
1869
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001870// ----------------------------------------------------------------------------
1871// NVIDIA extensions
1872// ----------------------------------------------------------------------------
1873EGLuint64NV eglGetSystemTimeFrequencyNV()
1874{
1875 clearError();
1876
1877 if (egl_init_drivers() == EGL_FALSE) {
1878 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1879 }
1880
1881 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001882 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001883
Mathias Agopianada798b2012-02-13 17:09:30 -08001884 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1885 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001886 }
1887
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001888 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001889}
1890
1891EGLuint64NV eglGetSystemTimeNV()
1892{
1893 clearError();
1894
1895 if (egl_init_drivers() == EGL_FALSE) {
1896 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1897 }
1898
1899 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001900 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001901
Mathias Agopianada798b2012-02-13 17:09:30 -08001902 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1903 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001904 }
1905
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001906 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001907}
Dan Stozaa894d082015-02-19 15:27:36 -08001908
1909// ----------------------------------------------------------------------------
1910// Partial update extension
1911// ----------------------------------------------------------------------------
1912EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1913 EGLint *rects, EGLint n_rects)
1914{
1915 clearError();
1916
1917 const egl_display_ptr dp = validate_display(dpy);
1918 if (!dp) {
1919 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1920 return EGL_FALSE;
1921 }
1922
1923 SurfaceRef _s(dp.get(), surface);
1924 if (!_s.get()) {
1925 setError(EGL_BAD_SURFACE, EGL_FALSE);
1926 return EGL_FALSE;
1927 }
1928
1929 egl_surface_t const * const s = get_surface(surface);
1930 if (s->cnx->egl.eglSetDamageRegionKHR) {
1931 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1932 rects, n_rects);
1933 }
1934
1935 return EGL_FALSE;
1936}