wined3d: Don't free D3D buffers until the wined3d buffer is destroyed.
[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         IDirect3DIndexBuffer9_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     if (ref == 1)
51     {
52         IDirect3DDevice9Ex_AddRef(This->parentDevice);
53         wined3d_mutex_lock();
54         IWineD3DBuffer_AddRef(This->wineD3DIndexBuffer);
55         wined3d_mutex_unlock();
56     }
57
58     return ref;
59 }
60
61 static ULONG WINAPI IDirect3DIndexBuffer9Impl_Release(LPDIRECT3DINDEXBUFFER9 iface) {
62     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
63     ULONG ref = InterlockedDecrement(&This->ref);
64
65     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
66
67     if (ref == 0) {
68         IDirect3DDevice9Ex_Release(This->parentDevice);
69         wined3d_mutex_lock();
70         IWineD3DBuffer_Release(This->wineD3DIndexBuffer);
71         wined3d_mutex_unlock();
72     }
73     return ref;
74 }
75
76 /* IDirect3DIndexBuffer9 IDirect3DResource9 Interface follow: */
77 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetDevice(LPDIRECT3DINDEXBUFFER9 iface, IDirect3DDevice9** ppDevice) {
78     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
79     IWineD3DDevice *wined3d_device;
80     HRESULT hr;
81     TRACE("(%p) Relay\n", This);
82
83     wined3d_mutex_lock();
84     hr = IWineD3DBuffer_GetDevice(This->wineD3DIndexBuffer, &wined3d_device);
85     if (SUCCEEDED(hr))
86     {
87         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
88         IWineD3DDevice_Release(wined3d_device);
89     }
90     wined3d_mutex_unlock();
91
92     return hr;
93 }
94
95 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_SetPrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
96     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
97     HRESULT hr;
98     TRACE("(%p) Relay\n", This);
99
100     wined3d_mutex_lock();
101     hr = IWineD3DBuffer_SetPrivateData(This->wineD3DIndexBuffer, refguid, pData, SizeOfData, Flags);
102     wined3d_mutex_unlock();
103
104     return hr;
105 }
106
107 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetPrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
108     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
109     HRESULT hr;
110     TRACE("(%p) Relay\n", This);
111
112     wined3d_mutex_lock();
113     hr = IWineD3DBuffer_GetPrivateData(This->wineD3DIndexBuffer, refguid, pData, pSizeOfData);
114     wined3d_mutex_unlock();
115
116     return hr;
117 }
118
119 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_FreePrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid) {
120     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
121     HRESULT hr;
122     TRACE("(%p) Relay\n", This);
123
124     wined3d_mutex_lock();
125     hr = IWineD3DBuffer_FreePrivateData(This->wineD3DIndexBuffer, refguid);
126     wined3d_mutex_unlock();
127
128     return hr;
129 }
130
131 static DWORD WINAPI IDirect3DIndexBuffer9Impl_SetPriority(LPDIRECT3DINDEXBUFFER9 iface, DWORD PriorityNew) {
132     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
133     DWORD ret;
134     TRACE("(%p) Relay\n", This);
135
136     wined3d_mutex_lock();
137     ret = IWineD3DBuffer_SetPriority(This->wineD3DIndexBuffer, PriorityNew);
138     wined3d_mutex_unlock();
139
140     return ret;
141 }
142
143 static DWORD WINAPI IDirect3DIndexBuffer9Impl_GetPriority(LPDIRECT3DINDEXBUFFER9 iface) {
144     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
145     DWORD ret;
146     TRACE("(%p) Relay\n", This);
147
148     wined3d_mutex_lock();
149     ret = IWineD3DBuffer_GetPriority(This->wineD3DIndexBuffer);
150     wined3d_mutex_unlock();
151
152     return ret;
153 }
154
155 static void WINAPI IDirect3DIndexBuffer9Impl_PreLoad(LPDIRECT3DINDEXBUFFER9 iface) {
156     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
157     TRACE("(%p) Relay\n", This);
158
159     wined3d_mutex_lock();
160     IWineD3DBuffer_PreLoad(This->wineD3DIndexBuffer);
161     wined3d_mutex_unlock();
162 }
163
164 static D3DRESOURCETYPE WINAPI IDirect3DIndexBuffer9Impl_GetType(LPDIRECT3DINDEXBUFFER9 iface) {
165     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
166     TRACE("(%p)\n", This);
167
168     return D3DRTYPE_INDEXBUFFER;
169 }
170
171 /* IDirect3DIndexBuffer9 Interface follow: */
172 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_Lock(LPDIRECT3DINDEXBUFFER9 iface, UINT OffsetToLock, UINT SizeToLock, void** ppbData, DWORD Flags) {
173     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
174     HRESULT hr;
175     TRACE("(%p) Relay\n", This);
176
177     wined3d_mutex_lock();
178     hr = IWineD3DBuffer_Map(This->wineD3DIndexBuffer, OffsetToLock, SizeToLock, (BYTE **)ppbData, Flags);
179     wined3d_mutex_unlock();
180
181     return hr;
182 }
183
184 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_Unlock(LPDIRECT3DINDEXBUFFER9 iface) {
185     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
186     HRESULT hr;
187     TRACE("(%p) Relay\n", This);
188
189     wined3d_mutex_lock();
190     hr = IWineD3DBuffer_Unmap(This->wineD3DIndexBuffer);
191     wined3d_mutex_unlock();
192
193     return hr;
194 }
195
196 static HRESULT  WINAPI        IDirect3DIndexBuffer9Impl_GetDesc(LPDIRECT3DINDEXBUFFER9 iface, D3DINDEXBUFFER_DESC *pDesc) {
197     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
198     HRESULT hr;
199     WINED3DBUFFER_DESC desc;
200     TRACE("(%p) Relay\n", This);
201
202     wined3d_mutex_lock();
203     hr = IWineD3DBuffer_GetDesc(This->wineD3DIndexBuffer, &desc);
204     wined3d_mutex_unlock();
205
206     if (SUCCEEDED(hr)) {
207         pDesc->Format = d3dformat_from_wined3dformat(This->format);
208         pDesc->Usage = desc.Usage;
209         pDesc->Pool = desc.Pool;
210         pDesc->Size = desc.Size;
211         pDesc->Type = D3DRTYPE_INDEXBUFFER;
212     }
213
214     return hr;
215 }
216
217
218 static const IDirect3DIndexBuffer9Vtbl Direct3DIndexBuffer9_Vtbl =
219 {
220     /* IUnknown */
221     IDirect3DIndexBuffer9Impl_QueryInterface,
222     IDirect3DIndexBuffer9Impl_AddRef,
223     IDirect3DIndexBuffer9Impl_Release,
224     /* IDirect3DResource9 */
225     IDirect3DIndexBuffer9Impl_GetDevice,
226     IDirect3DIndexBuffer9Impl_SetPrivateData,
227     IDirect3DIndexBuffer9Impl_GetPrivateData,
228     IDirect3DIndexBuffer9Impl_FreePrivateData,
229     IDirect3DIndexBuffer9Impl_SetPriority,
230     IDirect3DIndexBuffer9Impl_GetPriority,
231     IDirect3DIndexBuffer9Impl_PreLoad,
232     IDirect3DIndexBuffer9Impl_GetType,
233     /* IDirect3DIndexBuffer9 */
234     IDirect3DIndexBuffer9Impl_Lock,
235     IDirect3DIndexBuffer9Impl_Unlock,
236     IDirect3DIndexBuffer9Impl_GetDesc
237 };
238
239 static void STDMETHODCALLTYPE d3d9_indexbuffer_wined3d_object_destroyed(void *parent)
240 {
241     HeapFree(GetProcessHeap(), 0, parent);
242 }
243
244 static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
245 {
246     d3d9_indexbuffer_wined3d_object_destroyed,
247 };
248
249 HRESULT indexbuffer_init(IDirect3DIndexBuffer9Impl *buffer, IDirect3DDevice9Impl *device,
250         UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
251 {
252     HRESULT hr;
253
254     buffer->lpVtbl = &Direct3DIndexBuffer9_Vtbl;
255     buffer->ref = 1;
256     buffer->format = wined3dformat_from_d3dformat(format);
257
258     wined3d_mutex_lock();
259     hr = IWineD3DDevice_CreateIndexBuffer(device->WineD3DDevice, size,
260             usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, &buffer->wineD3DIndexBuffer,
261             (IUnknown *)buffer, &d3d9_indexbuffer_wined3d_parent_ops);
262     wined3d_mutex_unlock();
263     if (FAILED(hr))
264     {
265         WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
266         return hr;
267     }
268
269     buffer->parentDevice = (IDirect3DDevice9Ex *)device;
270     IDirect3DDevice9Ex_AddRef(buffer->parentDevice);
271
272     return D3D_OK;
273 }