blob: 2fa36faf536fe5d1338e5037ac76f2e1b07f3a6e [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
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <ctype.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <dlfcn.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <stdlib.h>
22#include <string.h>
23
Craig Donnere96a3252017-02-02 12:13:34 -080024#include <hardware/gralloc1.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070025
26#include <EGL/egl.h>
27#include <EGL/eglext.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
Craig Donner60761072017-01-27 12:30:44 -080029#include <android/hardware_buffer.h>
Mathias Agopian89ed4c82017-02-09 18:48:34 -080030#include <private/android/AHardwareBufferHelpers.h>
31
Mathias Agopian7db993a2012-03-25 00:49:46 -070032#include <cutils/compiler.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070034#include <log/log.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070035
Mathias Agopian65421432017-03-08 11:49:05 -080036#include <condition_variable>
37#include <deque>
38#include <mutex>
39#include <unordered_map>
40#include <string>
41#include <thread>
Mathias Agopian518ec112011-05-13 16:21:08 -070042
Mathias Agopian39c24a22013-04-04 23:17:56 -070043#include "../egl_impl.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070044
45#include "egl_display.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070046#include "egl_object.h"
47#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080048#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070049
50using namespace android;
51
52// ----------------------------------------------------------------------------
53
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070054namespace android {
55
Mathias Agopian65421432017-03-08 11:49:05 -080056using nsecs_t = int64_t;
57
Mathias Agopian518ec112011-05-13 16:21:08 -070058struct extention_map_t {
59 const char* name;
60 __eglMustCastToProperFunctionPointerType address;
61};
62
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070063/*
Jesse Hall21558da2013-08-06 15:31:22 -070064 * This is the list of EGL extensions exposed to applications.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070065 *
Jesse Hall21558da2013-08-06 15:31:22 -070066 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
67 * wrapper and are always available.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070068 *
Jesse Hall21558da2013-08-06 15:31:22 -070069 * The rest (gExtensionString) depend on support in the EGL driver, and are
70 * only available if the driver supports them. However, some of these must be
71 * supported because they are used by the Android system itself; these are
Pablo Ceballos02b05da2016-02-02 17:53:18 -080072 * listed as mandatory below and are required by the CDD. The system *assumes*
Jesse Hall21558da2013-08-06 15:31:22 -070073 * the mandatory extensions are present and may not function properly if some
74 * are missing.
75 *
76 * NOTE: Both strings MUST have a single space as the last character.
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070077 */
Mathias Agopian737b8962017-02-24 14:32:05 -080078
Mathias Agopian311b4792017-02-28 15:00:49 -080079extern char const * const gBuiltinExtensionString;
80extern char const * const gExtensionString;
Mathias Agopian737b8962017-02-24 14:32:05 -080081
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -060082// clang-format off
Mathias Agopian311b4792017-02-28 15:00:49 -080083char const * const gBuiltinExtensionString =
Jesse Hall21558da2013-08-06 15:31:22 -070084 "EGL_KHR_get_all_proc_addresses "
85 "EGL_ANDROID_presentation_time "
Dan Stozaa894d082015-02-19 15:27:36 -080086 "EGL_KHR_swap_buffers_with_damage "
Craig Donner60761072017-01-27 12:30:44 -080087 "EGL_ANDROID_get_native_client_buffer "
Pablo Ceballos02b05da2016-02-02 17:53:18 -080088 "EGL_ANDROID_front_buffer_auto_refresh "
Pablo Ceballosc18be292016-05-31 14:55:42 -070089 "EGL_ANDROID_get_frame_timestamps "
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -060090 "EGL_EXT_gl_colorspace_scrgb "
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -060091 "EGL_EXT_gl_colorspace_scrgb_linear "
92 "EGL_EXT_gl_colorspace_display_p3_linear "
93 "EGL_EXT_gl_colorspace_display_p3 "
Jesse Hall21558da2013-08-06 15:31:22 -070094 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080095
96char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070097 "EGL_KHR_image " // mandatory
98 "EGL_KHR_image_base " // mandatory
99 "EGL_KHR_image_pixmap "
100 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -0700101 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700102 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -0700103 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700104 "EGL_KHR_gl_texture_cubemap_image "
105 "EGL_KHR_gl_renderbuffer_image "
106 "EGL_KHR_reusable_sync "
107 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700108 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700109 "EGL_KHR_config_attribs "
110 "EGL_KHR_surfaceless_context "
111 "EGL_KHR_stream "
112 "EGL_KHR_stream_fifo "
113 "EGL_KHR_stream_producer_eglsurface "
114 "EGL_KHR_stream_consumer_gltexture "
115 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700116 "EGL_EXT_create_context_robustness "
117 "EGL_NV_system_time "
118 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700119 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700120 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800121 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700122 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800123 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700124 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700125 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700126 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700127 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000128 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800129 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700130 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600131// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700132
133// extensions not exposed to applications but used by the ANDROID system
134// "EGL_ANDROID_blob_cache " // strongly recommended
135// "EGL_IMG_hibernate_process " // optional
136// "EGL_ANDROID_native_fence_sync " // strongly recommended
137// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700138// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700139
140/*
141 * EGL Extensions entry-points exposed to 3rd party applications
142 * (keep in sync with gExtensionString above)
143 *
144 */
145static const extention_map_t sExtensionMap[] = {
146 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700147 { "eglLockSurfaceKHR",
148 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
149 { "eglUnlockSurfaceKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700151
152 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700153 { "eglCreateImageKHR",
154 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
155 { "eglDestroyImageKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700157
158 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
159 { "eglCreateSyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
161 { "eglDestroySyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
163 { "eglClientWaitSyncKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
165 { "eglSignalSyncKHR",
166 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
167 { "eglGetSyncAttribKHR",
168 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
169
170 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800171 { "eglGetSystemTimeFrequencyNV",
172 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
173 { "eglGetSystemTimeNV",
174 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700175
Mathias Agopian2bb71682013-03-27 17:32:41 -0700176 // EGL_KHR_wait_sync
177 { "eglWaitSyncKHR",
178 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700179
180 // EGL_ANDROID_presentation_time
181 { "eglPresentationTimeANDROID",
182 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800183
184 // EGL_KHR_swap_buffers_with_damage
185 { "eglSwapBuffersWithDamageKHR",
186 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
187
Craig Donner60761072017-01-27 12:30:44 -0800188 // EGL_ANDROID_get_native_client_buffer
189 { "eglGetNativeClientBufferANDROID",
190 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
191
Dan Stozaa894d082015-02-19 15:27:36 -0800192 // EGL_KHR_partial_update
193 { "eglSetDamageRegionKHR",
194 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700195
196 { "eglCreateStreamKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
198 { "eglDestroyStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
200 { "eglStreamAttribKHR",
201 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
202 { "eglQueryStreamKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
204 { "eglQueryStreamu64KHR",
205 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
206 { "eglQueryStreamTimeKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
208 { "eglCreateStreamProducerSurfaceKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
210 { "eglStreamConsumerGLTextureExternalKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
212 { "eglStreamConsumerAcquireKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
214 { "eglStreamConsumerReleaseKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
216 { "eglGetStreamFileDescriptorKHR",
217 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
218 { "eglCreateStreamFromFileDescriptorKHR",
219 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700220
221 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800222 { "eglGetNextFrameIdANDROID",
223 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800224 { "eglGetCompositorTimingANDROID",
225 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
226 { "eglGetCompositorTimingSupportedANDROID",
227 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700228 { "eglGetFrameTimestampsANDROID",
229 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800230 { "eglGetFrameTimestampSupportedANDROID",
231 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700232
233 // EGL_ANDROID_native_fence_sync
234 { "eglDupNativeFenceFDANDROID",
235 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700236};
237
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700238/*
239 * These extensions entry-points should not be exposed to applications.
240 * They're used internally by the Android EGL layer.
241 */
242#define FILTER_EXTENSIONS(procname) \
243 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
244 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700245 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700246
247
248
Mathias Agopian518ec112011-05-13 16:21:08 -0700249// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800250static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
251
Mathias Agopian518ec112011-05-13 16:21:08 -0700252static int sGLExtentionSlot = 0;
253static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
254
255static void(*findProcAddress(const char* name,
256 const extention_map_t* map, size_t n))() {
257 for (uint32_t i=0 ; i<n ; i++) {
258 if (!strcmp(name, map[i].name)) {
259 return map[i].address;
260 }
261 }
262 return NULL;
263}
264
265// ----------------------------------------------------------------------------
266
Mathias Agopian518ec112011-05-13 16:21:08 -0700267extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
268extern EGLBoolean egl_init_drivers();
269extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700270extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700271
Mathias Agopian518ec112011-05-13 16:21:08 -0700272} // namespace android;
273
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700274
Mathias Agopian518ec112011-05-13 16:21:08 -0700275// ----------------------------------------------------------------------------
276
277static inline void clearError() { egl_tls_t::clearError(); }
278static inline EGLContext getContext() { return egl_tls_t::getContext(); }
279
280// ----------------------------------------------------------------------------
281
282EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
283{
Jesse Hall1508ae62017-01-19 17:43:26 -0800284 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700285 clearError();
286
Dan Stozac3289c42014-01-17 11:38:34 -0800287 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700288 if (index >= NUM_DISPLAYS) {
289 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
290 }
291
292 if (egl_init_drivers() == EGL_FALSE) {
293 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
294 }
295
296 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
297 return dpy;
298}
299
300// ----------------------------------------------------------------------------
301// Initialization
302// ----------------------------------------------------------------------------
303
304EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
305{
306 clearError();
307
Jesse Hallb29e5e82012-04-04 16:53:42 -0700308 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800309 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700310
311 EGLBoolean res = dp->initialize(major, minor);
312
313 return res;
314}
315
316EGLBoolean eglTerminate(EGLDisplay dpy)
317{
318 // NOTE: don't unload the drivers b/c some APIs can be called
319 // after eglTerminate() has been called. eglTerminate() only
320 // terminates an EGLDisplay, not a EGL itself.
321
322 clearError();
323
Jesse Hallb29e5e82012-04-04 16:53:42 -0700324 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800325 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700326
327 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800328
Mathias Agopian518ec112011-05-13 16:21:08 -0700329 return res;
330}
331
332// ----------------------------------------------------------------------------
333// configuration
334// ----------------------------------------------------------------------------
335
336EGLBoolean eglGetConfigs( EGLDisplay dpy,
337 EGLConfig *configs,
338 EGLint config_size, EGLint *num_config)
339{
340 clearError();
341
Jesse Hallb29e5e82012-04-04 16:53:42 -0700342 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700343 if (!dp) return EGL_FALSE;
344
Mathias Agopian7773c432012-02-13 20:06:08 -0800345 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800346 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700347 }
348
Mathias Agopian7773c432012-02-13 20:06:08 -0800349 EGLBoolean res = EGL_FALSE;
350 *num_config = 0;
351
352 egl_connection_t* const cnx = &gEGLImpl;
353 if (cnx->dso) {
354 res = cnx->egl.eglGetConfigs(
355 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700356 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800357
358 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700359}
360
361EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
362 EGLConfig *configs, EGLint config_size,
363 EGLint *num_config)
364{
365 clearError();
366
Jesse Hallb29e5e82012-04-04 16:53:42 -0700367 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700368 if (!dp) return EGL_FALSE;
369
370 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800371 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700372 }
373
Mathias Agopian518ec112011-05-13 16:21:08 -0700374 EGLBoolean res = EGL_FALSE;
375 *num_config = 0;
376
Mathias Agopianada798b2012-02-13 17:09:30 -0800377 egl_connection_t* const cnx = &gEGLImpl;
378 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700379 if (attrib_list) {
380 char value[PROPERTY_VALUE_MAX];
381 property_get("debug.egl.force_msaa", value, "false");
382
383 if (!strcmp(value, "true")) {
384 size_t attribCount = 0;
385 EGLint attrib = attrib_list[0];
386
387 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700388 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700389 const EGLint *attribRendererable = NULL;
390 const EGLint *attribCaveat = NULL;
391
392 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700393 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700394 while (attrib != EGL_NONE) {
395 attrib = attrib_list[attribCount];
396 switch (attrib) {
397 case EGL_RENDERABLE_TYPE:
398 attribRendererable = &attrib_list[attribCount];
399 break;
400 case EGL_CONFIG_CAVEAT:
401 attribCaveat = &attrib_list[attribCount];
402 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800403 default:
404 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700405 }
406 attribCount++;
407 }
408
409 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
410 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800411
Romain Guy1cffc802012-10-15 18:13:05 -0700412 // Insert 2 extra attributes to force-enable MSAA 4x
413 EGLint aaAttribs[attribCount + 4];
414 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
415 aaAttribs[1] = 1;
416 aaAttribs[2] = EGL_SAMPLES;
417 aaAttribs[3] = 4;
418
419 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
420
421 EGLint numConfigAA;
422 EGLBoolean resAA = cnx->egl.eglChooseConfig(
423 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
424
425 if (resAA == EGL_TRUE && numConfigAA > 0) {
426 ALOGD("Enabling MSAA 4x");
427 *num_config = numConfigAA;
428 return resAA;
429 }
430 }
431 }
432 }
433
Mathias Agopian7773c432012-02-13 20:06:08 -0800434 res = cnx->egl.eglChooseConfig(
435 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700436 }
437 return res;
438}
439
440EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
441 EGLint attribute, EGLint *value)
442{
443 clearError();
444
Jesse Hallb29e5e82012-04-04 16:53:42 -0700445 egl_connection_t* cnx = NULL;
446 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
447 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800448
Mathias Agopian518ec112011-05-13 16:21:08 -0700449 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800450 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700451}
452
453// ----------------------------------------------------------------------------
454// surfaces
455// ----------------------------------------------------------------------------
456
Jesse Hallc2e41222013-08-08 13:40:22 -0700457// Turn linear formats into corresponding sRGB formats when colorspace is
458// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
459// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800460// the modification isn't possible, the original dataSpace is returned.
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600461static android_dataspace modifyBufferDataspace(android_dataspace dataSpace,
462 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700463 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800464 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700465 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800466 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600467 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
468 return HAL_DATASPACE_DISPLAY_P3;
469 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
470 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600471 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
472 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600473 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
474 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700475 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800476 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700477}
478
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600479// Return true if we stripped any EGL_GL_COLORSPACE_KHR attributes.
480static EGLBoolean stripColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
481 EGLint format,
482 std::vector<EGLint>& stripped_attrib_list) {
483 std::vector<EGLint> allowedColorSpaces;
484 switch (format) {
485 case HAL_PIXEL_FORMAT_RGBA_8888:
486 case HAL_PIXEL_FORMAT_RGB_565:
487 // driver okay with linear & sRGB for 8888, but can't handle
488 // Display-P3 or other spaces.
489 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
490 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
491 break;
492
493 case HAL_PIXEL_FORMAT_RGBA_FP16:
494 case HAL_PIXEL_FORMAT_RGBA_1010102:
495 default:
496 // driver does not want to see colorspace attributes for 1010102 or fp16.
497 // Future: if driver supports XXXX extension, we can pass down that colorspace
498 break;
499 }
500
501 bool stripped = false;
502 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
503 for (const EGLint* attr = attrib_list; attr[0] != EGL_NONE; attr += 2) {
504 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
505 EGLint colorSpace = attr[1];
506 bool found = false;
507 // Verify that color space is allowed
508 for (auto it : allowedColorSpaces) {
509 if (colorSpace == it) {
510 found = true;
511 }
512 }
513 if (!found) {
514 stripped = true;
515 } else {
516 stripped_attrib_list.push_back(attr[0]);
517 stripped_attrib_list.push_back(attr[1]);
518 }
519 } else {
520 stripped_attrib_list.push_back(attr[0]);
521 stripped_attrib_list.push_back(attr[1]);
522 }
523 }
524 }
525 if (stripped) {
526 stripped_attrib_list.push_back(EGL_NONE);
527 stripped_attrib_list.push_back(EGL_NONE);
528 }
529 return stripped;
530}
531
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600532static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
533 EGLint& colorSpace, android_dataspace& dataSpace) {
534 colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
535 dataSpace = HAL_DATASPACE_UNKNOWN;
536 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
537 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
538 if (*attr == EGL_GL_COLORSPACE_KHR) {
539 colorSpace = attr[1];
540 bool found = false;
541 // Verify that color space is allowed
542 if (colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
543 colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR) {
544 found = true;
545 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_linear &&
546 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
547 found = true;
548 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_pq &&
549 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
550 found = true;
Courtney Goeltzenleuchter33e2b782017-06-23 09:06:52 -0600551 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_EXT &&
552 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb")) {
553 found = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600554 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
555 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
556 found = true;
557 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
558 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
559 found = true;
560 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
561 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
562 found = true;
563 }
564 if (!found) {
565 return false;
566 }
Courtney Goeltzenleuchter1d4f7a22017-05-05 13:30:25 -0600567 // Only change the dataSpace from default if the application
568 // has explicitly set the color space with a EGL_GL_COLORSPACE_KHR attribute.
569 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600570 }
571 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600572 }
573 return true;
574}
575
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600576void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config, EGLint& format) {
577 // Set the native window's buffers format to match what this config requests.
578 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
579 // of our native format. So if sRGB gamma is requested, we have to
580 // modify the EGLconfig's format before setting the native window's
581 // format.
582
583 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
584 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
585
586 EGLint a = 0;
587 EGLint r, g, b;
588 r = g = b = 0;
589 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
590 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
591 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
592 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
593 EGLint colorDepth = r + g + b;
594
595 // Today, the driver only understands sRGB and linear on 888X
596 // formats. Strip other colorspaces from the attribute list and
597 // only use them to set the dataspace via
598 // native_window_set_buffers_dataspace
599 // if pixel format is RGBX 8888
600 // TBD: Can test for future extensions that indicate that driver
601 // handles requested color space and we can let it through.
602 // allow SRGB and LINEAR. All others need to be stripped.
603 // else if 565, 4444
604 // TBD: Can we assume these are supported if 8888 is?
605 // else if FP16 or 1010102
606 // strip colorspace from attribs.
607 // endif
608 if (a == 0) {
609 if (colorDepth <= 16) {
610 format = HAL_PIXEL_FORMAT_RGB_565;
611 } else {
612 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
613 if (colorDepth > 24) {
614 format = HAL_PIXEL_FORMAT_RGBA_1010102;
615 } else {
616 format = HAL_PIXEL_FORMAT_RGBX_8888;
617 }
618 } else {
619 format = HAL_PIXEL_FORMAT_RGBA_FP16;
620 }
621 }
622 } else {
623 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
624 if (colorDepth > 24) {
625 format = HAL_PIXEL_FORMAT_RGBA_1010102;
626 } else {
627 format = HAL_PIXEL_FORMAT_RGBA_8888;
628 }
629 } else {
630 format = HAL_PIXEL_FORMAT_RGBA_FP16;
631 }
632 }
633}
634
Mathias Agopian518ec112011-05-13 16:21:08 -0700635EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
636 NativeWindowType window,
637 const EGLint *attrib_list)
638{
639 clearError();
640
Jesse Hallb29e5e82012-04-04 16:53:42 -0700641 egl_connection_t* cnx = NULL;
642 egl_display_ptr dp = validate_display_connection(dpy, cnx);
643 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800644 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700645
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800646 if (!window) {
647 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
648 }
649
650 int value = 0;
651 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
652 if (!value) {
653 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
654 }
655
Andy McFaddend566ce32014-01-07 15:54:17 -0800656 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800657 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800658 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
659 "failed (%#x) (already connected to another API?)",
660 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700661 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700662 }
663
Mathias Agopian95921562017-02-24 14:31:31 -0800664 EGLint format;
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600665 getNativePixelFormat(iDpy, cnx, config, format);
666
667 // now select correct colorspace and dataspace based on user's attribute list
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600668 EGLint colorSpace;
669 android_dataspace dataSpace;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600670 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
671 ALOGE("error invalid colorspace: %d", colorSpace);
672 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700673 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800674
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600675 std::vector<EGLint> strippedAttribList;
676 if (stripColorSpaceAttribute(dp, attrib_list, format, strippedAttribList)) {
677 // Had to modify the attribute list due to use of color space.
678 // Use modified list from here on.
679 attrib_list = strippedAttribList.data();
680 }
681
Jesse Hallc2e41222013-08-08 13:40:22 -0700682 if (format != 0) {
683 int err = native_window_set_buffers_format(window, format);
684 if (err != 0) {
685 ALOGE("error setting native window pixel format: %s (%d)",
686 strerror(-err), err);
687 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
688 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
689 }
690 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700691
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800692 if (dataSpace != 0) {
693 int err = native_window_set_buffers_data_space(window, dataSpace);
694 if (err != 0) {
695 ALOGE("error setting native window pixel dataSpace: %s (%d)",
696 strerror(-err), err);
697 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
698 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
699 }
700 }
701
Jamie Gennis59769462011-11-19 18:04:43 -0800702 // the EGL spec requires that a new EGLSurface default to swap interval
703 // 1, so explicitly set that on the window here.
704 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
705 anw->setSwapInterval(anw, 1);
706
Mathias Agopian518ec112011-05-13 16:21:08 -0700707 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800708 iDpy, config, window, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700709 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600710 egl_surface_t* s =
711 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700712 return s;
713 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700714
715 // EGLSurface creation failed
716 native_window_set_buffers_format(window, 0);
717 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700718 }
719 return EGL_NO_SURFACE;
720}
721
722EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
723 NativePixmapType pixmap,
724 const EGLint *attrib_list)
725{
726 clearError();
727
Jesse Hallb29e5e82012-04-04 16:53:42 -0700728 egl_connection_t* cnx = NULL;
729 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600730 EGLint colorSpace;
731 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700732 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600733 // now select a corresponding sRGB format if needed
734 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
735 ALOGE("error invalid colorspace: %d", colorSpace);
736 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
737 }
738
Mathias Agopian518ec112011-05-13 16:21:08 -0700739 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800740 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700741 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600742 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700743 return s;
744 }
745 }
746 return EGL_NO_SURFACE;
747}
748
749EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
750 const EGLint *attrib_list)
751{
752 clearError();
753
Jesse Hallb29e5e82012-04-04 16:53:42 -0700754 egl_connection_t* cnx = NULL;
755 egl_display_ptr dp = validate_display_connection(dpy, cnx);
756 if (dp) {
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600757 EGLDisplay iDpy = dp->disp.dpy;
758 EGLint format;
759 getNativePixelFormat(iDpy, cnx, config, format);
760
761 // now select correct colorspace and dataspace based on user's attribute list
762 EGLint colorSpace;
763 android_dataspace dataSpace;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600764 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
765 ALOGE("error invalid colorspace: %d", colorSpace);
766 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
767 }
768
Courtney Goeltzenleuchtereeaa52b2017-06-20 08:12:54 -0600769 // Pbuffers are not displayed so we don't need to store the
770 // colorspace. We do need to filter out color spaces the
771 // driver doesn't know how to process.
772 std::vector<EGLint> strippedAttribList;
773 if (stripColorSpaceAttribute(dp, attrib_list, format, strippedAttribList)) {
774 // Had to modify the attribute list due to use of color space.
775 // Use modified list from here on.
776 attrib_list = strippedAttribList.data();
777 }
778
Mathias Agopian518ec112011-05-13 16:21:08 -0700779 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800780 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700781 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600782 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700783 return s;
784 }
785 }
786 return EGL_NO_SURFACE;
787}
Jesse Hall47743382013-02-08 11:13:46 -0800788
Mathias Agopian518ec112011-05-13 16:21:08 -0700789EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
790{
791 clearError();
792
Jesse Hallb29e5e82012-04-04 16:53:42 -0700793 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700794 if (!dp) return EGL_FALSE;
795
Jesse Hallb29e5e82012-04-04 16:53:42 -0700796 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700797 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800798 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700799
800 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800801 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700802 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700803 _s.terminate();
804 }
805 return result;
806}
807
808EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
809 EGLint attribute, EGLint *value)
810{
811 clearError();
812
Jesse Hallb29e5e82012-04-04 16:53:42 -0700813 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700814 if (!dp) return EGL_FALSE;
815
Jesse Hallb29e5e82012-04-04 16:53:42 -0700816 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700817 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800818 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700819
Mathias Agopian518ec112011-05-13 16:21:08 -0700820 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600821 if (attribute == EGL_GL_COLORSPACE_KHR) {
822 *value = s->getColorSpace();
823 return EGL_TRUE;
824 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800825 return s->cnx->egl.eglQuerySurface(
826 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700827}
828
Jamie Gennise8696a42012-01-15 18:54:57 -0800829void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800830 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800831 clearError();
832
Jesse Hallb29e5e82012-04-04 16:53:42 -0700833 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800834 if (!dp) {
835 return;
836 }
837
Jesse Hallb29e5e82012-04-04 16:53:42 -0700838 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800839 if (!_s.get()) {
840 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800841 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800842}
843
Mathias Agopian518ec112011-05-13 16:21:08 -0700844// ----------------------------------------------------------------------------
845// Contexts
846// ----------------------------------------------------------------------------
847
848EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
849 EGLContext share_list, const EGLint *attrib_list)
850{
851 clearError();
852
Jesse Hallb29e5e82012-04-04 16:53:42 -0700853 egl_connection_t* cnx = NULL;
854 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700855 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700856 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700857 if (!ContextRef(dp.get(), share_list).get()) {
858 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
859 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700860 egl_context_t* const c = get_context(share_list);
861 share_list = c->context;
862 }
863 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800864 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700865 if (context != EGL_NO_CONTEXT) {
866 // figure out if it's a GLESv1 or GLESv2
867 int version = 0;
868 if (attrib_list) {
869 while (*attrib_list != EGL_NONE) {
870 GLint attr = *attrib_list++;
871 GLint value = *attrib_list++;
872 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
873 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800874 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800875 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800876 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700877 }
878 }
879 };
880 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700881 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
882 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700883 return c;
884 }
885 }
886 return EGL_NO_CONTEXT;
887}
888
889EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
890{
891 clearError();
892
Jesse Hallb29e5e82012-04-04 16:53:42 -0700893 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700894 if (!dp)
895 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700896
Jesse Hallb29e5e82012-04-04 16:53:42 -0700897 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700898 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800899 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800900
Mathias Agopian518ec112011-05-13 16:21:08 -0700901 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800902 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700903 if (result == EGL_TRUE) {
904 _c.terminate();
905 }
906 return result;
907}
908
Mathias Agopian518ec112011-05-13 16:21:08 -0700909EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
910 EGLSurface read, EGLContext ctx)
911{
912 clearError();
913
Jesse Hallb29e5e82012-04-04 16:53:42 -0700914 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800915 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700916
Mathias Agopian5b287a62011-05-16 18:58:55 -0700917 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
918 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
919 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700920 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
921 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800922 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700923 }
924
925 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700926 ContextRef _c(dp.get(), ctx);
927 SurfaceRef _d(dp.get(), draw);
928 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700929
930 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700931 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700932 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800933 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700934 }
935
936 // these are the underlying implementation's object
937 EGLContext impl_ctx = EGL_NO_CONTEXT;
938 EGLSurface impl_draw = EGL_NO_SURFACE;
939 EGLSurface impl_read = EGL_NO_SURFACE;
940
941 // these are our objects structs passed in
942 egl_context_t * c = NULL;
943 egl_surface_t const * d = NULL;
944 egl_surface_t const * r = NULL;
945
946 // these are the current objects structs
947 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800948
Mathias Agopian518ec112011-05-13 16:21:08 -0700949 if (ctx != EGL_NO_CONTEXT) {
950 c = get_context(ctx);
951 impl_ctx = c->context;
952 } else {
953 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700954 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
955 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800956 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700957 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700958 if (cur_c == NULL) {
959 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700960 // not an error, there is just no current context.
961 return EGL_TRUE;
962 }
963 }
964
965 // retrieve the underlying implementation's draw EGLSurface
966 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800967 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700968 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -0700969 impl_draw = d->surface;
970 }
971
972 // retrieve the underlying implementation's read EGLSurface
973 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800974 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700975 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700976 impl_read = r->surface;
977 }
978
Mathias Agopian518ec112011-05-13 16:21:08 -0700979
Jesse Hallb29e5e82012-04-04 16:53:42 -0700980 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800981 draw, read, ctx,
982 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700983
984 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -0800985 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700986 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
987 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700988 _c.acquire();
989 _r.acquire();
990 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -0700991 } else {
992 setGLHooksThreadSpecific(&gHooksNoContext);
993 egl_tls_t::setContext(EGL_NO_CONTEXT);
994 }
Mathias Agopian5fecea72011-08-25 18:38:24 -0700995 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -0600996
Mike Stroyan47e741b2017-06-01 13:56:18 -0600997 if (cur_c != NULL) {
998 // Force return to current context for drivers that cannot handle errors
999 EGLBoolean restore_result = EGL_FALSE;
1000 // get a reference to the old current objects
1001 ContextRef _c2(dp.get(), cur_c);
1002 SurfaceRef _d2(dp.get(), cur_c->draw);
1003 SurfaceRef _r2(dp.get(), cur_c->read);
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001004
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001005 c = cur_c;
1006 impl_ctx = c->context;
1007 impl_draw = EGL_NO_SURFACE;
1008 if (cur_c->draw != EGL_NO_SURFACE) {
1009 d = get_surface(cur_c->draw);
1010 impl_draw = d->surface;
1011 }
1012 impl_read = EGL_NO_SURFACE;
1013 if (cur_c->read != EGL_NO_SURFACE) {
1014 r = get_surface(cur_c->read);
1015 impl_read = r->surface;
1016 }
1017 restore_result = dp->makeCurrent(c, cur_c,
1018 cur_c->draw, cur_c->read, cur_c->context,
1019 impl_draw, impl_read, impl_ctx);
Mike Stroyan47e741b2017-06-01 13:56:18 -06001020 if (restore_result == EGL_TRUE) {
1021 _c2.acquire();
1022 _r2.acquire();
1023 _d2.acquire();
1024 } else {
1025 ALOGE("Could not restore original EGL context");
1026 }
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001027 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001028 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001029 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001030 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001031 }
1032 return result;
1033}
1034
1035
1036EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1037 EGLint attribute, EGLint *value)
1038{
1039 clearError();
1040
Jesse Hallb29e5e82012-04-04 16:53:42 -07001041 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001042 if (!dp) return EGL_FALSE;
1043
Jesse Hallb29e5e82012-04-04 16:53:42 -07001044 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001045 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001046
Mathias Agopian518ec112011-05-13 16:21:08 -07001047 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001048 return c->cnx->egl.eglQueryContext(
1049 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001050
Mathias Agopian518ec112011-05-13 16:21:08 -07001051}
1052
1053EGLContext eglGetCurrentContext(void)
1054{
1055 // could be called before eglInitialize(), but we wouldn't have a context
1056 // then, and this function would correctly return EGL_NO_CONTEXT.
1057
1058 clearError();
1059
1060 EGLContext ctx = getContext();
1061 return ctx;
1062}
1063
1064EGLSurface eglGetCurrentSurface(EGLint readdraw)
1065{
1066 // could be called before eglInitialize(), but we wouldn't have a context
1067 // then, and this function would correctly return EGL_NO_SURFACE.
1068
1069 clearError();
1070
1071 EGLContext ctx = getContext();
1072 if (ctx) {
1073 egl_context_t const * const c = get_context(ctx);
1074 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1075 switch (readdraw) {
1076 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001077 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001078 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1079 }
1080 }
1081 return EGL_NO_SURFACE;
1082}
1083
1084EGLDisplay eglGetCurrentDisplay(void)
1085{
1086 // could be called before eglInitialize(), but we wouldn't have a context
1087 // then, and this function would correctly return EGL_NO_DISPLAY.
1088
1089 clearError();
1090
1091 EGLContext ctx = getContext();
1092 if (ctx) {
1093 egl_context_t const * const c = get_context(ctx);
1094 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1095 return c->dpy;
1096 }
1097 return EGL_NO_DISPLAY;
1098}
1099
1100EGLBoolean eglWaitGL(void)
1101{
Mathias Agopian518ec112011-05-13 16:21:08 -07001102 clearError();
1103
Mathias Agopianada798b2012-02-13 17:09:30 -08001104 egl_connection_t* const cnx = &gEGLImpl;
1105 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001106 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001107
1108 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001109}
1110
1111EGLBoolean eglWaitNative(EGLint engine)
1112{
Mathias Agopian518ec112011-05-13 16:21:08 -07001113 clearError();
1114
Mathias Agopianada798b2012-02-13 17:09:30 -08001115 egl_connection_t* const cnx = &gEGLImpl;
1116 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001117 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001118
1119 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001120}
1121
1122EGLint eglGetError(void)
1123{
Mathias Agopianada798b2012-02-13 17:09:30 -08001124 EGLint err = EGL_SUCCESS;
1125 egl_connection_t* const cnx = &gEGLImpl;
1126 if (cnx->dso) {
1127 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001128 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001129 if (err == EGL_SUCCESS) {
1130 err = egl_tls_t::getError();
1131 }
1132 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001133}
1134
Michael Chockc0ec5e22014-01-27 08:14:33 -08001135static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001136 const char* procname) {
1137 const egl_connection_t* cnx = &gEGLImpl;
1138 void* proc = NULL;
1139
Michael Chockc0ec5e22014-01-27 08:14:33 -08001140 proc = dlsym(cnx->libEgl, procname);
1141 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1142
Jesse Hallc07b5202013-07-04 12:08:16 -07001143 proc = dlsym(cnx->libGles2, procname);
1144 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1145
1146 proc = dlsym(cnx->libGles1, procname);
1147 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1148
1149 return NULL;
1150}
1151
Mathias Agopian518ec112011-05-13 16:21:08 -07001152__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1153{
1154 // eglGetProcAddress() could be the very first function called
1155 // in which case we must make sure we've initialized ourselves, this
1156 // happens the first time egl_get_display() is called.
1157
1158 clearError();
1159
1160 if (egl_init_drivers() == EGL_FALSE) {
1161 setError(EGL_BAD_PARAMETER, NULL);
1162 return NULL;
1163 }
1164
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001165 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001166 return NULL;
1167 }
1168
Mathias Agopian518ec112011-05-13 16:21:08 -07001169 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001170 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001171 if (addr) return addr;
1172
Michael Chockc0ec5e22014-01-27 08:14:33 -08001173 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001174 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001175
Mathias Agopian518ec112011-05-13 16:21:08 -07001176 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1177 pthread_mutex_lock(&sExtensionMapMutex);
1178
1179 /*
1180 * Since eglGetProcAddress() is not associated to anything, it needs
1181 * to return a function pointer that "works" regardless of what
1182 * the current context is.
1183 *
1184 * For this reason, we return a "forwarder", a small stub that takes
1185 * care of calling the function associated with the context
1186 * currently bound.
1187 *
1188 * We first look for extensions we've already resolved, if we're seeing
1189 * this extension for the first time, we go through all our
1190 * implementations and call eglGetProcAddress() and record the
1191 * result in the appropriate implementation hooks and return the
1192 * address of the forwarder corresponding to that hook set.
1193 *
1194 */
1195
Mathias Agopian65421432017-03-08 11:49:05 -08001196 const std::string name(procname);
1197
1198 auto& extentionMap = sGLExtentionMap;
1199 auto pos = extentionMap.find(name);
1200 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001201 const int slot = sGLExtentionSlot;
1202
Steve Blocke6f43dd2012-01-06 19:20:56 +00001203 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001204 "no more slots for eglGetProcAddress(\"%s\")",
1205 procname);
1206
1207 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1208 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001209
1210 egl_connection_t* const cnx = &gEGLImpl;
1211 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001212 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001213 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001214 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1215 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001216 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001217 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001218 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001219
Mathias Agopian518ec112011-05-13 16:21:08 -07001220 if (found) {
1221 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001222 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001223 sGLExtentionSlot++;
1224 }
1225 }
1226
1227 pthread_mutex_unlock(&sExtensionMapMutex);
1228 return addr;
1229}
1230
Mathias Agopian65421432017-03-08 11:49:05 -08001231class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001232public:
1233
1234 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001235 static FrameCompletionThread thread;
1236
1237 char name[64];
1238
1239 std::lock_guard<std::mutex> lock(thread.mMutex);
1240 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1241 ATRACE_NAME(name);
1242
1243 thread.mQueue.push_back(sync);
1244 thread.mCondition.notify_one();
1245 thread.mFramesQueued++;
1246 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001247 }
1248
1249private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001250
Mathias Agopian65421432017-03-08 11:49:05 -08001251 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1252 std::thread thread(&FrameCompletionThread::loop, this);
1253 thread.detach();
1254 }
1255
1256#pragma clang diagnostic push
1257#pragma clang diagnostic ignored "-Wmissing-noreturn"
1258 void loop() {
1259 while (true) {
1260 threadLoop();
1261 }
1262 }
1263#pragma clang diagnostic pop
1264
1265 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001266 EGLSyncKHR sync;
1267 uint32_t frameNum;
1268 {
Mathias Agopian65421432017-03-08 11:49:05 -08001269 std::unique_lock<std::mutex> lock(mMutex);
1270 while (mQueue.empty()) {
1271 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001272 }
1273 sync = mQueue[0];
1274 frameNum = mFramesCompleted;
1275 }
1276 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1277 {
Mathias Agopian65421432017-03-08 11:49:05 -08001278 char name[64];
1279 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1280 ATRACE_NAME(name);
1281
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001282 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1283 if (result == EGL_FALSE) {
1284 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1285 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1286 ALOGE("FrameCompletion: timeout waiting for fence");
1287 }
1288 eglDestroySyncKHR(dpy, sync);
1289 }
1290 {
Mathias Agopian65421432017-03-08 11:49:05 -08001291 std::lock_guard<std::mutex> lock(mMutex);
1292 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001293 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001294 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001295 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001296 }
1297
1298 uint32_t mFramesQueued;
1299 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001300 std::deque<EGLSyncKHR> mQueue;
1301 std::condition_variable mCondition;
1302 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001303};
1304
Dan Stozaa894d082015-02-19 15:27:36 -08001305EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1306 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001307{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001308 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001309 clearError();
1310
Jesse Hallb29e5e82012-04-04 16:53:42 -07001311 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001312 if (!dp) return EGL_FALSE;
1313
Jesse Hallb29e5e82012-04-04 16:53:42 -07001314 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001315 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001316 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001317
Mathias Agopian518ec112011-05-13 16:21:08 -07001318 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001319
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001320 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1321 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1322 if (sync != EGL_NO_SYNC_KHR) {
1323 FrameCompletionThread::queueSync(sync);
1324 }
1325 }
1326
Mathias Agopian7db993a2012-03-25 00:49:46 -07001327 if (CC_UNLIKELY(dp->finishOnSwap)) {
1328 uint32_t pixel;
1329 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1330 if (c) {
1331 // glReadPixels() ensures that the frame is complete
1332 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1333 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1334 }
1335 }
1336
Dan Stozaa894d082015-02-19 15:27:36 -08001337 if (n_rects == 0) {
1338 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1339 }
1340
Mathias Agopian65421432017-03-08 11:49:05 -08001341 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001342 for (int r = 0; r < n_rects; ++r) {
1343 int offset = r * 4;
1344 int x = rects[offset];
1345 int y = rects[offset + 1];
1346 int width = rects[offset + 2];
1347 int height = rects[offset + 3];
1348 android_native_rect_t androidRect;
1349 androidRect.left = x;
1350 androidRect.top = y + height;
1351 androidRect.right = x + width;
1352 androidRect.bottom = y;
1353 androidRects.push_back(androidRect);
1354 }
Mathias Agopian65421432017-03-08 11:49:05 -08001355 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001356
1357 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1358 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1359 rects, n_rects);
1360 } else {
1361 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1362 }
1363}
1364
1365EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1366{
1367 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001368}
1369
1370EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1371 NativePixmapType target)
1372{
1373 clearError();
1374
Jesse Hallb29e5e82012-04-04 16:53:42 -07001375 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001376 if (!dp) return EGL_FALSE;
1377
Jesse Hallb29e5e82012-04-04 16:53:42 -07001378 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001379 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001380 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001381
Mathias Agopian518ec112011-05-13 16:21:08 -07001382 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001383 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001384}
1385
1386const char* eglQueryString(EGLDisplay dpy, EGLint name)
1387{
1388 clearError();
1389
Chia-I Wue57d1352016-08-15 16:10:02 +08001390 // Generate an error quietly when client extensions (as defined by
1391 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1392 // validate_display to generate the error as validate_display would log
1393 // the error, which can be misleading.
1394 //
1395 // If we want to support EGL_EXT_client_extensions later, we can return
1396 // the client extension string here instead.
1397 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001398 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001399
Jesse Hallb29e5e82012-04-04 16:53:42 -07001400 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001401 if (!dp) return (const char *) NULL;
1402
1403 switch (name) {
1404 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001405 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001406 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001407 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001408 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001409 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001410 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001411 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001412 default:
1413 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001414 }
1415 return setError(EGL_BAD_PARAMETER, (const char *)0);
1416}
1417
Mathias Agopianca088332013-03-28 17:44:13 -07001418EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1419{
1420 clearError();
1421
1422 const egl_display_ptr dp = validate_display(dpy);
1423 if (!dp) return (const char *) NULL;
1424
1425 switch (name) {
1426 case EGL_VENDOR:
1427 return dp->disp.queryString.vendor;
1428 case EGL_VERSION:
1429 return dp->disp.queryString.version;
1430 case EGL_EXTENSIONS:
1431 return dp->disp.queryString.extensions;
1432 case EGL_CLIENT_APIS:
1433 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001434 default:
1435 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001436 }
1437 return setError(EGL_BAD_PARAMETER, (const char *)0);
1438}
Mathias Agopian518ec112011-05-13 16:21:08 -07001439
1440// ----------------------------------------------------------------------------
1441// EGL 1.1
1442// ----------------------------------------------------------------------------
1443
1444EGLBoolean eglSurfaceAttrib(
1445 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1446{
1447 clearError();
1448
Jesse Hallb29e5e82012-04-04 16:53:42 -07001449 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001450 if (!dp) return EGL_FALSE;
1451
Jesse Hallb29e5e82012-04-04 16:53:42 -07001452 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001453 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001454 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001455
Pablo Ceballosc18be292016-05-31 14:55:42 -07001456 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001457
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001458 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001459 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001460 setError(EGL_BAD_SURFACE, EGL_FALSE);
1461 }
Mathias Agopian65421432017-03-08 11:49:05 -08001462 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1463 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001464 }
1465
Pablo Ceballosc18be292016-05-31 14:55:42 -07001466 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001467 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001468 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001469 }
Mathias Agopian65421432017-03-08 11:49:05 -08001470 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1471 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001472 }
1473
Mathias Agopian518ec112011-05-13 16:21:08 -07001474 if (s->cnx->egl.eglSurfaceAttrib) {
1475 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001476 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001477 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001478 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001479}
1480
1481EGLBoolean eglBindTexImage(
1482 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1483{
1484 clearError();
1485
Jesse Hallb29e5e82012-04-04 16:53:42 -07001486 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001487 if (!dp) return EGL_FALSE;
1488
Jesse Hallb29e5e82012-04-04 16:53:42 -07001489 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001490 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001491 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001492
Mathias Agopian518ec112011-05-13 16:21:08 -07001493 egl_surface_t const * const s = get_surface(surface);
1494 if (s->cnx->egl.eglBindTexImage) {
1495 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001496 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001497 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001498 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001499}
1500
1501EGLBoolean eglReleaseTexImage(
1502 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1503{
1504 clearError();
1505
Jesse Hallb29e5e82012-04-04 16:53:42 -07001506 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001507 if (!dp) return EGL_FALSE;
1508
Jesse Hallb29e5e82012-04-04 16:53:42 -07001509 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001510 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001511 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001512
Mathias Agopian518ec112011-05-13 16:21:08 -07001513 egl_surface_t const * const s = get_surface(surface);
1514 if (s->cnx->egl.eglReleaseTexImage) {
1515 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001516 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001517 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001518 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001519}
1520
1521EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1522{
1523 clearError();
1524
Jesse Hallb29e5e82012-04-04 16:53:42 -07001525 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001526 if (!dp) return EGL_FALSE;
1527
1528 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001529 egl_connection_t* const cnx = &gEGLImpl;
1530 if (cnx->dso && cnx->egl.eglSwapInterval) {
1531 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001532 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001533
Mathias Agopian518ec112011-05-13 16:21:08 -07001534 return res;
1535}
1536
1537
1538// ----------------------------------------------------------------------------
1539// EGL 1.2
1540// ----------------------------------------------------------------------------
1541
1542EGLBoolean eglWaitClient(void)
1543{
1544 clearError();
1545
Mathias Agopianada798b2012-02-13 17:09:30 -08001546 egl_connection_t* const cnx = &gEGLImpl;
1547 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001548 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001549
1550 EGLBoolean res;
1551 if (cnx->egl.eglWaitClient) {
1552 res = cnx->egl.eglWaitClient();
1553 } else {
1554 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001555 }
1556 return res;
1557}
1558
1559EGLBoolean eglBindAPI(EGLenum api)
1560{
1561 clearError();
1562
1563 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001564 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001565 }
1566
1567 // bind this API on all EGLs
1568 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001569 egl_connection_t* const cnx = &gEGLImpl;
1570 if (cnx->dso && cnx->egl.eglBindAPI) {
1571 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001572 }
1573 return res;
1574}
1575
1576EGLenum eglQueryAPI(void)
1577{
1578 clearError();
1579
1580 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001581 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001582 }
1583
Mathias Agopianada798b2012-02-13 17:09:30 -08001584 egl_connection_t* const cnx = &gEGLImpl;
1585 if (cnx->dso && cnx->egl.eglQueryAPI) {
1586 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001587 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001588
Mathias Agopian518ec112011-05-13 16:21:08 -07001589 // or, it can only be OpenGL ES
1590 return EGL_OPENGL_ES_API;
1591}
1592
1593EGLBoolean eglReleaseThread(void)
1594{
1595 clearError();
1596
Mathias Agopianada798b2012-02-13 17:09:30 -08001597 egl_connection_t* const cnx = &gEGLImpl;
1598 if (cnx->dso && cnx->egl.eglReleaseThread) {
1599 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001600 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301601
1602 // If there is context bound to the thread, release it
1603 egl_display_t::loseCurrent(get_context(getContext()));
1604
Mathias Agopian518ec112011-05-13 16:21:08 -07001605 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001606 return EGL_TRUE;
1607}
1608
1609EGLSurface eglCreatePbufferFromClientBuffer(
1610 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1611 EGLConfig config, const EGLint *attrib_list)
1612{
1613 clearError();
1614
Jesse Hallb29e5e82012-04-04 16:53:42 -07001615 egl_connection_t* cnx = NULL;
1616 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1617 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001618 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1619 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001620 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001621 }
1622 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1623}
1624
1625// ----------------------------------------------------------------------------
1626// EGL_EGLEXT_VERSION 3
1627// ----------------------------------------------------------------------------
1628
1629EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1630 const EGLint *attrib_list)
1631{
1632 clearError();
1633
Jesse Hallb29e5e82012-04-04 16:53:42 -07001634 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001635 if (!dp) return EGL_FALSE;
1636
Jesse Hallb29e5e82012-04-04 16:53:42 -07001637 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001638 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001639 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001640
1641 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001642 if (s->cnx->egl.eglLockSurfaceKHR) {
1643 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001644 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001645 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001646 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001647}
1648
1649EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1650{
1651 clearError();
1652
Jesse Hallb29e5e82012-04-04 16:53:42 -07001653 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001654 if (!dp) return EGL_FALSE;
1655
Jesse Hallb29e5e82012-04-04 16:53:42 -07001656 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001657 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001658 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001659
1660 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001661 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001662 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001663 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001664 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001665}
1666
1667EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1668 EGLClientBuffer buffer, const EGLint *attrib_list)
1669{
1670 clearError();
1671
Jesse Hallb29e5e82012-04-04 16:53:42 -07001672 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001673 if (!dp) return EGL_NO_IMAGE_KHR;
1674
Jesse Hallb29e5e82012-04-04 16:53:42 -07001675 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001676 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001677
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001678 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1679 egl_connection_t* const cnx = &gEGLImpl;
1680 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1681 result = cnx->egl.eglCreateImageKHR(
1682 dp->disp.dpy,
1683 c ? c->context : EGL_NO_CONTEXT,
1684 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001685 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001686 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001687}
1688
1689EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1690{
1691 clearError();
1692
Jesse Hallb29e5e82012-04-04 16:53:42 -07001693 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001694 if (!dp) return EGL_FALSE;
1695
Steven Holte646a5c52012-06-04 20:02:11 -07001696 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001697 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001698 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001699 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001700 }
Steven Holte646a5c52012-06-04 20:02:11 -07001701 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001702}
1703
1704// ----------------------------------------------------------------------------
1705// EGL_EGLEXT_VERSION 5
1706// ----------------------------------------------------------------------------
1707
1708
1709EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1710{
1711 clearError();
1712
Jesse Hallb29e5e82012-04-04 16:53:42 -07001713 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001714 if (!dp) return EGL_NO_SYNC_KHR;
1715
Mathias Agopian518ec112011-05-13 16:21:08 -07001716 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001717 egl_connection_t* const cnx = &gEGLImpl;
1718 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1719 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001720 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001721 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001722}
1723
1724EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1725{
1726 clearError();
1727
Jesse Hallb29e5e82012-04-04 16:53:42 -07001728 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001729 if (!dp) return EGL_FALSE;
1730
Mathias Agopian518ec112011-05-13 16:21:08 -07001731 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001732 egl_connection_t* const cnx = &gEGLImpl;
1733 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1734 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001735 }
1736 return result;
1737}
1738
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001739EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1740 clearError();
1741
1742 const egl_display_ptr dp = validate_display(dpy);
1743 if (!dp) return EGL_FALSE;
1744
1745 EGLBoolean result = EGL_FALSE;
1746 egl_connection_t* const cnx = &gEGLImpl;
1747 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1748 result = cnx->egl.eglSignalSyncKHR(
1749 dp->disp.dpy, sync, mode);
1750 }
1751 return result;
1752}
1753
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001754EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1755 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001756{
1757 clearError();
1758
Jesse Hallb29e5e82012-04-04 16:53:42 -07001759 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001760 if (!dp) return EGL_FALSE;
1761
Mathias Agopian737b8962017-02-24 14:32:05 -08001762 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001763 egl_connection_t* const cnx = &gEGLImpl;
1764 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1765 result = cnx->egl.eglClientWaitSyncKHR(
1766 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001767 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001768 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001769}
1770
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001771EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1772 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001773{
1774 clearError();
1775
Jesse Hallb29e5e82012-04-04 16:53:42 -07001776 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001777 if (!dp) return EGL_FALSE;
1778
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001779 EGLBoolean result = EGL_FALSE;
1780 egl_connection_t* const cnx = &gEGLImpl;
1781 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1782 result = cnx->egl.eglGetSyncAttribKHR(
1783 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001784 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001785 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001786}
1787
Season Li000d88f2015-07-01 11:39:40 -07001788EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1789{
1790 clearError();
1791
1792 const egl_display_ptr dp = validate_display(dpy);
1793 if (!dp) return EGL_NO_STREAM_KHR;
1794
1795 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1796 egl_connection_t* const cnx = &gEGLImpl;
1797 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1798 result = cnx->egl.eglCreateStreamKHR(
1799 dp->disp.dpy, attrib_list);
1800 }
1801 return result;
1802}
1803
1804EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1805{
1806 clearError();
1807
1808 const egl_display_ptr dp = validate_display(dpy);
1809 if (!dp) return EGL_FALSE;
1810
1811 EGLBoolean result = EGL_FALSE;
1812 egl_connection_t* const cnx = &gEGLImpl;
1813 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1814 result = cnx->egl.eglDestroyStreamKHR(
1815 dp->disp.dpy, stream);
1816 }
1817 return result;
1818}
1819
1820EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1821 EGLenum attribute, EGLint value)
1822{
1823 clearError();
1824
1825 const egl_display_ptr dp = validate_display(dpy);
1826 if (!dp) return EGL_FALSE;
1827
1828 EGLBoolean result = EGL_FALSE;
1829 egl_connection_t* const cnx = &gEGLImpl;
1830 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1831 result = cnx->egl.eglStreamAttribKHR(
1832 dp->disp.dpy, stream, attribute, value);
1833 }
1834 return result;
1835}
1836
1837EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1838 EGLenum attribute, EGLint *value)
1839{
1840 clearError();
1841
1842 const egl_display_ptr dp = validate_display(dpy);
1843 if (!dp) return EGL_FALSE;
1844
1845 EGLBoolean result = EGL_FALSE;
1846 egl_connection_t* const cnx = &gEGLImpl;
1847 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1848 result = cnx->egl.eglQueryStreamKHR(
1849 dp->disp.dpy, stream, attribute, value);
1850 }
1851 return result;
1852}
1853
1854EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1855 EGLenum attribute, EGLuint64KHR *value)
1856{
1857 clearError();
1858
1859 const egl_display_ptr dp = validate_display(dpy);
1860 if (!dp) return EGL_FALSE;
1861
1862 EGLBoolean result = EGL_FALSE;
1863 egl_connection_t* const cnx = &gEGLImpl;
1864 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1865 result = cnx->egl.eglQueryStreamu64KHR(
1866 dp->disp.dpy, stream, attribute, value);
1867 }
1868 return result;
1869}
1870
1871EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1872 EGLenum attribute, EGLTimeKHR *value)
1873{
1874 clearError();
1875
1876 const egl_display_ptr dp = validate_display(dpy);
1877 if (!dp) return EGL_FALSE;
1878
1879 EGLBoolean result = EGL_FALSE;
1880 egl_connection_t* const cnx = &gEGLImpl;
1881 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1882 result = cnx->egl.eglQueryStreamTimeKHR(
1883 dp->disp.dpy, stream, attribute, value);
1884 }
1885 return result;
1886}
1887
1888EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1889 EGLStreamKHR stream, const EGLint *attrib_list)
1890{
1891 clearError();
1892
1893 egl_display_ptr dp = validate_display(dpy);
1894 if (!dp) return EGL_NO_SURFACE;
1895
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06001896 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
1897 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
1898 // TODO: Probably need to update EGL_KHR_stream_producer_eglsurface to
1899 // indicate support for EGL_GL_COLORSPACE_KHR.
1900 // now select a corresponding sRGB format if needed
1901 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
1902 ALOGE("error invalid colorspace: %d", colorSpace);
1903 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1904 }
1905
Season Li000d88f2015-07-01 11:39:40 -07001906 egl_connection_t* const cnx = &gEGLImpl;
1907 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1908 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1909 dp->disp.dpy, config, stream, attrib_list);
1910 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06001911 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001912 return s;
1913 }
1914 }
1915 return EGL_NO_SURFACE;
1916}
1917
1918EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1919 EGLStreamKHR stream)
1920{
1921 clearError();
1922
1923 const egl_display_ptr dp = validate_display(dpy);
1924 if (!dp) return EGL_FALSE;
1925
1926 EGLBoolean result = EGL_FALSE;
1927 egl_connection_t* const cnx = &gEGLImpl;
1928 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1929 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1930 dp->disp.dpy, stream);
1931 }
1932 return result;
1933}
1934
1935EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1936 EGLStreamKHR stream)
1937{
1938 clearError();
1939
1940 const egl_display_ptr dp = validate_display(dpy);
1941 if (!dp) return EGL_FALSE;
1942
1943 EGLBoolean result = EGL_FALSE;
1944 egl_connection_t* const cnx = &gEGLImpl;
1945 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1946 result = cnx->egl.eglStreamConsumerAcquireKHR(
1947 dp->disp.dpy, stream);
1948 }
1949 return result;
1950}
1951
1952EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1953 EGLStreamKHR stream)
1954{
1955 clearError();
1956
1957 const egl_display_ptr dp = validate_display(dpy);
1958 if (!dp) return EGL_FALSE;
1959
1960 EGLBoolean result = EGL_FALSE;
1961 egl_connection_t* const cnx = &gEGLImpl;
1962 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1963 result = cnx->egl.eglStreamConsumerReleaseKHR(
1964 dp->disp.dpy, stream);
1965 }
1966 return result;
1967}
1968
1969EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1970 EGLDisplay dpy, EGLStreamKHR stream)
1971{
1972 clearError();
1973
1974 const egl_display_ptr dp = validate_display(dpy);
1975 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1976
1977 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1978 egl_connection_t* const cnx = &gEGLImpl;
1979 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1980 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1981 dp->disp.dpy, stream);
1982 }
1983 return result;
1984}
1985
1986EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1987 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1988{
1989 clearError();
1990
1991 const egl_display_ptr dp = validate_display(dpy);
1992 if (!dp) return EGL_NO_STREAM_KHR;
1993
1994 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1995 egl_connection_t* const cnx = &gEGLImpl;
1996 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1997 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1998 dp->disp.dpy, file_descriptor);
1999 }
2000 return result;
2001}
2002
Mathias Agopian518ec112011-05-13 16:21:08 -07002003// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002004// EGL_EGLEXT_VERSION 15
2005// ----------------------------------------------------------------------------
2006
2007EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2008 clearError();
2009 const egl_display_ptr dp = validate_display(dpy);
2010 if (!dp) return EGL_FALSE;
2011 EGLint result = EGL_FALSE;
2012 egl_connection_t* const cnx = &gEGLImpl;
2013 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2014 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2015 }
2016 return result;
2017}
2018
2019// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002020// ANDROID extensions
2021// ----------------------------------------------------------------------------
2022
Jamie Gennis331841b2012-09-06 14:52:00 -07002023EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2024{
2025 clearError();
2026
2027 const egl_display_ptr dp = validate_display(dpy);
2028 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2029
2030 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2031 egl_connection_t* const cnx = &gEGLImpl;
2032 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2033 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2034 }
2035 return result;
2036}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002037
Andy McFadden72841452013-03-01 16:25:32 -08002038EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2039 EGLnsecsANDROID time)
2040{
2041 clearError();
2042
2043 const egl_display_ptr dp = validate_display(dpy);
2044 if (!dp) {
2045 return EGL_FALSE;
2046 }
2047
2048 SurfaceRef _s(dp.get(), surface);
2049 if (!_s.get()) {
2050 setError(EGL_BAD_SURFACE, EGL_FALSE);
2051 return EGL_FALSE;
2052 }
2053
2054 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002055 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002056
2057 return EGL_TRUE;
2058}
2059
Craig Donner60761072017-01-27 12:30:44 -08002060EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2061 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002062 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2063 // this function cannot be implemented when this libEGL is built for
2064 // vendors.
2065#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002066 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002067 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002068#else
2069 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2070#endif
Craig Donner60761072017-01-27 12:30:44 -08002071}
2072
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002073// ----------------------------------------------------------------------------
2074// NVIDIA extensions
2075// ----------------------------------------------------------------------------
2076EGLuint64NV eglGetSystemTimeFrequencyNV()
2077{
2078 clearError();
2079
2080 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002081 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002082 }
2083
2084 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002085 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002086
Mathias Agopianada798b2012-02-13 17:09:30 -08002087 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2088 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002089 }
2090
Mathias Agopian737b8962017-02-24 14:32:05 -08002091 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002092}
2093
2094EGLuint64NV eglGetSystemTimeNV()
2095{
2096 clearError();
2097
2098 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002099 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002100 }
2101
2102 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002103 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002104
Mathias Agopianada798b2012-02-13 17:09:30 -08002105 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2106 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002107 }
2108
Mathias Agopian737b8962017-02-24 14:32:05 -08002109 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002110}
Dan Stozaa894d082015-02-19 15:27:36 -08002111
2112// ----------------------------------------------------------------------------
2113// Partial update extension
2114// ----------------------------------------------------------------------------
2115EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2116 EGLint *rects, EGLint n_rects)
2117{
2118 clearError();
2119
2120 const egl_display_ptr dp = validate_display(dpy);
2121 if (!dp) {
2122 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2123 return EGL_FALSE;
2124 }
2125
2126 SurfaceRef _s(dp.get(), surface);
2127 if (!_s.get()) {
2128 setError(EGL_BAD_SURFACE, EGL_FALSE);
2129 return EGL_FALSE;
2130 }
2131
2132 egl_surface_t const * const s = get_surface(surface);
2133 if (s->cnx->egl.eglSetDamageRegionKHR) {
2134 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2135 rects, n_rects);
2136 }
2137
2138 return EGL_FALSE;
2139}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002140
Brian Anderson1049d1d2016-12-16 17:25:57 -08002141EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2142 EGLuint64KHR *frameId) {
2143 clearError();
2144
2145 const egl_display_ptr dp = validate_display(dpy);
2146 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002147 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002148 }
2149
2150 SurfaceRef _s(dp.get(), surface);
2151 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002152 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002153 }
2154
2155 egl_surface_t const * const s = get_surface(surface);
2156
Mathias Agopian65421432017-03-08 11:49:05 -08002157 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002158 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002159 }
2160
2161 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002162 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002163
Mathias Agopian65421432017-03-08 11:49:05 -08002164 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002165 // This should not happen. Return an error that is not in the spec
2166 // so it's obvious something is very wrong.
2167 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002168 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002169 }
2170
2171 *frameId = nextFrameId;
2172 return EGL_TRUE;
2173}
2174
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002175EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2176 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2177{
2178 clearError();
2179
2180 const egl_display_ptr dp = validate_display(dpy);
2181 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002182 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002183 }
2184
2185 SurfaceRef _s(dp.get(), surface);
2186 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002187 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002188 }
2189
2190 egl_surface_t const * const s = get_surface(surface);
2191
Mathias Agopian65421432017-03-08 11:49:05 -08002192 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002193 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002194 }
2195
2196 nsecs_t* compositeDeadline = nullptr;
2197 nsecs_t* compositeInterval = nullptr;
2198 nsecs_t* compositeToPresentLatency = nullptr;
2199
2200 for (int i = 0; i < numTimestamps; i++) {
2201 switch (names[i]) {
2202 case EGL_COMPOSITE_DEADLINE_ANDROID:
2203 compositeDeadline = &values[i];
2204 break;
2205 case EGL_COMPOSITE_INTERVAL_ANDROID:
2206 compositeInterval = &values[i];
2207 break;
2208 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2209 compositeToPresentLatency = &values[i];
2210 break;
2211 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002212 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002213 }
2214 }
2215
Mathias Agopian65421432017-03-08 11:49:05 -08002216 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002217 compositeDeadline, compositeInterval, compositeToPresentLatency);
2218
2219 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002220 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002221 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002222 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002223 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002224 default:
2225 // This should not happen. Return an error that is not in the spec
2226 // so it's obvious something is very wrong.
2227 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002228 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002229 }
2230}
2231
2232EGLBoolean eglGetCompositorTimingSupportedANDROID(
2233 EGLDisplay dpy, EGLSurface surface, EGLint name)
2234{
2235 clearError();
2236
2237 const egl_display_ptr dp = validate_display(dpy);
2238 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002239 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002240 }
2241
2242 SurfaceRef _s(dp.get(), surface);
2243 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002244 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002245 }
2246
2247 egl_surface_t const * const s = get_surface(surface);
2248
Mathias Agopian65421432017-03-08 11:49:05 -08002249 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002250 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002251 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002252 }
2253
2254 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002255 case EGL_COMPOSITE_DEADLINE_ANDROID:
2256 case EGL_COMPOSITE_INTERVAL_ANDROID:
2257 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2258 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002259 default:
2260 return EGL_FALSE;
2261 }
2262}
2263
Pablo Ceballosc18be292016-05-31 14:55:42 -07002264EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002265 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002266 EGLnsecsANDROID *values)
2267{
2268 clearError();
2269
2270 const egl_display_ptr dp = validate_display(dpy);
2271 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002272 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002273 }
2274
2275 SurfaceRef _s(dp.get(), surface);
2276 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002277 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002278 }
2279
2280 egl_surface_t const * const s = get_surface(surface);
2281
Mathias Agopian65421432017-03-08 11:49:05 -08002282 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002283 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002284 }
2285
Brian Andersondbd0ea82016-07-22 09:38:59 -07002286 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002287 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002288 nsecs_t* latchTime = nullptr;
2289 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002290 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002291 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002292 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002293 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002294 nsecs_t* releaseTime = nullptr;
2295
2296 for (int i = 0; i < numTimestamps; i++) {
2297 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002298 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2299 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002300 break;
2301 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2302 acquireTime = &values[i];
2303 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002304 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2305 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002306 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002307 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2308 firstRefreshStartTime = &values[i];
2309 break;
2310 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2311 lastRefreshStartTime = &values[i];
2312 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002313 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2314 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002315 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002316 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2317 displayPresentTime = &values[i];
2318 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002319 case EGL_DEQUEUE_READY_TIME_ANDROID:
2320 dequeueReadyTime = &values[i];
2321 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002322 case EGL_READS_DONE_TIME_ANDROID:
2323 releaseTime = &values[i];
2324 break;
2325 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002326 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002327 }
2328 }
2329
Mathias Agopian65421432017-03-08 11:49:05 -08002330 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002331 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002332 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002333 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002334
Brian Anderson069b3652016-07-22 10:32:47 -07002335 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002336 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002337 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002338 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002339 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002340 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002341 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002342 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002343 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2344 default:
2345 // This should not happen. Return an error that is not in the spec
2346 // so it's obvious something is very wrong.
2347 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2348 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002349 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002350}
2351
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002352EGLBoolean eglGetFrameTimestampSupportedANDROID(
2353 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002354{
2355 clearError();
2356
2357 const egl_display_ptr dp = validate_display(dpy);
2358 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002359 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002360 }
2361
2362 SurfaceRef _s(dp.get(), surface);
2363 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002364 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002365 }
2366
2367 egl_surface_t const * const s = get_surface(surface);
2368
Mathias Agopian65421432017-03-08 11:49:05 -08002369 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002370 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002371 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002372 }
2373
2374 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002375 case EGL_COMPOSITE_DEADLINE_ANDROID:
2376 case EGL_COMPOSITE_INTERVAL_ANDROID:
2377 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002378 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002379 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002380 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2381 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2382 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002383 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002384 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002385 case EGL_READS_DONE_TIME_ANDROID:
2386 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002387 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2388 int value = 0;
2389 window->query(window,
2390 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2391 return value == 0 ? EGL_FALSE : EGL_TRUE;
2392 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002393 default:
2394 return EGL_FALSE;
2395 }
2396}