]> git.cworth.org Git - apitrace/blob - retrace/glretrace_ws.cpp
Cleanup glretrace<->glws integration.
[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 glws::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 glws::Context *
92 createContext(glws::Context *shareContext, glws::Profile profile) {
93     glws::Context *ctx = glws::createContext(getVisual(profile), shareContext, profile, retrace::debug);
94     if (!ctx) {
95         std::cerr << "error: failed to create OpenGL context\n";
96         exit(1);
97         return NULL;
98     }
99
100     return ctx;
101 }
102
103
104 glws::Context *
105 createContext(glws::Context *shareContext) {
106     return createContext(shareContext, getDefaultProfile());
107 }
108
109
110 bool
111 makeCurrent(trace::Call &call, glws::Drawable *drawable, glws::Context *context)
112 {
113     if (drawable == currentDrawable && context == currentContext) {
114         return true;
115     }
116
117     if (currentDrawable && currentContext) {
118         glFlush();
119         if (!retrace::doubleBuffer) {
120             frame_complete(call);
121         }
122     }
123
124     bool success = glws::makeCurrent(drawable, context);
125
126     if (!success) {
127         std::cerr << "error: failed to make current OpenGL context and drawable\n";
128         exit(1);
129         return false;
130     }
131
132     if (drawable && context) {
133         currentDrawable = drawable;
134         currentContext = context;
135     } else {
136         currentDrawable = NULL;
137         currentContext = NULL;
138     }
139
140     return true;
141 }
142
143
144
145
146 /**
147  * Grow the current drawble.
148  *
149  * We need to infer the drawable size from GL calls because the drawable sizes
150  * are specified by OS specific calls which we do not trace.
151  */
152 void
153 updateDrawable(int width, int height) {
154     if (!currentDrawable) {
155         return;
156     }
157
158     if (!currentDrawable->visible &&
159         width  <= currentDrawable->width &&
160         height <= currentDrawable->height) {
161         return;
162     }
163
164     // Ignore zero area viewports
165     if (width == 0 || height == 0) {
166         return;
167     }
168
169     // Check for bound framebuffer last, as this may have a performance impact.
170     GLint draw_framebuffer = 0;
171     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
172     if (draw_framebuffer != 0) {
173         return;
174     }
175
176     currentDrawable->resize(width, height);
177     currentDrawable->show();
178
179     glScissor(0, 0, width, height);
180 }
181
182
183 } /* namespace glretrace */