xref: /reactos/dll/directx/d3d9/d3d9_baseobject.c (revision b09b5584)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS ReactX
4  * FILE:            dll/directx/d3d9/d3d9_baseobject.c
5  * PURPOSE:         Direct3D9's base object
6  * PROGRAMERS:      Gregor Gullwi <gbrunmar (dot) ros (at) gmail (dot) com>
7  */
8 #include "d3d9_baseobject.h"
9 #include "d3d9_device.h"
10 
11 #include <debug.h>
12 #include "d3d9_helpers.h"
13 
14 VOID D3D9BaseObject_Destroy(D3D9BaseObject* pBaseObject, BOOL bFreeThis)
15 {
16     if (bFreeThis)
17     {
18         AlignedFree((LPVOID*) pBaseObject);
19     }
20 }
21 
22 ID3D9BaseObjectVtbl D3D9BaseObject_Vtbl =
23 {
24     /* D3D9BaseObject */
25     D3D9BaseObject_Destroy
26 };
27 
28 VOID InitD3D9BaseObject(D3D9BaseObject* pBaseObject, enum REF_TYPE RefType, IUnknown* pUnknown)
29 {
30     pBaseObject->lpVtbl = &D3D9BaseObject_Vtbl;
31     pBaseObject->RefType = RefType;
32     pBaseObject->pUnknown = pUnknown;
33 }
34 
35 ULONG D3D9BaseObject_AddRef(D3D9BaseObject* pBaseObject)
36 {
37     if (pBaseObject->pUnknown)
38     {
39         pBaseObject->pUnknown->lpVtbl->AddRef((IUnknown*) &pBaseObject->pUnknown->lpVtbl);
40     }
41 
42     return InterlockedIncrement(&pBaseObject->lRefCnt);
43 }
44 
45 ULONG D3D9BaseObject_Release(D3D9BaseObject* pBaseObject)
46 {
47     ULONG Ref = 0;
48 
49     if (pBaseObject)
50     {
51         Ref = InterlockedDecrement(&pBaseObject->lRefCnt);
52 
53         if (Ref == 0)
54         {
55             if (pBaseObject->pUnknown)
56             {
57                 pBaseObject->pUnknown->lpVtbl->Release((IUnknown*) &pBaseObject->pUnknown->lpVtbl);
58             }
59         }
60     }
61 
62     return Ref;
63 }
64 
65 HRESULT D3D9BaseObject_GetDevice(D3D9BaseObject* pBaseObject, IDirect3DDevice9** ppDevice)
66 {
67     if (pBaseObject->pUnknown)
68     {
69         return IUnknown_QueryInterface(pBaseObject->pUnknown, &IID_IDirect3DDevice9, (void**)ppDevice);
70     }
71 
72     return E_NOINTERFACE;
73 }
74 
75 HRESULT D3D9BaseObject_GetDeviceInt(D3D9BaseObject* pBaseObject, DIRECT3DDEVICE9_INT** ppDevice)
76 {
77     if (NULL == ppDevice)
78         return D3DERR_INVALIDCALL;
79 
80     *ppDevice = NULL;
81 
82     if (pBaseObject->pUnknown)
83     {
84         LPDIRECT3DDEVICE9 pDevice;
85         if (FAILED(IUnknown_QueryInterface(pBaseObject->pUnknown, &IID_IDirect3DDevice9, (void**)&pDevice)))
86             return E_NOINTERFACE;
87 
88         *ppDevice = IDirect3DDevice9ToImpl(pDevice);
89         return D3D_OK;
90     }
91 
92     return E_NOINTERFACE;
93 }
94 
95 VOID D3D9BaseObject_LockDevice(D3D9BaseObject* pBaseObject)
96 {
97     DIRECT3DDEVICE9_INT* pDevice;
98     if (FAILED(D3D9BaseObject_GetDeviceInt(pBaseObject, &pDevice)))
99         return;
100 
101     if (pDevice->bLockDevice)
102         EnterCriticalSection(&pDevice->CriticalSection);
103 }
104 
105 VOID D3D9BaseObject_UnlockDevice(D3D9BaseObject* pBaseObject)
106 {
107     DIRECT3DDEVICE9_INT* pDevice;
108     if (FAILED(D3D9BaseObject_GetDeviceInt(pBaseObject, &pDevice)))
109         return;
110 
111     if (pDevice->bLockDevice)
112         LeaveCriticalSection(&pDevice->CriticalSection);
113 }
114