blob: 05700f8c693bbb465e784cf7d8fc7f08645bbe28 [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
75 * listd as mandatory below and are required by the CDD. The system *assumes*
76 * 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 "
Jesse Hall21558da2013-08-06 15:31:22 -070086 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070087extern char const * const gExtensionString =
88 "EGL_KHR_image " // mandatory
89 "EGL_KHR_image_base " // mandatory
90 "EGL_KHR_image_pixmap "
91 "EGL_KHR_lock_surface "
Jesse Halla2ba4282013-09-14 21:00:14 -070092#if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
Jesse Hallc2e41222013-08-08 13:40:22 -070093 "EGL_KHR_gl_colorspace "
Jesse Halla2ba4282013-09-14 21:00:14 -070094#endif
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 "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700117 ;
118
119// extensions not exposed to applications but used by the ANDROID system
120// "EGL_ANDROID_blob_cache " // strongly recommended
121// "EGL_IMG_hibernate_process " // optional
122// "EGL_ANDROID_native_fence_sync " // strongly recommended
123// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700124// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700125
126/*
127 * EGL Extensions entry-points exposed to 3rd party applications
128 * (keep in sync with gExtensionString above)
129 *
130 */
131static const extention_map_t sExtensionMap[] = {
132 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700133 { "eglLockSurfaceKHR",
134 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
135 { "eglUnlockSurfaceKHR",
136 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700137
138 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700139 { "eglCreateImageKHR",
140 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
141 { "eglDestroyImageKHR",
142 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700143
144 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
145 { "eglCreateSyncKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
147 { "eglDestroySyncKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
149 { "eglClientWaitSyncKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
151 { "eglSignalSyncKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
153 { "eglGetSyncAttribKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
155
156 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800157 { "eglGetSystemTimeFrequencyNV",
158 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
159 { "eglGetSystemTimeNV",
160 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700161
Mathias Agopian2bb71682013-03-27 17:32:41 -0700162 // EGL_KHR_wait_sync
163 { "eglWaitSyncKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700165
166 // EGL_ANDROID_presentation_time
167 { "eglPresentationTimeANDROID",
168 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800169
170 // EGL_KHR_swap_buffers_with_damage
171 { "eglSwapBuffersWithDamageKHR",
172 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
173
Craig Donner05249fc2016-01-15 19:33:55 -0800174 // EGL_ANDROID_native_client_buffer
175 { "eglCreateNativeClientBufferANDROID",
176 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
177
Dan Stozaa894d082015-02-19 15:27:36 -0800178 // EGL_KHR_partial_update
179 { "eglSetDamageRegionKHR",
180 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700181
182 { "eglCreateStreamKHR",
183 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
184 { "eglDestroyStreamKHR",
185 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
186 { "eglStreamAttribKHR",
187 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
188 { "eglQueryStreamKHR",
189 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
190 { "eglQueryStreamu64KHR",
191 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
192 { "eglQueryStreamTimeKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
194 { "eglCreateStreamProducerSurfaceKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
196 { "eglStreamConsumerGLTextureExternalKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
198 { "eglStreamConsumerAcquireKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
200 { "eglStreamConsumerReleaseKHR",
201 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
202 { "eglGetStreamFileDescriptorKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
204 { "eglCreateStreamFromFileDescriptorKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Mathias Agopian518ec112011-05-13 16:21:08 -0700206};
207
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700208/*
209 * These extensions entry-points should not be exposed to applications.
210 * They're used internally by the Android EGL layer.
211 */
212#define FILTER_EXTENSIONS(procname) \
213 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
214 !strcmp((procname), "eglHibernateProcessIMG") || \
215 !strcmp((procname), "eglAwakenProcessIMG") || \
216 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
217
218
219
Mathias Agopian518ec112011-05-13 16:21:08 -0700220// accesses protected by sExtensionMapMutex
221static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
222static int sGLExtentionSlot = 0;
223static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
224
225static void(*findProcAddress(const char* name,
226 const extention_map_t* map, size_t n))() {
227 for (uint32_t i=0 ; i<n ; i++) {
228 if (!strcmp(name, map[i].name)) {
229 return map[i].address;
230 }
231 }
232 return NULL;
233}
234
235// ----------------------------------------------------------------------------
236
Mathias Agopian518ec112011-05-13 16:21:08 -0700237extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
238extern EGLBoolean egl_init_drivers();
239extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700240extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700241
Mathias Agopian518ec112011-05-13 16:21:08 -0700242} // namespace android;
243
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700244
Mathias Agopian518ec112011-05-13 16:21:08 -0700245// ----------------------------------------------------------------------------
246
247static inline void clearError() { egl_tls_t::clearError(); }
248static inline EGLContext getContext() { return egl_tls_t::getContext(); }
249
250// ----------------------------------------------------------------------------
251
252EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
253{
254 clearError();
255
Dan Stozac3289c42014-01-17 11:38:34 -0800256 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700257 if (index >= NUM_DISPLAYS) {
258 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
259 }
260
261 if (egl_init_drivers() == EGL_FALSE) {
262 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
263 }
264
265 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
266 return dpy;
267}
268
269// ----------------------------------------------------------------------------
270// Initialization
271// ----------------------------------------------------------------------------
272
273EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
274{
275 clearError();
276
Jesse Hallb29e5e82012-04-04 16:53:42 -0700277 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700278 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
279
280 EGLBoolean res = dp->initialize(major, minor);
281
282 return res;
283}
284
285EGLBoolean eglTerminate(EGLDisplay dpy)
286{
287 // NOTE: don't unload the drivers b/c some APIs can be called
288 // after eglTerminate() has been called. eglTerminate() only
289 // terminates an EGLDisplay, not a EGL itself.
290
291 clearError();
292
Jesse Hallb29e5e82012-04-04 16:53:42 -0700293 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
295
296 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800297
Mathias Agopian518ec112011-05-13 16:21:08 -0700298 return res;
299}
300
301// ----------------------------------------------------------------------------
302// configuration
303// ----------------------------------------------------------------------------
304
305EGLBoolean eglGetConfigs( EGLDisplay dpy,
306 EGLConfig *configs,
307 EGLint config_size, EGLint *num_config)
308{
309 clearError();
310
Jesse Hallb29e5e82012-04-04 16:53:42 -0700311 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700312 if (!dp) return EGL_FALSE;
313
Mathias Agopian7773c432012-02-13 20:06:08 -0800314 if (num_config==0) {
315 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700316 }
317
Mathias Agopian7773c432012-02-13 20:06:08 -0800318 EGLBoolean res = EGL_FALSE;
319 *num_config = 0;
320
321 egl_connection_t* const cnx = &gEGLImpl;
322 if (cnx->dso) {
323 res = cnx->egl.eglGetConfigs(
324 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700325 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800326
327 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700328}
329
330EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
331 EGLConfig *configs, EGLint config_size,
332 EGLint *num_config)
333{
334 clearError();
335
Jesse Hallb29e5e82012-04-04 16:53:42 -0700336 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700337 if (!dp) return EGL_FALSE;
338
339 if (num_config==0) {
340 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
341 }
342
Mathias Agopian518ec112011-05-13 16:21:08 -0700343 EGLBoolean res = EGL_FALSE;
344 *num_config = 0;
345
Mathias Agopianada798b2012-02-13 17:09:30 -0800346 egl_connection_t* const cnx = &gEGLImpl;
347 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700348 if (attrib_list) {
349 char value[PROPERTY_VALUE_MAX];
350 property_get("debug.egl.force_msaa", value, "false");
351
352 if (!strcmp(value, "true")) {
353 size_t attribCount = 0;
354 EGLint attrib = attrib_list[0];
355
356 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700357 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700358 const EGLint *attribRendererable = NULL;
359 const EGLint *attribCaveat = NULL;
360
361 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700362 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700363 while (attrib != EGL_NONE) {
364 attrib = attrib_list[attribCount];
365 switch (attrib) {
366 case EGL_RENDERABLE_TYPE:
367 attribRendererable = &attrib_list[attribCount];
368 break;
369 case EGL_CONFIG_CAVEAT:
370 attribCaveat = &attrib_list[attribCount];
371 break;
372 }
373 attribCount++;
374 }
375
376 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
377 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800378
Romain Guy1cffc802012-10-15 18:13:05 -0700379 // Insert 2 extra attributes to force-enable MSAA 4x
380 EGLint aaAttribs[attribCount + 4];
381 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
382 aaAttribs[1] = 1;
383 aaAttribs[2] = EGL_SAMPLES;
384 aaAttribs[3] = 4;
385
386 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
387
388 EGLint numConfigAA;
389 EGLBoolean resAA = cnx->egl.eglChooseConfig(
390 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
391
392 if (resAA == EGL_TRUE && numConfigAA > 0) {
393 ALOGD("Enabling MSAA 4x");
394 *num_config = numConfigAA;
395 return resAA;
396 }
397 }
398 }
399 }
400
Mathias Agopian7773c432012-02-13 20:06:08 -0800401 res = cnx->egl.eglChooseConfig(
402 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700403 }
404 return res;
405}
406
407EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
408 EGLint attribute, EGLint *value)
409{
410 clearError();
411
Jesse Hallb29e5e82012-04-04 16:53:42 -0700412 egl_connection_t* cnx = NULL;
413 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
414 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800415
Mathias Agopian518ec112011-05-13 16:21:08 -0700416 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800417 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700418}
419
420// ----------------------------------------------------------------------------
421// surfaces
422// ----------------------------------------------------------------------------
423
Jesse Halla2ba4282013-09-14 21:00:14 -0700424// The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
Jesse Hallc2e41222013-08-08 13:40:22 -0700425// been added to the Khronos egl.h.
426#define EGL_GL_COLORSPACE_KHR EGL_VG_COLORSPACE
427#define EGL_GL_COLORSPACE_SRGB_KHR EGL_VG_COLORSPACE_sRGB
428#define EGL_GL_COLORSPACE_LINEAR_KHR EGL_VG_COLORSPACE_LINEAR
429
430// Turn linear formats into corresponding sRGB formats when colorspace is
431// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
432// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800433// the modification isn't possible, the original dataSpace is returned.
434static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
435 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700436 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800437 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700438 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800439 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700440 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800441 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700442}
443
Mathias Agopian518ec112011-05-13 16:21:08 -0700444EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
445 NativeWindowType window,
446 const EGLint *attrib_list)
447{
448 clearError();
449
Jesse Hallb29e5e82012-04-04 16:53:42 -0700450 egl_connection_t* cnx = NULL;
451 egl_display_ptr dp = validate_display_connection(dpy, cnx);
452 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800453 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700454
Andy McFaddend566ce32014-01-07 15:54:17 -0800455 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
456 if (result != OK) {
457 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
458 "failed (%#x) (already connected to another API?)",
459 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700460 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700461 }
462
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700463 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700464 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
465 // of our native format. So if sRGB gamma is requested, we have to
466 // modify the EGLconfig's format before setting the native window's
467 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800468
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700469 // by default, just pick RGBA_8888
470 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800471 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700472
473 EGLint a = 0;
474 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
475 if (a > 0) {
476 // alpha-channel requested, there's really only one suitable format
477 format = HAL_PIXEL_FORMAT_RGBA_8888;
478 } else {
479 EGLint r, g, b;
480 r = g = b = 0;
481 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
482 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
483 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
484 EGLint colorDepth = r + g + b;
485 if (colorDepth <= 16) {
486 format = HAL_PIXEL_FORMAT_RGB_565;
487 } else {
488 format = HAL_PIXEL_FORMAT_RGBX_8888;
489 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700490 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700491
492 // now select a corresponding sRGB format if needed
493 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
494 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
495 if (*attr == EGL_GL_COLORSPACE_KHR) {
Jesse Halla2ba4282013-09-14 21:00:14 -0700496 if (ENABLE_EGL_KHR_GL_COLORSPACE) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800497 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jesse Halla2ba4282013-09-14 21:00:14 -0700498 } else {
499 // Normally we'd pass through unhandled attributes to
500 // the driver. But in case the driver implements this
501 // extension but we're disabling it, we want to prevent
502 // it getting through -- support will be broken without
503 // our help.
504 ALOGE("sRGB window surfaces not supported");
505 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
506 }
Jamie Gennisbee205f2011-07-01 13:12:07 -0700507 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700508 }
509 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800510
Jesse Hallc2e41222013-08-08 13:40:22 -0700511 if (format != 0) {
512 int err = native_window_set_buffers_format(window, format);
513 if (err != 0) {
514 ALOGE("error setting native window pixel format: %s (%d)",
515 strerror(-err), err);
516 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
517 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
518 }
519 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700520
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800521 if (dataSpace != 0) {
522 int err = native_window_set_buffers_data_space(window, dataSpace);
523 if (err != 0) {
524 ALOGE("error setting native window pixel dataSpace: %s (%d)",
525 strerror(-err), err);
526 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
527 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
528 }
529 }
530
Jamie Gennis59769462011-11-19 18:04:43 -0800531 // the EGL spec requires that a new EGLSurface default to swap interval
532 // 1, so explicitly set that on the window here.
533 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
534 anw->setSwapInterval(anw, 1);
535
Mathias Agopian518ec112011-05-13 16:21:08 -0700536 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800537 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700538 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700539 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
540 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700541 return s;
542 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700543
544 // EGLSurface creation failed
545 native_window_set_buffers_format(window, 0);
546 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700547 }
548 return EGL_NO_SURFACE;
549}
550
551EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
552 NativePixmapType pixmap,
553 const EGLint *attrib_list)
554{
555 clearError();
556
Jesse Hallb29e5e82012-04-04 16:53:42 -0700557 egl_connection_t* cnx = NULL;
558 egl_display_ptr dp = validate_display_connection(dpy, cnx);
559 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700560 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800561 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700562 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700563 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
564 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700565 return s;
566 }
567 }
568 return EGL_NO_SURFACE;
569}
570
571EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
572 const EGLint *attrib_list)
573{
574 clearError();
575
Jesse Hallb29e5e82012-04-04 16:53:42 -0700576 egl_connection_t* cnx = NULL;
577 egl_display_ptr dp = validate_display_connection(dpy, cnx);
578 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700579 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800580 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700581 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700582 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
583 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700584 return s;
585 }
586 }
587 return EGL_NO_SURFACE;
588}
Jesse Hall47743382013-02-08 11:13:46 -0800589
Mathias Agopian518ec112011-05-13 16:21:08 -0700590EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
591{
592 clearError();
593
Jesse Hallb29e5e82012-04-04 16:53:42 -0700594 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700595 if (!dp) return EGL_FALSE;
596
Jesse Hallb29e5e82012-04-04 16:53:42 -0700597 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700598 if (!_s.get())
599 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700600
601 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800602 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700603 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700604 _s.terminate();
605 }
606 return result;
607}
608
609EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
610 EGLint attribute, EGLint *value)
611{
612 clearError();
613
Jesse Hallb29e5e82012-04-04 16:53:42 -0700614 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700615 if (!dp) return EGL_FALSE;
616
Jesse Hallb29e5e82012-04-04 16:53:42 -0700617 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700618 if (!_s.get())
619 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700620
Mathias Agopian518ec112011-05-13 16:21:08 -0700621 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800622 return s->cnx->egl.eglQuerySurface(
623 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700624}
625
Jamie Gennise8696a42012-01-15 18:54:57 -0800626void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800627 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800628 clearError();
629
Jesse Hallb29e5e82012-04-04 16:53:42 -0700630 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800631 if (!dp) {
632 return;
633 }
634
Jesse Hallb29e5e82012-04-04 16:53:42 -0700635 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800636 if (!_s.get()) {
637 setError(EGL_BAD_SURFACE, EGL_FALSE);
638 return;
639 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800640}
641
Mathias Agopian518ec112011-05-13 16:21:08 -0700642// ----------------------------------------------------------------------------
643// Contexts
644// ----------------------------------------------------------------------------
645
646EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
647 EGLContext share_list, const EGLint *attrib_list)
648{
649 clearError();
650
Jesse Hallb29e5e82012-04-04 16:53:42 -0700651 egl_connection_t* cnx = NULL;
652 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700653 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700654 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700655 if (!ContextRef(dp.get(), share_list).get()) {
656 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
657 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700658 egl_context_t* const c = get_context(share_list);
659 share_list = c->context;
660 }
661 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800662 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700663 if (context != EGL_NO_CONTEXT) {
664 // figure out if it's a GLESv1 or GLESv2
665 int version = 0;
666 if (attrib_list) {
667 while (*attrib_list != EGL_NONE) {
668 GLint attr = *attrib_list++;
669 GLint value = *attrib_list++;
670 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
671 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800672 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800673 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800674 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700675 }
676 }
677 };
678 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700679 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
680 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700681 return c;
682 }
683 }
684 return EGL_NO_CONTEXT;
685}
686
687EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
688{
689 clearError();
690
Jesse Hallb29e5e82012-04-04 16:53:42 -0700691 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700692 if (!dp)
693 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700694
Jesse Hallb29e5e82012-04-04 16:53:42 -0700695 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700696 if (!_c.get())
697 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800698
Mathias Agopian518ec112011-05-13 16:21:08 -0700699 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800700 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700701 if (result == EGL_TRUE) {
702 _c.terminate();
703 }
704 return result;
705}
706
Mathias Agopian518ec112011-05-13 16:21:08 -0700707EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
708 EGLSurface read, EGLContext ctx)
709{
710 clearError();
711
Jesse Hallb29e5e82012-04-04 16:53:42 -0700712 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
714
Mathias Agopian5b287a62011-05-16 18:58:55 -0700715 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
716 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
717 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700718 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
719 (draw != EGL_NO_SURFACE) ) {
720 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
721 }
722
723 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700724 ContextRef _c(dp.get(), ctx);
725 SurfaceRef _d(dp.get(), draw);
726 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700727
728 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700729 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700730 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700731 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700732 }
733
734 // these are the underlying implementation's object
735 EGLContext impl_ctx = EGL_NO_CONTEXT;
736 EGLSurface impl_draw = EGL_NO_SURFACE;
737 EGLSurface impl_read = EGL_NO_SURFACE;
738
739 // these are our objects structs passed in
740 egl_context_t * c = NULL;
741 egl_surface_t const * d = NULL;
742 egl_surface_t const * r = NULL;
743
744 // these are the current objects structs
745 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800746
Mathias Agopian518ec112011-05-13 16:21:08 -0700747 if (ctx != EGL_NO_CONTEXT) {
748 c = get_context(ctx);
749 impl_ctx = c->context;
750 } else {
751 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700752 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
753 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
754 return setError(EGL_BAD_MATCH, EGL_FALSE);
755 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 if (cur_c == NULL) {
757 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 // not an error, there is just no current context.
759 return EGL_TRUE;
760 }
761 }
762
763 // retrieve the underlying implementation's draw EGLSurface
764 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700765 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700766 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 impl_draw = d->surface;
768 }
769
770 // retrieve the underlying implementation's read EGLSurface
771 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700772 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700773 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700774 impl_read = r->surface;
775 }
776
Mathias Agopian518ec112011-05-13 16:21:08 -0700777
Jesse Hallb29e5e82012-04-04 16:53:42 -0700778 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800779 draw, read, ctx,
780 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700781
782 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800783 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700784 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
785 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700786 _c.acquire();
787 _r.acquire();
788 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700789 } else {
790 setGLHooksThreadSpecific(&gHooksNoContext);
791 egl_tls_t::setContext(EGL_NO_CONTEXT);
792 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700793 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000794 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700795 egl_connection_t* const cnx = &gEGLImpl;
796 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700797 }
798 return result;
799}
800
801
802EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
803 EGLint attribute, EGLint *value)
804{
805 clearError();
806
Jesse Hallb29e5e82012-04-04 16:53:42 -0700807 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700808 if (!dp) return EGL_FALSE;
809
Jesse Hallb29e5e82012-04-04 16:53:42 -0700810 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
812
Mathias Agopian518ec112011-05-13 16:21:08 -0700813 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800814 return c->cnx->egl.eglQueryContext(
815 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700816
Mathias Agopian518ec112011-05-13 16:21:08 -0700817}
818
819EGLContext eglGetCurrentContext(void)
820{
821 // could be called before eglInitialize(), but we wouldn't have a context
822 // then, and this function would correctly return EGL_NO_CONTEXT.
823
824 clearError();
825
826 EGLContext ctx = getContext();
827 return ctx;
828}
829
830EGLSurface eglGetCurrentSurface(EGLint readdraw)
831{
832 // could be called before eglInitialize(), but we wouldn't have a context
833 // then, and this function would correctly return EGL_NO_SURFACE.
834
835 clearError();
836
837 EGLContext ctx = getContext();
838 if (ctx) {
839 egl_context_t const * const c = get_context(ctx);
840 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
841 switch (readdraw) {
842 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800843 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700844 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
845 }
846 }
847 return EGL_NO_SURFACE;
848}
849
850EGLDisplay eglGetCurrentDisplay(void)
851{
852 // could be called before eglInitialize(), but we wouldn't have a context
853 // then, and this function would correctly return EGL_NO_DISPLAY.
854
855 clearError();
856
857 EGLContext ctx = getContext();
858 if (ctx) {
859 egl_context_t const * const c = get_context(ctx);
860 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
861 return c->dpy;
862 }
863 return EGL_NO_DISPLAY;
864}
865
866EGLBoolean eglWaitGL(void)
867{
Mathias Agopian518ec112011-05-13 16:21:08 -0700868 clearError();
869
Mathias Agopianada798b2012-02-13 17:09:30 -0800870 egl_connection_t* const cnx = &gEGLImpl;
871 if (!cnx->dso)
872 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
873
874 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700875}
876
877EGLBoolean eglWaitNative(EGLint engine)
878{
Mathias Agopian518ec112011-05-13 16:21:08 -0700879 clearError();
880
Mathias Agopianada798b2012-02-13 17:09:30 -0800881 egl_connection_t* const cnx = &gEGLImpl;
882 if (!cnx->dso)
883 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
884
885 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700886}
887
888EGLint eglGetError(void)
889{
Mathias Agopianada798b2012-02-13 17:09:30 -0800890 EGLint err = EGL_SUCCESS;
891 egl_connection_t* const cnx = &gEGLImpl;
892 if (cnx->dso) {
893 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700894 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800895 if (err == EGL_SUCCESS) {
896 err = egl_tls_t::getError();
897 }
898 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700899}
900
Michael Chockc0ec5e22014-01-27 08:14:33 -0800901static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700902 const char* procname) {
903 const egl_connection_t* cnx = &gEGLImpl;
904 void* proc = NULL;
905
Michael Chockc0ec5e22014-01-27 08:14:33 -0800906 proc = dlsym(cnx->libEgl, procname);
907 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
908
Jesse Hallc07b5202013-07-04 12:08:16 -0700909 proc = dlsym(cnx->libGles2, procname);
910 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
911
912 proc = dlsym(cnx->libGles1, procname);
913 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
914
915 return NULL;
916}
917
Mathias Agopian518ec112011-05-13 16:21:08 -0700918__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
919{
920 // eglGetProcAddress() could be the very first function called
921 // in which case we must make sure we've initialized ourselves, this
922 // happens the first time egl_get_display() is called.
923
924 clearError();
925
926 if (egl_init_drivers() == EGL_FALSE) {
927 setError(EGL_BAD_PARAMETER, NULL);
928 return NULL;
929 }
930
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700931 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700932 return NULL;
933 }
934
Mathias Agopian518ec112011-05-13 16:21:08 -0700935 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700936 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700937 if (addr) return addr;
938
Michael Chockc0ec5e22014-01-27 08:14:33 -0800939 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700940 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700941
Mathias Agopian518ec112011-05-13 16:21:08 -0700942 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
943 pthread_mutex_lock(&sExtensionMapMutex);
944
945 /*
946 * Since eglGetProcAddress() is not associated to anything, it needs
947 * to return a function pointer that "works" regardless of what
948 * the current context is.
949 *
950 * For this reason, we return a "forwarder", a small stub that takes
951 * care of calling the function associated with the context
952 * currently bound.
953 *
954 * We first look for extensions we've already resolved, if we're seeing
955 * this extension for the first time, we go through all our
956 * implementations and call eglGetProcAddress() and record the
957 * result in the appropriate implementation hooks and return the
958 * address of the forwarder corresponding to that hook set.
959 *
960 */
961
962 const String8 name(procname);
963 addr = sGLExtentionMap.valueFor(name);
964 const int slot = sGLExtentionSlot;
965
Steve Blocke6f43dd2012-01-06 19:20:56 +0000966 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700967 "no more slots for eglGetProcAddress(\"%s\")",
968 procname);
969
970 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
971 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800972
973 egl_connection_t* const cnx = &gEGLImpl;
974 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800975 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800976 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800977 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
978 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800979 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800980 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700981 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800982
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 if (found) {
984 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700985 sGLExtentionMap.add(name, addr);
986 sGLExtentionSlot++;
987 }
988 }
989
990 pthread_mutex_unlock(&sExtensionMapMutex);
991 return addr;
992}
993
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700994class FrameCompletionThread : public Thread {
995public:
996
997 static void queueSync(EGLSyncKHR sync) {
998 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
999 static bool running = false;
1000 if (!running) {
1001 thread->run("GPUFrameCompletion");
1002 running = true;
1003 }
1004 {
1005 Mutex::Autolock lock(thread->mMutex);
1006 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1007 thread->mFramesQueued).string());
1008 thread->mQueue.push_back(sync);
1009 thread->mCondition.signal();
1010 thread->mFramesQueued++;
1011 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1012 }
1013 }
1014
1015private:
1016 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1017
1018 virtual bool threadLoop() {
1019 EGLSyncKHR sync;
1020 uint32_t frameNum;
1021 {
1022 Mutex::Autolock lock(mMutex);
1023 while (mQueue.isEmpty()) {
1024 mCondition.wait(mMutex);
1025 }
1026 sync = mQueue[0];
1027 frameNum = mFramesCompleted;
1028 }
1029 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1030 {
1031 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1032 frameNum).string());
1033 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1034 if (result == EGL_FALSE) {
1035 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1036 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1037 ALOGE("FrameCompletion: timeout waiting for fence");
1038 }
1039 eglDestroySyncKHR(dpy, sync);
1040 }
1041 {
1042 Mutex::Autolock lock(mMutex);
1043 mQueue.removeAt(0);
1044 mFramesCompleted++;
1045 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1046 }
1047 return true;
1048 }
1049
1050 uint32_t mFramesQueued;
1051 uint32_t mFramesCompleted;
1052 Vector<EGLSyncKHR> mQueue;
1053 Condition mCondition;
1054 Mutex mMutex;
1055};
1056
Dan Stozaa894d082015-02-19 15:27:36 -08001057EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1058 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001059{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001060 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001061 clearError();
1062
Jesse Hallb29e5e82012-04-04 16:53:42 -07001063 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001064 if (!dp) return EGL_FALSE;
1065
Jesse Hallb29e5e82012-04-04 16:53:42 -07001066 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001067 if (!_s.get())
1068 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001069
Mathias Agopian518ec112011-05-13 16:21:08 -07001070 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001071
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001072 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1073 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1074 if (sync != EGL_NO_SYNC_KHR) {
1075 FrameCompletionThread::queueSync(sync);
1076 }
1077 }
1078
Mathias Agopian7db993a2012-03-25 00:49:46 -07001079 if (CC_UNLIKELY(dp->finishOnSwap)) {
1080 uint32_t pixel;
1081 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1082 if (c) {
1083 // glReadPixels() ensures that the frame is complete
1084 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1085 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1086 }
1087 }
1088
Dan Stozaa894d082015-02-19 15:27:36 -08001089 if (n_rects == 0) {
1090 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1091 }
1092
1093 Vector<android_native_rect_t> androidRects;
1094 for (int r = 0; r < n_rects; ++r) {
1095 int offset = r * 4;
1096 int x = rects[offset];
1097 int y = rects[offset + 1];
1098 int width = rects[offset + 2];
1099 int height = rects[offset + 3];
1100 android_native_rect_t androidRect;
1101 androidRect.left = x;
1102 androidRect.top = y + height;
1103 androidRect.right = x + width;
1104 androidRect.bottom = y;
1105 androidRects.push_back(androidRect);
1106 }
1107 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1108 androidRects.size());
1109
1110 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1111 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1112 rects, n_rects);
1113 } else {
1114 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1115 }
1116}
1117
1118EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1119{
1120 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001121}
1122
1123EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1124 NativePixmapType target)
1125{
1126 clearError();
1127
Jesse Hallb29e5e82012-04-04 16:53:42 -07001128 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001129 if (!dp) return EGL_FALSE;
1130
Jesse Hallb29e5e82012-04-04 16:53:42 -07001131 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001132 if (!_s.get())
1133 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001134
Mathias Agopian518ec112011-05-13 16:21:08 -07001135 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001136 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001137}
1138
1139const char* eglQueryString(EGLDisplay dpy, EGLint name)
1140{
1141 clearError();
1142
Jesse Hallb29e5e82012-04-04 16:53:42 -07001143 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001144 if (!dp) return (const char *) NULL;
1145
1146 switch (name) {
1147 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001148 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001149 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001150 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001151 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001152 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001153 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001154 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001155 }
1156 return setError(EGL_BAD_PARAMETER, (const char *)0);
1157}
1158
Mathias Agopianca088332013-03-28 17:44:13 -07001159EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1160{
1161 clearError();
1162
1163 const egl_display_ptr dp = validate_display(dpy);
1164 if (!dp) return (const char *) NULL;
1165
1166 switch (name) {
1167 case EGL_VENDOR:
1168 return dp->disp.queryString.vendor;
1169 case EGL_VERSION:
1170 return dp->disp.queryString.version;
1171 case EGL_EXTENSIONS:
1172 return dp->disp.queryString.extensions;
1173 case EGL_CLIENT_APIS:
1174 return dp->disp.queryString.clientApi;
1175 }
1176 return setError(EGL_BAD_PARAMETER, (const char *)0);
1177}
Mathias Agopian518ec112011-05-13 16:21:08 -07001178
1179// ----------------------------------------------------------------------------
1180// EGL 1.1
1181// ----------------------------------------------------------------------------
1182
1183EGLBoolean eglSurfaceAttrib(
1184 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1185{
1186 clearError();
1187
Jesse Hallb29e5e82012-04-04 16:53:42 -07001188 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001189 if (!dp) return EGL_FALSE;
1190
Jesse Hallb29e5e82012-04-04 16:53:42 -07001191 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001192 if (!_s.get())
1193 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001194
Mathias Agopian518ec112011-05-13 16:21:08 -07001195 egl_surface_t const * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001196
1197 //XXX: temporary hack for the EGL hook-up for single buffer mode
1198 if (attribute == EGL_RENDER_BUFFER && (value == EGL_BACK_BUFFER ||
1199 value == EGL_SINGLE_BUFFER)) {
Pablo Ceballosff95aab2016-01-13 17:09:58 -08001200 native_window_set_auto_refresh(s->win.get(), true);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001201 return (native_window_set_single_buffer_mode(s->win.get(),
1202 value == EGL_SINGLE_BUFFER)) ? EGL_TRUE : EGL_FALSE;
1203 }
1204
Mathias Agopian518ec112011-05-13 16:21:08 -07001205 if (s->cnx->egl.eglSurfaceAttrib) {
1206 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001207 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001208 }
1209 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1210}
1211
1212EGLBoolean eglBindTexImage(
1213 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1214{
1215 clearError();
1216
Jesse Hallb29e5e82012-04-04 16:53:42 -07001217 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001218 if (!dp) return EGL_FALSE;
1219
Jesse Hallb29e5e82012-04-04 16:53:42 -07001220 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001221 if (!_s.get())
1222 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001223
Mathias Agopian518ec112011-05-13 16:21:08 -07001224 egl_surface_t const * const s = get_surface(surface);
1225 if (s->cnx->egl.eglBindTexImage) {
1226 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001227 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001228 }
1229 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1230}
1231
1232EGLBoolean eglReleaseTexImage(
1233 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1234{
1235 clearError();
1236
Jesse Hallb29e5e82012-04-04 16:53:42 -07001237 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001238 if (!dp) return EGL_FALSE;
1239
Jesse Hallb29e5e82012-04-04 16:53:42 -07001240 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001241 if (!_s.get())
1242 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001243
Mathias Agopian518ec112011-05-13 16:21:08 -07001244 egl_surface_t const * const s = get_surface(surface);
1245 if (s->cnx->egl.eglReleaseTexImage) {
1246 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001247 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001248 }
1249 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1250}
1251
1252EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1253{
1254 clearError();
1255
Jesse Hallb29e5e82012-04-04 16:53:42 -07001256 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001257 if (!dp) return EGL_FALSE;
1258
1259 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001260 egl_connection_t* const cnx = &gEGLImpl;
1261 if (cnx->dso && cnx->egl.eglSwapInterval) {
1262 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001263 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001264
Mathias Agopian518ec112011-05-13 16:21:08 -07001265 return res;
1266}
1267
1268
1269// ----------------------------------------------------------------------------
1270// EGL 1.2
1271// ----------------------------------------------------------------------------
1272
1273EGLBoolean eglWaitClient(void)
1274{
1275 clearError();
1276
Mathias Agopianada798b2012-02-13 17:09:30 -08001277 egl_connection_t* const cnx = &gEGLImpl;
1278 if (!cnx->dso)
1279 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1280
1281 EGLBoolean res;
1282 if (cnx->egl.eglWaitClient) {
1283 res = cnx->egl.eglWaitClient();
1284 } else {
1285 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001286 }
1287 return res;
1288}
1289
1290EGLBoolean eglBindAPI(EGLenum api)
1291{
1292 clearError();
1293
1294 if (egl_init_drivers() == EGL_FALSE) {
1295 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1296 }
1297
1298 // bind this API on all EGLs
1299 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001300 egl_connection_t* const cnx = &gEGLImpl;
1301 if (cnx->dso && cnx->egl.eglBindAPI) {
1302 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001303 }
1304 return res;
1305}
1306
1307EGLenum eglQueryAPI(void)
1308{
1309 clearError();
1310
1311 if (egl_init_drivers() == EGL_FALSE) {
1312 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1313 }
1314
Mathias Agopianada798b2012-02-13 17:09:30 -08001315 egl_connection_t* const cnx = &gEGLImpl;
1316 if (cnx->dso && cnx->egl.eglQueryAPI) {
1317 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001318 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001319
Mathias Agopian518ec112011-05-13 16:21:08 -07001320 // or, it can only be OpenGL ES
1321 return EGL_OPENGL_ES_API;
1322}
1323
1324EGLBoolean eglReleaseThread(void)
1325{
1326 clearError();
1327
1328 // If there is context bound to the thread, release it
Mathias Agopianfb87e542012-01-30 18:20:52 -08001329 egl_display_t::loseCurrent(get_context(getContext()));
Mathias Agopian518ec112011-05-13 16:21:08 -07001330
Mathias Agopianada798b2012-02-13 17:09:30 -08001331 egl_connection_t* const cnx = &gEGLImpl;
1332 if (cnx->dso && cnx->egl.eglReleaseThread) {
1333 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001334 }
1335 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001336 return EGL_TRUE;
1337}
1338
1339EGLSurface eglCreatePbufferFromClientBuffer(
1340 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1341 EGLConfig config, const EGLint *attrib_list)
1342{
1343 clearError();
1344
Jesse Hallb29e5e82012-04-04 16:53:42 -07001345 egl_connection_t* cnx = NULL;
1346 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1347 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001348 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1349 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001350 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001351 }
1352 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1353}
1354
1355// ----------------------------------------------------------------------------
1356// EGL_EGLEXT_VERSION 3
1357// ----------------------------------------------------------------------------
1358
1359EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1360 const EGLint *attrib_list)
1361{
1362 clearError();
1363
Jesse Hallb29e5e82012-04-04 16:53:42 -07001364 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001365 if (!dp) return EGL_FALSE;
1366
Jesse Hallb29e5e82012-04-04 16:53:42 -07001367 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001368 if (!_s.get())
1369 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001370
1371 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001372 if (s->cnx->egl.eglLockSurfaceKHR) {
1373 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001374 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001375 }
1376 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1377}
1378
1379EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1380{
1381 clearError();
1382
Jesse Hallb29e5e82012-04-04 16:53:42 -07001383 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001384 if (!dp) return EGL_FALSE;
1385
Jesse Hallb29e5e82012-04-04 16:53:42 -07001386 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001387 if (!_s.get())
1388 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001389
1390 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001391 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001392 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001393 }
1394 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1395}
1396
1397EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1398 EGLClientBuffer buffer, const EGLint *attrib_list)
1399{
1400 clearError();
1401
Jesse Hallb29e5e82012-04-04 16:53:42 -07001402 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001403 if (!dp) return EGL_NO_IMAGE_KHR;
1404
Jesse Hallb29e5e82012-04-04 16:53:42 -07001405 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001406 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001407
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001408 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1409 egl_connection_t* const cnx = &gEGLImpl;
1410 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1411 result = cnx->egl.eglCreateImageKHR(
1412 dp->disp.dpy,
1413 c ? c->context : EGL_NO_CONTEXT,
1414 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001415 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001416 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001417}
1418
1419EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1420{
1421 clearError();
1422
Jesse Hallb29e5e82012-04-04 16:53:42 -07001423 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001424 if (!dp) return EGL_FALSE;
1425
Steven Holte646a5c52012-06-04 20:02:11 -07001426 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001427 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001428 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001429 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001430 }
Steven Holte646a5c52012-06-04 20:02:11 -07001431 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001432}
1433
1434// ----------------------------------------------------------------------------
1435// EGL_EGLEXT_VERSION 5
1436// ----------------------------------------------------------------------------
1437
1438
1439EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1440{
1441 clearError();
1442
Jesse Hallb29e5e82012-04-04 16:53:42 -07001443 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001444 if (!dp) return EGL_NO_SYNC_KHR;
1445
Mathias Agopian518ec112011-05-13 16:21:08 -07001446 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001447 egl_connection_t* const cnx = &gEGLImpl;
1448 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1449 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001451 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001452}
1453
1454EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1455{
1456 clearError();
1457
Jesse Hallb29e5e82012-04-04 16:53:42 -07001458 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001459 if (!dp) return EGL_FALSE;
1460
Mathias Agopian518ec112011-05-13 16:21:08 -07001461 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001462 egl_connection_t* const cnx = &gEGLImpl;
1463 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1464 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001465 }
1466 return result;
1467}
1468
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001469EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1470 clearError();
1471
1472 const egl_display_ptr dp = validate_display(dpy);
1473 if (!dp) return EGL_FALSE;
1474
1475 EGLBoolean result = EGL_FALSE;
1476 egl_connection_t* const cnx = &gEGLImpl;
1477 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1478 result = cnx->egl.eglSignalSyncKHR(
1479 dp->disp.dpy, sync, mode);
1480 }
1481 return result;
1482}
1483
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001484EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1485 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001486{
1487 clearError();
1488
Jesse Hallb29e5e82012-04-04 16:53:42 -07001489 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001490 if (!dp) return EGL_FALSE;
1491
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001492 EGLBoolean result = EGL_FALSE;
1493 egl_connection_t* const cnx = &gEGLImpl;
1494 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1495 result = cnx->egl.eglClientWaitSyncKHR(
1496 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001497 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001498 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001499}
1500
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001501EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1502 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001503{
1504 clearError();
1505
Jesse Hallb29e5e82012-04-04 16:53:42 -07001506 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001507 if (!dp) return EGL_FALSE;
1508
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001509 EGLBoolean result = EGL_FALSE;
1510 egl_connection_t* const cnx = &gEGLImpl;
1511 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1512 result = cnx->egl.eglGetSyncAttribKHR(
1513 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001514 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001515 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001516}
1517
Season Li000d88f2015-07-01 11:39:40 -07001518EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1519{
1520 clearError();
1521
1522 const egl_display_ptr dp = validate_display(dpy);
1523 if (!dp) return EGL_NO_STREAM_KHR;
1524
1525 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1526 egl_connection_t* const cnx = &gEGLImpl;
1527 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1528 result = cnx->egl.eglCreateStreamKHR(
1529 dp->disp.dpy, attrib_list);
1530 }
1531 return result;
1532}
1533
1534EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1535{
1536 clearError();
1537
1538 const egl_display_ptr dp = validate_display(dpy);
1539 if (!dp) return EGL_FALSE;
1540
1541 EGLBoolean result = EGL_FALSE;
1542 egl_connection_t* const cnx = &gEGLImpl;
1543 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1544 result = cnx->egl.eglDestroyStreamKHR(
1545 dp->disp.dpy, stream);
1546 }
1547 return result;
1548}
1549
1550EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1551 EGLenum attribute, EGLint value)
1552{
1553 clearError();
1554
1555 const egl_display_ptr dp = validate_display(dpy);
1556 if (!dp) return EGL_FALSE;
1557
1558 EGLBoolean result = EGL_FALSE;
1559 egl_connection_t* const cnx = &gEGLImpl;
1560 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1561 result = cnx->egl.eglStreamAttribKHR(
1562 dp->disp.dpy, stream, attribute, value);
1563 }
1564 return result;
1565}
1566
1567EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1568 EGLenum attribute, EGLint *value)
1569{
1570 clearError();
1571
1572 const egl_display_ptr dp = validate_display(dpy);
1573 if (!dp) return EGL_FALSE;
1574
1575 EGLBoolean result = EGL_FALSE;
1576 egl_connection_t* const cnx = &gEGLImpl;
1577 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1578 result = cnx->egl.eglQueryStreamKHR(
1579 dp->disp.dpy, stream, attribute, value);
1580 }
1581 return result;
1582}
1583
1584EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1585 EGLenum attribute, EGLuint64KHR *value)
1586{
1587 clearError();
1588
1589 const egl_display_ptr dp = validate_display(dpy);
1590 if (!dp) return EGL_FALSE;
1591
1592 EGLBoolean result = EGL_FALSE;
1593 egl_connection_t* const cnx = &gEGLImpl;
1594 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1595 result = cnx->egl.eglQueryStreamu64KHR(
1596 dp->disp.dpy, stream, attribute, value);
1597 }
1598 return result;
1599}
1600
1601EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1602 EGLenum attribute, EGLTimeKHR *value)
1603{
1604 clearError();
1605
1606 const egl_display_ptr dp = validate_display(dpy);
1607 if (!dp) return EGL_FALSE;
1608
1609 EGLBoolean result = EGL_FALSE;
1610 egl_connection_t* const cnx = &gEGLImpl;
1611 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1612 result = cnx->egl.eglQueryStreamTimeKHR(
1613 dp->disp.dpy, stream, attribute, value);
1614 }
1615 return result;
1616}
1617
1618EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1619 EGLStreamKHR stream, const EGLint *attrib_list)
1620{
1621 clearError();
1622
1623 egl_display_ptr dp = validate_display(dpy);
1624 if (!dp) return EGL_NO_SURFACE;
1625
1626 egl_connection_t* const cnx = &gEGLImpl;
1627 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1628 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1629 dp->disp.dpy, config, stream, attrib_list);
1630 if (surface != EGL_NO_SURFACE) {
1631 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1632 surface, cnx);
1633 return s;
1634 }
1635 }
1636 return EGL_NO_SURFACE;
1637}
1638
1639EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1640 EGLStreamKHR stream)
1641{
1642 clearError();
1643
1644 const egl_display_ptr dp = validate_display(dpy);
1645 if (!dp) return EGL_FALSE;
1646
1647 EGLBoolean result = EGL_FALSE;
1648 egl_connection_t* const cnx = &gEGLImpl;
1649 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1650 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1651 dp->disp.dpy, stream);
1652 }
1653 return result;
1654}
1655
1656EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1657 EGLStreamKHR stream)
1658{
1659 clearError();
1660
1661 const egl_display_ptr dp = validate_display(dpy);
1662 if (!dp) return EGL_FALSE;
1663
1664 EGLBoolean result = EGL_FALSE;
1665 egl_connection_t* const cnx = &gEGLImpl;
1666 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1667 result = cnx->egl.eglStreamConsumerAcquireKHR(
1668 dp->disp.dpy, stream);
1669 }
1670 return result;
1671}
1672
1673EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1674 EGLStreamKHR stream)
1675{
1676 clearError();
1677
1678 const egl_display_ptr dp = validate_display(dpy);
1679 if (!dp) return EGL_FALSE;
1680
1681 EGLBoolean result = EGL_FALSE;
1682 egl_connection_t* const cnx = &gEGLImpl;
1683 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1684 result = cnx->egl.eglStreamConsumerReleaseKHR(
1685 dp->disp.dpy, stream);
1686 }
1687 return result;
1688}
1689
1690EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1691 EGLDisplay dpy, EGLStreamKHR stream)
1692{
1693 clearError();
1694
1695 const egl_display_ptr dp = validate_display(dpy);
1696 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1697
1698 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1699 egl_connection_t* const cnx = &gEGLImpl;
1700 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1701 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1702 dp->disp.dpy, stream);
1703 }
1704 return result;
1705}
1706
1707EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1708 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1709{
1710 clearError();
1711
1712 const egl_display_ptr dp = validate_display(dpy);
1713 if (!dp) return EGL_NO_STREAM_KHR;
1714
1715 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1716 egl_connection_t* const cnx = &gEGLImpl;
1717 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1718 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1719 dp->disp.dpy, file_descriptor);
1720 }
1721 return result;
1722}
1723
Mathias Agopian518ec112011-05-13 16:21:08 -07001724// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001725// EGL_EGLEXT_VERSION 15
1726// ----------------------------------------------------------------------------
1727
1728EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1729 clearError();
1730 const egl_display_ptr dp = validate_display(dpy);
1731 if (!dp) return EGL_FALSE;
1732 EGLint result = EGL_FALSE;
1733 egl_connection_t* const cnx = &gEGLImpl;
1734 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1735 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1736 }
1737 return result;
1738}
1739
1740// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001741// ANDROID extensions
1742// ----------------------------------------------------------------------------
1743
Jamie Gennis331841b2012-09-06 14:52:00 -07001744EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1745{
1746 clearError();
1747
1748 const egl_display_ptr dp = validate_display(dpy);
1749 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1750
1751 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1752 egl_connection_t* const cnx = &gEGLImpl;
1753 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1754 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1755 }
1756 return result;
1757}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001758
Andy McFadden72841452013-03-01 16:25:32 -08001759EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1760 EGLnsecsANDROID time)
1761{
1762 clearError();
1763
1764 const egl_display_ptr dp = validate_display(dpy);
1765 if (!dp) {
1766 return EGL_FALSE;
1767 }
1768
1769 SurfaceRef _s(dp.get(), surface);
1770 if (!_s.get()) {
1771 setError(EGL_BAD_SURFACE, EGL_FALSE);
1772 return EGL_FALSE;
1773 }
1774
1775 egl_surface_t const * const s = get_surface(surface);
1776 native_window_set_buffers_timestamp(s->win.get(), time);
1777
1778 return EGL_TRUE;
1779}
1780
Craig Donner05249fc2016-01-15 19:33:55 -08001781EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1782{
1783 clearError();
1784
1785 int usage = 0;
1786 uint32_t width = 0;
1787 uint32_t height = 0;
1788 uint32_t format = 0;
1789 uint32_t red_size = 0;
1790 uint32_t green_size = 0;
1791 uint32_t blue_size = 0;
1792 uint32_t alpha_size = 0;
1793
1794#define GET_POSITIVE_VALUE(case_name, target) \
1795 case case_name: \
1796 if (value > 0) { \
1797 target = value; \
1798 } else { \
1799 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1800 } \
1801 break
1802
1803 if (attrib_list) {
1804 while (*attrib_list != EGL_NONE) {
1805 GLint attr = *attrib_list++;
1806 GLint value = *attrib_list++;
1807 switch (attr) {
1808 GET_POSITIVE_VALUE(EGL_WIDTH, width);
1809 GET_POSITIVE_VALUE(EGL_HEIGHT, height);
1810 GET_POSITIVE_VALUE(EGL_RED_SIZE, red_size);
1811 GET_POSITIVE_VALUE(EGL_GREEN_SIZE, green_size);
1812 GET_POSITIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1813 GET_POSITIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
1814 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1815 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1816 usage |= GRALLOC_USAGE_PROTECTED;
1817 // If we are using QCOM then add in extra bits. This
1818 // should be removed before launch. These correspond to:
1819 // USAGE_PRIVATE_MM_HEAP | USAGE_PRIVATE_UNCACHED
1820 usage |= 0x82000000;
1821 }
1822 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_ANDROID) {
1823 usage |= GRALLOC_USAGE_HW_RENDER;
1824 }
1825 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_ANDROID) {
1826 usage |= GRALLOC_USAGE_HW_TEXTURE;
1827 }
1828 // The buffer must be used for either a texture or a
1829 // renderbuffer.
1830 if ((value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_ANDROID) &&
1831 (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_ANDROID)) {
1832 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1833 }
1834 break;
1835 default:
1836 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1837 }
1838 }
1839 }
1840#undef GET_POSITIVE_VALUE
1841
1842 // Validate format.
1843 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1844 if (alpha_size == 8) {
1845 format = HAL_PIXEL_FORMAT_RGBA_8888;
1846 } else {
1847 format = HAL_PIXEL_FORMAT_RGB_888;
1848 }
1849 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1850 alpha_size == 0) {
1851 format == HAL_PIXEL_FORMAT_RGB_565;
1852 } else {
1853 ALOGE("Invalid native pixel format { r=%d, g=%d, b=%d, a=%d }",
1854 red_size, green_size, blue_size, alpha_size);
1855 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1856 }
1857
1858 GraphicBuffer* gBuffer = new GraphicBuffer(width, height, format, usage);
1859 const status_t err = gBuffer->initCheck();
1860 if (err != NO_ERROR) {
1861 ALOGE("Unable to create native buffer { w=%d, h=%d, f=%d, u=%#x }: %#x",
1862 width, height, format, usage, err);
1863 // Destroy the buffer.
1864 sp<GraphicBuffer> holder(gBuffer);
1865 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
1866 }
1867 ALOGD("Created new native buffer %p { w=%d, h=%d, f=%d, u=%#x }",
1868 gBuffer, width, height, format, usage);
1869 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
1870}
1871
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001872// ----------------------------------------------------------------------------
1873// NVIDIA extensions
1874// ----------------------------------------------------------------------------
1875EGLuint64NV eglGetSystemTimeFrequencyNV()
1876{
1877 clearError();
1878
1879 if (egl_init_drivers() == EGL_FALSE) {
1880 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1881 }
1882
1883 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001884 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001885
Mathias Agopianada798b2012-02-13 17:09:30 -08001886 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1887 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001888 }
1889
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001890 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001891}
1892
1893EGLuint64NV eglGetSystemTimeNV()
1894{
1895 clearError();
1896
1897 if (egl_init_drivers() == EGL_FALSE) {
1898 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1899 }
1900
1901 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001902 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001903
Mathias Agopianada798b2012-02-13 17:09:30 -08001904 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1905 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001906 }
1907
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001908 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001909}
Dan Stozaa894d082015-02-19 15:27:36 -08001910
1911// ----------------------------------------------------------------------------
1912// Partial update extension
1913// ----------------------------------------------------------------------------
1914EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1915 EGLint *rects, EGLint n_rects)
1916{
1917 clearError();
1918
1919 const egl_display_ptr dp = validate_display(dpy);
1920 if (!dp) {
1921 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1922 return EGL_FALSE;
1923 }
1924
1925 SurfaceRef _s(dp.get(), surface);
1926 if (!_s.get()) {
1927 setError(EGL_BAD_SURFACE, EGL_FALSE);
1928 return EGL_FALSE;
1929 }
1930
1931 egl_surface_t const * const s = get_surface(surface);
1932 if (s->cnx->egl.eglSetDamageRegionKHR) {
1933 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1934 rects, n_rects);
1935 }
1936
1937 return EGL_FALSE;
1938}