comctl32/datetime: Use SDK defined class name instead of another local constant.
[wine] / dlls / d3d9 / vertexbuffer.c
1 /*
2  * IDirect3DVertexBuffer9 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2002-2004 Raphael Junqueira
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 /* IDirect3DVertexBuffer9 IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_QueryInterface(LPDIRECT3DVERTEXBUFFER9 iface, REFIID riid, LPVOID* ppobj) {
30     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DResource9)
34         || IsEqualGUID(riid, &IID_IDirect3DVertexBuffer9)) {
35         IDirect3DVertexBuffer9_AddRef(iface);
36         *ppobj = This;
37         return S_OK;
38     }
39
40     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41     *ppobj = NULL;
42     return E_NOINTERFACE;
43 }
44
45 static ULONG WINAPI IDirect3DVertexBuffer9Impl_AddRef(LPDIRECT3DVERTEXBUFFER9 iface) {
46     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
47     ULONG ref = InterlockedIncrement(&This->ref);
48
49     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
50
51     if (ref == 1)
52     {
53         IDirect3DDevice9Ex_AddRef(This->parentDevice);
54         wined3d_mutex_lock();
55         IWineD3DBuffer_AddRef(This->wineD3DVertexBuffer);
56         wined3d_mutex_unlock();
57     }
58
59     return ref;
60 }
61
62 static ULONG WINAPI IDirect3DVertexBuffer9Impl_Release(LPDIRECT3DVERTEXBUFFER9 iface) {
63     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
64     ULONG ref = InterlockedDecrement(&This->ref);
65
66     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
67
68     if (ref == 0) {
69         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
70
71         wined3d_mutex_lock();
72         IWineD3DBuffer_Release(This->wineD3DVertexBuffer);
73         wined3d_mutex_unlock();
74
75         /* Release the device last, as it may cause the device to be destroyed. */
76         IDirect3DDevice9Ex_Release(parentDevice);
77     }
78     return ref;
79 }
80
81 /* IDirect3DVertexBuffer9 IDirect3DResource9 Interface follow: */
82 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetDevice(LPDIRECT3DVERTEXBUFFER9 iface, IDirect3DDevice9** ppDevice) {
83     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
84     IWineD3DDevice *wined3d_device;
85     HRESULT hr;
86     TRACE("(%p) Relay\n", This);
87
88     wined3d_mutex_lock();
89     hr = IWineD3DBuffer_GetDevice(This->wineD3DVertexBuffer, &wined3d_device);
90     if (SUCCEEDED(hr))
91     {
92         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
93         IWineD3DDevice_Release(wined3d_device);
94     }
95     wined3d_mutex_unlock();
96
97     return hr;
98 }
99
100 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_SetPrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
101     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
102     HRESULT hr;
103
104     TRACE("(%p) Relay\n", This);
105
106     wined3d_mutex_lock();
107     hr = IWineD3DBuffer_SetPrivateData(This->wineD3DVertexBuffer, refguid, pData, SizeOfData, Flags);
108     wined3d_mutex_unlock();
109
110     return hr;
111 }
112
113 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetPrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
114     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
115     HRESULT hr;
116     TRACE("(%p) Relay\n", This);
117
118     wined3d_mutex_lock();
119     hr = IWineD3DBuffer_GetPrivateData(This->wineD3DVertexBuffer, refguid, pData, pSizeOfData);
120     wined3d_mutex_unlock();
121
122     return hr;
123 }
124
125 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_FreePrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid) {
126     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
127     HRESULT hr;
128     TRACE("(%p) Relay\n", This);
129
130     wined3d_mutex_lock();
131     hr = IWineD3DBuffer_FreePrivateData(This->wineD3DVertexBuffer, refguid);
132     wined3d_mutex_unlock();
133
134     return hr;
135 }
136
137 static DWORD WINAPI IDirect3DVertexBuffer9Impl_SetPriority(LPDIRECT3DVERTEXBUFFER9 iface, DWORD PriorityNew) {
138     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
139     HRESULT hr;
140     TRACE("(%p) Relay\n", This);
141
142     wined3d_mutex_lock();
143     hr = IWineD3DBuffer_SetPriority(This->wineD3DVertexBuffer, PriorityNew);
144     wined3d_mutex_unlock();
145
146     return hr;
147 }
148
149 static DWORD WINAPI IDirect3DVertexBuffer9Impl_GetPriority(LPDIRECT3DVERTEXBUFFER9 iface) {
150     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
151     HRESULT hr;
152     TRACE("(%p) Relay\n", This);
153
154     wined3d_mutex_lock();
155     hr = IWineD3DBuffer_GetPriority(This->wineD3DVertexBuffer);
156     wined3d_mutex_unlock();
157
158     return hr;
159 }
160
161 static void WINAPI IDirect3DVertexBuffer9Impl_PreLoad(LPDIRECT3DVERTEXBUFFER9 iface) {
162     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
163     TRACE("(%p) Relay\n", This);
164
165     wined3d_mutex_lock();
166     IWineD3DBuffer_PreLoad(This->wineD3DVertexBuffer);
167     wined3d_mutex_unlock();
168 }
169
170 static D3DRESOURCETYPE WINAPI IDirect3DVertexBuffer9Impl_GetType(LPDIRECT3DVERTEXBUFFER9 iface) {
171     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
172     TRACE("(%p)\n", This);
173
174     return D3DRTYPE_VERTEXBUFFER;
175 }
176
177 /* IDirect3DVertexBuffer9 Interface follow: */
178 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_Lock(LPDIRECT3DVERTEXBUFFER9 iface, UINT OffsetToLock, UINT SizeToLock, void** ppbData, DWORD Flags) {
179     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
180     HRESULT hr;
181     TRACE("(%p) Relay\n", This);
182
183     wined3d_mutex_lock();
184     hr = IWineD3DBuffer_Map(This->wineD3DVertexBuffer, OffsetToLock, SizeToLock, (BYTE **)ppbData, Flags);
185     wined3d_mutex_unlock();
186
187     return hr;
188 }
189
190 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_Unlock(LPDIRECT3DVERTEXBUFFER9 iface) {
191     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
192     HRESULT hr;
193     TRACE("(%p) Relay\n", This);
194
195     wined3d_mutex_lock();
196     hr = IWineD3DBuffer_Unmap(This->wineD3DVertexBuffer);
197     wined3d_mutex_unlock();
198
199     return hr;
200 }
201
202 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetDesc(LPDIRECT3DVERTEXBUFFER9 iface, D3DVERTEXBUFFER_DESC* pDesc) {
203     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
204     HRESULT hr;
205     WINED3DBUFFER_DESC desc;
206     TRACE("(%p) Relay\n", This);
207
208     wined3d_mutex_lock();
209     hr = IWineD3DBuffer_GetDesc(This->wineD3DVertexBuffer, &desc);
210     wined3d_mutex_unlock();
211
212     if (SUCCEEDED(hr)) {
213         pDesc->Format = D3DFMT_VERTEXDATA;
214         pDesc->Usage = desc.Usage;
215         pDesc->Pool = desc.Pool;
216         pDesc->Size = desc.Size;
217         pDesc->Type = D3DRTYPE_VERTEXBUFFER;
218         pDesc->FVF = This->fvf;
219     }
220
221
222     return hr;
223 }
224
225 static const IDirect3DVertexBuffer9Vtbl Direct3DVertexBuffer9_Vtbl =
226 {
227     /* IUnknown */
228     IDirect3DVertexBuffer9Impl_QueryInterface,
229     IDirect3DVertexBuffer9Impl_AddRef,
230     IDirect3DVertexBuffer9Impl_Release,
231     /* IDirect3DResource9 */
232     IDirect3DVertexBuffer9Impl_GetDevice,
233     IDirect3DVertexBuffer9Impl_SetPrivateData,
234     IDirect3DVertexBuffer9Impl_GetPrivateData,
235     IDirect3DVertexBuffer9Impl_FreePrivateData,
236     IDirect3DVertexBuffer9Impl_SetPriority,
237     IDirect3DVertexBuffer9Impl_GetPriority,
238     IDirect3DVertexBuffer9Impl_PreLoad,
239     IDirect3DVertexBuffer9Impl_GetType,
240     /* IDirect3DVertexBuffer9 */
241     IDirect3DVertexBuffer9Impl_Lock,
242     IDirect3DVertexBuffer9Impl_Unlock,
243     IDirect3DVertexBuffer9Impl_GetDesc
244 };
245
246 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
247 {
248     HeapFree(GetProcessHeap(), 0, parent);
249 }
250
251 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
252 {
253     d3d9_vertexbuffer_wined3d_object_destroyed,
254 };
255
256 HRESULT vertexbuffer_init(IDirect3DVertexBuffer9Impl *buffer, IDirect3DDevice9Impl *device,
257         UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
258 {
259     HRESULT hr;
260
261     buffer->lpVtbl = &Direct3DVertexBuffer9_Vtbl;
262     buffer->ref = 1;
263     buffer->fvf = fvf;
264
265     wined3d_mutex_lock();
266     hr = IWineD3DDevice_CreateVertexBuffer(device->WineD3DDevice, size,
267             usage & WINED3DUSAGE_MASK, 0, (WINED3DPOOL)pool, &buffer->wineD3DVertexBuffer,
268             (IUnknown *)buffer, &d3d9_vertexbuffer_wined3d_parent_ops);
269     wined3d_mutex_unlock();
270     if (FAILED(hr))
271     {
272         WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
273         return hr;
274     }
275
276     buffer->parentDevice = (IDirect3DDevice9Ex *)device;
277     IDirect3DDevice9Ex_AddRef(buffer->parentDevice);
278
279     return D3D_OK;
280 }