blob: 34b0ba25162672c09677f5944e40323e1ea569cd [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/atomic.h>
24#include <cutils/properties.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080025
Mark Salyzyn7823e122016-09-29 08:08:05 -070026#include <log/log.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080027
Romain Guye03de932011-07-11 15:33:51 -070028#include <utils/CallStack.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070029
Mathias Agopian39c24a22013-04-04 23:17:56 -070030#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070031
Mathias Agopian39c24a22013-04-04 23:17:56 -070032#include "egldefs.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080033#include "egl_tls.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070034#include "egl_display.h"
35#include "egl_object.h"
Mathias Agopian311b4792017-02-28 15:00:49 -080036#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Dan Stozac3289c42014-01-17 11:38:34 -080038typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer;
39
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040// ----------------------------------------------------------------------------
41namespace android {
42// ----------------------------------------------------------------------------
43
Mathias Agopianada798b2012-02-13 17:09:30 -080044egl_connection_t gEGLImpl;
45gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070046gl_hooks_t gHooksNoContext;
47pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048
49// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Mathias Agopian518ec112011-05-13 16:21:08 -070051void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070052 setGlThreadSpecific(value);
53}
54
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055/*****************************************************************************/
56
Mathias Agopian6f087122010-09-23 16:38:38 -070057static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -070058 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -070059 char const* const error = "call to OpenGL ES API with "
60 "no current context (logged once per thread)";
61 if (LOG_NDEBUG) {
62 ALOGE(error);
63 } else {
64 LOG_ALWAYS_FATAL(error);
65 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070066 char value[PROPERTY_VALUE_MAX];
67 property_get("debug.egl.callstack", value, "0");
John Reck1f246d72014-04-24 23:34:32 +000068 if (atoi(value)) {
Mathias Agopiancab25d62013-03-21 17:12:40 -070069 CallStack stack(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070070 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070071 }
Mathias Agopian6f087122010-09-23 16:38:38 -070072 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070073}
74
Jesse Hall47743382013-02-08 11:13:46 -080075static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076{
Dan Stozac3289c42014-01-17 11:38:34 -080077 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
78 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
79 for (int hook = 0; hook < numHooks; ++hook) {
80 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
81 }
Mathias Agopian05c53112010-09-23 11:32:52 -070082
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070083 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084}
85
86static pthread_once_t once_control = PTHREAD_ONCE_INIT;
87static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
88
Mathias Agopian518ec112011-05-13 16:21:08 -070089// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090
Jesse Hallb29e5e82012-04-04 16:53:42 -070091egl_display_ptr validate_display(EGLDisplay dpy) {
92 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070093 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -070094 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -070095 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -070096 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -070097
98 return dp;
99}
100
Jesse Hallb29e5e82012-04-04 16:53:42 -0700101egl_display_ptr validate_display_connection(EGLDisplay dpy,
102 egl_connection_t*& cnx) {
103 cnx = NULL;
104 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700105 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700106 return dp;
107 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700109 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700111 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112}
113
Mathias Agopian518ec112011-05-13 16:21:08 -0700114// ----------------------------------------------------------------------------
115
Mathias Agopian48d438d2012-01-28 21:44:00 -0800116const GLubyte * egl_get_string_for_current_context(GLenum name) {
117 // NOTE: returning NULL here will fall-back to the default
118 // implementation.
119
120 EGLContext context = egl_tls_t::getContext();
121 if (context == EGL_NO_CONTEXT)
122 return NULL;
123
124 egl_context_t const * const c = get_context(context);
125 if (c == NULL) // this should never happen, by construction
126 return NULL;
127
128 if (name != GL_EXTENSIONS)
129 return NULL;
130
131 return (const GLubyte *)c->gl_extensions.string();
132}
133
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700134const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
135 // NOTE: returning NULL here will fall-back to the default
136 // implementation.
137
138 EGLContext context = egl_tls_t::getContext();
139 if (context == EGL_NO_CONTEXT)
140 return NULL;
141
142 egl_context_t const * const c = get_context(context);
143 if (c == NULL) // this should never happen, by construction
144 return NULL;
145
146 if (name != GL_EXTENSIONS)
147 return NULL;
148
149 // if index is out of bounds, assume it will be in the default
150 // implementation too, so we don't have to generate a GL error here
151 if (index >= c->tokenized_gl_extensions.size())
152 return NULL;
153
154 return (const GLubyte *)c->tokenized_gl_extensions.itemAt(index).string();
155}
156
157GLint egl_get_num_extensions_for_current_context() {
158 // NOTE: returning -1 here will fall-back to the default
159 // implementation.
160
161 EGLContext context = egl_tls_t::getContext();
162 if (context == EGL_NO_CONTEXT)
163 return -1;
164
165 egl_context_t const * const c = get_context(context);
166 if (c == NULL) // this should never happen, by construction
167 return -1;
168
169 return (GLint)c->tokenized_gl_extensions.size();
170}
171
Mathias Agopian48d438d2012-01-28 21:44:00 -0800172// ----------------------------------------------------------------------------
173
Mathias Agopian923c6612009-08-17 18:07:06 -0700174// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700175// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700176// egl_init_drivers_locked()
177//
Mathias Agopian518ec112011-05-13 16:21:08 -0700178static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700180 // initialized by static ctor. should be set here.
181 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 }
183
Mathias Agopiande586972009-05-28 17:39:03 -0700184 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700185 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700186
Mathias Agopianada798b2012-02-13 17:09:30 -0800187 // dynamically load our EGL implementation
188 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800190 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
191 &gHooks[egl_connection_t::GLESv1_INDEX];
192 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
193 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800194 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 }
196
Mathias Agopianada798b2012-02-13 17:09:30 -0800197 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198}
199
Mathias Agopian518ec112011-05-13 16:21:08 -0700200static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
201
202EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700203 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700204 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700205 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700206 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700207 return res;
208}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700209
Michael Lentine12c4bda2014-09-11 12:24:13 -0700210static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
211static nsecs_t sLogPrintTime = 0;
212#define NSECS_DURATION 1000000000
213
Mathias Agopian1cadb252011-05-23 17:26:14 -0700214void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700215 bool printLog = false;
216 nsecs_t now = systemTime();
217 pthread_mutex_lock(&sLogPrintMutex);
218 if ((now - sLogPrintTime) > NSECS_DURATION) {
219 sLogPrintTime = now;
220 printLog = true;
221 }
222 pthread_mutex_unlock(&sLogPrintMutex);
223 if (printLog) {
224 ALOGE("called unimplemented OpenGL ES API");
225 char value[PROPERTY_VALUE_MAX];
226 property_get("debug.egl.callstack", value, "0");
227 if (atoi(value)) {
228 CallStack stack(LOG_TAG);
229 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700230 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700231}
232
Mathias Agopian48d438d2012-01-28 21:44:00 -0800233void gl_noop() {
234}
235
Mathias Agopian1cadb252011-05-23 17:26:14 -0700236// ----------------------------------------------------------------------------
237
Mathias Agopian1cadb252011-05-23 17:26:14 -0700238void setGlThreadSpecific(gl_hooks_t const *value) {
239 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
240 tls_hooks[TLS_SLOT_OPENGL_API] = value;
241}
242
Mathias Agopian1cadb252011-05-23 17:26:14 -0700243// ----------------------------------------------------------------------------
244// GL / EGL hooks
245// ----------------------------------------------------------------------------
246
247#undef GL_ENTRY
248#undef EGL_ENTRY
249#define GL_ENTRY(_r, _api, ...) #_api,
250#define EGL_ENTRY(_r, _api, ...) #_api,
251
252char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700253 #include "../entries.in"
Mathias Agopian1cadb252011-05-23 17:26:14 -0700254 NULL
255};
256
257char const * const egl_names[] = {
258 #include "egl_entries.in"
259 NULL
260};
261
262#undef GL_ENTRY
263#undef EGL_ENTRY
264
265
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700266// ----------------------------------------------------------------------------
267}; // namespace android
268// ----------------------------------------------------------------------------
269