]> git.cworth.org Git - apitrace/blob - glws_wgl.cpp
Recognize glFrameTerminatorGREMEDY as swapbuffer frame marker.
[apitrace] / glws_wgl.cpp
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 #include "glimports.hpp"
27 #include "glws.hpp"
28
29
30 namespace glws {
31
32
33 static LRESULT CALLBACK
34 WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
35 {
36     MINMAXINFO *pMMI;
37     switch (uMsg) {
38     case WM_GETMINMAXINFO:
39         // Allow to create a window bigger than the desktop
40         pMMI = (MINMAXINFO *)lParam;
41         pMMI->ptMaxSize.x = 60000;
42         pMMI->ptMaxSize.y = 60000;
43         pMMI->ptMaxTrackSize.x = 60000;
44         pMMI->ptMaxTrackSize.y = 60000;
45         break;
46     default:
47         break;
48     }
49
50     return DefWindowProc(hWnd, uMsg, wParam, lParam);
51 }
52
53
54 class WglDrawable : public Drawable
55 {
56 public:
57     DWORD dwExStyle;
58     DWORD dwStyle;
59     HWND hWnd;
60     HDC hDC;
61     PIXELFORMATDESCRIPTOR pfd;
62     int iPixelFormat;
63
64     WglDrawable(const Visual *vis, int width, int height) :
65         Drawable(vis, width, height)
66     {
67         static bool first = TRUE;
68         RECT rect;
69
70         if (first) {
71             WNDCLASS wc;
72             memset(&wc, 0, sizeof wc);
73             wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
74             wc.hCursor = LoadCursor(NULL, IDC_ARROW);
75             wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
76             wc.lpfnWndProc = WndProc;
77             wc.lpszClassName = "glretrace";
78             wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
79             RegisterClass(&wc);
80             first = FALSE;
81         }
82
83         dwExStyle = 0;
84         dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
85
86         int x = 0, y = 0;
87
88         rect.left = x;
89         rect.top = y;
90         rect.right = rect.left + width;
91         rect.bottom = rect.top + height;
92
93         AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
94
95         hWnd = CreateWindowEx(dwExStyle,
96                               "glretrace", /* wc.lpszClassName */
97                               NULL,
98                               dwStyle,
99                               0, /* x */
100                               0, /* y */
101                               rect.right - rect.left, /* width */
102                               rect.bottom - rect.top, /* height */
103                               NULL,
104                               NULL,
105                               NULL,
106                               NULL);
107         hDC = GetDC(hWnd);
108    
109         memset(&pfd, 0, sizeof pfd);
110         pfd.cColorBits = 4;
111         pfd.cRedBits = 1;
112         pfd.cGreenBits = 1;
113         pfd.cBlueBits = 1;
114         pfd.cAlphaBits = 1;
115         pfd.cDepthBits = 1;
116         pfd.cStencilBits = 1;
117         pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
118         pfd.iLayerType = PFD_MAIN_PLANE;
119         pfd.iPixelType = PFD_TYPE_RGBA;
120         pfd.nSize = sizeof(pfd);
121         pfd.nVersion = 1;
122
123         if (visual->doubleBuffer) {
124            pfd.dwFlags |= PFD_DOUBLEBUFFER;
125         }
126
127         iPixelFormat = ChoosePixelFormat(hDC, &pfd);
128
129         SetPixelFormat(hDC, iPixelFormat, &pfd);
130     }
131
132     ~WglDrawable() {
133         ReleaseDC(hWnd, hDC);
134         DestroyWindow(hWnd);
135     }
136     
137     void
138     resize(int w, int h) {
139         Drawable::resize(w, h);
140         RECT rClient, rWindow;
141         GetClientRect(hWnd, &rClient);
142         GetWindowRect(hWnd, &rWindow);
143         w += (rWindow.right  - rWindow.left) - rClient.right;
144         h += (rWindow.bottom - rWindow.top)  - rClient.bottom;
145         SetWindowPos(hWnd, NULL, rWindow.left, rWindow.top, w, h, SWP_NOMOVE);
146     }
147
148     void show(void) {
149         if (!visible) {
150             ShowWindow(hWnd, SW_SHOW);
151
152             Drawable::show();
153         }
154     }
155
156     void swapBuffers(void) {
157         SwapBuffers(hDC);
158     }
159 };
160
161
162 class WglContext : public Context
163 {
164 public:
165     HGLRC hglrc;
166     WglContext *shareContext;
167
168     WglContext(const Visual *vis, WglContext *share) :
169         Context(vis),
170         hglrc(0),
171         shareContext(share)
172     {}
173
174     ~WglContext() {
175         if (hglrc) {
176             wglDeleteContext(hglrc);
177         }
178     }
179 };
180
181
182 class WglWindowSystem : public WindowSystem
183 {
184 public:
185     Visual *
186     createVisual(bool doubleBuffer) {
187         Visual *visual = new Visual();
188
189         visual->doubleBuffer = doubleBuffer;
190
191         return visual;
192     }
193     
194     Drawable *
195     createDrawable(const Visual *visual, int width, int height)
196     {
197         return new WglDrawable(visual, width, height);
198     }
199
200     Context *
201     createContext(const Visual *visual, Context *shareContext)
202     {
203         return new WglContext(visual, dynamic_cast<WglContext *>(shareContext));
204     }
205
206     bool
207     makeCurrent(Drawable *drawable, Context *context)
208     {
209         if (!drawable || !context) {
210             return wglMakeCurrent(NULL, NULL);
211         } else {
212             WglDrawable *wglDrawable = dynamic_cast<WglDrawable *>(drawable);
213             WglContext *wglContext = dynamic_cast<WglContext *>(context);
214
215             if (!wglContext->hglrc) {
216                 wglContext->hglrc = wglCreateContext(wglDrawable->hDC);
217                 if (!wglContext->hglrc) {
218                     return false;
219                 }
220                 if (wglContext->shareContext) {
221                     wglShareLists(wglContext->shareContext->hglrc,
222                                   wglContext->hglrc);
223                 }
224             }
225
226             return wglMakeCurrent(wglDrawable->hDC, wglContext->hglrc);
227         }
228     }
229
230     bool
231     processEvents(void) {
232         // TODO
233         return true;
234     }
235 };
236
237
238 WindowSystem *createNativeWindowSystem(void) {
239     return new WglWindowSystem();
240 }
241
242
243 } /* namespace glretrace */