VirtualBox

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

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

3 parameters

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;
147
148 OGL_CMD(DrvCreateContext, 6);
149 OGL_PARAM(HDC, hdc);
150 OGL_PARAM(uint32_t, cx);
151 OGL_PARAM(uint32_t, cy);
152 OGL_PARAM(BYTE, cColorBits);
153 OGL_PARAM(BYTE, iPixelType);
154 OGL_PARAM(BYTE, cDepthBits);
155
156 Log(("DrvCreateContext %x (%d,%d) bpp=%d type=%x depth=%d\n", hdc, cx, cy, cColorBits, iPixelType, cDepthBits));
157#ifdef VBOX_OGL_DEBUG_WINDOW_OUTPUT
158 if (!pClient->hwnd)
159 {
160 RTThreadCreate(NULL, vboxWndThread, pClient, 0, RTTHREADTYPE_DEFAULT, 0, "OpenGLWnd");
161 while (!pClient->hwnd)
162 RTThreadSleep(100);
163 }
164 SetWindowPos(pClient->hwnd, NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
165
166 PIXELFORMATDESCRIPTOR pfd = {0};
167 int format;
168
169 pClient->hdc = GetDC(pClient->hwnd);
170
171 pfd.nSize = sizeof(pfd);
172 pfd.nVersion = 1;
173 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
174 pfd.iPixelType = iPixelType;
175 pfd.cColorBits = cColorBits;
176 pfd.cDepthBits = cDepthBits;
177 pfd.iLayerType = PFD_MAIN_PLANE;
178 uint32_t lasterr = glGetError();
179 format = ChoosePixelFormat(pClient->hdc, &pfd);
180 lasterr = glGetError();
181 SetPixelFormat(pClient->hdc, format, &pfd);
182
183 lasterr = glGetError();
184 glrc = wglCreateContext(pClient->hdc);
185 lasterr = glGetError();
186 Assert(glrc);
187#else
188 AssertFailed();
189 glrc = 0;
190#endif
191
192 pClient->lastretval = (uint64_t)glrc;
193}
194
195void vboxglDrvSetContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
196{
197 BOOL ret;
198
199 OGL_CMD(DrvSetContext, 2);
200 OGL_PARAM(HDC, hdc);
201 OGL_PARAM(HGLRC, hglrc);
202
203 Log(("DrvSetyContext %x %x\n", hdc, hglrc));
204#ifdef VBOX_OGL_DEBUG_WINDOW_OUTPUT
205 ret = wglMakeCurrent(pClient->hdc, hglrc);
206 if (!ret)
207 Log(("wglMakeCurrent failed with %d\n", GetLastError()));
208#else
209 AssertFailed();
210#endif
211}
212
213void vboxglDrvCopyContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
214{
215 OGL_CMD(DrvDeleteContext, 3);
216 OGL_PARAM(HGLRC, hglrcSrc);
217 OGL_PARAM(HGLRC, hglrcDst);
218 OGL_PARAM(UINT, mask);
219 Log(("DrvCopyContext %x %x %x\n", hglrcSrc, hglrcDst, mask));
220 pClient->lastretval = wglCopyContext(hglrcSrc, hglrcDst, mask);
221 if (!pClient->lastretval)
222 Log(("wglCopyContext failed with %d\n", GetLastError()));
223}
224
225void vboxglDrvReleaseContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
226{
227 BOOL ret;
228
229 OGL_CMD(DrvReleaseContext, 1);
230 OGL_PARAM(HGLRC, hglrc);
231
232 Log(("DrvReleaseContext %x\n", hglrc));
233 /* clear current selection */
234 ret = wglMakeCurrent(pClient->hdc, NULL);
235 if (!ret)
236 Log(("wglMakeCurrent failed with %d\n", GetLastError()));
237}
238
239void vboxglDrvDeleteContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
240{
241 BOOL ret;
242
243 OGL_CMD(DrvDeleteContext, 1);
244 OGL_PARAM(HGLRC, hglrc);
245
246 Log(("DrvDeleteContext %x\n", hglrc));
247 ret = wglDeleteContext(hglrc);
248 if (!ret)
249 Log(("wglDeleteContext failed with %d\n", GetLastError()));
250}
251
252void vboxglDrvCreateLayerContext(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
253{
254 HGLRC glrc;
255
256 OGL_CMD(DrvCreateLayerContext, 7);
257 OGL_PARAM(HDC, hdc);
258 OGL_PARAM(int, iLayerPlane);
259 OGL_PARAM(uint32_t, cx);
260 OGL_PARAM(uint32_t, cy);
261 OGL_PARAM(BYTE, cColorBits);
262 OGL_PARAM(BYTE, iPixelType);
263 OGL_PARAM(BYTE, cDepthBits);
264
265 Log(("DrvCreateLayerContext %x (%d,%d) bpp=%d type=%x depth=%d\n", hdc, cx, cy, cColorBits, iPixelType, cDepthBits));
266#ifdef VBOX_OGL_DEBUG_WINDOW_OUTPUT
267 if (!pClient->hwnd)
268 {
269 pClient->hwnd= CreateWindow("VBoxOGL", "VirtualBox OpenGL",
270 WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
271 0, 0, cx, cy,
272 NULL, NULL, 0, NULL);
273 }
274 else
275 {
276 SetWindowPos(pClient->hwnd, NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
277 }
278 PIXELFORMATDESCRIPTOR pfd = {0};
279 int format;
280
281 pClient->hdc = GetDC(pClient->hwnd);
282
283 pfd.nSize = sizeof(pfd);
284 pfd.nVersion = 1;
285 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
286 pfd.iPixelType = iPixelType;
287 pfd.cColorBits = cColorBits;
288 pfd.cDepthBits = cDepthBits;
289 pfd.iLayerType = PFD_MAIN_PLANE;
290 format = ChoosePixelFormat(pClient->hdc, &pfd);
291 SetPixelFormat(pClient->hdc, format, &pfd);
292
293 glrc = wglCreateLayerContext(pClient->hdc, iLayerPlane);
294#else
295 AssertFailed();
296#endif
297}
298
299void vboxglDrvShareLists(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
300{
301 OGL_CMD(DrvShareLists, 3);
302 OGL_PARAM(HGLRC, hglrc1);
303 OGL_PARAM(HGLRC, hglrc2);
304
305 Log(("DrvShareLists %x %x\n", hglrc1, hglrc2));
306 pClient->lastretval = wglShareLists(hglrc1, hglrc2);
307}
308
309
310void vboxglDrvRealizeLayerPalette(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
311{
312 OGL_CMD(DrvRealizeLayerPalette, 3);
313 OGL_PARAM(HDC, hdc);
314 OGL_PARAM(int, iLayerPlane);
315 OGL_PARAM(BOOL, bRealize);
316 Log(("DrvRealizeLayerPalette %x %d %d\n", hdc, iLayerPlane, bRealize));
317 pClient->lastretval = wglRealizeLayerPalette(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iLayerPlane, bRealize);
318}
319
320void vboxglDrvSwapLayerBuffers(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
321{
322 OGL_CMD(DrvSwapLayerBuffers, 2);
323 OGL_PARAM(HDC, hdc);
324 OGL_PARAM(UINT, fuPlanes);
325 Log(("DrvSwapLayerBuffers %x %d\n", hdc, fuPlanes));
326 pClient->lastretval = wglSwapLayerBuffers(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), fuPlanes);
327}
328
329void vboxglDrvSetPixelFormat(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
330{
331 int rc;
332 PIXELFORMATDESCRIPTOR pfd;
333
334 OGL_CMD(DrvSetPixelFormat, 2);
335 OGL_PARAM(HDC, hdc);
336 OGL_PARAM(int, iPixelFormat);
337
338 Log(("DrvSetPixelFormat %x %d\n", hdc, iPixelFormat));
339 rc = DescribePixelFormat(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iPixelFormat, sizeof(pfd), &pfd);
340 if (rc)
341 {
342 pClient->lastretval = SetPixelFormat(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iPixelFormat, &pfd);
343 }
344 else
345 {
346 Log(("DescribePixelFormat %d failed with 0 (%d)\n", iPixelFormat, GetLastError()));
347 pClient->lastretval = 0;
348 }
349}
350
351void vboxglDrvSwapBuffers(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
352{
353 OGL_CMD(DrvSwapBuffers, 1);
354 OGL_PARAM(HDC, hdc);
355
356 Log(("DrvSwapBuffers %x\n", hdc));
357 pClient->lastretval = SwapBuffers(VBOX_OGL_GUEST_TO_HOST_HDC(hdc));
358 if (!pClient->lastretval)
359 Log(("SwapBuffers failed with %d\n", GetLastError()));
360
361 /** @todo sync bitmap/screen contents */
362}
363
364void vboxglDrvDescribeLayerPlane(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
365{
366 PLAYERPLANEDESCRIPTOR plpd;
367
368 OGL_CMD(DrvDescribeLayerPlane, 4);
369 OGL_PARAM(HDC, hdc);
370 OGL_PARAM(int, iPixelFormat);
371 OGL_PARAM(int, iLayerPlane);
372 OGL_PARAM(UINT, nBytes);
373 Assert(pClient->cbLastParam == nBytes);
374 plpd = (PLAYERPLANEDESCRIPTOR)pClient->pLastParam;
375
376 Log(("DrvDescribeLayerPlane %x %d %d %d %x\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd));
377 pClient->lastretval = wglDescribeLayerPlane(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iPixelFormat, iLayerPlane, nBytes, plpd);
378 if (!pClient->lastretval)
379 Log(("wglDescribeLayerPlane failed with %d\n", GetLastError()));
380}
381
382void vboxglDrvSetLayerPaletteEntries(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
383{
384 OGL_CMD(DrvSetLayerPaletteEntries, 5);
385 OGL_PARAM(HDC, hdc);
386 OGL_PARAM(int, iLayerPlane);
387 OGL_PARAM(int, iStart);
388 OGL_PARAM(int, cEntries);
389 OGL_MEMPARAM(COLORREF, pcr);
390
391 Log(("DrvSetLayerPaletteEntries %x %d %d %d %x\n", hdc, iLayerPlane, iStart, cEntries, pcr));
392 pClient->lastretval = wglSetLayerPaletteEntries(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iLayerPlane, iStart, cEntries, pcr);
393 if (!pClient->lastretval)
394 Log(("wglSetLayerPaletteEntries failed with %d\n", GetLastError()));
395}
396
397void vboxglDrvGetLayerPaletteEntries(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
398{
399 COLORREF *pcr;
400
401 OGL_CMD(DrvGetLayerPaletteEntries, 4);
402 OGL_PARAM(HDC, hdc);
403 OGL_PARAM(int, iLayerPlane);
404 OGL_PARAM(int, iStart);
405 OGL_PARAM(int, cEntries);
406
407 Assert(pClient->cbLastParam == sizeof(COLORREF)*cEntries);
408 pcr = (COLORREF *)pClient->pLastParam;
409
410 Log(("DrvGetLayerPaletteEntries %x %d %d %d %x\n", hdc, iLayerPlane, iStart, cEntries, pcr));
411 pClient->lastretval = wglGetLayerPaletteEntries(VBOX_OGL_GUEST_TO_HOST_HDC(hdc), iLayerPlane, iStart, cEntries, pcr);
412 if (!pClient->lastretval)
413 Log(("wglGetLayerPaletteEntries failed with %d\n", GetLastError()));
414}
415
416void vboxglDrvDescribePixelFormat(VBOXOGLCTX *pClient, uint8_t *pCmdBuffer)
417{
418 LPPIXELFORMATDESCRIPTOR ppfd;
419
420 OGL_CMD(DrvDescribePixelFormat, 3);
421 OGL_PARAM(HDC, hdc);
422 OGL_PARAM(int, iPixelFormat);
423 OGL_PARAM(UINT, nBytes);
424 Assert(pClient->cbLastParam == nBytes);
425 ppfd = (LPPIXELFORMATDESCRIPTOR)pClient->pLastParam;
426
427 hdc = VBOX_OGL_GUEST_TO_HOST_HDC(hdc);
428 if (!hdc)
429 hdc = GetDC(0);
430
431 Log(("DrvDescribePixelFormat %x %d %d %x\n", hdc, iPixelFormat, nBytes, ppfd));
432 pClient->lastretval = DescribePixelFormat(hdc, iPixelFormat, nBytes, ppfd);
433 if (!pClient->lastretval)
434 Log(("DescribePixelFormat failed with %d\n", GetLastError()));
435
436 if (!VBOX_OGL_GUEST_TO_HOST_HDC(hdc))
437 ReleaseDC(0, pClient->hdc);
438}
Note: See TracBrowser for help on using the repository browser.

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