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