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;
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);
75 std::cerr << "error: failed to create OpenGL drawable\n";
85 createDrawable(glws::Profile profile) {
86 return createDrawableHelper(profile);
91 createDrawable(void) {
92 return createDrawable(getDefaultProfile());
97 createPbuffer(int width, int height) {
98 return createDrawableHelper(getDefaultProfile(), width, height, true);
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);
107 std::cerr << "error: failed to create OpenGL context\n";
112 return new Context(ctx);
117 createContext(Context *shareContext) {
118 return createContext(shareContext, getDefaultProfile());
122 typedef Context * CurrentData;
123 static os::thread_specific_ptr<CurrentData> currentData;
127 makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
129 Context *currentContext = getCurrentContext();
130 glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
132 if (drawable == currentDrawable && context == currentContext) {
136 if (currentContext) {
138 if (!retrace::doubleBuffer) {
139 frame_complete(call);
145 bool success = glws::makeCurrent(drawable, context ? context->wsContext : NULL);
148 std::cerr << "error: failed to make current OpenGL context and drawable\n";
153 if (currentContext) {
154 currentContext->drawable = NULL;
157 CurrentData *currentDataPtr = currentData.get();
158 if (!currentDataPtr) {
159 currentDataPtr = new CurrentData;
160 currentData.reset(currentDataPtr);
163 if (drawable && context) {
164 context->drawable = drawable;
165 *currentData = context;
167 if (!context->used) {
169 context->used = true;
180 getCurrentContext(void) {
181 CurrentData *currentDataPtr = currentData.get();
182 if (!currentDataPtr) {
185 return *currentDataPtr;
192 * Grow the current drawble.
194 * We need to infer the drawable size from GL calls because the drawable sizes
195 * are specified by OS specific calls which we do not trace.
198 updateDrawable(int width, int height) {
199 Context *currentContext = getCurrentContext();
200 if (!currentContext) {
204 glws::Drawable *currentDrawable = currentContext->drawable;
205 assert(currentDrawable);
207 if (currentDrawable->pbuffer) {
211 if (currentDrawable->visible &&
212 width <= currentDrawable->width &&
213 height <= currentDrawable->height) {
217 // Ignore zero area viewports
218 if (width == 0 || height == 0) {
222 // Check for bound framebuffer last, as this may have a performance impact.
223 GLint draw_framebuffer = 0;
224 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
225 if (draw_framebuffer != 0) {
229 currentDrawable->resize(width, height);
230 currentDrawable->show();
232 glScissor(0, 0, width, height);
237 parseAttrib(const trace::Value *attribs, int param, int default_ = 0) {
238 const trace::Array *attribs_ = dynamic_cast<const trace::Array *>(attribs);
241 for (size_t i = 0; i + 1 < attribs_->values.size(); i += 2) {
242 int param_i = attribs_->values[i]->toSInt();
247 if (param_i == param) {
248 int value = attribs_->values[i + 1]->toSInt();
258 } /* namespace glretrace */