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"
45 visuals[glws::PROFILE_MAX];
49 getVisual(glws::Profile profile) {
50 glws::Visual * & visual = visuals[profile];
52 visual = glws::createVisual(retrace::doubleBuffer, profile);
54 std::cerr << "error: failed to create OpenGL visual\n";
63 getDefaultProfile(void)
65 if (retrace::coreProfile) {
66 return glws::PROFILE_CORE;
68 return glws::PROFILE_COMPAT;
73 static glws::Drawable *
74 createDrawableHelper(glws::Profile profile, int width = 32, int height = 32, bool pbuffer = false) {
75 glws::Visual *visual = getVisual(profile);
76 glws::Drawable *draw = glws::createDrawable(visual, width, height, pbuffer);
78 std::cerr << "error: failed to create OpenGL drawable\n";
87 createDrawable(glws::Profile profile) {
88 return createDrawableHelper(profile);
93 createDrawable(void) {
94 return createDrawable(getDefaultProfile());
99 createPbuffer(int width, int height) {
100 return createDrawableHelper(getDefaultProfile(), width, height, true);
105 createContext(Context *shareContext, glws::Profile profile) {
106 glws::Visual *visual = getVisual(profile);
107 glws::Context *shareWsContext = shareContext ? shareContext->wsContext : NULL;
108 glws::Context *ctx = glws::createContext(visual, shareWsContext, profile, retrace::debug);
110 std::cerr << "error: failed to create OpenGL context\n";
115 return new Context(ctx);
120 createContext(Context *shareContext) {
121 return createContext(shareContext, getDefaultProfile());
125 static thread_specific Context *
130 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
132 Context *currentContext = currentContextPtr;
133 glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
135 if (drawable == currentDrawable && context == currentContext) {
139 if (currentContext) {
141 if (!retrace::doubleBuffer) {
142 frame_complete(call);
148 bool success = glws::makeCurrent(drawable, context ? context->wsContext : NULL);
151 std::cerr << "error: failed to make current OpenGL context and drawable\n";
156 if (currentContext) {
157 currentContext->drawable = NULL;
159 currentContextPtr = context;
161 if (drawable && context) {
162 context->drawable = drawable;
164 if (!context->used) {
166 context->used = true;
175 getCurrentContext(void) {
176 return currentContextPtr;
181 * Grow the current drawble.
183 * We need to infer the drawable size from GL calls because the drawable sizes
184 * are specified by OS specific calls which we do not trace.
187 updateDrawable(int width, int height) {
188 Context *currentContext = getCurrentContext();
189 if (!currentContext) {
193 glws::Drawable *currentDrawable = currentContext->drawable;
194 assert(currentDrawable);
196 if (currentDrawable->pbuffer) {
200 if (currentDrawable->visible &&
201 width <= currentDrawable->width &&
202 height <= currentDrawable->height) {
206 // Ignore zero area viewports
207 if (width == 0 || height == 0) {
211 // Check for bound framebuffer last, as this may have a performance impact.
212 GLint draw_framebuffer = 0;
213 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
214 if (draw_framebuffer != 0) {
218 currentDrawable->resize(width, height);
219 currentDrawable->show();
221 glScissor(0, 0, width, height);
226 parseAttrib(const trace::Value *attribs, int param, int default_ = 0) {
227 const trace::Array *attribs_ = dynamic_cast<const trace::Array *>(attribs);
230 for (size_t i = 0; i + 1 < attribs_->values.size(); i += 2) {
231 int param_i = attribs_->values[i]->toSInt();
236 if (param_i == param) {
237 int value = attribs_->values[i + 1]->toSInt();
247 } /* namespace glretrace */