d3d9: Hold the lock in stateblock methods.
[wine] / dlls / d3d9 / stateblock.c
1 /*
2  * IDirect3DStateBlock9 implementation
3  *
4  * Copyright 2002-2003 Raphael Junqueira
5  * Copyright 2002-2003 Jason Edmeades
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 /* IDirect3DStateBlock9 IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DStateBlock9Impl_QueryInterface(LPDIRECT3DSTATEBLOCK9 iface, REFIID riid, LPVOID* ppobj) {
30     IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DStateBlock9)) {
34         IUnknown_AddRef(iface);
35         *ppobj = This;
36         return S_OK;
37     }
38
39     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40     *ppobj = NULL;
41     return E_NOINTERFACE;
42 }
43
44 static ULONG WINAPI IDirect3DStateBlock9Impl_AddRef(LPDIRECT3DSTATEBLOCK9 iface) {
45     IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
46     ULONG ref = InterlockedIncrement(&This->ref);
47
48     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
49
50     return ref;
51 }
52
53 static ULONG WINAPI IDirect3DStateBlock9Impl_Release(LPDIRECT3DSTATEBLOCK9 iface) {
54     IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
55     ULONG ref = InterlockedDecrement(&This->ref);
56
57     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
58
59     if (ref == 0) {
60         EnterCriticalSection(&d3d9_cs);
61         IWineD3DStateBlock_Release(This->wineD3DStateBlock);    
62         LeaveCriticalSection(&d3d9_cs);
63         IUnknown_Release(This->parentDevice);
64         HeapFree(GetProcessHeap(), 0, This);
65     }
66     return ref;
67 }
68
69 /* IDirect3DStateBlock9 Interface follow: */
70 static HRESULT WINAPI IDirect3DStateBlock9Impl_GetDevice(LPDIRECT3DSTATEBLOCK9 iface, IDirect3DDevice9** ppDevice) {
71     IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
72     HRESULT hr;
73     TRACE("(%p) Relay\n", This);
74
75     EnterCriticalSection(&d3d9_cs);
76     hr = IDirect3DResource9Impl_GetDevice((LPDIRECT3DRESOURCE9) This, ppDevice);
77     LeaveCriticalSection(&d3d9_cs);
78     return hr;
79 }
80
81 static HRESULT WINAPI IDirect3DStateBlock9Impl_Capture(LPDIRECT3DSTATEBLOCK9 iface) {
82     IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
83     HRESULT hr;
84     TRACE("(%p) Relay\n", This); 
85
86     EnterCriticalSection(&d3d9_cs);
87     hr = IWineD3DStateBlock_Capture(This->wineD3DStateBlock);
88     LeaveCriticalSection(&d3d9_cs);
89     return hr;
90 }
91
92 static HRESULT WINAPI IDirect3DStateBlock9Impl_Apply(LPDIRECT3DSTATEBLOCK9 iface) {
93     IDirect3DStateBlock9Impl *This = (IDirect3DStateBlock9Impl *)iface;
94     HRESULT hr;
95     TRACE("(%p) Relay\n", This);
96
97     EnterCriticalSection(&d3d9_cs);
98     hr = IWineD3DStateBlock_Apply(This->wineD3DStateBlock);
99     LeaveCriticalSection(&d3d9_cs);
100     return hr;
101 }
102
103
104 static const IDirect3DStateBlock9Vtbl Direct3DStateBlock9_Vtbl =
105 {
106     /* IUnknown */
107     IDirect3DStateBlock9Impl_QueryInterface,
108     IDirect3DStateBlock9Impl_AddRef,
109     IDirect3DStateBlock9Impl_Release,
110     /* IDirect3DStateBlock9 */
111     IDirect3DStateBlock9Impl_GetDevice,
112     IDirect3DStateBlock9Impl_Capture,
113     IDirect3DStateBlock9Impl_Apply
114 };
115
116
117 /* IDirect3DDevice9 IDirect3DStateBlock9 Methods follow: */
118 HRESULT WINAPI IDirect3DDevice9Impl_CreateStateBlock(LPDIRECT3DDEVICE9 iface, D3DSTATEBLOCKTYPE Type, IDirect3DStateBlock9** ppStateBlock) {
119    IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
120    IDirect3DStateBlock9Impl* object;
121    HRESULT hrc = D3D_OK;
122    
123    TRACE("(%p) Relay\n", This);
124    
125    object  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock9Impl));
126    if (NULL == object) return E_OUTOFMEMORY;
127    object->lpVtbl = &Direct3DStateBlock9_Vtbl;
128    object->ref = 1;
129    
130    EnterCriticalSection(&d3d9_cs);
131    hrc = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown*)object);
132    LeaveCriticalSection(&d3d9_cs);
133    if(hrc != D3D_OK){
134        FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
135        HeapFree(GetProcessHeap(), 0, object);
136    } else {
137         IUnknown_AddRef(iface);
138         object->parentDevice = iface;
139         *ppStateBlock = (IDirect3DStateBlock9*)object;
140         TRACE("(%p) : Created stateblock %p\n", This, object);
141    }
142    TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
143    return hrc;
144 }
145
146 HRESULT  WINAPI  IDirect3DDevice9Impl_BeginStateBlock(LPDIRECT3DDEVICE9 iface) {
147     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
148     HRESULT hr;
149     TRACE("(%p) Relay\n", This);
150
151     EnterCriticalSection(&d3d9_cs);
152     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
153     LeaveCriticalSection(&d3d9_cs);
154     return hr;
155 }
156
157 HRESULT  WINAPI  IDirect3DDevice9Impl_EndStateBlock(LPDIRECT3DDEVICE9 iface, IDirect3DStateBlock9** ppSB) {
158     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;   
159     HRESULT hr;
160     IWineD3DStateBlock* wineD3DStateBlock;    
161     IDirect3DStateBlock9Impl* object;
162
163     TRACE("(%p) Relay\n", This); 
164     
165     /* Tell wineD3D to endstatablock before anything else (in case we run out
166      * of memory later and cause locking problems)
167      */
168     EnterCriticalSection(&d3d9_cs);
169     hr=IWineD3DDevice_EndStateBlock(This->WineD3DDevice,&wineD3DStateBlock);
170     LeaveCriticalSection(&d3d9_cs);
171     if(hr!= D3D_OK){
172        FIXME("IWineD3DDevice_EndStateBlock returned an error\n");
173        return hr;
174     }    
175     /* allocate a new IDirectD3DStateBlock */
176     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock9Impl));      
177     if (!object) return E_OUTOFMEMORY;
178     object->ref = 1;
179     object->lpVtbl = &Direct3DStateBlock9_Vtbl;
180     object->wineD3DStateBlock = wineD3DStateBlock;
181
182     IUnknown_AddRef(iface);
183     object->parentDevice = iface;
184     *ppSB=(IDirect3DStateBlock9*)object;
185     TRACE("(%p)Returning %p %p\n", This, *ppSB, wineD3DStateBlock);
186     return D3D_OK;
187 }