]> git.cworth.org Git - apitrace/blob - retrace/glretrace_ws.cpp
Fix creation of shared WGL contexts.
[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 "retrace.hpp"
35 #include "glproc.hpp"
36 #include "glstate.hpp"
37 #include "glretrace.hpp"
38
39
40 namespace glretrace {
41
42
43 Context *currentContext = NULL;
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 bool
123 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
124 {
125     glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
126
127     if (drawable == currentDrawable && context == currentContext) {
128         return true;
129     }
130
131     if (currentContext) {
132         glFlush();
133         if (!retrace::doubleBuffer) {
134             frame_complete(call);
135         }
136     }
137
138     flushQueries();
139
140     bool success = glws::makeCurrent(drawable, context ? context->wsContext : NULL);
141
142     if (!success) {
143         std::cerr << "error: failed to make current OpenGL context and drawable\n";
144         exit(1);
145         return false;
146     }
147
148     if (currentContext) {
149         currentContext->drawable = NULL;
150     }
151
152     if (drawable && context) {
153         currentContext = context;
154         currentContext->drawable = drawable;
155         
156         if (!context->used) {
157             initContext();
158             context->used = true;
159         }
160     } else {
161         currentContext = NULL;
162     }
163
164     return true;
165 }
166
167
168
169
170 /**
171  * Grow the current drawble.
172  *
173  * We need to infer the drawable size from GL calls because the drawable sizes
174  * are specified by OS specific calls which we do not trace.
175  */
176 void
177 updateDrawable(int width, int height) {
178     if (!currentContext) {
179         return;
180     }
181
182     glws::Drawable *currentDrawable = currentContext->drawable;
183     assert(currentDrawable);
184
185     if (currentDrawable->pbuffer) {
186         return;
187     }
188
189     if (currentDrawable->visible &&
190         width  <= currentDrawable->width &&
191         height <= currentDrawable->height) {
192         return;
193     }
194
195     // Ignore zero area viewports
196     if (width == 0 || height == 0) {
197         return;
198     }
199
200     // Check for bound framebuffer last, as this may have a performance impact.
201     GLint draw_framebuffer = 0;
202     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
203     if (draw_framebuffer != 0) {
204         return;
205     }
206
207     currentDrawable->resize(width, height);
208     currentDrawable->show();
209
210     glScissor(0, 0, width, height);
211 }
212
213
214 int
215 parseAttrib(const trace::Value *attribs, int param, int default_ = 0) {
216     const trace::Array *attribs_ = dynamic_cast<const trace::Array *>(attribs);
217
218     if (attribs_) {
219         for (size_t i = 0; i + 1 < attribs_->values.size(); i += 2) {
220             int param_i = attribs_->values[i]->toSInt();
221             if (param_i == 0) {
222                 break;
223             }
224
225             if (param_i == param) {
226                 int value = attribs_->values[i + 1]->toSInt();
227                 return value;
228             }
229         }
230     }
231
232     return default_;
233 }
234
235
236 } /* namespace glretrace */