]> git.cworth.org Git - apitrace/blob - retrace/glretrace_ws.cpp
Handle visual creation errors.
[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         if (!visual) {
56             std::cerr << "error: failed to create OpenGL visual\n";
57             exit(1);
58         }
59     }
60     return visual;
61 }
62
63
64 inline glws::Profile
65 getDefaultProfile(void)
66 {
67     if (retrace::coreProfile) {
68         return glws::PROFILE_CORE;
69     } else {
70         return glws::PROFILE_COMPAT;
71     }
72 }
73
74
75 static glws::Drawable *
76 createDrawableHelper(glws::Profile profile, int width = 32, int height = 32, bool pbuffer = false) {
77     glws::Visual *visual = getVisual(profile);
78     glws::Drawable *draw = glws::createDrawable(visual, width, height, pbuffer);
79     if (!draw) {
80         std::cerr << "error: failed to create OpenGL drawable\n";
81         exit(1);
82     }
83
84     return draw;
85 }
86
87
88 glws::Drawable *
89 createDrawable(glws::Profile profile) {
90     return createDrawableHelper(profile);
91 }
92
93
94 glws::Drawable *
95 createDrawable(void) {
96     return createDrawable(getDefaultProfile());
97 }
98
99
100 glws::Drawable *
101 createPbuffer(int width, int height) {
102     return createDrawableHelper(getDefaultProfile(), width, height, true);
103 }
104
105
106 Context *
107 createContext(Context *shareContext, glws::Profile profile) {
108     glws::Visual *visual = getVisual(profile);
109     glws::Context *shareWsContext = shareContext ? shareContext->wsContext : NULL;
110     glws::Context *ctx = glws::createContext(visual, shareWsContext, profile, retrace::debug);
111     if (!ctx) {
112         std::cerr << "error: failed to create OpenGL context\n";
113         exit(1);
114         return NULL;
115     }
116
117     return new Context(ctx);
118 }
119
120
121 Context *
122 createContext(Context *shareContext) {
123     return createContext(shareContext, getDefaultProfile());
124 }
125
126
127 bool
128 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
129 {
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     if (drawable && context) {
158         currentContext = context;
159         currentContext->drawable = drawable;
160         
161         if (!context->used) {
162             initContext();
163             context->used = true;
164         }
165     } else {
166         currentContext = NULL;
167     }
168
169     return true;
170 }
171
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     if (!currentContext) {
184         return;
185     }
186
187     glws::Drawable *currentDrawable = currentContext->drawable;
188     assert(currentDrawable);
189
190     if (currentDrawable->pbuffer) {
191         return;
192     }
193
194     if (currentDrawable->visible &&
195         width  <= currentDrawable->width &&
196         height <= currentDrawable->height) {
197         return;
198     }
199
200     // Ignore zero area viewports
201     if (width == 0 || height == 0) {
202         return;
203     }
204
205     // Check for bound framebuffer last, as this may have a performance impact.
206     GLint draw_framebuffer = 0;
207     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
208     if (draw_framebuffer != 0) {
209         return;
210     }
211
212     currentDrawable->resize(width, height);
213     currentDrawable->show();
214
215     glScissor(0, 0, width, height);
216 }
217
218
219 int
220 parseAttrib(const trace::Value *attribs, int param, int default_ = 0) {
221     const trace::Array *attribs_ = dynamic_cast<const trace::Array *>(attribs);
222
223     if (attribs_) {
224         for (size_t i = 0; i + 1 < attribs_->values.size(); i += 2) {
225             int param_i = attribs_->values[i]->toSInt();
226             if (param_i == 0) {
227                 break;
228             }
229
230             if (param_i == param) {
231                 int value = attribs_->values[i + 1]->toSInt();
232                 return value;
233             }
234         }
235     }
236
237     return default_;
238 }
239
240
241 } /* namespace glretrace */