d3d9: Hold the lock in indexbuffer methods.
[wine] / dlls / d3d9 / indexbuffer.c
1 /*
2  * IDirect3DIndexBuffer9 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DIndexBuffer9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_QueryInterface(LPDIRECT3DINDEXBUFFER9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3DResource9)
33         || IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)) {
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 IDirect3DIndexBuffer9Impl_AddRef(LPDIRECT3DINDEXBUFFER9 iface) {
45     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)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 IDirect3DIndexBuffer9Impl_Release(LPDIRECT3DINDEXBUFFER9 iface) {
54     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)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         IWineD3DIndexBuffer_Release(This->wineD3DIndexBuffer);
62         LeaveCriticalSection(&d3d9_cs);
63         IUnknown_Release(This->parentDevice);
64         HeapFree(GetProcessHeap(), 0, This);
65     }
66     return ref;
67 }
68
69 /* IDirect3DIndexBuffer9 IDirect3DResource9 Interface follow: */
70 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetDevice(LPDIRECT3DINDEXBUFFER9 iface, IDirect3DDevice9** ppDevice) {
71     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)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 IDirect3DIndexBuffer9Impl_SetPrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
82     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
83     HRESULT hr;
84     TRACE("(%p) Relay\n", This);
85
86     EnterCriticalSection(&d3d9_cs);
87     hr = IWineD3DIndexBuffer_SetPrivateData(This->wineD3DIndexBuffer, refguid, pData, SizeOfData, Flags);
88     LeaveCriticalSection(&d3d9_cs);
89     return hr;
90 }
91
92 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetPrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
93     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
94     HRESULT hr;
95     TRACE("(%p) Relay\n", This);
96
97     EnterCriticalSection(&d3d9_cs);
98     hr = IWineD3DIndexBuffer_GetPrivateData(This->wineD3DIndexBuffer, refguid, pData, pSizeOfData);
99     LeaveCriticalSection(&d3d9_cs);
100     return hr;
101 }
102
103 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_FreePrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid) {
104     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
105     HRESULT hr;
106     TRACE("(%p) Relay\n", This);
107
108     EnterCriticalSection(&d3d9_cs);
109     hr = IWineD3DIndexBuffer_FreePrivateData(This->wineD3DIndexBuffer, refguid);
110     LeaveCriticalSection(&d3d9_cs);
111     return hr;
112 }
113
114 static DWORD WINAPI IDirect3DIndexBuffer9Impl_SetPriority(LPDIRECT3DINDEXBUFFER9 iface, DWORD PriorityNew) {
115     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
116     DWORD ret;
117     TRACE("(%p) Relay\n", This);
118
119     EnterCriticalSection(&d3d9_cs);
120     ret = IWineD3DIndexBuffer_SetPriority(This->wineD3DIndexBuffer, PriorityNew);
121     LeaveCriticalSection(&d3d9_cs);
122     return ret;
123 }
124
125 static DWORD WINAPI IDirect3DIndexBuffer9Impl_GetPriority(LPDIRECT3DINDEXBUFFER9 iface) {
126     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
127     DWORD ret;
128     TRACE("(%p) Relay\n", This);
129
130     EnterCriticalSection(&d3d9_cs);
131     ret = IWineD3DIndexBuffer_GetPriority(This->wineD3DIndexBuffer);
132     LeaveCriticalSection(&d3d9_cs);
133     return ret;
134 }
135
136 static void WINAPI IDirect3DIndexBuffer9Impl_PreLoad(LPDIRECT3DINDEXBUFFER9 iface) {
137     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
138     TRACE("(%p) Relay\n", This);
139
140     EnterCriticalSection(&d3d9_cs);
141     IWineD3DIndexBuffer_PreLoad(This->wineD3DIndexBuffer);
142     LeaveCriticalSection(&d3d9_cs);
143 }
144
145 static D3DRESOURCETYPE WINAPI IDirect3DIndexBuffer9Impl_GetType(LPDIRECT3DINDEXBUFFER9 iface) {
146     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
147     D3DRESOURCETYPE ret;
148     TRACE("(%p) Relay\n", This);
149
150     EnterCriticalSection(&d3d9_cs);
151     ret = IWineD3DIndexBuffer_GetType(This->wineD3DIndexBuffer);
152     LeaveCriticalSection(&d3d9_cs);
153     return ret;
154 }
155
156 /* IDirect3DIndexBuffer9 Interface follow: */
157 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_Lock(LPDIRECT3DINDEXBUFFER9 iface, UINT OffsetToLock, UINT SizeToLock, void** ppbData, DWORD Flags) {
158     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
159     HRESULT hr;
160     TRACE("(%p) Relay\n", This);
161
162     EnterCriticalSection(&d3d9_cs);
163     hr = IWineD3DIndexBuffer_Lock(This->wineD3DIndexBuffer, OffsetToLock, SizeToLock, (BYTE **)ppbData, Flags);
164     LeaveCriticalSection(&d3d9_cs);
165     return hr;
166 }
167
168 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_Unlock(LPDIRECT3DINDEXBUFFER9 iface) {
169     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
170     HRESULT hr;
171     TRACE("(%p) Relay\n", This);
172
173     EnterCriticalSection(&d3d9_cs);
174     hr = IWineD3DIndexBuffer_Unlock(This->wineD3DIndexBuffer);
175     LeaveCriticalSection(&d3d9_cs);
176     return hr;
177 }
178
179 static HRESULT  WINAPI        IDirect3DIndexBuffer9Impl_GetDesc(LPDIRECT3DINDEXBUFFER9 iface, D3DINDEXBUFFER_DESC *pDesc) {
180     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
181     HRESULT hr;
182     TRACE("(%p) Relay\n", This);
183
184     EnterCriticalSection(&d3d9_cs);
185     hr = IWineD3DIndexBuffer_GetDesc(This->wineD3DIndexBuffer, (WINED3DINDEXBUFFER_DESC *) pDesc);
186     LeaveCriticalSection(&d3d9_cs);
187     return hr;
188 }
189
190
191 static const IDirect3DIndexBuffer9Vtbl Direct3DIndexBuffer9_Vtbl =
192 {
193     /* IUnknown */
194     IDirect3DIndexBuffer9Impl_QueryInterface,
195     IDirect3DIndexBuffer9Impl_AddRef,
196     IDirect3DIndexBuffer9Impl_Release,
197     /* IDirect3DResource9 */
198     IDirect3DIndexBuffer9Impl_GetDevice,
199     IDirect3DIndexBuffer9Impl_SetPrivateData,
200     IDirect3DIndexBuffer9Impl_GetPrivateData,
201     IDirect3DIndexBuffer9Impl_FreePrivateData,
202     IDirect3DIndexBuffer9Impl_SetPriority,
203     IDirect3DIndexBuffer9Impl_GetPriority,
204     IDirect3DIndexBuffer9Impl_PreLoad,
205     IDirect3DIndexBuffer9Impl_GetType,
206     /* IDirect3DIndexBuffer9 */
207     IDirect3DIndexBuffer9Impl_Lock,
208     IDirect3DIndexBuffer9Impl_Unlock,
209     IDirect3DIndexBuffer9Impl_GetDesc
210 };
211
212
213 /* IDirect3DDevice9 IDirect3DIndexBuffer9 Methods follow: */
214 HRESULT WINAPI IDirect3DDevice9Impl_CreateIndexBuffer(LPDIRECT3DDEVICE9 iface, 
215                               UINT Length, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool,
216                               IDirect3DIndexBuffer9** ppIndexBuffer, HANDLE* pSharedHandle) {
217     
218     IDirect3DIndexBuffer9Impl *object;
219     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
220     HRESULT hrc = D3D_OK;
221     
222     TRACE("(%p) Relay\n", This);
223     /* Allocate the storage for the device */
224     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
225     if (NULL == object) {
226         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
227         return D3DERR_OUTOFVIDEOMEMORY;
228     }
229
230     object->lpVtbl = &Direct3DIndexBuffer9_Vtbl;
231     object->ref = 1;
232     TRACE("Calling wined3d create index buffer\n");
233     hrc = IWineD3DDevice_CreateIndexBuffer(This->WineD3DDevice, Length, Usage & WINED3DUSAGE_MASK, Format, (WINED3DPOOL) Pool, &object->wineD3DIndexBuffer, pSharedHandle, (IUnknown *)object);
234     if (hrc != D3D_OK) {
235
236         /* free up object */
237         FIXME("(%p) call to IWineD3DDevice_CreateIndexBuffer failed\n", This);
238         HeapFree(GetProcessHeap(), 0, object);
239     } else {
240         IUnknown_AddRef(iface);
241         object->parentDevice = iface;
242         *ppIndexBuffer = (LPDIRECT3DINDEXBUFFER9) object;
243         TRACE("(%p) : Created index buffer %p\n", This, object);
244     }
245     return hrc;
246 }