VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/wined3d/vboxext.c@ 49419

Last change on this file since 49419 was 46521, checked in by vboxsync, 12 years ago

wine/new: export

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.2 KB
Line 
1/* $Id: vboxext.c 46521 2013-06-13 10:44:58Z vboxsync $ */
2/** @file
3 *
4 * VBox extension to Wine D3D
5 *
6 * Copyright (C) 2011 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16#include "config.h"
17#include "wine/port.h"
18#include "wined3d_private.h"
19#include "vboxext.h"
20#ifdef VBOX_WITH_WDDM
21#include <VBox/VBoxCrHgsmi.h>
22#include <iprt/err.h>
23#endif
24
25WINE_DEFAULT_DEBUG_CHANNEL(d3d_vbox);
26
27typedef DECLCALLBACK(void) FNVBOXEXTWORKERCB(void *pvUser);
28typedef FNVBOXEXTWORKERCB *PFNVBOXEXTWORKERCB;
29
30HRESULT VBoxExtDwSubmitProcSync(PFNVBOXEXTWORKERCB pfnCb, void *pvCb);
31HRESULT VBoxExtDwSubmitProcAsync(PFNVBOXEXTWORKERCB pfnCb, void *pvCb);
32
33/*******************************/
34#ifdef VBOX_WITH_WDDM
35# if defined(VBOX_WDDM_WOW64)
36# define VBOXEXT_WINE_MODULE_NAME "wined3dwddm-x86.dll"
37# else
38# define VBOXEXT_WINE_MODULE_NAME "wined3dwddm.dll"
39# endif
40#else
41/* both 32bit and 64bit versions of xpdm wine libs are named identically */
42# define VBOXEXT_WINE_MODULE_NAME "wined3d.dll"
43#endif
44
45typedef struct VBOXEXT_WORKER
46{
47 CRITICAL_SECTION CritSect;
48
49 HANDLE hEvent;
50
51 HANDLE hThread;
52 DWORD idThread;
53 /* wine does not seem to guarantie the dll is not unloaded in case FreeLibrary is used
54 * while d3d object is not terminated, keep an extra reference to ensure we're not unloaded
55 * while we are active */
56 HMODULE hSelf;
57} VBOXEXT_WORKER, *PVBOXEXT_WORKER;
58
59
60
61HRESULT VBoxExtWorkerCreate(PVBOXEXT_WORKER pWorker);
62HRESULT VBoxExtWorkerDestroy(PVBOXEXT_WORKER pWorker);
63HRESULT VBoxExtWorkerSubmitProc(PVBOXEXT_WORKER pWorker, PFNVBOXEXTWORKERCB pfnCb, void *pvCb);
64
65
66/*******************************/
67typedef struct VBOXEXT_GLOBAL
68{
69 VBOXEXT_WORKER Worker;
70} VBOXEXT_GLOBAL, *PVBOXEXT_GLOBAL;
71
72static VBOXEXT_GLOBAL g_VBoxExtGlobal;
73
74#define WM_VBOXEXT_CALLPROC (WM_APP+1)
75#define WM_VBOXEXT_INIT_QUIT (WM_APP+2)
76
77typedef struct VBOXEXT_CALLPROC
78{
79 PFNVBOXEXTWORKERCB pfnCb;
80 void *pvCb;
81} VBOXEXT_CALLPROC, *PVBOXEXT_CALLPROC;
82
83static DWORD WINAPI vboxExtWorkerThread(void *pvUser)
84{
85 PVBOXEXT_WORKER pWorker = (PVBOXEXT_WORKER)pvUser;
86 MSG Msg;
87
88 PeekMessage(&Msg,
89 NULL /* HWND hWnd */,
90 WM_USER /* UINT wMsgFilterMin */,
91 WM_USER /* UINT wMsgFilterMax */,
92 PM_NOREMOVE);
93 SetEvent(pWorker->hEvent);
94
95 do
96 {
97 BOOL bResult = GetMessage(&Msg,
98 0 /*HWND hWnd*/,
99 0 /*UINT wMsgFilterMin*/,
100 0 /*UINT wMsgFilterMax*/
101 );
102
103 if(!bResult) /* WM_QUIT was posted */
104 break;
105
106 Assert(bResult != -1);
107 if(bResult == -1) /* error occurred */
108 break;
109
110 switch (Msg.message)
111 {
112 case WM_VBOXEXT_CALLPROC:
113 {
114 VBOXEXT_CALLPROC* pData = (VBOXEXT_CALLPROC*)Msg.lParam;
115 pData->pfnCb(pData->pvCb);
116 SetEvent(pWorker->hEvent);
117 break;
118 }
119 case WM_VBOXEXT_INIT_QUIT:
120 case WM_CLOSE:
121 {
122 PostQuitMessage(0);
123 break;
124 }
125 default:
126 TranslateMessage(&Msg);
127 DispatchMessage(&Msg);
128 }
129 } while (1);
130 return 0;
131}
132
133HRESULT VBoxExtWorkerCreate(PVBOXEXT_WORKER pWorker)
134{
135 if(!GetModuleHandleEx(0, VBOXEXT_WINE_MODULE_NAME, &pWorker->hSelf))
136 {
137 DWORD dwEr = GetLastError();
138 ERR("GetModuleHandleEx failed, %d", dwEr);
139 return E_FAIL;
140 }
141
142 InitializeCriticalSection(&pWorker->CritSect);
143 pWorker->hEvent = CreateEvent(NULL, /* LPSECURITY_ATTRIBUTES lpEventAttributes */
144 FALSE, /* BOOL bManualReset */
145 FALSE, /* BOOL bInitialState */
146 NULL /* LPCTSTR lpName */
147 );
148 if (pWorker->hEvent)
149 {
150 pWorker->hThread = CreateThread(
151 NULL /* LPSECURITY_ATTRIBUTES lpThreadAttributes */,
152 0 /* SIZE_T dwStackSize */,
153 vboxExtWorkerThread,
154 pWorker,
155 0 /* DWORD dwCreationFlags */,
156 &pWorker->idThread);
157 if (pWorker->hThread)
158 {
159 DWORD dwResult = WaitForSingleObject(pWorker->hEvent, INFINITE);
160 if (WAIT_OBJECT_0 == dwResult)
161 return S_OK;
162 ERR("WaitForSingleObject returned %d\n", dwResult);
163 }
164 else
165 {
166 DWORD winErr = GetLastError();
167 ERR("CreateThread failed, winErr = (%d)", winErr);
168 }
169
170 DeleteCriticalSection(&pWorker->CritSect);
171 }
172 else
173 {
174 DWORD winErr = GetLastError();
175 ERR("CreateEvent failed, winErr = (%d)", winErr);
176 }
177
178 FreeLibrary(pWorker->hSelf);
179
180 return E_FAIL;
181}
182
183HRESULT VBoxExtWorkerDestroy(PVBOXEXT_WORKER pWorker)
184{
185 BOOL bResult = PostThreadMessage(pWorker->idThread, WM_VBOXEXT_INIT_QUIT, 0, 0);
186 DWORD dwErr;
187 if (!bResult)
188 {
189 DWORD winErr = GetLastError();
190 ERR("PostThreadMessage failed, winErr = (%d)", winErr);
191 return E_FAIL;
192 }
193
194 dwErr = WaitForSingleObject(pWorker->hThread, INFINITE);
195 if (dwErr != WAIT_OBJECT_0)
196 {
197 ERR("WaitForSingleObject returned (%d)", dwErr);
198 return E_FAIL;
199 }
200
201 CloseHandle(pWorker->hEvent);
202 DeleteCriticalSection(&pWorker->CritSect);
203
204 FreeLibrary(pWorker->hSelf);
205
206 CloseHandle(pWorker->hThread);
207
208 return S_OK;
209}
210
211static HRESULT vboxExtWorkerSubmit(VBOXEXT_WORKER *pWorker, UINT Msg, LPARAM lParam, BOOL fSync)
212{
213 HRESULT hr = E_FAIL;
214 BOOL bResult;
215 /* need to serialize since vboxExtWorkerThread is using one pWorker->hEvent
216 * to signal job completion */
217 EnterCriticalSection(&pWorker->CritSect);
218 bResult = PostThreadMessage(pWorker->idThread, Msg, 0, lParam);
219 if (bResult)
220 {
221 if (fSync)
222 {
223 DWORD dwErr = WaitForSingleObject(pWorker->hEvent, INFINITE);
224 if (dwErr == WAIT_OBJECT_0)
225 {
226 hr = S_OK;
227 }
228 else
229 {
230 ERR("WaitForSingleObject returned (%d)", dwErr);
231 }
232 }
233 else
234 hr = S_OK;
235 }
236 else
237 {
238 DWORD winErr = GetLastError();
239 ERR("PostThreadMessage failed, winErr = (%d)", winErr);
240 return E_FAIL;
241 }
242 LeaveCriticalSection(&pWorker->CritSect);
243 return hr;
244}
245
246HRESULT VBoxExtWorkerSubmitProcSync(PVBOXEXT_WORKER pWorker, PFNVBOXEXTWORKERCB pfnCb, void *pvCb)
247{
248 VBOXEXT_CALLPROC Ctx;
249 Ctx.pfnCb = pfnCb;
250 Ctx.pvCb = pvCb;
251 return vboxExtWorkerSubmit(pWorker, WM_VBOXEXT_CALLPROC, (LPARAM)&Ctx, TRUE);
252}
253
254static DECLCALLBACK(void) vboxExtWorkerSubmitProcAsyncWorker(void *pvUser)
255{
256 PVBOXEXT_CALLPROC pCallInfo = (PVBOXEXT_CALLPROC)pvUser;
257 pCallInfo[1].pfnCb(pCallInfo[1].pvCb);
258 HeapFree(GetProcessHeap(), 0, pCallInfo);
259}
260
261HRESULT VBoxExtWorkerSubmitProcAsync(PVBOXEXT_WORKER pWorker, PFNVBOXEXTWORKERCB pfnCb, void *pvCb)
262{
263 HRESULT hr;
264 PVBOXEXT_CALLPROC pCallInfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof (VBOXEXT_CALLPROC) * 2);
265 if (!pCallInfo)
266 {
267 ERR("HeapAlloc failed\n");
268 return E_OUTOFMEMORY;
269 }
270 pCallInfo[0].pfnCb = vboxExtWorkerSubmitProcAsyncWorker;
271 pCallInfo[0].pvCb = pCallInfo;
272 pCallInfo[1].pfnCb = pfnCb;
273 pCallInfo[1].pvCb = pvCb;
274 hr = vboxExtWorkerSubmit(pWorker, WM_VBOXEXT_CALLPROC, (LPARAM)pCallInfo, FALSE);
275 if (FAILED(hr))
276 {
277 ERR("vboxExtWorkerSubmit failed, hr 0x%x\n", hr);
278 HeapFree(GetProcessHeap(), 0, pCallInfo);
279 return hr;
280 }
281 return S_OK;
282}
283
284
285static HRESULT vboxExtInit()
286{
287 HRESULT hr = S_OK;
288#ifdef VBOX_WITH_WDDM
289 int rc = VBoxCrHgsmiInit();
290 if (!RT_SUCCESS(rc))
291 {
292 ERR("VBoxCrHgsmiInit failed rc %d", rc);
293 return E_FAIL;
294 }
295#endif
296 memset(&g_VBoxExtGlobal, 0, sizeof (g_VBoxExtGlobal));
297 hr = VBoxExtWorkerCreate(&g_VBoxExtGlobal.Worker);
298 if (SUCCEEDED(hr))
299 return S_OK;
300
301 /* failure branch */
302#ifdef VBOX_WITH_WDDM
303 VBoxCrHgsmiTerm();
304#endif
305 return hr;
306}
307
308
309static HRESULT vboxExtWndCleanup();
310
311static HRESULT vboxExtTerm()
312{
313 HRESULT hr = vboxExtWndCleanup();
314 if (!SUCCEEDED(hr))
315 {
316 ERR("vboxExtWndCleanup failed, hr %d", hr);
317 return hr;
318 }
319
320 hr = VBoxExtWorkerDestroy(&g_VBoxExtGlobal.Worker);
321 if (!SUCCEEDED(hr))
322 {
323 ERR("VBoxExtWorkerDestroy failed, hr %d", hr);
324 return hr;
325 }
326
327#ifdef VBOX_WITH_WDDM
328 VBoxCrHgsmiTerm();
329#endif
330
331 return S_OK;
332}
333
334/* wine serializes all calls to us, so no need for any synchronization here */
335static DWORD g_cVBoxExtInits = 0;
336
337static DWORD vboxExtAddRef()
338{
339 return ++g_cVBoxExtInits;
340}
341
342static DWORD vboxExtRelease()
343{
344 DWORD cVBoxExtInits = --g_cVBoxExtInits;
345 Assert(cVBoxExtInits < UINT32_MAX/2);
346 return cVBoxExtInits;
347}
348
349static DWORD vboxExtGetRef()
350{
351 return g_cVBoxExtInits;
352}
353
354HRESULT VBoxExtCheckInit()
355{
356 HRESULT hr = S_OK;
357 if (!vboxExtGetRef())
358 {
359 hr = vboxExtInit();
360 if (FAILED(hr))
361 {
362 ERR("vboxExtInit failed, hr (0x%x)", hr);
363 return hr;
364 }
365 }
366 vboxExtAddRef();
367 return S_OK;
368}
369
370HRESULT VBoxExtCheckTerm()
371{
372 HRESULT hr = S_OK;
373 if (vboxExtGetRef() == 1)
374 {
375 hr = vboxExtTerm();
376 if (FAILED(hr))
377 {
378 ERR("vboxExtTerm failed, hr (0x%x)", hr);
379 return hr;
380 }
381 }
382 vboxExtRelease();
383 return S_OK;
384}
385
386HRESULT VBoxExtDwSubmitProcSync(PFNVBOXEXTWORKERCB pfnCb, void *pvCb)
387{
388 return VBoxExtWorkerSubmitProcSync(&g_VBoxExtGlobal.Worker, pfnCb, pvCb);
389}
390
391HRESULT VBoxExtDwSubmitProcAsync(PFNVBOXEXTWORKERCB pfnCb, void *pvCb)
392{
393 return VBoxExtWorkerSubmitProcAsync(&g_VBoxExtGlobal.Worker, pfnCb, pvCb);
394}
395
396#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
397# ifndef VBOX_WITH_WDDM
398typedef struct VBOXEXT_GETDC_CB
399{
400 HWND hWnd;
401 HDC hDC;
402} VBOXEXT_GETDC_CB, *PVBOXEXT_GETDC_CB;
403
404static DECLCALLBACK(void) vboxExtGetDCWorker(void *pvUser)
405{
406 PVBOXEXT_GETDC_CB pData = (PVBOXEXT_GETDC_CB)pvUser;
407 pData->hDC = GetDC(pData->hWnd);
408}
409
410typedef struct VBOXEXT_RELEASEDC_CB
411{
412 HWND hWnd;
413 HDC hDC;
414 int ret;
415} VBOXEXT_RELEASEDC_CB, *PVBOXEXT_RELEASEDC_CB;
416
417static DECLCALLBACK(void) vboxExtReleaseDCWorker(void *pvUser)
418{
419 PVBOXEXT_RELEASEDC_CB pData = (PVBOXEXT_RELEASEDC_CB)pvUser;
420 pData->ret = ReleaseDC(pData->hWnd, pData->hDC);
421}
422
423HDC VBoxExtGetDC(HWND hWnd)
424{
425 HRESULT hr;
426 VBOXEXT_GETDC_CB Data = {0};
427 Data.hWnd = hWnd;
428 Data.hDC = NULL;
429
430 hr = VBoxExtDwSubmitProcSync(vboxExtGetDCWorker, &Data);
431 if (FAILED(hr))
432 {
433 ERR("VBoxExtDwSubmitProcSync feiled, hr (0x%x)\n", hr);
434 return NULL;
435 }
436
437 return Data.hDC;
438}
439
440int VBoxExtReleaseDC(HWND hWnd, HDC hDC)
441{
442 HRESULT hr;
443 VBOXEXT_RELEASEDC_CB Data = {0};
444 Data.hWnd = hWnd;
445 Data.hDC = hDC;
446 Data.ret = 0;
447
448 hr = VBoxExtDwSubmitProcSync(vboxExtReleaseDCWorker, &Data);
449 if (FAILED(hr))
450 {
451 ERR("VBoxExtDwSubmitProcSync feiled, hr (0x%x)\n", hr);
452 return -1;
453 }
454
455 return Data.ret;
456}
457# endif /* #ifndef VBOX_WITH_WDDM */
458
459static DECLCALLBACK(void) vboxExtReleaseContextWorker(void *pvUser)
460{
461 struct wined3d_context *context = (struct wined3d_context *)pvUser;
462 VBoxTlsRefRelease(context);
463}
464
465void VBoxExtReleaseContextAsync(struct wined3d_context *context)
466{
467 HRESULT hr;
468
469 hr = VBoxExtDwSubmitProcAsync(vboxExtReleaseContextWorker, context);
470 if (FAILED(hr))
471 {
472 ERR("VBoxExtDwSubmitProcAsync feiled, hr (0x%x)\n", hr);
473 return;
474 }
475}
476
477#endif /* #if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT) */
478
479/* window creation API */
480static LRESULT CALLBACK vboxExtWndProc(HWND hwnd,
481 UINT uMsg,
482 WPARAM wParam,
483 LPARAM lParam
484)
485{
486 switch(uMsg)
487 {
488 case WM_CLOSE:
489 TRACE("got WM_CLOSE for hwnd(0x%x)", hwnd);
490 return 0;
491 case WM_DESTROY:
492 TRACE("got WM_DESTROY for hwnd(0x%x)", hwnd);
493 return 0;
494 case WM_NCHITTEST:
495 TRACE("got WM_NCHITTEST for hwnd(0x%x)\n", hwnd);
496 return HTNOWHERE;
497 }
498
499 return DefWindowProc(hwnd, uMsg, wParam, lParam);
500}
501
502#define VBOXEXTWND_NAME "VboxDispD3DWineWnd"
503
504static HRESULT vboxExtWndDoCleanup()
505{
506 HRESULT hr = S_OK;
507 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
508 WNDCLASS wc;
509 if (GetClassInfo(hInstance, VBOXEXTWND_NAME, &wc))
510 {
511 if (!UnregisterClass(VBOXEXTWND_NAME, hInstance))
512 {
513 DWORD winEr = GetLastError();
514 ERR("UnregisterClass failed, winErr(%d)\n", winEr);
515 hr = E_FAIL;
516 }
517 }
518 return hr;
519}
520
521static HRESULT vboxExtWndDoCreate(DWORD w, DWORD h, HWND *phWnd, HDC *phDC)
522{
523 HRESULT hr = S_OK;
524 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
525 /* Register the Window Class. */
526 WNDCLASS wc;
527 if (!GetClassInfo(hInstance, VBOXEXTWND_NAME, &wc))
528 {
529 wc.style = 0;//CS_OWNDC;
530 wc.lpfnWndProc = vboxExtWndProc;
531 wc.cbClsExtra = 0;
532 wc.cbWndExtra = 0;
533 wc.hInstance = hInstance;
534 wc.hIcon = NULL;
535 wc.hCursor = NULL;
536 wc.hbrBackground = NULL;
537 wc.lpszMenuName = NULL;
538 wc.lpszClassName = VBOXEXTWND_NAME;
539 if (!RegisterClass(&wc))
540 {
541 DWORD winErr = GetLastError();
542 ERR("RegisterClass failed, winErr(%d)\n", winErr);
543 hr = E_FAIL;
544 }
545 }
546
547 if (hr == S_OK)
548 {
549 HWND hWnd = CreateWindowEx (WS_EX_TOOLWINDOW,
550 VBOXEXTWND_NAME, VBOXEXTWND_NAME,
551 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
552 0, 0,
553 w, h,
554 NULL, //GetDesktopWindow() /* hWndParent */,
555 NULL /* hMenu */,
556 hInstance,
557 NULL /* lpParam */);
558 Assert(hWnd);
559 if (hWnd)
560 {
561 *phWnd = hWnd;
562 *phDC = GetDC(hWnd);
563 /* make sure we keep inited until the window is active */
564 vboxExtAddRef();
565 }
566 else
567 {
568 DWORD winErr = GetLastError();
569 ERR("CreateWindowEx failed, winErr(%d)\n", winErr);
570 hr = E_FAIL;
571 }
572 }
573
574 return hr;
575}
576
577static HRESULT vboxExtWndDoDestroy(HWND hWnd, HDC hDC)
578{
579 BOOL bResult;
580 DWORD winErr;
581 ReleaseDC(hWnd, hDC);
582 bResult = DestroyWindow(hWnd);
583 Assert(bResult);
584 if (bResult)
585 {
586 /* release the reference we previously acquired on window creation */
587 vboxExtRelease();
588 return S_OK;
589 }
590
591 winErr = GetLastError();
592 ERR("DestroyWindow failed, winErr(%d) for hWnd(0x%x)\n", winErr, hWnd);
593
594 return E_FAIL;
595}
596
597typedef struct VBOXEXTWND_CREATE_INFO
598{
599 int hr;
600 HWND hWnd;
601 HDC hDC;
602 DWORD width;
603 DWORD height;
604} VBOXEXTWND_CREATE_INFO;
605
606typedef struct VBOXEXTWND_DESTROY_INFO
607{
608 int hr;
609 HWND hWnd;
610 HDC hDC;
611} VBOXEXTWND_DESTROY_INFO;
612
613typedef struct VBOXEXTWND_CLEANUP_INFO
614{
615 int hr;
616} VBOXEXTWND_CLEANUP_INFO;
617
618static DECLCALLBACK(void) vboxExtWndDestroyWorker(void *pvUser)
619{
620 VBOXEXTWND_DESTROY_INFO *pInfo = (VBOXEXTWND_DESTROY_INFO*)pvUser;
621 pInfo->hr = vboxExtWndDoDestroy(pInfo->hWnd, pInfo->hDC);
622 Assert(pInfo->hr == S_OK);
623}
624
625static DECLCALLBACK(void) vboxExtWndCreateWorker(void *pvUser)
626{
627 VBOXEXTWND_CREATE_INFO *pInfo = (VBOXEXTWND_CREATE_INFO*)pvUser;
628 pInfo->hr = vboxExtWndDoCreate(pInfo->width, pInfo->height, &pInfo->hWnd, &pInfo->hDC);
629 Assert(pInfo->hr == S_OK);
630}
631
632static DECLCALLBACK(void) vboxExtWndCleanupWorker(void *pvUser)
633{
634 VBOXEXTWND_CLEANUP_INFO *pInfo = (VBOXEXTWND_CLEANUP_INFO*)pvUser;
635 pInfo-> hr = vboxExtWndDoCleanup();
636}
637
638HRESULT VBoxExtWndDestroy(HWND hWnd, HDC hDC)
639{
640 HRESULT hr;
641 VBOXEXTWND_DESTROY_INFO Info;
642 Info.hr = E_FAIL;
643 Info.hWnd = hWnd;
644 Info.hDC = hDC;
645 hr = VBoxExtDwSubmitProcSync(vboxExtWndDestroyWorker, &Info);
646 if (!SUCCEEDED(hr))
647 {
648 ERR("VBoxExtDwSubmitProcSync-vboxExtWndDestroyWorker failed hr %d", hr);
649 return hr;
650 }
651
652 if (!SUCCEEDED(Info.hr))
653 {
654 ERR("vboxExtWndDestroyWorker failed hr %d", Info.hr);
655 return Info.hr;
656 }
657
658 return S_OK;
659}
660
661HRESULT VBoxExtWndCreate(DWORD width, DWORD height, HWND *phWnd, HDC *phDC)
662{
663 HRESULT hr;
664 VBOXEXTWND_CREATE_INFO Info;
665 Info.hr = E_FAIL;
666 Info.width = width;
667 Info.height = height;
668 hr = VBoxExtDwSubmitProcSync(vboxExtWndCreateWorker, &Info);
669 if (!SUCCEEDED(hr))
670 {
671 ERR("VBoxExtDwSubmitProcSync-vboxExtWndCreateWorker failed hr %d", hr);
672 return hr;
673 }
674
675 Assert(Info.hr == S_OK);
676 if (!SUCCEEDED(Info.hr))
677 {
678 ERR("vboxExtWndCreateWorker failed hr %d", Info.hr);
679 return Info.hr;
680 }
681
682 *phWnd = Info.hWnd;
683 *phDC = Info.hDC;
684 return S_OK;
685}
686
687static HRESULT vboxExtWndCleanup()
688{
689 HRESULT hr;
690 VBOXEXTWND_CLEANUP_INFO Info;
691 Info.hr = E_FAIL;
692 hr = VBoxExtDwSubmitProcSync(vboxExtWndCleanupWorker, &Info);
693 if (!SUCCEEDED(hr))
694 {
695 ERR("VBoxExtDwSubmitProcSync-vboxExtWndCleanupWorker failed hr %d", hr);
696 return hr;
697 }
698
699 if (!SUCCEEDED(Info.hr))
700 {
701 ERR("vboxExtWndCleanupWorker failed hr %d", Info.hr);
702 return Info.hr;
703 }
704
705 return S_OK;
706}
707
708
709/* hash map impl */
710static void vboxExtHashInitEntries(PVBOXEXT_HASHMAP pMap)
711{
712 uint32_t i;
713 pMap->cEntries = 0;
714 for (i = 0; i < RT_ELEMENTS(pMap->aBuckets); ++i)
715 {
716 RTListInit(&pMap->aBuckets[i].EntryList);
717 }
718}
719
720void VBoxExtHashInit(PVBOXEXT_HASHMAP pMap, PFNVBOXEXT_HASHMAP_HASH pfnHash, PFNVBOXEXT_HASHMAP_EQUAL pfnEqual)
721{
722 pMap->pfnHash = pfnHash;
723 pMap->pfnEqual = pfnEqual;
724 vboxExtHashInitEntries(pMap);
725}
726
727static DECLINLINE(uint32_t) vboxExtHashIdx(uint32_t u32Hash)
728{
729 return u32Hash % VBOXEXT_HASHMAP_NUM_BUCKETS;
730}
731
732#define VBOXEXT_FOREACH_NODE(_pNode, _pList, _op) do { \
733 PRTLISTNODE _pNode; \
734 PRTLISTNODE __pNext; \
735 for (_pNode = (_pList)->pNext; \
736 _pNode != (_pList); \
737 _pNode = __pNext) \
738 { \
739 __pNext = _pNode->pNext; /* <- the _pNode should not be referenced after the _op */ \
740 _op \
741 } \
742 } while (0)
743
744DECLINLINE(PVBOXEXT_HASHMAP_ENTRY) vboxExtHashSearchEntry(PVBOXEXT_HASHMAP pMap, void *pvKey)
745{
746 uint32_t u32Hash = pMap->pfnHash(pvKey);
747 uint32_t u32HashIdx = vboxExtHashIdx(u32Hash);
748 PVBOXEXT_HASHMAP_BUCKET pBucket = &pMap->aBuckets[u32HashIdx];
749 PVBOXEXT_HASHMAP_ENTRY pEntry;
750 VBOXEXT_FOREACH_NODE(pNode, &pBucket->EntryList,
751 pEntry = RT_FROM_MEMBER(pNode, VBOXEXT_HASHMAP_ENTRY, ListNode);
752 if (pEntry->u32Hash != u32Hash)
753 continue;
754
755 if (!pMap->pfnEqual(pvKey, pEntry->pvKey))
756 continue;
757 return pEntry;
758 );
759 return NULL;
760}
761
762void* VBoxExtHashRemoveEntry(PVBOXEXT_HASHMAP pMap, PVBOXEXT_HASHMAP_ENTRY pEntry)
763{
764 RTListNodeRemove(&pEntry->ListNode);
765 --pMap->cEntries;
766 Assert(pMap->cEntries <= UINT32_MAX/2);
767 return pEntry->pvKey;
768}
769
770static void vboxExtHashPutEntry(PVBOXEXT_HASHMAP pMap, PVBOXEXT_HASHMAP_BUCKET pBucket, PVBOXEXT_HASHMAP_ENTRY pEntry)
771{
772 RTListNodeInsertAfter(&pBucket->EntryList, &pEntry->ListNode);
773 ++pMap->cEntries;
774}
775
776PVBOXEXT_HASHMAP_ENTRY VBoxExtHashRemove(PVBOXEXT_HASHMAP pMap, void *pvKey)
777{
778 PVBOXEXT_HASHMAP_ENTRY pEntry = vboxExtHashSearchEntry(pMap, pvKey);
779 if (!pEntry)
780 return NULL;
781
782 VBoxExtHashRemoveEntry(pMap, pEntry);
783 return pEntry;
784}
785
786PVBOXEXT_HASHMAP_ENTRY VBoxExtHashPut(PVBOXEXT_HASHMAP pMap, void *pvKey, PVBOXEXT_HASHMAP_ENTRY pEntry)
787{
788 PVBOXEXT_HASHMAP_ENTRY pOldEntry = VBoxExtHashRemove(pMap, pvKey);
789 uint32_t u32Hash = pMap->pfnHash(pvKey);
790 uint32_t u32HashIdx = vboxExtHashIdx(u32Hash);
791 pEntry->pvKey = pvKey;
792 pEntry->u32Hash = u32Hash;
793 vboxExtHashPutEntry(pMap, &pMap->aBuckets[u32HashIdx], pEntry);
794 return pOldEntry;
795}
796
797
798PVBOXEXT_HASHMAP_ENTRY VBoxExtHashGet(PVBOXEXT_HASHMAP pMap, void *pvKey)
799{
800 return vboxExtHashSearchEntry(pMap, pvKey);
801}
802
803void VBoxExtHashVisit(PVBOXEXT_HASHMAP pMap, PFNVBOXEXT_HASHMAP_VISITOR pfnVisitor, void *pvVisitor)
804{
805 uint32_t iBucket = 0, iEntry = 0;
806 uint32_t cEntries = pMap->cEntries;
807
808 if (!cEntries)
809 return;
810
811 for (; ; ++iBucket)
812 {
813 PVBOXEXT_HASHMAP_ENTRY pEntry;
814 PVBOXEXT_HASHMAP_BUCKET pBucket = &pMap->aBuckets[iBucket];
815 Assert(iBucket < RT_ELEMENTS(pMap->aBuckets));
816 VBOXEXT_FOREACH_NODE(pNode, &pBucket->EntryList,
817 pEntry = RT_FROM_MEMBER(pNode, VBOXEXT_HASHMAP_ENTRY, ListNode);
818 if (!pfnVisitor(pMap, pEntry->pvKey, pEntry, pvVisitor))
819 return;
820
821 if (++iEntry == cEntries)
822 return;
823 );
824 }
825
826 /* should not be here! */
827 AssertFailed();
828}
829
830void VBoxExtHashCleanup(PVBOXEXT_HASHMAP pMap, PFNVBOXEXT_HASHMAP_VISITOR pfnVisitor, void *pvVisitor)
831{
832 VBoxExtHashVisit(pMap, pfnVisitor, pvVisitor);
833 vboxExtHashInitEntries(pMap);
834}
835
836static DECLCALLBACK(bool) vboxExtCacheCleanupCb(struct VBOXEXT_HASHMAP *pMap, void *pvKey, struct VBOXEXT_HASHMAP_ENTRY *pValue, void *pvVisitor)
837{
838 PVBOXEXT_HASHCACHE pCache = VBOXEXT_HASHCACHE_FROM_MAP(pMap);
839 PVBOXEXT_HASHCACHE_ENTRY pCacheEntry = VBOXEXT_HASHCACHE_ENTRY_FROM_MAP(pValue);
840 pCache->pfnCleanupEntry(pvKey, pCacheEntry);
841 return TRUE;
842}
843
844void VBoxExtCacheCleanup(PVBOXEXT_HASHCACHE pCache)
845{
846 VBoxExtHashCleanup(&pCache->Map, vboxExtCacheCleanupCb, NULL);
847}
848
849#if defined(VBOXWINEDBG_SHADERS) || defined(VBOX_WINE_WITH_PROFILE)
850void vboxWDbgPrintF(char * szString, ...)
851{
852 char szBuffer[4096*2] = {0};
853 va_list pArgList;
854 va_start(pArgList, szString);
855 _vsnprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), szString, pArgList);
856 va_end(pArgList);
857
858 OutputDebugStringA(szBuffer);
859}
860#endif
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