blob: 25b1009ba2ef762001cd088e4b126c8f775954d0 [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;
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700190 cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX];
191 cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX];
192 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193
Cody Northrop629ce4e2018-10-15 07:22:09 -0600194 // Check to see if any layers are enabled and route functions through them
195 if (cnx->dso) {
196 // Layers can be enabled long after the drivers have been loaded.
197 // They will only be initialized once.
198 LayerLoader& layer_loader(LayerLoader::getInstance());
199 layer_loader.InitLayers(cnx);
200 }
201
Mathias Agopianada798b2012-02-13 17:09:30 -0800202 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203}
204
Mathias Agopian518ec112011-05-13 16:21:08 -0700205static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
206
207EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700208 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700209 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700210 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700211 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700212 return res;
213}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700214
Michael Lentine12c4bda2014-09-11 12:24:13 -0700215static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian65421432017-03-08 11:49:05 -0800216static std::chrono::steady_clock::time_point sLogPrintTime;
217static constexpr std::chrono::seconds DURATION(1);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700218
Mathias Agopian1cadb252011-05-23 17:26:14 -0700219void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700220 bool printLog = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800221 auto now = std::chrono::steady_clock::now();
Michael Lentine12c4bda2014-09-11 12:24:13 -0700222 pthread_mutex_lock(&sLogPrintMutex);
Mathias Agopian65421432017-03-08 11:49:05 -0800223 if ((now - sLogPrintTime) > DURATION) {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700224 sLogPrintTime = now;
225 printLog = true;
226 }
227 pthread_mutex_unlock(&sLogPrintMutex);
228 if (printLog) {
229 ALOGE("called unimplemented OpenGL ES API");
230 char value[PROPERTY_VALUE_MAX];
231 property_get("debug.egl.callstack", value, "0");
232 if (atoi(value)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -0800233 CallStack::log(LOG_TAG);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700234 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700235 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700236}
237
Mathias Agopian48d438d2012-01-28 21:44:00 -0800238void gl_noop() {
239}
240
Mathias Agopian1cadb252011-05-23 17:26:14 -0700241// ----------------------------------------------------------------------------
242
Mathias Agopian1cadb252011-05-23 17:26:14 -0700243void setGlThreadSpecific(gl_hooks_t const *value) {
244 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
245 tls_hooks[TLS_SLOT_OPENGL_API] = value;
246}
247
Mathias Agopian1cadb252011-05-23 17:26:14 -0700248// ----------------------------------------------------------------------------
249// GL / EGL hooks
250// ----------------------------------------------------------------------------
251
252#undef GL_ENTRY
253#undef EGL_ENTRY
254#define GL_ENTRY(_r, _api, ...) #_api,
255#define EGL_ENTRY(_r, _api, ...) #_api,
256
257char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700258 #include "../entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700259 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700260};
261
Yiwei Zhang7cc58922018-10-10 11:37:43 -0700262char const * const gl_names_1[] = {
263 #include "../entries_gles1.in"
264 nullptr
265};
266
Mathias Agopian1cadb252011-05-23 17:26:14 -0700267char const * const egl_names[] = {
268 #include "egl_entries.in"
Yi Kong48a6cd22018-07-18 10:07:09 -0700269 nullptr
Mathias Agopian1cadb252011-05-23 17:26:14 -0700270};
271
Cody Northrop68d10352018-10-15 07:22:09 -0600272char const * const platform_names[] = {
273 #include "platform_entries.in"
274 nullptr
275};
276
Mathias Agopian1cadb252011-05-23 17:26:14 -0700277#undef GL_ENTRY
278#undef EGL_ENTRY
279
280
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700281// ----------------------------------------------------------------------------
282}; // namespace android
283// ----------------------------------------------------------------------------
284