rsaenh: Avoid double 'va_end(args)' in case of a premature loop termination.
[wine.git] / dlls / dxgi / device.c
blob9e62e25b01f3b0755f8f85c9c4b998d79e307248
1 /*
2 * Copyright 2008 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "dxgi_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
27 static inline struct dxgi_device *impl_from_IWineDXGIDevice(IWineDXGIDevice *iface)
29 return CONTAINING_RECORD(iface, struct dxgi_device, IWineDXGIDevice_iface);
32 /* IUnknown methods */
34 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
36 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
38 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
40 if (IsEqualGUID(riid, &IID_IUnknown)
41 || IsEqualGUID(riid, &IID_IDXGIObject)
42 || IsEqualGUID(riid, &IID_IDXGIDevice)
43 || IsEqualGUID(riid, &IID_IDXGIDevice1)
44 || IsEqualGUID(riid, &IID_IWineDXGIDevice))
46 IUnknown_AddRef(iface);
47 *object = iface;
48 return S_OK;
51 if (This->child_layer)
53 TRACE("forwarding to child layer %p.\n", This->child_layer);
54 return IUnknown_QueryInterface(This->child_layer, riid, object);
57 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
59 *object = NULL;
60 return E_NOINTERFACE;
63 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
65 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
66 ULONG refcount = InterlockedIncrement(&This->refcount);
68 TRACE("%p increasing refcount to %u\n", This, refcount);
70 return refcount;
73 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
75 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
76 ULONG refcount = InterlockedDecrement(&device->refcount);
78 TRACE("%p decreasing refcount to %u.\n", device, refcount);
80 if (!refcount)
82 if (device->child_layer)
83 IUnknown_Release(device->child_layer);
84 wined3d_mutex_lock();
85 wined3d_device_uninit_3d(device->wined3d_device);
86 wined3d_device_decref(device->wined3d_device);
87 wined3d_mutex_unlock();
88 IDXGIAdapter1_Release(device->adapter);
89 wined3d_private_store_cleanup(&device->private_store);
90 HeapFree(GetProcessHeap(), 0, device);
93 return refcount;
96 /* IDXGIObject methods */
98 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
99 REFGUID guid, UINT data_size, const void *data)
101 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
103 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
105 return dxgi_set_private_data(&device->private_store, guid, data_size, data);
108 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
109 REFGUID guid, const IUnknown *object)
111 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
113 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
115 return dxgi_set_private_data_interface(&device->private_store, guid, object);
118 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
119 REFGUID guid, UINT *data_size, void *data)
121 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
123 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
125 return dxgi_get_private_data(&device->private_store, guid, data_size, data);
128 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
130 IDXGIAdapter *adapter;
131 HRESULT hr;
133 TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
135 hr = IWineDXGIDevice_GetAdapter(iface, &adapter);
136 if (FAILED(hr))
138 ERR("Failed to get adapter, hr %#x.\n", hr);
139 return hr;
142 hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
143 IDXGIAdapter_Release(adapter);
145 return hr;
148 /* IDXGIDevice methods */
150 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
152 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
154 TRACE("iface %p, adapter %p.\n", iface, adapter);
156 *adapter = (IDXGIAdapter *)device->adapter;
157 IDXGIAdapter_AddRef(*adapter);
158 return S_OK;
161 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
162 const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
163 const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
165 struct wined3d_device_parent *device_parent;
166 struct wined3d_resource_desc surface_desc;
167 IWineDXGIDeviceParent *dxgi_device_parent;
168 HRESULT hr;
169 UINT i;
170 UINT j;
172 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
173 iface, desc, surface_count, usage, shared_resource, surface);
175 hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
176 if (FAILED(hr))
178 ERR("Device should implement IWineDXGIDeviceParent\n");
179 return E_FAIL;
182 device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
184 FIXME("Implement DXGI<->wined3d usage conversion\n");
185 surface_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
186 surface_desc.format = wined3dformat_from_dxgi_format(desc->Format);
187 wined3d_sample_desc_from_dxgi(&surface_desc.multisample_type,
188 &surface_desc.multisample_quality, &desc->SampleDesc);
189 surface_desc.usage = usage;
190 surface_desc.pool = WINED3D_POOL_DEFAULT;
191 surface_desc.width = desc->Width;
192 surface_desc.height = desc->Height;
193 surface_desc.depth = 1;
194 surface_desc.size = 0;
196 wined3d_mutex_lock();
197 memset(surface, 0, surface_count * sizeof(*surface));
198 for (i = 0; i < surface_count; ++i)
200 struct wined3d_texture *wined3d_texture;
201 IUnknown *parent;
203 if (FAILED(hr = device_parent->ops->create_swapchain_texture(device_parent,
204 NULL, &surface_desc, 0, &wined3d_texture)))
206 ERR("Failed to create surface, hr %#x.\n", hr);
207 goto fail;
210 parent = wined3d_texture_get_parent(wined3d_texture);
211 hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
212 wined3d_texture_decref(wined3d_texture);
213 if (FAILED(hr))
215 ERR("Surface should implement IDXGISurface\n");
216 goto fail;
219 TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
221 wined3d_mutex_unlock();
222 IWineDXGIDeviceParent_Release(dxgi_device_parent);
224 return S_OK;
226 fail:
227 wined3d_mutex_unlock();
228 for (j = 0; j < i; ++j)
230 IDXGISurface_Release(surface[i]);
232 IWineDXGIDeviceParent_Release(dxgi_device_parent);
233 return hr;
236 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
237 IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
239 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
240 iface, resources, residency, resource_count);
242 return E_NOTIMPL;
245 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
247 FIXME("iface %p, priority %d stub!\n", iface, priority);
249 return E_NOTIMPL;
252 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
254 FIXME("iface %p, priority %p stub!\n", iface, priority);
256 return E_NOTIMPL;
259 static HRESULT STDMETHODCALLTYPE dxgi_device_SetMaximumFrameLatency(IWineDXGIDevice *iface, UINT max_latency)
261 FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
263 if (max_latency > DXGI_FRAME_LATENCY_MAX)
264 return DXGI_ERROR_INVALID_CALL;
266 return E_NOTIMPL;
269 static HRESULT STDMETHODCALLTYPE dxgi_device_GetMaximumFrameLatency(IWineDXGIDevice *iface, UINT *max_latency)
271 FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
273 if (max_latency)
274 *max_latency = DXGI_FRAME_LATENCY_DEFAULT;
276 return E_NOTIMPL;
279 /* IWineDXGIDevice methods */
281 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface,
282 struct wined3d_texture *wined3d_texture, DXGI_USAGE usage,
283 const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
285 struct dxgi_surface *object;
286 HRESULT hr;
288 TRACE("iface %p, wined3d_texture %p, usage %#x, shared_resource %p, outer %p, surface %p.\n",
289 iface, wined3d_texture, usage, shared_resource, outer, surface);
291 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
293 ERR("Failed to allocate DXGI surface object memory\n");
294 return E_OUTOFMEMORY;
297 if (FAILED(hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer, wined3d_texture)))
299 WARN("Failed to initialize surface, hr %#x.\n", hr);
300 HeapFree(GetProcessHeap(), 0, object);
301 return hr;
304 TRACE("Created IDXGISurface %p\n", object);
305 *surface = outer ? &object->IUnknown_iface : (IUnknown *)&object->IDXGISurface1_iface;
307 return S_OK;
310 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
311 struct wined3d_swapchain_desc *desc, BOOL implicit, struct wined3d_swapchain **wined3d_swapchain)
313 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
314 struct dxgi_swapchain *object;
315 HRESULT hr;
317 TRACE("iface %p, desc %p, wined3d_swapchain %p.\n",
318 iface, desc, wined3d_swapchain);
320 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
322 ERR("Failed to allocate DXGI swapchain object memory\n");
323 return E_OUTOFMEMORY;
326 if (FAILED(hr = dxgi_swapchain_init(object, device, desc, implicit)))
328 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
329 HeapFree(GetProcessHeap(), 0, object);
330 return hr;
333 TRACE("Created IDXGISwapChain %p.\n", object);
334 *wined3d_swapchain = object->wined3d_swapchain;
336 return S_OK;
339 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
341 /* IUnknown methods */
342 dxgi_device_QueryInterface,
343 dxgi_device_AddRef,
344 dxgi_device_Release,
345 /* IDXGIObject methods */
346 dxgi_device_SetPrivateData,
347 dxgi_device_SetPrivateDataInterface,
348 dxgi_device_GetPrivateData,
349 dxgi_device_GetParent,
350 /* IDXGIDevice methods */
351 dxgi_device_GetAdapter,
352 dxgi_device_CreateSurface,
353 dxgi_device_QueryResourceResidency,
354 dxgi_device_SetGPUThreadPriority,
355 dxgi_device_GetGPUThreadPriority,
356 /* IDXGIDevice1 methods */
357 dxgi_device_SetMaximumFrameLatency,
358 dxgi_device_GetMaximumFrameLatency,
359 /* IWineDXGIDevice methods */
360 dxgi_device_create_surface,
361 dxgi_device_create_swapchain,
364 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
365 IDXGIFactory *factory, IDXGIAdapter *adapter,
366 const D3D_FEATURE_LEVEL *feature_levels, unsigned int level_count)
368 struct wined3d_device_parent *wined3d_device_parent;
369 struct wined3d_swapchain_desc swapchain_desc;
370 IWineDXGIDeviceParent *dxgi_device_parent;
371 struct dxgi_adapter *dxgi_adapter;
372 struct dxgi_factory *dxgi_factory;
373 D3D_FEATURE_LEVEL feature_level;
374 void *layer_base;
375 HRESULT hr;
377 if (!(dxgi_factory = unsafe_impl_from_IDXGIFactory1((IDXGIFactory1 *)factory)))
379 WARN("This is not the factory we're looking for.\n");
380 return E_FAIL;
383 if (!(dxgi_adapter = unsafe_impl_from_IDXGIAdapter1((IDXGIAdapter1 *)adapter)))
385 WARN("This is not the adapter we're looking for.\n");
386 return E_FAIL;
389 device->IWineDXGIDevice_iface.lpVtbl = &dxgi_device_vtbl;
390 device->refcount = 1;
391 wined3d_mutex_lock();
392 wined3d_private_store_init(&device->private_store);
394 layer_base = device + 1;
396 if (FAILED(hr = layer->create(layer->id, &layer_base, 0,
397 device, &IID_IUnknown, (void **)&device->child_layer)))
399 WARN("Failed to create device, returning %#x.\n", hr);
400 wined3d_private_store_cleanup(&device->private_store);
401 wined3d_mutex_unlock();
402 return hr;
405 if (FAILED(hr = IWineDXGIDevice_QueryInterface(&device->IWineDXGIDevice_iface,
406 &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent)))
408 ERR("DXGI device should implement IWineDXGIDeviceParent.\n");
409 IUnknown_Release(device->child_layer);
410 wined3d_private_store_cleanup(&device->private_store);
411 wined3d_mutex_unlock();
412 return hr;
414 wined3d_device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
415 IWineDXGIDeviceParent_Release(dxgi_device_parent);
417 if (!(feature_level = dxgi_check_feature_level_support(dxgi_factory, dxgi_adapter,
418 feature_levels, level_count)))
420 IUnknown_Release(device->child_layer);
421 wined3d_private_store_cleanup(&device->private_store);
422 wined3d_mutex_unlock();
423 return E_FAIL;
426 FIXME("Ignoring adapter type.\n");
428 hr = wined3d_device_create(dxgi_factory->wined3d, dxgi_adapter->ordinal, WINED3D_DEVICE_TYPE_HAL,
429 NULL, 0, 4, wined3d_device_parent, &device->wined3d_device);
430 if (FAILED(hr))
432 WARN("Failed to create a wined3d device, returning %#x.\n", hr);
433 IUnknown_Release(device->child_layer);
434 wined3d_private_store_cleanup(&device->private_store);
435 wined3d_mutex_unlock();
436 return hr;
439 layer->set_feature_level(layer->id, device->child_layer, feature_level);
441 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
442 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
443 swapchain_desc.device_window = dxgi_factory_get_device_window(dxgi_factory);
444 swapchain_desc.windowed = TRUE;
445 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc)))
447 ERR("Failed to initialize 3D, hr %#x.\n", hr);
448 wined3d_device_decref(device->wined3d_device);
449 IUnknown_Release(device->child_layer);
450 wined3d_private_store_cleanup(&device->private_store);
451 wined3d_mutex_unlock();
452 return hr;
454 wined3d_mutex_unlock();
456 device->adapter = &dxgi_adapter->IDXGIAdapter1_iface;
457 IDXGIAdapter1_AddRef(device->adapter);
459 return S_OK;