richedit: Moved optional message loop in tests to its own function.
[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         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     *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     return ref;
52 }
53
54 static ULONG WINAPI IDirect3DVertexBuffer9Impl_Release(LPDIRECT3DVERTEXBUFFER9 iface) {
55     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
56     ULONG ref = InterlockedDecrement(&This->ref);
57
58     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
59
60     if (ref == 0) {
61         EnterCriticalSection(&d3d9_cs);
62         IWineD3DVertexBuffer_Release(This->wineD3DVertexBuffer);
63         LeaveCriticalSection(&d3d9_cs);
64         IUnknown_Release(This->parentDevice);
65         HeapFree(GetProcessHeap(), 0, This);
66     }
67     return ref;
68 }
69
70 /* IDirect3DVertexBuffer9 IDirect3DResource9 Interface follow: */
71 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetDevice(LPDIRECT3DVERTEXBUFFER9 iface, IDirect3DDevice9** ppDevice) {
72     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
73     HRESULT hr;
74     TRACE("(%p) Relay\n", This);
75
76     EnterCriticalSection(&d3d9_cs);
77     hr = IDirect3DResource9Impl_GetDevice((LPDIRECT3DRESOURCE9) This, ppDevice);
78     LeaveCriticalSection(&d3d9_cs);
79     return hr;
80 }
81
82 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_SetPrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
83     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
84     TRACE("(%p) Relay\n", This);
85     return IWineD3DVertexBuffer_SetPrivateData(This->wineD3DVertexBuffer, refguid, pData, SizeOfData, Flags);
86 }
87
88 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetPrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
89     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
90     HRESULT hr;
91     TRACE("(%p) Relay\n", This);
92
93     EnterCriticalSection(&d3d9_cs);
94     hr = IWineD3DVertexBuffer_GetPrivateData(This->wineD3DVertexBuffer, refguid, pData, pSizeOfData);
95     LeaveCriticalSection(&d3d9_cs);
96     return hr;
97 }
98
99 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_FreePrivateData(LPDIRECT3DVERTEXBUFFER9 iface, REFGUID refguid) {
100     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
101     HRESULT hr;
102     TRACE("(%p) Relay\n", This);
103
104     EnterCriticalSection(&d3d9_cs);
105     hr = IWineD3DVertexBuffer_FreePrivateData(This->wineD3DVertexBuffer, refguid);
106     LeaveCriticalSection(&d3d9_cs);
107     return hr;
108 }
109
110 static DWORD WINAPI IDirect3DVertexBuffer9Impl_SetPriority(LPDIRECT3DVERTEXBUFFER9 iface, DWORD PriorityNew) {
111     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
112     HRESULT hr;
113     TRACE("(%p) Relay\n", This);
114
115     EnterCriticalSection(&d3d9_cs);
116     hr = IWineD3DVertexBuffer_SetPriority(This->wineD3DVertexBuffer, PriorityNew);
117     LeaveCriticalSection(&d3d9_cs);
118     return hr;
119 }
120
121 static DWORD WINAPI IDirect3DVertexBuffer9Impl_GetPriority(LPDIRECT3DVERTEXBUFFER9 iface) {
122     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
123     HRESULT hr;
124     TRACE("(%p) Relay\n", This);
125
126     EnterCriticalSection(&d3d9_cs);
127     hr = IWineD3DVertexBuffer_GetPriority(This->wineD3DVertexBuffer);
128     LeaveCriticalSection(&d3d9_cs);
129     return hr;
130 }
131
132 static void WINAPI IDirect3DVertexBuffer9Impl_PreLoad(LPDIRECT3DVERTEXBUFFER9 iface) {
133     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
134     TRACE("(%p) Relay\n", This);
135
136     EnterCriticalSection(&d3d9_cs);
137     IWineD3DVertexBuffer_PreLoad(This->wineD3DVertexBuffer);
138     LeaveCriticalSection(&d3d9_cs);
139     return ;
140 }
141
142 static D3DRESOURCETYPE WINAPI IDirect3DVertexBuffer9Impl_GetType(LPDIRECT3DVERTEXBUFFER9 iface) {
143     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
144     D3DRESOURCETYPE ret;
145     TRACE("(%p) Relay\n", This);
146
147     EnterCriticalSection(&d3d9_cs);
148     ret = IWineD3DVertexBuffer_GetType(This->wineD3DVertexBuffer);
149     LeaveCriticalSection(&d3d9_cs);
150     return ret;
151 }
152
153 /* IDirect3DVertexBuffer9 Interface follow: */
154 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_Lock(LPDIRECT3DVERTEXBUFFER9 iface, UINT OffsetToLock, UINT SizeToLock, void** ppbData, DWORD Flags) {
155     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
156     HRESULT hr;
157     TRACE("(%p) Relay\n", This);
158
159     EnterCriticalSection(&d3d9_cs);
160     hr = IWineD3DVertexBuffer_Lock(This->wineD3DVertexBuffer, OffsetToLock, SizeToLock, (BYTE **)ppbData, Flags);
161     LeaveCriticalSection(&d3d9_cs);
162     return hr;
163 }
164
165 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_Unlock(LPDIRECT3DVERTEXBUFFER9 iface) {
166     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
167     HRESULT hr;
168     TRACE("(%p) Relay\n", This);
169
170     EnterCriticalSection(&d3d9_cs);
171     hr = IWineD3DVertexBuffer_Unlock(This->wineD3DVertexBuffer);
172     LeaveCriticalSection(&d3d9_cs);
173     return hr;
174 }
175
176 static HRESULT WINAPI IDirect3DVertexBuffer9Impl_GetDesc(LPDIRECT3DVERTEXBUFFER9 iface, D3DVERTEXBUFFER_DESC* pDesc) {
177     IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl *)iface;
178     HRESULT hr;
179     TRACE("(%p) Relay\n", This);
180
181     EnterCriticalSection(&d3d9_cs);
182     hr = IWineD3DVertexBuffer_GetDesc(This->wineD3DVertexBuffer, (WINED3DVERTEXBUFFER_DESC *) pDesc);
183     LeaveCriticalSection(&d3d9_cs);
184     return hr;
185 }
186
187 static const IDirect3DVertexBuffer9Vtbl Direct3DVertexBuffer9_Vtbl =
188 {
189     /* IUnknown */
190     IDirect3DVertexBuffer9Impl_QueryInterface,
191     IDirect3DVertexBuffer9Impl_AddRef,
192     IDirect3DVertexBuffer9Impl_Release,
193     /* IDirect3DResource9 */
194     IDirect3DVertexBuffer9Impl_GetDevice,
195     IDirect3DVertexBuffer9Impl_SetPrivateData,
196     IDirect3DVertexBuffer9Impl_GetPrivateData,
197     IDirect3DVertexBuffer9Impl_FreePrivateData,
198     IDirect3DVertexBuffer9Impl_SetPriority,
199     IDirect3DVertexBuffer9Impl_GetPriority,
200     IDirect3DVertexBuffer9Impl_PreLoad,
201     IDirect3DVertexBuffer9Impl_GetType,
202     /* IDirect3DVertexBuffer9 */
203     IDirect3DVertexBuffer9Impl_Lock,
204     IDirect3DVertexBuffer9Impl_Unlock,
205     IDirect3DVertexBuffer9Impl_GetDesc
206 };
207
208
209 /* IDirect3DDevice9 IDirect3DVertexBuffer9 Methods follow: */
210 HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexBuffer(LPDIRECT3DDEVICE9EX iface,
211                                 UINT Size, DWORD Usage, DWORD FVF, D3DPOOL Pool,
212                                 IDirect3DVertexBuffer9** ppVertexBuffer, HANDLE* pSharedHandle) {
213     
214     IDirect3DVertexBuffer9Impl *object;
215     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
216     HRESULT hrc = D3D_OK;
217
218     /* Allocate the storage for the device */
219     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexBuffer9Impl));
220     if (NULL == object) {
221         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
222         return D3DERR_OUTOFVIDEOMEMORY;
223     }
224
225     object->lpVtbl = &Direct3DVertexBuffer9_Vtbl;
226     object->ref = 1;
227     EnterCriticalSection(&d3d9_cs);
228     hrc = IWineD3DDevice_CreateVertexBuffer(This->WineD3DDevice, Size, Usage & WINED3DUSAGE_MASK, FVF, (WINED3DPOOL) Pool, &(object->wineD3DVertexBuffer), pSharedHandle, (IUnknown *)object);
229     LeaveCriticalSection(&d3d9_cs);
230     
231     if (hrc != D3D_OK) {
232
233         /* free up object */
234         WARN("(%p) call to IWineD3DDevice_CreateVertexBuffer failed\n", This);
235         HeapFree(GetProcessHeap(), 0, object);
236     } else {
237         IUnknown_AddRef(iface);
238         object->parentDevice = iface;
239         TRACE("(%p) : Created vertex buffer %p\n", This, object);
240         *ppVertexBuffer = (LPDIRECT3DVERTEXBUFFER9) object;
241     }
242     return hrc;
243 }