~ubuntu-branches/ubuntu/natty/virtualbox-ose/natty

« back to all changes in this revision

Viewing changes to src/VBox/Additions/common/crOpenGL/load.c

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-30 23:27:25 UTC
  • mfrom: (0.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130232725-2ouajjd2ggdet0zd
Tags: 4.0.2-dfsg-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add Apport hook.
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Drop *-source packages.
* Drop ubuntu-01-fix-build-gcc45.patch, fixed upstream.
* Drop ubuntu-02-as-needed.patch, added to the Debian package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include <stdlib.h>
19
19
#include <string.h>
20
20
#include <signal.h>
 
21
#include <iprt/initterm.h>
 
22
#include <iprt/thread.h>
 
23
#include <iprt/err.h>
 
24
#include <iprt/asm.h>
21
25
#ifndef WINDOWS
22
 
#include <sys/types.h>
23
 
#include <unistd.h>
 
26
# include <sys/types.h>
 
27
# include <unistd.h>
24
28
#endif
25
29
#ifdef CHROMIUM_THREADSAFE
26
30
#include "cr_threads.h"
27
31
#endif
28
32
 
 
33
#ifdef VBOX_WITH_WDDM
 
34
#include <d3d9types.h>
 
35
#include <D3dumddi.h>
 
36
#include "../../WINNT/Graphics/Miniport/wddm/VBoxVideoIf.h"
 
37
#include "../../WINNT/Graphics/Display/wddm/vboxdispmp.h"
 
38
#endif
 
39
 
29
40
/**
30
41
 * If you change this, see the comments in tilesortspu_context.c
31
42
 */
79
90
static DrawBufferFunc_t origDrawBuffer;
80
91
static ScissorFunc_t origScissor;
81
92
 
82
 
static void stubCheckWindowState(WindowInfo *window)
 
93
static void stubCheckWindowState(WindowInfo *window, GLboolean bFlushOnChange)
83
94
{
84
95
    bool bForceUpdate = false;
 
96
    bool bChanged = false;
85
97
 
86
98
#ifdef WINDOWS
87
99
    /* @todo install hook and track for WM_DISPLAYCHANGE */
101
113
    }
102
114
#endif
103
115
 
104
 
    stubUpdateWindowGeometry(window, bForceUpdate);
 
116
    bChanged = stubUpdateWindowGeometry(window, bForceUpdate) || bForceUpdate;
105
117
 
106
118
#if defined(GLX) || defined (WINDOWS)
107
119
    if (stub.trackWindowVisibleRgn)
108
120
    {
109
 
        stubUpdateWindowVisibileRegions(window);
 
121
        bChanged = stubUpdateWindowVisibileRegions(window) || bChanged;
110
122
    }
111
123
#endif
112
124
 
116
128
            crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
117
129
            stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
118
130
            window->mapped = mapped;
 
131
            bChanged = true;
119
132
        }
120
133
    }
 
134
 
 
135
    if (bFlushOnChange && bChanged)
 
136
    {
 
137
        stub.spu->dispatch_table.Flush();
 
138
    }
121
139
}
122
140
 
123
141
static bool stubSystemWindowExist(WindowInfo *pWindow)
124
142
{
125
143
#ifdef WINDOWS
126
 
    if (!WindowFromDC(pWindow->drawable))
 
144
    if (pWindow->hWnd!=WindowFromDC(pWindow->drawable))
127
145
    {
128
146
        return false;
129
147
    }
131
149
    Window root;
132
150
    int x, y;
133
151
    unsigned int border, depth, w, h;
134
 
 
135
 
    if (!XGetGeometry(pWindow->dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
 
152
    Display *dpy;
 
153
 
 
154
    dpy = stubGetWindowDisplay(pWindow);
 
155
 
 
156
    XLOCK(dpy);
 
157
    if (!XGetGeometry(dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
136
158
    {
 
159
        XUNLOCK(dpy);
137
160
        return false;
138
161
    }
 
162
    XUNLOCK(dpy);
139
163
#endif
140
164
 
141
165
    return true;
163
187
        return;
164
188
    }
165
189
 
166
 
    stubCheckWindowState(pWindow);
 
190
    stubCheckWindowState(pWindow, GL_FALSE);
167
191
}
168
192
 
169
193
static void stubCheckWindowsState(void)
173
197
    if (!stub.currentContext)
174
198
        return;
175
199
 
176
 
    stubCheckWindowState(stub.currentContext->currentDrawable);
177
 
 
 
200
#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
 
201
    if (stub.bRunningUnderWDDM)
 
202
        return;
 
203
#endif
 
204
 
 
205
#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
 
206
    crLockMutex(&stub.mutex);
 
207
#endif
 
208
 
 
209
    stubCheckWindowState(stub.currentContext->currentDrawable, GL_TRUE);
178
210
    crHashtableWalk(stub.windowTable, stubCheckWindowsCB, stub.currentContext);
 
211
 
 
212
#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
 
213
    crUnlockMutex(&stub.mutex);
 
214
#endif
179
215
}
180
216
 
181
217
 
254
290
        origScissor = stub.spuDispatch.Scissor;
255
291
        stub.spuDispatch.Clear = trapClear;
256
292
        stub.spuDispatch.Viewport = trapViewport;
 
293
 
257
294
        if (stub.viewportHack)
258
295
            stub.spuDispatch.Scissor = trapScissor;
259
296
        /*stub.spuDispatch.SwapBuffers = trapSwapBuffers;
281
318
    stub_initialized = 0;
282
319
 
283
320
#ifdef WINDOWS
 
321
# ifndef CR_NEWWINTRACK
284
322
    stubUninstallWindowMessageHook();
 
323
# endif
 
324
#endif
 
325
 
 
326
#ifdef CR_NEWWINTRACK
 
327
    ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
285
328
#endif
286
329
  
287
330
    //delete all created contexts
298
341
    crUnloadOpenGL();
299
342
#endif
300
343
 
 
344
#ifndef WINDOWS
301
345
    crNetTearDown();
 
346
#endif
302
347
 
303
348
#ifdef GLX
304
349
    if (stub.xshmSI.shmid>=0)
312
357
    crFreeHashtable(stub.windowTable, crFree);
313
358
    crFreeHashtable(stub.contextTable, NULL);
314
359
 
315
 
    crMemset(&stub, 0, sizeof(stub) );
 
360
    crMemset(&stub, 0, sizeof(stub));
316
361
}
317
362
 
318
363
static void stubSPUSafeTearDown(void)
329
374
    crLockMutex(mutex);
330
375
#endif
331
376
    crDebug("stubSPUSafeTearDown");
332
 
    crNetTearDown();
 
377
 
333
378
#ifdef WINDOWS
 
379
# ifndef CR_NEWWINTRACK
334
380
    stubUninstallWindowMessageHook();
335
 
#endif
336
 
    crMemset(&stub, 0, sizeof(stub));
 
381
# endif
 
382
#endif
 
383
 
 
384
#if defined(CR_NEWWINTRACK)
 
385
    crUnlockMutex(mutex);
 
386
# if defined(WINDOWS)
 
387
    if (RTThreadGetState(stub.hSyncThread)!=RTTHREADSTATE_TERMINATED)
 
388
    {
 
389
        HANDLE hNative;
 
390
        DWORD ec=0;
 
391
 
 
392
        hNative = OpenThread(SYNCHRONIZE|THREAD_QUERY_INFORMATION|THREAD_TERMINATE,
 
393
                             false, RTThreadGetNative(stub.hSyncThread));
 
394
        if (!hNative)
 
395
        {
 
396
            crWarning("Failed to get handle for sync thread(%#x)", GetLastError());
 
397
        }
 
398
        else
 
399
        {
 
400
            crDebug("Got handle %p for thread %#x", hNative, RTThreadGetNative(stub.hSyncThread));
 
401
        }
 
402
 
 
403
        ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
 
404
 
 
405
        if (PostThreadMessage(RTThreadGetNative(stub.hSyncThread), WM_QUIT, 0, 0))
 
406
        {
 
407
            RTThreadWait(stub.hSyncThread, 1000, NULL);
 
408
 
 
409
            /*Same issue as on linux, RTThreadWait exits before system thread is terminated, which leads
 
410
             * to issues as our dll goes to be unloaded.
 
411
             *@todo 
 
412
             *We usually call this function from DllMain which seems to be holding some lock and thus we have to
 
413
             * kill thread via TerminateThread.
 
414
             */
 
415
            if (WaitForSingleObject(hNative, 100)==WAIT_TIMEOUT)
 
416
            {
 
417
                crDebug("Wait failed, terminating");
 
418
                if (!TerminateThread(hNative, 1))
 
419
                {
 
420
                    crDebug("TerminateThread failed");
 
421
                }
 
422
            }
 
423
            if (GetExitCodeThread(hNative, &ec))
 
424
            {
 
425
                crDebug("Thread %p exited with ec=%i", hNative, ec);
 
426
            }
 
427
            else
 
428
            {
 
429
                crDebug("GetExitCodeThread failed(%#x)", GetLastError());
 
430
            }
 
431
        }
 
432
        else
 
433
        {
 
434
            crDebug("Sync thread killed before DLL_PROCESS_DETACH");
 
435
        }
 
436
 
 
437
        if (hNative)
 
438
        {
 
439
            CloseHandle(hNative);
 
440
        }
 
441
    }
 
442
#else
 
443
    if (stub.hSyncThread!=NIL_RTTHREAD)
 
444
    {
 
445
        ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
 
446
        {
 
447
            /*RTThreadWait might return too early, which cause our code being unloaded while RT thread wrapper is still running*/
 
448
            int rc = pthread_join(RTThreadGetNative(stub.hSyncThread), NULL);
 
449
            if (!rc)
 
450
            {
 
451
                crDebug("pthread_join failed %i", rc);
 
452
            }
 
453
        }
 
454
    }
 
455
#endif
 
456
    crLockMutex(mutex);
 
457
#endif
 
458
 
 
459
#ifndef WINDOWS
 
460
    crNetTearDown();
 
461
#endif
 
462
 
337
463
#ifdef CHROMIUM_THREADSAFE
338
464
    crUnlockMutex(mutex);
339
465
    crFreeMutex(mutex);
340
466
#endif
 
467
    crMemset(&stub, 0, sizeof(stub));
341
468
}
342
469
 
343
470
 
393
520
 
394
521
    stub.windowTable = crAllocHashtable();
395
522
 
 
523
#ifdef CR_NEWWINTRACK
 
524
    stub.bShutdownSyncThread = false;
 
525
    stub.hSyncThread = NIL_RTTHREAD;
 
526
#endif
 
527
 
396
528
    defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
397
529
    defaultWin->type = CHROMIUM;
398
530
    defaultWin->spuWindow = 0;  /* window 0 always exists */
577
709
        char name[1000];
578
710
        int i;
579
711
 
 
712
# ifdef VBOX_WITH_WDDM
 
713
        stub.bRunningUnderWDDM = false;
 
714
# endif
580
715
        /* Apply viewport hack only if we're running under wine */
581
 
        if (NULL!=GetModuleHandle("wined3d.dll"))
 
716
        if (NULL!=GetModuleHandle("wined3d.dll") || NULL != GetModuleHandle("wined3dwddm.dll"))
582
717
        {
583
718
            crGetProcName(name, 1000);
584
719
            for (i=0; gsViewportHackApps[i]; ++i)
594
729
#endif
595
730
}
596
731
 
 
732
#ifdef CR_NEWWINTRACK
 
733
# ifdef VBOX_WITH_WDDM
 
734
static stubDispatchVisibleRegions(WindowInfo *pWindow)
 
735
{
 
736
    DWORD dwCount;
 
737
    LPRGNDATA lpRgnData;
 
738
 
 
739
    dwCount = GetRegionData(pWindow->hVisibleRegion, 0, NULL);
 
740
    lpRgnData = crAlloc(dwCount);
 
741
 
 
742
    if (lpRgnData)
 
743
    {
 
744
        GetRegionData(pWindow->hVisibleRegion, dwCount, lpRgnData);
 
745
        crDebug("Dispatched WindowVisibleRegion (%i, cRects=%i)", pWindow->spuWindow, lpRgnData->rdh.nCount);
 
746
        stub.spuDispatch.WindowVisibleRegion(pWindow->spuWindow, lpRgnData->rdh.nCount, (GLint*) lpRgnData->Buffer);
 
747
        crFree(lpRgnData);
 
748
    }
 
749
    else crWarning("GetRegionData failed, VisibleRegions update failed");
 
750
}
 
751
 
 
752
static HRGN stubMakeRegionFromRects(PVBOXVIDEOCM_CMD_RECTS pRegions, uint32_t start)
 
753
{
 
754
    HRGN hRgn, hTmpRgn;
 
755
    uint32_t i;
 
756
 
 
757
    if (pRegions->RectsInfo.cRects<=start)
 
758
    {
 
759
        return INVALID_HANDLE_VALUE;
 
760
    }
 
761
 
 
762
    hRgn = CreateRectRgn(0, 0, 0, 0);
 
763
    for (i=start; i<pRegions->RectsInfo.cRects; ++i)
 
764
    {
 
765
        hTmpRgn = CreateRectRgnIndirect(&pRegions->RectsInfo.aRects[i]);
 
766
        CombineRgn(hRgn, hRgn, hTmpRgn, RGN_OR);
 
767
        DeleteObject(hTmpRgn);
 
768
    }
 
769
    return hRgn;
 
770
}
 
771
 
 
772
static void stubSyncTrUpdateWindowCB(unsigned long key, void *data1, void *data2)
 
773
{
 
774
    WindowInfo *pWindow = (WindowInfo *) data1;
 
775
    VBOXDISPMP_REGIONS *pRegions = (VBOXDISPMP_REGIONS*) data2;
 
776
    bool bChanged = false;
 
777
    HRGN hNewRgn = INVALID_HANDLE_VALUE;
 
778
 
 
779
    if (pRegions->hWnd != pWindow->hWnd)
 
780
    {
 
781
        return;
 
782
    }
 
783
 
 
784
    stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
 
785
 
 
786
    if (!stubSystemWindowExist(pWindow))
 
787
    {
 
788
        crWindowDestroy((GLint)pWindow->hWnd);
 
789
        return;
 
790
    }
 
791
 
 
792
    if (!pWindow->mapped)
 
793
    {
 
794
        pWindow->mapped = GL_TRUE;
 
795
        bChanged = true;
 
796
        crDebug("Dispatched: WindowShow(%i, %i)", pWindow->spuWindow, pWindow->mapped);
 
797
        stub.spu->dispatch_table.WindowShow(pWindow->spuWindow, pWindow->mapped);
 
798
    }
 
799
 
 
800
    if (pRegions->pRegions->fFlags.bSetVisibleRects || pRegions->pRegions->fFlags.bSetViewRect)
 
801
    {
 
802
        /* ensure data integrity */
 
803
        Assert(!pRegions->pRegions->fFlags.bAddHiddenRects);
 
804
 
 
805
        if (pRegions->pRegions->fFlags.bSetViewRect)
 
806
        {
 
807
            int winX, winY;
 
808
            unsigned int winW, winH;
 
809
            BOOL bRc;
 
810
 
 
811
            winX = pRegions->pRegions->RectsInfo.aRects[0].left;
 
812
            winY = pRegions->pRegions->RectsInfo.aRects[0].top;
 
813
            winW = pRegions->pRegions->RectsInfo.aRects[0].right - winX;
 
814
            winH = pRegions->pRegions->RectsInfo.aRects[0].bottom - winY;
 
815
 
 
816
            if (stub.trackWindowPos && (winX!=pWindow->x || winY!=pWindow->y))
 
817
            {
 
818
                crDebug("Dispatched WindowPosition (%i)", pWindow->spuWindow);
 
819
                stub.spuDispatch.WindowPosition(pWindow->spuWindow, winX, winY);
 
820
                pWindow->x = winX;
 
821
                pWindow->y = winY;
 
822
                bChanged = true;
 
823
            }
 
824
 
 
825
            if (stub.trackWindowSize && (winW!=pWindow->width || winH!=pWindow->height))
 
826
            {
 
827
                crDebug("Dispatched WindowSize (%i)", pWindow->spuWindow);
 
828
                stub.spuDispatch.WindowSize(pWindow->spuWindow, winW, winH);
 
829
                pWindow->width = winW;
 
830
                pWindow->height = winH;
 
831
                bChanged = true;
 
832
            }
 
833
 
 
834
            bRc = MoveWindow(pRegions->hWnd, winX, winY, winW, winH, FALSE /*BOOL bRepaint*/);
 
835
            if (!bRc)
 
836
            {
 
837
                DWORD winEr = GetLastError();
 
838
                crWarning("stubSyncTrUpdateWindowCB: MoveWindow failed winEr(%d)", winEr);
 
839
            }
 
840
        }
 
841
 
 
842
        if (pRegions->pRegions->fFlags.bSetVisibleRects)
 
843
        {
 
844
            hNewRgn = stubMakeRegionFromRects(pRegions->pRegions, pRegions->pRegions->fFlags.bSetViewRect ? 1 : 0);
 
845
        }
 
846
    }
 
847
    else if (!pRegions->pRegions->fFlags.bHide)
 
848
    {
 
849
        Assert(pRegions->pRegions->fFlags.bAddHiddenRects);
 
850
        hNewRgn = stubMakeRegionFromRects(pRegions->pRegions, 0);
 
851
    }
 
852
    else
 
853
    {
 
854
        Assert(pRegions->pRegions->fFlags.bAddHiddenRects);
 
855
        hNewRgn = CreateRectRgn(pWindow->x, pWindow->y, pWindow->x + pWindow->width, pWindow->y + pWindow->height);
 
856
    }
 
857
 
 
858
    if (hNewRgn!=INVALID_HANDLE_VALUE)
 
859
    {
 
860
        OffsetRgn(hNewRgn, -pWindow->x, -pWindow->y);
 
861
 
 
862
        if (pWindow->hVisibleRegion!=INVALID_HANDLE_VALUE)
 
863
        {
 
864
            CombineRgn(hNewRgn, pWindow->hVisibleRegion, hNewRgn, 
 
865
                       pRegions->pRegions->fFlags.bAddHiddenRects ? RGN_DIFF:RGN_OR);
 
866
 
 
867
            if (!EqualRgn(pWindow->hVisibleRegion, hNewRgn))
 
868
            {
 
869
                DeleteObject(pWindow->hVisibleRegion);
 
870
                pWindow->hVisibleRegion = hNewRgn;
 
871
                stubDispatchVisibleRegions(pWindow);
 
872
                bChanged = true;
 
873
            }
 
874
            else
 
875
            {
 
876
                DeleteObject(hNewRgn);
 
877
            }
 
878
        }
 
879
        else
 
880
        {
 
881
            if (pRegions->pRegions->fFlags.bSetVisibleRects)
 
882
            {
 
883
                pWindow->hVisibleRegion = hNewRgn;
 
884
                stubDispatchVisibleRegions(pWindow);
 
885
                bChanged = true;
 
886
            }
 
887
        }
 
888
    }
 
889
 
 
890
    if (bChanged)
 
891
    {
 
892
        stub.spu->dispatch_table.Flush();
 
893
    }
 
894
}
 
895
# endif
 
896
 
 
897
static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
 
898
{
 
899
    WindowInfo *pWindow = (WindowInfo *) data1;
 
900
    (void) data2;
 
901
 
 
902
    if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
 
903
    {
 
904
        return;
 
905
    }
 
906
 
 
907
    stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
 
908
 
 
909
    if (!stubSystemWindowExist(pWindow))
 
910
    {
 
911
#ifdef WINDOWS
 
912
        crWindowDestroy((GLint)pWindow->hWnd);
 
913
#else
 
914
        crWindowDestroy((GLint)pWindow->drawable);
 
915
#endif
 
916
        /*No need to flush here as crWindowDestroy does it*/
 
917
        return;
 
918
    }
 
919
 
 
920
#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
 
921
    if (stub.bRunningUnderWDDM)
 
922
        return;
 
923
#endif
 
924
    stubCheckWindowState(pWindow, GL_TRUE);
 
925
}
 
926
 
 
927
static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
 
928
{
 
929
#ifdef WINDOWS
 
930
    MSG msg;
 
931
# ifdef VBOX_WITH_WDDM
 
932
    static VBOXDISPMP_CALLBACKS VBoxDispMpTstCallbacks = {NULL, NULL, NULL};
 
933
    HMODULE hVBoxD3D = NULL;
 
934
    VBOXDISPMP_REGIONS Regions;
 
935
    HRESULT hr;
 
936
# endif
 
937
#endif
 
938
 
 
939
    (void) pvUser;
 
940
 
 
941
    crDebug("Sync thread started");
 
942
#ifdef WINDOWS
 
943
    PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
 
944
# ifdef VBOX_WITH_WDDM
 
945
    hVBoxD3D = GetModuleHandle("VBoxDispD3D");
 
946
    if (hVBoxD3D)
 
947
    {
 
948
        hVBoxD3D = LoadLibrary("VBoxDispD3D");
 
949
    }
 
950
 
 
951
    if (hVBoxD3D)
 
952
    {
 
953
        PFNVBOXDISPMP_GETCALLBACKS pfnVBoxDispMpGetCallbacks;
 
954
        pfnVBoxDispMpGetCallbacks = (PFNVBOXDISPMP_GETCALLBACKS)GetProcAddress(hVBoxD3D, TEXT("VBoxDispMpGetCallbacks"));
 
955
        if (pfnVBoxDispMpGetCallbacks)
 
956
        {
 
957
            hr = pfnVBoxDispMpGetCallbacks(VBOXDISPMP_VERSION, &VBoxDispMpTstCallbacks);
 
958
            if (S_OK==hr)
 
959
            {
 
960
                CRASSERT(VBoxDispMpTstCallbacks.pfnEnableEvents);
 
961
                CRASSERT(VBoxDispMpTstCallbacks.pfnDisableEvents);
 
962
                CRASSERT(VBoxDispMpTstCallbacks.pfnGetRegions);
 
963
 
 
964
                hr = VBoxDispMpTstCallbacks.pfnEnableEvents();
 
965
                if (hr != S_OK)
 
966
                {
 
967
                    crWarning("VBoxDispMpTstCallbacks.pfnEnableEvents failed");
 
968
                }
 
969
                else
 
970
                {
 
971
                    crDebug("running with VBoxDispD3D");
 
972
                    stub.trackWindowVisibleRgn = 0;
 
973
                    stub.bRunningUnderWDDM = true;
 
974
                }
 
975
            }
 
976
            else
 
977
            {
 
978
                crWarning("VBoxDispMpGetCallbacks failed");
 
979
            }
 
980
        }
 
981
    }
 
982
# endif
 
983
#endif
 
984
 
 
985
    crLockMutex(&stub.mutex);
 
986
    stub.spu->dispatch_table.VBoxPackSetInjectThread();
 
987
    crUnlockMutex(&stub.mutex);
 
988
 
 
989
    RTThreadUserSignal(ThreadSelf);
 
990
 
 
991
    while(!stub.bShutdownSyncThread)
 
992
    {
 
993
#ifdef WINDOWS
 
994
        if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
 
995
        {
 
996
# ifdef VBOX_WITH_WDDM
 
997
            if (VBoxDispMpTstCallbacks.pfnGetRegions)
 
998
            {
 
999
                hr = VBoxDispMpTstCallbacks.pfnGetRegions(&Regions, 50);
 
1000
                if (S_OK==hr)
 
1001
                {
 
1002
#  if 0
 
1003
                    uint32_t i;
 
1004
                    crDebug(">>>Regions for HWND(0x%x)>>>", Regions.hWnd);
 
1005
                    crDebug("Flags(0x%x)", Regions.pRegions->fFlags.Value);
 
1006
                    for (i = 0; i < Regions.pRegions->RectsInfo.cRects; ++i)
 
1007
                    {
 
1008
                        RECT *pRect = &Regions.pRegions->RectsInfo.aRects[i];
 
1009
                        crDebug("Rect(%d): left(%d), top(%d), right(%d), bottom(%d)", i, pRect->left, pRect->top, pRect->right, pRect->bottom);
 
1010
                    }
 
1011
                    crDebug("<<<<<");
 
1012
#  endif
 
1013
                    /*hacky way to make sure window wouldn't be deleted in another thread as we hold hashtable lock here*/
 
1014
                    crHashtableWalk(stub.windowTable, stubSyncTrUpdateWindowCB, &Regions);
 
1015
                }
 
1016
                else
 
1017
                {
 
1018
                    if (WAIT_TIMEOUT!=hr)
 
1019
                    {
 
1020
                        crWarning("VBoxDispMpTstCallbacks.pfnGetRegions failed with 0x%x", hr);
 
1021
                    }
 
1022
                    crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
 
1023
                }
 
1024
            }
 
1025
            else
 
1026
# endif
 
1027
            {
 
1028
                crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
 
1029
                RTThreadSleep(50);
 
1030
            }
 
1031
        }
 
1032
        else
 
1033
        {
 
1034
            if (WM_QUIT==msg.message)
 
1035
            {
 
1036
                crDebug("Sync thread got WM_QUIT");
 
1037
                break;
 
1038
            }
 
1039
            else
 
1040
            {
 
1041
                TranslateMessage(&msg);
 
1042
                DispatchMessage(&msg);
 
1043
            }
 
1044
        }
 
1045
#else
 
1046
        crLockMutex(&stub.mutex);
 
1047
        crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
 
1048
        crUnlockMutex(&stub.mutex);
 
1049
        RTThreadSleep(50);
 
1050
#endif
 
1051
    }
 
1052
 
 
1053
#ifdef VBOX_WITH_WDDM
 
1054
    if (VBoxDispMpTstCallbacks.pfnDisableEvents)
 
1055
    {
 
1056
        VBoxDispMpTstCallbacks.pfnDisableEvents();
 
1057
    }
 
1058
    if (hVBoxD3D)
 
1059
    {
 
1060
        FreeLibrary(hVBoxD3D);
 
1061
    }
 
1062
#endif
 
1063
    crDebug("Sync thread stopped");
 
1064
    return 0;
 
1065
}
 
1066
#endif
 
1067
 
597
1068
/**
598
1069
 * Do one-time initializations for the faker.
599
1070
 * Returns TRUE on success, FALSE otherwise.
616
1087
    char **spu_names;
617
1088
    const char *app_id;
618
1089
    int i;
 
1090
    int disable_sync = 0;
619
1091
 
620
1092
    if (stub_initialized)
621
1093
        return true;
622
1094
 
623
1095
    stubInitVars();
624
1096
 
 
1097
    crGetProcName(response, 1024);
 
1098
    crDebug("Stub launched for %s", response);
 
1099
 
 
1100
#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
 
1101
    /*@todo when vm boots with compiz turned on, new code causes hang in xcb_wait_for_reply in the sync thread*/
 
1102
    if (!crStrcmp(response, "compiz") || !crStrcmp(response, "compiz_real")
 
1103
        || !crStrcmp(response, "compiz-bin"))
 
1104
    {
 
1105
        disable_sync = 1;
 
1106
    }
 
1107
#endif
 
1108
 
625
1109
    /* @todo check if it'd be of any use on other than guests, no use for windows */
626
1110
    app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
627
1111
 
643
1127
        {
644
1128
            crNetFreeConnection(ns.conn);
645
1129
        }
 
1130
#if 0 && defined(CR_NEWWINTRACK)
 
1131
        {
 
1132
            Status st = XInitThreads();
 
1133
            if (st==0)
 
1134
            {
 
1135
                crWarning("XInitThreads returned %i", (int)st);
 
1136
            }
 
1137
        }
 
1138
#endif
646
1139
    }
647
1140
#endif
648
1141
 
691
1184
raise(SIGINT);*/
692
1185
 
693
1186
#ifdef WINDOWS
 
1187
# ifndef CR_NEWWINTRACK
694
1188
    stubInstallWindowMessageHook();
 
1189
# endif
 
1190
#endif
 
1191
 
 
1192
#ifdef CR_NEWWINTRACK
 
1193
    {
 
1194
        int rc;
 
1195
 
 
1196
        RTR3Init();
 
1197
 
 
1198
        if (!disable_sync)
 
1199
        {
 
1200
            crDebug("Starting sync thread");
 
1201
 
 
1202
            rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
 
1203
            if (RT_FAILURE(rc))
 
1204
            {
 
1205
                crError("Failed to start sync thread! (%x)", rc);
 
1206
            }
 
1207
            RTThreadUserWait(stub.hSyncThread, 60 * 1000);
 
1208
            RTThreadUserReset(stub.hSyncThread);
 
1209
 
 
1210
            crDebug("Going on");
 
1211
        }
 
1212
    }
695
1213
#endif
696
1214
 
697
1215
#ifdef GLX
698
1216
    stub.xshmSI.shmid = -1;
699
1217
    stub.bShmInitFailed = GL_FALSE;
700
1218
    stub.pGLXPixmapsHash = crAllocHashtable();
 
1219
 
 
1220
    stub.bXExtensionsChecked = GL_FALSE;
 
1221
    stub.bHaveXComposite = GL_FALSE;
 
1222
    stub.bHaveXFixes = GL_FALSE;
701
1223
#endif
702
1224
 
703
1225
    stub_initialized = 1;
704
1226
    return true;
705
1227
}
706
1228
 
707
 
 
708
 
 
709
1229
/* Sigh -- we can't do initialization at load time, since Windows forbids 
710
1230
 * the loading of other libraries from DLLMain. */
711
1231
 
750
1270
        break;
751
1271
    }
752
1272
 
 
1273
#if 0
753
1274
    case DLL_THREAD_ATTACH:
 
1275
    {
 
1276
        if (stub_initialized)
 
1277
        {
 
1278
            CRASSERT(stub.spu);
 
1279
            stub.spu->dispatch_table.VBoxPackAttachThread();
 
1280
        }
754
1281
        break;
 
1282
    }
755
1283
 
756
1284
    case DLL_THREAD_DETACH:
 
1285
    {
 
1286
        if (stub_initialized)
 
1287
        {
 
1288
            CRASSERT(stub.spu);
 
1289
            stub.spu->dispatch_table.VBoxPackDetachThread();
 
1290
        }
757
1291
        break;
 
1292
    }
 
1293
#endif
758
1294
 
759
1295
    default:
760
1296
        break;