blob: 58758602e20b576ed654b897a895435d6ec32aa4 [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 "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000126 "EGL_IMG_context_priority "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700127 ;
128
129// extensions not exposed to applications but used by the ANDROID system
130// "EGL_ANDROID_blob_cache " // strongly recommended
131// "EGL_IMG_hibernate_process " // optional
132// "EGL_ANDROID_native_fence_sync " // strongly recommended
133// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700134// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700135
136/*
137 * EGL Extensions entry-points exposed to 3rd party applications
138 * (keep in sync with gExtensionString above)
139 *
140 */
141static const extention_map_t sExtensionMap[] = {
142 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700143 { "eglLockSurfaceKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
145 { "eglUnlockSurfaceKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700147
148 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700149 { "eglCreateImageKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
151 { "eglDestroyImageKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700153
154 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
155 { "eglCreateSyncKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
157 { "eglDestroySyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
159 { "eglClientWaitSyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
161 { "eglSignalSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
163 { "eglGetSyncAttribKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
165
166 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800167 { "eglGetSystemTimeFrequencyNV",
168 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
169 { "eglGetSystemTimeNV",
170 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700171
Mathias Agopian2bb71682013-03-27 17:32:41 -0700172 // EGL_KHR_wait_sync
173 { "eglWaitSyncKHR",
174 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700175
176 // EGL_ANDROID_presentation_time
177 { "eglPresentationTimeANDROID",
178 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800179
180 // EGL_KHR_swap_buffers_with_damage
181 { "eglSwapBuffersWithDamageKHR",
182 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
183
Craig Donner05249fc2016-01-15 19:33:55 -0800184 // EGL_ANDROID_native_client_buffer
185 { "eglCreateNativeClientBufferANDROID",
186 (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
187
Dan Stozaa894d082015-02-19 15:27:36 -0800188 // EGL_KHR_partial_update
189 { "eglSetDamageRegionKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700191
192 { "eglCreateStreamKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
194 { "eglDestroyStreamKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
196 { "eglStreamAttribKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
198 { "eglQueryStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
200 { "eglQueryStreamu64KHR",
201 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
202 { "eglQueryStreamTimeKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
204 { "eglCreateStreamProducerSurfaceKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
206 { "eglStreamConsumerGLTextureExternalKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
208 { "eglStreamConsumerAcquireKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
210 { "eglStreamConsumerReleaseKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
212 { "eglGetStreamFileDescriptorKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
214 { "eglCreateStreamFromFileDescriptorKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700216
217 // EGL_ANDROID_get_frame_timestamps
218 { "eglGetFrameTimestampsANDROID",
219 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
220 { "eglQueryTimestampSupportedANDROID",
221 (__eglMustCastToProperFunctionPointerType)&eglQueryTimestampSupportedANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700222};
223
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700224/*
225 * These extensions entry-points should not be exposed to applications.
226 * They're used internally by the Android EGL layer.
227 */
228#define FILTER_EXTENSIONS(procname) \
229 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
230 !strcmp((procname), "eglHibernateProcessIMG") || \
231 !strcmp((procname), "eglAwakenProcessIMG") || \
232 !strcmp((procname), "eglDupNativeFenceFDANDROID"))
233
234
235
Mathias Agopian518ec112011-05-13 16:21:08 -0700236// accesses protected by sExtensionMapMutex
237static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
238static int sGLExtentionSlot = 0;
239static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
240
241static void(*findProcAddress(const char* name,
242 const extention_map_t* map, size_t n))() {
243 for (uint32_t i=0 ; i<n ; i++) {
244 if (!strcmp(name, map[i].name)) {
245 return map[i].address;
246 }
247 }
248 return NULL;
249}
250
251// ----------------------------------------------------------------------------
252
Mathias Agopian518ec112011-05-13 16:21:08 -0700253extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
254extern EGLBoolean egl_init_drivers();
255extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700256extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700257
Mathias Agopian518ec112011-05-13 16:21:08 -0700258} // namespace android;
259
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700260
Mathias Agopian518ec112011-05-13 16:21:08 -0700261// ----------------------------------------------------------------------------
262
263static inline void clearError() { egl_tls_t::clearError(); }
264static inline EGLContext getContext() { return egl_tls_t::getContext(); }
265
266// ----------------------------------------------------------------------------
267
268EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
269{
270 clearError();
271
Dan Stozac3289c42014-01-17 11:38:34 -0800272 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700273 if (index >= NUM_DISPLAYS) {
274 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
275 }
276
277 if (egl_init_drivers() == EGL_FALSE) {
278 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
279 }
280
281 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
282 return dpy;
283}
284
285// ----------------------------------------------------------------------------
286// Initialization
287// ----------------------------------------------------------------------------
288
289EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
290{
291 clearError();
292
Jesse Hallb29e5e82012-04-04 16:53:42 -0700293 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
295
296 EGLBoolean res = dp->initialize(major, minor);
297
298 return res;
299}
300
301EGLBoolean eglTerminate(EGLDisplay dpy)
302{
303 // NOTE: don't unload the drivers b/c some APIs can be called
304 // after eglTerminate() has been called. eglTerminate() only
305 // terminates an EGLDisplay, not a EGL itself.
306
307 clearError();
308
Jesse Hallb29e5e82012-04-04 16:53:42 -0700309 egl_display_ptr dp = get_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700310 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
311
312 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800313
Mathias Agopian518ec112011-05-13 16:21:08 -0700314 return res;
315}
316
317// ----------------------------------------------------------------------------
318// configuration
319// ----------------------------------------------------------------------------
320
321EGLBoolean eglGetConfigs( EGLDisplay dpy,
322 EGLConfig *configs,
323 EGLint config_size, EGLint *num_config)
324{
325 clearError();
326
Jesse Hallb29e5e82012-04-04 16:53:42 -0700327 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700328 if (!dp) return EGL_FALSE;
329
Mathias Agopian7773c432012-02-13 20:06:08 -0800330 if (num_config==0) {
331 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700332 }
333
Mathias Agopian7773c432012-02-13 20:06:08 -0800334 EGLBoolean res = EGL_FALSE;
335 *num_config = 0;
336
337 egl_connection_t* const cnx = &gEGLImpl;
338 if (cnx->dso) {
339 res = cnx->egl.eglGetConfigs(
340 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700341 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800342
343 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700344}
345
346EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
347 EGLConfig *configs, EGLint config_size,
348 EGLint *num_config)
349{
350 clearError();
351
Jesse Hallb29e5e82012-04-04 16:53:42 -0700352 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700353 if (!dp) return EGL_FALSE;
354
355 if (num_config==0) {
356 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
357 }
358
Mathias Agopian518ec112011-05-13 16:21:08 -0700359 EGLBoolean res = EGL_FALSE;
360 *num_config = 0;
361
Mathias Agopianada798b2012-02-13 17:09:30 -0800362 egl_connection_t* const cnx = &gEGLImpl;
363 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700364 if (attrib_list) {
365 char value[PROPERTY_VALUE_MAX];
366 property_get("debug.egl.force_msaa", value, "false");
367
368 if (!strcmp(value, "true")) {
369 size_t attribCount = 0;
370 EGLint attrib = attrib_list[0];
371
372 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700373 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700374 const EGLint *attribRendererable = NULL;
375 const EGLint *attribCaveat = NULL;
376
377 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700378 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700379 while (attrib != EGL_NONE) {
380 attrib = attrib_list[attribCount];
381 switch (attrib) {
382 case EGL_RENDERABLE_TYPE:
383 attribRendererable = &attrib_list[attribCount];
384 break;
385 case EGL_CONFIG_CAVEAT:
386 attribCaveat = &attrib_list[attribCount];
387 break;
388 }
389 attribCount++;
390 }
391
392 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
393 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800394
Romain Guy1cffc802012-10-15 18:13:05 -0700395 // Insert 2 extra attributes to force-enable MSAA 4x
396 EGLint aaAttribs[attribCount + 4];
397 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
398 aaAttribs[1] = 1;
399 aaAttribs[2] = EGL_SAMPLES;
400 aaAttribs[3] = 4;
401
402 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
403
404 EGLint numConfigAA;
405 EGLBoolean resAA = cnx->egl.eglChooseConfig(
406 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
407
408 if (resAA == EGL_TRUE && numConfigAA > 0) {
409 ALOGD("Enabling MSAA 4x");
410 *num_config = numConfigAA;
411 return resAA;
412 }
413 }
414 }
415 }
416
Mathias Agopian7773c432012-02-13 20:06:08 -0800417 res = cnx->egl.eglChooseConfig(
418 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700419 }
420 return res;
421}
422
423EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
424 EGLint attribute, EGLint *value)
425{
426 clearError();
427
Jesse Hallb29e5e82012-04-04 16:53:42 -0700428 egl_connection_t* cnx = NULL;
429 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
430 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800431
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800433 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700434}
435
436// ----------------------------------------------------------------------------
437// surfaces
438// ----------------------------------------------------------------------------
439
Jesse Hallc2e41222013-08-08 13:40:22 -0700440// Turn linear formats into corresponding sRGB formats when colorspace is
441// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
442// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800443// the modification isn't possible, the original dataSpace is returned.
444static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
445 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700446 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800447 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700448 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800449 return HAL_DATASPACE_SRGB;
Jesse Hallc2e41222013-08-08 13:40:22 -0700450 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800451 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700452}
453
Mathias Agopian518ec112011-05-13 16:21:08 -0700454EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
455 NativeWindowType window,
456 const EGLint *attrib_list)
457{
458 clearError();
459
Jesse Hallb29e5e82012-04-04 16:53:42 -0700460 egl_connection_t* cnx = NULL;
461 egl_display_ptr dp = validate_display_connection(dpy, cnx);
462 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800463 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700464
Andy McFaddend566ce32014-01-07 15:54:17 -0800465 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
466 if (result != OK) {
467 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
468 "failed (%#x) (already connected to another API?)",
469 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700470 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700471 }
472
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700473 // Set the native window's buffers format to match what this config requests.
Jesse Hallc2e41222013-08-08 13:40:22 -0700474 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
475 // of our native format. So if sRGB gamma is requested, we have to
476 // modify the EGLconfig's format before setting the native window's
477 // format.
Alistair Strachan733a8072015-02-12 12:33:25 -0800478
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700479 // by default, just pick RGBA_8888
480 EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800481 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700482
483 EGLint a = 0;
484 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
485 if (a > 0) {
486 // alpha-channel requested, there's really only one suitable format
487 format = HAL_PIXEL_FORMAT_RGBA_8888;
488 } else {
489 EGLint r, g, b;
490 r = g = b = 0;
491 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
492 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
493 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
494 EGLint colorDepth = r + g + b;
495 if (colorDepth <= 16) {
496 format = HAL_PIXEL_FORMAT_RGB_565;
497 } else {
498 format = HAL_PIXEL_FORMAT_RGBX_8888;
499 }
Jesse Hallc2e41222013-08-08 13:40:22 -0700500 }
Mathias Agopian0f288fc2013-08-21 16:36:34 -0700501
502 // now select a corresponding sRGB format if needed
503 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
504 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
505 if (*attr == EGL_GL_COLORSPACE_KHR) {
Sandeep Shinde9c67bfd2015-02-10 16:04:15 +0530506 dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
Jamie Gennisbee205f2011-07-01 13:12:07 -0700507 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700508 }
509 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800510
Jesse Hallc2e41222013-08-08 13:40:22 -0700511 if (format != 0) {
512 int err = native_window_set_buffers_format(window, format);
513 if (err != 0) {
514 ALOGE("error setting native window pixel format: %s (%d)",
515 strerror(-err), err);
516 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
517 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
518 }
519 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700520
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800521 if (dataSpace != 0) {
522 int err = native_window_set_buffers_data_space(window, dataSpace);
523 if (err != 0) {
524 ALOGE("error setting native window pixel dataSpace: %s (%d)",
525 strerror(-err), err);
526 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
527 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
528 }
529 }
530
Jamie Gennis59769462011-11-19 18:04:43 -0800531 // the EGL spec requires that a new EGLSurface default to swap interval
532 // 1, so explicitly set that on the window here.
533 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
534 anw->setSwapInterval(anw, 1);
535
Mathias Agopian518ec112011-05-13 16:21:08 -0700536 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800537 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700538 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700539 egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
540 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700541 return s;
542 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700543
544 // EGLSurface creation failed
545 native_window_set_buffers_format(window, 0);
546 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700547 }
548 return EGL_NO_SURFACE;
549}
550
551EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
552 NativePixmapType pixmap,
553 const EGLint *attrib_list)
554{
555 clearError();
556
Jesse Hallb29e5e82012-04-04 16:53:42 -0700557 egl_connection_t* cnx = NULL;
558 egl_display_ptr dp = validate_display_connection(dpy, cnx);
559 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700560 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800561 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700562 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700563 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
564 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700565 return s;
566 }
567 }
568 return EGL_NO_SURFACE;
569}
570
571EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
572 const EGLint *attrib_list)
573{
574 clearError();
575
Jesse Hallb29e5e82012-04-04 16:53:42 -0700576 egl_connection_t* cnx = NULL;
577 egl_display_ptr dp = validate_display_connection(dpy, cnx);
578 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700579 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800580 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700581 if (surface != EGL_NO_SURFACE) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700582 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
583 surface, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700584 return s;
585 }
586 }
587 return EGL_NO_SURFACE;
588}
Jesse Hall47743382013-02-08 11:13:46 -0800589
Mathias Agopian518ec112011-05-13 16:21:08 -0700590EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
591{
592 clearError();
593
Jesse Hallb29e5e82012-04-04 16:53:42 -0700594 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700595 if (!dp) return EGL_FALSE;
596
Jesse Hallb29e5e82012-04-04 16:53:42 -0700597 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700598 if (!_s.get())
599 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700600
601 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800602 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700603 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700604 _s.terminate();
605 }
606 return result;
607}
608
609EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
610 EGLint attribute, EGLint *value)
611{
612 clearError();
613
Jesse Hallb29e5e82012-04-04 16:53:42 -0700614 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700615 if (!dp) return EGL_FALSE;
616
Jesse Hallb29e5e82012-04-04 16:53:42 -0700617 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700618 if (!_s.get())
619 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700620
Mathias Agopian518ec112011-05-13 16:21:08 -0700621 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian7773c432012-02-13 20:06:08 -0800622 return s->cnx->egl.eglQuerySurface(
623 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700624}
625
Jamie Gennise8696a42012-01-15 18:54:57 -0800626void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800627 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800628 clearError();
629
Jesse Hallb29e5e82012-04-04 16:53:42 -0700630 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800631 if (!dp) {
632 return;
633 }
634
Jesse Hallb29e5e82012-04-04 16:53:42 -0700635 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800636 if (!_s.get()) {
637 setError(EGL_BAD_SURFACE, EGL_FALSE);
638 return;
639 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800640}
641
Mathias Agopian518ec112011-05-13 16:21:08 -0700642// ----------------------------------------------------------------------------
643// Contexts
644// ----------------------------------------------------------------------------
645
646EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
647 EGLContext share_list, const EGLint *attrib_list)
648{
649 clearError();
650
Jesse Hallb29e5e82012-04-04 16:53:42 -0700651 egl_connection_t* cnx = NULL;
652 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700653 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700654 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700655 if (!ContextRef(dp.get(), share_list).get()) {
656 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
657 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700658 egl_context_t* const c = get_context(share_list);
659 share_list = c->context;
660 }
661 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800662 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700663 if (context != EGL_NO_CONTEXT) {
664 // figure out if it's a GLESv1 or GLESv2
665 int version = 0;
666 if (attrib_list) {
667 while (*attrib_list != EGL_NONE) {
668 GLint attr = *attrib_list++;
669 GLint value = *attrib_list++;
670 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
671 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800672 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800673 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800674 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700675 }
676 }
677 };
678 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700679 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
680 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700681 return c;
682 }
683 }
684 return EGL_NO_CONTEXT;
685}
686
687EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
688{
689 clearError();
690
Jesse Hallb29e5e82012-04-04 16:53:42 -0700691 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700692 if (!dp)
693 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700694
Jesse Hallb29e5e82012-04-04 16:53:42 -0700695 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700696 if (!_c.get())
697 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800698
Mathias Agopian518ec112011-05-13 16:21:08 -0700699 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800700 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700701 if (result == EGL_TRUE) {
702 _c.terminate();
703 }
704 return result;
705}
706
Mathias Agopian518ec112011-05-13 16:21:08 -0700707EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
708 EGLSurface read, EGLContext ctx)
709{
710 clearError();
711
Jesse Hallb29e5e82012-04-04 16:53:42 -0700712 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700713 if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
714
Mathias Agopian5b287a62011-05-16 18:58:55 -0700715 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
716 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
717 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700718 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
719 (draw != EGL_NO_SURFACE) ) {
720 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
721 }
722
723 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700724 ContextRef _c(dp.get(), ctx);
725 SurfaceRef _d(dp.get(), draw);
726 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700727
728 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700729 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700730 // EGL_NO_CONTEXT is valid
Michael Chock0673e1e2012-06-21 12:53:17 -0700731 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700732 }
733
734 // these are the underlying implementation's object
735 EGLContext impl_ctx = EGL_NO_CONTEXT;
736 EGLSurface impl_draw = EGL_NO_SURFACE;
737 EGLSurface impl_read = EGL_NO_SURFACE;
738
739 // these are our objects structs passed in
740 egl_context_t * c = NULL;
741 egl_surface_t const * d = NULL;
742 egl_surface_t const * r = NULL;
743
744 // these are the current objects structs
745 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800746
Mathias Agopian518ec112011-05-13 16:21:08 -0700747 if (ctx != EGL_NO_CONTEXT) {
748 c = get_context(ctx);
749 impl_ctx = c->context;
750 } else {
751 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700752 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
753 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
754 return setError(EGL_BAD_MATCH, EGL_FALSE);
755 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700756 if (cur_c == NULL) {
757 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700758 // not an error, there is just no current context.
759 return EGL_TRUE;
760 }
761 }
762
763 // retrieve the underlying implementation's draw EGLSurface
764 if (draw != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700765 if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700766 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700767 impl_draw = d->surface;
768 }
769
770 // retrieve the underlying implementation's read EGLSurface
771 if (read != EGL_NO_SURFACE) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700772 if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700773 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700774 impl_read = r->surface;
775 }
776
Mathias Agopian518ec112011-05-13 16:21:08 -0700777
Jesse Hallb29e5e82012-04-04 16:53:42 -0700778 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800779 draw, read, ctx,
780 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700781
782 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800783 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700784 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
785 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700786 _c.acquire();
787 _r.acquire();
788 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700789 } else {
790 setGLHooksThreadSpecific(&gHooksNoContext);
791 egl_tls_t::setContext(EGL_NO_CONTEXT);
792 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700793 } else {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000794 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -0700795 egl_connection_t* const cnx = &gEGLImpl;
796 result = setError(cnx->egl.eglGetError(), EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700797 }
798 return result;
799}
800
801
802EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
803 EGLint attribute, EGLint *value)
804{
805 clearError();
806
Jesse Hallb29e5e82012-04-04 16:53:42 -0700807 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700808 if (!dp) return EGL_FALSE;
809
Jesse Hallb29e5e82012-04-04 16:53:42 -0700810 ContextRef _c(dp.get(), ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
812
Mathias Agopian518ec112011-05-13 16:21:08 -0700813 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -0800814 return c->cnx->egl.eglQueryContext(
815 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700816
Mathias Agopian518ec112011-05-13 16:21:08 -0700817}
818
819EGLContext eglGetCurrentContext(void)
820{
821 // could be called before eglInitialize(), but we wouldn't have a context
822 // then, and this function would correctly return EGL_NO_CONTEXT.
823
824 clearError();
825
826 EGLContext ctx = getContext();
827 return ctx;
828}
829
830EGLSurface eglGetCurrentSurface(EGLint readdraw)
831{
832 // could be called before eglInitialize(), but we wouldn't have a context
833 // then, and this function would correctly return EGL_NO_SURFACE.
834
835 clearError();
836
837 EGLContext ctx = getContext();
838 if (ctx) {
839 egl_context_t const * const c = get_context(ctx);
840 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
841 switch (readdraw) {
842 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -0800843 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -0700844 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
845 }
846 }
847 return EGL_NO_SURFACE;
848}
849
850EGLDisplay eglGetCurrentDisplay(void)
851{
852 // could be called before eglInitialize(), but we wouldn't have a context
853 // then, and this function would correctly return EGL_NO_DISPLAY.
854
855 clearError();
856
857 EGLContext ctx = getContext();
858 if (ctx) {
859 egl_context_t const * const c = get_context(ctx);
860 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
861 return c->dpy;
862 }
863 return EGL_NO_DISPLAY;
864}
865
866EGLBoolean eglWaitGL(void)
867{
Mathias Agopian518ec112011-05-13 16:21:08 -0700868 clearError();
869
Mathias Agopianada798b2012-02-13 17:09:30 -0800870 egl_connection_t* const cnx = &gEGLImpl;
871 if (!cnx->dso)
872 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
873
874 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700875}
876
877EGLBoolean eglWaitNative(EGLint engine)
878{
Mathias Agopian518ec112011-05-13 16:21:08 -0700879 clearError();
880
Mathias Agopianada798b2012-02-13 17:09:30 -0800881 egl_connection_t* const cnx = &gEGLImpl;
882 if (!cnx->dso)
883 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
884
885 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -0700886}
887
888EGLint eglGetError(void)
889{
Mathias Agopianada798b2012-02-13 17:09:30 -0800890 EGLint err = EGL_SUCCESS;
891 egl_connection_t* const cnx = &gEGLImpl;
892 if (cnx->dso) {
893 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -0700894 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800895 if (err == EGL_SUCCESS) {
896 err = egl_tls_t::getError();
897 }
898 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -0700899}
900
Michael Chockc0ec5e22014-01-27 08:14:33 -0800901static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -0700902 const char* procname) {
903 const egl_connection_t* cnx = &gEGLImpl;
904 void* proc = NULL;
905
Michael Chockc0ec5e22014-01-27 08:14:33 -0800906 proc = dlsym(cnx->libEgl, procname);
907 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
908
Jesse Hallc07b5202013-07-04 12:08:16 -0700909 proc = dlsym(cnx->libGles2, procname);
910 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
911
912 proc = dlsym(cnx->libGles1, procname);
913 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
914
915 return NULL;
916}
917
Mathias Agopian518ec112011-05-13 16:21:08 -0700918__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
919{
920 // eglGetProcAddress() could be the very first function called
921 // in which case we must make sure we've initialized ourselves, this
922 // happens the first time egl_get_display() is called.
923
924 clearError();
925
926 if (egl_init_drivers() == EGL_FALSE) {
927 setError(EGL_BAD_PARAMETER, NULL);
928 return NULL;
929 }
930
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700931 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -0700932 return NULL;
933 }
934
Mathias Agopian518ec112011-05-13 16:21:08 -0700935 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700936 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -0700937 if (addr) return addr;
938
Michael Chockc0ec5e22014-01-27 08:14:33 -0800939 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -0700940 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -0700941
Mathias Agopian518ec112011-05-13 16:21:08 -0700942 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
943 pthread_mutex_lock(&sExtensionMapMutex);
944
945 /*
946 * Since eglGetProcAddress() is not associated to anything, it needs
947 * to return a function pointer that "works" regardless of what
948 * the current context is.
949 *
950 * For this reason, we return a "forwarder", a small stub that takes
951 * care of calling the function associated with the context
952 * currently bound.
953 *
954 * We first look for extensions we've already resolved, if we're seeing
955 * this extension for the first time, we go through all our
956 * implementations and call eglGetProcAddress() and record the
957 * result in the appropriate implementation hooks and return the
958 * address of the forwarder corresponding to that hook set.
959 *
960 */
961
962 const String8 name(procname);
963 addr = sGLExtentionMap.valueFor(name);
964 const int slot = sGLExtentionSlot;
965
Steve Blocke6f43dd2012-01-06 19:20:56 +0000966 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -0700967 "no more slots for eglGetProcAddress(\"%s\")",
968 procname);
969
970 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
971 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -0800972
973 egl_connection_t* const cnx = &gEGLImpl;
974 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800975 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +0800976 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -0800977 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
978 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -0800979 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +0800980 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -0700981 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800982
Mathias Agopian518ec112011-05-13 16:21:08 -0700983 if (found) {
984 addr = gExtensionForwarders[slot];
Mathias Agopian518ec112011-05-13 16:21:08 -0700985 sGLExtentionMap.add(name, addr);
986 sGLExtentionSlot++;
987 }
988 }
989
990 pthread_mutex_unlock(&sExtensionMapMutex);
991 return addr;
992}
993
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700994class FrameCompletionThread : public Thread {
995public:
996
997 static void queueSync(EGLSyncKHR sync) {
998 static sp<FrameCompletionThread> thread(new FrameCompletionThread);
999 static bool running = false;
1000 if (!running) {
1001 thread->run("GPUFrameCompletion");
1002 running = true;
1003 }
1004 {
1005 Mutex::Autolock lock(thread->mMutex);
1006 ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1007 thread->mFramesQueued).string());
1008 thread->mQueue.push_back(sync);
1009 thread->mCondition.signal();
1010 thread->mFramesQueued++;
1011 ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1012 }
1013 }
1014
1015private:
1016 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1017
1018 virtual bool threadLoop() {
1019 EGLSyncKHR sync;
1020 uint32_t frameNum;
1021 {
1022 Mutex::Autolock lock(mMutex);
1023 while (mQueue.isEmpty()) {
1024 mCondition.wait(mMutex);
1025 }
1026 sync = mQueue[0];
1027 frameNum = mFramesCompleted;
1028 }
1029 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1030 {
1031 ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1032 frameNum).string());
1033 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1034 if (result == EGL_FALSE) {
1035 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1036 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1037 ALOGE("FrameCompletion: timeout waiting for fence");
1038 }
1039 eglDestroySyncKHR(dpy, sync);
1040 }
1041 {
1042 Mutex::Autolock lock(mMutex);
1043 mQueue.removeAt(0);
1044 mFramesCompleted++;
1045 ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1046 }
1047 return true;
1048 }
1049
1050 uint32_t mFramesQueued;
1051 uint32_t mFramesCompleted;
1052 Vector<EGLSyncKHR> mQueue;
1053 Condition mCondition;
1054 Mutex mMutex;
1055};
1056
Dan Stozaa894d082015-02-19 15:27:36 -08001057EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1058 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001059{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001060 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001061 clearError();
1062
Jesse Hallb29e5e82012-04-04 16:53:42 -07001063 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001064 if (!dp) return EGL_FALSE;
1065
Jesse Hallb29e5e82012-04-04 16:53:42 -07001066 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001067 if (!_s.get())
1068 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001069
Mathias Agopian518ec112011-05-13 16:21:08 -07001070 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001071
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001072 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1073 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1074 if (sync != EGL_NO_SYNC_KHR) {
1075 FrameCompletionThread::queueSync(sync);
1076 }
1077 }
1078
Mathias Agopian7db993a2012-03-25 00:49:46 -07001079 if (CC_UNLIKELY(dp->finishOnSwap)) {
1080 uint32_t pixel;
1081 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1082 if (c) {
1083 // glReadPixels() ensures that the frame is complete
1084 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1085 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1086 }
1087 }
1088
Dan Stozaa894d082015-02-19 15:27:36 -08001089 if (n_rects == 0) {
1090 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1091 }
1092
1093 Vector<android_native_rect_t> androidRects;
1094 for (int r = 0; r < n_rects; ++r) {
1095 int offset = r * 4;
1096 int x = rects[offset];
1097 int y = rects[offset + 1];
1098 int width = rects[offset + 2];
1099 int height = rects[offset + 3];
1100 android_native_rect_t androidRect;
1101 androidRect.left = x;
1102 androidRect.top = y + height;
1103 androidRect.right = x + width;
1104 androidRect.bottom = y;
1105 androidRects.push_back(androidRect);
1106 }
1107 native_window_set_surface_damage(s->win.get(), androidRects.array(),
1108 androidRects.size());
1109
1110 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1111 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1112 rects, n_rects);
1113 } else {
1114 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1115 }
1116}
1117
1118EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1119{
1120 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001121}
1122
1123EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1124 NativePixmapType target)
1125{
1126 clearError();
1127
Jesse Hallb29e5e82012-04-04 16:53:42 -07001128 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001129 if (!dp) return EGL_FALSE;
1130
Jesse Hallb29e5e82012-04-04 16:53:42 -07001131 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001132 if (!_s.get())
1133 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001134
Mathias Agopian518ec112011-05-13 16:21:08 -07001135 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001136 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001137}
1138
1139const char* eglQueryString(EGLDisplay dpy, EGLint name)
1140{
1141 clearError();
1142
Jesse Hallb29e5e82012-04-04 16:53:42 -07001143 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001144 if (!dp) return (const char *) NULL;
1145
1146 switch (name) {
1147 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001148 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001149 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001150 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001151 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001152 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001153 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001154 return dp->getClientApiString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001155 }
1156 return setError(EGL_BAD_PARAMETER, (const char *)0);
1157}
1158
Mathias Agopianca088332013-03-28 17:44:13 -07001159EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1160{
1161 clearError();
1162
1163 const egl_display_ptr dp = validate_display(dpy);
1164 if (!dp) return (const char *) NULL;
1165
1166 switch (name) {
1167 case EGL_VENDOR:
1168 return dp->disp.queryString.vendor;
1169 case EGL_VERSION:
1170 return dp->disp.queryString.version;
1171 case EGL_EXTENSIONS:
1172 return dp->disp.queryString.extensions;
1173 case EGL_CLIENT_APIS:
1174 return dp->disp.queryString.clientApi;
1175 }
1176 return setError(EGL_BAD_PARAMETER, (const char *)0);
1177}
Mathias Agopian518ec112011-05-13 16:21:08 -07001178
1179// ----------------------------------------------------------------------------
1180// EGL 1.1
1181// ----------------------------------------------------------------------------
1182
1183EGLBoolean eglSurfaceAttrib(
1184 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1185{
1186 clearError();
1187
Jesse Hallb29e5e82012-04-04 16:53:42 -07001188 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001189 if (!dp) return EGL_FALSE;
1190
Jesse Hallb29e5e82012-04-04 16:53:42 -07001191 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001192 if (!_s.get())
1193 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001194
Pablo Ceballosc18be292016-05-31 14:55:42 -07001195 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001196
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001197 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Pablo Ceballosf051ade2016-03-15 18:27:20 -07001198 int err = native_window_set_auto_refresh(s->win.get(),
1199 value ? true : false);
1200 return (err == NO_ERROR) ? EGL_TRUE :
1201 setError(EGL_BAD_SURFACE, EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001202 }
1203
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001204#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07001205 if (attribute == EGL_TIMESTAMPS_ANDROID) {
1206 s->enableTimestamps = value;
1207 return EGL_TRUE;
1208 }
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07001209#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07001210
Mathias Agopian518ec112011-05-13 16:21:08 -07001211 if (s->cnx->egl.eglSurfaceAttrib) {
1212 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001213 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001214 }
1215 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1216}
1217
1218EGLBoolean eglBindTexImage(
1219 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1220{
1221 clearError();
1222
Jesse Hallb29e5e82012-04-04 16:53:42 -07001223 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001224 if (!dp) return EGL_FALSE;
1225
Jesse Hallb29e5e82012-04-04 16:53:42 -07001226 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001227 if (!_s.get())
1228 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001229
Mathias Agopian518ec112011-05-13 16:21:08 -07001230 egl_surface_t const * const s = get_surface(surface);
1231 if (s->cnx->egl.eglBindTexImage) {
1232 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001233 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001234 }
1235 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1236}
1237
1238EGLBoolean eglReleaseTexImage(
1239 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1240{
1241 clearError();
1242
Jesse Hallb29e5e82012-04-04 16:53:42 -07001243 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001244 if (!dp) return EGL_FALSE;
1245
Jesse Hallb29e5e82012-04-04 16:53:42 -07001246 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001247 if (!_s.get())
1248 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001249
Mathias Agopian518ec112011-05-13 16:21:08 -07001250 egl_surface_t const * const s = get_surface(surface);
1251 if (s->cnx->egl.eglReleaseTexImage) {
1252 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001253 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001254 }
1255 return setError(EGL_BAD_SURFACE, EGL_FALSE);
1256}
1257
1258EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1259{
1260 clearError();
1261
Jesse Hallb29e5e82012-04-04 16:53:42 -07001262 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001263 if (!dp) return EGL_FALSE;
1264
1265 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001266 egl_connection_t* const cnx = &gEGLImpl;
1267 if (cnx->dso && cnx->egl.eglSwapInterval) {
1268 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001269 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001270
Mathias Agopian518ec112011-05-13 16:21:08 -07001271 return res;
1272}
1273
1274
1275// ----------------------------------------------------------------------------
1276// EGL 1.2
1277// ----------------------------------------------------------------------------
1278
1279EGLBoolean eglWaitClient(void)
1280{
1281 clearError();
1282
Mathias Agopianada798b2012-02-13 17:09:30 -08001283 egl_connection_t* const cnx = &gEGLImpl;
1284 if (!cnx->dso)
1285 return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1286
1287 EGLBoolean res;
1288 if (cnx->egl.eglWaitClient) {
1289 res = cnx->egl.eglWaitClient();
1290 } else {
1291 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001292 }
1293 return res;
1294}
1295
1296EGLBoolean eglBindAPI(EGLenum api)
1297{
1298 clearError();
1299
1300 if (egl_init_drivers() == EGL_FALSE) {
1301 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1302 }
1303
1304 // bind this API on all EGLs
1305 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001306 egl_connection_t* const cnx = &gEGLImpl;
1307 if (cnx->dso && cnx->egl.eglBindAPI) {
1308 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001309 }
1310 return res;
1311}
1312
1313EGLenum eglQueryAPI(void)
1314{
1315 clearError();
1316
1317 if (egl_init_drivers() == EGL_FALSE) {
1318 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1319 }
1320
Mathias Agopianada798b2012-02-13 17:09:30 -08001321 egl_connection_t* const cnx = &gEGLImpl;
1322 if (cnx->dso && cnx->egl.eglQueryAPI) {
1323 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001324 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001325
Mathias Agopian518ec112011-05-13 16:21:08 -07001326 // or, it can only be OpenGL ES
1327 return EGL_OPENGL_ES_API;
1328}
1329
1330EGLBoolean eglReleaseThread(void)
1331{
1332 clearError();
1333
Mathias Agopianada798b2012-02-13 17:09:30 -08001334 egl_connection_t* const cnx = &gEGLImpl;
1335 if (cnx->dso && cnx->egl.eglReleaseThread) {
1336 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001337 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301338
1339 // If there is context bound to the thread, release it
1340 egl_display_t::loseCurrent(get_context(getContext()));
1341
Mathias Agopian518ec112011-05-13 16:21:08 -07001342 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001343 return EGL_TRUE;
1344}
1345
1346EGLSurface eglCreatePbufferFromClientBuffer(
1347 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1348 EGLConfig config, const EGLint *attrib_list)
1349{
1350 clearError();
1351
Jesse Hallb29e5e82012-04-04 16:53:42 -07001352 egl_connection_t* cnx = NULL;
1353 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1354 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001355 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1356 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001357 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001358 }
1359 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1360}
1361
1362// ----------------------------------------------------------------------------
1363// EGL_EGLEXT_VERSION 3
1364// ----------------------------------------------------------------------------
1365
1366EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1367 const EGLint *attrib_list)
1368{
1369 clearError();
1370
Jesse Hallb29e5e82012-04-04 16:53:42 -07001371 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001372 if (!dp) return EGL_FALSE;
1373
Jesse Hallb29e5e82012-04-04 16:53:42 -07001374 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001375 if (!_s.get())
1376 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001377
1378 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001379 if (s->cnx->egl.eglLockSurfaceKHR) {
1380 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001381 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001382 }
1383 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1384}
1385
1386EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1387{
1388 clearError();
1389
Jesse Hallb29e5e82012-04-04 16:53:42 -07001390 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001391 if (!dp) return EGL_FALSE;
1392
Jesse Hallb29e5e82012-04-04 16:53:42 -07001393 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001394 if (!_s.get())
1395 return setError(EGL_BAD_SURFACE, EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001396
1397 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001398 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001399 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001400 }
1401 return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1402}
1403
1404EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1405 EGLClientBuffer buffer, const EGLint *attrib_list)
1406{
1407 clearError();
1408
Jesse Hallb29e5e82012-04-04 16:53:42 -07001409 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001410 if (!dp) return EGL_NO_IMAGE_KHR;
1411
Jesse Hallb29e5e82012-04-04 16:53:42 -07001412 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001413 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001414
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001415 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1416 egl_connection_t* const cnx = &gEGLImpl;
1417 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1418 result = cnx->egl.eglCreateImageKHR(
1419 dp->disp.dpy,
1420 c ? c->context : EGL_NO_CONTEXT,
1421 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001422 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001423 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001424}
1425
1426EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1427{
1428 clearError();
1429
Jesse Hallb29e5e82012-04-04 16:53:42 -07001430 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001431 if (!dp) return EGL_FALSE;
1432
Steven Holte646a5c52012-06-04 20:02:11 -07001433 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001434 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001435 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001436 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001437 }
Steven Holte646a5c52012-06-04 20:02:11 -07001438 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001439}
1440
1441// ----------------------------------------------------------------------------
1442// EGL_EGLEXT_VERSION 5
1443// ----------------------------------------------------------------------------
1444
1445
1446EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1447{
1448 clearError();
1449
Jesse Hallb29e5e82012-04-04 16:53:42 -07001450 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001451 if (!dp) return EGL_NO_SYNC_KHR;
1452
Mathias Agopian518ec112011-05-13 16:21:08 -07001453 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001454 egl_connection_t* const cnx = &gEGLImpl;
1455 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1456 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001457 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001458 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001459}
1460
1461EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1462{
1463 clearError();
1464
Jesse Hallb29e5e82012-04-04 16:53:42 -07001465 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001466 if (!dp) return EGL_FALSE;
1467
Mathias Agopian518ec112011-05-13 16:21:08 -07001468 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001469 egl_connection_t* const cnx = &gEGLImpl;
1470 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1471 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001472 }
1473 return result;
1474}
1475
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001476EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1477 clearError();
1478
1479 const egl_display_ptr dp = validate_display(dpy);
1480 if (!dp) return EGL_FALSE;
1481
1482 EGLBoolean result = EGL_FALSE;
1483 egl_connection_t* const cnx = &gEGLImpl;
1484 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1485 result = cnx->egl.eglSignalSyncKHR(
1486 dp->disp.dpy, sync, mode);
1487 }
1488 return result;
1489}
1490
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001491EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1492 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001493{
1494 clearError();
1495
Jesse Hallb29e5e82012-04-04 16:53:42 -07001496 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001497 if (!dp) return EGL_FALSE;
1498
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001499 EGLBoolean result = EGL_FALSE;
1500 egl_connection_t* const cnx = &gEGLImpl;
1501 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1502 result = cnx->egl.eglClientWaitSyncKHR(
1503 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001504 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001505 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001506}
1507
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001508EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1509 EGLint attribute, EGLint *value)
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.eglGetSyncAttribKHR) {
1519 result = cnx->egl.eglGetSyncAttribKHR(
1520 dp->disp.dpy, sync, attribute, value);
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
Season Li000d88f2015-07-01 11:39:40 -07001525EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1526{
1527 clearError();
1528
1529 const egl_display_ptr dp = validate_display(dpy);
1530 if (!dp) return EGL_NO_STREAM_KHR;
1531
1532 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1533 egl_connection_t* const cnx = &gEGLImpl;
1534 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1535 result = cnx->egl.eglCreateStreamKHR(
1536 dp->disp.dpy, attrib_list);
1537 }
1538 return result;
1539}
1540
1541EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1542{
1543 clearError();
1544
1545 const egl_display_ptr dp = validate_display(dpy);
1546 if (!dp) return EGL_FALSE;
1547
1548 EGLBoolean result = EGL_FALSE;
1549 egl_connection_t* const cnx = &gEGLImpl;
1550 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1551 result = cnx->egl.eglDestroyStreamKHR(
1552 dp->disp.dpy, stream);
1553 }
1554 return result;
1555}
1556
1557EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1558 EGLenum attribute, EGLint value)
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.eglStreamAttribKHR) {
1568 result = cnx->egl.eglStreamAttribKHR(
1569 dp->disp.dpy, stream, attribute, value);
1570 }
1571 return result;
1572}
1573
1574EGLBoolean eglQueryStreamKHR(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.eglQueryStreamKHR) {
1585 result = cnx->egl.eglQueryStreamKHR(
1586 dp->disp.dpy, stream, attribute, value);
1587 }
1588 return result;
1589}
1590
1591EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1592 EGLenum attribute, EGLuint64KHR *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.eglQueryStreamu64KHR) {
1602 result = cnx->egl.eglQueryStreamu64KHR(
1603 dp->disp.dpy, stream, attribute, value);
1604 }
1605 return result;
1606}
1607
1608EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1609 EGLenum attribute, EGLTimeKHR *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.eglQueryStreamTimeKHR) {
1619 result = cnx->egl.eglQueryStreamTimeKHR(
1620 dp->disp.dpy, stream, attribute, value);
1621 }
1622 return result;
1623}
1624
1625EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1626 EGLStreamKHR stream, const EGLint *attrib_list)
1627{
1628 clearError();
1629
1630 egl_display_ptr dp = validate_display(dpy);
1631 if (!dp) return EGL_NO_SURFACE;
1632
1633 egl_connection_t* const cnx = &gEGLImpl;
1634 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1635 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1636 dp->disp.dpy, config, stream, attrib_list);
1637 if (surface != EGL_NO_SURFACE) {
1638 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1639 surface, cnx);
1640 return s;
1641 }
1642 }
1643 return EGL_NO_SURFACE;
1644}
1645
1646EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1647 EGLStreamKHR stream)
1648{
1649 clearError();
1650
1651 const egl_display_ptr dp = validate_display(dpy);
1652 if (!dp) return EGL_FALSE;
1653
1654 EGLBoolean result = EGL_FALSE;
1655 egl_connection_t* const cnx = &gEGLImpl;
1656 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1657 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1658 dp->disp.dpy, stream);
1659 }
1660 return result;
1661}
1662
1663EGLBoolean eglStreamConsumerAcquireKHR(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.eglStreamConsumerAcquireKHR) {
1674 result = cnx->egl.eglStreamConsumerAcquireKHR(
1675 dp->disp.dpy, stream);
1676 }
1677 return result;
1678}
1679
1680EGLBoolean eglStreamConsumerReleaseKHR(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.eglStreamConsumerReleaseKHR) {
1691 result = cnx->egl.eglStreamConsumerReleaseKHR(
1692 dp->disp.dpy, stream);
1693 }
1694 return result;
1695}
1696
1697EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1698 EGLDisplay dpy, EGLStreamKHR stream)
1699{
1700 clearError();
1701
1702 const egl_display_ptr dp = validate_display(dpy);
1703 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1704
1705 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1706 egl_connection_t* const cnx = &gEGLImpl;
1707 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1708 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1709 dp->disp.dpy, stream);
1710 }
1711 return result;
1712}
1713
1714EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1715 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1716{
1717 clearError();
1718
1719 const egl_display_ptr dp = validate_display(dpy);
1720 if (!dp) return EGL_NO_STREAM_KHR;
1721
1722 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1723 egl_connection_t* const cnx = &gEGLImpl;
1724 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1725 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1726 dp->disp.dpy, file_descriptor);
1727 }
1728 return result;
1729}
1730
Mathias Agopian518ec112011-05-13 16:21:08 -07001731// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07001732// EGL_EGLEXT_VERSION 15
1733// ----------------------------------------------------------------------------
1734
1735EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1736 clearError();
1737 const egl_display_ptr dp = validate_display(dpy);
1738 if (!dp) return EGL_FALSE;
1739 EGLint result = EGL_FALSE;
1740 egl_connection_t* const cnx = &gEGLImpl;
1741 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1742 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1743 }
1744 return result;
1745}
1746
1747// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07001748// ANDROID extensions
1749// ----------------------------------------------------------------------------
1750
Jamie Gennis331841b2012-09-06 14:52:00 -07001751EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1752{
1753 clearError();
1754
1755 const egl_display_ptr dp = validate_display(dpy);
1756 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1757
1758 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1759 egl_connection_t* const cnx = &gEGLImpl;
1760 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1761 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1762 }
1763 return result;
1764}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001765
Andy McFadden72841452013-03-01 16:25:32 -08001766EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1767 EGLnsecsANDROID time)
1768{
1769 clearError();
1770
1771 const egl_display_ptr dp = validate_display(dpy);
1772 if (!dp) {
1773 return EGL_FALSE;
1774 }
1775
1776 SurfaceRef _s(dp.get(), surface);
1777 if (!_s.get()) {
1778 setError(EGL_BAD_SURFACE, EGL_FALSE);
1779 return EGL_FALSE;
1780 }
1781
1782 egl_surface_t const * const s = get_surface(surface);
1783 native_window_set_buffers_timestamp(s->win.get(), time);
1784
1785 return EGL_TRUE;
1786}
1787
Craig Donner05249fc2016-01-15 19:33:55 -08001788EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1789{
1790 clearError();
1791
1792 int usage = 0;
1793 uint32_t width = 0;
1794 uint32_t height = 0;
1795 uint32_t format = 0;
1796 uint32_t red_size = 0;
1797 uint32_t green_size = 0;
1798 uint32_t blue_size = 0;
1799 uint32_t alpha_size = 0;
1800
Craig Donnerb40504a2016-06-03 17:54:25 -07001801#define GET_NONNEGATIVE_VALUE(case_name, target) \
Craig Donner05249fc2016-01-15 19:33:55 -08001802 case case_name: \
Craig Donnerb40504a2016-06-03 17:54:25 -07001803 if (value >= 0) { \
Craig Donner05249fc2016-01-15 19:33:55 -08001804 target = value; \
1805 } else { \
1806 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1807 } \
1808 break
1809
1810 if (attrib_list) {
1811 while (*attrib_list != EGL_NONE) {
1812 GLint attr = *attrib_list++;
1813 GLint value = *attrib_list++;
1814 switch (attr) {
Craig Donnerb40504a2016-06-03 17:54:25 -07001815 GET_NONNEGATIVE_VALUE(EGL_WIDTH, width);
1816 GET_NONNEGATIVE_VALUE(EGL_HEIGHT, height);
1817 GET_NONNEGATIVE_VALUE(EGL_RED_SIZE, red_size);
1818 GET_NONNEGATIVE_VALUE(EGL_GREEN_SIZE, green_size);
1819 GET_NONNEGATIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1820 GET_NONNEGATIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
Craig Donner05249fc2016-01-15 19:33:55 -08001821 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1822 if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1823 usage |= GRALLOC_USAGE_PROTECTED;
Craig Donner05249fc2016-01-15 19:33:55 -08001824 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001825 if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001826 usage |= GRALLOC_USAGE_HW_RENDER;
1827 }
Craig Donner8cfae6d2016-04-12 16:54:03 -07001828 if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
Craig Donner05249fc2016-01-15 19:33:55 -08001829 usage |= GRALLOC_USAGE_HW_TEXTURE;
1830 }
Craig Donner05249fc2016-01-15 19:33:55 -08001831 break;
1832 default:
1833 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1834 }
1835 }
1836 }
Craig Donnerb40504a2016-06-03 17:54:25 -07001837#undef GET_NONNEGATIVE_VALUE
Craig Donner05249fc2016-01-15 19:33:55 -08001838
1839 // Validate format.
1840 if (red_size == 8 && green_size == 8 && blue_size == 8) {
1841 if (alpha_size == 8) {
1842 format = HAL_PIXEL_FORMAT_RGBA_8888;
1843 } else {
1844 format = HAL_PIXEL_FORMAT_RGB_888;
1845 }
1846 } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1847 alpha_size == 0) {
Craig Donner478f7db2016-06-10 17:20:15 -07001848 format = HAL_PIXEL_FORMAT_RGB_565;
Craig Donner05249fc2016-01-15 19:33:55 -08001849 } else {
1850 ALOGE("Invalid native pixel format { r=%d, g=%d, b=%d, a=%d }",
1851 red_size, green_size, blue_size, alpha_size);
1852 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1853 }
1854
Craig Donner68671532016-07-18 10:19:54 -07001855#define CHECK_ERROR_CONDITION(message) \
1856 if (err != NO_ERROR) { \
1857 ALOGE(message); \
1858 goto error_condition; \
1859 }
1860
1861 // The holder is used to destroy the buffer if an error occurs.
1862 GraphicBuffer* gBuffer = new GraphicBuffer();
1863 sp<IServiceManager> sm = defaultServiceManager();
1864 sp<IBinder> surfaceFlinger = sm->getService(String16("SurfaceFlinger"));
1865 sp<IBinder> allocator;
1866 Parcel sc_data, sc_reply, data, reply;
1867 status_t err = NO_ERROR;
1868 if (sm == NULL) {
1869 ALOGE("Unable to connect to ServiceManager");
1870 goto error_condition;
1871 }
1872
1873 // Obtain an allocator.
1874 if (surfaceFlinger == NULL) {
1875 ALOGE("Unable to connect to SurfaceFlinger");
1876 goto error_condition;
1877 }
1878 sc_data.writeInterfaceToken(String16("android.ui.ISurfaceComposer"));
1879 err = surfaceFlinger->transact(
1880 BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, sc_data, &sc_reply);
1881 CHECK_ERROR_CONDITION("Unable to obtain allocator from SurfaceFlinger");
1882 allocator = sc_reply.readStrongBinder();
1883
1884 if (allocator == NULL) {
1885 ALOGE("Unable to obtain an ISurfaceComposer");
1886 goto error_condition;
1887 }
1888 data.writeInterfaceToken(String16("android.ui.IGraphicBufferAlloc"));
1889 err = data.writeUint32(width);
1890 CHECK_ERROR_CONDITION("Unable to write width");
1891 err = data.writeUint32(height);
1892 CHECK_ERROR_CONDITION("Unable to write height");
1893 err = data.writeInt32(static_cast<int32_t>(format));
1894 CHECK_ERROR_CONDITION("Unable to write format");
1895 err = data.writeUint32(usage);
1896 CHECK_ERROR_CONDITION("Unable to write usage");
Dan Stozad4079af2016-08-22 17:26:41 -07001897 err = data.writeUtf8AsUtf16(
1898 std::string("[eglCreateNativeClientBufferANDROID pid ") +
1899 std::to_string(getpid()) + ']');
1900 CHECK_ERROR_CONDITION("Unable to write requestor name");
Craig Donner68671532016-07-18 10:19:54 -07001901 err = allocator->transact(IBinder::FIRST_CALL_TRANSACTION, data,
1902 &reply);
1903 CHECK_ERROR_CONDITION(
1904 "Unable to request buffer allocation from surface composer");
1905 err = reply.readInt32();
1906 CHECK_ERROR_CONDITION("Unable to obtain buffer from surface composer");
1907 err = reply.read(*gBuffer);
1908 CHECK_ERROR_CONDITION("Unable to read buffer from surface composer");
1909
1910 err = gBuffer->initCheck();
Craig Donner05249fc2016-01-15 19:33:55 -08001911 if (err != NO_ERROR) {
1912 ALOGE("Unable to create native buffer { w=%d, h=%d, f=%d, u=%#x }: %#x",
1913 width, height, format, usage, err);
Craig Donner68671532016-07-18 10:19:54 -07001914 goto error_condition;
Craig Donner05249fc2016-01-15 19:33:55 -08001915 }
1916 ALOGD("Created new native buffer %p { w=%d, h=%d, f=%d, u=%#x }",
1917 gBuffer, width, height, format, usage);
1918 return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
Craig Donner68671532016-07-18 10:19:54 -07001919
1920#undef CHECK_ERROR_CONDITION
1921
1922error_condition:
1923 // Delete the buffer.
1924 sp<GraphicBuffer> holder(gBuffer);
1925 return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
Craig Donner05249fc2016-01-15 19:33:55 -08001926}
1927
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001928// ----------------------------------------------------------------------------
1929// NVIDIA extensions
1930// ----------------------------------------------------------------------------
1931EGLuint64NV eglGetSystemTimeFrequencyNV()
1932{
1933 clearError();
1934
1935 if (egl_init_drivers() == EGL_FALSE) {
1936 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1937 }
1938
1939 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001940 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001941
Mathias Agopianada798b2012-02-13 17:09:30 -08001942 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1943 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001944 }
1945
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001946 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001947}
1948
1949EGLuint64NV eglGetSystemTimeNV()
1950{
1951 clearError();
1952
1953 if (egl_init_drivers() == EGL_FALSE) {
1954 return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1955 }
1956
1957 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08001958 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001959
Mathias Agopianada798b2012-02-13 17:09:30 -08001960 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1961 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001962 }
1963
Mathias Agopian0e8bbee2011-10-05 19:15:05 -07001964 return setErrorQuiet(EGL_BAD_DISPLAY, 0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08001965}
Dan Stozaa894d082015-02-19 15:27:36 -08001966
1967// ----------------------------------------------------------------------------
1968// Partial update extension
1969// ----------------------------------------------------------------------------
1970EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1971 EGLint *rects, EGLint n_rects)
1972{
1973 clearError();
1974
1975 const egl_display_ptr dp = validate_display(dpy);
1976 if (!dp) {
1977 setError(EGL_BAD_DISPLAY, EGL_FALSE);
1978 return EGL_FALSE;
1979 }
1980
1981 SurfaceRef _s(dp.get(), surface);
1982 if (!_s.get()) {
1983 setError(EGL_BAD_SURFACE, EGL_FALSE);
1984 return EGL_FALSE;
1985 }
1986
1987 egl_surface_t const * const s = get_surface(surface);
1988 if (s->cnx->egl.eglSetDamageRegionKHR) {
1989 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1990 rects, n_rects);
1991 }
1992
1993 return EGL_FALSE;
1994}
Pablo Ceballosc18be292016-05-31 14:55:42 -07001995
1996EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
1997 EGLint framesAgo, EGLint numTimestamps, const EGLint *timestamps,
1998 EGLnsecsANDROID *values)
1999{
2000 clearError();
2001
2002 const egl_display_ptr dp = validate_display(dpy);
2003 if (!dp) {
2004 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2005 return EGL_FALSE;
2006 }
2007
2008 SurfaceRef _s(dp.get(), surface);
2009 if (!_s.get()) {
2010 setError(EGL_BAD_SURFACE, EGL_FALSE);
2011 return EGL_FALSE;
2012 }
2013
2014 egl_surface_t const * const s = get_surface(surface);
2015
2016 if (!s->enableTimestamps) {
2017 setError(EGL_BAD_SURFACE, EGL_FALSE);
2018 return EGL_FALSE;
2019 }
2020
2021 nsecs_t* postedTime = nullptr;
2022 nsecs_t* acquireTime = nullptr;
2023 nsecs_t* refreshStartTime = nullptr;
2024 nsecs_t* GLCompositionDoneTime = nullptr;
2025 nsecs_t* displayRetireTime = nullptr;
2026 nsecs_t* releaseTime = nullptr;
2027
2028 for (int i = 0; i < numTimestamps; i++) {
2029 switch (timestamps[i]) {
2030 case EGL_QUEUE_TIME_ANDROID:
2031 postedTime = &values[i];
2032 break;
2033 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2034 acquireTime = &values[i];
2035 break;
2036 case EGL_COMPOSITION_START_TIME_ANDROID:
2037 refreshStartTime = &values[i];
2038 break;
2039 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
2040 GLCompositionDoneTime = &values[i];
2041 break;
2042 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2043 displayRetireTime = &values[i];
2044 break;
2045 case EGL_READS_DONE_TIME_ANDROID:
2046 releaseTime = &values[i];
2047 break;
2048 default:
2049 setError(EGL_BAD_PARAMETER, EGL_FALSE);
2050 return EGL_FALSE;
2051 }
2052 }
2053
2054 status_t ret = native_window_get_frame_timestamps(s->win.get(), framesAgo,
2055 postedTime, acquireTime, refreshStartTime, GLCompositionDoneTime,
2056 displayRetireTime, releaseTime);
2057
2058 if (ret != NO_ERROR) {
2059 setError(EGL_BAD_ACCESS, EGL_FALSE);
2060 return EGL_FALSE;
2061 }
2062
2063 return EGL_TRUE;
2064}
2065
2066EGLBoolean eglQueryTimestampSupportedANDROID(EGLDisplay dpy, EGLSurface surface,
2067 EGLint timestamp)
2068{
2069 clearError();
2070
2071 const egl_display_ptr dp = validate_display(dpy);
2072 if (!dp) {
2073 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2074 return EGL_FALSE;
2075 }
2076
2077 SurfaceRef _s(dp.get(), surface);
2078 if (!_s.get()) {
2079 setError(EGL_BAD_SURFACE, EGL_FALSE);
2080 return EGL_FALSE;
2081 }
2082
2083 switch (timestamp) {
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002084#if ENABLE_EGL_ANDROID_GET_FRAME_TIMESTAMPS
Pablo Ceballosc18be292016-05-31 14:55:42 -07002085 case EGL_QUEUE_TIME_ANDROID:
2086 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2087 case EGL_COMPOSITION_START_TIME_ANDROID:
2088 case EGL_COMPOSITION_FINISHED_TIME_ANDROID:
2089 case EGL_DISPLAY_RETIRE_TIME_ANDROID:
2090 case EGL_READS_DONE_TIME_ANDROID:
2091 return EGL_TRUE;
Pablo Ceballosb1e2c722016-07-14 08:02:14 -07002092#endif
Pablo Ceballosc18be292016-05-31 14:55:42 -07002093 default:
2094 return EGL_FALSE;
2095 }
2096}