]> git.cworth.org Git - apitrace/blob - retrace/glretrace_ws.cpp
glretrace: Put currentContext on TLS.
[apitrace] / retrace / glretrace_ws.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011-2012 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 /**
28  * Window system helpers for glretrace.
29  */
30
31
32 #include <string.h>
33
34 #include "os_thread.hpp"
35 #include "retrace.hpp"
36 #include "glproc.hpp"
37 #include "glstate.hpp"
38 #include "glretrace.hpp"
39
40
41 namespace glretrace {
42
43
44
45
46 static glws::Visual *
47 visuals[glws::PROFILE_MAX];
48
49
50 inline glws::Visual *
51 getVisual(glws::Profile profile) {
52     glws::Visual * & visual = visuals[profile];
53     if (!visual) {
54         visual = glws::createVisual(retrace::doubleBuffer, profile);
55     }
56     return visual;
57 }
58
59
60 inline glws::Profile
61 getDefaultProfile(void)
62 {
63     if (retrace::coreProfile) {
64         return glws::PROFILE_CORE;
65     } else {
66         return glws::PROFILE_COMPAT;
67     }
68 }
69
70
71 glws::Drawable *
72 createDrawable(glws::Profile profile) {
73     glws::Drawable *draw = glws::createDrawable(getVisual(profile));
74     if (!draw) {
75         std::cerr << "error: failed to create OpenGL drawable\n";
76         exit(1);
77         return NULL;
78     }
79
80     return draw;
81 }
82
83
84 glws::Drawable *
85 createDrawable(void) {
86     return glretrace::createDrawable(getDefaultProfile());
87 }
88
89
90 Context *
91 createContext(Context *shareContext, glws::Profile profile) {
92     glws::Context *shareWsContext = shareContext ? shareContext->wsContext : NULL;
93     glws::Context *ctx = glws::createContext(getVisual(profile), shareWsContext, profile, retrace::debug);
94     if (!ctx) {
95         std::cerr << "error: failed to create OpenGL context\n";
96         exit(1);
97         return NULL;
98     }
99
100     return new Context(ctx);
101 }
102
103
104 Context *
105 createContext(Context *shareContext) {
106     return createContext(shareContext, getDefaultProfile());
107 }
108
109
110 typedef Context * CurrentData;
111 static os::thread_specific_ptr<CurrentData> currentData;
112
113
114 bool
115 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
116 {
117     Context *currentContext = getCurrentContext();
118     glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
119
120     if (drawable == currentDrawable && context == currentContext) {
121         return true;
122     }
123
124     if (currentContext) {
125         glFlush();
126         if (!retrace::doubleBuffer) {
127             frame_complete(call);
128         }
129     }
130
131     flushQueries();
132
133     bool success = glws::makeCurrent(drawable, context ? context->wsContext : NULL);
134
135     if (!success) {
136         std::cerr << "error: failed to make current OpenGL context and drawable\n";
137         exit(1);
138         return false;
139     }
140
141     if (currentContext) {
142         currentContext->drawable = NULL;
143     }
144
145     CurrentData *currentDataPtr = currentData.get();
146     if (!currentDataPtr) {
147         currentDataPtr = new CurrentData;
148         currentData.reset(currentDataPtr);
149     }
150
151     if (drawable && context) {
152         context->drawable = drawable;
153         *currentData = context;
154         
155         if (!context->used) {
156             initContext();
157             context->used = true;
158         }
159     } else {
160         *currentData = NULL;
161     }
162
163     return true;
164 }
165
166
167 Context *
168 getCurrentContext(void) {
169     CurrentData *currentDataPtr = currentData.get();
170     if (!currentDataPtr) {
171         return NULL;
172     }
173     return *currentDataPtr;
174 }
175
176
177
178
179 /**
180  * Grow the current drawble.
181  *
182  * We need to infer the drawable size from GL calls because the drawable sizes
183  * are specified by OS specific calls which we do not trace.
184  */
185 void
186 updateDrawable(int width, int height) {
187     Context *currentContext = getCurrentContext();
188     if (!currentContext) {
189         return;
190     }
191
192     glws::Drawable *currentDrawable = currentContext->drawable;
193     assert(currentDrawable);
194
195     if (currentDrawable->visible &&
196         width  <= currentDrawable->width &&
197         height <= currentDrawable->height) {
198         return;
199     }
200
201     // Ignore zero area viewports
202     if (width == 0 || height == 0) {
203         return;
204     }
205
206     // Check for bound framebuffer last, as this may have a performance impact.
207     GLint draw_framebuffer = 0;
208     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
209     if (draw_framebuffer != 0) {
210         return;
211     }
212
213     currentDrawable->resize(width, height);
214     currentDrawable->show();
215
216     glScissor(0, 0, width, height);
217 }
218
219
220 int
221 parseAttrib(const trace::Value *attribs, int param, int default_ = 0) {
222     const trace::Array *attribs_ = dynamic_cast<const trace::Array *>(attribs);
223
224     if (attribs_) {
225         for (size_t i = 0; i + 1 < attribs_->values.size(); i += 2) {
226             int param_i = attribs_->values[i]->toSInt();
227             if (param_i == 0) {
228                 break;
229             }
230
231             if (param_i == param) {
232                 int value = attribs_->values[i + 1]->toSInt();
233                 return value;
234             }
235         }
236     }
237
238     return default_;
239 }
240
241
242 } /* namespace glretrace */