blob: 3d3df766a8288fb7e74a35a1268cfdfb20d582ae [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall47743382013-02-08 11:13:46 -08004 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall47743382013-02-08 11:13:46 -080010 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Jesse Hallc07b5202013-07-04 12:08:16 -070019#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070020#include <ctype.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include <hardware/gralloc.h>
25#include <system/window.h>
26
27#include <EGL/egl.h>
28#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
30#include <cutils/log.h>
31#include <cutils/atomic.h>
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070033#include <cutils/properties.h>
34#include <cutils/memory.h>
35
Craig Donner68671532016-07-18 10:19:54 -070036#include <gui/ISurfaceComposer.h>
37
Craig Donner05249fc2016-01-15 19:33:55 -080038#include <ui/GraphicBuffer.h>
39
Mathias Agopian518ec112011-05-13 16:21:08 -070040#include <utils/KeyedVector.h>
41#include <utils/SortedVector.h>
42#include <utils/String8.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080043#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070044
Craig Donner68671532016-07-18 10:19:54 -070045#include "binder/Binder.h"
46#include "binder/Parcel.h"
47#include "binder/IServiceManager.h"
48
Mathias Agopian39c24a22013-04-04 23:17:56 -070049#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070050#include "../hooks.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070051
52#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070053#include "egl_object.h"
54#include "egl_tls.h"
Mathias Agopianada798b2012-02-13 17:09:30 -080055#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070056
57using namespace android;
58
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070059#define ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS 0
60
Mathias Agopian518ec112011-05-13 16:21:08 -070061// ----------------------------------------------------------------------------
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063namespace android {
64
Mathias Agopian518ec112011-05-13 16:21:08 -070065struct extention_map_t {
66 const char* name;
67 __eglMustCastToProperFunctionPointerType address;
68};
69
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070070/*
Jesse Hall21558da2013-08-06 15:31:22 -070071 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070072 *
Jesse Hall21558da2013-08-06 15:31:22 -070073 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
74 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070075 *
Jesse Hall21558da2013-08-06 15:31:22 -070076 * The rest (gExtensionString) depend on support in the EGL driver, and are
77 * only available if the driver supports them. However, some of these must be
78 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080079 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070080 * the mandatory extensions are present and may not function properly if some
81 * are missing.
82 *
83 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070084 */
Jesse Hall21558da2013-08-06 15:31:22 -070085extern char const * const gBuiltinExtensionString =
86 "EGL_KHR_get_all_proc_addresses "
87 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080088 "EGL_KHR_swap_buffers_with_damage "
Craig Donner05249fc2016-01-15 19:33:55 -080089 "EGL_ANDROID_create_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080090 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070091#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -070092 "EGL_ANDROID_get_frame_timestamps "
Pablo Ceballosb1e2c722016-07-14 08:02:14 -070093#endif
Jesse Hall21558da2013-08-06 15:31:22 -070094 ;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070095extern char const * const gExtensionString =
96 "EGL_KHR_image " // mandatory
97 "EGL_KHR_image_base " // mandatory
98 "EGL_KHR_image_pixmap "
99 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700100 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700101 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700102 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700103 "EGL_KHR_gl_texture_cubemap_image "
104 "EGL_KHR_gl_renderbuffer_image "
105 "EGL_KHR_reusable_sync "
106 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700107 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700108 "EGL_KHR_config_attribs "
109 "EGL_KHR_surfaceless_context "
110 "EGL_KHR_stream "
111 "EGL_KHR_stream_fifo "
112 "EGL_KHR_stream_producer_eglsurface "
113 "EGL_KHR_stream_consumer_gltexture "
114 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700115 "EGL_EXT_create_context_robustness "
116 "EGL_NV_system_time "
117 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700118 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700119 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800120 "EGL_KHR_partial_update " // strongly recommended
121 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700122 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700123 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700124 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700125 "EGL_EXT_protected_content "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700126 ;
127
128// extensions not exposed to applications but used by the ANDROID system
129// "EGL_ANDROID_blob_cache " // strongly recommended
130// "EGL_IMG_hibernate_process " // optional
131// "EGL_ANDROID_native_fence_sync " // strongly recommended
132// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700133// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700134
135/*
136 * EGL Extensions entry-points exposed to 3rd party applications
137 * (keep in sync with gExtensionString above)
138 *
139 */
140static const extention_map_t sExtensionMap[] = {
141 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700142 { "eglLockSurfaceKHR",
143 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
144 { "eglUnlockSurfaceKHR",
145 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700146
147 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700148 { "eglCreateImageKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
150 { "eglDestroyImageKHR",
151 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700152
153 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
154 { "eglCreateSyncKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
156 { "eglDestroySyncKHR",
157 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
158 { "eglClientWaitSyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
160 { "eglSignalSyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
162 { "eglGetSyncAttribKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
164
165 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800166 { "eglGetSystemTimeFrequencyNV",
167 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
168 { "eglGetSystemTimeNV",
169 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700170
Mathias Agopian2bb71682013-03-27 17:32:41 -0700171 // EGL_KHR_wait_sync
172 { "eglWaitSyncKHR",
173 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700174
175 // EGL_ANDROID_presentation_time
176 { "eglPresentationTimeANDROID",
177 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800178
179 // EGL_KHR_swap_buffers_with_damage
180 { "eglSwapBuffersWithDamageKHR",
181 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
182
Craig Donner05249fc2016-01-15 19:33:55 -0800183 // EGL_ANDROID_native_client_buffer
184 { "eglCreateNativeClientBufferANDROID",
185 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
186
Dan Stozaa894d082015-02-19 15:27:36 -0800187 // EGL_KHR_partial_update
188 { "eglSetDamageRegionKHR",
189 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700190
191 { "eglCreateStreamKHR",
192 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
193 { "eglDestroyStreamKHR",
194 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
195 { "eglStreamAttribKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
197 { "eglQueryStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
199 { "eglQueryStreamu64KHR",
200 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
201 { "eglQueryStreamTimeKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
203 { "eglCreateStreamProducerSurfaceKHR",
204 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
205 { "eglStreamConsumerGLTextureExternalKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
207 { "eglStreamConsumerAcquireKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
209 { "eglStreamConsumerReleaseKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
211 { "eglGetStreamFileDescriptorKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
213 { "eglCreateStreamFromFileDescriptorKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700215
216 // EGL_ANDROID_get_frame_timestamps
217 { "eglGetFrameTimestampsANDROID",
218 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
219 { "eglQueryTimestampSupportedANDROID",
220 (__eglMustCastToProperFunctionPointerType)&eglQueryTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700221};
222
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700223/*
224 * These extensions entry-points should not be exposed to applications.
225 * They're used internally by the Android EGL layer.
226 */
227#define FILTER_EXTENSIONS(procname) \
228 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
229 !strcmp((procname), "eglHibernateProcessIMG") || \
230 !strcmp((procname), "eglAwakenProcessIMG") || \
231 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
232
233
234
Mathias Agopian518ec112011-05-13 16:21:08 -0700235// accesses protected by sExtensionMapMutex
236static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
237static int sGLExtentionSlot = 0;
238static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
239
240static void(*findProcAddress(const char* name,
241 const extention_map_t* map, size_t n))() {
242 for (uint32_t i=0 ; i<n ; i++) {
243 if (!strcmp(name, map[i].name)) {
244 return map[i].address;
245 }
246 }
247 return NULL;
248}
249
250// ----------------------------------------------------------------------------
251
Mathias Agopian518ec112011-05-13 16:21:08 -0700252extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
253extern EGLBoolean egl_init_drivers();
254extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700255extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700256
Mathias Agopian518ec112011-05-13 16:21:08 -0700257} // namespace android;
258
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700259
Mathias Agopian518ec112011-05-13 16:21:08 -0700260// ----------------------------------------------------------------------------
261
262static inline void clearError() { egl_tls_t::clearError(); }
263static inline EGLContext getContext() { return egl_tls_t::getContext(); }
264
265// ----------------------------------------------------------------------------
266
267EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
268{
269 clearError();
270
Dan Stozac3289c42014-01-17 11:38:34 -0800271 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700272 if (index >= NUM_DISPLAYS) {
273 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
274 }
275
276 if (egl_init_drivers() == EGL_FALSE) {
277 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
278 }
279
280 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
281 return dpy;
282}
283
284// ----------------------------------------------------------------------------
285// Initialization
286// ----------------------------------------------------------------------------
287
288EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
289{
290 clearError();
291
Jesse Hallb29e5e82012-04-04 16:53:42 -0700292 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700293 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
294
295 EGLBoolean res = dp->initialize(major, minor);
296
297 return res;
298}
299
300EGLBoolean eglTerminate(EGLDisplay dpy)
301{
302 // NOTE: don't unload the drivers b/c some APIs can be called
303 // after eglTerminate() has been called. eglTerminate() only
304 // terminates an EGLDisplay, not a EGL itself.
305
306 clearError();
307
Jesse Hallb29e5e82012-04-04 16:53:42 -0700308 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700309 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
310
311 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800312
Mathias Agopian518ec112011-05-13 16:21:08 -0700313 return res;
314}
315
316// ----------------------------------------------------------------------------
317// configuration
318// ----------------------------------------------------------------------------
319
320EGLBoolean eglGetConfigs( EGLDisplay dpy,
321 EGLConfig *configs,
322 EGLint config_size, EGLint *num_config)
323{
324 clearError();
325
Jesse Hallb29e5e82012-04-04 16:53:42 -0700326 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700327 if (!dp) return EGL_FALSE;
328
Mathias Agopian7773c432012-02-13 20:06:08 -0800329 if (num_config==0) {
330 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700331 }
332
Mathias Agopian7773c432012-02-13 20:06:08 -0800333 EGLBoolean res = EGL_FALSE;
334 *num_config = 0;
335
336 egl_connection_t* const cnx = &gEGLImpl;
337 if (cnx->dso) {
338 res = cnx->egl.eglGetConfigs(
339 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700340 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800341
342 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700343}
344
345EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
346 EGLConfig *configs, EGLint config_size,
347 EGLint *num_config)
348{
349 clearError();
350
Jesse Hallb29e5e82012-04-04 16:53:42 -0700351 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700352 if (!dp) return EGL_FALSE;
353
354 if (num_config==0) {
355 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
356 }
357
Mathias Agopian518ec112011-05-13 16:21:08 -0700358 EGLBoolean res = EGL_FALSE;
359 *num_config = 0;
360
Mathias Agopianada798b2012-02-13 17:09:30 -0800361 egl_connection_t* const cnx = &gEGLImpl;
362 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700363 if (attrib_list) {
364 char value[PROPERTY_VALUE_MAX];
365 property_get("debug.egl.force_msaa", value, "false");
366
367 if (!strcmp(value, "true")) {
368 size_t attribCount = 0;
369 EGLint attrib = attrib_list[0];
370
371 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700372 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700373 const EGLint *attribRendererable = NULL;
374 const EGLint *attribCaveat = NULL;
375
376 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700377 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700378 while (attrib != EGL_NONE) {
379 attrib = attrib_list[attribCount];
380 switch (attrib) {
381 case EGL_RENDERABLE_TYPE:
382 attribRendererable = &attrib_list[attribCount];
383 break;
384 case EGL_CONFIG_CAVEAT:
385 attribCaveat = &attrib_list[attribCount];
386 break;
387 }
388 attribCount++;
389 }
390
391 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
392 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800393
Romain Guy1cffc802012-10-15 18:13:05 -0700394 // Insert 2 extra attributes to force-enable MSAA 4x
395 EGLint aaAttribs[attribCount + 4];
396 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
397 aaAttribs[1] = 1;
398 aaAttribs[2] = EGL_SAMPLES;
399 aaAttribs[3] = 4;
400
401 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
402
403 EGLint numConfigAA;
404 EGLBoolean resAA = cnx->egl.eglChooseConfig(
405 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
406
407 if (resAA == EGL_TRUE && numConfigAA > 0) {
408 ALOGD("Enabling MSAA 4x");
409 *num_config = numConfigAA;
410 return resAA;
411 }
412 }
413 }
414 }
415
Mathias Agopian7773c432012-02-13 20:06:08 -0800416 res = cnx->egl.eglChooseConfig(
417 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700418 }
419 return res;
420}
421
422EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
423 EGLint attribute, EGLint *value)
424{
425 clearError();
426
Jesse Hallb29e5e82012-04-04 16:53:42 -0700427 egl_connection_t* cnx = NULL;
428 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
429 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800430
Mathias Agopian518ec112011-05-13 16:21:08 -0700431 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800432 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700433}
434
435// ----------------------------------------------------------------------------
436// surfaces
437// ----------------------------------------------------------------------------
438
Jesse Hallc2e41222013-08-08 13:40:22 -0700439// Turn linear formats into corresponding sRGB formats when colorspace is
440// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
441// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800442// the modification isn't possible, the original dataSpace is returned.
443static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
444 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700445 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800446 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700447 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800448 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700449 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800450 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700451}
452
Mathias Agopian518ec112011-05-13 16:21:08 -0700453EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
454 NativeWindowType window,
455 const EGLint *attrib_list)
456{
457 clearError();
458
Jesse Hallb29e5e82012-04-04 16:53:42 -0700459 egl_connection_t* cnx = NULL;
460 egl_display_ptr dp = validate_display_connection(dpy, cnx);
461 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800462 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700463
Andy McFaddend566ce32014-01-07 15:54:17 -0800464 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
465 if (result != OK) {
466 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
467 "failed (%#x) (already connected to another API?)",
468 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700469 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700470 }
471
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700472 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700473 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
474 // of our native format. So if sRGB gamma is requested, we have to
475 // modify the EGLconfig's format before setting the native window's
476 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800477
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700478 // by default, just pick RGBA_8888
479 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800480 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700481
482 EGLint a = 0;
483 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
484 if (a > 0) {
485 // alpha-channel requested, there's really only one suitable format
486 format = HAL_PIXEL_FORMAT_RGBA_8888;
487 } else {
488 EGLint r, g, b;
489 r = g = b = 0;
490 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
491 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
492 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
493 EGLint colorDepth = r + g + b;
494 if (colorDepth <= 16) {
495 format = HAL_PIXEL_FORMAT_RGB_565;
496 } else {
497 format = HAL_PIXEL_FORMAT_RGBX_8888;
498 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700499 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700500
501 // now select a corresponding sRGB format if needed
502 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
503 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
504 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530505 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700506 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700507 }
508 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800509
Jesse Hallc2e41222013-08-08 13:40:22 -0700510 if (format != 0) {
511 int err = native_window_set_buffers_format(window, format);
512 if (err != 0) {
513 ALOGE("error setting native window pixel format: %s (%d)",
514 strerror(-err), err);
515 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
516 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
517 }
518 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700519
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800520 if (dataSpace != 0) {
521 int err = native_window_set_buffers_data_space(window, dataSpace);
522 if (err != 0) {
523 ALOGE("error setting native window pixel dataSpace: %s (%d)",
524 strerror(-err), err);
525 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
526 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
527 }
528 }
529
Jamie Gennis59769462011-11-19 18:04:43 -0800530 // the EGL spec requires that a new EGLSurface default to swap interval
531 // 1, so explicitly set that on the window here.
532 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
533 anw->setSwapInterval(anw, 1);
534
Mathias Agopian518ec112011-05-13 16:21:08 -0700535 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800536 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700537 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700538 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
539 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700540 return s;
541 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700542
543 // EGLSurface creation failed
544 native_window_set_buffers_format(window, 0);
545 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700546 }
547 return EGL_NO_SURFACE;
548}
549
550EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
551 NativePixmapType pixmap,
552 const EGLint *attrib_list)
553{
554 clearError();
555
Jesse Hallb29e5e82012-04-04 16:53:42 -0700556 egl_connection_t* cnx = NULL;
557 egl_display_ptr dp = validate_display_connection(dpy, cnx);
558 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700559 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800560 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700561 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700562 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
563 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700564 return s;
565 }
566 }
567 return EGL_NO_SURFACE;
568}
569
570EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
571 const EGLint *attrib_list)
572{
573 clearError();
574
Jesse Hallb29e5e82012-04-04 16:53:42 -0700575 egl_connection_t* cnx = NULL;
576 egl_display_ptr dp = validate_display_connection(dpy, cnx);
577 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700578 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800579 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700580 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700581 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
582 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700583 return s;
584 }
585 }
586 return EGL_NO_SURFACE;
587}
Jesse Hall47743382013-02-08 11:13:46 -0800588
Mathias Agopian518ec112011-05-13 16:21:08 -0700589EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
590{
591 clearError();
592
Jesse Hallb29e5e82012-04-04 16:53:42 -0700593 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700594 if (!dp) return EGL_FALSE;
595
Jesse Hallb29e5e82012-04-04 16:53:42 -0700596 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700597 if (!_s.get())
598 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700599
600 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800601 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700602 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700603 _s.terminate();
604 }
605 return result;
606}
607
608EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
609 EGLint attribute, EGLint *value)
610{
611 clearError();
612
Jesse Hallb29e5e82012-04-04 16:53:42 -0700613 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700614 if (!dp) return EGL_FALSE;
615
Jesse Hallb29e5e82012-04-04 16:53:42 -0700616 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700617 if (!_s.get())
618 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700619
Mathias Agopian518ec112011-05-13 16:21:08 -0700620 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800621 return s->cnx->egl.eglQuerySurface(
622 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700623}
624
Jamie Gennise8696a42012-01-15 18:54:57 -0800625void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800626 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800627 clearError();
628
Jesse Hallb29e5e82012-04-04 16:53:42 -0700629 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800630 if (!dp) {
631 return;
632 }
633
Jesse Hallb29e5e82012-04-04 16:53:42 -0700634 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800635 if (!_s.get()) {
636 setError(EGL_BAD_SURFACE, EGL_FALSE);
637 return;
638 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800639}
640
Mathias Agopian518ec112011-05-13 16:21:08 -0700641// ----------------------------------------------------------------------------
642// Contexts
643// ----------------------------------------------------------------------------
644
645EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
646 EGLContext share_list, const EGLint *attrib_list)
647{
648 clearError();
649
Jesse Hallb29e5e82012-04-04 16:53:42 -0700650 egl_connection_t* cnx = NULL;
651 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700652 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700653 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700654 if (!ContextRef(dp.get(), share_list).get()) {
655 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
656 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700657 egl_context_t* const c = get_context(share_list);
658 share_list = c->context;
659 }
660 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800661 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700662 if (context != EGL_NO_CONTEXT) {
663 // figure out if it's a GLESv1 or GLESv2
664 int version = 0;
665 if (attrib_list) {
666 while (*attrib_list != EGL_NONE) {
667 GLint attr = *attrib_list++;
668 GLint value = *attrib_list++;
669 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
670 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800671 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800672 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800673 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700674 }
675 }
676 };
677 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700678 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
679 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700680 return c;
681 }
682 }
683 return EGL_NO_CONTEXT;
684}
685
686EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
687{
688 clearError();
689
Jesse Hallb29e5e82012-04-04 16:53:42 -0700690 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700691 if (!dp)
692 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700693
Jesse Hallb29e5e82012-04-04 16:53:42 -0700694 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700695 if (!_c.get())
696 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800697
Mathias Agopian518ec112011-05-13 16:21:08 -0700698 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800699 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700700 if (result == EGL_TRUE) {
701 _c.terminate();
702 }
703 return result;
704}
705
Mathias Agopian518ec112011-05-13 16:21:08 -0700706EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
707 EGLSurface read, EGLContext ctx)
708{
709 clearError();
710
Jesse Hallb29e5e82012-04-04 16:53:42 -0700711 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700712 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
713
Mathias Agopian5b287a62011-05-16 18:58:55 -0700714 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
715 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
716 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700717 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
718 (draw != EGL_NO_SURFACE) ) {
719 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
720 }
721
722 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700723 ContextRef _c(dp.get(), ctx);
724 SurfaceRef _d(dp.get(), draw);
725 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700726
727 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700728 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700729 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700730 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700731 }
732
733 // these are the underlying implementation's object
734 EGLContext impl_ctx = EGL_NO_CONTEXT;
735 EGLSurface impl_draw = EGL_NO_SURFACE;
736 EGLSurface impl_read = EGL_NO_SURFACE;
737
738 // these are our objects structs passed in
739 egl_context_t * c = NULL;
740 egl_surface_t const * d = NULL;
741 egl_surface_t const * r = NULL;
742
743 // these are the current objects structs
744 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800745
Mathias Agopian518ec112011-05-13 16:21:08 -0700746 if (ctx != EGL_NO_CONTEXT) {
747 c = get_context(ctx);
748 impl_ctx = c->context;
749 } else {
750 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700751 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
752 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
753 return setError(EGL_BAD_MATCH, EGL_FALSE);
754 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700755 if (cur_c == NULL) {
756 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700757 // not an error, there is just no current context.
758 return EGL_TRUE;
759 }
760 }
761
762 // retrieve the underlying implementation's draw EGLSurface
763 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700764 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700765 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700766 impl_draw = d->surface;
767 }
768
769 // retrieve the underlying implementation's read EGLSurface
770 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700771 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700772 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700773 impl_read = r->surface;
774 }
775
Mathias Agopian518ec112011-05-13 16:21:08 -0700776
Jesse Hallb29e5e82012-04-04 16:53:42 -0700777 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800778 draw, read, ctx,
779 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700780
781 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800782 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700783 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
784 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700785 _c.acquire();
786 _r.acquire();
787 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700788 } else {
789 setGLHooksThreadSpecific(&gHooksNoContext);
790 egl_tls_t::setContext(EGL_NO_CONTEXT);
791 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700792 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000793 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700794 egl_connection_t* const cnx = &gEGLImpl;
795 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700796 }
797 return result;
798}
799
800
801EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
802 EGLint attribute, EGLint *value)
803{
804 clearError();
805
Jesse Hallb29e5e82012-04-04 16:53:42 -0700806 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700807 if (!dp) return EGL_FALSE;
808
Jesse Hallb29e5e82012-04-04 16:53:42 -0700809 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700810 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
811
Mathias Agopian518ec112011-05-13 16:21:08 -0700812 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800813 return c->cnx->egl.eglQueryContext(
814 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700815
Mathias Agopian518ec112011-05-13 16:21:08 -0700816}
817
818EGLContext eglGetCurrentContext(void)
819{
820 // could be called before eglInitialize(), but we wouldn't have a context
821 // then, and this function would correctly return EGL_NO_CONTEXT.
822
823 clearError();
824
825 EGLContext ctx = getContext();
826 return ctx;
827}
828
829EGLSurface eglGetCurrentSurface(EGLint readdraw)
830{
831 // could be called before eglInitialize(), but we wouldn't have a context
832 // then, and this function would correctly return EGL_NO_SURFACE.
833
834 clearError();
835
836 EGLContext ctx = getContext();
837 if (ctx) {
838 egl_context_t const * const c = get_context(ctx);
839 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
840 switch (readdraw) {
841 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800842 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700843 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
844 }
845 }
846 return EGL_NO_SURFACE;
847}
848
849EGLDisplay eglGetCurrentDisplay(void)
850{
851 // could be called before eglInitialize(), but we wouldn't have a context
852 // then, and this function would correctly return EGL_NO_DISPLAY.
853
854 clearError();
855
856 EGLContext ctx = getContext();
857 if (ctx) {
858 egl_context_t const * const c = get_context(ctx);
859 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
860 return c->dpy;
861 }
862 return EGL_NO_DISPLAY;
863}
864
865EGLBoolean eglWaitGL(void)
866{
Mathias Agopian518ec112011-05-13 16:21:08 -0700867 clearError();
868
Mathias Agopianada798b2012-02-13 17:09:30 -0800869 egl_connection_t* const cnx = &gEGLImpl;
870 if (!cnx->dso)
871 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
872
873 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700874}
875
876EGLBoolean eglWaitNative(EGLint engine)
877{
Mathias Agopian518ec112011-05-13 16:21:08 -0700878 clearError();
879
Mathias Agopianada798b2012-02-13 17:09:30 -0800880 egl_connection_t* const cnx = &gEGLImpl;
881 if (!cnx->dso)
882 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
883
884 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700885}
886
887EGLint eglGetError(void)
888{
Mathias Agopianada798b2012-02-13 17:09:30 -0800889 EGLint err = EGL_SUCCESS;
890 egl_connection_t* const cnx = &gEGLImpl;
891 if (cnx->dso) {
892 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700893 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800894 if (err == EGL_SUCCESS) {
895 err = egl_tls_t::getError();
896 }
897 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700898}
899
Michael Chockc0ec5e22014-01-27 08:14:33 -0800900static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700901 const char* procname) {
902 const egl_connection_t* cnx = &gEGLImpl;
903 void* proc = NULL;
904
Michael Chockc0ec5e22014-01-27 08:14:33 -0800905 proc = dlsym(cnx->libEgl, procname);
906 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
907
Jesse Hallc07b5202013-07-04 12:08:16 -0700908 proc = dlsym(cnx->libGles2, procname);
909 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
910
911 proc = dlsym(cnx->libGles1, procname);
912 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
913
914 return NULL;
915}
916
Mathias Agopian518ec112011-05-13 16:21:08 -0700917__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
918{
919 // eglGetProcAddress() could be the very first function called
920 // in which case we must make sure we've initialized ourselves, this
921 // happens the first time egl_get_display() is called.
922
923 clearError();
924
925 if (egl_init_drivers() == EGL_FALSE) {
926 setError(EGL_BAD_PARAMETER, NULL);
927 return NULL;
928 }
929
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700930 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700931 return NULL;
932 }
933
Mathias Agopian518ec112011-05-13 16:21:08 -0700934 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700935 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700936 if (addr) return addr;
937
Michael Chockc0ec5e22014-01-27 08:14:33 -0800938 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700939 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700940
Mathias Agopian518ec112011-05-13 16:21:08 -0700941 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
942 pthread_mutex_lock(&sExtensionMapMutex);
943
944 /*
945 * Since eglGetProcAddress() is not associated to anything, it needs
946 * to return a function pointer that "works" regardless of what
947 * the current context is.
948 *
949 * For this reason, we return a "forwarder", a small stub that takes
950 * care of calling the function associated with the context
951 * currently bound.
952 *
953 * We first look for extensions we've already resolved, if we're seeing
954 * this extension for the first time, we go through all our
955 * implementations and call eglGetProcAddress() and record the
956 * result in the appropriate implementation hooks and return the
957 * address of the forwarder corresponding to that hook set.
958 *
959 */
960
961 const String8 name(procname);
962 addr = sGLExtentionMap.valueFor(name);
963 const int slot = sGLExtentionSlot;
964
Steve Blocke6f43dd2012-01-06 19:20:56 +0000965 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700966 "no more slots for eglGetProcAddress(\"%s\")",
967 procname);
968
969 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
970 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800971
972 egl_connection_t* const cnx = &gEGLImpl;
973 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800974 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800975 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800976 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
977 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800978 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800979 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700980 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800981
Mathias Agopian518ec112011-05-13 16:21:08 -0700982 if (found) {
983 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700984 sGLExtentionMap.add(name, addr);
985 sGLExtentionSlot++;
986 }
987 }
988
989 pthread_mutex_unlock(&sExtensionMapMutex);
990 return addr;
991}
992
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700993class FrameCompletionThread : public Thread {
994public:
995
996 static void queueSync(EGLSyncKHR sync) {
997 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
998 static bool running = false;
999 if (!running) {
1000 thread->run("GPUFrameCompletion");
1001 running = true;
1002 }
1003 {
1004 Mutex::Autolock lock(thread->mMutex);
1005 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1006 thread->mFramesQueued).string());
1007 thread->mQueue.push_back(sync);
1008 thread->mCondition.signal();
1009 thread->mFramesQueued++;
1010 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1011 }
1012 }
1013
1014private:
1015 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1016
1017 virtual bool threadLoop() {
1018 EGLSyncKHR sync;
1019 uint32_t frameNum;
1020 {
1021 Mutex::Autolock lock(mMutex);
1022 while (mQueue.isEmpty()) {
1023 mCondition.wait(mMutex);
1024 }
1025 sync = mQueue[0];
1026 frameNum = mFramesCompleted;
1027 }
1028 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1029 {
1030 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1031 frameNum).string());
1032 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1033 if (result == EGL_FALSE) {
1034 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1035 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1036 ALOGE("FrameCompletion: timeout waiting for fence");
1037 }
1038 eglDestroySyncKHR(dpy, sync);
1039 }
1040 {
1041 Mutex::Autolock lock(mMutex);
1042 mQueue.removeAt(0);
1043 mFramesCompleted++;
1044 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1045 }
1046 return true;
1047 }
1048
1049 uint32_t mFramesQueued;
1050 uint32_t mFramesCompleted;
1051 Vector<EGLSyncKHR> mQueue;
1052 Condition mCondition;
1053 Mutex mMutex;
1054};
1055
Dan Stozaa894d082015-02-19 15:27:36 -08001056EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1057 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001058{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001059 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001060 clearError();
1061
Jesse Hallb29e5e82012-04-04 16:53:42 -07001062 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001063 if (!dp) return EGL_FALSE;
1064
Jesse Hallb29e5e82012-04-04 16:53:42 -07001065 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001066 if (!_s.get())
1067 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001068
Mathias Agopian518ec112011-05-13 16:21:08 -07001069 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001070
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001071 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1072 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1073 if (sync != EGL_NO_SYNC_KHR) {
1074 FrameCompletionThread::queueSync(sync);
1075 }
1076 }
1077
Mathias Agopian7db993a2012-03-25 00:49:46 -07001078 if (CC_UNLIKELY(dp->finishOnSwap)) {
1079 uint32_t pixel;
1080 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1081 if (c) {
1082 // glReadPixels() ensures that the frame is complete
1083 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1084 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1085 }
1086 }
1087
Dan Stozaa894d082015-02-19 15:27:36 -08001088 if (n_rects == 0) {
1089 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1090 }
1091
1092 Vector<android_native_rect_t> androidRects;
1093 for (int r = 0; r < n_rects; ++r) {
1094 int offset = r * 4;
1095 int x = rects[offset];
1096 int y = rects[offset + 1];
1097 int width = rects[offset + 2];
1098 int height = rects[offset + 3];
1099 android_native_rect_t androidRect;
1100 androidRect.left = x;
1101 androidRect.top = y + height;
1102 androidRect.right = x + width;
1103 androidRect.bottom = y;
1104 androidRects.push_back(androidRect);
1105 }
1106 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1107 androidRects.size());
1108
1109 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1110 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1111 rects, n_rects);
1112 } else {
1113 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1114 }
1115}
1116
1117EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1118{
1119 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001120}
1121
1122EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1123 NativePixmapType target)
1124{
1125 clearError();
1126
Jesse Hallb29e5e82012-04-04 16:53:42 -07001127 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001128 if (!dp) return EGL_FALSE;
1129
Jesse Hallb29e5e82012-04-04 16:53:42 -07001130 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001131 if (!_s.get())
1132 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001133
Mathias Agopian518ec112011-05-13 16:21:08 -07001134 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001135 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001136}
1137
1138const char* eglQueryString(EGLDisplay dpy, EGLint name)
1139{
1140 clearError();
1141
Chia-I Wue57d1352016-08-15 16:10:02 +08001142 // Generate an error quietly when client extensions (as defined by
1143 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1144 // validate_display to generate the error as validate_display would log
1145 // the error, which can be misleading.
1146 //
1147 // If we want to support EGL_EXT_client_extensions later, we can return
1148 // the client extension string here instead.
1149 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1150 return setErrorQuiet(EGL_BAD_DISPLAY, nullptr);
1151
Jesse Hallb29e5e82012-04-04 16:53:42 -07001152 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001153 if (!dp) return (const char *) NULL;
1154
1155 switch (name) {
1156 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001157 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001158 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001159 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001160 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001161 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001162 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001163 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001164 }
1165 return setError(EGL_BAD_PARAMETER, (const char *)0);
1166}
1167
Mathias Agopianca088332013-03-28 17:44:13 -07001168EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1169{
1170 clearError();
1171
1172 const egl_display_ptr dp = validate_display(dpy);
1173 if (!dp) return (const char *) NULL;
1174
1175 switch (name) {
1176 case EGL_VENDOR:
1177 return dp->disp.queryString.vendor;
1178 case EGL_VERSION:
1179 return dp->disp.queryString.version;
1180 case EGL_EXTENSIONS:
1181 return dp->disp.queryString.extensions;
1182 case EGL_CLIENT_APIS:
1183 return dp->disp.queryString.clientApi;
1184 }
1185 return setError(EGL_BAD_PARAMETER, (const char *)0);
1186}
Mathias Agopian518ec112011-05-13 16:21:08 -07001187
1188// ----------------------------------------------------------------------------
1189// EGL 1.1
1190// ----------------------------------------------------------------------------
1191
1192EGLBoolean eglSurfaceAttrib(
1193 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1194{
1195 clearError();
1196
Jesse Hallb29e5e82012-04-04 16:53:42 -07001197 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001198 if (!dp) return EGL_FALSE;
1199
Jesse Hallb29e5e82012-04-04 16:53:42 -07001200 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001201 if (!_s.get())
1202 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001203
Pablo Ceballosc18be292016-05-31 14:55:42 -07001204 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001205
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001206 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001207 if (!s->win.get()) {
1208 setError(EGL_BAD_SURFACE, EGL_FALSE);
1209 }
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001210 int err = native_window_set_auto_refresh(s->win.get(),
1211 value ? true : false);
1212 return (err == NO_ERROR) ? EGL_TRUE :
1213 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001214 }
1215
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001216#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001217 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Brian Anderson069b3652016-07-22 10:32:47 -07001218 if (!s->win.get()) {
1219 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1220 }
Brian Anderson069b3652016-07-22 10:32:47 -07001221 int err = native_window_enable_frame_timestamps(
1222 s->win.get(), value ? true : false);
1223 return (err == NO_ERROR) ? EGL_TRUE :
1224 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001225 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001226#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001227
Mathias Agopian518ec112011-05-13 16:21:08 -07001228 if (s->cnx->egl.eglSurfaceAttrib) {
1229 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001230 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001231 }
1232 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1233}
1234
1235EGLBoolean eglBindTexImage(
1236 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1237{
1238 clearError();
1239
Jesse Hallb29e5e82012-04-04 16:53:42 -07001240 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001241 if (!dp) return EGL_FALSE;
1242
Jesse Hallb29e5e82012-04-04 16:53:42 -07001243 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001244 if (!_s.get())
1245 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001246
Mathias Agopian518ec112011-05-13 16:21:08 -07001247 egl_surface_t const * const s = get_surface(surface);
1248 if (s->cnx->egl.eglBindTexImage) {
1249 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001250 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001251 }
1252 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1253}
1254
1255EGLBoolean eglReleaseTexImage(
1256 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1257{
1258 clearError();
1259
Jesse Hallb29e5e82012-04-04 16:53:42 -07001260 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001261 if (!dp) return EGL_FALSE;
1262
Jesse Hallb29e5e82012-04-04 16:53:42 -07001263 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001264 if (!_s.get())
1265 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001266
Mathias Agopian518ec112011-05-13 16:21:08 -07001267 egl_surface_t const * const s = get_surface(surface);
1268 if (s->cnx->egl.eglReleaseTexImage) {
1269 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001270 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001271 }
1272 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1273}
1274
1275EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1276{
1277 clearError();
1278
Jesse Hallb29e5e82012-04-04 16:53:42 -07001279 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001280 if (!dp) return EGL_FALSE;
1281
1282 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001283 egl_connection_t* const cnx = &gEGLImpl;
1284 if (cnx->dso && cnx->egl.eglSwapInterval) {
1285 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001286 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001287
Mathias Agopian518ec112011-05-13 16:21:08 -07001288 return res;
1289}
1290
1291
1292// ----------------------------------------------------------------------------
1293// EGL 1.2
1294// ----------------------------------------------------------------------------
1295
1296EGLBoolean eglWaitClient(void)
1297{
1298 clearError();
1299
Mathias Agopianada798b2012-02-13 17:09:30 -08001300 egl_connection_t* const cnx = &gEGLImpl;
1301 if (!cnx->dso)
1302 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1303
1304 EGLBoolean res;
1305 if (cnx->egl.eglWaitClient) {
1306 res = cnx->egl.eglWaitClient();
1307 } else {
1308 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001309 }
1310 return res;
1311}
1312
1313EGLBoolean eglBindAPI(EGLenum api)
1314{
1315 clearError();
1316
1317 if (egl_init_drivers() == EGL_FALSE) {
1318 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1319 }
1320
1321 // bind this API on all EGLs
1322 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001323 egl_connection_t* const cnx = &gEGLImpl;
1324 if (cnx->dso && cnx->egl.eglBindAPI) {
1325 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001326 }
1327 return res;
1328}
1329
1330EGLenum eglQueryAPI(void)
1331{
1332 clearError();
1333
1334 if (egl_init_drivers() == EGL_FALSE) {
1335 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1336 }
1337
Mathias Agopianada798b2012-02-13 17:09:30 -08001338 egl_connection_t* const cnx = &gEGLImpl;
1339 if (cnx->dso && cnx->egl.eglQueryAPI) {
1340 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001341 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001342
Mathias Agopian518ec112011-05-13 16:21:08 -07001343 // or, it can only be OpenGL ES
1344 return EGL_OPENGL_ES_API;
1345}
1346
1347EGLBoolean eglReleaseThread(void)
1348{
1349 clearError();
1350
Mathias Agopianada798b2012-02-13 17:09:30 -08001351 egl_connection_t* const cnx = &gEGLImpl;
1352 if (cnx->dso && cnx->egl.eglReleaseThread) {
1353 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001354 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301355
1356 // If there is context bound to the thread, release it
1357 egl_display_t::loseCurrent(get_context(getContext()));
1358
Mathias Agopian518ec112011-05-13 16:21:08 -07001359 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001360 return EGL_TRUE;
1361}
1362
1363EGLSurface eglCreatePbufferFromClientBuffer(
1364 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1365 EGLConfig config, const EGLint *attrib_list)
1366{
1367 clearError();
1368
Jesse Hallb29e5e82012-04-04 16:53:42 -07001369 egl_connection_t* cnx = NULL;
1370 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1371 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001372 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1373 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001374 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001375 }
1376 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1377}
1378
1379// ----------------------------------------------------------------------------
1380// EGL_EGLEXT_VERSION 3
1381// ----------------------------------------------------------------------------
1382
1383EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1384 const EGLint *attrib_list)
1385{
1386 clearError();
1387
Jesse Hallb29e5e82012-04-04 16:53:42 -07001388 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001389 if (!dp) return EGL_FALSE;
1390
Jesse Hallb29e5e82012-04-04 16:53:42 -07001391 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001392 if (!_s.get())
1393 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001394
1395 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001396 if (s->cnx->egl.eglLockSurfaceKHR) {
1397 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001398 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001399 }
1400 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1401}
1402
1403EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1404{
1405 clearError();
1406
Jesse Hallb29e5e82012-04-04 16:53:42 -07001407 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001408 if (!dp) return EGL_FALSE;
1409
Jesse Hallb29e5e82012-04-04 16:53:42 -07001410 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001411 if (!_s.get())
1412 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001413
1414 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001415 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001416 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001417 }
1418 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1419}
1420
1421EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1422 EGLClientBuffer buffer, const EGLint *attrib_list)
1423{
1424 clearError();
1425
Jesse Hallb29e5e82012-04-04 16:53:42 -07001426 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001427 if (!dp) return EGL_NO_IMAGE_KHR;
1428
Jesse Hallb29e5e82012-04-04 16:53:42 -07001429 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001430 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001431
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001432 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1433 egl_connection_t* const cnx = &gEGLImpl;
1434 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1435 result = cnx->egl.eglCreateImageKHR(
1436 dp->disp.dpy,
1437 c ? c->context : EGL_NO_CONTEXT,
1438 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001439 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001440 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001441}
1442
1443EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1444{
1445 clearError();
1446
Jesse Hallb29e5e82012-04-04 16:53:42 -07001447 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001448 if (!dp) return EGL_FALSE;
1449
Steven Holte646a5c52012-06-04 20:02:11 -07001450 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001451 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001452 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001453 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001454 }
Steven Holte646a5c52012-06-04 20:02:11 -07001455 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001456}
1457
1458// ----------------------------------------------------------------------------
1459// EGL_EGLEXT_VERSION 5
1460// ----------------------------------------------------------------------------
1461
1462
1463EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1464{
1465 clearError();
1466
Jesse Hallb29e5e82012-04-04 16:53:42 -07001467 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001468 if (!dp) return EGL_NO_SYNC_KHR;
1469
Mathias Agopian518ec112011-05-13 16:21:08 -07001470 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001471 egl_connection_t* const cnx = &gEGLImpl;
1472 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1473 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001474 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001475 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001476}
1477
1478EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1479{
1480 clearError();
1481
Jesse Hallb29e5e82012-04-04 16:53:42 -07001482 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001483 if (!dp) return EGL_FALSE;
1484
Mathias Agopian518ec112011-05-13 16:21:08 -07001485 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001486 egl_connection_t* const cnx = &gEGLImpl;
1487 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1488 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001489 }
1490 return result;
1491}
1492
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001493EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1494 clearError();
1495
1496 const egl_display_ptr dp = validate_display(dpy);
1497 if (!dp) return EGL_FALSE;
1498
1499 EGLBoolean result = EGL_FALSE;
1500 egl_connection_t* const cnx = &gEGLImpl;
1501 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1502 result = cnx->egl.eglSignalSyncKHR(
1503 dp->disp.dpy, sync, mode);
1504 }
1505 return result;
1506}
1507
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001508EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1509 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001510{
1511 clearError();
1512
Jesse Hallb29e5e82012-04-04 16:53:42 -07001513 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001514 if (!dp) return EGL_FALSE;
1515
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001516 EGLBoolean result = EGL_FALSE;
1517 egl_connection_t* const cnx = &gEGLImpl;
1518 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1519 result = cnx->egl.eglClientWaitSyncKHR(
1520 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001521 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001522 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001523}
1524
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001525EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1526 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001527{
1528 clearError();
1529
Jesse Hallb29e5e82012-04-04 16:53:42 -07001530 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001531 if (!dp) return EGL_FALSE;
1532
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001533 EGLBoolean result = EGL_FALSE;
1534 egl_connection_t* const cnx = &gEGLImpl;
1535 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1536 result = cnx->egl.eglGetSyncAttribKHR(
1537 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001538 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001539 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001540}
1541
Season Li000d88f2015-07-01 11:39:40 -07001542EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1543{
1544 clearError();
1545
1546 const egl_display_ptr dp = validate_display(dpy);
1547 if (!dp) return EGL_NO_STREAM_KHR;
1548
1549 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1550 egl_connection_t* const cnx = &gEGLImpl;
1551 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1552 result = cnx->egl.eglCreateStreamKHR(
1553 dp->disp.dpy, attrib_list);
1554 }
1555 return result;
1556}
1557
1558EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1559{
1560 clearError();
1561
1562 const egl_display_ptr dp = validate_display(dpy);
1563 if (!dp) return EGL_FALSE;
1564
1565 EGLBoolean result = EGL_FALSE;
1566 egl_connection_t* const cnx = &gEGLImpl;
1567 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1568 result = cnx->egl.eglDestroyStreamKHR(
1569 dp->disp.dpy, stream);
1570 }
1571 return result;
1572}
1573
1574EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1575 EGLenum attribute, EGLint value)
1576{
1577 clearError();
1578
1579 const egl_display_ptr dp = validate_display(dpy);
1580 if (!dp) return EGL_FALSE;
1581
1582 EGLBoolean result = EGL_FALSE;
1583 egl_connection_t* const cnx = &gEGLImpl;
1584 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1585 result = cnx->egl.eglStreamAttribKHR(
1586 dp->disp.dpy, stream, attribute, value);
1587 }
1588 return result;
1589}
1590
1591EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1592 EGLenum attribute, EGLint *value)
1593{
1594 clearError();
1595
1596 const egl_display_ptr dp = validate_display(dpy);
1597 if (!dp) return EGL_FALSE;
1598
1599 EGLBoolean result = EGL_FALSE;
1600 egl_connection_t* const cnx = &gEGLImpl;
1601 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1602 result = cnx->egl.eglQueryStreamKHR(
1603 dp->disp.dpy, stream, attribute, value);
1604 }
1605 return result;
1606}
1607
1608EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1609 EGLenum attribute, EGLuint64KHR *value)
1610{
1611 clearError();
1612
1613 const egl_display_ptr dp = validate_display(dpy);
1614 if (!dp) return EGL_FALSE;
1615
1616 EGLBoolean result = EGL_FALSE;
1617 egl_connection_t* const cnx = &gEGLImpl;
1618 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1619 result = cnx->egl.eglQueryStreamu64KHR(
1620 dp->disp.dpy, stream, attribute, value);
1621 }
1622 return result;
1623}
1624
1625EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1626 EGLenum attribute, EGLTimeKHR *value)
1627{
1628 clearError();
1629
1630 const egl_display_ptr dp = validate_display(dpy);
1631 if (!dp) return EGL_FALSE;
1632
1633 EGLBoolean result = EGL_FALSE;
1634 egl_connection_t* const cnx = &gEGLImpl;
1635 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1636 result = cnx->egl.eglQueryStreamTimeKHR(
1637 dp->disp.dpy, stream, attribute, value);
1638 }
1639 return result;
1640}
1641
1642EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1643 EGLStreamKHR stream, const EGLint *attrib_list)
1644{
1645 clearError();
1646
1647 egl_display_ptr dp = validate_display(dpy);
1648 if (!dp) return EGL_NO_SURFACE;
1649
1650 egl_connection_t* const cnx = &gEGLImpl;
1651 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1652 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1653 dp->disp.dpy, config, stream, attrib_list);
1654 if (surface != EGL_NO_SURFACE) {
1655 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1656 surface, cnx);
1657 return s;
1658 }
1659 }
1660 return EGL_NO_SURFACE;
1661}
1662
1663EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1664 EGLStreamKHR stream)
1665{
1666 clearError();
1667
1668 const egl_display_ptr dp = validate_display(dpy);
1669 if (!dp) return EGL_FALSE;
1670
1671 EGLBoolean result = EGL_FALSE;
1672 egl_connection_t* const cnx = &gEGLImpl;
1673 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1674 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1675 dp->disp.dpy, stream);
1676 }
1677 return result;
1678}
1679
1680EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1681 EGLStreamKHR stream)
1682{
1683 clearError();
1684
1685 const egl_display_ptr dp = validate_display(dpy);
1686 if (!dp) return EGL_FALSE;
1687
1688 EGLBoolean result = EGL_FALSE;
1689 egl_connection_t* const cnx = &gEGLImpl;
1690 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1691 result = cnx->egl.eglStreamConsumerAcquireKHR(
1692 dp->disp.dpy, stream);
1693 }
1694 return result;
1695}
1696
1697EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1698 EGLStreamKHR stream)
1699{
1700 clearError();
1701
1702 const egl_display_ptr dp = validate_display(dpy);
1703 if (!dp) return EGL_FALSE;
1704
1705 EGLBoolean result = EGL_FALSE;
1706 egl_connection_t* const cnx = &gEGLImpl;
1707 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1708 result = cnx->egl.eglStreamConsumerReleaseKHR(
1709 dp->disp.dpy, stream);
1710 }
1711 return result;
1712}
1713
1714EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1715 EGLDisplay dpy, EGLStreamKHR stream)
1716{
1717 clearError();
1718
1719 const egl_display_ptr dp = validate_display(dpy);
1720 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1721
1722 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1723 egl_connection_t* const cnx = &gEGLImpl;
1724 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1725 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1726 dp->disp.dpy, stream);
1727 }
1728 return result;
1729}
1730
1731EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1732 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1733{
1734 clearError();
1735
1736 const egl_display_ptr dp = validate_display(dpy);
1737 if (!dp) return EGL_NO_STREAM_KHR;
1738
1739 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1740 egl_connection_t* const cnx = &gEGLImpl;
1741 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1742 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1743 dp->disp.dpy, file_descriptor);
1744 }
1745 return result;
1746}
1747
Mathias Agopian518ec112011-05-13 16:21:08 -07001748// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001749// EGL_EGLEXT_VERSION 15
1750// ----------------------------------------------------------------------------
1751
1752EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1753 clearError();
1754 const egl_display_ptr dp = validate_display(dpy);
1755 if (!dp) return EGL_FALSE;
1756 EGLint result = EGL_FALSE;
1757 egl_connection_t* const cnx = &gEGLImpl;
1758 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1759 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1760 }
1761 return result;
1762}
1763
1764// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001765// ANDROID extensions
1766// ----------------------------------------------------------------------------
1767
Jamie Gennis331841b2012-09-06 14:52:00 -07001768EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1769{
1770 clearError();
1771
1772 const egl_display_ptr dp = validate_display(dpy);
1773 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1774
1775 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1776 egl_connection_t* const cnx = &gEGLImpl;
1777 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1778 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1779 }
1780 return result;
1781}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001782
Andy McFadden72841452013-03-01 16:25:32 -08001783EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1784 EGLnsecsANDROID time)
1785{
1786 clearError();
1787
1788 const egl_display_ptr dp = validate_display(dpy);
1789 if (!dp) {
1790 return EGL_FALSE;
1791 }
1792
1793 SurfaceRef _s(dp.get(), surface);
1794 if (!_s.get()) {
1795 setError(EGL_BAD_SURFACE, EGL_FALSE);
1796 return EGL_FALSE;
1797 }
1798
1799 egl_surface_t const * const s = get_surface(surface);
1800 native_window_set_buffers_timestamp(s->win.get(), time);
1801
1802 return EGL_TRUE;
1803}
1804
Craig Donner05249fc2016-01-15 19:33:55 -08001805EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1806{
1807 clearError();
1808
1809 int usage = 0;
1810 uint32_t width = 0;
1811 uint32_t height = 0;
1812 uint32_t format = 0;
Craig Donner6ebc46a2016-10-21 15:23:44 -07001813 uint32_t layer_count = 1;
Craig Donner05249fc2016-01-15 19:33:55 -08001814 uint32_t red_size = 0;
1815 uint32_t green_size = 0;
1816 uint32_t blue_size = 0;
1817 uint32_t alpha_size = 0;
1818
Craig Donnerb40504a2016-06-03 17:54:25 -07001819#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001820 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001821 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001822 target = value; \
1823 } else { \
1824 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1825 } \
1826 break
1827
1828 if (attrib_list) {
1829 while (*attrib_list != EGL_NONE) {
1830 GLint attr = *attrib_list++;
1831 GLint value = *attrib_list++;
1832 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001833 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1834 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1835 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1836 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1837 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1838 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner6ebc46a2016-10-21 15:23:44 -07001839 GET_NONNEGATIVE_VALUE(EGL_LAYER_COUNT_ANDROID, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001840 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1841 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1842 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001843 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001844 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001845 usage |= GRALLOC_USAGE_HW_RENDER;
1846 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001847 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001848 usage |= GRALLOC_USAGE_HW_TEXTURE;
1849 }
1850 // The buffer must be used for either a texture or a
1851 // renderbuffer.
Craig Donner8cfae6d2016-04-12 16:54:03 -07001852 if ((value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) &&
1853 (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID)) {
Craig Donner05249fc2016-01-15 19:33:55 -08001854 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1855 }
1856 break;
1857 default:
1858 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1859 }
1860 }
1861 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001862#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001863
1864 // Validate format.
1865 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1866 if (alpha_size == 8) {
1867 format = HAL_PIXEL_FORMAT_RGBA_8888;
1868 } else {
1869 format = HAL_PIXEL_FORMAT_RGB_888;
1870 }
1871 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1872 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001873 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001874 } else {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001875 ALOGE("Invalid native pixel format { r=%u, g=%u, b=%u, a=%u }",
Craig Donner05249fc2016-01-15 19:33:55 -08001876 red_size, green_size, blue_size, alpha_size);
1877 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1878 }
1879
Craig Donner68671532016-07-18 10:19:54 -07001880#define CHECK_ERROR_CONDITION(message) \
1881 if (err != NO_ERROR) { \
1882 ALOGE(message); \
1883 goto error_condition; \
1884 }
1885
1886 // The holder is used to destroy the buffer if an error occurs.
1887 GraphicBuffer* gBuffer = new GraphicBuffer();
1888 sp<IServiceManager> sm = defaultServiceManager();
1889 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1890 sp<IBinder> allocator;
1891 Parcel sc_data, sc_reply, data, reply;
1892 status_t err = NO_ERROR;
1893 if (sm == NULL) {
1894 ALOGE("Unable to connect to ServiceManager");
1895 goto error_condition;
1896 }
1897
1898 // Obtain an allocator.
1899 if (surfaceFlinger == NULL) {
1900 ALOGE("Unable to connect to SurfaceFlinger");
1901 goto error_condition;
1902 }
1903 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1904 err = surfaceFlinger->transact(
1905 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1906 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1907 allocator = sc_reply.readStrongBinder();
1908
1909 if (allocator == NULL) {
1910 ALOGE("Unable to obtain an ISurfaceComposer");
1911 goto error_condition;
1912 }
1913 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1914 err = data.writeUint32(width);
1915 CHECK_ERROR_CONDITION("Unable to write width");
1916 err = data.writeUint32(height);
1917 CHECK_ERROR_CONDITION("Unable to write height");
1918 err = data.writeInt32(static_cast<int32_t>(format));
1919 CHECK_ERROR_CONDITION("Unable to write format");
Craig Donner6ebc46a2016-10-21 15:23:44 -07001920 err = data.writeUint32(layer_count);
1921 CHECK_ERROR_CONDITION("Unable to write layer count");
1922 err = data.writeUint32(usage);
Craig Donner68671532016-07-18 10:19:54 -07001923 CHECK_ERROR_CONDITION("Unable to write usage");
Dan Stoza024e9312016-08-24 12:17:29 -07001924 err = data.writeUtf8AsUtf16(
1925 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1926 std::to_string(getpid()) + ']');
1927 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001928 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1929 &reply);
1930 CHECK_ERROR_CONDITION(
1931 "Unable to request buffer allocation from surface composer");
1932 err = reply.readInt32();
1933 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1934 err = reply.read(*gBuffer);
1935 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1936
1937 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001938 if (err != NO_ERROR) {
Craig Donner6ebc46a2016-10-21 15:23:44 -07001939 ALOGE("Unable to create native buffer "
1940 "{ w=%u, h=%u, f=%u, u=%#x, lc=%u}: %#x", width, height, format,
1941 usage, layer_count, err);
Craig Donner68671532016-07-18 10:19:54 -07001942 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001943 }
Craig Donner6ebc46a2016-10-21 15:23:44 -07001944 ALOGV("Created new native buffer %p { w=%u, h=%u, f=%u, u=%#x, lc=%u}",
1945 gBuffer, width, height, format, usage, layer_count);
Craig Donner05249fc2016-01-15 19:33:55 -08001946 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001947
1948#undef CHECK_ERROR_CONDITION
1949
1950error_condition:
1951 // Delete the buffer.
1952 sp<GraphicBuffer> holder(gBuffer);
1953 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001954}
1955
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001956// ----------------------------------------------------------------------------
1957// NVIDIA extensions
1958// ----------------------------------------------------------------------------
1959EGLuint64NV eglGetSystemTimeFrequencyNV()
1960{
1961 clearError();
1962
1963 if (egl_init_drivers() == EGL_FALSE) {
1964 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1965 }
1966
1967 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001968 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001969
Mathias Agopianada798b2012-02-13 17:09:30 -08001970 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1971 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001972 }
1973
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001974 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001975}
1976
1977EGLuint64NV eglGetSystemTimeNV()
1978{
1979 clearError();
1980
1981 if (egl_init_drivers() == EGL_FALSE) {
1982 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1983 }
1984
1985 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001986 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001987
Mathias Agopianada798b2012-02-13 17:09:30 -08001988 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1989 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001990 }
1991
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001992 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001993}
Dan Stozaa894d082015-02-19 15:27:36 -08001994
1995// ----------------------------------------------------------------------------
1996// Partial update extension
1997// ----------------------------------------------------------------------------
1998EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1999 EGLint *rects, EGLint n_rects)
2000{
2001 clearError();
2002
2003 const egl_display_ptr dp = validate_display(dpy);
2004 if (!dp) {
2005 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2006 return EGL_FALSE;
2007 }
2008
2009 SurfaceRef _s(dp.get(), surface);
2010 if (!_s.get()) {
2011 setError(EGL_BAD_SURFACE, EGL_FALSE);
2012 return EGL_FALSE;
2013 }
2014
2015 egl_surface_t const * const s = get_surface(surface);
2016 if (s->cnx->egl.eglSetDamageRegionKHR) {
2017 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2018 rects, n_rects);
2019 }
2020
2021 return EGL_FALSE;
2022}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002023
2024EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
2025 EGLint framesAgo, EGLint numTimestamps, const EGLint *timestamps,
2026 EGLnsecsANDROID *values)
2027{
2028 clearError();
2029
2030 const egl_display_ptr dp = validate_display(dpy);
2031 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002032 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002033 }
2034
2035 SurfaceRef _s(dp.get(), surface);
2036 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002037 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002038 }
2039
2040 egl_surface_t const * const s = get_surface(surface);
2041
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002042 if (!s->win.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002043 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002044 }
2045
Brian Andersondbd0ea82016-07-22 09:38:59 -07002046 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002047 nsecs_t* acquireTime = nullptr;
2048 nsecs_t* refreshStartTime = nullptr;
2049 nsecs_t* GLCompositionDoneTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002050 nsecs_t* displayPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002051 nsecs_t* displayRetireTime = nullptr;
2052 nsecs_t* releaseTime = nullptr;
2053
2054 for (int i = 0; i < numTimestamps; i++) {
2055 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002056 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2057 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002058 break;
2059 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2060 acquireTime = &values[i];
2061 break;
2062 case EGL_COMPOSITION_START_TIME_ANDROID:
2063 refreshStartTime = &values[i];
2064 break;
2065 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
2066 GLCompositionDoneTime = &values[i];
2067 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002068 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2069 displayPresentTime = &values[i];
2070 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002071 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2072 displayRetireTime = &values[i];
2073 break;
2074 case EGL_READS_DONE_TIME_ANDROID:
2075 releaseTime = &values[i];
2076 break;
2077 default:
Brian Anderson069b3652016-07-22 10:32:47 -07002078 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002079 }
2080 }
2081
2082 status_t ret = native_window_get_frame_timestamps(s->win.get(), framesAgo,
Brian Andersondbd0ea82016-07-22 09:38:59 -07002083 requestedPresentTime, acquireTime, refreshStartTime,
Brian Anderson069b3652016-07-22 10:32:47 -07002084 GLCompositionDoneTime, displayPresentTime, displayRetireTime,
2085 releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002086
Brian Anderson069b3652016-07-22 10:32:47 -07002087 switch (ret) {
2088 case NO_ERROR:
2089 return EGL_TRUE;
2090 case NAME_NOT_FOUND:
2091 return setError(EGL_BAD_ACCESS, EGL_FALSE);
Brian Anderson7c3ba8a2016-07-25 12:48:08 -07002092 case INVALID_OPERATION:
2093 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002094 case BAD_VALUE:
2095 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
2096 default:
2097 // This should not happen. Return an error that is not in the spec
2098 // so it's obvious something is very wrong.
2099 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002100 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002101}
2102
2103EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2104 EGLint timestamp)
2105{
2106 clearError();
2107
2108 const egl_display_ptr dp = validate_display(dpy);
2109 if (!dp) {
Brian Anderson069b3652016-07-22 10:32:47 -07002110 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002111 }
2112
2113 SurfaceRef _s(dp.get(), surface);
2114 if (!_s.get()) {
Brian Anderson069b3652016-07-22 10:32:47 -07002115 return setError(EGL_BAD_SURFACE, EGL_FALSE);
2116 }
2117
2118 egl_surface_t const * const s = get_surface(surface);
2119
2120 ANativeWindow* window = s->win.get();
2121 if (!window) {
2122 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002123 }
2124
2125 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002126#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Brian Andersondbd0ea82016-07-22 09:38:59 -07002127 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002128 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2129 case EGL_COMPOSITION_START_TIME_ANDROID:
2130 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002131 case EGL_READS_DONE_TIME_ANDROID:
2132 return EGL_TRUE;
Brian Anderson069b3652016-07-22 10:32:47 -07002133 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2134 int value = 0;
2135 window->query(window,
2136 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2137 return value == 0 ? EGL_FALSE : EGL_TRUE;
2138 }
2139 case EGL_DISPLAY_RETIRE_TIME_ANDROID: {
2140 int value = 0;
2141 window->query(window,
2142 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_RETIRE, &value);
2143 return value == 0 ? EGL_FALSE : EGL_TRUE;
2144 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002145#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002146 default:
2147 return EGL_FALSE;
2148 }
2149}