blob: 18cf26174ef4ff5ab3536ed7a210ac9c3fc0b84d [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
27#include <cutils/log.h>
28#include <cutils/atomic.h>
29#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Romain Guye03de932011-07-11 15:33:51 -070031#include <utils/CallStack.h>
Mathias Agopian24035332010-08-02 17:34:32 -070032#include <utils/String8.h>
Mathias Agopian9429e9c2009-08-21 02:18:25 -070033
Mathias Agopian39c24a22013-04-04 23:17:56 -070034#include "../egl_impl.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070035
David Li864f8392011-03-28 10:39:28 -070036#include "egl_tls.h"
Mathias Agopian39c24a22013-04-04 23:17:56 -070037#include "egldefs.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070038#include "Loader.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
Mathias Agopian518ec112011-05-13 16:21:08 -070040#include "egl_display.h"
41#include "egl_object.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
Dan Stozac3289c42014-01-17 11:38:34 -080043typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer;
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045// ----------------------------------------------------------------------------
46namespace android {
47// ----------------------------------------------------------------------------
48
Mathias Agopianada798b2012-02-13 17:09:30 -080049egl_connection_t gEGLImpl;
50gl_hooks_t gHooks[2];
Mathias Agopian518ec112011-05-13 16:21:08 -070051gl_hooks_t gHooksNoContext;
52pthread_key_t gGLWrapperKey = -1;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070053
54// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian518ec112011-05-13 16:21:08 -070056void setGLHooksThreadSpecific(gl_hooks_t const *value) {
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070057 setGlThreadSpecific(value);
58}
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060/*****************************************************************************/
61
Mathias Agopian6f087122010-09-23 16:38:38 -070062static int gl_no_context() {
Mathias Agopian518ec112011-05-13 16:21:08 -070063 if (egl_tls_t::logNoContextCall()) {
Mathias Agopian455e3602012-09-26 17:19:48 -070064 char const* const error = "call to OpenGL ES API with "
65 "no current context (logged once per thread)";
66 if (LOG_NDEBUG) {
67 ALOGE(error);
68 } else {
69 LOG_ALWAYS_FATAL(error);
70 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070071 char value[PROPERTY_VALUE_MAX];
72 property_get("debug.egl.callstack", value, "0");
John Reck1f246d72014-04-24 23:34:32 +000073 if (atoi(value)) {
Mathias Agopiancab25d62013-03-21 17:12:40 -070074 CallStack stack(LOG_TAG);
Mathias Agopianecfe0912011-09-06 17:24:05 -070075 }
Mathias Agopiand274eae2009-07-31 16:21:17 -070076 }
Mathias Agopian6f087122010-09-23 16:38:38 -070077 return 0;
Mathias Agopian05c53112010-09-23 11:32:52 -070078}
79
Jesse Hall47743382013-02-08 11:13:46 -080080static void early_egl_init(void)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081{
Dan Stozac3289c42014-01-17 11:38:34 -080082 int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
83 EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
84 for (int hook = 0; hook < numHooks; ++hook) {
85 *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
86 }
Mathias Agopian05c53112010-09-23 11:32:52 -070087
Jack Palevicha2dd6cf2010-10-26 15:21:24 -070088 setGLHooksThreadSpecific(&gHooksNoContext);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089}
90
91static pthread_once_t once_control = PTHREAD_ONCE_INIT;
92static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
93
Mathias Agopian518ec112011-05-13 16:21:08 -070094// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095
Jesse Hallb29e5e82012-04-04 16:53:42 -070096egl_display_ptr validate_display(EGLDisplay dpy) {
97 egl_display_ptr dp = get_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -070098 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -070099 return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL));
Mathias Agopian5b287a62011-05-16 18:58:55 -0700100 if (!dp->isReady())
Jesse Hallb29e5e82012-04-04 16:53:42 -0700101 return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL));
Eric Hassold3ede7c12011-03-23 15:59:00 -0700102
103 return dp;
104}
105
Jesse Hallb29e5e82012-04-04 16:53:42 -0700106egl_display_ptr validate_display_connection(EGLDisplay dpy,
107 egl_connection_t*& cnx) {
108 cnx = NULL;
109 egl_display_ptr dp = validate_display(dpy);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700110 if (!dp)
Jesse Hallb29e5e82012-04-04 16:53:42 -0700111 return dp;
112 cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 if (cnx->dso == 0) {
Jesse Hallb29e5e82012-04-04 16:53:42 -0700114 return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700116 return dp;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117}
118
Mathias Agopian518ec112011-05-13 16:21:08 -0700119// ----------------------------------------------------------------------------
120
Mathias Agopian48d438d2012-01-28 21:44:00 -0800121const GLubyte * egl_get_string_for_current_context(GLenum name) {
122 // NOTE: returning NULL here will fall-back to the default
123 // implementation.
124
125 EGLContext context = egl_tls_t::getContext();
126 if (context == EGL_NO_CONTEXT)
127 return NULL;
128
129 egl_context_t const * const c = get_context(context);
130 if (c == NULL) // this should never happen, by construction
131 return NULL;
132
133 if (name != GL_EXTENSIONS)
134 return NULL;
135
136 return (const GLubyte *)c->gl_extensions.string();
137}
138
Alistair Strachanedfe72e2015-05-22 14:10:09 -0700139const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
140 // NOTE: returning NULL here will fall-back to the default
141 // implementation.
142
143 EGLContext context = egl_tls_t::getContext();
144 if (context == EGL_NO_CONTEXT)
145 return NULL;
146
147 egl_context_t const * const c = get_context(context);
148 if (c == NULL) // this should never happen, by construction
149 return NULL;
150
151 if (name != GL_EXTENSIONS)
152 return NULL;
153
154 // if index is out of bounds, assume it will be in the default
155 // implementation too, so we don't have to generate a GL error here
156 if (index >= c->tokenized_gl_extensions.size())
157 return NULL;
158
159 return (const GLubyte *)c->tokenized_gl_extensions.itemAt(index).string();
160}
161
162GLint egl_get_num_extensions_for_current_context() {
163 // NOTE: returning -1 here will fall-back to the default
164 // implementation.
165
166 EGLContext context = egl_tls_t::getContext();
167 if (context == EGL_NO_CONTEXT)
168 return -1;
169
170 egl_context_t const * const c = get_context(context);
171 if (c == NULL) // this should never happen, by construction
172 return -1;
173
174 return (GLint)c->tokenized_gl_extensions.size();
175}
176
Mathias Agopian48d438d2012-01-28 21:44:00 -0800177// ----------------------------------------------------------------------------
178
Mathias Agopian923c6612009-08-17 18:07:06 -0700179// this mutex protects:
Mathias Agopiana69e0ed2009-08-24 21:47:13 -0700180// d->disp[]
Mathias Agopian923c6612009-08-17 18:07:06 -0700181// egl_init_drivers_locked()
182//
Mathias Agopian518ec112011-05-13 16:21:08 -0700183static EGLBoolean egl_init_drivers_locked() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 if (sEarlyInitState) {
Mathias Agopian923c6612009-08-17 18:07:06 -0700185 // initialized by static ctor. should be set here.
186 return EGL_FALSE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 }
188
Mathias Agopiande586972009-05-28 17:39:03 -0700189 // get our driver loader
Mathias Agopian923c6612009-08-17 18:07:06 -0700190 Loader& loader(Loader::getInstance());
Mathias Agopian518ec112011-05-13 16:21:08 -0700191
Mathias Agopianada798b2012-02-13 17:09:30 -0800192 // dynamically load our EGL implementation
193 egl_connection_t* cnx = &gEGLImpl;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 if (cnx->dso == 0) {
Mathias Agopian7773c432012-02-13 20:06:08 -0800195 cnx->hooks[egl_connection_t::GLESv1_INDEX] =
196 &gHooks[egl_connection_t::GLESv1_INDEX];
197 cnx->hooks[egl_connection_t::GLESv2_INDEX] =
198 &gHooks[egl_connection_t::GLESv2_INDEX];
Mathias Agopianada798b2012-02-13 17:09:30 -0800199 cnx->dso = loader.open(cnx);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 }
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;
216static nsecs_t sLogPrintTime = 0;
217#define NSECS_DURATION 1000000000
218
Mathias Agopian1cadb252011-05-23 17:26:14 -0700219void gl_unimplemented() {
Michael Lentine12c4bda2014-09-11 12:24:13 -0700220 bool printLog = false;
221 nsecs_t now = systemTime();
222 pthread_mutex_lock(&sLogPrintMutex);
223 if ((now - sLogPrintTime) > NSECS_DURATION) {
224 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)) {
233 CallStack stack(LOG_TAG);
234 }
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"
Mathias Agopian1cadb252011-05-23 17:26:14 -0700259 NULL
260};
261
262char const * const egl_names[] = {
263 #include "egl_entries.in"
264 NULL
265};
266
267#undef GL_ENTRY
268#undef EGL_ENTRY
269
270
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700271// ----------------------------------------------------------------------------
272}; // namespace android
273// ----------------------------------------------------------------------------
274