blob: 94dfe6a9de4b7d3a0064b8cc60e8b856da1be5d3 [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 "
Jesse Hall21558da2013-08-06 15:31:22 -070090 ;
Mathias Agopian311b4792017-02-28 15:00:49 -080091
92char const * const gExtensionString =
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070093 "EGL_KHR_image " // mandatory
94 "EGL_KHR_image_base " // mandatory
95 "EGL_KHR_image_pixmap "
96 "EGL_KHR_lock_surface "
Jesse Hallc2e41222013-08-08 13:40:22 -070097 "EGL_KHR_gl_colorspace "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070098 "EGL_KHR_gl_texture_2D_image "
Season Li000d88f2015-07-01 11:39:40 -070099 "EGL_KHR_gl_texture_3D_image "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700100 "EGL_KHR_gl_texture_cubemap_image "
101 "EGL_KHR_gl_renderbuffer_image "
102 "EGL_KHR_reusable_sync "
103 "EGL_KHR_fence_sync "
Jamie Gennisf6d1c392013-04-25 18:48:41 -0700104 "EGL_KHR_create_context "
Season Li000d88f2015-07-01 11:39:40 -0700105 "EGL_KHR_config_attribs "
106 "EGL_KHR_surfaceless_context "
107 "EGL_KHR_stream "
108 "EGL_KHR_stream_fifo "
109 "EGL_KHR_stream_producer_eglsurface "
110 "EGL_KHR_stream_consumer_gltexture "
111 "EGL_KHR_stream_cross_process_fd "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700112 "EGL_EXT_create_context_robustness "
113 "EGL_NV_system_time "
114 "EGL_ANDROID_image_native_buffer " // mandatory
Mathias Agopian2bb71682013-03-27 17:32:41 -0700115 "EGL_KHR_wait_sync " // strongly recommended
Jamie Gennisdbe92452013-09-23 17:22:10 -0700116 "EGL_ANDROID_recordable " // mandatory
Dan Stozaa894d082015-02-19 15:27:36 -0800117 "EGL_KHR_partial_update " // strongly recommended
Courtney Goeltzenleuchter0e4e3952016-11-16 13:53:40 -0700118 "EGL_EXT_pixel_format_float "
Dan Stozaa894d082015-02-19 15:27:36 -0800119 "EGL_EXT_buffer_age " // strongly recommended with partial_update
Jesse Hall408e59f2015-04-24 01:40:42 -0700120 "EGL_KHR_create_context_no_error "
Pablo Ceballosceb9ee72016-04-13 11:17:32 -0700121 "EGL_KHR_mutable_render_buffer "
Mika Isojärvif37864b2016-04-15 11:58:56 -0700122 "EGL_EXT_yuv_surface "
Craig Donneraec86972016-04-28 18:09:40 -0700123 "EGL_EXT_protected_content "
Christian Poetzscha7805f62016-12-01 16:34:39 +0000124 "EGL_IMG_context_priority "
Pyry Haulos51d53c42017-03-06 09:39:09 -0800125 "EGL_KHR_no_config_context "
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700126 ;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600127// clang-format on
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700128
129// extensions not exposed to applications but used by the ANDROID system
130// "EGL_ANDROID_blob_cache " // strongly recommended
131// "EGL_IMG_hibernate_process " // optional
132// "EGL_ANDROID_native_fence_sync " // strongly recommended
133// "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
Jamie Gennisdbe92452013-09-23 17:22:10 -0700134// "EGL_ANDROID_image_crop " // optional
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700135
136/*
137 * EGL Extensions entry-points exposed to 3rd party applications
138 * (keep in sync with gExtensionString above)
139 *
140 */
141static const extention_map_t sExtensionMap[] = {
142 // EGL_KHR_lock_surface
Mathias Agopian518ec112011-05-13 16:21:08 -0700143 { "eglLockSurfaceKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
145 { "eglUnlockSurfaceKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700147
148 // EGL_KHR_image, EGL_KHR_image_base
Mathias Agopian518ec112011-05-13 16:21:08 -0700149 { "eglCreateImageKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
151 { "eglDestroyImageKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700153
154 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
155 { "eglCreateSyncKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
157 { "eglDestroySyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
159 { "eglClientWaitSyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
161 { "eglSignalSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
163 { "eglGetSyncAttribKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
165
166 // EGL_NV_system_time
Jonas Yang1c3d72a2011-08-26 20:04:39 +0800167 { "eglGetSystemTimeFrequencyNV",
168 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
169 { "eglGetSystemTimeNV",
170 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700171
Mathias Agopian2bb71682013-03-27 17:32:41 -0700172 // EGL_KHR_wait_sync
173 { "eglWaitSyncKHR",
174 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700175
176 // EGL_ANDROID_presentation_time
177 { "eglPresentationTimeANDROID",
178 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
Dan Stozaa894d082015-02-19 15:27:36 -0800179
180 // EGL_KHR_swap_buffers_with_damage
181 { "eglSwapBuffersWithDamageKHR",
182 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
183
Craig Donner60761072017-01-27 12:30:44 -0800184 // EGL_ANDROID_get_native_client_buffer
185 { "eglGetNativeClientBufferANDROID",
186 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
187
Dan Stozaa894d082015-02-19 15:27:36 -0800188 // EGL_KHR_partial_update
189 { "eglSetDamageRegionKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
Season Li000d88f2015-07-01 11:39:40 -0700191
192 { "eglCreateStreamKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
194 { "eglDestroyStreamKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
196 { "eglStreamAttribKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
198 { "eglQueryStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
200 { "eglQueryStreamu64KHR",
201 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
202 { "eglQueryStreamTimeKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
204 { "eglCreateStreamProducerSurfaceKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
206 { "eglStreamConsumerGLTextureExternalKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
208 { "eglStreamConsumerAcquireKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
210 { "eglStreamConsumerReleaseKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
212 { "eglGetStreamFileDescriptorKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
214 { "eglCreateStreamFromFileDescriptorKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700216
217 // EGL_ANDROID_get_frame_timestamps
Brian Anderson1049d1d2016-12-16 17:25:57 -0800218 { "eglGetNextFrameIdANDROID",
219 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800220 { "eglGetCompositorTimingANDROID",
221 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
222 { "eglGetCompositorTimingSupportedANDROID",
223 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
Pablo Ceballosc18be292016-05-31 14:55:42 -0700224 { "eglGetFrameTimestampsANDROID",
225 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800226 { "eglGetFrameTimestampSupportedANDROID",
227 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
Craig Donner55901a22017-04-17 15:31:06 -0700228
229 // EGL_ANDROID_native_fence_sync
230 { "eglDupNativeFenceFDANDROID",
231 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
Mathias Agopian518ec112011-05-13 16:21:08 -0700232};
233
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700234/*
235 * These extensions entry-points should not be exposed to applications.
236 * They're used internally by the Android EGL layer.
237 */
238#define FILTER_EXTENSIONS(procname) \
239 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
240 !strcmp((procname), "eglHibernateProcessIMG") || \
Craig Donner55901a22017-04-17 15:31:06 -0700241 !strcmp((procname), "eglAwakenProcessIMG"))
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700242
243
244
Mathias Agopian518ec112011-05-13 16:21:08 -0700245// accesses protected by sExtensionMapMutex
Mathias Agopian65421432017-03-08 11:49:05 -0800246static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
247
Mathias Agopian518ec112011-05-13 16:21:08 -0700248static int sGLExtentionSlot = 0;
249static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
250
251static void(*findProcAddress(const char* name,
252 const extention_map_t* map, size_t n))() {
253 for (uint32_t i=0 ; i<n ; i++) {
254 if (!strcmp(name, map[i].name)) {
255 return map[i].address;
256 }
257 }
258 return NULL;
259}
260
261// ----------------------------------------------------------------------------
262
Mathias Agopian518ec112011-05-13 16:21:08 -0700263extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
264extern EGLBoolean egl_init_drivers();
265extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
Mathias Agopian518ec112011-05-13 16:21:08 -0700266extern gl_hooks_t gHooksTrace;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700267
Mathias Agopian518ec112011-05-13 16:21:08 -0700268} // namespace android;
269
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700270
Mathias Agopian518ec112011-05-13 16:21:08 -0700271// ----------------------------------------------------------------------------
272
273static inline void clearError() { egl_tls_t::clearError(); }
274static inline EGLContext getContext() { return egl_tls_t::getContext(); }
275
276// ----------------------------------------------------------------------------
277
278EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
279{
Jesse Hall1508ae62017-01-19 17:43:26 -0800280 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700281 clearError();
282
Dan Stozac3289c42014-01-17 11:38:34 -0800283 uintptr_t index = reinterpret_cast<uintptr_t>(display);
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 if (index >= NUM_DISPLAYS) {
285 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
286 }
287
288 if (egl_init_drivers() == EGL_FALSE) {
289 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
290 }
291
292 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
293 return dpy;
294}
295
296// ----------------------------------------------------------------------------
297// Initialization
298// ----------------------------------------------------------------------------
299
300EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
301{
302 clearError();
303
Jesse Hallb29e5e82012-04-04 16:53:42 -0700304 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800305 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700306
307 EGLBoolean res = dp->initialize(major, minor);
308
309 return res;
310}
311
312EGLBoolean eglTerminate(EGLDisplay dpy)
313{
314 // NOTE: don't unload the drivers b/c some APIs can be called
315 // after eglTerminate() has been called. eglTerminate() only
316 // terminates an EGLDisplay, not a EGL itself.
317
318 clearError();
319
Jesse Hallb29e5e82012-04-04 16:53:42 -0700320 egl_display_ptr dp = get_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800321 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700322
323 EGLBoolean res = dp->terminate();
Jesse Hall47743382013-02-08 11:13:46 -0800324
Mathias Agopian518ec112011-05-13 16:21:08 -0700325 return res;
326}
327
328// ----------------------------------------------------------------------------
329// configuration
330// ----------------------------------------------------------------------------
331
332EGLBoolean eglGetConfigs( EGLDisplay dpy,
333 EGLConfig *configs,
334 EGLint config_size, EGLint *num_config)
335{
336 clearError();
337
Jesse Hallb29e5e82012-04-04 16:53:42 -0700338 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700339 if (!dp) return EGL_FALSE;
340
Mathias Agopian7773c432012-02-13 20:06:08 -0800341 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800342 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700343 }
344
Mathias Agopian7773c432012-02-13 20:06:08 -0800345 EGLBoolean res = EGL_FALSE;
346 *num_config = 0;
347
348 egl_connection_t* const cnx = &gEGLImpl;
349 if (cnx->dso) {
350 res = cnx->egl.eglGetConfigs(
351 dp->disp.dpy, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700352 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800353
354 return res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700355}
356
357EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
358 EGLConfig *configs, EGLint config_size,
359 EGLint *num_config)
360{
361 clearError();
362
Jesse Hallb29e5e82012-04-04 16:53:42 -0700363 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700364 if (!dp) return EGL_FALSE;
365
366 if (num_config==0) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800367 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700368 }
369
Mathias Agopian518ec112011-05-13 16:21:08 -0700370 EGLBoolean res = EGL_FALSE;
371 *num_config = 0;
372
Mathias Agopianada798b2012-02-13 17:09:30 -0800373 egl_connection_t* const cnx = &gEGLImpl;
374 if (cnx->dso) {
Romain Guy1cffc802012-10-15 18:13:05 -0700375 if (attrib_list) {
376 char value[PROPERTY_VALUE_MAX];
377 property_get("debug.egl.force_msaa", value, "false");
378
379 if (!strcmp(value, "true")) {
380 size_t attribCount = 0;
381 EGLint attrib = attrib_list[0];
382
383 // Only enable MSAA if the context is OpenGL ES 2.0 and
Romain Guybe3c3e42012-10-15 19:25:18 -0700384 // if no caveat is requested
Romain Guy1cffc802012-10-15 18:13:05 -0700385 const EGLint *attribRendererable = NULL;
386 const EGLint *attribCaveat = NULL;
387
388 // Count the number of attributes and look for
Romain Guybe3c3e42012-10-15 19:25:18 -0700389 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
Romain Guy1cffc802012-10-15 18:13:05 -0700390 while (attrib != EGL_NONE) {
391 attrib = attrib_list[attribCount];
392 switch (attrib) {
393 case EGL_RENDERABLE_TYPE:
394 attribRendererable = &attrib_list[attribCount];
395 break;
396 case EGL_CONFIG_CAVEAT:
397 attribCaveat = &attrib_list[attribCount];
398 break;
Mathias Agopian737b8962017-02-24 14:32:05 -0800399 default:
400 break;
Romain Guy1cffc802012-10-15 18:13:05 -0700401 }
402 attribCount++;
403 }
404
405 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
406 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
Jesse Hall47743382013-02-08 11:13:46 -0800407
Romain Guy1cffc802012-10-15 18:13:05 -0700408 // Insert 2 extra attributes to force-enable MSAA 4x
409 EGLint aaAttribs[attribCount + 4];
410 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
411 aaAttribs[1] = 1;
412 aaAttribs[2] = EGL_SAMPLES;
413 aaAttribs[3] = 4;
414
415 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
416
417 EGLint numConfigAA;
418 EGLBoolean resAA = cnx->egl.eglChooseConfig(
419 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
420
421 if (resAA == EGL_TRUE && numConfigAA > 0) {
422 ALOGD("Enabling MSAA 4x");
423 *num_config = numConfigAA;
424 return resAA;
425 }
426 }
427 }
428 }
429
Mathias Agopian7773c432012-02-13 20:06:08 -0800430 res = cnx->egl.eglChooseConfig(
431 dp->disp.dpy, attrib_list, configs, config_size, num_config);
Mathias Agopian518ec112011-05-13 16:21:08 -0700432 }
433 return res;
434}
435
436EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
437 EGLint attribute, EGLint *value)
438{
439 clearError();
440
Jesse Hallb29e5e82012-04-04 16:53:42 -0700441 egl_connection_t* cnx = NULL;
442 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
443 if (!dp) return EGL_FALSE;
Jesse Hall47743382013-02-08 11:13:46 -0800444
Mathias Agopian518ec112011-05-13 16:21:08 -0700445 return cnx->egl.eglGetConfigAttrib(
Mathias Agopian7773c432012-02-13 20:06:08 -0800446 dp->disp.dpy, config, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700447}
448
449// ----------------------------------------------------------------------------
450// surfaces
451// ----------------------------------------------------------------------------
452
Jesse Hallc2e41222013-08-08 13:40:22 -0700453// Turn linear formats into corresponding sRGB formats when colorspace is
454// EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
455// formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800456// the modification isn't possible, the original dataSpace is returned.
Courtney Goeltzenleuchter15f79782017-06-20 08:12:54 -0600457static android_dataspace modifyBufferDataspace(android_dataspace dataSpace,
458 EGLint colorspace) {
Jesse Hallc2e41222013-08-08 13:40:22 -0700459 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800460 return HAL_DATASPACE_SRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700461 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800462 return HAL_DATASPACE_SRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600463 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
464 return HAL_DATASPACE_DISPLAY_P3;
465 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
466 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
Courtney Goeltzenleuchter2cd68e62017-06-23 09:06:52 -0600467 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
468 return HAL_DATASPACE_V0_SCRGB;
Courtney Goeltzenleuchter21b33cb2017-04-17 17:31:29 -0600469 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
470 return HAL_DATASPACE_V0_SCRGB_LINEAR;
Jesse Hallc2e41222013-08-08 13:40:22 -0700471 }
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800472 return dataSpace;
Jesse Hallc2e41222013-08-08 13:40:22 -0700473}
474
Courtney Goeltzenleuchter15f79782017-06-20 08:12:54 -0600475// Return true if we stripped any EGL_GL_COLORSPACE_KHR attributes.
476static EGLBoolean stripColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
477 EGLint format,
478 std::vector<EGLint>& stripped_attrib_list) {
479 std::vector<EGLint> allowedColorSpaces;
480 switch (format) {
481 case HAL_PIXEL_FORMAT_RGBA_8888:
482 case HAL_PIXEL_FORMAT_RGB_565:
483 // driver okay with linear & sRGB for 8888, but can't handle
484 // Display-P3 or other spaces.
485 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
486 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
487 break;
488
489 case HAL_PIXEL_FORMAT_RGBA_FP16:
490 case HAL_PIXEL_FORMAT_RGBA_1010102:
491 default:
492 // driver does not want to see colorspace attributes for 1010102 or fp16.
493 // Future: if driver supports XXXX extension, we can pass down that colorspace
494 break;
495 }
496
497 bool stripped = false;
498 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
499 for (const EGLint* attr = attrib_list; attr[0] != EGL_NONE; attr += 2) {
500 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
501 EGLint colorSpace = attr[1];
502 bool found = false;
503 // Verify that color space is allowed
504 for (auto it : allowedColorSpaces) {
505 if (colorSpace == it) {
506 found = true;
507 }
508 }
509 if (!found) {
510 stripped = true;
511 } else {
512 stripped_attrib_list.push_back(attr[0]);
513 stripped_attrib_list.push_back(attr[1]);
514 }
515 } else {
516 stripped_attrib_list.push_back(attr[0]);
517 stripped_attrib_list.push_back(attr[1]);
518 }
519 }
520 }
521 if (stripped) {
522 stripped_attrib_list.push_back(EGL_NONE);
523 stripped_attrib_list.push_back(EGL_NONE);
524 }
525 return stripped;
526}
527
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600528static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, NativeWindowType window,
529 const EGLint* attrib_list, EGLint& colorSpace,
530 android_dataspace& dataSpace) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600531 colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
532 dataSpace = HAL_DATASPACE_UNKNOWN;
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600533
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600534 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
535 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
536 if (*attr == EGL_GL_COLORSPACE_KHR) {
537 colorSpace = attr[1];
538 bool found = false;
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600539 bool verify = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600540 // Verify that color space is allowed
541 if (colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
542 colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR) {
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600543 // SRGB and LINEAR are always supported when EGL_KHR_gl_colorspace
544 // is available, so no need to verify.
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600545 found = true;
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600546 verify = false;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600547 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_linear &&
548 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
549 found = true;
550 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_pq &&
551 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
552 found = true;
Courtney Goeltzenleuchter2cd68e62017-06-23 09:06:52 -0600553 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_EXT &&
554 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb")) {
555 found = true;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600556 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
557 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
558 found = true;
559 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
560 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
561 found = true;
562 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
563 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
564 found = true;
565 }
566 if (!found) {
567 return false;
568 }
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600569 if (verify && window) {
570 bool wide_color_support = true;
571 // Ordinarily we'd put a call to native_window_get_wide_color_support
572 // at the beginning of the function so that we'll have the
573 // result when needed elsewhere in the function.
574 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
575 // SurfaceFlinger is required to answer the call below we would
576 // end up in a deadlock situation. By moving the call to only happen
577 // if the application has specifically asked for wide-color we avoid
578 // the deadlock with SurfaceFlinger since it will not ask for a
579 // wide-color surface.
580 int err = native_window_get_wide_color_support(window, &wide_color_support);
581
582 if (err) {
583 ALOGE("getColorSpaceAttribute: invalid window (win=%p) "
584 "failed (%#x) (already connected to another API?)",
585 window, err);
586 return false;
587 }
588 if (!wide_color_support) {
589 // Application has asked for a wide-color colorspace but
590 // wide-color support isn't available on the display the window is on.
591 return false;
592 }
593 }
Courtney Goeltzenleuchter1d4f7a22017-05-05 13:30:25 -0600594 // Only change the dataSpace from default if the application
595 // has explicitly set the color space with a EGL_GL_COLORSPACE_KHR attribute.
596 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600597 }
598 }
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600599 }
600 return true;
601}
602
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600603static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
604 EGLint& colorSpace, android_dataspace& dataSpace) {
605 return getColorSpaceAttribute(dp, NULL, attrib_list, colorSpace, dataSpace);
606}
607
Courtney Goeltzenleuchter15f79782017-06-20 08:12:54 -0600608void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config, EGLint& format) {
609 // Set the native window's buffers format to match what this config requests.
610 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
611 // of our native format. So if sRGB gamma is requested, we have to
612 // modify the EGLconfig's format before setting the native window's
613 // format.
614
615 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
616 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
617
618 EGLint a = 0;
619 EGLint r, g, b;
620 r = g = b = 0;
621 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
622 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
623 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
624 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
625 EGLint colorDepth = r + g + b;
626
627 // Today, the driver only understands sRGB and linear on 888X
628 // formats. Strip other colorspaces from the attribute list and
629 // only use them to set the dataspace via
630 // native_window_set_buffers_dataspace
631 // if pixel format is RGBX 8888
632 // TBD: Can test for future extensions that indicate that driver
633 // handles requested color space and we can let it through.
634 // allow SRGB and LINEAR. All others need to be stripped.
635 // else if 565, 4444
636 // TBD: Can we assume these are supported if 8888 is?
637 // else if FP16 or 1010102
638 // strip colorspace from attribs.
639 // endif
640 if (a == 0) {
641 if (colorDepth <= 16) {
642 format = HAL_PIXEL_FORMAT_RGB_565;
643 } else {
644 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
645 if (colorDepth > 24) {
646 format = HAL_PIXEL_FORMAT_RGBA_1010102;
647 } else {
648 format = HAL_PIXEL_FORMAT_RGBX_8888;
649 }
650 } else {
651 format = HAL_PIXEL_FORMAT_RGBA_FP16;
652 }
653 }
654 } else {
655 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
656 if (colorDepth > 24) {
657 format = HAL_PIXEL_FORMAT_RGBA_1010102;
658 } else {
659 format = HAL_PIXEL_FORMAT_RGBA_8888;
660 }
661 } else {
662 format = HAL_PIXEL_FORMAT_RGBA_FP16;
663 }
664 }
665}
666
Mathias Agopian518ec112011-05-13 16:21:08 -0700667EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
668 NativeWindowType window,
669 const EGLint *attrib_list)
670{
671 clearError();
672
Jesse Hallb29e5e82012-04-04 16:53:42 -0700673 egl_connection_t* cnx = NULL;
674 egl_display_ptr dp = validate_display_connection(dpy, cnx);
675 if (dp) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800676 EGLDisplay iDpy = dp->disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700677
Mathias Agopian10e9ab52017-03-08 15:02:55 -0800678 if (!window) {
679 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
680 }
681
682 int value = 0;
683 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
684 if (!value) {
685 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
686 }
687
Andy McFaddend566ce32014-01-07 15:54:17 -0800688 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian65421432017-03-08 11:49:05 -0800689 if (result < 0) {
Andy McFaddend566ce32014-01-07 15:54:17 -0800690 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
691 "failed (%#x) (already connected to another API?)",
692 window, result);
Jonathan Hamilton77a9b4a2013-07-17 09:41:42 -0700693 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
Mathias Agopian81a63352011-07-29 17:55:48 -0700694 }
695
Mathias Agopian95921562017-02-24 14:31:31 -0800696 EGLint format;
Courtney Goeltzenleuchter15f79782017-06-20 08:12:54 -0600697 getNativePixelFormat(iDpy, cnx, config, format);
698
699 // now select correct colorspace and dataspace based on user's attribute list
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600700 EGLint colorSpace;
701 android_dataspace dataSpace;
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600702 if (!getColorSpaceAttribute(dp, window, attrib_list, colorSpace, dataSpace)) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600703 ALOGE("error invalid colorspace: %d", colorSpace);
704 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700705 }
Alistair Strachan733a8072015-02-12 12:33:25 -0800706
Courtney Goeltzenleuchter15f79782017-06-20 08:12:54 -0600707 std::vector<EGLint> strippedAttribList;
708 if (stripColorSpaceAttribute(dp, attrib_list, format, strippedAttribList)) {
709 // Had to modify the attribute list due to use of color space.
710 // Use modified list from here on.
711 attrib_list = strippedAttribList.data();
712 }
713
Jesse Hallc2e41222013-08-08 13:40:22 -0700714 if (format != 0) {
715 int err = native_window_set_buffers_format(window, format);
716 if (err != 0) {
717 ALOGE("error setting native window pixel format: %s (%d)",
718 strerror(-err), err);
719 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
720 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
721 }
722 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700723
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800724 if (dataSpace != 0) {
725 int err = native_window_set_buffers_data_space(window, dataSpace);
726 if (err != 0) {
727 ALOGE("error setting native window pixel dataSpace: %s (%d)",
728 strerror(-err), err);
729 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
730 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
731 }
732 }
733
Jamie Gennis59769462011-11-19 18:04:43 -0800734 // the EGL spec requires that a new EGLSurface default to swap interval
735 // 1, so explicitly set that on the window here.
736 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
737 anw->setSwapInterval(anw, 1);
738
Mathias Agopian518ec112011-05-13 16:21:08 -0700739 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800740 iDpy, config, window, 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 =
743 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700744 return s;
745 }
Mathias Agopian81a63352011-07-29 17:55:48 -0700746
747 // EGLSurface creation failed
748 native_window_set_buffers_format(window, 0);
749 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
Mathias Agopian518ec112011-05-13 16:21:08 -0700750 }
751 return EGL_NO_SURFACE;
752}
753
754EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
755 NativePixmapType pixmap,
756 const EGLint *attrib_list)
757{
758 clearError();
759
Jesse Hallb29e5e82012-04-04 16:53:42 -0700760 egl_connection_t* cnx = NULL;
761 egl_display_ptr dp = validate_display_connection(dpy, cnx);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600762 EGLint colorSpace;
763 android_dataspace dataSpace;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700764 if (dp) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600765 // now select a corresponding sRGB format if needed
766 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
767 ALOGE("error invalid colorspace: %d", colorSpace);
768 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
769 }
770
Mathias Agopian518ec112011-05-13 16:21:08 -0700771 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800772 dp->disp.dpy, config, pixmap, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700773 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600774 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700775 return s;
776 }
777 }
778 return EGL_NO_SURFACE;
779}
780
781EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
782 const EGLint *attrib_list)
783{
784 clearError();
785
Jesse Hallb29e5e82012-04-04 16:53:42 -0700786 egl_connection_t* cnx = NULL;
787 egl_display_ptr dp = validate_display_connection(dpy, cnx);
788 if (dp) {
Courtney Goeltzenleuchter15f79782017-06-20 08:12:54 -0600789 EGLDisplay iDpy = dp->disp.dpy;
790 EGLint format;
791 getNativePixelFormat(iDpy, cnx, config, format);
792
793 // now select correct colorspace and dataspace based on user's attribute list
794 EGLint colorSpace;
795 android_dataspace dataSpace;
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600796 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
797 ALOGE("error invalid colorspace: %d", colorSpace);
798 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
799 }
800
Courtney Goeltzenleuchter15f79782017-06-20 08:12:54 -0600801 // Pbuffers are not displayed so we don't need to store the
802 // colorspace. We do need to filter out color spaces the
803 // driver doesn't know how to process.
804 std::vector<EGLint> strippedAttribList;
805 if (stripColorSpaceAttribute(dp, attrib_list, format, strippedAttribList)) {
806 // Had to modify the attribute list due to use of color space.
807 // Use modified list from here on.
808 attrib_list = strippedAttribList.data();
809 }
810
Mathias Agopian518ec112011-05-13 16:21:08 -0700811 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
Mathias Agopian7773c432012-02-13 20:06:08 -0800812 dp->disp.dpy, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700813 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600814 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700815 return s;
816 }
817 }
818 return EGL_NO_SURFACE;
819}
Jesse Hall47743382013-02-08 11:13:46 -0800820
Mathias Agopian518ec112011-05-13 16:21:08 -0700821EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
822{
823 clearError();
824
Jesse Hallb29e5e82012-04-04 16:53:42 -0700825 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700826 if (!dp) return EGL_FALSE;
827
Jesse Hallb29e5e82012-04-04 16:53:42 -0700828 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700829 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800830 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700831
832 egl_surface_t * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -0800833 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -0700834 if (result == EGL_TRUE) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700835 _s.terminate();
836 }
837 return result;
838}
839
840EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
841 EGLint attribute, EGLint *value)
842{
843 clearError();
844
Jesse Hallb29e5e82012-04-04 16:53:42 -0700845 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -0700846 if (!dp) return EGL_FALSE;
847
Jesse Hallb29e5e82012-04-04 16:53:42 -0700848 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700849 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800850 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700851
Mathias Agopian518ec112011-05-13 16:21:08 -0700852 egl_surface_t const * const s = get_surface(surface);
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -0600853 if (attribute == EGL_GL_COLORSPACE_KHR) {
854 *value = s->getColorSpace();
855 return EGL_TRUE;
856 }
Mathias Agopian7773c432012-02-13 20:06:08 -0800857 return s->cnx->egl.eglQuerySurface(
858 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -0700859}
860
Jamie Gennise8696a42012-01-15 18:54:57 -0800861void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800862 ATRACE_CALL();
Jamie Gennise8696a42012-01-15 18:54:57 -0800863 clearError();
864
Jesse Hallb29e5e82012-04-04 16:53:42 -0700865 const egl_display_ptr dp = validate_display(dpy);
Jamie Gennise8696a42012-01-15 18:54:57 -0800866 if (!dp) {
867 return;
868 }
869
Jesse Hallb29e5e82012-04-04 16:53:42 -0700870 SurfaceRef _s(dp.get(), surface);
Jamie Gennise8696a42012-01-15 18:54:57 -0800871 if (!_s.get()) {
872 setError(EGL_BAD_SURFACE, EGL_FALSE);
Jamie Gennise8696a42012-01-15 18:54:57 -0800873 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800874}
875
Mathias Agopian518ec112011-05-13 16:21:08 -0700876// ----------------------------------------------------------------------------
877// Contexts
878// ----------------------------------------------------------------------------
879
880EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
881 EGLContext share_list, const EGLint *attrib_list)
882{
883 clearError();
884
Jesse Hallb29e5e82012-04-04 16:53:42 -0700885 egl_connection_t* cnx = NULL;
886 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
Michael Chock0673e1e2012-06-21 12:53:17 -0700887 if (dp) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700888 if (share_list != EGL_NO_CONTEXT) {
Michael Chock0673e1e2012-06-21 12:53:17 -0700889 if (!ContextRef(dp.get(), share_list).get()) {
890 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
891 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700892 egl_context_t* const c = get_context(share_list);
893 share_list = c->context;
894 }
895 EGLContext context = cnx->egl.eglCreateContext(
Mathias Agopian7773c432012-02-13 20:06:08 -0800896 dp->disp.dpy, config, share_list, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -0700897 if (context != EGL_NO_CONTEXT) {
898 // figure out if it's a GLESv1 or GLESv2
899 int version = 0;
900 if (attrib_list) {
901 while (*attrib_list != EGL_NONE) {
902 GLint attr = *attrib_list++;
903 GLint value = *attrib_list++;
904 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
905 if (value == 1) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800906 version = egl_connection_t::GLESv1_INDEX;
Jesse Hall47743382013-02-08 11:13:46 -0800907 } else if (value == 2 || value == 3) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800908 version = egl_connection_t::GLESv2_INDEX;
Mathias Agopian518ec112011-05-13 16:21:08 -0700909 }
910 }
911 };
912 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700913 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
914 version);
Mathias Agopian518ec112011-05-13 16:21:08 -0700915 return c;
916 }
917 }
918 return EGL_NO_CONTEXT;
919}
920
921EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
922{
923 clearError();
924
Jesse Hallb29e5e82012-04-04 16:53:42 -0700925 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700926 if (!dp)
927 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700928
Jesse Hallb29e5e82012-04-04 16:53:42 -0700929 ContextRef _c(dp.get(), ctx);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700930 if (!_c.get())
Mathias Agopian737b8962017-02-24 14:32:05 -0800931 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Jesse Hall47743382013-02-08 11:13:46 -0800932
Mathias Agopian518ec112011-05-13 16:21:08 -0700933 egl_context_t * const c = get_context(ctx);
Mathias Agopianada798b2012-02-13 17:09:30 -0800934 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
Mathias Agopian518ec112011-05-13 16:21:08 -0700935 if (result == EGL_TRUE) {
936 _c.terminate();
937 }
938 return result;
939}
940
Mathias Agopian518ec112011-05-13 16:21:08 -0700941EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
942 EGLSurface read, EGLContext ctx)
943{
944 clearError();
945
Jesse Hallb29e5e82012-04-04 16:53:42 -0700946 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian737b8962017-02-24 14:32:05 -0800947 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700948
Mathias Agopian5b287a62011-05-16 18:58:55 -0700949 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
950 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
951 // a valid but uninitialized display.
Mathias Agopian518ec112011-05-13 16:21:08 -0700952 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
953 (draw != EGL_NO_SURFACE) ) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800954 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700955 }
956
957 // get a reference to the object passed in
Jesse Hallb29e5e82012-04-04 16:53:42 -0700958 ContextRef _c(dp.get(), ctx);
959 SurfaceRef _d(dp.get(), draw);
960 SurfaceRef _r(dp.get(), read);
Mathias Agopian518ec112011-05-13 16:21:08 -0700961
962 // validate the context (if not EGL_NO_CONTEXT)
Mathias Agopian5b287a62011-05-16 18:58:55 -0700963 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700964 // EGL_NO_CONTEXT is valid
Mathias Agopian737b8962017-02-24 14:32:05 -0800965 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -0700966 }
967
968 // these are the underlying implementation's object
969 EGLContext impl_ctx = EGL_NO_CONTEXT;
970 EGLSurface impl_draw = EGL_NO_SURFACE;
971 EGLSurface impl_read = EGL_NO_SURFACE;
972
973 // these are our objects structs passed in
974 egl_context_t * c = NULL;
975 egl_surface_t const * d = NULL;
976 egl_surface_t const * r = NULL;
977
978 // these are the current objects structs
979 egl_context_t * cur_c = get_context(getContext());
Jesse Hall47743382013-02-08 11:13:46 -0800980
Mathias Agopian518ec112011-05-13 16:21:08 -0700981 if (ctx != EGL_NO_CONTEXT) {
982 c = get_context(ctx);
983 impl_ctx = c->context;
984 } else {
985 // no context given, use the implementation of the current context
Michael Chock0673e1e2012-06-21 12:53:17 -0700986 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
987 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
Mathias Agopian737b8962017-02-24 14:32:05 -0800988 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
Michael Chock0673e1e2012-06-21 12:53:17 -0700989 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700990 if (cur_c == NULL) {
991 // no current context
Mathias Agopian518ec112011-05-13 16:21:08 -0700992 // not an error, there is just no current context.
993 return EGL_TRUE;
994 }
995 }
996
997 // retrieve the underlying implementation's draw EGLSurface
998 if (draw != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -0800999 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001000 d = get_surface(draw);
Mathias Agopian518ec112011-05-13 16:21:08 -07001001 impl_draw = d->surface;
1002 }
1003
1004 // retrieve the underlying implementation's read EGLSurface
1005 if (read != EGL_NO_SURFACE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001006 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001007 r = get_surface(read);
Mathias Agopian518ec112011-05-13 16:21:08 -07001008 impl_read = r->surface;
1009 }
1010
Mathias Agopian518ec112011-05-13 16:21:08 -07001011
Jesse Hallb29e5e82012-04-04 16:53:42 -07001012 EGLBoolean result = dp->makeCurrent(c, cur_c,
Mathias Agopianfb87e542012-01-30 18:20:52 -08001013 draw, read, ctx,
1014 impl_draw, impl_read, impl_ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001015
1016 if (result == EGL_TRUE) {
Mathias Agopianfb87e542012-01-30 18:20:52 -08001017 if (c) {
Mathias Agopian518ec112011-05-13 16:21:08 -07001018 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1019 egl_tls_t::setContext(ctx);
Mathias Agopian518ec112011-05-13 16:21:08 -07001020 _c.acquire();
1021 _r.acquire();
1022 _d.acquire();
Mathias Agopian518ec112011-05-13 16:21:08 -07001023 } else {
1024 setGLHooksThreadSpecific(&gHooksNoContext);
1025 egl_tls_t::setContext(EGL_NO_CONTEXT);
1026 }
Mathias Agopian5fecea72011-08-25 18:38:24 -07001027 } else {
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001028
Mike Stroyan47e741b2017-06-01 13:56:18 -06001029 if (cur_c != NULL) {
1030 // Force return to current context for drivers that cannot handle errors
1031 EGLBoolean restore_result = EGL_FALSE;
1032 // get a reference to the old current objects
1033 ContextRef _c2(dp.get(), cur_c);
1034 SurfaceRef _d2(dp.get(), cur_c->draw);
1035 SurfaceRef _r2(dp.get(), cur_c->read);
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001036
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001037 c = cur_c;
1038 impl_ctx = c->context;
1039 impl_draw = EGL_NO_SURFACE;
1040 if (cur_c->draw != EGL_NO_SURFACE) {
1041 d = get_surface(cur_c->draw);
1042 impl_draw = d->surface;
1043 }
1044 impl_read = EGL_NO_SURFACE;
1045 if (cur_c->read != EGL_NO_SURFACE) {
1046 r = get_surface(cur_c->read);
1047 impl_read = r->surface;
1048 }
1049 restore_result = dp->makeCurrent(c, cur_c,
1050 cur_c->draw, cur_c->read, cur_c->context,
1051 impl_draw, impl_read, impl_ctx);
Mike Stroyan47e741b2017-06-01 13:56:18 -06001052 if (restore_result == EGL_TRUE) {
1053 _c2.acquire();
1054 _r2.acquire();
1055 _d2.acquire();
1056 } else {
1057 ALOGE("Could not restore original EGL context");
1058 }
Mike Stroyan02ba5c72017-05-08 10:47:24 -06001059 }
Steve Blocke6f43dd2012-01-06 19:20:56 +00001060 // this will ALOGE the error
Mathias Agopian63108c32013-09-06 13:36:49 -07001061 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian737b8962017-02-24 14:32:05 -08001062 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001063 }
1064 return result;
1065}
1066
1067
1068EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1069 EGLint attribute, EGLint *value)
1070{
1071 clearError();
1072
Jesse Hallb29e5e82012-04-04 16:53:42 -07001073 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001074 if (!dp) return EGL_FALSE;
1075
Jesse Hallb29e5e82012-04-04 16:53:42 -07001076 ContextRef _c(dp.get(), ctx);
Mathias Agopian737b8962017-02-24 14:32:05 -08001077 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001078
Mathias Agopian518ec112011-05-13 16:21:08 -07001079 egl_context_t * const c = get_context(ctx);
Mathias Agopian7773c432012-02-13 20:06:08 -08001080 return c->cnx->egl.eglQueryContext(
1081 dp->disp.dpy, c->context, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001082
Mathias Agopian518ec112011-05-13 16:21:08 -07001083}
1084
1085EGLContext eglGetCurrentContext(void)
1086{
1087 // could be called before eglInitialize(), but we wouldn't have a context
1088 // then, and this function would correctly return EGL_NO_CONTEXT.
1089
1090 clearError();
1091
1092 EGLContext ctx = getContext();
1093 return ctx;
1094}
1095
1096EGLSurface eglGetCurrentSurface(EGLint readdraw)
1097{
1098 // could be called before eglInitialize(), but we wouldn't have a context
1099 // then, and this function would correctly return EGL_NO_SURFACE.
1100
1101 clearError();
1102
1103 EGLContext ctx = getContext();
1104 if (ctx) {
1105 egl_context_t const * const c = get_context(ctx);
1106 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1107 switch (readdraw) {
1108 case EGL_READ: return c->read;
Jesse Hall47743382013-02-08 11:13:46 -08001109 case EGL_DRAW: return c->draw;
Mathias Agopian518ec112011-05-13 16:21:08 -07001110 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1111 }
1112 }
1113 return EGL_NO_SURFACE;
1114}
1115
1116EGLDisplay eglGetCurrentDisplay(void)
1117{
1118 // could be called before eglInitialize(), but we wouldn't have a context
1119 // then, and this function would correctly return EGL_NO_DISPLAY.
1120
1121 clearError();
1122
1123 EGLContext ctx = getContext();
1124 if (ctx) {
1125 egl_context_t const * const c = get_context(ctx);
1126 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1127 return c->dpy;
1128 }
1129 return EGL_NO_DISPLAY;
1130}
1131
1132EGLBoolean eglWaitGL(void)
1133{
Mathias Agopian518ec112011-05-13 16:21:08 -07001134 clearError();
1135
Mathias Agopianada798b2012-02-13 17:09:30 -08001136 egl_connection_t* const cnx = &gEGLImpl;
1137 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001138 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001139
1140 return cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001141}
1142
1143EGLBoolean eglWaitNative(EGLint engine)
1144{
Mathias Agopian518ec112011-05-13 16:21:08 -07001145 clearError();
1146
Mathias Agopianada798b2012-02-13 17:09:30 -08001147 egl_connection_t* const cnx = &gEGLImpl;
1148 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001149 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001150
1151 return cnx->egl.eglWaitNative(engine);
Mathias Agopian518ec112011-05-13 16:21:08 -07001152}
1153
1154EGLint eglGetError(void)
1155{
Mathias Agopianada798b2012-02-13 17:09:30 -08001156 EGLint err = EGL_SUCCESS;
1157 egl_connection_t* const cnx = &gEGLImpl;
1158 if (cnx->dso) {
1159 err = cnx->egl.eglGetError();
Mathias Agopian518ec112011-05-13 16:21:08 -07001160 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001161 if (err == EGL_SUCCESS) {
1162 err = egl_tls_t::getError();
1163 }
1164 return err;
Mathias Agopian518ec112011-05-13 16:21:08 -07001165}
1166
Michael Chockc0ec5e22014-01-27 08:14:33 -08001167static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
Jesse Hallc07b5202013-07-04 12:08:16 -07001168 const char* procname) {
1169 const egl_connection_t* cnx = &gEGLImpl;
1170 void* proc = NULL;
1171
Michael Chockc0ec5e22014-01-27 08:14:33 -08001172 proc = dlsym(cnx->libEgl, procname);
1173 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1174
Jesse Hallc07b5202013-07-04 12:08:16 -07001175 proc = dlsym(cnx->libGles2, procname);
1176 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1177
1178 proc = dlsym(cnx->libGles1, procname);
1179 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1180
1181 return NULL;
1182}
1183
Mathias Agopian518ec112011-05-13 16:21:08 -07001184__eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1185{
1186 // eglGetProcAddress() could be the very first function called
1187 // in which case we must make sure we've initialized ourselves, this
1188 // happens the first time egl_get_display() is called.
1189
1190 clearError();
1191
1192 if (egl_init_drivers() == EGL_FALSE) {
1193 setError(EGL_BAD_PARAMETER, NULL);
1194 return NULL;
1195 }
1196
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001197 if (FILTER_EXTENSIONS(procname)) {
Jamie Gennisaca51c02011-11-03 17:42:43 -07001198 return NULL;
1199 }
1200
Mathias Agopian518ec112011-05-13 16:21:08 -07001201 __eglMustCastToProperFunctionPointerType addr;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001202 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
Mathias Agopian518ec112011-05-13 16:21:08 -07001203 if (addr) return addr;
1204
Michael Chockc0ec5e22014-01-27 08:14:33 -08001205 addr = findBuiltinWrapper(procname);
Jesse Hallc07b5202013-07-04 12:08:16 -07001206 if (addr) return addr;
Jamie Gennisaca51c02011-11-03 17:42:43 -07001207
Mathias Agopian518ec112011-05-13 16:21:08 -07001208 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1209 pthread_mutex_lock(&sExtensionMapMutex);
1210
1211 /*
1212 * Since eglGetProcAddress() is not associated to anything, it needs
1213 * to return a function pointer that "works" regardless of what
1214 * the current context is.
1215 *
1216 * For this reason, we return a "forwarder", a small stub that takes
1217 * care of calling the function associated with the context
1218 * currently bound.
1219 *
1220 * We first look for extensions we've already resolved, if we're seeing
1221 * this extension for the first time, we go through all our
1222 * implementations and call eglGetProcAddress() and record the
1223 * result in the appropriate implementation hooks and return the
1224 * address of the forwarder corresponding to that hook set.
1225 *
1226 */
1227
Mathias Agopian65421432017-03-08 11:49:05 -08001228 const std::string name(procname);
1229
1230 auto& extentionMap = sGLExtentionMap;
1231 auto pos = extentionMap.find(name);
1232 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001233 const int slot = sGLExtentionSlot;
1234
Steve Blocke6f43dd2012-01-06 19:20:56 +00001235 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
Mathias Agopian518ec112011-05-13 16:21:08 -07001236 "no more slots for eglGetProcAddress(\"%s\")",
1237 procname);
1238
1239 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1240 bool found = false;
Mathias Agopianada798b2012-02-13 17:09:30 -08001241
1242 egl_connection_t* const cnx = &gEGLImpl;
1243 if (cnx->dso && cnx->egl.eglGetProcAddress) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001244 // Extensions are independent of the bound context
luliuhui69d10072012-08-30 11:15:36 +08001245 addr =
Mathias Agopian7773c432012-02-13 20:06:08 -08001246 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1247 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
Mathias Agopianada798b2012-02-13 17:09:30 -08001248 cnx->egl.eglGetProcAddress(procname);
luliuhui69d10072012-08-30 11:15:36 +08001249 if (addr) found = true;
Mathias Agopian518ec112011-05-13 16:21:08 -07001250 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001251
Mathias Agopian518ec112011-05-13 16:21:08 -07001252 if (found) {
1253 addr = gExtensionForwarders[slot];
Mathias Agopian65421432017-03-08 11:49:05 -08001254 extentionMap[name] = addr;
Mathias Agopian518ec112011-05-13 16:21:08 -07001255 sGLExtentionSlot++;
1256 }
1257 }
1258
1259 pthread_mutex_unlock(&sExtensionMapMutex);
1260 return addr;
1261}
1262
Mathias Agopian65421432017-03-08 11:49:05 -08001263class FrameCompletionThread {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001264public:
1265
1266 static void queueSync(EGLSyncKHR sync) {
Mathias Agopian65421432017-03-08 11:49:05 -08001267 static FrameCompletionThread thread;
1268
1269 char name[64];
1270
1271 std::lock_guard<std::mutex> lock(thread.mMutex);
1272 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1273 ATRACE_NAME(name);
1274
1275 thread.mQueue.push_back(sync);
1276 thread.mCondition.notify_one();
1277 thread.mFramesQueued++;
1278 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001279 }
1280
1281private:
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001282
Mathias Agopian65421432017-03-08 11:49:05 -08001283 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1284 std::thread thread(&FrameCompletionThread::loop, this);
1285 thread.detach();
1286 }
1287
1288#pragma clang diagnostic push
1289#pragma clang diagnostic ignored "-Wmissing-noreturn"
1290 void loop() {
1291 while (true) {
1292 threadLoop();
1293 }
1294 }
1295#pragma clang diagnostic pop
1296
1297 void threadLoop() {
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001298 EGLSyncKHR sync;
1299 uint32_t frameNum;
1300 {
Mathias Agopian65421432017-03-08 11:49:05 -08001301 std::unique_lock<std::mutex> lock(mMutex);
1302 while (mQueue.empty()) {
1303 mCondition.wait(lock);
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001304 }
1305 sync = mQueue[0];
1306 frameNum = mFramesCompleted;
1307 }
1308 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1309 {
Mathias Agopian65421432017-03-08 11:49:05 -08001310 char name[64];
1311 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1312 ATRACE_NAME(name);
1313
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001314 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1315 if (result == EGL_FALSE) {
1316 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1317 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1318 ALOGE("FrameCompletion: timeout waiting for fence");
1319 }
1320 eglDestroySyncKHR(dpy, sync);
1321 }
1322 {
Mathias Agopian65421432017-03-08 11:49:05 -08001323 std::lock_guard<std::mutex> lock(mMutex);
1324 mQueue.pop_front();
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001325 mFramesCompleted++;
Mathias Agopian737b8962017-02-24 14:32:05 -08001326 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001327 }
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001328 }
1329
1330 uint32_t mFramesQueued;
1331 uint32_t mFramesCompleted;
Mathias Agopian65421432017-03-08 11:49:05 -08001332 std::deque<EGLSyncKHR> mQueue;
1333 std::condition_variable mCondition;
1334 std::mutex mMutex;
Jamie Gennis28ef8d72012-04-05 20:34:54 -07001335};
1336
Dan Stozaa894d082015-02-19 15:27:36 -08001337EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1338 EGLint *rects, EGLint n_rects)
Mathias Agopian518ec112011-05-13 16:21:08 -07001339{
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001340 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001341 clearError();
1342
Jesse Hallb29e5e82012-04-04 16:53:42 -07001343 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001344 if (!dp) return EGL_FALSE;
1345
Jesse Hallb29e5e82012-04-04 16:53:42 -07001346 SurfaceRef _s(dp.get(), draw);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001347 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001348 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001349
Mathias Agopian518ec112011-05-13 16:21:08 -07001350 egl_surface_t const * const s = get_surface(draw);
Mathias Agopian7db993a2012-03-25 00:49:46 -07001351
Mathias Agopianed6d08b2013-04-16 16:39:46 -07001352 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1353 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1354 if (sync != EGL_NO_SYNC_KHR) {
1355 FrameCompletionThread::queueSync(sync);
1356 }
1357 }
1358
Mathias Agopian7db993a2012-03-25 00:49:46 -07001359 if (CC_UNLIKELY(dp->finishOnSwap)) {
1360 uint32_t pixel;
1361 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1362 if (c) {
1363 // glReadPixels() ensures that the frame is complete
1364 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1365 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1366 }
1367 }
1368
Dan Stozaa894d082015-02-19 15:27:36 -08001369 if (n_rects == 0) {
1370 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1371 }
1372
Mathias Agopian65421432017-03-08 11:49:05 -08001373 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
Dan Stozaa894d082015-02-19 15:27:36 -08001374 for (int r = 0; r < n_rects; ++r) {
1375 int offset = r * 4;
1376 int x = rects[offset];
1377 int y = rects[offset + 1];
1378 int width = rects[offset + 2];
1379 int height = rects[offset + 3];
1380 android_native_rect_t androidRect;
1381 androidRect.left = x;
1382 androidRect.top = y + height;
1383 androidRect.right = x + width;
1384 androidRect.bottom = y;
1385 androidRects.push_back(androidRect);
1386 }
Mathias Agopian65421432017-03-08 11:49:05 -08001387 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
Dan Stozaa894d082015-02-19 15:27:36 -08001388
1389 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1390 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1391 rects, n_rects);
1392 } else {
1393 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1394 }
1395}
1396
1397EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1398{
1399 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
Mathias Agopian518ec112011-05-13 16:21:08 -07001400}
1401
1402EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1403 NativePixmapType target)
1404{
1405 clearError();
1406
Jesse Hallb29e5e82012-04-04 16:53:42 -07001407 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001408 if (!dp) return EGL_FALSE;
1409
Jesse Hallb29e5e82012-04-04 16:53:42 -07001410 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001411 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001412 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001413
Mathias Agopian518ec112011-05-13 16:21:08 -07001414 egl_surface_t const * const s = get_surface(surface);
Mathias Agopianada798b2012-02-13 17:09:30 -08001415 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
Mathias Agopian518ec112011-05-13 16:21:08 -07001416}
1417
1418const char* eglQueryString(EGLDisplay dpy, EGLint name)
1419{
1420 clearError();
1421
Chia-I Wue57d1352016-08-15 16:10:02 +08001422 // Generate an error quietly when client extensions (as defined by
1423 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1424 // validate_display to generate the error as validate_display would log
1425 // the error, which can be misleading.
1426 //
1427 // If we want to support EGL_EXT_client_extensions later, we can return
1428 // the client extension string here instead.
1429 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
Mathias Agopian737b8962017-02-24 14:32:05 -08001430 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
Chia-I Wue57d1352016-08-15 16:10:02 +08001431
Jesse Hallb29e5e82012-04-04 16:53:42 -07001432 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001433 if (!dp) return (const char *) NULL;
1434
1435 switch (name) {
1436 case EGL_VENDOR:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001437 return dp->getVendorString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001438 case EGL_VERSION:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001439 return dp->getVersionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001440 case EGL_EXTENSIONS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001441 return dp->getExtensionString();
Mathias Agopian518ec112011-05-13 16:21:08 -07001442 case EGL_CLIENT_APIS:
Mathias Agopian4b9511c2011-11-13 23:52:47 -08001443 return dp->getClientApiString();
Mathias Agopian737b8962017-02-24 14:32:05 -08001444 default:
1445 break;
Mathias Agopian518ec112011-05-13 16:21:08 -07001446 }
1447 return setError(EGL_BAD_PARAMETER, (const char *)0);
1448}
1449
Jiyong Park8d455e92017-08-10 20:30:56 +09001450extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
Mathias Agopianca088332013-03-28 17:44:13 -07001451{
1452 clearError();
1453
1454 const egl_display_ptr dp = validate_display(dpy);
1455 if (!dp) return (const char *) NULL;
1456
1457 switch (name) {
1458 case EGL_VENDOR:
1459 return dp->disp.queryString.vendor;
1460 case EGL_VERSION:
1461 return dp->disp.queryString.version;
1462 case EGL_EXTENSIONS:
1463 return dp->disp.queryString.extensions;
1464 case EGL_CLIENT_APIS:
1465 return dp->disp.queryString.clientApi;
Mathias Agopian737b8962017-02-24 14:32:05 -08001466 default:
1467 break;
Mathias Agopianca088332013-03-28 17:44:13 -07001468 }
1469 return setError(EGL_BAD_PARAMETER, (const char *)0);
1470}
Mathias Agopian518ec112011-05-13 16:21:08 -07001471
1472// ----------------------------------------------------------------------------
1473// EGL 1.1
1474// ----------------------------------------------------------------------------
1475
1476EGLBoolean eglSurfaceAttrib(
1477 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1478{
1479 clearError();
1480
Jesse Hallb29e5e82012-04-04 16:53:42 -07001481 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001482 if (!dp) return EGL_FALSE;
1483
Jesse Hallb29e5e82012-04-04 16:53:42 -07001484 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001485 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001486 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001487
Pablo Ceballosc18be292016-05-31 14:55:42 -07001488 egl_surface_t * const s = get_surface(surface);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001489
Pablo Ceballos02b05da2016-02-02 17:53:18 -08001490 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001491 if (!s->getNativeWindow()) {
Brian Anderson069b3652016-07-22 10:32:47 -07001492 setError(EGL_BAD_SURFACE, EGL_FALSE);
1493 }
Mathias Agopian65421432017-03-08 11:49:05 -08001494 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1495 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosccdfd602015-10-07 15:05:45 -07001496 }
1497
Pablo Ceballosc18be292016-05-31 14:55:42 -07001498 if (attribute == EGL_TIMESTAMPS_ANDROID) {
Mathias Agopian65421432017-03-08 11:49:05 -08001499 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001500 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07001501 }
Mathias Agopian65421432017-03-08 11:49:05 -08001502 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1503 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07001504 }
1505
Mathias Agopian518ec112011-05-13 16:21:08 -07001506 if (s->cnx->egl.eglSurfaceAttrib) {
1507 return s->cnx->egl.eglSurfaceAttrib(
Mathias Agopianada798b2012-02-13 17:09:30 -08001508 dp->disp.dpy, s->surface, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001509 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001510 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001511}
1512
1513EGLBoolean eglBindTexImage(
1514 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1515{
1516 clearError();
1517
Jesse Hallb29e5e82012-04-04 16:53:42 -07001518 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001519 if (!dp) return EGL_FALSE;
1520
Jesse Hallb29e5e82012-04-04 16:53:42 -07001521 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001522 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001523 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001524
Mathias Agopian518ec112011-05-13 16:21:08 -07001525 egl_surface_t const * const s = get_surface(surface);
1526 if (s->cnx->egl.eglBindTexImage) {
1527 return s->cnx->egl.eglBindTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001528 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001529 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001530 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001531}
1532
1533EGLBoolean eglReleaseTexImage(
1534 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1535{
1536 clearError();
1537
Jesse Hallb29e5e82012-04-04 16:53:42 -07001538 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001539 if (!dp) return EGL_FALSE;
1540
Jesse Hallb29e5e82012-04-04 16:53:42 -07001541 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001542 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001543 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001544
Mathias Agopian518ec112011-05-13 16:21:08 -07001545 egl_surface_t const * const s = get_surface(surface);
1546 if (s->cnx->egl.eglReleaseTexImage) {
1547 return s->cnx->egl.eglReleaseTexImage(
Mathias Agopianada798b2012-02-13 17:09:30 -08001548 dp->disp.dpy, s->surface, buffer);
Mathias Agopian518ec112011-05-13 16:21:08 -07001549 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001550 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001551}
1552
1553EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1554{
1555 clearError();
1556
Jesse Hallb29e5e82012-04-04 16:53:42 -07001557 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001558 if (!dp) return EGL_FALSE;
1559
1560 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001561 egl_connection_t* const cnx = &gEGLImpl;
1562 if (cnx->dso && cnx->egl.eglSwapInterval) {
1563 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
Mathias Agopian518ec112011-05-13 16:21:08 -07001564 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001565
Mathias Agopian518ec112011-05-13 16:21:08 -07001566 return res;
1567}
1568
1569
1570// ----------------------------------------------------------------------------
1571// EGL 1.2
1572// ----------------------------------------------------------------------------
1573
1574EGLBoolean eglWaitClient(void)
1575{
1576 clearError();
1577
Mathias Agopianada798b2012-02-13 17:09:30 -08001578 egl_connection_t* const cnx = &gEGLImpl;
1579 if (!cnx->dso)
Mathias Agopian737b8962017-02-24 14:32:05 -08001580 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
Mathias Agopianada798b2012-02-13 17:09:30 -08001581
1582 EGLBoolean res;
1583 if (cnx->egl.eglWaitClient) {
1584 res = cnx->egl.eglWaitClient();
1585 } else {
1586 res = cnx->egl.eglWaitGL();
Mathias Agopian518ec112011-05-13 16:21:08 -07001587 }
1588 return res;
1589}
1590
1591EGLBoolean eglBindAPI(EGLenum api)
1592{
1593 clearError();
1594
1595 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001596 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001597 }
1598
1599 // bind this API on all EGLs
1600 EGLBoolean res = EGL_TRUE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001601 egl_connection_t* const cnx = &gEGLImpl;
1602 if (cnx->dso && cnx->egl.eglBindAPI) {
1603 res = cnx->egl.eglBindAPI(api);
Mathias Agopian518ec112011-05-13 16:21:08 -07001604 }
1605 return res;
1606}
1607
1608EGLenum eglQueryAPI(void)
1609{
1610 clearError();
1611
1612 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08001613 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001614 }
1615
Mathias Agopianada798b2012-02-13 17:09:30 -08001616 egl_connection_t* const cnx = &gEGLImpl;
1617 if (cnx->dso && cnx->egl.eglQueryAPI) {
1618 return cnx->egl.eglQueryAPI();
Mathias Agopian518ec112011-05-13 16:21:08 -07001619 }
Mathias Agopianada798b2012-02-13 17:09:30 -08001620
Mathias Agopian518ec112011-05-13 16:21:08 -07001621 // or, it can only be OpenGL ES
1622 return EGL_OPENGL_ES_API;
1623}
1624
1625EGLBoolean eglReleaseThread(void)
1626{
1627 clearError();
1628
Mathias Agopianada798b2012-02-13 17:09:30 -08001629 egl_connection_t* const cnx = &gEGLImpl;
1630 if (cnx->dso && cnx->egl.eglReleaseThread) {
1631 cnx->egl.eglReleaseThread();
Mathias Agopian518ec112011-05-13 16:21:08 -07001632 }
Sai Kiran Korwar3ac517a2014-01-31 21:15:07 +05301633
1634 // If there is context bound to the thread, release it
1635 egl_display_t::loseCurrent(get_context(getContext()));
1636
Mathias Agopian518ec112011-05-13 16:21:08 -07001637 egl_tls_t::clearTLS();
Mathias Agopian518ec112011-05-13 16:21:08 -07001638 return EGL_TRUE;
1639}
1640
1641EGLSurface eglCreatePbufferFromClientBuffer(
1642 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1643 EGLConfig config, const EGLint *attrib_list)
1644{
1645 clearError();
1646
Jesse Hallb29e5e82012-04-04 16:53:42 -07001647 egl_connection_t* cnx = NULL;
1648 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1649 if (!dp) return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -07001650 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1651 return cnx->egl.eglCreatePbufferFromClientBuffer(
Mathias Agopian7773c432012-02-13 20:06:08 -08001652 dp->disp.dpy, buftype, buffer, config, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001653 }
1654 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1655}
1656
1657// ----------------------------------------------------------------------------
1658// EGL_EGLEXT_VERSION 3
1659// ----------------------------------------------------------------------------
1660
1661EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1662 const EGLint *attrib_list)
1663{
1664 clearError();
1665
Jesse Hallb29e5e82012-04-04 16:53:42 -07001666 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001667 if (!dp) return EGL_FALSE;
1668
Jesse Hallb29e5e82012-04-04 16:53:42 -07001669 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001670 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001671 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001672
1673 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001674 if (s->cnx->egl.eglLockSurfaceKHR) {
1675 return s->cnx->egl.eglLockSurfaceKHR(
Mathias Agopianada798b2012-02-13 17:09:30 -08001676 dp->disp.dpy, s->surface, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001677 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001678 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001679}
1680
1681EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1682{
1683 clearError();
1684
Jesse Hallb29e5e82012-04-04 16:53:42 -07001685 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001686 if (!dp) return EGL_FALSE;
1687
Jesse Hallb29e5e82012-04-04 16:53:42 -07001688 SurfaceRef _s(dp.get(), surface);
Mathias Agopian5b287a62011-05-16 18:58:55 -07001689 if (!_s.get())
Mathias Agopian737b8962017-02-24 14:32:05 -08001690 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001691
1692 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001693 if (s->cnx->egl.eglUnlockSurfaceKHR) {
Mathias Agopianada798b2012-02-13 17:09:30 -08001694 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
Mathias Agopian518ec112011-05-13 16:21:08 -07001695 }
Mathias Agopian737b8962017-02-24 14:32:05 -08001696 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Mathias Agopian518ec112011-05-13 16:21:08 -07001697}
1698
1699EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1700 EGLClientBuffer buffer, const EGLint *attrib_list)
1701{
1702 clearError();
1703
Jesse Hallb29e5e82012-04-04 16:53:42 -07001704 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001705 if (!dp) return EGL_NO_IMAGE_KHR;
1706
Jesse Hallb29e5e82012-04-04 16:53:42 -07001707 ContextRef _c(dp.get(), ctx);
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001708 egl_context_t * const c = _c.get();
Mathias Agopian518ec112011-05-13 16:21:08 -07001709
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001710 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1711 egl_connection_t* const cnx = &gEGLImpl;
1712 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1713 result = cnx->egl.eglCreateImageKHR(
1714 dp->disp.dpy,
1715 c ? c->context : EGL_NO_CONTEXT,
1716 target, buffer, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001717 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001718 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001719}
1720
1721EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1722{
1723 clearError();
1724
Jesse Hallb29e5e82012-04-04 16:53:42 -07001725 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001726 if (!dp) return EGL_FALSE;
1727
Steven Holte646a5c52012-06-04 20:02:11 -07001728 EGLBoolean result = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -08001729 egl_connection_t* const cnx = &gEGLImpl;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001730 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
Steven Holte646a5c52012-06-04 20:02:11 -07001731 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
Mathias Agopian518ec112011-05-13 16:21:08 -07001732 }
Steven Holte646a5c52012-06-04 20:02:11 -07001733 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001734}
1735
1736// ----------------------------------------------------------------------------
1737// EGL_EGLEXT_VERSION 5
1738// ----------------------------------------------------------------------------
1739
1740
1741EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1742{
1743 clearError();
1744
Jesse Hallb29e5e82012-04-04 16:53:42 -07001745 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001746 if (!dp) return EGL_NO_SYNC_KHR;
1747
Mathias Agopian518ec112011-05-13 16:21:08 -07001748 EGLSyncKHR result = EGL_NO_SYNC_KHR;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001749 egl_connection_t* const cnx = &gEGLImpl;
1750 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1751 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
Mathias Agopian518ec112011-05-13 16:21:08 -07001752 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001753 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001754}
1755
1756EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1757{
1758 clearError();
1759
Jesse Hallb29e5e82012-04-04 16:53:42 -07001760 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001761 if (!dp) return EGL_FALSE;
1762
Mathias Agopian518ec112011-05-13 16:21:08 -07001763 EGLBoolean result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001764 egl_connection_t* const cnx = &gEGLImpl;
1765 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1766 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
Mathias Agopian518ec112011-05-13 16:21:08 -07001767 }
1768 return result;
1769}
1770
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -07001771EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1772 clearError();
1773
1774 const egl_display_ptr dp = validate_display(dpy);
1775 if (!dp) return EGL_FALSE;
1776
1777 EGLBoolean result = EGL_FALSE;
1778 egl_connection_t* const cnx = &gEGLImpl;
1779 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1780 result = cnx->egl.eglSignalSyncKHR(
1781 dp->disp.dpy, sync, mode);
1782 }
1783 return result;
1784}
1785
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001786EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1787 EGLint flags, EGLTimeKHR timeout)
Mathias Agopian518ec112011-05-13 16:21:08 -07001788{
1789 clearError();
1790
Jesse Hallb29e5e82012-04-04 16:53:42 -07001791 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001792 if (!dp) return EGL_FALSE;
1793
Mathias Agopian737b8962017-02-24 14:32:05 -08001794 EGLint result = EGL_FALSE;
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001795 egl_connection_t* const cnx = &gEGLImpl;
1796 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1797 result = cnx->egl.eglClientWaitSyncKHR(
1798 dp->disp.dpy, sync, flags, timeout);
Mathias Agopian518ec112011-05-13 16:21:08 -07001799 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001800 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001801}
1802
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001803EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1804 EGLint attribute, EGLint *value)
Mathias Agopian518ec112011-05-13 16:21:08 -07001805{
1806 clearError();
1807
Jesse Hallb29e5e82012-04-04 16:53:42 -07001808 const egl_display_ptr dp = validate_display(dpy);
Mathias Agopian518ec112011-05-13 16:21:08 -07001809 if (!dp) return EGL_FALSE;
1810
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001811 EGLBoolean result = EGL_FALSE;
1812 egl_connection_t* const cnx = &gEGLImpl;
1813 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1814 result = cnx->egl.eglGetSyncAttribKHR(
1815 dp->disp.dpy, sync, attribute, value);
Mathias Agopian518ec112011-05-13 16:21:08 -07001816 }
Mathias Agopian7c0441a2012-02-14 17:14:36 -08001817 return result;
Mathias Agopian518ec112011-05-13 16:21:08 -07001818}
1819
Season Li000d88f2015-07-01 11:39:40 -07001820EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1821{
1822 clearError();
1823
1824 const egl_display_ptr dp = validate_display(dpy);
1825 if (!dp) return EGL_NO_STREAM_KHR;
1826
1827 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1828 egl_connection_t* const cnx = &gEGLImpl;
1829 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1830 result = cnx->egl.eglCreateStreamKHR(
1831 dp->disp.dpy, attrib_list);
1832 }
1833 return result;
1834}
1835
1836EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1837{
1838 clearError();
1839
1840 const egl_display_ptr dp = validate_display(dpy);
1841 if (!dp) return EGL_FALSE;
1842
1843 EGLBoolean result = EGL_FALSE;
1844 egl_connection_t* const cnx = &gEGLImpl;
1845 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1846 result = cnx->egl.eglDestroyStreamKHR(
1847 dp->disp.dpy, stream);
1848 }
1849 return result;
1850}
1851
1852EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1853 EGLenum attribute, EGLint value)
1854{
1855 clearError();
1856
1857 const egl_display_ptr dp = validate_display(dpy);
1858 if (!dp) return EGL_FALSE;
1859
1860 EGLBoolean result = EGL_FALSE;
1861 egl_connection_t* const cnx = &gEGLImpl;
1862 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1863 result = cnx->egl.eglStreamAttribKHR(
1864 dp->disp.dpy, stream, attribute, value);
1865 }
1866 return result;
1867}
1868
1869EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1870 EGLenum attribute, EGLint *value)
1871{
1872 clearError();
1873
1874 const egl_display_ptr dp = validate_display(dpy);
1875 if (!dp) return EGL_FALSE;
1876
1877 EGLBoolean result = EGL_FALSE;
1878 egl_connection_t* const cnx = &gEGLImpl;
1879 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1880 result = cnx->egl.eglQueryStreamKHR(
1881 dp->disp.dpy, stream, attribute, value);
1882 }
1883 return result;
1884}
1885
1886EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1887 EGLenum attribute, EGLuint64KHR *value)
1888{
1889 clearError();
1890
1891 const egl_display_ptr dp = validate_display(dpy);
1892 if (!dp) return EGL_FALSE;
1893
1894 EGLBoolean result = EGL_FALSE;
1895 egl_connection_t* const cnx = &gEGLImpl;
1896 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1897 result = cnx->egl.eglQueryStreamu64KHR(
1898 dp->disp.dpy, stream, attribute, value);
1899 }
1900 return result;
1901}
1902
1903EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1904 EGLenum attribute, EGLTimeKHR *value)
1905{
1906 clearError();
1907
1908 const egl_display_ptr dp = validate_display(dpy);
1909 if (!dp) return EGL_FALSE;
1910
1911 EGLBoolean result = EGL_FALSE;
1912 egl_connection_t* const cnx = &gEGLImpl;
1913 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1914 result = cnx->egl.eglQueryStreamTimeKHR(
1915 dp->disp.dpy, stream, attribute, value);
1916 }
1917 return result;
1918}
1919
1920EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1921 EGLStreamKHR stream, const EGLint *attrib_list)
1922{
1923 clearError();
1924
1925 egl_display_ptr dp = validate_display(dpy);
1926 if (!dp) return EGL_NO_SURFACE;
1927
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06001928 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
1929 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
1930 // TODO: Probably need to update EGL_KHR_stream_producer_eglsurface to
1931 // indicate support for EGL_GL_COLORSPACE_KHR.
1932 // now select a corresponding sRGB format if needed
1933 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
1934 ALOGE("error invalid colorspace: %d", colorSpace);
1935 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1936 }
1937
Season Li000d88f2015-07-01 11:39:40 -07001938 egl_connection_t* const cnx = &gEGLImpl;
1939 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1940 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1941 dp->disp.dpy, config, stream, attrib_list);
1942 if (surface != EGL_NO_SURFACE) {
Courtney Goeltzenleuchter37836572017-04-26 15:07:51 -06001943 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
Season Li000d88f2015-07-01 11:39:40 -07001944 return s;
1945 }
1946 }
1947 return EGL_NO_SURFACE;
1948}
1949
1950EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1951 EGLStreamKHR stream)
1952{
1953 clearError();
1954
1955 const egl_display_ptr dp = validate_display(dpy);
1956 if (!dp) return EGL_FALSE;
1957
1958 EGLBoolean result = EGL_FALSE;
1959 egl_connection_t* const cnx = &gEGLImpl;
1960 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1961 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1962 dp->disp.dpy, stream);
1963 }
1964 return result;
1965}
1966
1967EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1968 EGLStreamKHR stream)
1969{
1970 clearError();
1971
1972 const egl_display_ptr dp = validate_display(dpy);
1973 if (!dp) return EGL_FALSE;
1974
1975 EGLBoolean result = EGL_FALSE;
1976 egl_connection_t* const cnx = &gEGLImpl;
1977 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1978 result = cnx->egl.eglStreamConsumerAcquireKHR(
1979 dp->disp.dpy, stream);
1980 }
1981 return result;
1982}
1983
1984EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1985 EGLStreamKHR stream)
1986{
1987 clearError();
1988
1989 const egl_display_ptr dp = validate_display(dpy);
1990 if (!dp) return EGL_FALSE;
1991
1992 EGLBoolean result = EGL_FALSE;
1993 egl_connection_t* const cnx = &gEGLImpl;
1994 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1995 result = cnx->egl.eglStreamConsumerReleaseKHR(
1996 dp->disp.dpy, stream);
1997 }
1998 return result;
1999}
2000
2001EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2002 EGLDisplay dpy, EGLStreamKHR stream)
2003{
2004 clearError();
2005
2006 const egl_display_ptr dp = validate_display(dpy);
2007 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2008
2009 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2010 egl_connection_t* const cnx = &gEGLImpl;
2011 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2012 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2013 dp->disp.dpy, stream);
2014 }
2015 return result;
2016}
2017
2018EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2019 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2020{
2021 clearError();
2022
2023 const egl_display_ptr dp = validate_display(dpy);
2024 if (!dp) return EGL_NO_STREAM_KHR;
2025
2026 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2027 egl_connection_t* const cnx = &gEGLImpl;
2028 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2029 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2030 dp->disp.dpy, file_descriptor);
2031 }
2032 return result;
2033}
2034
Mathias Agopian518ec112011-05-13 16:21:08 -07002035// ----------------------------------------------------------------------------
Mathias Agopian2bb71682013-03-27 17:32:41 -07002036// EGL_EGLEXT_VERSION 15
2037// ----------------------------------------------------------------------------
2038
2039EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2040 clearError();
2041 const egl_display_ptr dp = validate_display(dpy);
2042 if (!dp) return EGL_FALSE;
2043 EGLint result = EGL_FALSE;
2044 egl_connection_t* const cnx = &gEGLImpl;
2045 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2046 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2047 }
2048 return result;
2049}
2050
2051// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -07002052// ANDROID extensions
2053// ----------------------------------------------------------------------------
2054
Jamie Gennis331841b2012-09-06 14:52:00 -07002055EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2056{
2057 clearError();
2058
2059 const egl_display_ptr dp = validate_display(dpy);
2060 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2061
2062 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2063 egl_connection_t* const cnx = &gEGLImpl;
2064 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2065 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2066 }
2067 return result;
2068}
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002069
Andy McFadden72841452013-03-01 16:25:32 -08002070EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2071 EGLnsecsANDROID time)
2072{
2073 clearError();
2074
2075 const egl_display_ptr dp = validate_display(dpy);
2076 if (!dp) {
2077 return EGL_FALSE;
2078 }
2079
2080 SurfaceRef _s(dp.get(), surface);
2081 if (!_s.get()) {
2082 setError(EGL_BAD_SURFACE, EGL_FALSE);
2083 return EGL_FALSE;
2084 }
2085
2086 egl_surface_t const * const s = get_surface(surface);
Mathias Agopian65421432017-03-08 11:49:05 -08002087 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
Andy McFadden72841452013-03-01 16:25:32 -08002088
2089 return EGL_TRUE;
2090}
2091
Craig Donner60761072017-01-27 12:30:44 -08002092EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2093 clearError();
Jiyong Parka243e5d2017-06-21 12:26:51 +09002094 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2095 // this function cannot be implemented when this libEGL is built for
2096 // vendors.
2097#ifndef __ANDROID_VNDK__
Craig Donner60761072017-01-27 12:30:44 -08002098 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
Mathias Agopian61963402017-02-24 16:38:15 -08002099 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
Jiyong Parka243e5d2017-06-21 12:26:51 +09002100#else
2101 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2102#endif
Craig Donner60761072017-01-27 12:30:44 -08002103}
2104
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002105// ----------------------------------------------------------------------------
2106// NVIDIA extensions
2107// ----------------------------------------------------------------------------
2108EGLuint64NV eglGetSystemTimeFrequencyNV()
2109{
2110 clearError();
2111
2112 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002113 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002114 }
2115
2116 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002117 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002118
Mathias Agopianada798b2012-02-13 17:09:30 -08002119 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2120 return cnx->egl.eglGetSystemTimeFrequencyNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002121 }
2122
Mathias Agopian737b8962017-02-24 14:32:05 -08002123 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002124}
2125
2126EGLuint64NV eglGetSystemTimeNV()
2127{
2128 clearError();
2129
2130 if (egl_init_drivers() == EGL_FALSE) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002131 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002132 }
2133
2134 EGLuint64NV ret = 0;
Mathias Agopianada798b2012-02-13 17:09:30 -08002135 egl_connection_t* const cnx = &gEGLImpl;
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002136
Mathias Agopianada798b2012-02-13 17:09:30 -08002137 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2138 return cnx->egl.eglGetSystemTimeNV();
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002139 }
2140
Mathias Agopian737b8962017-02-24 14:32:05 -08002141 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
Jonas Yang1c3d72a2011-08-26 20:04:39 +08002142}
Dan Stozaa894d082015-02-19 15:27:36 -08002143
2144// ----------------------------------------------------------------------------
2145// Partial update extension
2146// ----------------------------------------------------------------------------
2147EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2148 EGLint *rects, EGLint n_rects)
2149{
2150 clearError();
2151
2152 const egl_display_ptr dp = validate_display(dpy);
2153 if (!dp) {
2154 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2155 return EGL_FALSE;
2156 }
2157
2158 SurfaceRef _s(dp.get(), surface);
2159 if (!_s.get()) {
2160 setError(EGL_BAD_SURFACE, EGL_FALSE);
2161 return EGL_FALSE;
2162 }
2163
2164 egl_surface_t const * const s = get_surface(surface);
2165 if (s->cnx->egl.eglSetDamageRegionKHR) {
2166 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2167 rects, n_rects);
2168 }
2169
2170 return EGL_FALSE;
2171}
Pablo Ceballosc18be292016-05-31 14:55:42 -07002172
Brian Anderson1049d1d2016-12-16 17:25:57 -08002173EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2174 EGLuint64KHR *frameId) {
2175 clearError();
2176
2177 const egl_display_ptr dp = validate_display(dpy);
2178 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002179 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002180 }
2181
2182 SurfaceRef _s(dp.get(), surface);
2183 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002184 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002185 }
2186
2187 egl_surface_t const * const s = get_surface(surface);
2188
Mathias Agopian65421432017-03-08 11:49:05 -08002189 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002190 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002191 }
2192
2193 uint64_t nextFrameId = 0;
Mathias Agopian65421432017-03-08 11:49:05 -08002194 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002195
Mathias Agopian65421432017-03-08 11:49:05 -08002196 if (ret != 0) {
Brian Anderson1049d1d2016-12-16 17:25:57 -08002197 // This should not happen. Return an error that is not in the spec
2198 // so it's obvious something is very wrong.
2199 ALOGE("eglGetNextFrameId: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002200 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson1049d1d2016-12-16 17:25:57 -08002201 }
2202
2203 *frameId = nextFrameId;
2204 return EGL_TRUE;
2205}
2206
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002207EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2208 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2209{
2210 clearError();
2211
2212 const egl_display_ptr dp = validate_display(dpy);
2213 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002214 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002215 }
2216
2217 SurfaceRef _s(dp.get(), surface);
2218 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002219 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002220 }
2221
2222 egl_surface_t const * const s = get_surface(surface);
2223
Mathias Agopian65421432017-03-08 11:49:05 -08002224 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002225 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002226 }
2227
2228 nsecs_t* compositeDeadline = nullptr;
2229 nsecs_t* compositeInterval = nullptr;
2230 nsecs_t* compositeToPresentLatency = nullptr;
2231
2232 for (int i = 0; i < numTimestamps; i++) {
2233 switch (names[i]) {
2234 case EGL_COMPOSITE_DEADLINE_ANDROID:
2235 compositeDeadline = &values[i];
2236 break;
2237 case EGL_COMPOSITE_INTERVAL_ANDROID:
2238 compositeInterval = &values[i];
2239 break;
2240 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2241 compositeToPresentLatency = &values[i];
2242 break;
2243 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002244 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002245 }
2246 }
2247
Mathias Agopian65421432017-03-08 11:49:05 -08002248 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002249 compositeDeadline, compositeInterval, compositeToPresentLatency);
2250
2251 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002252 case 0:
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002253 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002254 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002255 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002256 default:
2257 // This should not happen. Return an error that is not in the spec
2258 // so it's obvious something is very wrong.
2259 ALOGE("eglGetCompositorTiming: Unexpected error.");
Mathias Agopian737b8962017-02-24 14:32:05 -08002260 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002261 }
2262}
2263
2264EGLBoolean eglGetCompositorTimingSupportedANDROID(
2265 EGLDisplay dpy, EGLSurface surface, EGLint name)
2266{
2267 clearError();
2268
2269 const egl_display_ptr dp = validate_display(dpy);
2270 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002271 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002272 }
2273
2274 SurfaceRef _s(dp.get(), surface);
2275 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002276 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002277 }
2278
2279 egl_surface_t const * const s = get_surface(surface);
2280
Mathias Agopian65421432017-03-08 11:49:05 -08002281 ANativeWindow* window = s->getNativeWindow();
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002282 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002283 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002284 }
2285
2286 switch (name) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002287 case EGL_COMPOSITE_DEADLINE_ANDROID:
2288 case EGL_COMPOSITE_INTERVAL_ANDROID:
2289 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2290 return EGL_TRUE;
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002291 default:
2292 return EGL_FALSE;
2293 }
2294}
2295
Pablo Ceballosc18be292016-05-31 14:55:42 -07002296EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
Brian Anderson1049d1d2016-12-16 17:25:57 -08002297 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
Pablo Ceballosc18be292016-05-31 14:55:42 -07002298 EGLnsecsANDROID *values)
2299{
2300 clearError();
2301
2302 const egl_display_ptr dp = validate_display(dpy);
2303 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002304 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002305 }
2306
2307 SurfaceRef _s(dp.get(), surface);
2308 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002309 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002310 }
2311
2312 egl_surface_t const * const s = get_surface(surface);
2313
Mathias Agopian65421432017-03-08 11:49:05 -08002314 if (!s->getNativeWindow()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002315 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002316 }
2317
Brian Andersondbd0ea82016-07-22 09:38:59 -07002318 nsecs_t* requestedPresentTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002319 nsecs_t* acquireTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002320 nsecs_t* latchTime = nullptr;
2321 nsecs_t* firstRefreshStartTime = nullptr;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002322 nsecs_t* gpuCompositionDoneTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002323 nsecs_t* lastRefreshStartTime = nullptr;
Brian Anderson069b3652016-07-22 10:32:47 -07002324 nsecs_t* displayPresentTime = nullptr;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002325 nsecs_t* dequeueReadyTime = nullptr;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002326 nsecs_t* releaseTime = nullptr;
2327
2328 for (int i = 0; i < numTimestamps; i++) {
2329 switch (timestamps[i]) {
Brian Andersondbd0ea82016-07-22 09:38:59 -07002330 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2331 requestedPresentTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002332 break;
2333 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2334 acquireTime = &values[i];
2335 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002336 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2337 latchTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002338 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002339 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2340 firstRefreshStartTime = &values[i];
2341 break;
2342 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2343 lastRefreshStartTime = &values[i];
2344 break;
Brian Andersonb04c6f02016-10-21 12:57:46 -07002345 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2346 gpuCompositionDoneTime = &values[i];
Pablo Ceballosc18be292016-05-31 14:55:42 -07002347 break;
Brian Anderson069b3652016-07-22 10:32:47 -07002348 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2349 displayPresentTime = &values[i];
2350 break;
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002351 case EGL_DEQUEUE_READY_TIME_ANDROID:
2352 dequeueReadyTime = &values[i];
2353 break;
Pablo Ceballosc18be292016-05-31 14:55:42 -07002354 case EGL_READS_DONE_TIME_ANDROID:
2355 releaseTime = &values[i];
2356 break;
2357 default:
Mathias Agopian737b8962017-02-24 14:32:05 -08002358 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002359 }
2360 }
2361
Mathias Agopian65421432017-03-08 11:49:05 -08002362 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002363 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
Brian Andersonb04c6f02016-10-21 12:57:46 -07002364 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
Brian Anderson4e606e32017-03-16 15:34:57 -07002365 dequeueReadyTime, releaseTime);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002366
Brian Anderson069b3652016-07-22 10:32:47 -07002367 switch (ret) {
Mathias Agopian65421432017-03-08 11:49:05 -08002368 case 0:
Mathias Agopian737b8962017-02-24 14:32:05 -08002369 return EGL_TRUE;
Mathias Agopian65421432017-03-08 11:49:05 -08002370 case -ENOENT:
Mathias Agopian737b8962017-02-24 14:32:05 -08002371 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002372 case -ENOSYS:
Mathias Agopian737b8962017-02-24 14:32:05 -08002373 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Mathias Agopian65421432017-03-08 11:49:05 -08002374 case -EINVAL:
Mathias Agopian737b8962017-02-24 14:32:05 -08002375 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2376 default:
2377 // This should not happen. Return an error that is not in the spec
2378 // so it's obvious something is very wrong.
2379 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2380 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002381 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002382}
2383
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002384EGLBoolean eglGetFrameTimestampSupportedANDROID(
2385 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
Pablo Ceballosc18be292016-05-31 14:55:42 -07002386{
2387 clearError();
2388
2389 const egl_display_ptr dp = validate_display(dpy);
2390 if (!dp) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002391 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002392 }
2393
2394 SurfaceRef _s(dp.get(), surface);
2395 if (!_s.get()) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002396 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Brian Anderson069b3652016-07-22 10:32:47 -07002397 }
2398
2399 egl_surface_t const * const s = get_surface(surface);
2400
Mathias Agopian65421432017-03-08 11:49:05 -08002401 ANativeWindow* window = s->getNativeWindow();
Brian Anderson069b3652016-07-22 10:32:47 -07002402 if (!window) {
Mathias Agopian737b8962017-02-24 14:32:05 -08002403 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
Pablo Ceballosc18be292016-05-31 14:55:42 -07002404 }
2405
2406 switch (timestamp) {
Brian Anderson0a61b0c2016-12-07 14:55:56 -08002407 case EGL_COMPOSITE_DEADLINE_ANDROID:
2408 case EGL_COMPOSITE_INTERVAL_ANDROID:
2409 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
Brian Andersondbd0ea82016-07-22 09:38:59 -07002410 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002411 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002412 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2413 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2414 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
Brian Andersonb04c6f02016-10-21 12:57:46 -07002415 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
Brian Andersonf7fd56a2016-09-02 10:10:04 -07002416 case EGL_DEQUEUE_READY_TIME_ANDROID:
Pablo Ceballosc18be292016-05-31 14:55:42 -07002417 case EGL_READS_DONE_TIME_ANDROID:
2418 return EGL_TRUE;
Brian Anderson6b376712017-04-04 10:51:39 -07002419 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2420 int value = 0;
2421 window->query(window,
2422 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2423 return value == 0 ? EGL_FALSE : EGL_TRUE;
2424 }
Pablo Ceballosc18be292016-05-31 14:55:42 -07002425 default:
2426 return EGL_FALSE;
2427 }
2428}