mshtml: Improved tracing of nsACString arguments.
[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     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
33
34     if (IsEqualGUID(riid, &IID_IUnknown)
35         || IsEqualGUID(riid, &IID_IDirect3DResource9)
36         || IsEqualGUID(riid, &IID_IDirect3DVertexBuffer9)) {
37         IDirect3DVertexBuffer9_AddRef(iface);
38         *ppobj = This;
39         return S_OK;
40     }
41
42     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
43     *ppobj = NULL;
44     return E_NOINTERFACE;
45 }
46
47 static ULONG WINAPI IDirect3DVertexBuffer9Impl_AddRef(LPDIRECT3DVERTEXBUFFER9 iface) {
48     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
49     ULONG ref = InterlockedIncrement(&This->ref);
50
51     TRACE("%p increasing refcount to %u.\n", iface, ref);
52
53     if (ref == 1)
54     {
55         IDirect3DDevice9Ex_AddRef(This->parentDevice);
56         wined3d_mutex_lock();
57         IWineD3DBuffer_AddRef(This->wineD3DVertexBuffer);
58         wined3d_mutex_unlock();
59     }
60
61     return ref;
62 }
63
64 static ULONG WINAPI IDirect3DVertexBuffer9Impl_Release(LPDIRECT3DVERTEXBUFFER9 iface) {
65     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
66     ULONG ref = InterlockedDecrement(&This->ref);
67
68     TRACE("%p decreasing refcount to %u.\n", iface, ref);
69
70     if (ref == 0) {
71         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
72
73         wined3d_mutex_lock();
74         IWineD3DBuffer_Release(This->wineD3DVertexBuffer);
75         wined3d_mutex_unlock();
76
77         /* Release the device last, as it may cause the device to be destroyed. */
78         IDirect3DDevice9Ex_Release(parentDevice);
79     }
80     return ref;
81 }
82
83 /* IDirect3DVertexBuffer9 IDirect3DResource9 Interface follow: */
84 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetDevice(IDirect3DVertexBuffer9 *iface, IDirect3DDevice9 **device)
85 {
86     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
87
88     TRACE("iface %p, device %p.\n", iface, device);
89
90     *device = (IDirect3DDevice9 *)This->parentDevice;
91     IDirect3DDevice9_AddRef(*device);
92
93     TRACE("Returning device %p.\n", *device);
94
95     return D3D_OK;
96 }
97
98 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_SetPrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
99     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)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 IDirect3DVertexBuffer9Impl_GetPrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
113     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)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 IDirect3DVertexBuffer9Impl_FreePrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid) {
127     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)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 IDirect3DVertexBuffer9Impl_SetPriority(LPDIRECT3DVERTEXBUFFER9 iface, DWORD PriorityNew) {
140     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
141     HRESULT hr;
142
143     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
144
145     wined3d_mutex_lock();
146     hr = IWineD3DBuffer_SetPriority(This->wineD3DVertexBuffer, PriorityNew);
147     wined3d_mutex_unlock();
148
149     return hr;
150 }
151
152 static DWORD WINAPI IDirect3DVertexBuffer9Impl_GetPriority(LPDIRECT3DVERTEXBUFFER9 iface) {
153     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
154     HRESULT hr;
155
156     TRACE("iface %p.\n", iface);
157
158     wined3d_mutex_lock();
159     hr = IWineD3DBuffer_GetPriority(This->wineD3DVertexBuffer);
160     wined3d_mutex_unlock();
161
162     return hr;
163 }
164
165 static void WINAPI IDirect3DVertexBuffer9Impl_PreLoad(LPDIRECT3DVERTEXBUFFER9 iface) {
166     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)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 IDirect3DVertexBuffer9Impl_GetType(IDirect3DVertexBuffer9 *iface)
176 {
177     TRACE("iface %p.\n", iface);
178
179     return D3DRTYPE_VERTEXBUFFER;
180 }
181
182 /* IDirect3DVertexBuffer9 Interface follow: */
183 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_Lock(LPDIRECT3DVERTEXBUFFER9 iface, UINT OffsetToLock, UINT SizeToLock, void** ppbData, DWORD Flags) {
184     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)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, (BYTE **)ppbData, Flags);
192     wined3d_mutex_unlock();
193
194     return hr;
195 }
196
197 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_Unlock(LPDIRECT3DVERTEXBUFFER9 iface) {
198     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)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 IDirect3DVertexBuffer9Impl_GetDesc(LPDIRECT3DVERTEXBUFFER9 iface, D3DVERTEXBUFFER_DESC* pDesc) {
211     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)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->Format = D3DFMT_VERTEXDATA;
223         pDesc->Usage = desc.Usage;
224         pDesc->Pool = desc.Pool;
225         pDesc->Size = desc.Size;
226         pDesc->Type = D3DRTYPE_VERTEXBUFFER;
227         pDesc->FVF = This->fvf;
228     }
229
230
231     return hr;
232 }
233
234 static const IDirect3DVertexBuffer9Vtbl Direct3DVertexBuffer9_Vtbl =
235 {
236     /* IUnknown */
237     IDirect3DVertexBuffer9Impl_QueryInterface,
238     IDirect3DVertexBuffer9Impl_AddRef,
239     IDirect3DVertexBuffer9Impl_Release,
240     /* IDirect3DResource9 */
241     IDirect3DVertexBuffer9Impl_GetDevice,
242     IDirect3DVertexBuffer9Impl_SetPrivateData,
243     IDirect3DVertexBuffer9Impl_GetPrivateData,
244     IDirect3DVertexBuffer9Impl_FreePrivateData,
245     IDirect3DVertexBuffer9Impl_SetPriority,
246     IDirect3DVertexBuffer9Impl_GetPriority,
247     IDirect3DVertexBuffer9Impl_PreLoad,
248     IDirect3DVertexBuffer9Impl_GetType,
249     /* IDirect3DVertexBuffer9 */
250     IDirect3DVertexBuffer9Impl_Lock,
251     IDirect3DVertexBuffer9Impl_Unlock,
252     IDirect3DVertexBuffer9Impl_GetDesc
253 };
254
255 static void STDMETHODCALLTYPE d3d9_vertexbuffer_wined3d_object_destroyed(void *parent)
256 {
257     HeapFree(GetProcessHeap(), 0, parent);
258 }
259
260 static const struct wined3d_parent_ops d3d9_vertexbuffer_wined3d_parent_ops =
261 {
262     d3d9_vertexbuffer_wined3d_object_destroyed,
263 };
264
265 HRESULT vertexbuffer_init(IDirect3DVertexBuffer9Impl *buffer, IDirect3DDevice9Impl *device,
266         UINT size, UINT usage, DWORD fvf, D3DPOOL pool)
267 {
268     HRESULT hr;
269
270     buffer->lpVtbl = &Direct3DVertexBuffer9_Vtbl;
271     buffer->ref = 1;
272     buffer->fvf = fvf;
273
274     wined3d_mutex_lock();
275     hr = IWineD3DDevice_CreateVertexBuffer(device->WineD3DDevice, size,
276             usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, &buffer->wineD3DVertexBuffer,
277             (IUnknown *)buffer, &d3d9_vertexbuffer_wined3d_parent_ops);
278     wined3d_mutex_unlock();
279     if (FAILED(hr))
280     {
281         WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
282         return hr;
283     }
284
285     buffer->parentDevice = (IDirect3DDevice9Ex *)device;
286     IDirect3DDevice9Ex_AddRef(buffer->parentDevice);
287
288     return D3D_OK;
289 }