blob: 3c1edd148a014806219cd9c9fe9c993bbf9e6282 [file] [log] [blame]
Jesse Hall21558da2013-08-06 15:31:22 -07001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall21558da2013-08-06 15:31:22 -07004 ** 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 Hall21558da2013-08-06 15:31:22 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall21558da2013-08-06 15:31:22 -070010 ** 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
Jesse Hallb29e5e82012-04-04 16:53:42 -070017#define __STDC_LIMIT_MACROS 1
Jesse Hall1508ae62017-01-19 17:43:26 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hallb29e5e82012-04-04 16:53:42 -070019
Mathias Agopian311b4792017-02-28 15:00:49 -080020#include "egl_display.h"
Mathias Agopian4b9511c2011-11-13 23:52:47 -080021
Mathias Agopian39c24a22013-04-04 23:17:56 -070022#include "../egl_impl.h"
23
Mathias Agopianb7f9a242017-03-08 22:29:31 -080024#include <private/EGL/display.h>
25
Jamie Gennisaca51c02011-11-03 17:42:43 -070026#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070027#include "egl_object.h"
28#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080029#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070030#include "Loader.h"
Mathias Agopian7db993a2012-03-25 00:49:46 -070031#include <cutils/properties.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080032
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -060033#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
34#include <configstore/Utils.h>
35
36using namespace android::hardware::configstore;
37using namespace android::hardware::configstore::V1_0;
38
Mathias Agopian518ec112011-05-13 16:21:08 -070039// ----------------------------------------------------------------------------
40namespace android {
41// ----------------------------------------------------------------------------
42
Mathias Agopian4b9511c2011-11-13 23:52:47 -080043static char const * const sVendorString = "Android";
44static char const * const sVersionString = "1.4 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070045static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080046
Jesse Hall21558da2013-08-06 15:31:22 -070047extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070048extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080049
Mathias Agopian518ec112011-05-13 16:21:08 -070050extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
51
Mathias Agopian518ec112011-05-13 16:21:08 -070052// ----------------------------------------------------------------------------
53
Jesse Hallc2e41222013-08-08 13:40:22 -070054static bool findExtension(const char* exts, const char* name, size_t nameLen) {
55 if (exts) {
Kalle Raita7804aa22016-04-18 16:03:37 -070056 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
57 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
58 return true;
59 }
Jesse Hallc2e41222013-08-08 13:40:22 -070060 }
61 }
62 return false;
63}
64
Mathias Agopianb7f9a242017-03-08 22:29:31 -080065int egl_get_init_count(EGLDisplay dpy) {
66 egl_display_t* eglDisplay = egl_display_t::get(dpy);
67 return eglDisplay ? eglDisplay->getRefsCount() : 0;
68}
69
Mathias Agopian518ec112011-05-13 16:21:08 -070070egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
71
72egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080073 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070074}
75
76egl_display_t::~egl_display_t() {
77 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080078 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070079}
80
81egl_display_t* egl_display_t::get(EGLDisplay dpy) {
Ivan Lozano13f2c072017-12-06 14:19:44 -080082 if (uintptr_t(dpy) == 0) {
83 return nullptr;
84 }
85
Mathias Agopian518ec112011-05-13 16:21:08 -070086 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -070087 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
88 return nullptr;
89 }
90 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -070091}
92
93void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080094 std::lock_guard<std::mutex> _l(lock);
95 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -070096}
97
Mathias Agopian5b287a62011-05-16 18:58:55 -070098void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080099 std::lock_guard<std::mutex> _l(lock);
100 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700101}
102
Mathias Agopianf0480de2011-11-13 20:50:07 -0800103bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800104 std::lock_guard<std::mutex> _l(lock);
105 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800106 if (object->getDisplay() == this) {
107 object->incRef();
108 return true;
109 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700110 }
111 return false;
112}
113
Mathias Agopian518ec112011-05-13 16:21:08 -0700114EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
115 if (uintptr_t(disp) >= NUM_DISPLAYS)
116 return NULL;
117
118 return sDisplay[uintptr_t(disp)].getDisplay(disp);
119}
120
121EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
122
Mathias Agopian65421432017-03-08 11:49:05 -0800123 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800124 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700125
126 // get our driver loader
127 Loader& loader(Loader::getInstance());
128
Mathias Agopianada798b2012-02-13 17:09:30 -0800129 egl_connection_t* const cnx = &gEGLImpl;
130 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
131 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
132 disp.dpy = dpy;
133 if (dpy == EGL_NO_DISPLAY) {
134 loader.close(cnx->dso);
135 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700136 }
137 }
138
139 return EGLDisplay(uintptr_t(display) + 1U);
140}
141
142EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
143
Mathias Agopian65421432017-03-08 11:49:05 -0800144 { // scope for refLock
145 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800146 refs++;
147 if (refs > 1) {
148 if (major != NULL)
149 *major = VERSION_MAJOR;
150 if (minor != NULL)
151 *minor = VERSION_MINOR;
Mathias Agopian65421432017-03-08 11:49:05 -0800152 while(!eglIsInitialized) {
153 refCond.wait(_l);
154 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800155 return EGL_TRUE;
156 }
Mathias Agopian65421432017-03-08 11:49:05 -0800157 while(eglIsInitialized) {
158 refCond.wait(_l);
159 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800160 }
161
Mathias Agopian65421432017-03-08 11:49:05 -0800162 { // scope for lock
163 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800164
Michael Lentine54466bc2015-01-27 09:01:03 -0800165 setGLHooksThreadSpecific(&gHooksNoContext);
166
167 // initialize each EGL and
168 // build our own extension string first, based on the extension we know
169 // and the extension supported by our client implementation
170
171 egl_connection_t* const cnx = &gEGLImpl;
172 cnx->major = -1;
173 cnx->minor = -1;
174 if (cnx->dso) {
175 EGLDisplay idpy = disp.dpy;
176 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
177 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
178 // idpy, cnx->major, cnx->minor, cnx);
179
180 // display is now initialized
181 disp.state = egl_display_t::INITIALIZED;
182
183 // get the query-strings for this display for each implementation
184 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
185 EGL_VENDOR);
186 disp.queryString.version = cnx->egl.eglQueryString(idpy,
187 EGL_VERSION);
188 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
189 EGL_EXTENSIONS);
190 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
191 EGL_CLIENT_APIS);
192
193 } else {
194 ALOGW("eglInitialize(%p) failed (%s)", idpy,
195 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
196 }
197 }
198
199 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800200 mVendorString = sVendorString;
201 mVersionString = sVersionString;
202 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800203
Mathias Agopian65421432017-03-08 11:49:05 -0800204 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600205
206 bool wideColorBoardConfig =
207 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
208 false);
209
210 // Add wide-color extensions if device can support wide-color
211 if (wideColorBoardConfig) {
212 mExtensionString.append(
213 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
214 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
215 }
216
Michael Lentine54466bc2015-01-27 09:01:03 -0800217 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800218 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400219 // length of the extension name
220 size_t len = strcspn(start, " ");
221 if (len) {
222 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800223 const std::string ext(start, len);
224 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400225 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800226 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400227 // advance to the next extension name, skipping the space.
228 start += len;
229 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800230 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400231 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800232
233 egl_cache_t::get()->initialize(this);
234
235 char value[PROPERTY_VALUE_MAX];
236 property_get("debug.egl.finish", value, "0");
237 if (atoi(value)) {
238 finishOnSwap = true;
239 }
240
241 property_get("debug.egl.traceGpuCompletion", value, "0");
242 if (atoi(value)) {
243 traceGpuCompletion = true;
244 }
245
Mathias Agopian518ec112011-05-13 16:21:08 -0700246 if (major != NULL)
247 *major = VERSION_MAJOR;
248 if (minor != NULL)
249 *minor = VERSION_MINOR;
Mathias Agopian518ec112011-05-13 16:21:08 -0700250 }
251
Mathias Agopian65421432017-03-08 11:49:05 -0800252 { // scope for refLock
253 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800254 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800255 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700256 }
257
Mathias Agopian7773c432012-02-13 20:06:08 -0800258 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700259}
260
261EGLBoolean egl_display_t::terminate() {
262
Mathias Agopian65421432017-03-08 11:49:05 -0800263 { // scope for refLock
264 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800265 if (refs == 0) {
266 /*
267 * From the EGL spec (3.2):
268 * "Termination of a display that has already been terminated,
269 * (...), is allowed, but the only effect of such a call is
270 * to return EGL_TRUE (...)
271 */
272 return EGL_TRUE;
273 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700274
Michael Lentine54466bc2015-01-27 09:01:03 -0800275 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700276 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800277 if (refs > 0) {
278 return EGL_TRUE;
279 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700280 }
281
282 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800283
Mathias Agopian65421432017-03-08 11:49:05 -0800284 { // scope for lock
285 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800286
287 egl_connection_t* const cnx = &gEGLImpl;
288 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
289 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
290 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
291 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
292 }
293 // REVISIT: it's unclear what to do if eglTerminate() fails
294 disp.state = egl_display_t::TERMINATED;
295 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700296 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800297
Michael Lentine54466bc2015-01-27 09:01:03 -0800298 // Reset the extension string since it will be regenerated if we get
299 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800300 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800301
302 // Mark all objects remaining in the list as terminated, unless
303 // there are no reference to them, it which case, we're free to
304 // delete them.
305 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800306 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800307 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800308 o->destroy();
309 }
310
311 // this marks all object handles are "terminated"
312 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700313 }
314
Mathias Agopian65421432017-03-08 11:49:05 -0800315 { // scope for refLock
316 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800317 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800318 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700319 }
320
Mathias Agopian518ec112011-05-13 16:21:08 -0700321 return res;
322}
323
Mathias Agopianfb87e542012-01-30 18:20:52 -0800324void egl_display_t::loseCurrent(egl_context_t * cur_c)
325{
326 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800327 egl_display_t* display = cur_c->getDisplay();
328 if (display) {
329 display->loseCurrentImpl(cur_c);
330 }
331 }
332}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800333
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800334void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
335{
336 // by construction, these are either 0 or valid (possibly terminated)
337 // it should be impossible for these to be invalid
338 ContextRef _cur_c(cur_c);
339 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
340 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800341
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800342 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800343 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800344 cur_c->onLooseCurrent();
345
Mathias Agopianfb87e542012-01-30 18:20:52 -0800346 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800347
348 // This cannot be called with the lock held because it might end-up
349 // calling back into EGL (in particular when a surface is destroyed
350 // it calls ANativeWindow::disconnect
351 _cur_c.release();
352 _cur_r.release();
353 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800354}
355
356EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700357 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800358 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
359{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800360 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800361
362 // by construction, these are either 0 or valid (possibly terminated)
363 // it should be impossible for these to be invalid
364 ContextRef _cur_c(cur_c);
365 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
366 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
367
368 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800369 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800370 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800371 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800372 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800373 if (result == EGL_TRUE) {
374 c->onMakeCurrent(draw, read);
375 }
376 } else {
377 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800378 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800379 if (result == EGL_TRUE) {
380 cur_c->onLooseCurrent();
381 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800382 }
383 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800384
385 if (result == EGL_TRUE) {
386 // This cannot be called with the lock held because it might end-up
387 // calling back into EGL (in particular when a surface is destroyed
388 // it calls ANativeWindow::disconnect
389 _cur_c.release();
390 _cur_r.release();
391 _cur_d.release();
392 }
393
Mathias Agopianfb87e542012-01-30 18:20:52 -0800394 return result;
395}
Mathias Agopian518ec112011-05-13 16:21:08 -0700396
Jesse Hallc2e41222013-08-08 13:40:22 -0700397bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
398 if (!nameLen) {
399 nameLen = strlen(name);
400 }
Mathias Agopian65421432017-03-08 11:49:05 -0800401 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700402}
403
Jesse Halla0fef1c2012-04-17 12:02:26 -0700404// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700405}; // namespace android
406// ----------------------------------------------------------------------------