blob: 8508c5fe9d29b37f9af9d72e1f8be1e27fe8f9ef [file] [log] [blame]
Mathias Agopian518ec112011-05-13 16:21:08 -07001/*
2 ** Copyright 2011, 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
Mathias Agopian311b4792017-02-28 15:00:49 -080017#include "egl_tls.h"
18
Mark Salyzyna5e161b2016-09-29 08:08:05 -070019#include <stdlib.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070020
Mathias Agopianecfe0912011-09-06 17:24:05 -070021#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <log/log.h>
Mathias Agopian5f1af042017-03-09 18:50:05 -080023#include "CallStack.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070024
Mathias Agopian518ec112011-05-13 16:21:08 -070025namespace android {
26
Mathias Agopian4e620dd2013-05-30 16:07:36 -070027pthread_key_t egl_tls_t::sKey = TLS_KEY_NOT_INITIALIZED;
28pthread_once_t egl_tls_t::sOnceKey = PTHREAD_ONCE_INIT;
Mathias Agopian518ec112011-05-13 16:21:08 -070029
30egl_tls_t::egl_tls_t()
Mathias Agopian311b4792017-02-28 15:00:49 -080031 : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(true) {
Mathias Agopian518ec112011-05-13 16:21:08 -070032}
33
34const char *egl_tls_t::egl_strerror(EGLint err) {
35 switch (err) {
36 case EGL_SUCCESS: return "EGL_SUCCESS";
37 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
38 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
39 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
40 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
41 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
42 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
43 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
44 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
45 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
46 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
47 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
48 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
49 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
50 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
51 default: return "UNKNOWN";
52 }
53}
54
55void egl_tls_t::validateTLSKey()
56{
Mathias Agopian4e620dd2013-05-30 16:07:36 -070057 struct TlsKeyInitializer {
58 static void create() {
59 pthread_key_create(&sKey, (void (*)(void*))&eglReleaseThread);
60 }
61 };
62 pthread_once(&sOnceKey, TlsKeyInitializer::create);
Mathias Agopian518ec112011-05-13 16:21:08 -070063}
64
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070065void egl_tls_t::setErrorEtcImpl(
66 const char* caller, int line, EGLint error, bool quiet) {
Mathias Agopian518ec112011-05-13 16:21:08 -070067 validateTLSKey();
68 egl_tls_t* tls = getTLS();
69 if (tls->error != error) {
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070070 if (!quiet) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000071 ALOGE("%s:%d error %x (%s)",
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070072 caller, line, error, egl_strerror(error));
73 char value[PROPERTY_VALUE_MAX];
74 property_get("debug.egl.callstack", value, "0");
75 if (atoi(value)) {
Mathias Agopian5f1af042017-03-09 18:50:05 -080076 CallStack::log(LOG_TAG);
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070077 }
Mathias Agopianecfe0912011-09-06 17:24:05 -070078 }
Mathias Agopian0e8bbee2011-10-05 19:15:05 -070079 tls->error = error;
Mathias Agopian518ec112011-05-13 16:21:08 -070080 }
81}
82
83bool egl_tls_t::logNoContextCall() {
84 egl_tls_t* tls = getTLS();
Mathias Agopian311b4792017-02-28 15:00:49 -080085 if (tls->logCallWithNoContext) {
Mathias Agopian518ec112011-05-13 16:21:08 -070086 tls->logCallWithNoContext = false;
87 return true;
88 }
89 return false;
Mathias Agopian311b4792017-02-28 15:00:49 -080090
Mathias Agopian518ec112011-05-13 16:21:08 -070091}
92
93egl_tls_t* egl_tls_t::getTLS() {
94 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
95 if (tls == 0) {
96 tls = new egl_tls_t;
97 pthread_setspecific(sKey, tls);
98 }
99 return tls;
100}
101
102void egl_tls_t::clearTLS() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700103 if (sKey != TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700104 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
105 if (tls) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700106 pthread_setspecific(sKey, 0);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700107 delete tls;
Mathias Agopian518ec112011-05-13 16:21:08 -0700108 }
109 }
110}
111
112void egl_tls_t::clearError() {
113 // This must clear the error from all the underlying EGL implementations as
114 // well as the EGL wrapper layer.
115 eglGetError();
116}
117
118EGLint egl_tls_t::getError() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700119 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700120 return EGL_SUCCESS;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700121 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700122 egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700123 if (!tls) {
124 return EGL_SUCCESS;
125 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700126 EGLint error = tls->error;
127 tls->error = EGL_SUCCESS;
128 return error;
129}
130
131void egl_tls_t::setContext(EGLContext ctx) {
132 validateTLSKey();
133 getTLS()->ctx = ctx;
134}
135
136EGLContext egl_tls_t::getContext() {
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700137 if (sKey == TLS_KEY_NOT_INITIALIZED) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700138 return EGL_NO_CONTEXT;
Mathias Agopian4e620dd2013-05-30 16:07:36 -0700139 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700140 egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
141 if (!tls) return EGL_NO_CONTEXT;
142 return tls->ctx;
143}
144
145
146} // namespace android