winegstreamer: Move seeking to the Unix library.
[wine.git] / dlls / dxgi / device.c
blob74260f9b7695cf7785a4987371323801fd1a8c71
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 *device = 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_IDXGIDevice2)
45 || IsEqualGUID(riid, &IID_IDXGIDevice3)
46 || IsEqualGUID(riid, &IID_IWineDXGIDevice))
48 IUnknown_AddRef(iface);
49 *object = iface;
50 return S_OK;
53 if (IsEqualGUID(riid, &IID_IWineDXGISwapChainFactory))
55 IUnknown_AddRef(iface);
56 *object = &device->IWineDXGISwapChainFactory_iface;
57 return S_OK;
60 if (device->child_layer)
62 TRACE("Forwarding to child layer %p.\n", device->child_layer);
63 return IUnknown_QueryInterface(device->child_layer, riid, object);
66 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
68 *object = NULL;
69 return E_NOINTERFACE;
72 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
74 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
75 ULONG refcount = InterlockedIncrement(&device->refcount);
77 TRACE("%p increasing refcount to %u\n", device, refcount);
79 return refcount;
82 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
84 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
85 ULONG refcount = InterlockedDecrement(&device->refcount);
87 TRACE("%p decreasing refcount to %u.\n", device, refcount);
89 if (!refcount)
91 if (device->child_layer)
92 IUnknown_Release(device->child_layer);
93 wined3d_mutex_lock();
94 wined3d_swapchain_decref(device->implicit_swapchain);
95 wined3d_device_decref(device->wined3d_device);
96 wined3d_mutex_unlock();
97 IWineDXGIAdapter_Release(device->adapter);
98 wined3d_private_store_cleanup(&device->private_store);
99 heap_free(device);
102 return refcount;
105 /* IDXGIObject methods */
107 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
108 REFGUID guid, UINT data_size, const void *data)
110 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
112 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
114 return dxgi_set_private_data(&device->private_store, guid, data_size, data);
117 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
118 REFGUID guid, const IUnknown *object)
120 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
122 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
124 return dxgi_set_private_data_interface(&device->private_store, guid, object);
127 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
128 REFGUID guid, UINT *data_size, void *data)
130 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
132 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
134 return dxgi_get_private_data(&device->private_store, guid, data_size, data);
137 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
139 IDXGIAdapter *adapter;
140 HRESULT hr;
142 TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
144 hr = IWineDXGIDevice_GetAdapter(iface, &adapter);
145 if (FAILED(hr))
147 ERR("Failed to get adapter, hr %#x.\n", hr);
148 return hr;
151 hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
152 IDXGIAdapter_Release(adapter);
154 return hr;
157 /* IDXGIDevice methods */
159 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
161 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
163 TRACE("iface %p, adapter %p.\n", iface, adapter);
165 *adapter = (IDXGIAdapter *)device->adapter;
166 IDXGIAdapter_AddRef(*adapter);
167 return S_OK;
170 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
171 const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
172 const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
174 struct wined3d_device_parent *device_parent;
175 struct wined3d_resource_desc surface_desc;
176 IWineDXGIDeviceParent *dxgi_device_parent;
177 HRESULT hr;
178 UINT i;
179 UINT j;
181 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p.\n",
182 iface, desc, surface_count, usage, shared_resource, surface);
184 hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
185 if (FAILED(hr))
187 ERR("Device should implement IWineDXGIDeviceParent.\n");
188 return E_FAIL;
191 device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
193 surface_desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
194 surface_desc.format = wined3dformat_from_dxgi_format(desc->Format);
195 wined3d_sample_desc_from_dxgi(&surface_desc.multisample_type,
196 &surface_desc.multisample_quality, &desc->SampleDesc);
197 surface_desc.bind_flags = wined3d_bind_flags_from_dxgi_usage(usage);
198 surface_desc.usage = 0;
199 surface_desc.access = WINED3D_RESOURCE_ACCESS_GPU;
200 surface_desc.width = desc->Width;
201 surface_desc.height = desc->Height;
202 surface_desc.depth = 1;
203 surface_desc.size = 0;
205 wined3d_mutex_lock();
206 memset(surface, 0, surface_count * sizeof(*surface));
207 for (i = 0; i < surface_count; ++i)
209 struct wined3d_texture *wined3d_texture;
210 IUnknown *parent;
212 if (FAILED(hr = device_parent->ops->create_swapchain_texture(device_parent,
213 NULL, &surface_desc, 0, &wined3d_texture)))
215 ERR("Failed to create surface, hr %#x.\n", hr);
216 goto fail;
219 parent = wined3d_texture_get_parent(wined3d_texture);
220 hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
221 wined3d_texture_decref(wined3d_texture);
222 if (FAILED(hr))
224 ERR("Surface should implement IDXGISurface.\n");
225 goto fail;
228 TRACE("Created IDXGISurface %p (%u/%u).\n", surface[i], i + 1, surface_count);
230 wined3d_mutex_unlock();
231 IWineDXGIDeviceParent_Release(dxgi_device_parent);
233 return S_OK;
235 fail:
236 wined3d_mutex_unlock();
237 for (j = 0; j < i; ++j)
239 IDXGISurface_Release(surface[i]);
241 IWineDXGIDeviceParent_Release(dxgi_device_parent);
242 return hr;
245 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
246 IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
248 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
249 iface, resources, residency, resource_count);
251 return E_NOTIMPL;
254 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
256 FIXME("iface %p, priority %d stub!\n", iface, priority);
258 return E_NOTIMPL;
261 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
263 FIXME("iface %p, priority %p stub!\n", iface, priority);
265 return E_NOTIMPL;
268 static HRESULT STDMETHODCALLTYPE dxgi_device_SetMaximumFrameLatency(IWineDXGIDevice *iface, UINT max_latency)
270 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
272 TRACE("iface %p, max_latency %u.\n", iface, max_latency);
274 if (max_latency > DXGI_FRAME_LATENCY_MAX)
275 return DXGI_ERROR_INVALID_CALL;
277 wined3d_mutex_lock();
278 wined3d_device_set_max_frame_latency(device->wined3d_device, max_latency);
279 wined3d_mutex_unlock();
281 return S_OK;
284 static HRESULT STDMETHODCALLTYPE dxgi_device_GetMaximumFrameLatency(IWineDXGIDevice *iface, UINT *max_latency)
286 struct dxgi_device *device = impl_from_IWineDXGIDevice(iface);
288 TRACE("iface %p, max_latency %p.\n", iface, max_latency);
290 if (!max_latency)
291 return DXGI_ERROR_INVALID_CALL;
293 wined3d_mutex_lock();
294 *max_latency = wined3d_device_get_max_frame_latency(device->wined3d_device);
295 wined3d_mutex_unlock();
297 return S_OK;
300 static HRESULT STDMETHODCALLTYPE dxgi_device_OfferResources(IWineDXGIDevice *iface, UINT resource_count,
301 IDXGIResource * const *resources, DXGI_OFFER_RESOURCE_PRIORITY priority)
303 FIXME("iface %p, resource_count %u, resources %p, priority %u stub!\n", iface, resource_count,
304 resources, priority);
306 return E_NOTIMPL;
309 static HRESULT STDMETHODCALLTYPE dxgi_device_ReclaimResources(IWineDXGIDevice *iface, UINT resource_count,
310 IDXGIResource * const *resources, BOOL *discarded)
312 FIXME("iface %p, resource_count %u, resources %p, discarded %p stub!\n", iface, resource_count,
313 resources, discarded);
315 return E_NOTIMPL;
318 static HRESULT STDMETHODCALLTYPE dxgi_device_EnqueueSetEvent(IWineDXGIDevice *iface, HANDLE event)
320 FIXME("iface %p, event %p stub!\n", iface, event);
322 return E_NOTIMPL;
325 static void STDMETHODCALLTYPE dxgi_device_Trim(IWineDXGIDevice *iface)
327 FIXME("iface %p stub!\n", iface);
330 /* IWineDXGIDevice methods */
332 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface,
333 struct wined3d_texture *wined3d_texture, DXGI_USAGE usage,
334 const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
336 struct dxgi_surface *object;
337 HRESULT hr;
339 TRACE("iface %p, wined3d_texture %p, usage %#x, shared_resource %p, outer %p, surface %p.\n",
340 iface, wined3d_texture, usage, shared_resource, outer, surface);
342 if (!(object = heap_alloc_zero(sizeof(*object))))
344 ERR("Failed to allocate DXGI surface object memory.\n");
345 return E_OUTOFMEMORY;
348 if (FAILED(hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer, wined3d_texture)))
350 WARN("Failed to initialize surface, hr %#x.\n", hr);
351 heap_free(object);
352 return hr;
355 TRACE("Created IDXGISurface %p.\n", object);
356 *surface = outer ? &object->IUnknown_iface : (IUnknown *)&object->IDXGISurface1_iface;
358 return S_OK;
361 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
363 /* IUnknown methods */
364 dxgi_device_QueryInterface,
365 dxgi_device_AddRef,
366 dxgi_device_Release,
367 /* IDXGIObject methods */
368 dxgi_device_SetPrivateData,
369 dxgi_device_SetPrivateDataInterface,
370 dxgi_device_GetPrivateData,
371 dxgi_device_GetParent,
372 /* IDXGIDevice methods */
373 dxgi_device_GetAdapter,
374 dxgi_device_CreateSurface,
375 dxgi_device_QueryResourceResidency,
376 dxgi_device_SetGPUThreadPriority,
377 dxgi_device_GetGPUThreadPriority,
378 /* IDXGIDevice1 methods */
379 dxgi_device_SetMaximumFrameLatency,
380 dxgi_device_GetMaximumFrameLatency,
381 /* IDXGIDevice2 methods */
382 dxgi_device_OfferResources,
383 dxgi_device_ReclaimResources,
384 dxgi_device_EnqueueSetEvent,
385 /* IDXGIDevice3 methods */
386 dxgi_device_Trim,
387 /* IWineDXGIDevice methods */
388 dxgi_device_create_surface,
391 static inline struct dxgi_device *impl_from_IWineDXGISwapChainFactory(IWineDXGISwapChainFactory *iface)
393 return CONTAINING_RECORD(iface, struct dxgi_device, IWineDXGISwapChainFactory_iface);
396 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_factory_QueryInterface(IWineDXGISwapChainFactory *iface,
397 REFIID iid, void **out)
399 struct dxgi_device *device = impl_from_IWineDXGISwapChainFactory(iface);
401 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
403 return dxgi_device_QueryInterface(&device->IWineDXGIDevice_iface, iid, out);
406 static ULONG STDMETHODCALLTYPE dxgi_swapchain_factory_AddRef(IWineDXGISwapChainFactory *iface)
408 struct dxgi_device *device = impl_from_IWineDXGISwapChainFactory(iface);
410 TRACE("iface %p.\n", iface);
412 return dxgi_device_AddRef(&device->IWineDXGIDevice_iface);
415 static ULONG STDMETHODCALLTYPE dxgi_swapchain_factory_Release(IWineDXGISwapChainFactory *iface)
417 struct dxgi_device *device = impl_from_IWineDXGISwapChainFactory(iface);
419 TRACE("iface %p.\n", iface);
421 return dxgi_device_Release(&device->IWineDXGIDevice_iface);
424 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_factory_create_swapchain(IWineDXGISwapChainFactory *iface,
425 IDXGIFactory *factory, HWND window, const DXGI_SWAP_CHAIN_DESC1 *desc,
426 const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *fullscreen_desc, IDXGIOutput *output, IDXGISwapChain1 **swapchain)
428 struct dxgi_device *device = impl_from_IWineDXGISwapChainFactory(iface);
429 struct wined3d_swapchain_desc wined3d_desc;
430 struct IDXGIOutput *containing_output;
431 struct d3d11_swapchain *object;
432 HRESULT hr;
434 TRACE("iface %p, factory %p, window %p, desc %p, fullscreen_desc %p, output %p, swapchain %p.\n",
435 iface, factory, window, desc, fullscreen_desc, output, swapchain);
437 if (FAILED(hr = dxgi_get_output_from_window(factory, window, &containing_output)))
439 WARN("Failed to get output from window %p, hr %#x.\n", window, hr);
440 return hr;
443 hr = wined3d_swapchain_desc_from_dxgi(&wined3d_desc, containing_output, window, desc,
444 fullscreen_desc);
445 IDXGIOutput_Release(containing_output);
446 if (FAILED(hr))
447 return hr;
449 if (!(object = heap_alloc_zero(sizeof(*object))))
451 ERR("Failed to allocate swapchain memory.\n");
452 return E_OUTOFMEMORY;
455 if (FAILED(hr = d3d11_swapchain_init(object, device, &wined3d_desc)))
457 WARN("Failed to initialise swapchain, hr %#x.\n", hr);
458 heap_free(object);
459 return hr;
462 TRACE("Created swapchain %p.\n", object);
464 *swapchain = &object->IDXGISwapChain1_iface;
466 return S_OK;
469 static const struct IWineDXGISwapChainFactoryVtbl dxgi_swapchain_factory_vtbl =
471 dxgi_swapchain_factory_QueryInterface,
472 dxgi_swapchain_factory_AddRef,
473 dxgi_swapchain_factory_Release,
474 dxgi_swapchain_factory_create_swapchain,
477 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
478 IDXGIFactory *factory, IDXGIAdapter *adapter,
479 const D3D_FEATURE_LEVEL *feature_levels, unsigned int level_count)
481 struct wined3d_device_parent *wined3d_device_parent;
482 struct wined3d_swapchain_desc swapchain_desc;
483 IWineDXGIDeviceParent *dxgi_device_parent;
484 struct d3d11_swapchain *swapchain;
485 struct dxgi_adapter *dxgi_adapter;
486 struct dxgi_factory *dxgi_factory;
487 struct dxgi_output *dxgi_output;
488 struct IDXGIOutput *output;
489 void *layer_base;
490 HWND window;
491 HRESULT hr;
493 if (!(dxgi_factory = unsafe_impl_from_IDXGIFactory(factory)))
495 WARN("This is not the factory we're looking for.\n");
496 return E_FAIL;
499 if (!(dxgi_adapter = unsafe_impl_from_IDXGIAdapter(adapter)))
501 WARN("This is not the adapter we're looking for.\n");
502 return E_FAIL;
505 device->IWineDXGIDevice_iface.lpVtbl = &dxgi_device_vtbl;
506 device->IWineDXGISwapChainFactory_iface.lpVtbl = &dxgi_swapchain_factory_vtbl;
507 device->refcount = 1;
508 wined3d_mutex_lock();
509 wined3d_private_store_init(&device->private_store);
511 layer_base = device + 1;
513 if (FAILED(hr = layer->create(layer->id, &layer_base, 0,
514 device, &IID_IUnknown, (void **)&device->child_layer)))
516 WARN("Failed to create device, returning %#x.\n", hr);
517 wined3d_private_store_cleanup(&device->private_store);
518 wined3d_mutex_unlock();
519 return hr;
522 if (FAILED(hr = IWineDXGIDevice_QueryInterface(&device->IWineDXGIDevice_iface,
523 &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent)))
525 ERR("DXGI device should implement IWineDXGIDeviceParent.\n");
526 IUnknown_Release(device->child_layer);
527 wined3d_private_store_cleanup(&device->private_store);
528 wined3d_mutex_unlock();
529 return hr;
531 wined3d_device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
532 IWineDXGIDeviceParent_Release(dxgi_device_parent);
534 if (FAILED(hr = wined3d_device_create(dxgi_factory->wined3d,
535 dxgi_adapter->wined3d_adapter, WINED3D_DEVICE_TYPE_HAL, NULL, 0, 4,
536 (const enum wined3d_feature_level *)feature_levels, level_count,
537 wined3d_device_parent, &device->wined3d_device)))
539 WARN("Failed to create a wined3d device, returning %#x.\n", hr);
540 IUnknown_Release(device->child_layer);
541 wined3d_private_store_cleanup(&device->private_store);
542 wined3d_mutex_unlock();
543 return hr;
546 window = dxgi_factory_get_device_window(dxgi_factory);
547 if (FAILED(hr = dxgi_get_output_from_window(factory, window, &output)))
549 ERR("Failed to get output from window %p.\n", window);
550 wined3d_device_decref(device->wined3d_device);
551 IUnknown_Release(device->child_layer);
552 wined3d_private_store_cleanup(&device->private_store);
553 wined3d_mutex_unlock();
554 return hr;
556 dxgi_output = unsafe_impl_from_IDXGIOutput(output);
558 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
559 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
560 swapchain_desc.device_window = window;
561 swapchain_desc.windowed = TRUE;
562 swapchain_desc.flags = WINED3D_SWAPCHAIN_IMPLICIT;
563 swapchain_desc.output = dxgi_output->wined3d_output;
564 IDXGIOutput_Release(output);
566 if (!(swapchain = heap_alloc_zero(sizeof(*swapchain))))
568 ERR("Failed to allocate swapchain memory.\n");
569 wined3d_device_decref(device->wined3d_device);
570 IUnknown_Release(device->child_layer);
571 wined3d_private_store_cleanup(&device->private_store);
572 wined3d_mutex_unlock();
573 return E_OUTOFMEMORY;
576 if (FAILED(hr = d3d11_swapchain_init(swapchain, device, &swapchain_desc)))
578 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
579 heap_free(swapchain);
580 wined3d_device_decref(device->wined3d_device);
581 IUnknown_Release(device->child_layer);
582 wined3d_private_store_cleanup(&device->private_store);
583 wined3d_mutex_unlock();
584 return hr;
586 device->implicit_swapchain = swapchain->wined3d_swapchain;
588 TRACE("Created swapchain %p.\n", swapchain);
590 wined3d_mutex_unlock();
592 device->adapter = &dxgi_adapter->IWineDXGIAdapter_iface;
593 IWineDXGIAdapter_AddRef(device->adapter);
595 return S_OK;