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