wined3d: Don't free D3D buffers until the wined3d buffer is destroyed.
[wine] / dlls / d3d8 / vertexbuffer.c
1 /*
2  * IDirect3DVertexBuffer8 implementation
3  *
4  * Copyright 2005 Oliver Stieber
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "d3d8_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25
26 /* IDirect3DVertexBuffer8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_QueryInterface(LPDIRECT3DVERTEXBUFFER8 iface, REFIID riid, LPVOID *ppobj) {
28     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
29
30     if (IsEqualGUID(riid, &IID_IUnknown)
31         || IsEqualGUID(riid, &IID_IDirect3DResource8)
32         || IsEqualGUID(riid, &IID_IDirect3DVertexBuffer8)) {
33         IUnknown_AddRef(iface);
34         *ppobj = This;
35         return S_OK;
36     }
37
38     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
39
40     *ppobj = NULL;
41     return E_NOINTERFACE;
42 }
43
44 static ULONG WINAPI IDirect3DVertexBuffer8Impl_AddRef(LPDIRECT3DVERTEXBUFFER8 iface) {
45     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)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         IDirect3DDevice8_AddRef(This->parentDevice);
53         wined3d_mutex_lock();
54         IWineD3DBuffer_AddRef(This->wineD3DVertexBuffer);
55         wined3d_mutex_unlock();
56     }
57
58     return ref;
59 }
60
61 static ULONG WINAPI IDirect3DVertexBuffer8Impl_Release(LPDIRECT3DVERTEXBUFFER8 iface) {
62     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
63     ULONG ref = InterlockedDecrement(&This->ref);
64
65     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
66
67     if (ref == 0) {
68         IDirect3DDevice8_Release(This->parentDevice);
69         wined3d_mutex_lock();
70         IWineD3DBuffer_Release(This->wineD3DVertexBuffer);
71         wined3d_mutex_unlock();
72     }
73
74     return ref;
75 }
76
77 /* IDirect3DVertexBuffer8 IDirect3DResource8 Interface follow: */
78 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_GetDevice(LPDIRECT3DVERTEXBUFFER8 iface, IDirect3DDevice8 **ppDevice) {
79     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
80     IWineD3DDevice *wined3d_device;
81     HRESULT hr;
82     TRACE("(%p) Relay\n", This);
83
84     wined3d_mutex_lock();
85     hr = IWineD3DBuffer_GetDevice(This->wineD3DVertexBuffer, &wined3d_device);
86     if (SUCCEEDED(hr))
87     {
88         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
89         IWineD3DDevice_Release(wined3d_device);
90     }
91     wined3d_mutex_unlock();
92
93     return hr;
94 }
95
96 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_SetPrivateData(LPDIRECT3DVERTEXBUFFER8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
97     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
98     HRESULT hr;
99     TRACE("(%p) Relay\n", This);
100
101     wined3d_mutex_lock();
102     hr = IWineD3DBuffer_SetPrivateData(This->wineD3DVertexBuffer, refguid, pData, SizeOfData, Flags);
103     wined3d_mutex_unlock();
104
105     return hr;
106 }
107
108 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_GetPrivateData(LPDIRECT3DVERTEXBUFFER8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
109     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
110     HRESULT hr;
111     TRACE("(%p) Relay\n", This);
112
113     wined3d_mutex_lock();
114     hr = IWineD3DBuffer_GetPrivateData(This->wineD3DVertexBuffer, refguid, pData, pSizeOfData);
115     wined3d_mutex_unlock();
116
117     return hr;
118 }
119
120 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_FreePrivateData(LPDIRECT3DVERTEXBUFFER8 iface, REFGUID refguid) {
121     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
122     HRESULT hr;
123     TRACE("(%p) Relay\n", This);
124
125     wined3d_mutex_lock();
126     hr = IWineD3DBuffer_FreePrivateData(This->wineD3DVertexBuffer, refguid);
127     wined3d_mutex_unlock();
128
129     return hr;
130 }
131
132 static DWORD WINAPI IDirect3DVertexBuffer8Impl_SetPriority(LPDIRECT3DVERTEXBUFFER8 iface, DWORD PriorityNew) {
133     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
134     DWORD ret;
135     TRACE("(%p) Relay\n", This);
136
137     wined3d_mutex_lock();
138     ret = IWineD3DBuffer_SetPriority(This->wineD3DVertexBuffer, PriorityNew);
139     wined3d_mutex_unlock();
140
141     return ret;
142 }
143
144 static DWORD WINAPI IDirect3DVertexBuffer8Impl_GetPriority(LPDIRECT3DVERTEXBUFFER8 iface) {
145     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
146     DWORD ret;
147     TRACE("(%p) Relay\n", This);
148
149     wined3d_mutex_lock();
150     ret = IWineD3DBuffer_GetPriority(This->wineD3DVertexBuffer);
151     wined3d_mutex_unlock();
152
153     return ret;
154 }
155
156 static void WINAPI IDirect3DVertexBuffer8Impl_PreLoad(LPDIRECT3DVERTEXBUFFER8 iface) {
157     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
158     TRACE("(%p) Relay\n", This);
159
160     wined3d_mutex_lock();
161     IWineD3DBuffer_PreLoad(This->wineD3DVertexBuffer);
162     wined3d_mutex_unlock();
163 }
164
165 static D3DRESOURCETYPE WINAPI IDirect3DVertexBuffer8Impl_GetType(LPDIRECT3DVERTEXBUFFER8 iface) {
166     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
167     TRACE("(%p)\n", This);
168
169     return D3DRTYPE_VERTEXBUFFER;
170 }
171
172 /* IDirect3DVertexBuffer8 Interface follow: */
173 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_Lock(LPDIRECT3DVERTEXBUFFER8 iface, UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags) {
174     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
175     HRESULT hr;
176     TRACE("(%p) Relay\n", This);
177
178     wined3d_mutex_lock();
179     hr = IWineD3DBuffer_Map(This->wineD3DVertexBuffer, OffsetToLock, SizeToLock, ppbData, Flags);
180     wined3d_mutex_unlock();
181
182     return hr;
183 }
184
185 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_Unlock(LPDIRECT3DVERTEXBUFFER8 iface) {
186     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
187     HRESULT hr;
188     TRACE("(%p) Relay\n", This);
189
190     wined3d_mutex_lock();
191     hr = IWineD3DBuffer_Unmap(This->wineD3DVertexBuffer);
192     wined3d_mutex_unlock();
193
194     return hr;
195 }
196
197 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_GetDesc(LPDIRECT3DVERTEXBUFFER8 iface, D3DVERTEXBUFFER_DESC *pDesc) {
198     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
199     HRESULT hr;
200     WINED3DBUFFER_DESC desc;
201     TRACE("(%p) Relay\n", This);
202
203     wined3d_mutex_lock();
204     hr = IWineD3DBuffer_GetDesc(This->wineD3DVertexBuffer, &desc);
205     wined3d_mutex_unlock();
206
207     if (SUCCEEDED(hr)) {
208         pDesc->Type = D3DRTYPE_VERTEXBUFFER;
209         pDesc->Usage = desc.Usage;
210         pDesc->Pool = desc.Pool;
211         pDesc->Size = desc.Size;
212         pDesc->FVF = This->fvf;
213         pDesc->Format = D3DFMT_VERTEXDATA;
214     }
215
216     return hr;
217 }
218
219 static const IDirect3DVertexBuffer8Vtbl Direct3DVertexBuffer8_Vtbl =
220 {
221     /* IUnknown */
222     IDirect3DVertexBuffer8Impl_QueryInterface,
223     IDirect3DVertexBuffer8Impl_AddRef,
224     IDirect3DVertexBuffer8Impl_Release,
225     /* IDirect3DResource8 */
226     IDirect3DVertexBuffer8Impl_GetDevice,
227     IDirect3DVertexBuffer8Impl_SetPrivateData,
228     IDirect3DVertexBuffer8Impl_GetPrivateData,
229     IDirect3DVertexBuffer8Impl_FreePrivateData,
230     IDirect3DVertexBuffer8Impl_SetPriority,
231     IDirect3DVertexBuffer8Impl_GetPriority,
232     IDirect3DVertexBuffer8Impl_PreLoad,
233     IDirect3DVertexBuffer8Impl_GetType,
234     /* IDirect3DVertexBuffer8 */
235     IDirect3DVertexBuffer8Impl_Lock,
236     IDirect3DVertexBuffer8Impl_Unlock,
237     IDirect3DVertexBuffer8Impl_GetDesc
238 };
239
240 static void STDMETHODCALLTYPE d3d8_vertexbuffer_wined3d_object_destroyed(void *parent)
241 {
242     HeapFree(GetProcessHeap(), 0, parent);
243 }
244
245 static const struct wined3d_parent_ops d3d8_vertexbuffer_wined3d_parent_ops =
246 {
247     d3d8_vertexbuffer_wined3d_object_destroyed,
248 };
249
250 HRESULT vertexbuffer_init(IDirect3DVertexBuffer8Impl *buffer, IDirect3DDevice8Impl *device,
251         UINT size, DWORD usage, DWORD fvf, D3DPOOL pool)
252 {
253     HRESULT hr;
254
255     buffer->lpVtbl = &Direct3DVertexBuffer8_Vtbl;
256     buffer->ref = 1;
257     buffer->fvf = fvf;
258
259     wined3d_mutex_lock();
260     hr = IWineD3DDevice_CreateVertexBuffer(device->WineD3DDevice, size,
261             usage & WINED3DUSAGE_MASK, 0, (WINED3DPOOL)pool, &buffer->wineD3DVertexBuffer,
262             (IUnknown *)buffer, &d3d8_vertexbuffer_wined3d_parent_ops);
263     wined3d_mutex_unlock();
264     if (FAILED(hr))
265     {
266         WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
267         return hr;
268     }
269
270     buffer->parentDevice = (IDirect3DDevice8 *)device;
271     IUnknown_AddRef(buffer->parentDevice);
272
273     return D3D_OK;
274 }