VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/glwindrv.cpp@ 3463

Last change on this file since 3463 was 3463, checked in by vboxsync, 18 years ago

Some logging

File size: 12.8 KB
Line 
1/** @file
2 *
3 * VBox OpenGL
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23#include <iprt/alloc.h>
24#include <iprt/string.h>
25#include <iprt/assert.h>
26#include <iprt/thread.h>
27#include <VBox/err.h>
28#include "vboxgl.h"
29
30#define LOG_GROUP LOG_GROUP_SHARED_OPENGL
31#include <VBox/log.h>
32#include "gldrv.h"
33
34#ifdef VBOX_OGL_DEBUG_WINDOW_OUTPUT
35LRESULT CALLBACK VBoxOGLWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
36{
37 switch (message)
38 {
39 case WM_CREATE:
40 return 0;
41
42 case WM_CLOSE:
43 PostQuitMessage( 0 );
44 return 0;
45
46 case WM_DESTROY:
47 return 0;
48
49 default:
50 return DefWindowProc( hWnd, message, wParam, lParam );
51 }
52}
53
54DECLCALLBACK(int) vboxWndThread(RTTHREAD ThreadSelf, void *pvUser)
55{
56 VBOXOGLCTX *pClient = (VBOXOGLCTX *)pvUser;
57 HWND hwnd;
58
59 hwnd = pClient->hwnd= CreateWindow("VBoxOGL", "VirtualBox OpenGL",
60 WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
61 0, 0, 0, 0,
62 NULL, NULL, 0, NULL);
63 Assert(hwnd);
64 while(true)
65 {
66 MSG msg;
67
68 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
69 {
70 if (msg.message == WM_QUIT)
71 break;
72
73 TranslateMessage(&msg);
74 DispatchMessage(&msg);
75 }
76 }
77 DestroyWindow(hwnd);
78 return VINF_SUCCESS;
79}
80
81#endif
82
83/**
84 * Global init of VBox OpenGL for windows
85 *
86 * @returns VBox error code
87 */
88int vboxglGlobalInit()
89{
90#ifdef VBOX_OGL_DEBUG_WINDOW_OUTPUT
91 WNDCLASS wc;
92
93 wc.style = CS_OWNDC;
94 wc.lpfnWndProc = VBoxOGLWndProc;
95 wc.cbClsExtra = 0;
96 wc.cbWndExtra = 0;
97 wc.hInstance = 0;
98 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
99 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
100 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
101 wc.lpszMenuName = NULL;
102 wc.lpszClassName = "VBoxOGL";
103 RegisterClass(&wc);
104#endif
105
106 return VINF_SUCCESS;
107}
108
109/**
110 * Client connect init
111 *
112 * @returns VBox error code
113 * @param pClient Client context
114 */
115int vboxglConnect(PVBOXOGLCTX pClient)
116{
117 Log(("vboxglConnect\n"));
118 return VINF_SUCCESS;
119}
120
121/**
122 * Client disconnect cleanup
123 *
124 * @returns VBox error code
125 * @param pClient Client context
126 */
127int vboxglDisconnect(PVBOXOGLCTX pClient)
128{
129 Log(("vboxglDisconnect\n"));
130#ifdef VBOX_OGL_DEBUG_WINDOW_OUTPUT
131 if (pClient->hwnd)
132 {
133 if (pClient->hdc)
134 ReleaseDC(pClient->hwnd, pClient->hdc);
135 PostMessage(pClient->hwnd, WM_CLOSE, 0, 0);
136 pClient->hwnd = 0;
137 }
138#endif
139 return VINF_SUCCESS;
140}
141
142
143/* Driver functions */
144void vboxglDrvCreateContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
145{
146 HGLRC glrc = 0;
147
148 OGL_CMD(DrvCreateContext, 1);
149 OGL_PARAM(HDC, hdc);
150
151 Log(("DrvCreateContext %x\n", hdc));
152 Assert(VBOX_OGL_GUEST_TO_HOST_HDC(hdc));
153 glrc = wglCreateContext(pClient->hdc);
154
155 pClient->lastretval = (uint64_t)glrc;
156 pClient->fHasLastError = true;
157 pClient->ulLastError = GetLastError();
158}
159
160void vboxglDrvSetContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
161{
162 OGL_CMD(DrvSetContext, 2);
163 OGL_PARAM(HDC, hdc);
164 OGL_PARAM(HGLRC, hglrc);
165
166 Log(("DrvSetyContext %x %x\n", hdc, hglrc));
167 pClient->lastretval = wglMakeCurrent(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), hglrc);
168 if (!pClient->lastretval)
169 Log(("wglMakeCurrent failed with %d\n", GetLastError()));
170
171 pClient->fHasLastError = true;
172 pClient->ulLastError = GetLastError();
173}
174
175void vboxglDrvCopyContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
176{
177 OGL_CMD(DrvDeleteContext, 3);
178 OGL_PARAM(HGLRC, hglrcSrc);
179 OGL_PARAM(HGLRC, hglrcDst);
180 OGL_PARAM(UINT, mask);
181 Log(("DrvCopyContext %x %x %x\n", hglrcSrc, hglrcDst, mask));
182 pClient->lastretval = wglCopyContext(hglrcSrc, hglrcDst, mask);
183 if (!pClient->lastretval)
184 Log(("wglCopyContext failed with %d\n", GetLastError()));
185
186 pClient->fHasLastError = true;
187 pClient->ulLastError = GetLastError();
188}
189
190void vboxglDrvReleaseContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
191{
192 OGL_CMD(DrvReleaseContext, 1);
193 OGL_PARAM(HGLRC, hglrc);
194
195 Log(("DrvReleaseContext %x\n", hglrc));
196 /* clear current selection */
197 pClient->lastretval = wglMakeCurrent(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), NULL);
198 if (!pClient->lastretval)
199 Log(("wglMakeCurrent failed with %d\n", GetLastError()));
200 pClient->fHasLastError = true;
201 pClient->ulLastError = GetLastError();
202}
203
204void vboxglDrvDeleteContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
205{
206 OGL_CMD(DrvDeleteContext, 1);
207 OGL_PARAM(HGLRC, hglrc);
208
209 Log(("DrvDeleteContext %x\n", hglrc));
210 pClient->lastretval = wglDeleteContext(hglrc);
211 if (!pClient->lastretval)
212 Log(("wglDeleteContext failed with %d\n", GetLastError()));
213 pClient->fHasLastError = true;
214 pClient->ulLastError = GetLastError();
215}
216
217void vboxglDrvCreateLayerContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
218{
219 HGLRC glrc = 0;
220
221 OGL_CMD(DrvCreateLayerContext, 2);
222 OGL_PARAM(HDC, hdc);
223 OGL_PARAM(int, iLayerPlane);
224
225 Log(("DrvCreateLayerContext %x %d\n", hdc, iLayerPlane));
226 Assert(VBOX_OGL_GUEST_TO_HOST_HDC(hdc));
227 glrc = wglCreateLayerContext(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iLayerPlane);
228
229 pClient->lastretval = (uint64_t)glrc;
230 pClient->fHasLastError = true;
231 pClient->ulLastError = GetLastError();
232}
233
234void vboxglDrvShareLists(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
235{
236 OGL_CMD(DrvShareLists, 3);
237 OGL_PARAM(HGLRC, hglrc1);
238 OGL_PARAM(HGLRC, hglrc2);
239
240 Log(("DrvShareLists %x %x\n", hglrc1, hglrc2));
241 pClient->lastretval = wglShareLists(hglrc1, hglrc2);
242 pClient->fHasLastError = true;
243 pClient->ulLastError = GetLastError();
244}
245
246
247void vboxglDrvRealizeLayerPalette(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
248{
249 OGL_CMD(DrvRealizeLayerPalette, 3);
250 OGL_PARAM(HDC, hdc);
251 OGL_PARAM(int, iLayerPlane);
252 OGL_PARAM(BOOL, bRealize);
253 Log(("DrvRealizeLayerPalette %x %d %d\n", hdc, iLayerPlane, bRealize));
254 pClient->lastretval = wglRealizeLayerPalette(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iLayerPlane, bRealize);
255 pClient->fHasLastError = true;
256 pClient->ulLastError = GetLastError();
257}
258
259void vboxglDrvSwapLayerBuffers(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
260{
261 OGL_CMD(DrvSwapLayerBuffers, 2);
262 OGL_PARAM(HDC, hdc);
263 OGL_PARAM(UINT, fuPlanes);
264 Log(("DrvSwapLayerBuffers %x %d\n", hdc, fuPlanes));
265 pClient->lastretval = wglSwapLayerBuffers(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), fuPlanes);
266 pClient->fHasLastError = true;
267 pClient->ulLastError = GetLastError();
268}
269
270void vboxglDrvSetPixelFormat(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
271{
272 int rc;
273 PIXELFORMATDESCRIPTOR pfd;
274
275 OGL_CMD(DrvSetPixelFormat, 4);
276 OGL_PARAM(HDC, hdc);
277 OGL_PARAM(int, iPixelFormat);
278 OGL_PARAM(uint32_t, cx);
279 OGL_PARAM(uint32_t, cy);
280
281#ifdef VBOX_OGL_DEBUG_WINDOW_OUTPUT
282 if (!pClient->hwnd)
283 {
284 RTThreadCreate(NULL, vboxWndThread, pClient, 0, RTTHREADTYPE_DEFAULT, 0, "OpenGLWnd");
285 while (!pClient->hwnd)
286 RTThreadSleep(100);
287 }
288 RECT rect;
289 rect.bottom = 0;
290 rect.left = 0;
291 rect.right = cx;
292 rect.top = cy;
293 /* Convert client rectangel to window rectangle */
294 AdjustWindowRect(&rect, WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, FALSE);
295 SetWindowPos(pClient->hwnd, NULL, 0, 0, rect.right - rect.left, rect.top - rect.bottom, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
296
297 pClient->hdc = GetDC(pClient->hwnd);
298
299 rc = DescribePixelFormat(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iPixelFormat, sizeof(pfd), &pfd);
300 if (rc)
301 {
302 pClient->lastretval = SetPixelFormat(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iPixelFormat, &pfd);
303 }
304 else
305 {
306 Log(("DescribePixelFormat %d failed with 0 (%d)\n", iPixelFormat, GetLastError()));
307 pClient->lastretval = 0;
308 }
309
310#else
311 AssertFailed();
312#endif
313
314 Log(("DrvSetPixelFormat %x %d (%d,%d)\n", hdc, iPixelFormat, cx, cy));
315 pClient->fHasLastError = true;
316 pClient->ulLastError = GetLastError();
317}
318
319void vboxglDrvSwapBuffers(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
320{
321 OGL_CMD(DrvSwapBuffers, 1);
322 OGL_PARAM(HDC, hdc);
323
324 Log(("DrvSwapBuffers %x\n", hdc));
325 pClient->lastretval = SwapBuffers(VBOX_OGL_GUEST_TO_HOST_HDC(hdc));
326 if (!pClient->lastretval)
327 Log(("SwapBuffers failed with %d\n", GetLastError()));
328
329 /** @todo sync bitmap/screen contents */
330 pClient->fHasLastError = true;
331 pClient->ulLastError = GetLastError();
332}
333
334void vboxglDrvDescribeLayerPlane(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
335{
336 PLAYERPLANEDESCRIPTOR plpd;
337
338 OGL_CMD(DrvDescribeLayerPlane, 4);
339 OGL_PARAM(HDC, hdc);
340 OGL_PARAM(int, iPixelFormat);
341 OGL_PARAM(int, iLayerPlane);
342 OGL_PARAM(UINT, nBytes);
343 Assert(pClient->cbLastParam == nBytes);
344 plpd = (PLAYERPLANEDESCRIPTOR)pClient->pLastParam;
345
346 Log(("DrvDescribeLayerPlane %x %d %d %d %x\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd));
347 pClient->lastretval = wglDescribeLayerPlane(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iPixelFormat, iLayerPlane, nBytes, plpd);
348 if (!pClient->lastretval)
349 Log(("wglDescribeLayerPlane failed with %d\n", GetLastError()));
350 pClient->fHasLastError = true;
351 pClient->ulLastError = GetLastError();
352}
353
354void vboxglDrvSetLayerPaletteEntries(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
355{
356 OGL_CMD(DrvSetLayerPaletteEntries, 5);
357 OGL_PARAM(HDC, hdc);
358 OGL_PARAM(int, iLayerPlane);
359 OGL_PARAM(int, iStart);
360 OGL_PARAM(int, cEntries);
361 OGL_MEMPARAM(COLORREF, pcr);
362
363 Log(("DrvSetLayerPaletteEntries %x %d %d %d %x\n", hdc, iLayerPlane, iStart, cEntries, pcr));
364 pClient->lastretval = wglSetLayerPaletteEntries(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iLayerPlane, iStart, cEntries, pcr);
365 if (!pClient->lastretval)
366 Log(("wglSetLayerPaletteEntries failed with %d\n", GetLastError()));
367 pClient->fHasLastError = true;
368 pClient->ulLastError = GetLastError();
369}
370
371void vboxglDrvGetLayerPaletteEntries(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
372{
373 COLORREF *pcr;
374
375 OGL_CMD(DrvGetLayerPaletteEntries, 4);
376 OGL_PARAM(HDC, hdc);
377 OGL_PARAM(int, iLayerPlane);
378 OGL_PARAM(int, iStart);
379 OGL_PARAM(int, cEntries);
380
381 Assert(pClient->cbLastParam == sizeof(COLORREF)*cEntries);
382 pcr = (COLORREF *)pClient->pLastParam;
383
384 Log(("DrvGetLayerPaletteEntries %x %d %d %d %x\n", hdc, iLayerPlane, iStart, cEntries, pcr));
385 pClient->lastretval = wglGetLayerPaletteEntries(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iLayerPlane, iStart, cEntries, pcr);
386 if (!pClient->lastretval)
387 Log(("wglGetLayerPaletteEntries failed with %d\n", GetLastError()));
388 pClient->fHasLastError = true;
389 pClient->ulLastError = GetLastError();
390}
391
392void vboxglDrvDescribePixelFormat(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
393{
394 LPPIXELFORMATDESCRIPTOR ppfd;
395
396 OGL_CMD(DrvDescribePixelFormat, 3);
397 OGL_PARAM(HDC, hdc);
398 OGL_PARAM(int, iPixelFormat);
399 OGL_PARAM(UINT, nBytes);
400 Assert(pClient->cbLastParam == nBytes);
401 ppfd = (LPPIXELFORMATDESCRIPTOR)pClient->pLastParam;
402
403 hdc = VBOX_OGL_GUEST_TO_HOST_HDC(hdc);
404 if (!hdc)
405 hdc = GetDC(0);
406
407 Log(("DrvDescribePixelFormat %x %d %d %x\n", hdc, iPixelFormat, nBytes, ppfd));
408 pClient->lastretval = DescribePixelFormat(hdc, iPixelFormat, nBytes, ppfd);
409 if (!pClient->lastretval)
410 Log(("DescribePixelFormat failed with %d\n", GetLastError()));
411
412 pClient->fHasLastError = true;
413 pClient->ulLastError = GetLastError();
414
415 if (!VBOX_OGL_GUEST_TO_HOST_HDC(hdc))
416 ReleaseDC(0, pClient->hdc);
417}
418
419bool vboxDrvIsExtensionAvailable(char *pszExtFunctionName)
420{
421 Log(("vboxDrvIsExtensionAvailable %s -> %d\n", pszExtFunctionName, !!wglGetProcAddress(pszExtFunctionName)));
422 return !!wglGetProcAddress(pszExtFunctionName);
423}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette