blob: f44c1ca22bb6a4d956535d249fb9b81a135af67b [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
Romain Guye03de932011-07-11 15:33:51 -070027#include <utils/CallStack.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070028
Mathias Agopian39c24a22013-04-04 23:17:56 -070029#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070030
Mathias Agopian39c24a22013-04-04 23:17:56 -070031#include "egldefs.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080032#include "egl_tls.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070033#include "egl_display.h"
34#include "egl_object.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080035#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Dan Stozac3289c42014-01-17 11:38:34 -080037typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer;
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039// ----------------------------------------------------------------------------
40namespace android {
41// ----------------------------------------------------------------------------
42
Mathias Agopianada798b2012-02-13 17:09:30 -080043egl_connection_t gEGLImpl;
44gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070045gl_hooks_t gHooksNoContext;
46pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070047
48// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian518ec112011-05-13 16:21:08 -070050void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070051 setGlThreadSpecific(value);
52}
53
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054/*****************************************************************************/
55
Mathias Agopian6f087122010-09-23 16:38:38 -070056static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -070057 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -070058 char const* const error = "call to OpenGL ES API with "
59 "no current context (logged once per thread)";
60 if (LOG_NDEBUG) {
61 ALOGE(error);
62 } else {
63 LOG_ALWAYS_FATAL(error);
64 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070065 char value[PROPERTY_VALUE_MAX];
66 property_get("debug.egl.callstack", value, "0");
John Reck1f246d72014-04-24 23:34:32 +000067 if (atoi(value)) {
Mathias Agopiancab25d62013-03-21 17:12:40 -070068 CallStack stack(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070069 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070070 }
Mathias Agopian6f087122010-09-23 16:38:38 -070071 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070072}
73
Jesse Hall47743382013-02-08 11:13:46 -080074static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075{
Dan Stozac3289c42014-01-17 11:38:34 -080076 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
77 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
78 for (int hook = 0; hook < numHooks; ++hook) {
79 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
80 }
Mathias Agopian05c53112010-09-23 11:32:52 -070081
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070082 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083}
84
85static pthread_once_t once_control = PTHREAD_ONCE_INIT;
86static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
87
Mathias Agopian518ec112011-05-13 16:21:08 -070088// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089
Jesse Hallb29e5e82012-04-04 16:53:42 -070090egl_display_ptr validate_display(EGLDisplay dpy) {
91 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070092 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -070093 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -070094 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -070095 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -070096
97 return dp;
98}
99
Jesse Hallb29e5e82012-04-04 16:53:42 -0700100egl_display_ptr validate_display_connection(EGLDisplay dpy,
101 egl_connection_t*& cnx) {
102 cnx = NULL;
103 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700104 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700105 return dp;
106 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700108 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700110 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111}
112
Mathias Agopian518ec112011-05-13 16:21:08 -0700113// ----------------------------------------------------------------------------
114
Mathias Agopian48d438d2012-01-28 21:44:00 -0800115const GLubyte * egl_get_string_for_current_context(GLenum name) {
116 // NOTE: returning NULL here will fall-back to the default
117 // implementation.
118
119 EGLContext context = egl_tls_t::getContext();
120 if (context == EGL_NO_CONTEXT)
121 return NULL;
122
123 egl_context_t const * const c = get_context(context);
124 if (c == NULL) // this should never happen, by construction
125 return NULL;
126
127 if (name != GL_EXTENSIONS)
128 return NULL;
129
Mathias Agopian65421432017-03-08 11:49:05 -0800130 return (const GLubyte *)c->gl_extensions.c_str();
Mathias Agopian48d438d2012-01-28 21:44:00 -0800131}
132
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700133const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
134 // NOTE: returning NULL here will fall-back to the default
135 // implementation.
136
137 EGLContext context = egl_tls_t::getContext();
138 if (context == EGL_NO_CONTEXT)
139 return NULL;
140
141 egl_context_t const * const c = get_context(context);
142 if (c == NULL) // this should never happen, by construction
143 return NULL;
144
145 if (name != GL_EXTENSIONS)
146 return NULL;
147
148 // if index is out of bounds, assume it will be in the default
149 // implementation too, so we don't have to generate a GL error here
150 if (index >= c->tokenized_gl_extensions.size())
151 return NULL;
152
Mathias Agopian65421432017-03-08 11:49:05 -0800153 return (const GLubyte *)c->tokenized_gl_extensions[index].c_str();
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700154}
155
156GLint egl_get_num_extensions_for_current_context() {
157 // NOTE: returning -1 here will fall-back to the default
158 // implementation.
159
160 EGLContext context = egl_tls_t::getContext();
161 if (context == EGL_NO_CONTEXT)
162 return -1;
163
164 egl_context_t const * const c = get_context(context);
165 if (c == NULL) // this should never happen, by construction
166 return -1;
167
168 return (GLint)c->tokenized_gl_extensions.size();
169}
170
Mathias Agopian48d438d2012-01-28 21:44:00 -0800171// ----------------------------------------------------------------------------
172
Mathias Agopian923c6612009-08-17 18:07:06 -0700173// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700174// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700175// egl_init_drivers_locked()
176//
Mathias Agopian518ec112011-05-13 16:21:08 -0700177static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700179 // initialized by static ctor. should be set here.
180 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 }
182
Mathias Agopiande586972009-05-28 17:39:03 -0700183 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700184 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700185
Mathias Agopianada798b2012-02-13 17:09:30 -0800186 // dynamically load our EGL implementation
187 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800189 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
190 &gHooks[egl_connection_t::GLESv1_INDEX];
191 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
192 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800193 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 }
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
Mathias Agopian518ec112011-05-13 16:21:08 -0700199static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
200
201EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700202 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700203 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700204 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700205 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700206 return res;
207}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700208
Michael Lentine12c4bda2014-09-11 12:24:13 -0700209static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
Mathias Agopian65421432017-03-08 11:49:05 -0800210static std::chrono::steady_clock::time_point sLogPrintTime;
211static constexpr std::chrono::seconds DURATION(1);
Michael Lentine12c4bda2014-09-11 12:24:13 -0700212
Mathias Agopian1cadb252011-05-23 17:26:14 -0700213void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700214 bool printLog = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800215 auto now = std::chrono::steady_clock::now();
Michael Lentine12c4bda2014-09-11 12:24:13 -0700216 pthread_mutex_lock(&sLogPrintMutex);
Mathias Agopian65421432017-03-08 11:49:05 -0800217 if ((now - sLogPrintTime) > DURATION) {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700218 sLogPrintTime = now;
219 printLog = true;
220 }
221 pthread_mutex_unlock(&sLogPrintMutex);
222 if (printLog) {
223 ALOGE("called unimplemented OpenGL ES API");
224 char value[PROPERTY_VALUE_MAX];
225 property_get("debug.egl.callstack", value, "0");
226 if (atoi(value)) {
227 CallStack stack(LOG_TAG);
228 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700229 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700230}
231
Mathias Agopian48d438d2012-01-28 21:44:00 -0800232void gl_noop() {
233}
234
Mathias Agopian1cadb252011-05-23 17:26:14 -0700235// ----------------------------------------------------------------------------
236
Mathias Agopian1cadb252011-05-23 17:26:14 -0700237void setGlThreadSpecific(gl_hooks_t const *value) {
238 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
239 tls_hooks[TLS_SLOT_OPENGL_API] = value;
240}
241
Mathias Agopian1cadb252011-05-23 17:26:14 -0700242// ----------------------------------------------------------------------------
243// GL / EGL hooks
244// ----------------------------------------------------------------------------
245
246#undef GL_ENTRY
247#undef EGL_ENTRY
248#define GL_ENTRY(_r, _api, ...) #_api,
249#define EGL_ENTRY(_r, _api, ...) #_api,
250
251char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700252 #include "../entries.in"
Mathias Agopian1cadb252011-05-23 17:26:14 -0700253 NULL
254};
255
256char const * const egl_names[] = {
257 #include "egl_entries.in"
258 NULL
259};
260
261#undef GL_ENTRY
262#undef EGL_ENTRY
263
264
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700265// ----------------------------------------------------------------------------
266}; // namespace android
267// ----------------------------------------------------------------------------
268