]> git.cworth.org Git - apitrace/blob - retrace/glws.hpp
d07eeb4db88c9132ac07db566c2ed57a5165bf93
[apitrace] / retrace / glws.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 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  * Abstraction for GL window system specific APIs (GLX, WGL).
28  */
29
30 #ifndef _GLWS_HPP_
31 #define _GLWS_HPP_
32
33
34 #include <assert.h>
35
36 #include <vector>
37 #include <set>
38 #include <string>
39
40
41 namespace glws {
42
43
44 enum Profile {
45     PROFILE_COMPAT = 0,
46     PROFILE_CORE,
47     PROFILE_ES1,
48     PROFILE_ES2,
49     PROFILE_MAX
50 };
51
52
53 bool
54 checkExtension(const char *extName, const char *extString);
55
56
57 template< class T >
58 class Attributes {
59 protected:
60     std::vector<T> attribs;
61
62 public:
63     void add(T param) {
64         attribs.push_back(param);
65     }
66
67     void add(T pname, T pvalue) {
68         add(pname);
69         add(pvalue);
70     }
71
72     void end(T terminator = 0) {
73         add(terminator);
74     }
75
76     operator T * (void) {
77         return &attribs[0];
78     }
79
80     operator const T * (void) const {
81         return &attribs[0];
82     }
83 };
84
85
86 class Visual
87 {
88 public:
89     unsigned long redMask;
90     unsigned long greenMask;
91     unsigned long blueMask;
92     unsigned long alphaMask;
93     bool doubleBuffer;
94
95     virtual ~Visual() {}
96 };
97
98 class Drawable
99 {
100 public:
101     const Visual *visual;
102     int width;
103     int height;
104     bool pbuffer;
105     bool visible;
106
107     Drawable(const Visual *vis, int w, int h, bool pb) :
108         visual(vis),
109         width(w),
110         height(h),
111         pbuffer(pb),
112         visible(false)
113     {}
114
115     virtual ~Drawable() {}
116     
117     virtual void
118     resize(int w, int h) {
119         width = w;
120         height = h;
121     }
122
123     virtual void
124     show(void) {
125         assert(!pbuffer);
126         visible = true;
127     }
128
129     virtual void swapBuffers(void) = 0;
130 };
131
132 class Context
133 {
134 public:
135     const Visual *visual;
136     Profile profile;
137     
138     std::set<std::string> extensions;
139
140     Context(const Visual *vis, Profile prof) :
141         visual(vis),
142         profile(prof)
143     {}
144
145     virtual ~Context() {}
146
147     // Context must be current
148     bool
149     hasExtension(const char *extension);
150 };
151
152
153 void
154 init(void);
155
156 void
157 cleanup(void);
158
159 Visual *
160 createVisual(bool doubleBuffer = false, Profile profile = PROFILE_COMPAT);
161
162 Drawable *
163 createDrawable(const Visual *visual, int width, int height, bool pbuffer = false);
164
165 Context *
166 createContext(const Visual *visual, Context *shareContext = 0, Profile profile = PROFILE_COMPAT, bool debug = false);
167
168 bool
169 makeCurrent(Drawable *drawable, Context *context);
170
171 void
172 createWindow(Drawable *drawable, const Visual *visual);
173
174 void
175 destroyWindow(Drawable *drawable);
176
177 Drawable *
178 createPixmap(unsigned width, unsigned height, unsigned depth);
179
180 Drawable *
181 createGLPixmap(GLXFBConfig fbconfig, Drawable *pixmap,
182                unsigned width, unsigned height, int *attrib_list);
183
184 void
185 bindTexImage(glws::Drawable *pixmap, int buffer);
186
187 void
188 releaseTexImage(glws::Drawable *pixmap, int buffer);
189
190 void
191 copySubBuffer(glws::Drawable *drawable, int x, int y, int width, int height);
192
193 void
194 putImageData(glws::Drawable *drawable, char *data,
195              int width, int height, int depth,
196              int bits_per_pixel, int bytes_per_line, int byte_order);
197
198 GLXFBConfig
199 chooseConfig(int *attrib_list);
200
201 bool
202 processEvents(void);
203
204
205 } /* namespace glws */
206
207
208 #endif /* _GLWS_HPP_ */