blob: d5d57d7c9a6da986a03818753648312933a0f610 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <cutils/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 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070061 char value[PROPERTY_VALUE_MAX];
62 property_get("debug.egl.callstack", value, "0");
John Reck1f246d72014-04-24 23:34:32 +000063 if (atoi(value)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -080064 CallStack::log(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070065 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070066 }
Mathias Agopian6f087122010-09-23 16:38:38 -070067 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070068}
69
Jesse Hall47743382013-02-08 11:13:46 -080070static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071{
Dan Stozac3289c42014-01-17 11:38:34 -080072 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
73 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
74 for (int hook = 0; hook < numHooks; ++hook) {
75 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
76 }
Mathias Agopian05c53112010-09-23 11:32:52 -070077
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070078 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079}
80
81static pthread_once_t once_control = PTHREAD_ONCE_INIT;
82static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
83
Mathias Agopian518ec112011-05-13 16:21:08 -070084// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085
Jesse Hallb29e5e82012-04-04 16:53:42 -070086egl_display_ptr validate_display(EGLDisplay dpy) {
87 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070088 if (!dp)
Yi Kong48a6cd22018-07-18 10:07:09 -070089 return setError(EGL_BAD_DISPLAY, egl_display_ptr(nullptr));
Mathias Agopian5b287a62011-05-16 18:58:55 -070090 if (!dp->isReady())
Yi Kong48a6cd22018-07-18 10:07:09 -070091 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(nullptr));
Eric Hassold3ede7c12011-03-23 15:59:00 -070092
93 return dp;
94}
95
Jesse Hallb29e5e82012-04-04 16:53:42 -070096egl_display_ptr validate_display_connection(EGLDisplay dpy,
97 egl_connection_t*& cnx) {
Yi Kong48a6cd22018-07-18 10:07:09 -070098 cnx = nullptr;
Jesse Hallb29e5e82012-04-04 16:53:42 -070099 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700100 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700101 return dp;
102 cnx = &gEGLImpl;
Yi Kong48a6cd22018-07-18 10:07:09 -0700103 if (cnx->dso == nullptr) {
104 return setError(EGL_BAD_CONFIG, egl_display_ptr(nullptr));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700106 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107}
108
Mathias Agopian518ec112011-05-13 16:21:08 -0700109// ----------------------------------------------------------------------------
110
Mathias Agopian48d438d2012-01-28 21:44:00 -0800111const GLubyte * egl_get_string_for_current_context(GLenum name) {
112 // NOTE: returning NULL here will fall-back to the default
113 // implementation.
114
115 EGLContext context = egl_tls_t::getContext();
116 if (context == EGL_NO_CONTEXT)
Yi Kong48a6cd22018-07-18 10:07:09 -0700117 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800118
119 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700120 if (c == nullptr) // this should never happen, by construction
121 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800122
123 if (name != GL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700124 return nullptr;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800125
Mathias Agopian65421432017-03-08 11:49:05 -0800126 return (const GLubyte *)c->gl_extensions.c_str();
Mathias Agopian48d438d2012-01-28 21:44:00 -0800127}
128
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700129const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
130 // NOTE: returning NULL here will fall-back to the default
131 // implementation.
132
133 EGLContext context = egl_tls_t::getContext();
134 if (context == EGL_NO_CONTEXT)
Yi Kong48a6cd22018-07-18 10:07:09 -0700135 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700136
137 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700138 if (c == nullptr) // this should never happen, by construction
139 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700140
141 if (name != GL_EXTENSIONS)
Yi Kong48a6cd22018-07-18 10:07:09 -0700142 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700143
144 // if index is out of bounds, assume it will be in the default
145 // implementation too, so we don't have to generate a GL error here
146 if (index >= c->tokenized_gl_extensions.size())
Yi Kong48a6cd22018-07-18 10:07:09 -0700147 return nullptr;
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700148
Mathias Agopian65421432017-03-08 11:49:05 -0800149 return (const GLubyte *)c->tokenized_gl_extensions[index].c_str();
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700150}
151
152GLint egl_get_num_extensions_for_current_context() {
153 // NOTE: returning -1 here will fall-back to the default
154 // implementation.
155
156 EGLContext context = egl_tls_t::getContext();
157 if (context == EGL_NO_CONTEXT)
158 return -1;
159
160 egl_context_t const * const c = get_context(context);
Yi Kong48a6cd22018-07-18 10:07:09 -0700161 if (c == nullptr) // this should never happen, by construction
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700162 return -1;
163
164 return (GLint)c->tokenized_gl_extensions.size();
165}
166
Cody Northrop68d10352018-10-15 07:22:09 -0600167egl_connection_t* egl_get_connection() {
168 return &gEGLImpl;
169}
170
Mathias Agopian48d438d2012-01-28 21:44:00 -0800171// ----------------------------------------------------------------------------
172
Mathias Agopian518ec112011-05-13 16:21:08 -0700173static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700175 // initialized by static ctor. should be set here.
176 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 }
178
Mathias Agopiande586972009-05-28 17:39:03 -0700179 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700180 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700181
Mathias Agopianada798b2012-02-13 17:09:30 -0800182 // dynamically load our EGL implementation
183 egl_connection_t* cnx = &gEGLImpl;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700184 cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX];
185 cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX];
186 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187
Cody Northrop629ce4e2018-10-15 07:22:09 -0600188 // Check to see if any layers are enabled and route functions through them
189 if (cnx->dso) {
190 // Layers can be enabled long after the drivers have been loaded.
191 // They will only be initialized once.
192 LayerLoader& layer_loader(LayerLoader::getInstance());
193 layer_loader.InitLayers(cnx);
194 }
195
Mathias Agopianada798b2012-02-13 17:09:30 -0800196 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
198
Charlie Lao95c7aea2020-01-10 11:12:03 -0800199
Charlie Laoc51d5f52020-01-14 11:05:53 -0800200// this mutex protects driver load logic as a critical section since it accesses to global variable
201// like gEGLImpl
Mathias Agopian518ec112011-05-13 16:21:08 -0700202static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
203
204EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700205 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700206 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700207 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700208 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700209 return res;
210}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211
Michael Lentine12c4bda2014-09-11 12:24:13 -0700212static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian65421432017-03-08 11:49:05 -0800213static std::chrono::steady_clock::time_point sLogPrintTime;
214static constexpr std::chrono::seconds DURATION(1);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700215
Mathias Agopian1cadb252011-05-23 17:26:14 -0700216void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700217 bool printLog = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800218 auto now = std::chrono::steady_clock::now();
Michael Lentine12c4bda2014-09-11 12:24:13 -0700219 pthread_mutex_lock(&sLogPrintMutex);
Mathias Agopian65421432017-03-08 11:49:05 -0800220 if ((now - sLogPrintTime) > DURATION) {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700221 sLogPrintTime = now;
222 printLog = true;
223 }
224 pthread_mutex_unlock(&sLogPrintMutex);
225 if (printLog) {
226 ALOGE("called unimplemented OpenGL ES API");
227 char value[PROPERTY_VALUE_MAX];
228 property_get("debug.egl.callstack", value, "0");
229 if (atoi(value)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800230 CallStack::log(LOG_TAG);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700231 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700232 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700233}
234
Mathias Agopian48d438d2012-01-28 21:44:00 -0800235void gl_noop() {
236}
237
Mathias Agopian1cadb252011-05-23 17:26:14 -0700238// ----------------------------------------------------------------------------
239
Mathias Agopian1cadb252011-05-23 17:26:14 -0700240void setGlThreadSpecific(gl_hooks_t const *value) {
241 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
242 tls_hooks[TLS_SLOT_OPENGL_API] = value;
243}
244
Mathias Agopian1cadb252011-05-23 17:26:14 -0700245// ----------------------------------------------------------------------------
246// GL / EGL hooks
247// ----------------------------------------------------------------------------
248
249#undef GL_ENTRY
250#undef EGL_ENTRY
251#define GL_ENTRY(_r, _api, ...) #_api,
252#define EGL_ENTRY(_r, _api, ...) #_api,
253
254char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700255 #include "../entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700256 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700257};
258
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700259char const * const gl_names_1[] = {
260 #include "../entries_gles1.in"
261 nullptr
262};
263
Mathias Agopian1cadb252011-05-23 17:26:14 -0700264char const * const egl_names[] = {
265 #include "egl_entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700266 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700267};
268
Cody Northrop68d10352018-10-15 07:22:09 -0600269char const * const platform_names[] = {
270 #include "platform_entries.in"
271 nullptr
272};
273
Mathias Agopian1cadb252011-05-23 17:26:14 -0700274#undef GL_ENTRY
275#undef EGL_ENTRY
276
277
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700278// ----------------------------------------------------------------------------
279}; // namespace android
280// ----------------------------------------------------------------------------
281