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 
45 void setCurrentThreadFunctions (const glw::Functions* gl)
46 {
47 	s_functions = gl;
48 }
49 
50 inline 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)
64 		: m_ptr(deThreadLocal_create())
65 	{
66 		if (!m_ptr)
67 			throw std::runtime_error("glw: TLS allocation failed");
68 	}
69 
70 	inline void set (const glw::Functions* gl)
71 	{
72 		deThreadLocal_set(m_ptr, (void*)gl);
73 	}
74 
75 	inline const glw::Functions* get (void) const
76 	{
77 		return (const glw::Functions*)deThreadLocal_get(m_ptr);
78 	}
79 
80 private:
81 	deThreadLocal m_ptr;
82 };
83 
84 } // anonymous
85 
86 // Initialized in startup.
87 static FunctionTLSPtr s_functions;
88 
89 void setCurrentThreadFunctions (const glw::Functions* gl)
90 {
91 	s_functions.set(gl);
92 }
93 
94 inline const glw::Functions* getCurrentThreadFunctions (void)
95 {
96 	return s_functions.get();
97 }
98 
99 #endif // defined(DE_THREAD_LOCAL)
100 
101 } // glw
102 
103 DE_BEGIN_EXTERN_C
104 
105 #include "glwImpl.inl"
106 
107 DE_END_EXTERN_C
108