• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  ** Copyright 2007, The Android Open Source Project
3  **
4  ** 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
7  **
8  **     http://www.apache.org/licenses/LICENSE-2.0
9  **
10  ** 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
14  ** limitations under the License.
15  */
16 
17 #include <EGL/egl.h>
18 #include <android-base/properties.h>
19 #include <log/log.h>
20 #include <stdlib.h>
21 
22 #include "../egl_impl.h"
23 #include "CallStack.h"
24 #include "Loader.h"
25 #include "egl_display.h"
26 #include "egl_layers.h"
27 #include "egl_object.h"
28 #include "egl_tls.h"
29 #include "egldefs.h"
30 
31 namespace android {
32 
33 egl_connection_t gEGLImpl;
34 gl_hooks_t gHooks[2];
35 gl_hooks_t gHooksNoContext;
36 pthread_key_t gGLWrapperKey = -1;
37 
setGLHooksThreadSpecific(gl_hooks_t const * value)38 void setGLHooksThreadSpecific(gl_hooks_t const* value) {
39     setGlThreadSpecific(value);
40 }
41 
gl_no_context()42 static int gl_no_context() {
43     if (egl_tls_t::logNoContextCall()) {
44         const char* const error = "call to OpenGL ES API with "
45                                   "no current context (logged once per thread)";
46         if (LOG_NDEBUG) {
47             ALOGE(error);
48         } else {
49             LOG_ALWAYS_FATAL(error);
50         }
51         if (base::GetBoolProperty("debug.egl.callstack", false)) {
52             CallStack::log(LOG_TAG);
53         }
54     }
55     return 0;
56 }
57 
early_egl_init(void)58 static void early_egl_init(void) {
59     int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
60     EGLFuncPointer* iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
61     for (int hook = 0; hook < numHooks; ++hook) {
62         *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
63     }
64 
65     setGLHooksThreadSpecific(&gHooksNoContext);
66 }
67 
egl_get_string_for_current_context(GLenum name)68 const GLubyte* egl_get_string_for_current_context(GLenum name) {
69     // NOTE: returning NULL here will fall-back to the default
70     // implementation.
71 
72     EGLContext context = egl_tls_t::getContext();
73     if (context == EGL_NO_CONTEXT) return nullptr;
74 
75     const egl_context_t* const c = get_context(context);
76     if (c == nullptr) // this should never happen, by construction
77         return nullptr;
78 
79     if (name != GL_EXTENSIONS) return nullptr;
80 
81     return (const GLubyte*)c->gl_extensions.c_str();
82 }
83 
egl_get_string_for_current_context(GLenum name,GLuint index)84 const GLubyte* egl_get_string_for_current_context(GLenum name, GLuint index) {
85     // NOTE: returning NULL here will fall-back to the default
86     // implementation.
87 
88     EGLContext context = egl_tls_t::getContext();
89     if (context == EGL_NO_CONTEXT) return nullptr;
90 
91     const egl_context_t* const c = get_context(context);
92     if (c == nullptr) // this should never happen, by construction
93         return nullptr;
94 
95     if (name != GL_EXTENSIONS) return nullptr;
96 
97     // if index is out of bounds, assume it will be in the default
98     // implementation too, so we don't have to generate a GL error here
99     if (index >= c->tokenized_gl_extensions.size()) return nullptr;
100 
101     return (const GLubyte*)c->tokenized_gl_extensions[index].c_str();
102 }
103 
egl_get_num_extensions_for_current_context()104 GLint egl_get_num_extensions_for_current_context() {
105     // NOTE: returning -1 here will fall-back to the default
106     // implementation.
107 
108     EGLContext context = egl_tls_t::getContext();
109     if (context == EGL_NO_CONTEXT) return -1;
110 
111     const egl_context_t* const c = get_context(context);
112     if (c == nullptr) // this should never happen, by construction
113         return -1;
114 
115     return (GLint)c->tokenized_gl_extensions.size();
116 }
117 
egl_get_connection()118 egl_connection_t* egl_get_connection() {
119     return &gEGLImpl;
120 }
121 
122 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
123 static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
124 
egl_init_drivers_locked()125 static EGLBoolean egl_init_drivers_locked() {
126     if (sEarlyInitState) {
127         // initialized by static ctor. should be set here.
128         return EGL_FALSE;
129     }
130 
131     // get our driver loader
132     Loader& loader(Loader::getInstance());
133 
134     // dynamically load our EGL implementation
135     egl_connection_t* cnx = &gEGLImpl;
136     cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX];
137     cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX];
138     cnx->dso = loader.open(cnx);
139 
140     // Check to see if any layers are enabled and route functions through them
141     if (cnx->dso) {
142         // Layers can be enabled long after the drivers have been loaded.
143         // They will only be initialized once.
144         LayerLoader& layer_loader(LayerLoader::getInstance());
145         layer_loader.InitLayers(cnx);
146     }
147 
148     return cnx->dso ? EGL_TRUE : EGL_FALSE;
149 }
150 
151 // this mutex protects driver load logic as a critical section since it accesses to global variable
152 // like gEGLImpl
153 static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
154 
egl_init_drivers()155 EGLBoolean egl_init_drivers() {
156     EGLBoolean res;
157     pthread_mutex_lock(&sInitDriverMutex);
158     res = egl_init_drivers_locked();
159     pthread_mutex_unlock(&sInitDriverMutex);
160     return res;
161 }
162 
163 static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
164 static std::chrono::steady_clock::time_point sLogPrintTime;
165 static constexpr std::chrono::seconds DURATION(1);
166 
gl_unimplemented()167 void gl_unimplemented() {
168     bool printLog = false;
169     auto now = std::chrono::steady_clock::now();
170     pthread_mutex_lock(&sLogPrintMutex);
171     if ((now - sLogPrintTime) > DURATION) {
172         sLogPrintTime = now;
173         printLog = true;
174     }
175     pthread_mutex_unlock(&sLogPrintMutex);
176     if (printLog) {
177         ALOGE("called unimplemented OpenGL ES API");
178         if (base::GetBoolProperty("debug.egl.callstack", false)) {
179             CallStack::log(LOG_TAG);
180         }
181     }
182 }
183 
gl_noop()184 void gl_noop() {}
185 
setGlThreadSpecific(gl_hooks_t const * value)186 void setGlThreadSpecific(gl_hooks_t const* value) {
187     gl_hooks_t const* volatile* tls_hooks = get_tls_hooks();
188     tls_hooks[TLS_SLOT_OPENGL_API] = value;
189 }
190 
191 // ----------------------------------------------------------------------------
192 // GL / EGL hooks
193 // ----------------------------------------------------------------------------
194 
195 #undef GL_ENTRY
196 #undef EGL_ENTRY
197 #define GL_ENTRY(_r, _api, ...) #_api,
198 #define EGL_ENTRY(_r, _api, ...) #_api,
199 
200 char const * const gl_names[] = {
201     #include "../entries.in"
202     nullptr
203 };
204 
205 char const * const gl_names_1[] = {
206     #include "../entries_gles1.in"
207     nullptr
208 };
209 
210 char const * const egl_names[] = {
211     #include "egl_entries.in"
212     nullptr
213 };
214 
215 char const * const platform_names[] = {
216     #include "platform_entries.in"
217     nullptr
218 };
219 
220 #undef GL_ENTRY
221 #undef EGL_ENTRY
222 
223 }; // namespace android
224