d3d9: Remove superflous NULL checks (Coverity).
[wine] / dlls / d3d9 / surface.c
1 /*
2  * IDirect3DSurface9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 static inline IDirect3DSurface9Impl *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
28 {
29     return CONTAINING_RECORD(iface, IDirect3DSurface9Impl, lpVtbl);
30 }
31
32 /* IDirect3DSurface9 IUnknown parts follow: */
33 static HRESULT WINAPI IDirect3DSurface9Impl_QueryInterface(LPDIRECT3DSURFACE9 iface, REFIID riid, LPVOID* ppobj) {
34     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
35
36     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
37
38     if (IsEqualGUID(riid, &IID_IUnknown)
39         || IsEqualGUID(riid, &IID_IDirect3DResource9)
40         || IsEqualGUID(riid, &IID_IDirect3DSurface9)) {
41         IDirect3DSurface9_AddRef(iface);
42         *ppobj = This;
43         return S_OK;
44     }
45
46     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
47     *ppobj = NULL;
48     return E_NOINTERFACE;
49 }
50
51 static ULONG WINAPI IDirect3DSurface9Impl_AddRef(LPDIRECT3DSURFACE9 iface) {
52     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
53
54     TRACE("iface %p.\n", iface);
55
56     if (This->forwardReference) {
57         /* Forward refcounting */
58         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
59         return IUnknown_AddRef(This->forwardReference);
60     } else {
61         /* No container, handle our own refcounting */
62         ULONG ref = InterlockedIncrement(&This->ref);
63
64         TRACE("%p increasing refcount to %u.\n", iface, ref);
65
66         if (ref == 1)
67         {
68             if (This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
69             wined3d_mutex_lock();
70             wined3d_surface_incref(This->wined3d_surface);
71             wined3d_mutex_unlock();
72         }
73
74         return ref;
75     }
76
77 }
78
79 static ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
80     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
81
82     TRACE("iface %p.\n", iface);
83
84     if (This->forwardReference) {
85         /* Forward to the containerParent */
86         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
87         return IUnknown_Release(This->forwardReference);
88     } else {
89         /* No container, handle our own refcounting */
90         ULONG ref = InterlockedDecrement(&This->ref);
91
92         TRACE("%p decreasing refcount to %u.\n", iface, ref);
93
94         if (ref == 0) {
95             IDirect3DDevice9Ex *parentDevice = This->parentDevice;
96
97             wined3d_mutex_lock();
98             wined3d_surface_decref(This->wined3d_surface);
99             wined3d_mutex_unlock();
100
101             /* Release the device last, as it may cause the device to be destroyed. */
102             if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
103         }
104
105         return ref;
106     }
107 }
108
109 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
110 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
111 {
112     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
113
114     TRACE("iface %p, device %p.\n", iface, device);
115
116     if (This->forwardReference)
117     {
118         IDirect3DResource9 *resource;
119         HRESULT hr;
120
121         hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
122         if (SUCCEEDED(hr))
123         {
124             hr = IDirect3DResource9_GetDevice(resource, device);
125             IDirect3DResource9_Release(resource);
126
127             TRACE("Returning device %p.\n", *device);
128         }
129
130         return hr;
131     }
132
133     *device = (IDirect3DDevice9 *)This->parentDevice;
134     IDirect3DDevice9_AddRef(*device);
135
136     TRACE("Returning device %p.\n", *device);
137
138     return D3D_OK;
139 }
140
141 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
142         const void *data, DWORD data_size, DWORD flags)
143 {
144     IDirect3DSurface9Impl *surface = impl_from_IDirect3DSurface9(iface);
145     struct wined3d_resource *resource;
146     HRESULT hr;
147
148     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
149             iface, debugstr_guid(guid), data, data_size, flags);
150
151     wined3d_mutex_lock();
152     resource = wined3d_surface_get_resource(surface->wined3d_surface);
153     hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
154     wined3d_mutex_unlock();
155
156     return hr;
157 }
158
159 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
160         void *data, DWORD *data_size)
161 {
162     IDirect3DSurface9Impl *surface = impl_from_IDirect3DSurface9(iface);
163     struct wined3d_resource *resource;
164     HRESULT hr;
165
166     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
167             iface, debugstr_guid(guid), data, data_size);
168
169     wined3d_mutex_lock();
170     resource = wined3d_surface_get_resource(surface->wined3d_surface);
171     hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
172     wined3d_mutex_unlock();
173
174     return hr;
175 }
176
177 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
178 {
179     IDirect3DSurface9Impl *surface = impl_from_IDirect3DSurface9(iface);
180     struct wined3d_resource *resource;
181     HRESULT hr;
182
183     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
184
185     wined3d_mutex_lock();
186     resource = wined3d_surface_get_resource(surface->wined3d_surface);
187     hr = wined3d_resource_free_private_data(resource, guid);
188     wined3d_mutex_unlock();
189
190     return hr;
191 }
192
193 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
194     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
195     HRESULT hr;
196
197     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
198
199     wined3d_mutex_lock();
200     hr = wined3d_surface_set_priority(This->wined3d_surface, PriorityNew);
201     wined3d_mutex_unlock();
202
203     return hr;
204 }
205
206 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
207     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
208     HRESULT hr;
209
210     TRACE("iface %p.\n", iface);
211
212     wined3d_mutex_lock();
213     hr = wined3d_surface_get_priority(This->wined3d_surface);
214     wined3d_mutex_unlock();
215
216     return hr;
217 }
218
219 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
220     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
221
222     TRACE("iface %p.\n", iface);
223
224     wined3d_mutex_lock();
225     wined3d_surface_preload(This->wined3d_surface);
226     wined3d_mutex_unlock();
227 }
228
229 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(IDirect3DSurface9 *iface)
230 {
231     TRACE("iface %p.\n", iface);
232
233     return D3DRTYPE_SURFACE;
234 }
235
236 /* IDirect3DSurface9 Interface follow: */
237 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
238     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
239     HRESULT res;
240
241     TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
242
243     if (!This->container) return E_NOINTERFACE;
244
245     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
246
247     TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
248
249     return res;
250 }
251
252 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
253 {
254     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
255     struct wined3d_resource_desc wined3d_desc;
256     struct wined3d_resource *wined3d_resource;
257
258     TRACE("iface %p, desc %p.\n", iface, desc);
259
260     wined3d_mutex_lock();
261     wined3d_resource = wined3d_surface_get_resource(This->wined3d_surface);
262     wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
263     wined3d_mutex_unlock();
264
265     desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
266     desc->Type = wined3d_desc.resource_type;
267     desc->Usage = wined3d_desc.usage;
268     desc->Pool = wined3d_desc.pool;
269     desc->MultiSampleType = wined3d_desc.multisample_type;
270     desc->MultiSampleQuality = wined3d_desc.multisample_quality;
271     desc->Width = wined3d_desc.width;
272     desc->Height = wined3d_desc.height;
273
274     return D3D_OK;
275 }
276
277 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
278     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
279     HRESULT hr;
280
281     TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
282
283     wined3d_mutex_lock();
284     hr = wined3d_surface_map(This->wined3d_surface, (WINED3DLOCKED_RECT *)pLockedRect, pRect, Flags);
285     wined3d_mutex_unlock();
286
287     return hr;
288 }
289
290 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
291     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
292     HRESULT hr;
293
294     TRACE("iface %p.\n", iface);
295
296     wined3d_mutex_lock();
297     hr = wined3d_surface_unmap(This->wined3d_surface);
298     wined3d_mutex_unlock();
299
300     switch(hr)
301     {
302         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
303         default:                        return hr;
304     }
305 }
306
307 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
308     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
309     HRESULT hr;
310
311     TRACE("iface %p, hdc %p.\n", iface, phdc);
312
313     if(!This->getdc_supported)
314     {
315         WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
316         /* Don't touch the DC */
317         return D3DERR_INVALIDCALL;
318     }
319
320     wined3d_mutex_lock();
321     hr = wined3d_surface_getdc(This->wined3d_surface, phdc);
322     wined3d_mutex_unlock();
323
324     return hr;
325 }
326
327 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
328     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
329     HRESULT hr;
330
331     TRACE("iface %p, hdc %p.\n", iface, hdc);
332
333     wined3d_mutex_lock();
334     hr = wined3d_surface_releasedc(This->wined3d_surface, hdc);
335     wined3d_mutex_unlock();
336
337     switch (hr)
338     {
339         case WINEDDERR_NODC:    return D3DERR_INVALIDCALL;
340         default:                return hr;
341     }
342 }
343
344 static const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
345 {
346     /* IUnknown */
347     IDirect3DSurface9Impl_QueryInterface,
348     IDirect3DSurface9Impl_AddRef,
349     IDirect3DSurface9Impl_Release,
350     /* IDirect3DResource9 */
351     IDirect3DSurface9Impl_GetDevice,
352     IDirect3DSurface9Impl_SetPrivateData,
353     IDirect3DSurface9Impl_GetPrivateData,
354     IDirect3DSurface9Impl_FreePrivateData,
355     IDirect3DSurface9Impl_SetPriority,
356     IDirect3DSurface9Impl_GetPriority,
357     IDirect3DSurface9Impl_PreLoad,
358     IDirect3DSurface9Impl_GetType,
359     /* IDirect3DSurface9 */
360     IDirect3DSurface9Impl_GetContainer,
361     IDirect3DSurface9Impl_GetDesc,
362     IDirect3DSurface9Impl_LockRect,
363     IDirect3DSurface9Impl_UnlockRect,
364     IDirect3DSurface9Impl_GetDC,
365     IDirect3DSurface9Impl_ReleaseDC
366 };
367
368 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
369 {
370     HeapFree(GetProcessHeap(), 0, parent);
371 }
372
373 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
374 {
375     surface_wined3d_object_destroyed,
376 };
377
378 HRESULT surface_init(IDirect3DSurface9Impl *surface, IDirect3DDevice9Impl *device,
379         UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
380         DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
381 {
382     HRESULT hr;
383
384     surface->lpVtbl = &Direct3DSurface9_Vtbl;
385     surface->ref = 1;
386
387     switch (format)
388     {
389         case D3DFMT_A8R8G8B8:
390         case D3DFMT_X8R8G8B8:
391         case D3DFMT_R5G6B5:
392         case D3DFMT_X1R5G5B5:
393         case D3DFMT_A1R5G5B5:
394         case D3DFMT_R8G8B8:
395             surface->getdc_supported = TRUE;
396             break;
397
398         default:
399             surface->getdc_supported = FALSE;
400             break;
401     }
402
403     /* FIXME: Check MAX bounds of MultisampleQuality. */
404     if (multisample_quality > 0)
405     {
406         FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
407         multisample_quality = 0;
408     }
409
410     wined3d_mutex_lock();
411     hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
412             lockable, discard, level, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, multisample_type,
413             multisample_quality, SURFACE_OPENGL, surface, &d3d9_surface_wined3d_parent_ops, &surface->wined3d_surface);
414     wined3d_mutex_unlock();
415     if (FAILED(hr))
416     {
417         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
418         return hr;
419     }
420
421     surface->parentDevice = &device->IDirect3DDevice9Ex_iface;
422     IDirect3DDevice9Ex_AddRef(surface->parentDevice);
423
424     return D3D_OK;
425 }