gdiplus: Dereference texture after checking it for NULL (Coverity).
[wine] / dlls / d3d8 / directx.c
1 /*
2  * IDirect3D8 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
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
25 #include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 #include "d3d8_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39
40 /* IDirect3D IUnknown parts follow: */
41 static HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
42 {
43     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
44
45     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
46
47     if (IsEqualGUID(riid, &IID_IUnknown)
48         || IsEqualGUID(riid, &IID_IDirect3D8)) {
49         IUnknown_AddRef(iface);
50         *ppobj = This;
51         return S_OK;
52     }
53
54     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
55     *ppobj = NULL;
56     return E_NOINTERFACE;
57 }
58
59 static ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
60     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
61     ULONG ref = InterlockedIncrement(&This->ref);
62
63     TRACE("%p increasing refcount to %u.\n", iface, ref);
64
65     return ref;
66 }
67
68 static ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
69     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
70     ULONG ref = InterlockedDecrement(&This->ref);
71
72     TRACE("%p decreasing refcount to %u.\n", iface, ref);
73
74     if (ref == 0) {
75         TRACE("Releasing wined3d %p\n", This->WineD3D);
76
77         wined3d_mutex_lock();
78         IWineD3D_Release(This->WineD3D);
79         wined3d_mutex_unlock();
80
81         HeapFree(GetProcessHeap(), 0, This);
82     }
83
84     return ref;
85 }
86
87 /* IDirect3D8 Interface follow: */
88 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
89     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
90     HRESULT hr;
91
92     TRACE("iface %p, init_function %p.\n", iface, pInitializeFunction);
93
94     wined3d_mutex_lock();
95     hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
96     wined3d_mutex_unlock();
97
98     return hr;
99 }
100
101 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
102     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
103     HRESULT hr;
104
105     TRACE("iface %p.\n", iface);
106
107     wined3d_mutex_lock();
108     hr = IWineD3D_GetAdapterCount(This->WineD3D);
109     wined3d_mutex_unlock();
110
111     return hr;
112 }
113
114 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier(LPDIRECT3D8 iface,
115         UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8 *pIdentifier)
116 {
117     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
118     WINED3DADAPTER_IDENTIFIER adapter_id;
119     HRESULT hr;
120
121     TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
122             iface, Adapter, Flags, pIdentifier);
123
124     adapter_id.driver = pIdentifier->Driver;
125     adapter_id.driver_size = sizeof(pIdentifier->Driver);
126     adapter_id.description = pIdentifier->Description;
127     adapter_id.description_size = sizeof(pIdentifier->Description);
128     adapter_id.device_name = NULL; /* d3d9 only */
129     adapter_id.device_name_size = 0; /* d3d9 only */
130
131     wined3d_mutex_lock();
132     hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
133     wined3d_mutex_unlock();
134
135     pIdentifier->DriverVersion = adapter_id.driver_version;
136     pIdentifier->VendorId = adapter_id.vendor_id;
137     pIdentifier->DeviceId = adapter_id.device_id;
138     pIdentifier->SubSysId = adapter_id.subsystem_id;
139     pIdentifier->Revision = adapter_id.revision;
140     memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
141     pIdentifier->WHQLLevel = adapter_id.whql_level;
142
143     return hr;
144 }
145
146 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
147     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
148     HRESULT hr;
149
150     TRACE("iface %p, adapter %u.\n", iface, Adapter);
151
152     wined3d_mutex_lock();
153     hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
154     wined3d_mutex_unlock();
155
156     return hr;
157 }
158
159 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
160     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
161     HRESULT hr;
162
163     TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
164             iface, Adapter, Mode, pMode);
165
166     wined3d_mutex_lock();
167     hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
168     wined3d_mutex_unlock();
169
170     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
171
172     return hr;
173 }
174
175 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
176     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
177     HRESULT hr;
178
179     TRACE("iface %p, adapter %u, mode %p.\n",
180             iface, Adapter, pMode);
181
182     wined3d_mutex_lock();
183     hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
184     wined3d_mutex_unlock();
185
186     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
187
188     return hr;
189 }
190
191 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
192                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
193                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
194     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
195     HRESULT hr;
196
197     TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
198             iface, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed);
199
200     wined3d_mutex_lock();
201     hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
202             wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
203     wined3d_mutex_unlock();
204
205     return hr;
206 }
207
208 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
209                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
210                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
211     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
212     HRESULT hr;
213     WINED3DRESOURCETYPE WineD3DRType;
214
215     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
216             iface, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
217
218     if(CheckFormat == D3DFMT_R8G8B8)
219     {
220         /* See comment in dlls/d3d9/directx.c, IDirect3D9Impl_CheckDeviceFormat for details */
221         WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
222         return D3DERR_NOTAVAILABLE;
223     }
224
225
226     switch(RType) {
227         case D3DRTYPE_VERTEXBUFFER:
228         case D3DRTYPE_INDEXBUFFER:
229             WineD3DRType = WINED3DRTYPE_BUFFER;
230             break;
231
232         default:
233             WineD3DRType = RType;
234             break;
235     }
236
237     wined3d_mutex_lock();
238     hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
239             Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
240     wined3d_mutex_unlock();
241
242     return hr;
243 }
244
245 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT Adapter,
246         D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType)
247 {
248     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
249     HRESULT hr;
250
251     TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
252             iface, Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType);
253
254     wined3d_mutex_lock();
255     hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
256             wined3dformat_from_d3dformat(SurfaceFormat), Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
257     wined3d_mutex_unlock();
258
259     return hr;
260 }
261
262 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(IDirect3D8 *iface, UINT Adapter, D3DDEVTYPE DeviceType,
263         D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat)
264 {
265     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
266     HRESULT hr;
267
268     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
269             iface, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
270
271     wined3d_mutex_lock();
272     hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
273             wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
274             wined3dformat_from_d3dformat(DepthStencilFormat));
275     wined3d_mutex_unlock();
276
277     return hr;
278 }
279
280 void fixup_caps(WINED3DCAPS *caps)
281 {
282     /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
283     if (caps->PixelShaderVersion > D3DPS_VERSION(1,4)) {
284         caps->PixelShaderVersion = D3DPS_VERSION(1,4);
285     }
286     if (caps->VertexShaderVersion > D3DVS_VERSION(1,1)) {
287         caps->VertexShaderVersion = D3DVS_VERSION(1,1);
288     }
289     caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
290
291     caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
292 }
293
294 static HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
295     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
296     HRESULT hrc = D3D_OK;
297     WINED3DCAPS *pWineCaps;
298
299     TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, Adapter, DeviceType, pCaps);
300
301     if(NULL == pCaps){
302         return D3DERR_INVALIDCALL;
303     }
304     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
305     if(pWineCaps == NULL){
306         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
307     }
308
309     wined3d_mutex_lock();
310     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
311     wined3d_mutex_unlock();
312
313     fixup_caps(pWineCaps);
314     WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
315     HeapFree(GetProcessHeap(), 0, pWineCaps);
316
317     TRACE("(%p) returning %p\n", This, pCaps);
318     return hrc;
319 }
320
321 static HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
322     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
323     HMONITOR ret;
324
325     TRACE("iface %p, adapter %u.\n", iface, Adapter);
326
327     wined3d_mutex_lock();
328     ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
329     wined3d_mutex_unlock();
330
331     return ret;
332 }
333
334 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
335         HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters, IDirect3DDevice8 **device)
336 {
337     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
338     IDirect3DDevice8Impl *object;
339     HRESULT hr;
340
341     TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
342             iface, adapter, device_type, focus_window, flags, parameters, device);
343
344     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
345     if (!object)
346     {
347         ERR("Failed to allocate device memory.\n");
348         return E_OUTOFMEMORY;
349     }
350
351     hr = device_init(object, This->WineD3D, adapter, device_type, focus_window, flags, parameters);
352     if (FAILED(hr))
353     {
354         WARN("Failed to initialize device, hr %#x.\n", hr);
355         HeapFree(GetProcessHeap(), 0, object);
356         return hr;
357     }
358
359     TRACE("Created device %p.\n", object);
360     *device = (IDirect3DDevice8 *)object;
361
362     return D3D_OK;
363 }
364
365 const IDirect3D8Vtbl Direct3D8_Vtbl =
366 {
367     /* IUnknown */
368     IDirect3D8Impl_QueryInterface,
369     IDirect3D8Impl_AddRef,
370     IDirect3D8Impl_Release,
371     /* IDirect3D8 */
372     IDirect3D8Impl_RegisterSoftwareDevice,
373     IDirect3D8Impl_GetAdapterCount,
374     IDirect3D8Impl_GetAdapterIdentifier,
375     IDirect3D8Impl_GetAdapterModeCount,
376     IDirect3D8Impl_EnumAdapterModes,
377     IDirect3D8Impl_GetAdapterDisplayMode,
378     IDirect3D8Impl_CheckDeviceType,
379     IDirect3D8Impl_CheckDeviceFormat,
380     IDirect3D8Impl_CheckDeviceMultiSampleType,
381     IDirect3D8Impl_CheckDepthStencilMatch,
382     IDirect3D8Impl_GetDeviceCaps,
383     IDirect3D8Impl_GetAdapterMonitor,
384     IDirect3D8Impl_CreateDevice
385 };