1 /*********************************************************************
3 * Copyright 2012 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 *********************************************************************/
33 #include <gltrace.hpp>
34 #include <os_thread.hpp>
38 typedef std::tr1::shared_ptr<Context> context_ptr_t;
39 static std::map<uintptr_t, context_ptr_t> context_map;
40 static os::recursive_mutex context_map_mutex;
44 context_ptr_t current_context;
45 context_ptr_t dummy_context; /*
46 * For cases when there is no current
47 * context, but the app still calls some
48 * GL function that expects one.
50 ThreadState() : dummy_context(new Context)
52 current_context = dummy_context;
56 static os::thread_specific_ptr<struct ThreadState> thread_state;
58 static ThreadState *get_ts(void)
60 ThreadState *ts = thread_state.get();
64 thread_state.reset(ts);
70 static void _retainContext(context_ptr_t ctx)
75 void retainContext(uintptr_t context_id)
77 context_map_mutex.lock();
78 if (context_map.find(context_id) != context_map.end())
79 _retainContext(context_map[context_id]);
80 context_map_mutex.unlock();
83 static bool _releaseContext(context_ptr_t ctx)
85 return !(--ctx->retain_count);
89 * return true if the context was destroyed, false if only its refcount
90 * got decreased. Note that even if the context was destroyed it may
91 * still live, if it's the currently selected context (by setContext).
93 bool releaseContext(uintptr_t context_id)
97 context_map_mutex.lock();
99 * This can potentially called (from glX) with an invalid context_id,
100 * so don't assert on it being valid.
102 if (context_map.find(context_id) != context_map.end()) {
103 res = _releaseContext(context_map[context_id]);
105 context_map.erase(context_id);
107 context_map_mutex.unlock();
112 void createContext(uintptr_t context_id)
114 context_ptr_t ctx(new Context);
116 context_map_mutex.lock();
119 assert(context_map.find(context_id) == context_map.end());
120 context_map[context_id] = ctx;
122 context_map_mutex.unlock();
126 * return true if the context has been destroyed, false otherwise. See
127 * the note at releaseContext about the actual ccontext lifetime.
129 bool destroyContext(uintptr_t context_id)
131 return releaseContext(context_id);
134 void setContext(uintptr_t context_id)
136 ThreadState *ts = get_ts();
139 context_map_mutex.lock();
141 assert(context_map.find(context_id) != context_map.end());
142 ctx = context_map[context_id];
144 context_map_mutex.unlock();
146 ts->current_context = ctx;
149 void clearContext(void)
151 ThreadState *ts = get_ts();
153 ts->current_context = ts->dummy_context;
156 Context *getContext(void)
158 return get_ts()->current_context.get();