1 /**************************************************************************
3 * Copyright 2011-2012 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
28 * Window system helpers for glretrace.
34 #include "os_thread.hpp"
35 #include "retrace.hpp"
37 #include "glstate.hpp"
38 #include "glretrace.hpp"
47 visuals[glws::PROFILE_MAX];
51 getVisual(glws::Profile profile) {
52 glws::Visual * & visual = visuals[profile];
54 visual = glws::createVisual(retrace::doubleBuffer, profile);
61 getDefaultProfile(void)
63 if (retrace::coreProfile) {
64 return glws::PROFILE_CORE;
66 return glws::PROFILE_COMPAT;
72 createDrawable(glws::Profile profile) {
73 glws::Drawable *draw = glws::createDrawable(getVisual(profile));
75 std::cerr << "error: failed to create OpenGL drawable\n";
85 createDrawable(void) {
86 return glretrace::createDrawable(getDefaultProfile());
91 createContext(Context *shareContext, glws::Profile profile) {
92 glws::Context *shareWsContext = shareContext ? shareContext->wsContext : NULL;
93 glws::Context *ctx = glws::createContext(getVisual(profile), shareWsContext, profile, retrace::debug);
95 std::cerr << "error: failed to create OpenGL context\n";
100 return new Context(ctx);
105 createContext(Context *shareContext) {
106 return createContext(shareContext, getDefaultProfile());
110 typedef Context * CurrentData;
111 static os::thread_specific_ptr<CurrentData> currentData;
115 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
117 Context *currentContext = getCurrentContext();
118 glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
120 if (drawable == currentDrawable && context == currentContext) {
124 if (currentContext) {
126 if (!retrace::doubleBuffer) {
127 frame_complete(call);
133 bool success = glws::makeCurrent(drawable, context ? context->wsContext : NULL);
136 std::cerr << "error: failed to make current OpenGL context and drawable\n";
141 if (currentContext) {
142 currentContext->drawable = NULL;
145 CurrentData *currentDataPtr = currentData.get();
146 if (!currentDataPtr) {
147 currentDataPtr = new CurrentData;
148 currentData.reset(currentDataPtr);
151 if (drawable && context) {
152 context->drawable = drawable;
153 *currentData = context;
155 if (!context->used) {
157 context->used = true;
168 getCurrentContext(void) {
169 CurrentData *currentDataPtr = currentData.get();
170 if (!currentDataPtr) {
173 return *currentDataPtr;
180 * Grow the current drawble.
182 * We need to infer the drawable size from GL calls because the drawable sizes
183 * are specified by OS specific calls which we do not trace.
186 updateDrawable(int width, int height) {
187 Context *currentContext = getCurrentContext();
188 if (!currentContext) {
192 glws::Drawable *currentDrawable = currentContext->drawable;
193 assert(currentDrawable);
195 if (currentDrawable->visible &&
196 width <= currentDrawable->width &&
197 height <= currentDrawable->height) {
201 // Ignore zero area viewports
202 if (width == 0 || height == 0) {
206 // Check for bound framebuffer last, as this may have a performance impact.
207 GLint draw_framebuffer = 0;
208 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
209 if (draw_framebuffer != 0) {
213 currentDrawable->resize(width, height);
214 currentDrawable->show();
216 glScissor(0, 0, width, height);
221 parseAttrib(const trace::Value *attribs, int param, int default_ = 0) {
222 const trace::Array *attribs_ = dynamic_cast<const trace::Array *>(attribs);
225 for (size_t i = 0; i + 1 < attribs_->values.size(); i += 2) {
226 int param_i = attribs_->values[i]->toSInt();
231 if (param_i == param) {
232 int value = attribs_->values[i + 1]->toSInt();
242 } /* namespace glretrace */