Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / winsys.cpp
blob76216d0ee9e2e2601edca15a1d6763b5e19a1786
1 // BEGIN_COPYRIGHT -*- glean -*-
2 //
3 // Copyright (C) 1999 Allen Akin All Rights Reserved.
4 //
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
12 // conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
16 // Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
26 //
27 // END_COPYRIGHT
32 // winsys.cpp: implementation of window-system services class
34 #include <iostream>
35 #include "options.h"
36 #include "winsys.h"
37 #include "dsconfig.h"
38 #include "dsfilt.h"
39 #include "dsurf.h"
40 #include "rc.h"
42 using namespace std;
44 namespace GLEAN {
47 ///////////////////////////////////////////////////////////////////////////////
48 // Constructors
49 ///////////////////////////////////////////////////////////////////////////////
50 #if defined(__X11__)
51 WindowSystem::WindowSystem(Options& o) {
52 // If running in "compare" mode we never actually use the window
53 // system, so we don't initialize it here. This allows us to run
54 // on systems without graphics hardware/software.
55 if (o.mode == Options::compare) {
56 dpy = 0;
57 GLXVersMajor = GLXVersMinor = 0;
58 vip = 0;
59 return;
62 // Open the X11 display:
63 dpy = XOpenDisplay(o.dpyName.c_str());
64 if (!dpy)
65 throw CantOpenDisplay();
67 // Verify that GLX is supported:
68 int error_base, event_base;
69 if (glXQueryExtension(dpy, &error_base, &event_base) == False)
70 throw NoOpenGL();
72 // Record version numbers for later use:
73 if (glXQueryVersion(dpy, &GLXVersMajor, &GLXVersMinor) == False)
74 throw Error(); // this should never happen :-)
76 // Get the list of raw XVisualInfo structures:
77 XVisualInfo vit;
78 vit.screen = DefaultScreen(dpy);
79 int n;
80 vip = XGetVisualInfo(dpy, VisualScreenMask, &vit, &n);
82 // Construct a vector of DrawingSurfaceConfigs corresponding to the
83 // XVisualInfo structures that indicate they support OpenGL:
84 vector<DrawingSurfaceConfig*> glxv;
85 for (int i = 0; i < n; ++i) {
86 int supportsOpenGL;
87 glXGetConfig(dpy, &vip[i], GLX_USE_GL, &supportsOpenGL);
88 if (supportsOpenGL)
89 glxv.push_back(new DrawingSurfaceConfig (dpy, &vip[i]));
92 // Filter the basic list of DrawingSurfaceConfigs according to
93 // constraints provided by the user. (This makes it convenient
94 // to run tests on just a subset of all available configs.)
95 DrawingSurfaceFilter f(o.visFilter); // may throw an exception!
96 surfConfigs = f.filter(glxv, o.maxVisuals);
97 } // WindowSystem::WindowSystem
99 #elif defined(__WIN__)
100 WindowSystem::WindowSystem(Options& o) {
101 // register an window class
102 WNDCLASS wc;
104 wc.style = CS_OWNDC;
105 wc.lpfnWndProc = Window::WindowProc;
106 wc.cbClsExtra = 0;
107 wc.cbWndExtra = 0;
108 wc.hInstance = GetModuleHandle(NULL);
109 wc.hIcon = LoadIcon(wc.hInstance, "glean");
110 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
111 wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
112 wc.lpszMenuName = NULL;
113 wc.lpszClassName = "glean";
115 if (!RegisterClass(&wc))
116 throw Error();
119 HDC hDC = GetDC(GetDesktopWindow());
121 PIXELFORMATDESCRIPTOR pfd;
122 int n = DescribePixelFormat(hDC,0,sizeof(pfd),0);
124 vector<DrawingSurfaceConfig*> glpf;
126 for (int i = 1;i <= n;++i) {
127 DescribePixelFormat(hDC,i,sizeof(pfd),&pfd);
129 glpf.push_back(new DrawingSurfaceConfig(i,&pfd));
132 ReleaseDC(GetDesktopWindow(),hDC);
134 // Filter the basic list of DrawingSurfaceConfigs according to
135 // constraints provided by the user. (This makes it convenient
136 // to run tests on just a subset of all available configs.)
137 DrawingSurfaceFilter f(o.visFilter); // may throw an exception!
138 surfConfigs = f.filter(glpf, o.maxVisuals);
141 #elif defined(__BEWIN__)
142 WindowSystem::WindowSystem(Options& o) {
143 //cout << "Implement Me! WindowSystem::WindowSystem(Options& o)\n";
145 theApp = new BApplication("application/x-AJH-glean");
147 /* for BeOS, we just stack the current config onto the vector so */
148 /* there is at least one thing to iterate over */
149 vector<DrawingSurfaceConfig*> glconfigs;
150 glconfigs.push_back(new DrawingSurfaceConfig());
152 DrawingSurfaceFilter f(o.visFilter); // may throw an exception!
153 surfConfigs = f.filter(glconfigs, o.maxVisuals);
157 #elif defined(__AGL__)
158 WindowSystem::WindowSystem(Options& o) {
159 GDHandle mainGD;
160 //HW/SW Depth Reserved
161 GLint testTypes[][3] = {
162 {AGL_ACCELERATED, 16, 0 },
163 {AGL_ACCELERATED, 16, 0 },
164 {AGL_ACCELERATED, 16, 0 },
165 {0, 16, 0 },
166 {0, 16, 0 },
167 {0, 0, 0 }
169 GLint testAttrib[][10]= {
170 { AGL_RGBA, AGL_DOUBLEBUFFER, AGL_ACCELERATED, 16, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE},
171 { AGL_RGBA, AGL_ACCELERATED, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 16, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE},
172 { AGL_RGBA, AGL_DOUBLEBUFFER, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE},
173 { AGL_RENDERER_ID, AGL_RENDERER_GENERIC_ID, AGL_RGBA, AGL_DOUBLEBUFFER, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE},
174 { AGL_RENDERER_ID, AGL_RENDERER_GENERIC_ID, AGL_RGBA, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 16 , AGL_NONE, AGL_NONE, AGL_NONE, AGL_NONE}
176 AGLPixelFormat pf;
177 GLint index = 0;
179 mainGD = GetMainDevice();
180 if (!mainGD)
181 throw CantOpenDisplay();
183 // Construct a vector of DrawingSurfaceConfigs corresponding to the
184 // returned pixel formats
185 vector<DrawingSurfaceConfig*> glpf;
187 while (testTypes[index][1] != 0)
189 pf = aglChoosePixelFormat(&mainGD, 1, testAttrib[index]);
190 if ( (pf == NULL) && ( testTypes[index][0] == 0) )
192 testAttrib[index][1] = 0x30300;
193 pf = aglChoosePixelFormat(&mainGD, 1, testAttrib[index]);
195 if (pf != NULL) glpf.push_back(new DrawingSurfaceConfig (index+1, pf));
197 index++;
200 // Filter the basic list of DrawingSurfaceConfigs according to
201 // constraints provided by the user. (This makes it convenient
202 // to run tests on just a subset of all available configs.)
203 DrawingSurfaceFilter f(o.visFilter); // may throw an exception!
204 surfConfigs = f.filter(glpf, o.maxVisuals);
207 #endif
209 ///////////////////////////////////////////////////////////////////////////////
210 // Destructors
211 ///////////////////////////////////////////////////////////////////////////////
212 #if defined(__X11__)
213 WindowSystem::~WindowSystem() {
214 XFree(vip);
215 } // WindowSystem:: ~WindowSystem
217 #elif defined(__WIN__)
218 WindowSystem::~WindowSystem() {
220 #elif defined(__BEWIN__)
221 WindowSystem::~WindowSystem() {
222 delete theApp;
223 } // WindowSystem:: ~WindowSystem
224 #elif defined(__AGL__)
225 WindowSystem::~WindowSystem() {
227 #endif
229 ///////////////////////////////////////////////////////////////////////////////
230 // makeCurrent and friends - binding contexts and drawing surfaces
231 ///////////////////////////////////////////////////////////////////////////////
233 bool
234 WindowSystem::makeCurrent() {
235 # if defined(__X11__)
236 # if defined(GLX_VERSION_1_3)
237 // XXX Need to write GLX 1.3 MakeCurrent code
238 # endif
239 return glXMakeCurrent(dpy, None, 0);
240 # elif defined(__WIN__)
241 return wglMakeCurrent(0,0);
242 # elif defined(__AGL__)
243 return aglSetCurrentContext(NULL);
244 # endif
245 } // WindowSystem::makeCurrent
247 bool
248 WindowSystem::makeCurrent(RenderingContext& r, Window& w) {
249 # if defined(__X11__)
250 # if defined(GLX_VERSION_1_3)
251 // XXX Need to write GLX 1.3 MakeCurrent code
252 # endif
253 return glXMakeCurrent(dpy, w.xWindow, r.rc);
254 # elif defined(__WIN__)
255 return wglMakeCurrent(w.get_dc(),r.rc);
256 # elif defined(__AGL__)
257 if (GL_FALSE == aglSetDrawable(r.rc, (AGLDrawable) GetWindowPort (w.macWindow)))
258 return GL_FALSE;
259 if (GL_FALSE == aglSetCurrentContext(r.rc))
260 return GL_FALSE;
261 return true;
262 # endif
263 } // WindowSystem::makeCurrent
265 void
266 WindowSystem::quiesce() {
267 # if defined(__X11__)
268 XSync(dpy, False);
269 # elif defined(__WIN__)
270 # endif
271 } // WindowSystem::quiesce
273 } // namespace GLEAN