blob: f53cf3f8cc9cd1c7a62864125cc5873ecc8161ae [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"
Mathias Agopian5f1af042017-03-09 18:50:05 -080033#include "CallStack.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080034#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
Dan Stozac3289c42014-01-17 11:38:34 -080036typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer;
37
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038// ----------------------------------------------------------------------------
39namespace android {
40// ----------------------------------------------------------------------------
41
Mathias Agopianada798b2012-02-13 17:09:30 -080042egl_connection_t gEGLImpl;
43gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070044gl_hooks_t gHooksNoContext;
45pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046
47// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
Mathias Agopian518ec112011-05-13 16:21:08 -070049void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070050 setGlThreadSpecific(value);
51}
52
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053/*****************************************************************************/
54
Mathias Agopian6f087122010-09-23 16:38:38 -070055static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -070056 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -070057 char const* const error = "call to OpenGL ES API with "
58 "no current context (logged once per thread)";
59 if (LOG_NDEBUG) {
60 ALOGE(error);
61 } else {
62 LOG_ALWAYS_FATAL(error);
63 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070064 char value[PROPERTY_VALUE_MAX];
65 property_get("debug.egl.callstack", value, "0");
John Reck1f246d72014-04-24 23:34:32 +000066 if (atoi(value)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -080067 CallStack::log(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070068 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070069 }
Mathias Agopian6f087122010-09-23 16:38:38 -070070 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070071}
72
Jesse Hall47743382013-02-08 11:13:46 -080073static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074{
Dan Stozac3289c42014-01-17 11:38:34 -080075 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
76 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
77 for (int hook = 0; hook < numHooks; ++hook) {
78 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
79 }
Mathias Agopian05c53112010-09-23 11:32:52 -070080
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070081 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082}
83
84static pthread_once_t once_control = PTHREAD_ONCE_INIT;
85static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
86
Mathias Agopian518ec112011-05-13 16:21:08 -070087// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
Jesse Hallb29e5e82012-04-04 16:53:42 -070089egl_display_ptr validate_display(EGLDisplay dpy) {
90 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070091 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -070092 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -070093 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -070094 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -070095
96 return dp;
97}
98
Jesse Hallb29e5e82012-04-04 16:53:42 -070099egl_display_ptr validate_display_connection(EGLDisplay dpy,
100 egl_connection_t*& cnx) {
101 cnx = NULL;
102 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700103 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700104 return dp;
105 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700107 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700109 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110}
111
Mathias Agopian518ec112011-05-13 16:21:08 -0700112// ----------------------------------------------------------------------------
113
Mathias Agopian48d438d2012-01-28 21:44:00 -0800114const GLubyte * egl_get_string_for_current_context(GLenum name) {
115 // NOTE: returning NULL here will fall-back to the default
116 // implementation.
117
118 EGLContext context = egl_tls_t::getContext();
119 if (context == EGL_NO_CONTEXT)
120 return NULL;
121
122 egl_context_t const * const c = get_context(context);
123 if (c == NULL) // this should never happen, by construction
124 return NULL;
125
126 if (name != GL_EXTENSIONS)
127 return NULL;
128
Mathias Agopian65421432017-03-08 11:49:05 -0800129 return (const GLubyte *)c->gl_extensions.c_str();
Mathias Agopian48d438d2012-01-28 21:44:00 -0800130}
131
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700132const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
133 // NOTE: returning NULL here will fall-back to the default
134 // implementation.
135
136 EGLContext context = egl_tls_t::getContext();
137 if (context == EGL_NO_CONTEXT)
138 return NULL;
139
140 egl_context_t const * const c = get_context(context);
141 if (c == NULL) // this should never happen, by construction
142 return NULL;
143
144 if (name != GL_EXTENSIONS)
145 return NULL;
146
147 // if index is out of bounds, assume it will be in the default
148 // implementation too, so we don't have to generate a GL error here
149 if (index >= c->tokenized_gl_extensions.size())
150 return NULL;
151
Mathias Agopian65421432017-03-08 11:49:05 -0800152 return (const GLubyte *)c->tokenized_gl_extensions[index].c_str();
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700153}
154
155GLint egl_get_num_extensions_for_current_context() {
156 // NOTE: returning -1 here will fall-back to the default
157 // implementation.
158
159 EGLContext context = egl_tls_t::getContext();
160 if (context == EGL_NO_CONTEXT)
161 return -1;
162
163 egl_context_t const * const c = get_context(context);
164 if (c == NULL) // this should never happen, by construction
165 return -1;
166
167 return (GLint)c->tokenized_gl_extensions.size();
168}
169
Mathias Agopian48d438d2012-01-28 21:44:00 -0800170// ----------------------------------------------------------------------------
171
Mathias Agopian923c6612009-08-17 18:07:06 -0700172// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700173// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700174// egl_init_drivers_locked()
175//
Mathias Agopian518ec112011-05-13 16:21:08 -0700176static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700178 // initialized by static ctor. should be set here.
179 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 }
181
Mathias Agopiande586972009-05-28 17:39:03 -0700182 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700183 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700184
Mathias Agopianada798b2012-02-13 17:09:30 -0800185 // dynamically load our EGL implementation
186 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800188 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
189 &gHooks[egl_connection_t::GLESv1_INDEX];
190 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
191 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800192 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 }
194
Mathias Agopianada798b2012-02-13 17:09:30 -0800195 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196}
197
Mathias Agopian518ec112011-05-13 16:21:08 -0700198static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
199
200EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700201 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700202 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700203 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700204 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700205 return res;
206}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700207
Michael Lentine12c4bda2014-09-11 12:24:13 -0700208static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian65421432017-03-08 11:49:05 -0800209static std::chrono::steady_clock::time_point sLogPrintTime;
210static constexpr std::chrono::seconds DURATION(1);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700211
Mathias Agopian1cadb252011-05-23 17:26:14 -0700212void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700213 bool printLog = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800214 auto now = std::chrono::steady_clock::now();
Michael Lentine12c4bda2014-09-11 12:24:13 -0700215 pthread_mutex_lock(&sLogPrintMutex);
Mathias Agopian65421432017-03-08 11:49:05 -0800216 if ((now - sLogPrintTime) > DURATION) {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700217 sLogPrintTime = now;
218 printLog = true;
219 }
220 pthread_mutex_unlock(&sLogPrintMutex);
221 if (printLog) {
222 ALOGE("called unimplemented OpenGL ES API");
223 char value[PROPERTY_VALUE_MAX];
224 property_get("debug.egl.callstack", value, "0");
225 if (atoi(value)) {
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"
Mathias Agopian1cadb252011-05-23 17:26:14 -0700252 NULL
253};
254
255char const * const egl_names[] = {
256 #include "egl_entries.in"
257 NULL
258};
259
260#undef GL_ENTRY
261#undef EGL_ENTRY
262
263
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700264// ----------------------------------------------------------------------------
265}; // namespace android
266// ----------------------------------------------------------------------------
267