blob: e5b9e14bd5fef60e53f2aeba0d2bfc022359b3ec [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
Yiwei Zhang8af03062020-08-12 21:28:15 -070017#include <EGL/egl.h>
18#include <android-base/properties.h>
19#include <log/log.h>
Mathias Agopiand8fb7b52009-05-17 18:50:16 -070020#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021
Mathias Agopian39c24a22013-04-04 23:17:56 -070022#include "../egl_impl.h"
Mathias Agopian5f1af042017-03-09 18:50:05 -080023#include "CallStack.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080024#include "Loader.h"
Yiwei Zhang8af03062020-08-12 21:28:15 -070025#include "egl_display.h"
26#include "egl_layers.h"
27#include "egl_object.h"
28#include "egl_tls.h"
29#include "egldefs.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Mathias Agopianada798b2012-02-13 17:09:30 -080033egl_connection_t gEGLImpl;
34gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070035gl_hooks_t gHooksNoContext;
36pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037
Yiwei Zhang8af03062020-08-12 21:28:15 -070038void setGLHooksThreadSpecific(gl_hooks_t const* value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070039 setGlThreadSpecific(value);
40}
41
Mathias Agopian6f087122010-09-23 16:38:38 -070042static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -070043 if (egl_tls_t::logNoContextCall()) {
Yiwei Zhang8af03062020-08-12 21:28:15 -070044 const char* const error = "call to OpenGL ES API with "
45 "no current context (logged once per thread)";
Mathias Agopian455e3602012-09-26 17:19:48 -070046 if (LOG_NDEBUG) {
47 ALOGE(error);
48 } else {
49 LOG_ALWAYS_FATAL(error);
50 }
Michael Hoisie4e0f56b2020-04-30 18:40:55 -040051 if (base::GetBoolProperty("debug.egl.callstack", false)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -080052 CallStack::log(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070053 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070054 }
Mathias Agopian6f087122010-09-23 16:38:38 -070055 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070056}
57
Yiwei Zhang8af03062020-08-12 21:28:15 -070058static void early_egl_init(void) {
Dan Stozac3289c42014-01-17 11:38:34 -080059 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
Yiwei Zhang8af03062020-08-12 21:28:15 -070060 EGLFuncPointer* iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
Dan Stozac3289c42014-01-17 11:38:34 -080061 for (int hook = 0; hook < numHooks; ++hook) {
62 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
63 }
Mathias Agopian05c53112010-09-23 11:32:52 -070064
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070065 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066}
67
Yiwei Zhang8af03062020-08-12 21:28:15 -070068const GLubyte* egl_get_string_for_current_context(GLenum name) {
Mathias Agopian48d438d2012-01-28 21:44:00 -080069 // NOTE: returning NULL here will fall-back to the default
70 // implementation.
71
72 EGLContext context = egl_tls_t::getContext();
Yiwei Zhang8af03062020-08-12 21:28:15 -070073 if (context == EGL_NO_CONTEXT) return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -080074
Yiwei Zhang8af03062020-08-12 21:28:15 -070075 const egl_context_t* const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -070076 if (c == nullptr) // this should never happen, by construction
77 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -080078
Yiwei Zhang8af03062020-08-12 21:28:15 -070079 if (name != GL_EXTENSIONS) return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -080080
Yiwei Zhang8af03062020-08-12 21:28:15 -070081 return (const GLubyte*)c->gl_extensions.c_str();
Mathias Agopian48d438d2012-01-28 21:44:00 -080082}
83
Yiwei Zhang8af03062020-08-12 21:28:15 -070084const GLubyte* egl_get_string_for_current_context(GLenum name, GLuint index) {
Alistair Strachanedfe72e2015-05-22 14:10:09 -070085 // NOTE: returning NULL here will fall-back to the default
86 // implementation.
87
88 EGLContext context = egl_tls_t::getContext();
Yiwei Zhang8af03062020-08-12 21:28:15 -070089 if (context == EGL_NO_CONTEXT) return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -070090
Yiwei Zhang8af03062020-08-12 21:28:15 -070091 const egl_context_t* const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -070092 if (c == nullptr) // this should never happen, by construction
93 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -070094
Yiwei Zhang8af03062020-08-12 21:28:15 -070095 if (name != GL_EXTENSIONS) return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -070096
97 // if index is out of bounds, assume it will be in the default
98 // implementation too, so we don't have to generate a GL error here
Yiwei Zhang8af03062020-08-12 21:28:15 -070099 if (index >= c->tokenized_gl_extensions.size()) return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700100
Yiwei Zhang8af03062020-08-12 21:28:15 -0700101 return (const GLubyte*)c->tokenized_gl_extensions[index].c_str();
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700102}
103
104GLint egl_get_num_extensions_for_current_context() {
105 // NOTE: returning -1 here will fall-back to the default
106 // implementation.
107
108 EGLContext context = egl_tls_t::getContext();
Yiwei Zhang8af03062020-08-12 21:28:15 -0700109 if (context == EGL_NO_CONTEXT) return -1;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700110
Yiwei Zhang8af03062020-08-12 21:28:15 -0700111 const egl_context_t* const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700112 if (c == nullptr) // this should never happen, by construction
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700113 return -1;
114
115 return (GLint)c->tokenized_gl_extensions.size();
116}
117
Cody Northrop68d10352018-10-15 07:22:09 -0600118egl_connection_t* egl_get_connection() {
119 return &gEGLImpl;
120}
121
Yiwei Zhang8af03062020-08-12 21:28:15 -0700122static pthread_once_t once_control = PTHREAD_ONCE_INIT;
123static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
Mathias Agopian48d438d2012-01-28 21:44:00 -0800124
Mathias Agopian518ec112011-05-13 16:21:08 -0700125static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700127 // initialized by static ctor. should be set here.
128 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 }
130
Mathias Agopiande586972009-05-28 17:39:03 -0700131 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700132 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700133
Mathias Agopianada798b2012-02-13 17:09:30 -0800134 // dynamically load our EGL implementation
135 egl_connection_t* cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700136 cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX];
137 cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX];
138 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139
Cody Northrop629ce4e2018-10-15 07:22:09 -0600140 // Check to see if any layers are enabled and route functions through them
141 if (cnx->dso) {
142 // Layers can be enabled long after the drivers have been loaded.
143 // They will only be initialized once.
144 LayerLoader& layer_loader(LayerLoader::getInstance());
145 layer_loader.InitLayers(cnx);
146 }
147
Mathias Agopianada798b2012-02-13 17:09:30 -0800148 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149}
150
Charlie Laoc51d5f52020-01-14 11:05:53 -0800151// this mutex protects driver load logic as a critical section since it accesses to global variable
152// like gEGLImpl
Mathias Agopian518ec112011-05-13 16:21:08 -0700153static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
154
155EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700156 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700157 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700158 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700159 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700160 return res;
161}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700162
Michael Lentine12c4bda2014-09-11 12:24:13 -0700163static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian65421432017-03-08 11:49:05 -0800164static std::chrono::steady_clock::time_point sLogPrintTime;
165static constexpr std::chrono::seconds DURATION(1);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700166
Mathias Agopian1cadb252011-05-23 17:26:14 -0700167void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700168 bool printLog = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800169 auto now = std::chrono::steady_clock::now();
Michael Lentine12c4bda2014-09-11 12:24:13 -0700170 pthread_mutex_lock(&sLogPrintMutex);
Mathias Agopian65421432017-03-08 11:49:05 -0800171 if ((now - sLogPrintTime) > DURATION) {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700172 sLogPrintTime = now;
173 printLog = true;
174 }
175 pthread_mutex_unlock(&sLogPrintMutex);
176 if (printLog) {
177 ALOGE("called unimplemented OpenGL ES API");
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400178 if (base::GetBoolProperty("debug.egl.callstack", false)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800179 CallStack::log(LOG_TAG);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700180 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700181 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700182}
183
Yiwei Zhang8af03062020-08-12 21:28:15 -0700184void gl_noop() {}
Mathias Agopian48d438d2012-01-28 21:44:00 -0800185
Yiwei Zhang8af03062020-08-12 21:28:15 -0700186void setGlThreadSpecific(gl_hooks_t const* value) {
187 gl_hooks_t const* volatile* tls_hooks = get_tls_hooks();
Mathias Agopian1cadb252011-05-23 17:26:14 -0700188 tls_hooks[TLS_SLOT_OPENGL_API] = value;
189}
190
Mathias Agopian1cadb252011-05-23 17:26:14 -0700191// ----------------------------------------------------------------------------
192// GL / EGL hooks
193// ----------------------------------------------------------------------------
194
195#undef GL_ENTRY
196#undef EGL_ENTRY
197#define GL_ENTRY(_r, _api, ...) #_api,
198#define EGL_ENTRY(_r, _api, ...) #_api,
199
200char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700201 #include "../entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700202 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700203};
204
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700205char const * const gl_names_1[] = {
206 #include "../entries_gles1.in"
207 nullptr
208};
209
Mathias Agopian1cadb252011-05-23 17:26:14 -0700210char const * const egl_names[] = {
211 #include "egl_entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700212 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700213};
214
Cody Northrop68d10352018-10-15 07:22:09 -0600215char const * const platform_names[] = {
216 #include "platform_entries.in"
217 nullptr
218};
219
Mathias Agopian1cadb252011-05-23 17:26:14 -0700220#undef GL_ENTRY
221#undef EGL_ENTRY
222
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700223}; // namespace android