]> git.cworth.org Git - apitrace/blob - retrace/glretrace_ws.cpp
Simplify 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 static glws::Visual *
45 visuals[glws::PROFILE_MAX];
46
47
48 inline glws::Visual *
49 getVisual(glws::Profile profile) {
50     glws::Visual * & visual = visuals[profile];
51     if (!visual) {
52         visual = glws::createVisual(retrace::doubleBuffer, profile);
53     }
54     return visual;
55 }
56
57
58 inline glws::Profile
59 getDefaultProfile(void)
60 {
61     if (retrace::coreProfile) {
62         return glws::PROFILE_CORE;
63     } else {
64         return glws::PROFILE_COMPAT;
65     }
66 }
67
68
69 static glws::Drawable *
70 createDrawableHelper(glws::Profile profile, int width = 32, int height = 32, bool pbuffer = false) {
71     glws::Drawable *draw = glws::createDrawable(getVisual(profile), width, height, pbuffer);
72     if (!draw) {
73         std::cerr << "error: failed to create OpenGL drawable\n";
74         exit(1);
75         return NULL;
76     }
77
78     return draw;
79 }
80
81
82 glws::Drawable *
83 createDrawable(glws::Profile profile) {
84     return createDrawableHelper(profile);
85 }
86
87
88 glws::Drawable *
89 createDrawable(void) {
90     return createDrawable(getDefaultProfile());
91 }
92
93
94 glws::Drawable *
95 createPbuffer(int width, int height) {
96     return createDrawableHelper(getDefaultProfile(), width, height, true);
97 }
98
99
100 Context *
101 createContext(Context *shareContext, glws::Profile profile) {
102     glws::Context *shareWsContext = shareContext ? shareContext->wsContext : NULL;
103     glws::Context *ctx = glws::createContext(getVisual(profile), shareWsContext, profile, retrace::debug);
104     if (!ctx) {
105         std::cerr << "error: failed to create OpenGL context\n";
106         exit(1);
107         return NULL;
108     }
109
110     return new Context(ctx);
111 }
112
113
114 Context *
115 createContext(Context *shareContext) {
116     return createContext(shareContext, getDefaultProfile());
117 }
118
119
120 static os::thread_specific_ptr<Context>
121 currentContextPtr;
122
123
124 bool
125 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
126 {
127     Context *currentContext = currentContextPtr.release();
128     glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
129
130     if (drawable == currentDrawable && context == currentContext) {
131         return true;
132     }
133
134     if (currentContext) {
135         glFlush();
136         if (!retrace::doubleBuffer) {
137             frame_complete(call);
138         }
139     }
140
141     flushQueries();
142
143     bool success = glws::makeCurrent(drawable, context ? context->wsContext : NULL);
144
145     if (!success) {
146         std::cerr << "error: failed to make current OpenGL context and drawable\n";
147         exit(1);
148         return false;
149     }
150
151     if (currentContext) {
152         currentContext->drawable = NULL;
153     }
154
155     if (drawable && context) {
156         context->drawable = drawable;
157         currentContextPtr.reset(context);
158         
159         if (!context->used) {
160             initContext();
161             context->used = true;
162         }
163     }
164
165     return true;
166 }
167
168
169 Context *
170 getCurrentContext(void) {
171     return currentContextPtr.get();
172 }
173
174
175 /**
176  * Grow the current drawble.
177  *
178  * We need to infer the drawable size from GL calls because the drawable sizes
179  * are specified by OS specific calls which we do not trace.
180  */
181 void
182 updateDrawable(int width, int height) {
183     Context *currentContext = getCurrentContext();
184     if (!currentContext) {
185         return;
186     }
187
188     glws::Drawable *currentDrawable = currentContext->drawable;
189     assert(currentDrawable);
190
191     if (currentDrawable->pbuffer) {
192         return;
193     }
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 */