VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_wgl.c@ 43105

Last change on this file since 43105 was 41073, checked in by vboxsync, 13 years ago

crOpenGL: missing window possition state for wgl backend

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 46.2 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7
8#define WIN32_LEAN_AND_MEAN
9#include <windows.h>
10
11#include <stdlib.h>
12#include <stdio.h>
13#include <memory.h>
14
15#include "cr_environment.h"
16#include "cr_error.h"
17#include "cr_string.h"
18#include "renderspu.h"
19#include "cr_mem.h"
20
21
22/* IAT patcher stuff */
23#define RVA2PTR(_t, _base, _off) ((_t*)(((uint8_t*)(_base)) + (_off)))
24
25int renderspuIatPatcherGetImportAddress(HMODULE hModule, LPCSTR pszLib, LPCSTR pszName, void** ppAdr)
26{
27 PIMAGE_DOS_HEADER pDosHdr = (PIMAGE_DOS_HEADER)hModule;
28 PIMAGE_NT_HEADERS pNtHdr;
29 PIMAGE_IMPORT_DESCRIPTOR pImportDr;
30 DWORD rvaImport;
31
32 crDebug("searching entry %s from %s", pszName, pszLib);
33
34 *ppAdr = 0;
35
36 if (pDosHdr->e_magic != IMAGE_DOS_SIGNATURE)
37 {
38 crWarning("invalid dos signature");
39 return VERR_INVALID_HANDLE;
40 }
41 pNtHdr = RVA2PTR(IMAGE_NT_HEADERS, pDosHdr, pDosHdr->e_lfanew);
42 if (pNtHdr->Signature != IMAGE_NT_SIGNATURE)
43 {
44 crWarning("invalid nt signature");
45 return VERR_INVALID_HANDLE;
46 }
47 rvaImport = pNtHdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
48 if (!rvaImport)
49 {
50 crWarning("no imports found");
51 return VERR_NOT_FOUND;
52 }
53 pImportDr = RVA2PTR(IMAGE_IMPORT_DESCRIPTOR, pDosHdr, rvaImport);
54
55 for ( ;pImportDr->TimeDateStamp != 0 || pImportDr->Name != 0; ++pImportDr)
56 {
57 DWORD rvaINT, rvaIAT;
58 PIMAGE_THUNK_DATA pINT, pIAT;
59 LPCSTR pszLibCur = RVA2PTR(char, pDosHdr, pImportDr->Name);
60 if (stricmp(pszLibCur, pszLib))
61 continue;
62
63 /* got the necessary lib! */
64 crDebug("got info for lib");
65
66 rvaINT = pImportDr->OriginalFirstThunk;
67 rvaIAT = pImportDr->FirstThunk;
68
69 if (!rvaINT || !rvaIAT)
70 {
71 crWarning("either rvaINT(0x%x) or rvaIAT(0x%x) are NULL, nothing found!", rvaINT, rvaIAT);
72 return VERR_NOT_FOUND;
73 }
74
75 pINT = RVA2PTR(IMAGE_THUNK_DATA, pDosHdr, rvaINT);
76 pIAT = RVA2PTR(IMAGE_THUNK_DATA, pDosHdr, rvaIAT);
77
78 for ( ; pINT->u1.AddressOfData; ++pINT, ++pIAT)
79 {
80 PIMAGE_IMPORT_BY_NAME pIbn;
81
82 if (IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal))
83 continue;
84
85 pIbn = RVA2PTR(IMAGE_IMPORT_BY_NAME, pDosHdr, pINT->u1.AddressOfData);
86
87 if (stricmp(pszName, (char*)pIbn->Name))
88 continue;
89
90 *ppAdr = &pIAT->u1.Function;
91
92 crDebug("search succeeded!");
93 return VINF_SUCCESS;
94 }
95 }
96
97 crDebug("not found");
98 return VERR_NOT_FOUND;
99}
100
101int renderspuIatPatcherPatchEntry(void *pvEntry, void *pvValue, void **ppvOldVal)
102{
103 void **ppfn = (void**)pvEntry;
104 DWORD dwOldProtect = 0;
105
106 if (!VirtualProtect(pvEntry, sizeof (pvEntry), PAGE_READWRITE, &dwOldProtect))
107 {
108 crWarning("VirtualProtect 1 failed, %d", GetLastError());
109 return VERR_ACCESS_DENIED;
110 }
111
112 if (ppvOldVal)
113 *ppvOldVal = *ppfn;
114 *ppfn = pvValue;
115
116 if (!VirtualProtect(pvEntry, sizeof (pvEntry), dwOldProtect, &dwOldProtect))
117 {
118 crWarning("VirtualProtect 2 failed, %d.. ignoring", GetLastError());
119 }
120
121 return VINF_SUCCESS;
122}
123
124
125int renderspuIatPatcherPatchFunction(HMODULE hModule, LPCSTR pszLib, LPCSTR pszName, void* pfn)
126{
127 void* pAdr;
128 int rc = renderspuIatPatcherGetImportAddress(hModule, pszLib, pszName, &pAdr);
129 if (RT_FAILURE(rc))
130 {
131 crDebug("renderspuIatPatcherGetImportAddress failed, %d", rc);
132 return rc;
133 }
134
135 rc = renderspuIatPatcherPatchEntry(pAdr, pfn, NULL);
136 if (RT_FAILURE(rc))
137 {
138 crWarning("renderspuIatPatcherPatchEntry failed, %d", rc);
139 return rc;
140 }
141
142 return VINF_SUCCESS;
143}
144
145/* patch */
146static HWND __stdcall renderspuAtiQuirk_GetForegroundWindow()
147{
148 crDebug("renderspuAtiQuirk_GetForegroundWindow");
149 return NULL;
150}
151
152
153#define CRREG_MAXKEYNAME 8
154static int renderspuAtiQuirk_GetICDDriverList(char *pBuf, DWORD cbBuf, DWORD *pcbResult)
155{
156 static LPCSTR aValueNames[] = {"OpenGLVendorName", "OpenGLDriverName"};
157 char *pBufPos = pBuf;
158 DWORD cbBufRemain = cbBuf, cbTotal = 0;
159 HKEY hKey, hSubkey;
160 DWORD dwIndex = 0;
161 int i;
162 int rc = VINF_SUCCESS;
163 char NameBuf[CRREG_MAXKEYNAME];
164 LONG lRc;
165
166 if (pcbResult)
167 *pcbResult = 0;
168
169 lRc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
170 "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E968-E325-11CE-BFC1-08002BE10318}",
171 0, /* reserved*/
172 KEY_READ,
173 &hKey);
174 if (ERROR_SUCCESS != lRc)
175 {
176 crDebug("RegOpenKeyEx 1 failed, %d", lRc);
177 return VERR_OPEN_FAILED;
178 }
179
180 for ( ; ; ++dwIndex)
181 {
182 lRc = RegEnumKeyA(hKey, dwIndex, NameBuf, CRREG_MAXKEYNAME);
183 if (lRc == ERROR_NO_MORE_ITEMS)
184 break;
185 if (lRc == ERROR_MORE_DATA)
186 continue;
187 if (lRc != ERROR_SUCCESS)
188 {
189 crWarning("RegEnumKeyA failed, %d", lRc);
190 continue;
191 }
192
193 lRc = RegOpenKeyEx(hKey,
194 NameBuf,
195 0, /* reserved*/
196 KEY_READ,
197 &hSubkey);
198 if (ERROR_SUCCESS != lRc)
199 {
200 crDebug("RegOpenKeyEx 2 failed, %d", lRc);
201 RegCloseKey(hKey);
202 return VERR_OPEN_FAILED;
203 }
204
205 for (i = 0; i < RT_ELEMENTS(aValueNames); ++i)
206 {
207 DWORD cbCur = cbBufRemain;
208 DWORD type;
209 lRc = RegQueryValueExA(hSubkey, aValueNames[i], NULL, /* reserved*/
210 &type,
211 (PBYTE)pBufPos, &cbCur);
212 /* exclude second null termination */
213 --cbCur;
214
215 if (ERROR_MORE_DATA == lRc)
216 {
217 if (REG_MULTI_SZ != type)
218 {
219 crWarning("unexpected data type! %d", type);
220 continue;
221 }
222 rc = VERR_BUFFER_OVERFLOW;
223 pBufPos = NULL;
224 cbBufRemain = 0;
225 CRASSERT(cbCur > 0 && cbCur < UINT32_MAX/2);
226 cbTotal += cbCur;
227 continue;
228 }
229 if (ERROR_SUCCESS != lRc)
230 {
231 crDebug("RegQueryValueExA failed, %d", lRc);
232 continue;
233 }
234
235 if (REG_MULTI_SZ != type)
236 {
237 crWarning("unexpected data type! %d", type);
238 continue;
239 }
240
241 /* succeeded */
242 CRASSERT(cbCur > 0 && cbCur < UINT32_MAX/2);
243 pBufPos += cbCur;
244 cbBufRemain -= cbCur;
245 cbTotal += cbCur;
246 CRASSERT(cbBufRemain < UINT32_MAX/2);
247 }
248
249 RegCloseKey(hSubkey);
250 }
251
252 RegCloseKey(hKey);
253
254 if (cbTotal)
255 {
256 /* include second null termination */
257 CRASSERT(!pBufPos || pBufPos[0] == '\0');
258 ++cbTotal;
259 }
260
261 if (pcbResult)
262 *pcbResult = cbTotal;
263
264 return rc;
265}
266
267static int renderspuAtiQuirk_ApplyForModule(LPCSTR pszAtiDll)
268{
269 int rc;
270 HMODULE hAtiDll;
271
272 crDebug("renderspuAtiQuirk_ApplyForModule (%s)", pszAtiDll);
273
274 hAtiDll = GetModuleHandleA(pszAtiDll);
275 if (!hAtiDll)
276 {
277 crDebug("GetModuleHandle failed, %d", GetLastError());
278 return VERR_NOT_FOUND;
279 }
280
281 rc = renderspuIatPatcherPatchFunction(hAtiDll, "user32.dll", "GetForegroundWindow", (void*)renderspuAtiQuirk_GetForegroundWindow);
282 if (RT_FAILURE(rc))
283 {
284 crDebug("renderspuIatPatcherPatchFunction failed, %d", rc);
285 return rc;
286 }
287
288 crDebug("renderspuAtiQuirk_ApplyForModule SUCCEEDED!");
289 crInfo("ATI Fullscreen qwirk SUCCEEDED!");
290
291 return VINF_SUCCESS;
292}
293
294static LPCSTR renderspuRegMultiSzNextVal(LPCSTR pszBuf)
295{
296 pszBuf += strlen(pszBuf) + sizeof (pszBuf[0]);
297
298 if (pszBuf[0] == '\0')
299 return NULL;
300
301 return pszBuf;
302}
303
304static LPCSTR renderspuRegMultiSzCurVal(LPCSTR pszBuf)
305{
306 if (pszBuf[0] == '\0')
307 return NULL;
308
309 return pszBuf;
310}
311
312
313static int renderspuAtiQuirk_Apply()
314{
315 char aBuf[4096];
316 DWORD cbResult = 0;
317 LPCSTR pszVal;
318 int rc;
319
320 crDebug("renderspuAtiQuirk_Apply..");
321
322 rc = renderspuAtiQuirk_GetICDDriverList(aBuf, sizeof (aBuf), &cbResult);
323 if (RT_FAILURE(rc))
324 {
325 crDebug("renderspuAtiQuirk_GetICDDriverList failed, rc(%d)", rc);
326 return rc;
327 }
328
329 for (pszVal = renderspuRegMultiSzCurVal(aBuf);
330 pszVal;
331 pszVal = renderspuRegMultiSzNextVal(pszVal))
332 {
333 renderspuAtiQuirk_ApplyForModule(pszVal);
334 }
335
336 return VINF_SUCCESS;
337}
338
339static GLboolean renderspuAtiQuirk_Needed()
340{
341 const char * pszString = render_spu.ws.glGetString(GL_VENDOR);
342 if (pszString && strstr(pszString, "ATI"))
343 return GL_TRUE;
344 pszString = render_spu.ws.glGetString(GL_RENDERER);
345 if (pszString && strstr(pszString, "ATI"))
346 return GL_TRUE;
347 return GL_FALSE;
348}
349
350static void renderspuAtiQuirk_ChkApply()
351{
352 static GLboolean fChecked = GL_FALSE;
353 if (fChecked)
354 return;
355
356 fChecked = GL_TRUE;
357 if (!renderspuAtiQuirk_Needed())
358 return;
359
360 crInfo("This is an ATI card, taking care of fullscreen..");
361
362 /*
363 * ATI WDDM-based graphics have an issue with rendering fullscreen.
364 * See public tickets #9775 & #9267 .
365 * Namely ATI drivers check whether ogl window is foreground and fullscreen
366 * and if so - do D3DKMTSetDisplayMode for ogl surface,
367 * which prevented any other data from being displayed, no matter what.
368 *
369 * Here we check whether we're using an ATI card and if so, patch the ogl ICD driver's IAT
370 * to replace GetForegroundWindow reference with our renderspuAtiQuirk_GetForegroundWindow,
371 * which always returns NULL.
372 */
373 renderspuAtiQuirk_Apply();
374}
375
376#define WINDOW_NAME window->title
377
378static BOOL
379bSetupPixelFormat( HDC hdc, GLbitfield visAttribs );
380
381GLboolean renderspu_SystemInitVisual( VisualInfo *visual )
382{
383 if (visual->visAttribs & CR_PBUFFER_BIT) {
384 crWarning("Render SPU: PBuffers not support on Windows yet.");
385 }
386
387 /* In the windows world, we need a window before a context.
388 * Use the device_context as a marker to do just that */
389
390 return TRUE;
391}
392
393void renderspu_SystemDestroyWindow( WindowInfo *window )
394{
395 VBOX_RENDERSPU_DESTROY_WINDOW vrdw;
396
397 CRASSERT(window);
398
399 /*DestroyWindow( window->hWnd );*/
400
401 vrdw.hWnd = window->hWnd;
402
403 if (render_spu.dwWinThreadId)
404 {
405 PostThreadMessage(render_spu.dwWinThreadId, WM_VBOX_RENDERSPU_DESTROY_WINDOW, 0, (LPARAM) &vrdw);
406 WaitForSingleObject(render_spu.hWinThreadReadyEvent, INFINITE);
407 }
408 else
409 {
410 crError("Render SPU: window thread is not running");
411 }
412
413 window->hWnd = NULL;
414 window->visual = NULL;
415}
416
417static LONG WINAPI
418MainWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
419{
420 /* int w,h; */
421
422 switch ( uMsg ) {
423
424 case WM_SIZE:
425 /* w = LOWORD( lParam );
426 * h = HIWORD( lParam ); */
427
428 /* glViewport( 0, 0, w, h ); */
429#if 0
430 glViewport( -render_spu.mural_x, -render_spu.mural_y,
431 render_spu.mural_width, render_spu.mural_height );
432 glScissor( -render_spu.mural_x, -render_spu.mural_y,
433 render_spu.mural_width, render_spu.mural_height );
434#endif
435 break;
436
437 case WM_CLOSE:
438 crWarning( "Render SPU: caught WM_CLOSE -- quitting." );
439 exit( 0 );
440 break;
441
442 case WM_DESTROY:
443 crDebug("Render SPU: caught WM_DESTROY for our window %x", hWnd);
444 break;
445
446 case WM_NCHITTEST:
447 crDebug("WM_NCHITTEST");
448 return HTNOWHERE;
449 }
450
451 return DefWindowProc( hWnd, uMsg, wParam, lParam );
452}
453
454static BOOL
455bSetupPixelFormatEXT( HDC hdc, GLbitfield visAttribs)
456{
457 PIXELFORMATDESCRIPTOR ppfd;
458 int pixelFormat;
459 int attribList[100];
460 float fattribList[] = { 0.0, 0.0 };
461 int numFormats;
462 int i = 0;
463 BOOL vis;
464
465 CRASSERT(visAttribs & CR_RGB_BIT); /* anybody need color index */
466
467 crWarning("Render SPU: Using WGL_EXT_pixel_format to select visual ID.");
468
469 attribList[i++] = WGL_DRAW_TO_WINDOW_EXT;
470 attribList[i++] = GL_TRUE;
471 attribList[i++] = WGL_ACCELERATION_EXT;
472 attribList[i++] = WGL_FULL_ACCELERATION_EXT;
473 attribList[i++] = WGL_COLOR_BITS_EXT;
474 attribList[i++] = 24;
475 attribList[i++] = WGL_RED_BITS_EXT;
476 attribList[i++] = 1;
477 attribList[i++] = WGL_GREEN_BITS_EXT;
478 attribList[i++] = 1;
479 attribList[i++] = WGL_BLUE_BITS_EXT;
480 attribList[i++] = 1;
481
482 crWarning("Render SPU: Visual chosen is... RGB");
483
484 if (visAttribs & CR_ALPHA_BIT)
485 {
486 attribList[i++] = WGL_ALPHA_BITS_EXT;
487 attribList[i++] = 1;
488 crWarning("A");
489 }
490
491 crWarning(", ");
492
493 if (visAttribs & CR_DOUBLE_BIT) {
494 attribList[i++] = WGL_DOUBLE_BUFFER_EXT;
495 attribList[i++] = GL_TRUE;
496 crWarning("DB, ");
497 }
498
499 if (visAttribs & CR_STEREO_BIT) {
500 attribList[i++] = WGL_STEREO_EXT;
501 attribList[i++] = GL_TRUE;
502 crWarning("Stereo, ");
503 }
504
505 if (visAttribs & CR_DEPTH_BIT)
506 {
507 attribList[i++] = WGL_DEPTH_BITS_EXT;
508 attribList[i++] = 1;
509 crWarning("Z, ");
510 }
511
512 if (visAttribs & CR_STENCIL_BIT)
513 {
514 attribList[i++] = WGL_STENCIL_BITS_EXT;
515 attribList[i++] = 1;
516 crWarning("Stencil, ");
517 }
518
519 if (visAttribs & CR_ACCUM_BIT)
520 {
521 attribList[i++] = WGL_ACCUM_RED_BITS_EXT;
522 attribList[i++] = 1;
523 attribList[i++] = WGL_ACCUM_GREEN_BITS_EXT;
524 attribList[i++] = 1;
525 attribList[i++] = WGL_ACCUM_BLUE_BITS_EXT;
526 attribList[i++] = 1;
527 crWarning("Accum, ");
528 if (visAttribs & CR_ALPHA_BIT)
529 {
530 attribList[i++] = WGL_ACCUM_ALPHA_BITS_EXT;
531 attribList[i++] = 1;
532 crWarning("Accum Alpha, ");
533 }
534 }
535
536 if (visAttribs & CR_MULTISAMPLE_BIT)
537 {
538 attribList[i++] = WGL_SAMPLE_BUFFERS_EXT;
539 attribList[i++] = 1;
540 attribList[i++] = WGL_SAMPLES_EXT;
541 attribList[i++] = 4;
542 crWarning("Multisample, ");
543 }
544
545 crWarning("\n");
546
547 /* End the list */
548 attribList[i++] = 0;
549 attribList[i++] = 0;
550
551 vis = render_spu.ws.wglChoosePixelFormatEXT( hdc, attribList, fattribList, 1, &pixelFormat, &numFormats);
552
553 crDebug("Render SPU: wglChoosePixelFormatEXT (vis 0x%x, LastError 0x%x, pixelFormat 0x%x", vis, GetLastError(), pixelFormat);
554
555 render_spu.ws.wglSetPixelFormat( hdc, pixelFormat, &ppfd );
556
557 crDebug("Render SPU: wglSetPixelFormat (Last error 0x%x)", GetLastError());
558
559 return vis;
560}
561
562static BOOL
563bSetupPixelFormatNormal( HDC hdc, GLbitfield visAttribs )
564{
565 PIXELFORMATDESCRIPTOR pfd = {
566 sizeof(PIXELFORMATDESCRIPTOR), /* size of this pfd */
567 1, /* version number */
568 PFD_DRAW_TO_WINDOW | /* support window */
569 PFD_SUPPORT_OPENGL, /* support OpenGL */
570 PFD_TYPE_RGBA, /* RGBA type */
571 24, /* 24-bit color depth */
572 0, 0, 0, 0, 0, 0, /* color bits ignored */
573 0, /* no alpha buffer */
574 0, /* shift bit ignored */
575 0, /* no accumulation buffer */
576 0, 0, 0, 0, /* accum bits ignored */
577 0, /* set depth buffer */
578 0, /* set stencil buffer */
579 0, /* no auxiliary buffer */
580 PFD_MAIN_PLANE, /* main layer */
581 0, /* reserved */
582 0, 0, 0 /* layer masks ignored */
583 };
584 PIXELFORMATDESCRIPTOR *ppfd = &pfd;
585 char s[1000];
586 GLbitfield b = 0;
587 int pixelformat;
588
589 renderspuMakeVisString( visAttribs, s );
590
591 crDebug( "Render SPU: WGL wants these visual capabilities: %s", s);
592
593 /* These really come into play with sort-last configs */
594 if (visAttribs & CR_DEPTH_BIT)
595 ppfd->cDepthBits = 24;
596 if (visAttribs & CR_ACCUM_BIT)
597 ppfd->cAccumBits = 16;
598 if (visAttribs & CR_RGB_BIT)
599 ppfd->cColorBits = 24;
600 if (visAttribs & CR_STENCIL_BIT)
601 ppfd->cStencilBits = 8;
602 if (visAttribs & CR_ALPHA_BIT)
603 ppfd->cAlphaBits = 8;
604 if (visAttribs & CR_DOUBLE_BIT)
605 ppfd->dwFlags |= PFD_DOUBLEBUFFER;
606 if (visAttribs & CR_STEREO_BIT)
607 ppfd->dwFlags |= PFD_STEREO;
608
609 /*
610 * We call the wgl functions directly if the SPU was loaded
611 * by our faker library, otherwise we have to call the GDI
612 * versions.
613 */
614 if (crGetenv( "CR_WGL_DO_NOT_USE_GDI" ) != NULL)
615 {
616 pixelformat = render_spu.ws.wglChoosePixelFormat( hdc, ppfd );
617 /* doing this twice is normal Win32 magic */
618 pixelformat = render_spu.ws.wglChoosePixelFormat( hdc, ppfd );
619 if ( pixelformat == 0 )
620 {
621 crError( "render_spu.ws.wglChoosePixelFormat failed" );
622 }
623 if ( !render_spu.ws.wglSetPixelFormat( hdc, pixelformat, ppfd ) )
624 {
625 crError( "render_spu.ws.wglSetPixelFormat failed" );
626 }
627
628 render_spu.ws.wglDescribePixelFormat( hdc, pixelformat, sizeof(*ppfd), ppfd );
629 }
630 else
631 {
632 /* Okay, we were loaded manually. Call the GDI functions. */
633 pixelformat = ChoosePixelFormat( hdc, ppfd );
634 /* doing this twice is normal Win32 magic */
635 pixelformat = ChoosePixelFormat( hdc, ppfd );
636 if ( pixelformat == 0 )
637 {
638 crError( "ChoosePixelFormat failed" );
639 }
640 if ( !SetPixelFormat( hdc, pixelformat, ppfd ) )
641 {
642 crError( "SetPixelFormat failed (Error 0x%x)", GetLastError() );
643 }
644
645 DescribePixelFormat( hdc, pixelformat, sizeof(*ppfd), ppfd );
646 }
647
648
649 if (ppfd->cDepthBits > 0)
650 b |= CR_DEPTH_BIT;
651 if (ppfd->cAccumBits > 0)
652 b |= CR_ACCUM_BIT;
653 if (ppfd->cColorBits > 8)
654 b |= CR_RGB_BIT;
655 if (ppfd->cStencilBits > 0)
656 b |= CR_STENCIL_BIT;
657 if (ppfd->cAlphaBits > 0)
658 b |= CR_ALPHA_BIT;
659 if (ppfd->dwFlags & PFD_DOUBLEBUFFER)
660 b |= CR_DOUBLE_BIT;
661 if (ppfd->dwFlags & PFD_STEREO)
662 b |= CR_STEREO_BIT;
663
664 renderspuMakeVisString( b, s );
665
666 crDebug( "Render SPU: WGL chose these visual capabilities: %s", s);
667 return TRUE;
668}
669
670static BOOL
671bSetupPixelFormat( HDC hdc, GLbitfield visAttribs )
672{
673 /* According to http://www.opengl.org/resources/faq/technical/mswindows.htm
674 we shouldn't be using wgl functions to setup pixel formats unless we're loading ICD driver.
675 In particular, bSetupPixelFormatEXT bugs with Intel drivers.
676 */
677 return bSetupPixelFormatNormal(hdc, visAttribs);
678}
679
680GLboolean renderspu_SystemCreateWindow( VisualInfo *visual, GLboolean showIt, WindowInfo *window )
681{
682 HDESK desktop;
683 HINSTANCE hinstance;
684 WNDCLASS wc;
685 DWORD window_style;
686 int window_plus_caption_width;
687 int window_plus_caption_height;
688
689 window->visual = visual;
690 window->nativeWindow = 0;
691
692 if ( render_spu.use_L2 )
693 {
694 crWarning( "Going fullscreen because we think we're using Lightning-2." );
695 render_spu.fullscreen = 1;
696 }
697
698 /*
699 * Begin Windows / WGL code
700 */
701
702 hinstance = GetModuleHandle( NULL );
703 if (!hinstance)
704 {
705 crError( "Render SPU: Couldn't get a handle to my module." );
706 return GL_FALSE;
707 }
708 crDebug( "Render SPU: Got the module handle: 0x%x", hinstance );
709
710 /* If we were launched from a service, telnet, or rsh, we need to
711 * get the input desktop. */
712
713 desktop = OpenInputDesktop( 0, FALSE,
714 DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
715 DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL |
716 DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS |
717 DESKTOP_SWITCHDESKTOP | GENERIC_WRITE );
718
719 if ( !desktop )
720 {
721 crError( "Render SPU: Couldn't acquire input desktop" );
722 return GL_FALSE;
723 }
724 crDebug( "Render SPU: Got the desktop: 0x%x", desktop );
725
726 if ( !SetThreadDesktop( desktop ) )
727 {
728 /* If this function fails, it's probably because
729 * it's already been called (i.e., the render SPU
730 * is bolted to an application?) */
731
732 /*crError( "Couldn't set thread to input desktop" ); */
733 }
734 crDebug( "Render SPU: Set the thread desktop -- this might have failed." );
735
736 if ( !GetClassInfo(hinstance, WINDOW_NAME, &wc) )
737 {
738 wc.style = CS_OWNDC;
739 wc.lpfnWndProc = (WNDPROC) MainWndProc;
740 wc.cbClsExtra = 0;
741 wc.cbWndExtra = 0;
742 wc.hInstance = hinstance;
743 wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
744 wc.hCursor = LoadCursor( NULL, IDC_ARROW );
745 wc.hbrBackground = NULL;
746 wc.lpszMenuName = NULL;
747 wc.lpszClassName = WINDOW_NAME;
748
749 if ( !RegisterClass( &wc ) )
750 {
751 crError( "Render SPU: Couldn't register window class -- you're not trying "
752 "to do multi-pipe stuff on windows, are you?\n\nNote --"
753 "This error message is from 1997 and probably doesn't make"
754 "any sense any more, but it's nostalgic for Humper." );
755 return GL_FALSE;
756 }
757 crDebug( "Render SPU: Registered the class" );
758 }
759 crDebug( "Render SPU: Got the class information" );
760
761 /* Full screen window should be a popup (undecorated) window */
762#if 1
763 window_style = ( render_spu.fullscreen ? WS_POPUP : WS_CAPTION );
764#else
765 window_style = ( WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN );
766 window_style |= WS_SYSMENU;
767#endif
768
769 crDebug( "Render SPU: Fullscreen: %s", render_spu.fullscreen ? "yes" : "no");
770
771 if ( render_spu.fullscreen )
772 {
773#if 0
774
775 int smCxFixedFrame = GetSystemMetrics( SM_CXFIXEDFRAME );
776 int smCyFixedFrame = GetSystemMetrics( SM_CXFIXEDFRAME ) + 1;
777 int smCyCaption = GetSystemMetrics( SM_CYCAPTION );
778
779 window->width = GetSystemMetrics( SM_CXSCREEN ) ;
780 window->height = GetSystemMetrics( SM_CYSCREEN ) ;
781
782 crDebug( "Render SPU: Window Dims: %d, %d", window->width, window->height );
783
784 window->x = render_spu->defaultX - smCxFixedFrame - 1;
785 window->y = render_spu->defaultY - smCyFixedFrame - smCyCaption;
786
787 window_plus_caption_width = window->width + 2 * smCxFixedFrame;
788 window_plus_caption_height = window->height + 2 * smCyFixedFrame + smCyCaption;
789
790#else
791 /* Since it's undecorated, we don't have to do anything fancy
792 * with these parameters. */
793
794 window->width = GetSystemMetrics( SM_CXSCREEN ) ;
795 window->height = GetSystemMetrics( SM_CYSCREEN ) ;
796 window->x = 0;
797 window->y = 0;
798 window_plus_caption_width = window->width;
799 window_plus_caption_height = window->height;
800
801#endif
802 }
803 else
804 {
805 /* CreateWindow takes the size of the entire window, so we add
806 * in the size necessary for the frame and the caption. */
807
808 int smCxFixedFrame, smCyFixedFrame, smCyCaption;
809 smCxFixedFrame = GetSystemMetrics( SM_CXFIXEDFRAME );
810 crDebug( "Render SPU: Got the X fixed frame" );
811 smCyFixedFrame = GetSystemMetrics( SM_CYFIXEDFRAME );
812 crDebug( "Render SPU: Got the Y fixed frame" );
813 smCyCaption = GetSystemMetrics( SM_CYCAPTION );
814 crDebug( "Render SPU: Got the Caption " );
815
816 window_plus_caption_width = window->width + 2 * smCxFixedFrame;
817 window_plus_caption_height = window->height + 2 * smCyFixedFrame + smCyCaption;
818
819 window->x = render_spu.defaultX - smCxFixedFrame;
820 window->y = render_spu.defaultY - smCyFixedFrame - smCyCaption;
821 }
822
823 crDebug( "Render SPU: Creating the window: (%d,%d), (%d,%d)", render_spu.defaultX, render_spu.defaultY, window_plus_caption_width, window_plus_caption_height );
824 window->hWnd = CreateWindow( WINDOW_NAME, WINDOW_NAME,
825 window_style,
826 window->x, window->y,
827 window_plus_caption_width,
828 window_plus_caption_height,
829 NULL, NULL, hinstance, &render_spu );
830
831 if ( !window->hWnd )
832 {
833 crError( "Render SPU: Create Window failed! That's almost certainly terrible." );
834 return GL_FALSE;
835 }
836
837 if (showIt) {
838 /* NO ERROR CODE FOR SHOWWINDOW */
839 crDebug( "Render SPU: Showing the window" );
840 crDebug("renderspu_SystemCreateWindow: showwindow: %x", window->hWnd);
841 ShowWindow( window->hWnd, SW_SHOWNORMAL );
842 }
843
844 SetForegroundWindow( window->hWnd );
845
846 SetWindowPos( window->hWnd, HWND_TOP, window->x, window->y,
847 window_plus_caption_width, window_plus_caption_height,
848 ( render_spu.fullscreen ? (SWP_SHOWWINDOW |
849 SWP_NOSENDCHANGING |
850 SWP_NOREDRAW |
851 SWP_NOACTIVATE ) :
852 0 ) );
853
854 if ( render_spu.fullscreen )
855 ShowCursor( FALSE );
856
857 window->device_context = GetDC( window->hWnd );
858
859 crDebug( "Render SPU: Got the DC: 0x%x", window->device_context );
860
861 if ( !bSetupPixelFormat( window->device_context, visual->visAttribs ) )
862 {
863 crError( "Render SPU: Couldn't set up the device context! Yikes!" );
864 return GL_FALSE;
865 }
866
867 return GL_TRUE;
868}
869
870GLboolean renderspu_SystemVBoxCreateWindow( VisualInfo *visual, GLboolean showIt, WindowInfo *window )
871{
872#if 0
873 HDESK desktop;
874#endif
875 HINSTANCE hinstance;
876 WNDCLASS wc;
877 DWORD window_style;
878 int window_plus_caption_width;
879 int window_plus_caption_height;
880
881 window->visual = visual;
882 window->nativeWindow = 0;
883
884 if ( render_spu.use_L2 )
885 {
886 crWarning( "Going fullscreen because we think we're using Lightning-2." );
887 render_spu.fullscreen = 1;
888 }
889
890 /*
891 * Begin Windows / WGL code
892 */
893
894 hinstance = GetModuleHandle( NULL );
895 if (!hinstance)
896 {
897 crError( "Render SPU: Couldn't get a handle to my module." );
898 return GL_FALSE;
899 }
900 crDebug( "Render SPU: Got the module handle: 0x%x", hinstance );
901
902#if 0
903 /* If we were launched from a service, telnet, or rsh, we need to
904 * get the input desktop. */
905
906 desktop = OpenInputDesktop( 0, FALSE,
907 DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
908 DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL |
909 DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS |
910 DESKTOP_SWITCHDESKTOP | GENERIC_WRITE );
911
912 if ( !desktop )
913 {
914 crError( "Render SPU: Couldn't acquire input desktop" );
915 return GL_FALSE;
916 }
917 crDebug( "Render SPU: Got the desktop: 0x%x", desktop );
918
919 if ( !SetThreadDesktop( desktop ) )
920 {
921 /* If this function fails, it's probably because
922 * it's already been called (i.e., the render SPU
923 * is bolted to an application?) */
924
925 /*crError( "Couldn't set thread to input desktop" ); */
926 }
927 crDebug( "Render SPU: Set the thread desktop -- this might have failed." );
928#endif
929
930 if ( !GetClassInfo(hinstance, WINDOW_NAME, &wc) )
931 {
932 wc.style = CS_OWNDC; // | CS_PARENTDC;
933 wc.lpfnWndProc = (WNDPROC) MainWndProc;
934 wc.cbClsExtra = 0;
935 wc.cbWndExtra = 0;
936 wc.hInstance = hinstance;
937 wc.hIcon = NULL; //LoadIcon( NULL, IDI_APPLICATION );
938 wc.hCursor = NULL; //LoadCursor( NULL, IDC_ARROW );
939 wc.hbrBackground = NULL;
940 wc.lpszMenuName = NULL;
941 wc.lpszClassName = WINDOW_NAME;
942
943 if ( !RegisterClass( &wc ) )
944 {
945 crError( "Render SPU: Couldn't register window class -- you're not trying "
946 "to do multi-pipe stuff on windows, are you?\n\nNote --"
947 "This error message is from 1997 and probably doesn't make"
948 "any sense any more, but it's nostalgic for Humper." );
949 return GL_FALSE;
950 }
951 crDebug( "Render SPU: Registered the class" );
952 }
953 crDebug( "Render SPU: Got the class information" );
954
955 /* Full screen window should be a popup (undecorated) window */
956#if 1
957 window_style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED;
958 if (render_spu_parent_window_id)
959 {
960 window_style |= WS_CHILD;
961 }
962#else
963 window_style = ( WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN );
964 window_style |= WS_SYSMENU;
965#endif
966
967 crDebug( "Render SPU: Fullscreen: %s", render_spu.fullscreen ? "yes" : "no");
968
969 if ( render_spu.fullscreen )
970 {
971#if 0
972
973 int smCxFixedFrame = GetSystemMetrics( SM_CXFIXEDFRAME );
974 int smCyFixedFrame = GetSystemMetrics( SM_CXFIXEDFRAME ) + 1;
975 int smCyCaption = GetSystemMetrics( SM_CYCAPTION );
976
977 window->width = GetSystemMetrics( SM_CXSCREEN ) ;
978 window->height = GetSystemMetrics( SM_CYSCREEN ) ;
979
980 crDebug( "Render SPU: Window Dims: %d, %d", window->width, window->height );
981
982 window->x = render_spu->defaultX - smCxFixedFrame - 1;
983 window->y = render_spu->defaultY - smCyFixedFrame - smCyCaption;
984
985 window_plus_caption_width = window->width + 2 * smCxFixedFrame;
986 window_plus_caption_height = window->height + 2 * smCyFixedFrame + smCyCaption;
987
988#else
989 /* Since it's undecorated, we don't have to do anything fancy
990 * with these parameters. */
991
992 window->width = GetSystemMetrics( SM_CXSCREEN ) ;
993 window->height = GetSystemMetrics( SM_CYSCREEN ) ;
994 window->x = 0;
995 window->y = 0;
996 window_plus_caption_width = window->width;
997 window_plus_caption_height = window->height;
998
999#endif
1000 }
1001 else
1002 {
1003 /* CreateWindow takes the size of the entire window, so we add
1004 * in the size necessary for the frame and the caption. */
1005 int smCxFixedFrame, smCyFixedFrame, smCyCaption;
1006 smCxFixedFrame = GetSystemMetrics( SM_CXFIXEDFRAME );
1007 crDebug( "Render SPU: Got the X fixed frame" );
1008 smCyFixedFrame = GetSystemMetrics( SM_CYFIXEDFRAME );
1009 crDebug( "Render SPU: Got the Y fixed frame" );
1010 smCyCaption = GetSystemMetrics( SM_CYCAPTION );
1011 crDebug( "Render SPU: Got the Caption " );
1012
1013 window_plus_caption_width = window->width + 2 * smCxFixedFrame;
1014 window_plus_caption_height = window->height + 2 * smCyFixedFrame + smCyCaption;
1015
1016 window->x = render_spu.defaultX;
1017 window->y = render_spu.defaultY;
1018 }
1019
1020 crDebug( "Render SPU: Creating the window: (%d,%d), (%d,%d)", render_spu.defaultX, render_spu.defaultY, window_plus_caption_width, window_plus_caption_height );
1021 /*window->hWnd = CreateWindowEx( WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY,
1022 WINDOW_NAME, WINDOW_NAME,
1023 window_style,
1024 window->x, window->y,
1025 window->width,
1026 window->height,
1027 (void*) render_spu_parent_window_id, NULL, hinstance, &render_spu );*/
1028 {
1029 CREATESTRUCT cs;
1030
1031 cs.lpCreateParams = &window->hWnd;
1032
1033 cs.dwExStyle = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY;
1034 cs.lpszName = WINDOW_NAME;
1035 cs.lpszClass = WINDOW_NAME;
1036 cs.style = window_style;
1037 cs.x = window->x;
1038 cs.y = window->y;
1039 cs.cx = window->width;
1040 cs.cy = window->height;
1041 cs.hwndParent = (void*) render_spu_parent_window_id;
1042 cs.hMenu = NULL;
1043 cs.hInstance = hinstance;
1044
1045 if (render_spu.dwWinThreadId)
1046 {
1047 DWORD res;
1048 int cnt=0;
1049
1050 if (!PostThreadMessage(render_spu.dwWinThreadId, WM_VBOX_RENDERSPU_CREATE_WINDOW, 0, (LPARAM) &cs))
1051 {
1052 crError("Render SPU: PostThreadMessage failed with %i", GetLastError());
1053 return GL_FALSE;
1054 }
1055
1056 do
1057 {
1058 res = WaitForSingleObject(render_spu.hWinThreadReadyEvent, 1000);
1059 cnt++;
1060 }
1061 while ((res!=WAIT_OBJECT_0) && (cnt<10));
1062
1063 crDebug("Render SPU: window thread waited %i secs", cnt);
1064
1065 if (res!=WAIT_OBJECT_0)
1066 {
1067 crError("Render SPU: window thread not responded after %i tries", cnt);
1068 return GL_FALSE;
1069 }
1070 }
1071 else
1072 {
1073 crError("Render SPU: window thread is not running");
1074 return GL_FALSE;
1075 }
1076 }
1077
1078 if ( !window->hWnd )
1079 {
1080 crError( "Render SPU: Create Window failed! That's almost certainly terrible." );
1081 return GL_FALSE;
1082 }
1083
1084 if (showIt) {
1085 /* NO ERROR CODE FOR SHOWWINDOW */
1086 crDebug( "Render SPU: Showing the window" );
1087 crDebug("renderspu_SystemVBoxCreateWindow: showwindow: %x", window->hWnd);
1088 ShowWindow( window->hWnd, SW_SHOWNORMAL );
1089 }
1090
1091 //SetForegroundWindow( visual->hWnd );
1092
1093 SetWindowPos( window->hWnd, HWND_TOP, window->x, window->y,
1094 window->width, window->height,
1095 ( render_spu.fullscreen ?
1096 (SWP_SHOWWINDOW | SWP_NOSENDCHANGING | SWP_NOREDRAW | SWP_NOACTIVATE ) : SWP_NOACTIVATE
1097 ) );
1098 crDebug("Render SPU: SetWindowPos (%x, %d, %d, %d, %d)", window->hWnd,
1099 window->x, window->y, window->width, window->height);
1100
1101 if ( render_spu.fullscreen )
1102 ShowCursor( FALSE );
1103
1104 window->device_context = GetDC( window->hWnd );
1105
1106 crDebug( "Render SPU: Got the DC: 0x%x", window->device_context );
1107
1108 if ( !bSetupPixelFormat( window->device_context, visual->visAttribs ) )
1109 {
1110 crError( "Render SPU: Couldn't set up the device context! Yikes!" );
1111 return GL_FALSE;
1112 }
1113
1114 return GL_TRUE;
1115}
1116
1117
1118/* Either show or hide the render SPU's window. */
1119void renderspu_SystemShowWindow( WindowInfo *window, GLboolean showIt )
1120{
1121 if (showIt)
1122 {
1123 crDebug("SHOW renderspu_SystemShowWindow: %x", window->hWnd);
1124 ShowWindow( window->hWnd, SW_SHOWNORMAL );
1125 }
1126 else
1127 {
1128 crDebug("HIDE renderspu_SystemShowWindow: %x", window->hWnd);
1129 ShowWindow( window->hWnd, SW_HIDE );
1130 }
1131}
1132
1133GLboolean renderspu_SystemCreateContext( VisualInfo *visual, ContextInfo *context, ContextInfo *sharedContext )
1134{
1135 (void) sharedContext;
1136 context->visual = visual;
1137
1138 /* Found a visual, so we're o.k. to create the context now */
1139 if (0/*visual->device_context*/) {
1140
1141 //crDebug( "Render SPU: Using the DC: 0x%x", visual->device_context );
1142
1143 //context->hRC = render_spu.ws.wglCreateContext( visual->device_context );
1144 if (!context->hRC)
1145 {
1146 crError( "Render SPU: wglCreateContext failed (error 0x%x)", GetLastError() );
1147 return GL_FALSE;
1148 }
1149 } else {
1150 crDebug( "Render SPU: Delaying DC creation " );
1151 context->hRC = NULL; /* create it later in makecurrent */
1152 }
1153
1154
1155 return GL_TRUE;
1156}
1157
1158void renderspu_SystemDestroyContext( ContextInfo *context )
1159{
1160 render_spu.ws.wglDeleteContext( context->hRC );
1161 context->hRC = NULL;
1162}
1163
1164static GLboolean renderspuChkActivateSharedContext(ContextInfo *sharedContext)
1165{
1166 GLint crWindow;
1167 WindowInfo *window;
1168
1169 if (sharedContext->hRC)
1170 return GL_TRUE;
1171
1172 CRASSERT(sharedContext->id);
1173
1174 if (sharedContext->shared)
1175 renderspuChkActivateSharedContext(sharedContext->shared);
1176
1177 crWindow = renderspuWindowCreate(sharedContext->visual->displayName, sharedContext->visual->visAttribs);
1178 if (!crWindow)
1179 {
1180 crError("renderspuChkActivateSharedContext: renderspuWindowCreate failed!");
1181 return GL_FALSE;
1182 }
1183
1184 window = (WindowInfo *) crHashtableSearch(render_spu.windowTable, crWindow);
1185 if (!window)
1186 {
1187 crError("renderspuChkActivateSharedContext: crHashtableSearch failed!");
1188 renderspuWindowDestroy(crWindow);
1189 return GL_FALSE;
1190 }
1191
1192 CRASSERT(window->device_context);
1193
1194 crDebug( "Render SPU: renderspuChkActivateSharedContext: made the DC: 0x%x", window->device_context );
1195
1196 sharedContext->hRC = render_spu.ws.wglCreateContext(window->device_context);
1197 if (!sharedContext->hRC)
1198 {
1199 crError( "Render SPU: (renderspuChkActivateSharedContext) Couldn't create the context for the window (error 0x%x)", GetLastError() );
1200 renderspuWindowDestroy(crWindow);
1201 return GL_FALSE;
1202 }
1203
1204 sharedContext->currentWindow = window;
1205
1206 return GL_TRUE;
1207}
1208
1209void renderspu_SystemMakeCurrent( WindowInfo *window, GLint nativeWindow, ContextInfo *context )
1210{
1211 CRASSERT(render_spu.ws.wglMakeCurrent);
1212
1213 if (context && window) {
1214 if (window->visual != context->visual) {
1215 /*
1216 * XXX have to revisit this issue!!!
1217 *
1218 * But for now we destroy the current window
1219 * and re-create it with the context's visual abilities
1220 */
1221
1222 /*@todo Chromium has no correct code to remove window ids and associated info from
1223 * various tables. This is hack which just hides the root case.
1224 */
1225 crDebug("Recreating window in renderspu_SystemMakeCurrent\n");
1226 renderspu_SystemDestroyWindow( window );
1227 renderspu_SystemVBoxCreateWindow( context->visual, window->visible, window );
1228 }
1229
1230 if (render_spu.render_to_app_window && nativeWindow)
1231 {
1232 /* The render_to_app_window option
1233 * is set and we've got a nativeWindow
1234 * handle, save the handle for
1235 * later calls to swapbuffers().
1236 *
1237 * NOTE: This doesn't work, except
1238 * for software driven Mesa.
1239 * We'd need to object link the
1240 * crappfaker and crserver to be able to share
1241 * the HDC values between processes.. FIXME!
1242 */
1243 if (context->shared)
1244 {
1245 /* first make sure we have shared context created */
1246 renderspuChkActivateSharedContext(context->shared);
1247 }
1248
1249 window->nativeWindow = (HDC) nativeWindow;
1250 if (context->hRC == 0) {
1251 context->hRC = render_spu.ws.wglCreateContext( window->nativeWindow );
1252 if (!context->hRC)
1253 {
1254 crError( "(MakeCurrent) Couldn't create the context for the window (error 0x%x)", GetLastError() );
1255 }
1256 }
1257
1258 if (context->shared
1259 && context->shared->hRC
1260 && context->hRC)
1261 {
1262 /* share lists */
1263 render_spu.ws.wglShareLists(context->shared->hRC, context->hRC);
1264 }
1265
1266 render_spu.ws.wglMakeCurrent( window->nativeWindow, context->hRC );
1267 }
1268 else
1269 {
1270 if (!context->hRC) {
1271 if (context->shared)
1272 {
1273 /* first make sure we have shared context created */
1274 renderspuChkActivateSharedContext(context->shared);
1275 }
1276
1277 context->hRC = render_spu.ws.wglCreateContext(window->device_context);
1278 if (!context->hRC)
1279 {
1280 crError( "Render SPU: (MakeCurrent) Couldn't create the context for the window (error 0x%x)", GetLastError() );
1281 }
1282
1283 if (context->shared
1284 && context->shared->hRC
1285 && context->hRC)
1286 {
1287 /* share lists */
1288 render_spu.ws.wglShareLists(context->shared->hRC, context->hRC);
1289 }
1290
1291 /*Requery ext function pointers, we skip dummy ctx as it should never be used with ext functions*/
1292 if (0 && context->id)
1293 {
1294 int numFuncs, i;
1295 SPUNamedFunctionTable ext_table[1000];
1296
1297
1298 crDebug("Default server ctx created, requerying ext functions");
1299 /*requery ext functions*/
1300 numFuncs = renderspuCreateFunctions(ext_table);
1301 numFuncs += crLoadOpenGLExtensions( &render_spu.ws, ext_table+numFuncs);
1302 CRASSERT(numFuncs < 1000);
1303
1304 /*change spu dispatch*/
1305 crSPUChangeDispatch(&render_spu.self, ext_table);
1306
1307
1308 /*cleanup temp table*/
1309 for (i=0; i<numFuncs; ++i)
1310 {
1311 if (ext_table[i].name) crFree(ext_table[i].name);
1312 }
1313 }
1314 }
1315
1316 /*crDebug("MakeCurrent 0x%x, 0x%x", window->device_context, context->hRC);*/
1317 if (!render_spu.ws.wglMakeCurrent(window->device_context, context->hRC))
1318 {
1319 DWORD err = GetLastError();
1320 crError("Render SPU: (MakeCurrent) failed to make 0x%x, 0x%x current with 0x%x error.", window->device_context, context->hRC, err);
1321 }
1322 }
1323
1324 renderspuAtiQuirk_ChkApply();
1325 }
1326 else {
1327 render_spu.ws.wglMakeCurrent( 0, 0 );
1328 }
1329}
1330
1331void renderspu_SystemWindowSize( WindowInfo *window, GLint w, GLint h )
1332{
1333 int winprop;
1334 CRASSERT(window);
1335 CRASSERT(window->visual);
1336 if ( render_spu.fullscreen )
1337 winprop = SWP_SHOWWINDOW | SWP_NOSENDCHANGING |
1338 SWP_NOREDRAW | SWP_NOACTIVATE;
1339 else
1340 winprop = SWP_NOACTIVATE | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_NOZORDER; //SWP_SHOWWINDOW;
1341
1342 /*SetWindowRgn(window->hWnd, NULL, false);*/
1343
1344 if (!SetWindowPos( window->hWnd, HWND_TOP,
1345 window->x, window->y, w, h, winprop )) {
1346 crWarning("!!!FAILED!!! Render SPU: SetWindowPos (%x, %d, %d, %d, %d)", window->hWnd, window->x, window->y, w, h);
1347 } else {
1348 crDebug("Render SPU: SetWindowSize (%x, %d, %d, %d, %d)", window->hWnd, window->x, window->y, w, h);
1349 }
1350 /* save the new size */
1351 window->width = w;
1352 window->height = h;
1353}
1354
1355
1356void renderspu_SystemGetWindowGeometry( WindowInfo *window, GLint *x, GLint *y, GLint *w, GLint *h )
1357{
1358 RECT rect;
1359
1360 CRASSERT(window);
1361 CRASSERT(window->visual);
1362
1363 GetClientRect( window->hWnd, &rect );
1364 *x = rect.left;
1365 *y = rect.top;
1366 *w = rect.right - rect.left;
1367 *h = rect.bottom - rect.top;
1368}
1369
1370
1371void renderspu_SystemGetMaxWindowSize( WindowInfo *window, GLint *w, GLint *h )
1372{
1373 /* XXX fix this */
1374 (void) window;
1375 *w = 1600;
1376 *h = 1200;
1377}
1378
1379
1380void renderspu_SystemWindowPosition( WindowInfo *window, GLint x, GLint y )
1381{
1382 int winprop;
1383 CRASSERT(window);
1384 CRASSERT(window->visual);
1385 if ( render_spu.fullscreen )
1386 winprop = SWP_SHOWWINDOW | SWP_NOSENDCHANGING |
1387 SWP_NOREDRAW | SWP_NOACTIVATE;
1388 else
1389 winprop = SWP_NOACTIVATE | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_NOZORDER; //SWP_SHOWWINDOW;
1390
1391 /*SetWindowRgn(window->visual->hWnd, NULL, false);*/
1392
1393 if (!SetWindowPos( window->hWnd, HWND_TOP,
1394 x, y, window->width, window->height, winprop )) {
1395 crWarning("!!!FAILED!!! Render SPU: SetWindowPos (%x, %d, %d, %d, %d)", window->hWnd, x, y, window->width, window->height);
1396 } else {
1397 crDebug("Render SPU: SetWindowPos (%x, %d, %d, %d, %d)", window->hWnd,
1398 x, y, window->width, window->height);
1399 }
1400 /* save the new position */
1401 window->x = x;
1402 window->y = y;
1403}
1404
1405void renderspu_SystemWindowVisibleRegion(WindowInfo *window, GLint cRects, GLint* pRects)
1406{
1407 GLint i;
1408 HRGN hRgn, hTmpRgn;
1409
1410 CRASSERT(window);
1411 CRASSERT(window->visual);
1412
1413 hRgn = CreateRectRgn(0, 0, 0, 0);
1414
1415 for (i=0; i<cRects; i++)
1416 {
1417 hTmpRgn = CreateRectRgn(pRects[4*i], pRects[4*i+1], pRects[4*i+2], pRects[4*i+3]);
1418 CombineRgn(hRgn, hRgn, hTmpRgn, RGN_OR);
1419 DeleteObject(hTmpRgn);
1420 }
1421
1422 SetWindowRgn(window->hWnd, hRgn, true);
1423 crDebug("Render SPU: SetWindowRgn (%x, cRects=%i)", window->hWnd, cRects);
1424}
1425
1426static void renderspuHandleWindowMessages( HWND hWnd )
1427{
1428 MSG msg;
1429 while ( PeekMessage( &msg, hWnd, 0, 0xffffffff, PM_REMOVE ) )
1430 {
1431 TranslateMessage( &msg );
1432 DispatchMessage( &msg );
1433 }
1434
1435 //BringWindowToTop( hWnd );
1436}
1437
1438void renderspu_SystemSwapBuffers( WindowInfo *w, GLint flags )
1439{
1440 int return_value;
1441
1442 /* peek at the windows message queue */
1443 renderspuHandleWindowMessages( w->hWnd );
1444
1445 /* render_to_app_window:
1446 * w->nativeWindow will only be non-zero if the
1447 * render_spu.render_to_app_window option is true and
1448 * MakeCurrent() recorded the nativeWindow handle in the WindowInfo
1449 * structure.
1450 */
1451 if (render_spu.render_to_app_window && w->nativeWindow) {
1452 return_value = render_spu.ws.wglSwapBuffers( w->nativeWindow );
1453 } else {
1454 /*
1455 HRGN hRgn1, hRgn2, hRgn3;
1456 HWND hWndParent;
1457 LONG ws;
1458
1459 hRgn1 = CreateRectRgn(0, 0, w->width, w->height);
1460 hRgn2 = CreateRectRgn(50, 50, 100, 100);
1461 hRgn3 = CreateRectRgn(0, 0, 0, 0);
1462 CombineRgn(hRgn3, hRgn1, hRgn2, RGN_DIFF);
1463 SetWindowRgn(w->visual->hWnd, hRgn3, true);
1464 DeleteObject(hRgn1);
1465 DeleteObject(hRgn2);
1466
1467 hWndParent = GetParent(w->visual->hWnd);
1468 ws = GetWindowLong(hWndParent, GWL_STYLE);
1469 ws &= ~WS_CLIPCHILDREN;
1470 SetWindowLong(hWndParent, GWL_STYLE, ws);
1471
1472 RECT rcClip;
1473
1474 rcClip.left = 50;
1475 rcClip.top = 50;
1476 rcClip.right = 100;
1477 rcClip.bottom = 100;
1478 ValidateRect(w->visual->hWnd, &rcClip);
1479
1480 return_value = GetClipBox(w->visual->device_context, &rcClip);
1481 crDebug("GetClipBox returned %d (NR=%d,SR=%d,CR=%d,ERR=%d)",
1482 return_value, NULLREGION, SIMPLEREGION, COMPLEXREGION, ERROR);
1483
1484 crDebug("rcClip(%d, %d, %d, %d)", rcClip.left, rcClip.top, rcClip.right, rcClip.bottom);
1485
1486 return_value = ExcludeClipRect(w->visual->device_context, 50, 50, 100, 100);
1487 crDebug("ExcludeClipRect returned %d (NR=%d,SR=%d,CR=%d,ERR=%d)",
1488 return_value, NULLREGION, SIMPLEREGION, COMPLEXREGION, ERROR);
1489
1490 return_value = GetClipBox(w->visual->device_context, &rcClip);
1491 crDebug("GetClipBox returned %d (NR=%d,SR=%d,CR=%d,ERR=%d)",
1492 return_value, NULLREGION, SIMPLEREGION, COMPLEXREGION, ERROR);
1493 crDebug("rcClip(%d, %d, %d, %d)", rcClip.left, rcClip.top, rcClip.right, rcClip.bottom);
1494 */
1495 return_value = render_spu.ws.wglSwapBuffers( w->device_context );
1496 }
1497 if (!return_value)
1498 {
1499 /* GOD DAMN IT. The latest versions of the NVIDIA drivers
1500 * return failure from wglSwapBuffers, but it works just fine.
1501 * WHAT THE HELL?! */
1502
1503 crWarning( "wglSwapBuffers failed: return value of %d!", return_value);
1504 }
1505}
1506
1507void renderspu_SystemReparentWindow(WindowInfo *window)
1508{
1509 SetParent(window->hWnd, (HWND)render_spu_parent_window_id);
1510}
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