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