d3d8: Removed superflous NULL checks (Coverity).
[wine] / dlls / d3d8 / indexbuffer.c
1 /*
2  * IDirect3DIndexBuffer8 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 /* IDirect3DIndexBuffer8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_QueryInterface(LPDIRECT3DINDEXBUFFER8 iface, REFIID riid, LPVOID *ppobj) {
28     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)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_IDirect3DIndexBuffer8)) {
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 IDirect3DIndexBuffer8Impl_AddRef(LPDIRECT3DINDEXBUFFER8 iface) {
46     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
47     ULONG ref = InterlockedIncrement(&This->ref);
48
49     TRACE("%p increasing refcount to %u.\n", iface, ref);
50
51     if (ref == 1)
52     {
53         IDirect3DDevice8_AddRef(This->parentDevice);
54         wined3d_mutex_lock();
55         IWineD3DBuffer_AddRef(This->wineD3DIndexBuffer);
56         wined3d_mutex_unlock();
57     }
58
59     return ref;
60 }
61
62 static ULONG WINAPI IDirect3DIndexBuffer8Impl_Release(LPDIRECT3DINDEXBUFFER8 iface) {
63     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
64     ULONG ref = InterlockedDecrement(&This->ref);
65
66     TRACE("%p decreasing refcount to %u.\n", iface, ref);
67
68     if (ref == 0) {
69         IDirect3DDevice8 *parentDevice = This->parentDevice;
70
71         wined3d_mutex_lock();
72         IWineD3DBuffer_Release(This->wineD3DIndexBuffer);
73         wined3d_mutex_unlock();
74
75         /* Release the device last, as it may cause the device to be destroyed. */
76         IDirect3DDevice8_Release(parentDevice);
77     }
78     return ref;
79 }
80
81 /* IDirect3DIndexBuffer8 IDirect3DResource8 Interface follow: */
82 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetDevice(LPDIRECT3DINDEXBUFFER8 iface, IDirect3DDevice8 **ppDevice) {
83     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
84     IWineD3DDevice *wined3d_device;
85     HRESULT hr;
86
87     TRACE("iface %p, device %p.\n", iface, ppDevice);
88
89     wined3d_mutex_lock();
90     hr = IWineD3DBuffer_GetDevice(This->wineD3DIndexBuffer, &wined3d_device);
91     if (SUCCEEDED(hr))
92     {
93         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
94         IWineD3DDevice_Release(wined3d_device);
95     }
96     wined3d_mutex_unlock();
97
98     return hr;
99 }
100
101 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_SetPrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
102     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
103     HRESULT hr;
104
105     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
106             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
107
108     wined3d_mutex_lock();
109     hr = IWineD3DBuffer_SetPrivateData(This->wineD3DIndexBuffer, refguid, pData, SizeOfData, Flags);
110     wined3d_mutex_unlock();
111
112     return hr;
113 }
114
115 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetPrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
116     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
117     HRESULT hr;
118
119     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
120             iface, debugstr_guid(refguid), pData, pSizeOfData);
121
122     wined3d_mutex_lock();
123     hr = IWineD3DBuffer_GetPrivateData(This->wineD3DIndexBuffer, refguid, pData, pSizeOfData);
124     wined3d_mutex_unlock();
125
126     return hr;
127 }
128
129 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_FreePrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid) {
130     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
131     HRESULT hr;
132
133     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
134
135     wined3d_mutex_lock();
136     hr = IWineD3DBuffer_FreePrivateData(This->wineD3DIndexBuffer, refguid);
137     wined3d_mutex_unlock();
138
139     return hr;
140 }
141
142 static DWORD WINAPI IDirect3DIndexBuffer8Impl_SetPriority(LPDIRECT3DINDEXBUFFER8 iface, DWORD PriorityNew) {
143     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
144     DWORD ret;
145
146     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
147
148     wined3d_mutex_lock();
149     ret = IWineD3DBuffer_SetPriority(This->wineD3DIndexBuffer, PriorityNew);
150     wined3d_mutex_unlock();
151
152     return ret;
153 }
154
155 static DWORD WINAPI IDirect3DIndexBuffer8Impl_GetPriority(LPDIRECT3DINDEXBUFFER8 iface) {
156     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
157     DWORD ret;
158
159     TRACE("iface %p.\n", iface);
160
161     wined3d_mutex_lock();
162     ret = IWineD3DBuffer_GetPriority(This->wineD3DIndexBuffer);
163     wined3d_mutex_unlock();
164
165     return ret;
166 }
167
168 static void WINAPI IDirect3DIndexBuffer8Impl_PreLoad(LPDIRECT3DINDEXBUFFER8 iface) {
169     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
170
171     TRACE("iface %p.\n", iface);
172
173     wined3d_mutex_lock();
174     IWineD3DBuffer_PreLoad(This->wineD3DIndexBuffer);
175     wined3d_mutex_unlock();
176 }
177
178 static D3DRESOURCETYPE WINAPI IDirect3DIndexBuffer8Impl_GetType(IDirect3DIndexBuffer8 *iface)
179 {
180     TRACE("iface %p.\n", iface);
181
182     return D3DRTYPE_INDEXBUFFER;
183 }
184
185 /* IDirect3DIndexBuffer8 Interface follow: */
186 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_Lock(LPDIRECT3DINDEXBUFFER8 iface, UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags) {
187     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
188     HRESULT hr;
189
190     TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
191             iface, OffsetToLock, SizeToLock, ppbData, Flags);
192
193     wined3d_mutex_lock();
194     hr = IWineD3DBuffer_Map(This->wineD3DIndexBuffer, OffsetToLock, SizeToLock, ppbData, Flags);
195     wined3d_mutex_unlock();
196
197     return hr;
198 }
199
200 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_Unlock(LPDIRECT3DINDEXBUFFER8 iface) {
201     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
202     HRESULT hr;
203
204     TRACE("iface %p.\n", iface);
205
206     wined3d_mutex_lock();
207     hr = IWineD3DBuffer_Unmap(This->wineD3DIndexBuffer);
208     wined3d_mutex_unlock();
209
210     return hr;
211 }
212
213 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetDesc(LPDIRECT3DINDEXBUFFER8 iface, D3DINDEXBUFFER_DESC *pDesc) {
214     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
215     HRESULT hr;
216     WINED3DBUFFER_DESC desc;
217
218     TRACE("iface %p, desc %p.\n", iface, pDesc);
219
220     wined3d_mutex_lock();
221     hr = IWineD3DBuffer_GetDesc(This->wineD3DIndexBuffer, &desc);
222     wined3d_mutex_unlock();
223
224     if (SUCCEEDED(hr)) {
225         pDesc->Format = d3dformat_from_wined3dformat(This->format);
226         pDesc->Type = D3DRTYPE_INDEXBUFFER;
227         pDesc->Usage = desc.Usage;
228         pDesc->Pool = desc.Pool;
229         pDesc->Size = desc.Size;
230     }
231
232     return hr;
233 }
234
235 static const IDirect3DIndexBuffer8Vtbl Direct3DIndexBuffer8_Vtbl =
236 {
237     /* IUnknown */
238     IDirect3DIndexBuffer8Impl_QueryInterface,
239     IDirect3DIndexBuffer8Impl_AddRef,
240     IDirect3DIndexBuffer8Impl_Release,
241     /* IDirect3DResource8 */
242     IDirect3DIndexBuffer8Impl_GetDevice,
243     IDirect3DIndexBuffer8Impl_SetPrivateData,
244     IDirect3DIndexBuffer8Impl_GetPrivateData,
245     IDirect3DIndexBuffer8Impl_FreePrivateData,
246     IDirect3DIndexBuffer8Impl_SetPriority,
247     IDirect3DIndexBuffer8Impl_GetPriority,
248     IDirect3DIndexBuffer8Impl_PreLoad,
249     IDirect3DIndexBuffer8Impl_GetType,
250     /* IDirect3DIndexBuffer8 */
251     IDirect3DIndexBuffer8Impl_Lock,
252     IDirect3DIndexBuffer8Impl_Unlock,
253     IDirect3DIndexBuffer8Impl_GetDesc
254 };
255
256 static void STDMETHODCALLTYPE d3d8_indexbuffer_wined3d_object_destroyed(void *parent)
257 {
258     HeapFree(GetProcessHeap(), 0, parent);
259 }
260
261 static const struct wined3d_parent_ops d3d8_indexbuffer_wined3d_parent_ops =
262 {
263     d3d8_indexbuffer_wined3d_object_destroyed,
264 };
265
266 HRESULT indexbuffer_init(IDirect3DIndexBuffer8Impl *buffer, IDirect3DDevice8Impl *device,
267         UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
268 {
269     HRESULT hr;
270
271     buffer->lpVtbl = &Direct3DIndexBuffer8_Vtbl;
272     buffer->ref = 1;
273     buffer->format = wined3dformat_from_d3dformat(format);
274
275     wined3d_mutex_lock();
276     hr = IWineD3DDevice_CreateIndexBuffer(device->WineD3DDevice, size,
277             usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, &buffer->wineD3DIndexBuffer,
278             (IUnknown *)buffer, &d3d8_indexbuffer_wined3d_parent_ops);
279     wined3d_mutex_unlock();
280     if (FAILED(hr))
281     {
282         WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
283         return hr;
284     }
285
286     buffer->parentDevice = (IDirect3DDevice8 *)device;
287     IUnknown_AddRef(buffer->parentDevice);
288
289     return D3D_OK;
290 }