1 /*------------------------------------------------------------------------- 2 * drawElements Quality Program OpenGL Utilities 3 * --------------------------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 *//*! 20 * \file 21 * \brief OpenGL wrapper implementation. 22 *//*--------------------------------------------------------------------*/ 23 24 #include "glwWrapper.hpp" 25 #include "glwFunctions.hpp" 26 #include "deThreadLocal.h" 27 28 #include <stdexcept> 29 30 DE_BEGIN_EXTERN_C 31 32 #include "glwTypes.inl" 33 #include "glwEnums.inl" 34 35 DE_END_EXTERN_C 36 37 namespace glw 38 { 39 40 #if defined(DE_THREAD_LOCAL) 41 42 // Thread-local current function table. 43 DE_THREAD_LOCAL const glw::Functions *s_functions = DE_NULL; 44 setCurrentThreadFunctions(const glw::Functions * gl)45void setCurrentThreadFunctions(const glw::Functions *gl) 46 { 47 s_functions = gl; 48 } 49 getCurrentThreadFunctions(void)50inline const glw::Functions *getCurrentThreadFunctions(void) 51 { 52 return s_functions; 53 } 54 55 #else // defined(DE_THREAD_LOCAL) 56 57 namespace 58 { 59 60 class FunctionTLSPtr 61 { 62 public: 63 FunctionTLSPtr(void) : m_ptr(deThreadLocal_create()) 64 { 65 if (!m_ptr) 66 throw std::runtime_error("glw: TLS allocation failed"); 67 } 68 69 inline void set(const glw::Functions *gl) 70 { 71 deThreadLocal_set(m_ptr, (void *)gl); 72 } 73 74 inline const glw::Functions *get(void) const 75 { 76 return (const glw::Functions *)deThreadLocal_get(m_ptr); 77 } 78 79 private: 80 deThreadLocal m_ptr; 81 }; 82 83 } // namespace 84 85 // Initialized in startup. 86 static FunctionTLSPtr s_functions; 87 88 void setCurrentThreadFunctions(const glw::Functions *gl) 89 { 90 s_functions.set(gl); 91 } 92 93 inline const glw::Functions *getCurrentThreadFunctions(void) 94 { 95 return s_functions.get(); 96 } 97 98 #endif // defined(DE_THREAD_LOCAL) 99 100 } // namespace glw 101 102 DE_BEGIN_EXTERN_C 103 104 #include "glwImpl.inl" 105 106 DE_END_EXTERN_C 107