blob: ee83ada26de19073bcc717a7e19683639161d7a5 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <ctype.h>
Mathias Agopiand8fb7b52009-05-17 18:50:16 -070018#include <stdlib.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <string.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopian518ec112011-05-13 16:21:08 -070021#include <hardware/gralloc.h>
22#include <system/window.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <cutils/atomic.h>
28#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070029#include <log/log.h>
Romain Guye03de932011-07-11 15:33:51 -070030#include <utils/CallStack.h>
Mathias Agopian24035332010-08-02 17:34:32 -070031#include <utils/String8.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070032
Mathias Agopian39c24a22013-04-04 23:17:56 -070033#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070034
David Li864f8392011-03-28 10:39:28 -070035#include "egl_tls.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070036#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070037#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
Mathias Agopian518ec112011-05-13 16:21:08 -070039#include "egl_display.h"
40#include "egl_object.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
Dan Stozac3289c42014-01-17 11:38:34 -080042typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer;
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044// ----------------------------------------------------------------------------
45namespace android {
46// ----------------------------------------------------------------------------
47
Mathias Agopianada798b2012-02-13 17:09:30 -080048egl_connection_t gEGLImpl;
49gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070050gl_hooks_t gHooksNoContext;
51pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052
53// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian518ec112011-05-13 16:21:08 -070055void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070056 setGlThreadSpecific(value);
57}
58
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059/*****************************************************************************/
60
Mathias Agopian6f087122010-09-23 16:38:38 -070061static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -070062 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -070063 char const* const error = "call to OpenGL ES API with "
64 "no current context (logged once per thread)";
65 if (LOG_NDEBUG) {
66 ALOGE(error);
67 } else {
68 LOG_ALWAYS_FATAL(error);
69 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070070 char value[PROPERTY_VALUE_MAX];
71 property_get("debug.egl.callstack", value, "0");
John Reck1f246d72014-04-24 23:34:32 +000072 if (atoi(value)) {
Mathias Agopiancab25d62013-03-21 17:12:40 -070073 CallStack stack(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070074 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070075 }
Mathias Agopian6f087122010-09-23 16:38:38 -070076 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070077}
78
Jesse Hall47743382013-02-08 11:13:46 -080079static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080{
Dan Stozac3289c42014-01-17 11:38:34 -080081 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
82 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
83 for (int hook = 0; hook < numHooks; ++hook) {
84 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
85 }
Mathias Agopian05c53112010-09-23 11:32:52 -070086
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070087 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088}
89
90static pthread_once_t once_control = PTHREAD_ONCE_INIT;
91static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
92
Mathias Agopian518ec112011-05-13 16:21:08 -070093// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094
Jesse Hallb29e5e82012-04-04 16:53:42 -070095egl_display_ptr validate_display(EGLDisplay dpy) {
96 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070097 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -070098 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -070099 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -0700100 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -0700101
102 return dp;
103}
104
Jesse Hallb29e5e82012-04-04 16:53:42 -0700105egl_display_ptr validate_display_connection(EGLDisplay dpy,
106 egl_connection_t*& cnx) {
107 cnx = NULL;
108 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700109 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700110 return dp;
111 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700113 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700115 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Mathias Agopian518ec112011-05-13 16:21:08 -0700118// ----------------------------------------------------------------------------
119
Mathias Agopian48d438d2012-01-28 21:44:00 -0800120const GLubyte * egl_get_string_for_current_context(GLenum name) {
121 // NOTE: returning NULL here will fall-back to the default
122 // implementation.
123
124 EGLContext context = egl_tls_t::getContext();
125 if (context == EGL_NO_CONTEXT)
126 return NULL;
127
128 egl_context_t const * const c = get_context(context);
129 if (c == NULL) // this should never happen, by construction
130 return NULL;
131
132 if (name != GL_EXTENSIONS)
133 return NULL;
134
135 return (const GLubyte *)c->gl_extensions.string();
136}
137
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700138const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
139 // NOTE: returning NULL here will fall-back to the default
140 // implementation.
141
142 EGLContext context = egl_tls_t::getContext();
143 if (context == EGL_NO_CONTEXT)
144 return NULL;
145
146 egl_context_t const * const c = get_context(context);
147 if (c == NULL) // this should never happen, by construction
148 return NULL;
149
150 if (name != GL_EXTENSIONS)
151 return NULL;
152
153 // if index is out of bounds, assume it will be in the default
154 // implementation too, so we don't have to generate a GL error here
155 if (index >= c->tokenized_gl_extensions.size())
156 return NULL;
157
158 return (const GLubyte *)c->tokenized_gl_extensions.itemAt(index).string();
159}
160
161GLint egl_get_num_extensions_for_current_context() {
162 // NOTE: returning -1 here will fall-back to the default
163 // implementation.
164
165 EGLContext context = egl_tls_t::getContext();
166 if (context == EGL_NO_CONTEXT)
167 return -1;
168
169 egl_context_t const * const c = get_context(context);
170 if (c == NULL) // this should never happen, by construction
171 return -1;
172
173 return (GLint)c->tokenized_gl_extensions.size();
174}
175
Mathias Agopian48d438d2012-01-28 21:44:00 -0800176// ----------------------------------------------------------------------------
177
Mathias Agopian923c6612009-08-17 18:07:06 -0700178// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700179// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700180// egl_init_drivers_locked()
181//
Mathias Agopian518ec112011-05-13 16:21:08 -0700182static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700184 // initialized by static ctor. should be set here.
185 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 }
187
Mathias Agopiande586972009-05-28 17:39:03 -0700188 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700189 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700190
Mathias Agopianada798b2012-02-13 17:09:30 -0800191 // dynamically load our EGL implementation
192 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800194 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
195 &gHooks[egl_connection_t::GLESv1_INDEX];
196 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
197 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800198 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 }
200
Mathias Agopianada798b2012-02-13 17:09:30 -0800201 return cnx->dso ? EGL_TRUE : EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202}
203
Mathias Agopian518ec112011-05-13 16:21:08 -0700204static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
205
206EGLBoolean egl_init_drivers() {
Mathias Agopian923c6612009-08-17 18:07:06 -0700207 EGLBoolean res;
Mathias Agopian518ec112011-05-13 16:21:08 -0700208 pthread_mutex_lock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700209 res = egl_init_drivers_locked();
Mathias Agopian518ec112011-05-13 16:21:08 -0700210 pthread_mutex_unlock(&sInitDriverMutex);
Mathias Agopian923c6612009-08-17 18:07:06 -0700211 return res;
212}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213
Michael Lentine12c4bda2014-09-11 12:24:13 -0700214static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
215static nsecs_t sLogPrintTime = 0;
216#define NSECS_DURATION 1000000000
217
Mathias Agopian1cadb252011-05-23 17:26:14 -0700218void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700219 bool printLog = false;
220 nsecs_t now = systemTime();
221 pthread_mutex_lock(&sLogPrintMutex);
222 if ((now - sLogPrintTime) > NSECS_DURATION) {
223 sLogPrintTime = now;
224 printLog = true;
225 }
226 pthread_mutex_unlock(&sLogPrintMutex);
227 if (printLog) {
228 ALOGE("called unimplemented OpenGL ES API");
229 char value[PROPERTY_VALUE_MAX];
230 property_get("debug.egl.callstack", value, "0");
231 if (atoi(value)) {
232 CallStack stack(LOG_TAG);
233 }
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700234 }
Mathias Agopian1cadb252011-05-23 17:26:14 -0700235}
236
Mathias Agopian48d438d2012-01-28 21:44:00 -0800237void gl_noop() {
238}
239
Mathias Agopian1cadb252011-05-23 17:26:14 -0700240// ----------------------------------------------------------------------------
241
Mathias Agopian1cadb252011-05-23 17:26:14 -0700242void setGlThreadSpecific(gl_hooks_t const *value) {
243 gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
244 tls_hooks[TLS_SLOT_OPENGL_API] = value;
245}
246
Mathias Agopian1cadb252011-05-23 17:26:14 -0700247// ----------------------------------------------------------------------------
248// GL / EGL hooks
249// ----------------------------------------------------------------------------
250
251#undef GL_ENTRY
252#undef EGL_ENTRY
253#define GL_ENTRY(_r, _api, ...) #_api,
254#define EGL_ENTRY(_r, _api, ...) #_api,
255
256char const * const gl_names[] = {
Mathias Agopian39c24a22013-04-04 23:17:56 -0700257 #include "../entries.in"
Mathias Agopian1cadb252011-05-23 17:26:14 -0700258 NULL
259};
260
261char const * const egl_names[] = {
262 #include "egl_entries.in"
263 NULL
264};
265
266#undef GL_ENTRY
267#undef EGL_ENTRY
268
269
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700270// ----------------------------------------------------------------------------
271}; // namespace android
272// ----------------------------------------------------------------------------
273