]> git.cworth.org Git - apitrace/blob - retrace/glretrace_ws.cpp
Use skiplist-based FastCallSet within trace::CallSet
[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         if (!visual) {
54             std::cerr << "error: failed to create OpenGL visual\n";
55             exit(1);
56         }
57     }
58     return visual;
59 }
60
61
62 inline glws::Profile
63 getDefaultProfile(void)
64 {
65     if (retrace::coreProfile) {
66         return glws::PROFILE_CORE;
67     } else {
68         return glws::PROFILE_COMPAT;
69     }
70 }
71
72
73 static glws::Drawable *
74 createDrawableHelper(glws::Profile profile, int width = 32, int height = 32, bool pbuffer = false) {
75     glws::Visual *visual = getVisual(profile);
76     glws::Drawable *draw = glws::createDrawable(visual, width, height, pbuffer);
77     if (!draw) {
78         std::cerr << "error: failed to create OpenGL drawable\n";
79         exit(1);
80     }
81
82     return draw;
83 }
84
85
86 glws::Drawable *
87 createDrawable(glws::Profile profile) {
88     return createDrawableHelper(profile);
89 }
90
91
92 glws::Drawable *
93 createDrawable(void) {
94     return createDrawable(getDefaultProfile());
95 }
96
97
98 glws::Drawable *
99 createPbuffer(int width, int height) {
100     return createDrawableHelper(getDefaultProfile(), width, height, true);
101 }
102
103
104 Context *
105 createContext(Context *shareContext, glws::Profile profile) {
106     glws::Visual *visual = getVisual(profile);
107     glws::Context *shareWsContext = shareContext ? shareContext->wsContext : NULL;
108     glws::Context *ctx = glws::createContext(visual, shareWsContext, profile, retrace::debug);
109     if (!ctx) {
110         std::cerr << "error: failed to create OpenGL context\n";
111         exit(1);
112         return NULL;
113     }
114
115     return new Context(ctx);
116 }
117
118
119 Context *
120 createContext(Context *shareContext) {
121     return createContext(shareContext, getDefaultProfile());
122 }
123
124
125 Context::~Context()
126 {
127     //assert(this != getCurrentContext());
128     if (this != getCurrentContext()) {
129         delete wsContext;
130     }
131 }
132
133
134 static thread_specific Context *
135 currentContextPtr;
136
137
138 bool
139 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
140 {
141     Context *currentContext = currentContextPtr;
142     glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
143
144     if (drawable == currentDrawable && context == currentContext) {
145         return true;
146     }
147
148     if (currentContext) {
149         glFlush();
150         if (!retrace::doubleBuffer) {
151             frame_complete(call);
152         }
153     }
154
155     flushQueries();
156
157     bool success = glws::makeCurrent(drawable, context ? context->wsContext : NULL);
158
159     if (!success) {
160         std::cerr << "error: failed to make current OpenGL context and drawable\n";
161         exit(1);
162         return false;
163     }
164
165     currentContextPtr = context;
166
167     if (drawable && context) {
168         context->drawable = drawable;
169         
170         if (!context->used) {
171             initContext();
172             context->used = true;
173         }
174     }
175
176     return true;
177 }
178
179
180 Context *
181 getCurrentContext(void) {
182     return currentContextPtr;
183 }
184
185
186 /**
187  * Grow the current drawble.
188  *
189  * We need to infer the drawable size from GL calls because the drawable sizes
190  * are specified by OS specific calls which we do not trace.
191  */
192 void
193 updateDrawable(int width, int height) {
194     Context *currentContext = getCurrentContext();
195     if (!currentContext) {
196         return;
197     }
198
199     glws::Drawable *currentDrawable = currentContext->drawable;
200     assert(currentDrawable);
201
202     if (currentDrawable->pbuffer) {
203         return;
204     }
205
206     if (currentDrawable->visible &&
207         width  <= currentDrawable->width &&
208         height <= currentDrawable->height) {
209         return;
210     }
211
212     // Ignore zero area viewports
213     if (width == 0 || height == 0) {
214         return;
215     }
216
217     // Check for bound framebuffer last, as this may have a performance impact.
218     GLint draw_framebuffer = 0;
219     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
220     if (draw_framebuffer != 0) {
221         return;
222     }
223
224     currentDrawable->resize(width, height);
225     currentDrawable->show();
226
227     glScissor(0, 0, width, height);
228 }
229
230
231 int
232 parseAttrib(const trace::Value *attribs, int param, int default_ = 0) {
233     const trace::Array *attribs_ = dynamic_cast<const trace::Array *>(attribs);
234
235     if (attribs_) {
236         for (size_t i = 0; i + 1 < attribs_->values.size(); i += 2) {
237             int param_i = attribs_->values[i]->toSInt();
238             if (param_i == 0) {
239                 break;
240             }
241
242             if (param_i == param) {
243                 int value = attribs_->values[i + 1]->toSInt();
244                 return value;
245             }
246         }
247     }
248
249     return default_;
250 }
251
252
253 } /* namespace glretrace */