blob: a3e3859bba24f0ee21c27182e3fffc3aaf66f5c7 [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
Yiwei Zhang0657fba2020-08-12 19:09:26 -070084egl_display_t* validate_display(EGLDisplay dpy) {
85 egl_display_t* const dp = get_display(dpy);
86 if (!dp) return setError(EGL_BAD_DISPLAY, (egl_display_t*)nullptr);
87 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (egl_display_t*)nullptr);
Eric Hassold3ede7c12011-03-23 15:59:00 -070088
89 return dp;
90}
91
Yiwei Zhang0657fba2020-08-12 19:09:26 -070092egl_display_t* validate_display_connection(EGLDisplay dpy, egl_connection_t** outCnx) {
93 *outCnx = nullptr;
94 egl_display_t* dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070095 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -070096 return dp;
Yiwei Zhang0657fba2020-08-12 19:09:26 -070097 *outCnx = &gEGLImpl;
98 if ((*outCnx)->dso == nullptr) {
99 return setError(EGL_BAD_CONFIG, (egl_display_t*)nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700101 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102}
103
Mathias Agopian518ec112011-05-13 16:21:08 -0700104// ----------------------------------------------------------------------------
105
Mathias Agopian48d438d2012-01-28 21:44:00 -0800106const GLubyte * egl_get_string_for_current_context(GLenum name) {
107 // NOTE: returning NULL here will fall-back to the default
108 // implementation.
109
110 EGLContext context = egl_tls_t::getContext();
111 if (context == EGL_NO_CONTEXT)
Yi Kong48a6cd22018-07-18 10:07:09 -0700112 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800113
114 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700115 if (c == nullptr) // this should never happen, by construction
116 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800117
118 if (name != GL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700119 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800120
Mathias Agopian65421432017-03-08 11:49:05 -0800121 return (const GLubyte *)c->gl_extensions.c_str();
Mathias Agopian48d438d2012-01-28 21:44:00 -0800122}
123
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700124const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
125 // NOTE: returning NULL here will fall-back to the default
126 // implementation.
127
128 EGLContext context = egl_tls_t::getContext();
129 if (context == EGL_NO_CONTEXT)
Yi Kong48a6cd22018-07-18 10:07:09 -0700130 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700131
132 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700133 if (c == nullptr) // this should never happen, by construction
134 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700135
136 if (name != GL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700137 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700138
139 // if index is out of bounds, assume it will be in the default
140 // implementation too, so we don't have to generate a GL error here
141 if (index >= c->tokenized_gl_extensions.size())
Yi Kong48a6cd22018-07-18 10:07:09 -0700142 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700143
Mathias Agopian65421432017-03-08 11:49:05 -0800144 return (const GLubyte *)c->tokenized_gl_extensions[index].c_str();
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700145}
146
147GLint egl_get_num_extensions_for_current_context() {
148 // NOTE: returning -1 here will fall-back to the default
149 // implementation.
150
151 EGLContext context = egl_tls_t::getContext();
152 if (context == EGL_NO_CONTEXT)
153 return -1;
154
155 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700156 if (c == nullptr) // this should never happen, by construction
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700157 return -1;
158
159 return (GLint)c->tokenized_gl_extensions.size();
160}
161
Cody Northrop68d10352018-10-15 07:22:09 -0600162egl_connection_t* egl_get_connection() {
163 return &gEGLImpl;
164}
165
Mathias Agopian48d438d2012-01-28 21:44:00 -0800166// ----------------------------------------------------------------------------
167
Mathias Agopian518ec112011-05-13 16:21:08 -0700168static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700170 // initialized by static ctor. should be set here.
171 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 }
173
Mathias Agopiande586972009-05-28 17:39:03 -0700174 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700175 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700176
Mathias Agopianada798b2012-02-13 17:09:30 -0800177 // dynamically load our EGL implementation
178 egl_connection_t* cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700179 cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX];
180 cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX];
181 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182
Cody Northrop629ce4e2018-10-15 07:22:09 -0600183 // Check to see if any layers are enabled and route functions through them
184 if (cnx->dso) {
185 // Layers can be enabled long after the drivers have been loaded.
186 // They will only be initialized once.
187 LayerLoader& layer_loader(LayerLoader::getInstance());
188 layer_loader.InitLayers(cnx);
189 }
190
Mathias Agopianada798b2012-02-13 17:09:30 -0800191 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
Charlie Lao95c7aea2020-01-10 11:12:03 -0800194
Charlie Laoc51d5f52020-01-14 11:05:53 -0800195// this mutex protects driver load logic as a critical section since it accesses to global variable
196// like gEGLImpl
Mathias Agopian518ec112011-05-13 16:21:08 -0700197static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
198
199EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700200 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700201 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700202 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700203 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700204 return res;
205}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700206
Michael Lentine12c4bda2014-09-11 12:24:13 -0700207static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian65421432017-03-08 11:49:05 -0800208static std::chrono::steady_clock::time_point sLogPrintTime;
209static constexpr std::chrono::seconds DURATION(1);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700210
Mathias Agopian1cadb252011-05-23 17:26:14 -0700211void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700212 bool printLog = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800213 auto now = std::chrono::steady_clock::now();
Michael Lentine12c4bda2014-09-11 12:24:13 -0700214 pthread_mutex_lock(&sLogPrintMutex);
Mathias Agopian65421432017-03-08 11:49:05 -0800215 if ((now - sLogPrintTime) > DURATION) {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700216 sLogPrintTime = now;
217 printLog = true;
218 }
219 pthread_mutex_unlock(&sLogPrintMutex);
220 if (printLog) {
221 ALOGE("called unimplemented OpenGL ES API");
Michael Hoisie4e0f56b2020-04-30 18:40:55 -0400222 if (base::GetBoolProperty("debug.egl.callstack", false)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800223 CallStack::log(LOG_TAG);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700224 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700225 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700226}
227
Mathias Agopian48d438d2012-01-28 21:44:00 -0800228void gl_noop() {
229}
230
Mathias Agopian1cadb252011-05-23 17:26:14 -0700231// ----------------------------------------------------------------------------
232
Mathias Agopian1cadb252011-05-23 17:26:14 -0700233void setGlThreadSpecific(gl_hooks_t const *value) {
234 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
235 tls_hooks[TLS_SLOT_OPENGL_API] = value;
236}
237
Mathias Agopian1cadb252011-05-23 17:26:14 -0700238// ----------------------------------------------------------------------------
239// GL / EGL hooks
240// ----------------------------------------------------------------------------
241
242#undef GL_ENTRY
243#undef EGL_ENTRY
244#define GL_ENTRY(_r, _api, ...) #_api,
245#define EGL_ENTRY(_r, _api, ...) #_api,
246
247char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700248 #include "../entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700249 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700250};
251
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700252char const * const gl_names_1[] = {
253 #include "../entries_gles1.in"
254 nullptr
255};
256
Mathias Agopian1cadb252011-05-23 17:26:14 -0700257char const * const egl_names[] = {
258 #include "egl_entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700259 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700260};
261
Cody Northrop68d10352018-10-15 07:22:09 -0600262char const * const platform_names[] = {
263 #include "platform_entries.in"
264 nullptr
265};
266
Mathias Agopian1cadb252011-05-23 17:26:14 -0700267#undef GL_ENTRY
268#undef EGL_ENTRY
269
270
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700271// ----------------------------------------------------------------------------
272}; // namespace android
273// ----------------------------------------------------------------------------
274