comctl32: Fix compiler warnings with flag -Wcast-qual.
[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     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DResource8)
34         || IsEqualGUID(riid, &IID_IDirect3DVertexBuffer8)) {
35         IUnknown_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
42     *ppobj = NULL;
43     return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI IDirect3DVertexBuffer8Impl_AddRef(LPDIRECT3DVERTEXBUFFER8 iface) {
47     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
48     ULONG ref = InterlockedIncrement(&This->ref);
49
50     TRACE("%p increasing refcount to %u.\n", iface, ref);
51
52     if (ref == 1)
53     {
54         IDirect3DDevice8_AddRef(This->parentDevice);
55         wined3d_mutex_lock();
56         IWineD3DBuffer_AddRef(This->wineD3DVertexBuffer);
57         wined3d_mutex_unlock();
58     }
59
60     return ref;
61 }
62
63 static ULONG WINAPI IDirect3DVertexBuffer8Impl_Release(LPDIRECT3DVERTEXBUFFER8 iface) {
64     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
65     ULONG ref = InterlockedDecrement(&This->ref);
66
67     TRACE("%p decreasing refcount to %u.\n", iface, ref);
68
69     if (ref == 0) {
70         IDirect3DDevice8 *parentDevice = This->parentDevice;
71
72         wined3d_mutex_lock();
73         IWineD3DBuffer_Release(This->wineD3DVertexBuffer);
74         wined3d_mutex_unlock();
75
76         /* Release the device last, as it may cause the device to be destroyed. */
77         IDirect3DDevice8_Release(parentDevice);
78     }
79
80     return ref;
81 }
82
83 /* IDirect3DVertexBuffer8 IDirect3DResource8 Interface follow: */
84 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_GetDevice(IDirect3DVertexBuffer8 *iface, IDirect3DDevice8 **device)
85 {
86     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
87
88     TRACE("iface %p, device %p.\n", iface, device);
89
90     *device = (IDirect3DDevice8 *)This->parentDevice;
91     IDirect3DDevice8_AddRef(*device);
92
93     TRACE("Returning device %p.\n", *device);
94
95     return D3D_OK;
96 }
97
98 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_SetPrivateData(LPDIRECT3DVERTEXBUFFER8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
99     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
100     HRESULT hr;
101
102     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
103             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
104
105     wined3d_mutex_lock();
106     hr = IWineD3DBuffer_SetPrivateData(This->wineD3DVertexBuffer, refguid, pData, SizeOfData, Flags);
107     wined3d_mutex_unlock();
108
109     return hr;
110 }
111
112 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_GetPrivateData(LPDIRECT3DVERTEXBUFFER8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
113     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
114     HRESULT hr;
115
116     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
117             iface, debugstr_guid(refguid), pData, pSizeOfData);
118
119     wined3d_mutex_lock();
120     hr = IWineD3DBuffer_GetPrivateData(This->wineD3DVertexBuffer, refguid, pData, pSizeOfData);
121     wined3d_mutex_unlock();
122
123     return hr;
124 }
125
126 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_FreePrivateData(LPDIRECT3DVERTEXBUFFER8 iface, REFGUID refguid) {
127     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
128     HRESULT hr;
129
130     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
131
132     wined3d_mutex_lock();
133     hr = IWineD3DBuffer_FreePrivateData(This->wineD3DVertexBuffer, refguid);
134     wined3d_mutex_unlock();
135
136     return hr;
137 }
138
139 static DWORD WINAPI IDirect3DVertexBuffer8Impl_SetPriority(LPDIRECT3DVERTEXBUFFER8 iface, DWORD PriorityNew) {
140     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
141     DWORD ret;
142
143     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
144
145     wined3d_mutex_lock();
146     ret = IWineD3DBuffer_SetPriority(This->wineD3DVertexBuffer, PriorityNew);
147     wined3d_mutex_unlock();
148
149     return ret;
150 }
151
152 static DWORD WINAPI IDirect3DVertexBuffer8Impl_GetPriority(LPDIRECT3DVERTEXBUFFER8 iface) {
153     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
154     DWORD ret;
155
156     TRACE("iface %p.\n", iface);
157
158     wined3d_mutex_lock();
159     ret = IWineD3DBuffer_GetPriority(This->wineD3DVertexBuffer);
160     wined3d_mutex_unlock();
161
162     return ret;
163 }
164
165 static void WINAPI IDirect3DVertexBuffer8Impl_PreLoad(LPDIRECT3DVERTEXBUFFER8 iface) {
166     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
167
168     TRACE("iface %p.\n", iface);
169
170     wined3d_mutex_lock();
171     IWineD3DBuffer_PreLoad(This->wineD3DVertexBuffer);
172     wined3d_mutex_unlock();
173 }
174
175 static D3DRESOURCETYPE WINAPI IDirect3DVertexBuffer8Impl_GetType(IDirect3DVertexBuffer8 *iface)
176 {
177     TRACE("iface %p.\n", iface);
178
179     return D3DRTYPE_VERTEXBUFFER;
180 }
181
182 /* IDirect3DVertexBuffer8 Interface follow: */
183 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_Lock(LPDIRECT3DVERTEXBUFFER8 iface, UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags) {
184     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
185     HRESULT hr;
186
187     TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
188             iface, OffsetToLock, SizeToLock, ppbData, Flags);
189
190     wined3d_mutex_lock();
191     hr = IWineD3DBuffer_Map(This->wineD3DVertexBuffer, OffsetToLock, SizeToLock, ppbData, Flags);
192     wined3d_mutex_unlock();
193
194     return hr;
195 }
196
197 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_Unlock(LPDIRECT3DVERTEXBUFFER8 iface) {
198     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
199     HRESULT hr;
200
201     TRACE("iface %p.\n", iface);
202
203     wined3d_mutex_lock();
204     hr = IWineD3DBuffer_Unmap(This->wineD3DVertexBuffer);
205     wined3d_mutex_unlock();
206
207     return hr;
208 }
209
210 static HRESULT WINAPI IDirect3DVertexBuffer8Impl_GetDesc(LPDIRECT3DVERTEXBUFFER8 iface, D3DVERTEXBUFFER_DESC *pDesc) {
211     IDirect3DVertexBuffer8Impl *This = (IDirect3DVertexBuffer8Impl *)iface;
212     HRESULT hr;
213     WINED3DBUFFER_DESC desc;
214
215     TRACE("iface %p, desc %p.\n", iface, pDesc);
216
217     wined3d_mutex_lock();
218     hr = IWineD3DBuffer_GetDesc(This->wineD3DVertexBuffer, &desc);
219     wined3d_mutex_unlock();
220
221     if (SUCCEEDED(hr)) {
222         pDesc->Type = D3DRTYPE_VERTEXBUFFER;
223         pDesc->Usage = desc.Usage;
224         pDesc->Pool = desc.Pool;
225         pDesc->Size = desc.Size;
226         pDesc->FVF = This->fvf;
227         pDesc->Format = D3DFMT_VERTEXDATA;
228     }
229
230     return hr;
231 }
232
233 static const IDirect3DVertexBuffer8Vtbl Direct3DVertexBuffer8_Vtbl =
234 {
235     /* IUnknown */
236     IDirect3DVertexBuffer8Impl_QueryInterface,
237     IDirect3DVertexBuffer8Impl_AddRef,
238     IDirect3DVertexBuffer8Impl_Release,
239     /* IDirect3DResource8 */
240     IDirect3DVertexBuffer8Impl_GetDevice,
241     IDirect3DVertexBuffer8Impl_SetPrivateData,
242     IDirect3DVertexBuffer8Impl_GetPrivateData,
243     IDirect3DVertexBuffer8Impl_FreePrivateData,
244     IDirect3DVertexBuffer8Impl_SetPriority,
245     IDirect3DVertexBuffer8Impl_GetPriority,
246     IDirect3DVertexBuffer8Impl_PreLoad,
247     IDirect3DVertexBuffer8Impl_GetType,
248     /* IDirect3DVertexBuffer8 */
249     IDirect3DVertexBuffer8Impl_Lock,
250     IDirect3DVertexBuffer8Impl_Unlock,
251     IDirect3DVertexBuffer8Impl_GetDesc
252 };
253
254 static void STDMETHODCALLTYPE d3d8_vertexbuffer_wined3d_object_destroyed(void *parent)
255 {
256     HeapFree(GetProcessHeap(), 0, parent);
257 }
258
259 static const struct wined3d_parent_ops d3d8_vertexbuffer_wined3d_parent_ops =
260 {
261     d3d8_vertexbuffer_wined3d_object_destroyed,
262 };
263
264 HRESULT vertexbuffer_init(IDirect3DVertexBuffer8Impl *buffer, IDirect3DDevice8Impl *device,
265         UINT size, DWORD usage, DWORD fvf, D3DPOOL pool)
266 {
267     HRESULT hr;
268
269     buffer->lpVtbl = &Direct3DVertexBuffer8_Vtbl;
270     buffer->ref = 1;
271     buffer->fvf = fvf;
272
273     wined3d_mutex_lock();
274     hr = IWineD3DDevice_CreateVertexBuffer(device->WineD3DDevice, size,
275             usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, &buffer->wineD3DVertexBuffer,
276             (IUnknown *)buffer, &d3d8_vertexbuffer_wined3d_parent_ops);
277     wined3d_mutex_unlock();
278     if (FAILED(hr))
279     {
280         WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
281         return hr;
282     }
283
284     buffer->parentDevice = (IDirect3DDevice8 *)device;
285     IUnknown_AddRef(buffer->parentDevice);
286
287     return D3D_OK;
288 }