blob: 8870d5f571a34e02d4b46a6d5c5cfc16181c7174 [file] [log] [blame]
Jesse Hall47743382013-02-08 11:13:46 -08001/*
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002 ** 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08007 **
Jesse Hall47743382013-02-08 11:13:46 -08008 ** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08009 **
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080014 ** limitations under the License.
15 */
16
Mathias Agopiand8fb7b52009-05-17 18:50:16 -070017#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Mathias Agopian518ec112011-05-13 16:21:08 -070019#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
21#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <cutils/properties.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080024
Mark Salyzyn7823e122016-09-29 08:08:05 -070025#include <log/log.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080026
Mathias Agopian39c24a22013-04-04 23:17:56 -070027#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070028
Mathias Agopian39c24a22013-04-04 23:17:56 -070029#include "egldefs.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080030#include "egl_tls.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070031#include "egl_display.h"
32#include "egl_object.h"
Cody Northrop629ce4e2018-10-15 07:22:09 -060033#include "egl_layers.h"
Mathias Agopian5f1af042017-03-09 18:50:05 -080034#include "CallStack.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080035#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37// ----------------------------------------------------------------------------
38namespace android {
39// ----------------------------------------------------------------------------
40
Mathias Agopianada798b2012-02-13 17:09:30 -080041egl_connection_t gEGLImpl;
42gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070043gl_hooks_t gHooksNoContext;
44pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070045
46// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
Mathias Agopian518ec112011-05-13 16:21:08 -070048void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070049 setGlThreadSpecific(value);
50}
51
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052/*****************************************************************************/
53
Mathias Agopian6f087122010-09-23 16:38:38 -070054static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -070055 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -070056 char const* const error = "call to OpenGL ES API with "
57 "no current context (logged once per thread)";
58 if (LOG_NDEBUG) {
59 ALOGE(error);
60 } else {
61 LOG_ALWAYS_FATAL(error);
62 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070063 char value[PROPERTY_VALUE_MAX];
64 property_get("debug.egl.callstack", value, "0");
John Reck1f246d72014-04-24 23:34:32 +000065 if (atoi(value)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -080066 CallStack::log(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070067 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070068 }
Mathias Agopian6f087122010-09-23 16:38:38 -070069 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070070}
71
Jesse Hall47743382013-02-08 11:13:46 -080072static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073{
Dan Stozac3289c42014-01-17 11:38:34 -080074 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
75 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
76 for (int hook = 0; hook < numHooks; ++hook) {
77 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
78 }
Mathias Agopian05c53112010-09-23 11:32:52 -070079
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070080 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081}
82
83static pthread_once_t once_control = PTHREAD_ONCE_INIT;
84static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
85
Mathias Agopian518ec112011-05-13 16:21:08 -070086// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087
Jesse Hallb29e5e82012-04-04 16:53:42 -070088egl_display_ptr validate_display(EGLDisplay dpy) {
89 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070090 if (!dp)
Yi Kong48a6cd22018-07-18 10:07:09 -070091 return setError(EGL_BAD_DISPLAY, egl_display_ptr(nullptr));
Mathias Agopian5b287a62011-05-16 18:58:55 -070092 if (!dp->isReady())
Yi Kong48a6cd22018-07-18 10:07:09 -070093 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(nullptr));
Eric Hassold3ede7c12011-03-23 15:59:00 -070094
95 return dp;
96}
97
Jesse Hallb29e5e82012-04-04 16:53:42 -070098egl_display_ptr validate_display_connection(EGLDisplay dpy,
99 egl_connection_t*& cnx) {
Yi Kong48a6cd22018-07-18 10:07:09 -0700100 cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700101 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700102 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700103 return dp;
104 cnx = &gEGLImpl;
Yi Kong48a6cd22018-07-18 10:07:09 -0700105 if (cnx->dso == nullptr) {
106 return setError(EGL_BAD_CONFIG, egl_display_ptr(nullptr));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700108 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109}
110
Mathias Agopian518ec112011-05-13 16:21:08 -0700111// ----------------------------------------------------------------------------
112
Mathias Agopian48d438d2012-01-28 21:44:00 -0800113const GLubyte * egl_get_string_for_current_context(GLenum name) {
114 // NOTE: returning NULL here will fall-back to the default
115 // implementation.
116
117 EGLContext context = egl_tls_t::getContext();
118 if (context == EGL_NO_CONTEXT)
Yi Kong48a6cd22018-07-18 10:07:09 -0700119 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800120
121 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700122 if (c == nullptr) // this should never happen, by construction
123 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800124
125 if (name != GL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700126 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800127
Mathias Agopian65421432017-03-08 11:49:05 -0800128 return (const GLubyte *)c->gl_extensions.c_str();
Mathias Agopian48d438d2012-01-28 21:44:00 -0800129}
130
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700131const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
132 // NOTE: returning NULL here will fall-back to the default
133 // implementation.
134
135 EGLContext context = egl_tls_t::getContext();
136 if (context == EGL_NO_CONTEXT)
Yi Kong48a6cd22018-07-18 10:07:09 -0700137 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700138
139 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700140 if (c == nullptr) // this should never happen, by construction
141 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700142
143 if (name != GL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700144 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700145
146 // if index is out of bounds, assume it will be in the default
147 // implementation too, so we don't have to generate a GL error here
148 if (index >= c->tokenized_gl_extensions.size())
Yi Kong48a6cd22018-07-18 10:07:09 -0700149 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700150
Mathias Agopian65421432017-03-08 11:49:05 -0800151 return (const GLubyte *)c->tokenized_gl_extensions[index].c_str();
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700152}
153
154GLint egl_get_num_extensions_for_current_context() {
155 // NOTE: returning -1 here will fall-back to the default
156 // implementation.
157
158 EGLContext context = egl_tls_t::getContext();
159 if (context == EGL_NO_CONTEXT)
160 return -1;
161
162 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700163 if (c == nullptr) // this should never happen, by construction
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700164 return -1;
165
166 return (GLint)c->tokenized_gl_extensions.size();
167}
168
Cody Northrop68d10352018-10-15 07:22:09 -0600169egl_connection_t* egl_get_connection() {
170 return &gEGLImpl;
171}
172
Mathias Agopian48d438d2012-01-28 21:44:00 -0800173// ----------------------------------------------------------------------------
174
Mathias Agopian923c6612009-08-17 18:07:06 -0700175// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700176// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700177// egl_init_drivers_locked()
178//
Mathias Agopian518ec112011-05-13 16:21:08 -0700179static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700181 // initialized by static ctor. should be set here.
182 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 }
184
Mathias Agopiande586972009-05-28 17:39:03 -0700185 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700186 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700187
Mathias Agopianada798b2012-02-13 17:09:30 -0800188 // dynamically load our EGL implementation
189 egl_connection_t* cnx = &gEGLImpl;
Yi Kong48a6cd22018-07-18 10:07:09 -0700190 if (cnx->dso == nullptr) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800191 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
192 &gHooks[egl_connection_t::GLESv1_INDEX];
193 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
194 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800195 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 }
197
Cody Northrop629ce4e2018-10-15 07:22:09 -0600198 // Check to see if any layers are enabled and route functions through them
199 if (cnx->dso) {
200 // Layers can be enabled long after the drivers have been loaded.
201 // They will only be initialized once.
202 LayerLoader& layer_loader(LayerLoader::getInstance());
203 layer_loader.InitLayers(cnx);
204 }
205
Mathias Agopianada798b2012-02-13 17:09:30 -0800206 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
Mathias Agopian518ec112011-05-13 16:21:08 -0700209static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
210
211EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700212 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700213 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700214 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700215 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700216 return res;
217}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700218
Michael Lentine12c4bda2014-09-11 12:24:13 -0700219static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian65421432017-03-08 11:49:05 -0800220static std::chrono::steady_clock::time_point sLogPrintTime;
221static constexpr std::chrono::seconds DURATION(1);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700222
Mathias Agopian1cadb252011-05-23 17:26:14 -0700223void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700224 bool printLog = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800225 auto now = std::chrono::steady_clock::now();
Michael Lentine12c4bda2014-09-11 12:24:13 -0700226 pthread_mutex_lock(&sLogPrintMutex);
Mathias Agopian65421432017-03-08 11:49:05 -0800227 if ((now - sLogPrintTime) > DURATION) {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700228 sLogPrintTime = now;
229 printLog = true;
230 }
231 pthread_mutex_unlock(&sLogPrintMutex);
232 if (printLog) {
233 ALOGE("called unimplemented OpenGL ES API");
234 char value[PROPERTY_VALUE_MAX];
235 property_get("debug.egl.callstack", value, "0");
236 if (atoi(value)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800237 CallStack::log(LOG_TAG);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700238 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700239 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700240}
241
Mathias Agopian48d438d2012-01-28 21:44:00 -0800242void gl_noop() {
243}
244
Mathias Agopian1cadb252011-05-23 17:26:14 -0700245// ----------------------------------------------------------------------------
246
Mathias Agopian1cadb252011-05-23 17:26:14 -0700247void setGlThreadSpecific(gl_hooks_t const *value) {
248 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
249 tls_hooks[TLS_SLOT_OPENGL_API] = value;
250}
251
Mathias Agopian1cadb252011-05-23 17:26:14 -0700252// ----------------------------------------------------------------------------
253// GL / EGL hooks
254// ----------------------------------------------------------------------------
255
256#undef GL_ENTRY
257#undef EGL_ENTRY
258#define GL_ENTRY(_r, _api, ...) #_api,
259#define EGL_ENTRY(_r, _api, ...) #_api,
260
261char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700262 #include "../entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700263 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700264};
265
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700266char const * const gl_names_1[] = {
267 #include "../entries_gles1.in"
268 nullptr
269};
270
Mathias Agopian1cadb252011-05-23 17:26:14 -0700271char const * const egl_names[] = {
272 #include "egl_entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700273 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700274};
275
Cody Northrop68d10352018-10-15 07:22:09 -0600276char const * const platform_names[] = {
277 #include "platform_entries.in"
278 nullptr
279};
280
Mathias Agopian1cadb252011-05-23 17:26:14 -0700281#undef GL_ENTRY
282#undef EGL_ENTRY
283
284
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700285// ----------------------------------------------------------------------------
286}; // namespace android
287// ----------------------------------------------------------------------------
288