blob: 43f7a075a7d49c46db064562a94fe007ce47dff8 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040021#include <android-base/properties.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080022
Mark Salyzyn7823e122016-09-29 08:08:05 -070023#include <log/log.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080024
Mathias Agopian39c24a22013-04-04 23:17:56 -070025#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070026
Mathias Agopian39c24a22013-04-04 23:17:56 -070027#include "egldefs.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080028#include "egl_tls.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070029#include "egl_display.h"
30#include "egl_object.h"
Cody Northrop629ce4e2018-10-15 07:22:09 -060031#include "egl_layers.h"
Mathias Agopian5f1af042017-03-09 18:50:05 -080032#include "CallStack.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080033#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
35// ----------------------------------------------------------------------------
36namespace android {
37// ----------------------------------------------------------------------------
38
Mathias Agopianada798b2012-02-13 17:09:30 -080039egl_connection_t gEGLImpl;
40gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070041gl_hooks_t gHooksNoContext;
42pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070043
44// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian518ec112011-05-13 16:21:08 -070046void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070047 setGlThreadSpecific(value);
48}
49
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050/*****************************************************************************/
51
Mathias Agopian6f087122010-09-23 16:38:38 -070052static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -070053 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -070054 char const* const error = "call to OpenGL ES API with "
55 "no current context (logged once per thread)";
56 if (LOG_NDEBUG) {
57 ALOGE(error);
58 } else {
59 LOG_ALWAYS_FATAL(error);
60 }
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040061 if (base::GetBoolProperty("debug.egl.callstack", false)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -080062 CallStack::log(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070063 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070064 }
Mathias Agopian6f087122010-09-23 16:38:38 -070065 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070066}
67
Jesse Hall47743382013-02-08 11:13:46 -080068static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069{
Dan Stozac3289c42014-01-17 11:38:34 -080070 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
71 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
72 for (int hook = 0; hook < numHooks; ++hook) {
73 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
74 }
Mathias Agopian05c53112010-09-23 11:32:52 -070075
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070076 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077}
78
79static pthread_once_t once_control = PTHREAD_ONCE_INIT;
80static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
81
Mathias Agopian518ec112011-05-13 16:21:08 -070082// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083
Jesse Hallb29e5e82012-04-04 16:53:42 -070084egl_display_ptr validate_display(EGLDisplay dpy) {
85 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070086 if (!dp)
Yi Kong48a6cd22018-07-18 10:07:09 -070087 return setError(EGL_BAD_DISPLAY, egl_display_ptr(nullptr));
Mathias Agopian5b287a62011-05-16 18:58:55 -070088 if (!dp->isReady())
Yi Kong48a6cd22018-07-18 10:07:09 -070089 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(nullptr));
Eric Hassold3ede7c12011-03-23 15:59:00 -070090
91 return dp;
92}
93
Jesse Hallb29e5e82012-04-04 16:53:42 -070094egl_display_ptr validate_display_connection(EGLDisplay dpy,
95 egl_connection_t*& cnx) {
Yi Kong48a6cd22018-07-18 10:07:09 -070096 cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -070097 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070098 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -070099 return dp;
100 cnx = &gEGLImpl;
Yi Kong48a6cd22018-07-18 10:07:09 -0700101 if (cnx->dso == nullptr) {
102 return setError(EGL_BAD_CONFIG, egl_display_ptr(nullptr));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700104 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105}
106
Mathias Agopian518ec112011-05-13 16:21:08 -0700107// ----------------------------------------------------------------------------
108
Mathias Agopian48d438d2012-01-28 21:44:00 -0800109const GLubyte * egl_get_string_for_current_context(GLenum name) {
110 // NOTE: returning NULL here will fall-back to the default
111 // implementation.
112
113 EGLContext context = egl_tls_t::getContext();
114 if (context == EGL_NO_CONTEXT)
Yi Kong48a6cd22018-07-18 10:07:09 -0700115 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800116
117 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700118 if (c == nullptr) // this should never happen, by construction
119 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800120
121 if (name != GL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700122 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800123
Mathias Agopian65421432017-03-08 11:49:05 -0800124 return (const GLubyte *)c->gl_extensions.c_str();
Mathias Agopian48d438d2012-01-28 21:44:00 -0800125}
126
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700127const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
128 // NOTE: returning NULL here will fall-back to the default
129 // implementation.
130
131 EGLContext context = egl_tls_t::getContext();
132 if (context == EGL_NO_CONTEXT)
Yi Kong48a6cd22018-07-18 10:07:09 -0700133 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700134
135 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700136 if (c == nullptr) // this should never happen, by construction
137 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700138
139 if (name != GL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700140 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700141
142 // if index is out of bounds, assume it will be in the default
143 // implementation too, so we don't have to generate a GL error here
144 if (index >= c->tokenized_gl_extensions.size())
Yi Kong48a6cd22018-07-18 10:07:09 -0700145 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700146
Mathias Agopian65421432017-03-08 11:49:05 -0800147 return (const GLubyte *)c->tokenized_gl_extensions[index].c_str();
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700148}
149
150GLint egl_get_num_extensions_for_current_context() {
151 // NOTE: returning -1 here will fall-back to the default
152 // implementation.
153
154 EGLContext context = egl_tls_t::getContext();
155 if (context == EGL_NO_CONTEXT)
156 return -1;
157
158 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700159 if (c == nullptr) // this should never happen, by construction
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700160 return -1;
161
162 return (GLint)c->tokenized_gl_extensions.size();
163}
164
Cody Northrop68d10352018-10-15 07:22:09 -0600165egl_connection_t* egl_get_connection() {
166 return &gEGLImpl;
167}
168
Mathias Agopian48d438d2012-01-28 21:44:00 -0800169// ----------------------------------------------------------------------------
170
Mathias Agopian518ec112011-05-13 16:21:08 -0700171static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700173 // initialized by static ctor. should be set here.
174 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 }
176
Mathias Agopiande586972009-05-28 17:39:03 -0700177 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700178 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700179
Mathias Agopianada798b2012-02-13 17:09:30 -0800180 // dynamically load our EGL implementation
181 egl_connection_t* cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700182 cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX];
183 cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX];
184 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185
Cody Northrop629ce4e2018-10-15 07:22:09 -0600186 // Check to see if any layers are enabled and route functions through them
187 if (cnx->dso) {
188 // Layers can be enabled long after the drivers have been loaded.
189 // They will only be initialized once.
190 LayerLoader& layer_loader(LayerLoader::getInstance());
191 layer_loader.InitLayers(cnx);
192 }
193
Mathias Agopianada798b2012-02-13 17:09:30 -0800194 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
Charlie Lao95c7aea2020-01-10 11:12:03 -0800197
Charlie Laoc51d5f52020-01-14 11:05:53 -0800198// this mutex protects driver load logic as a critical section since it accesses to global variable
199// like gEGLImpl
Mathias Agopian518ec112011-05-13 16:21:08 -0700200static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
201
202EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700203 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700204 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700205 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700206 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700207 return res;
208}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209
Michael Lentine12c4bda2014-09-11 12:24:13 -0700210static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian65421432017-03-08 11:49:05 -0800211static std::chrono::steady_clock::time_point sLogPrintTime;
212static constexpr std::chrono::seconds DURATION(1);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700213
Mathias Agopian1cadb252011-05-23 17:26:14 -0700214void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700215 bool printLog = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800216 auto now = std::chrono::steady_clock::now();
Michael Lentine12c4bda2014-09-11 12:24:13 -0700217 pthread_mutex_lock(&sLogPrintMutex);
Mathias Agopian65421432017-03-08 11:49:05 -0800218 if ((now - sLogPrintTime) > DURATION) {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700219 sLogPrintTime = now;
220 printLog = true;
221 }
222 pthread_mutex_unlock(&sLogPrintMutex);
223 if (printLog) {
224 ALOGE("called unimplemented OpenGL ES API");
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400225 if (base::GetBoolProperty("debug.egl.callstack", false)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800226 CallStack::log(LOG_TAG);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700227 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700228 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700229}
230
Mathias Agopian48d438d2012-01-28 21:44:00 -0800231void gl_noop() {
232}
233
Mathias Agopian1cadb252011-05-23 17:26:14 -0700234// ----------------------------------------------------------------------------
235
Mathias Agopian1cadb252011-05-23 17:26:14 -0700236void setGlThreadSpecific(gl_hooks_t const *value) {
237 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
238 tls_hooks[TLS_SLOT_OPENGL_API] = value;
239}
240
Mathias Agopian1cadb252011-05-23 17:26:14 -0700241// ----------------------------------------------------------------------------
242// GL / EGL hooks
243// ----------------------------------------------------------------------------
244
245#undef GL_ENTRY
246#undef EGL_ENTRY
247#define GL_ENTRY(_r, _api, ...) #_api,
248#define EGL_ENTRY(_r, _api, ...) #_api,
249
250char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700251 #include "../entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700252 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700253};
254
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700255char const * const gl_names_1[] = {
256 #include "../entries_gles1.in"
257 nullptr
258};
259
Mathias Agopian1cadb252011-05-23 17:26:14 -0700260char const * const egl_names[] = {
261 #include "egl_entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700262 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700263};
264
Cody Northrop68d10352018-10-15 07:22:09 -0600265char const * const platform_names[] = {
266 #include "platform_entries.in"
267 nullptr
268};
269
Mathias Agopian1cadb252011-05-23 17:26:14 -0700270#undef GL_ENTRY
271#undef EGL_ENTRY
272
273
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700274// ----------------------------------------------------------------------------
275}; // namespace android
276// ----------------------------------------------------------------------------
277