]> git.cworth.org Git - apitrace/blob - wrappers/gltrace_state.cpp
Handle spurious calls from wglCreateContextAttribsARB to wglCreateContext (issue...
[apitrace] / wrappers / gltrace_state.cpp
1 /*********************************************************************
2  *
3  * Copyright 2012 Intel Corporation
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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
24  * SOFTWARE.
25  *
26  *********************************************************************/
27
28 #include <assert.h>
29
30 #include <map>
31 #if defined(_MSC_VER)
32 #include <memory>
33 #else
34 #include <tr1/memory>
35 #endif
36
37 #include <gltrace.hpp>
38 #include <os_thread.hpp>
39
40 namespace gltrace {
41
42 typedef std::tr1::shared_ptr<Context> context_ptr_t;
43 static std::map<uintptr_t, context_ptr_t> context_map;
44 static os::recursive_mutex context_map_mutex;
45
46 class ThreadState {
47 public:
48     context_ptr_t current_context;
49     context_ptr_t dummy_context;     /*
50                                       * For cases when there is no current
51                                       * context, but the app still calls some
52                                       * GL function that expects one.
53                                       */
54     ThreadState() : dummy_context(new Context)
55     {
56         current_context = dummy_context;
57     }
58 };
59
60 static os::thread_specific_ptr<struct ThreadState> thread_state;
61
62 static ThreadState *get_ts(void)
63 {
64     ThreadState *ts = thread_state.get();
65
66     if (!ts) {
67         ts = new ThreadState;
68         thread_state.reset(ts);
69     }
70
71     return ts;
72 }
73
74 static void _retainContext(context_ptr_t ctx)
75 {
76     ctx->retain_count++;
77 }
78
79 void retainContext(uintptr_t context_id)
80 {
81     context_map_mutex.lock();
82     if (context_map.find(context_id) != context_map.end())
83         _retainContext(context_map[context_id]);
84     context_map_mutex.unlock();
85 }
86
87 static bool _releaseContext(context_ptr_t ctx)
88 {
89     return !(--ctx->retain_count);
90 }
91
92 /*
93  * return true if the context was destroyed, false if only its refcount
94  * got decreased. Note that even if the context was destroyed it may
95  * still live, if it's the currently selected context (by setContext).
96  */
97 bool releaseContext(uintptr_t context_id)
98 {
99     bool res;
100
101     context_map_mutex.lock();
102     /*
103      * This can potentially called (from glX) with an invalid context_id,
104      * so don't assert on it being valid.
105      */
106     if (context_map.find(context_id) != context_map.end()) {
107         res = _releaseContext(context_map[context_id]);
108         if (res)
109             context_map.erase(context_id);
110     }
111     context_map_mutex.unlock();
112
113     return res;
114 }
115
116 void createContext(uintptr_t context_id)
117 {
118     // wglCreateContextAttribsARB causes internal calls to wglCreateContext to be
119     // traced, causing context to be defined twice.
120     if (context_map.find(context_id) != context_map.end()) {
121         return;
122     }
123
124     context_ptr_t ctx(new Context);
125
126     context_map_mutex.lock();
127
128     _retainContext(ctx);
129     context_map[context_id] = ctx;
130
131     context_map_mutex.unlock();
132 }
133
134 void setContext(uintptr_t context_id)
135 {
136     ThreadState *ts = get_ts();
137     context_ptr_t ctx;
138
139     context_map_mutex.lock();
140
141     assert(context_map.find(context_id) != context_map.end());
142     ctx = context_map[context_id];
143
144     context_map_mutex.unlock();
145
146     ts->current_context = ctx;
147 }
148
149 void clearContext(void)
150 {
151     ThreadState *ts = get_ts();
152
153     ts->current_context = ts->dummy_context;
154 }
155
156 Context *getContext(void)
157 {
158     return get_ts()->current_context.get();
159 }
160
161 }