dxgi: Implement dxgi_factory_SetPrivateDataInterface().
[wine/multimedia.git] / dlls / dxgi / factory.c
blob3943051be09ea222721a755ecf0154ba5a5a535b
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_factory *impl_from_IDXGIFactory1(IDXGIFactory1 *iface)
29 return CONTAINING_RECORD(iface, struct dxgi_factory, IDXGIFactory1_iface);
32 static HRESULT STDMETHODCALLTYPE dxgi_factory_QueryInterface(IDXGIFactory1 *iface, REFIID iid, void **out)
34 struct dxgi_factory *factory = impl_from_IDXGIFactory1(iface);
36 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
38 if ((factory->extended && IsEqualGUID(iid, &IID_IDXGIFactory1))
39 || IsEqualGUID(iid, &IID_IDXGIFactory)
40 || IsEqualGUID(iid, &IID_IDXGIObject)
41 || IsEqualGUID(iid, &IID_IUnknown))
43 IUnknown_AddRef(iface);
44 *out = iface;
45 return S_OK;
48 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
50 *out = NULL;
51 return E_NOINTERFACE;
54 static ULONG STDMETHODCALLTYPE dxgi_factory_AddRef(IDXGIFactory1 *iface)
56 struct dxgi_factory *factory = impl_from_IDXGIFactory1(iface);
57 ULONG refcount = InterlockedIncrement(&factory->refcount);
59 TRACE("%p increasing refcount to %u.\n", iface, refcount);
61 return refcount;
64 static ULONG STDMETHODCALLTYPE dxgi_factory_Release(IDXGIFactory1 *iface)
66 struct dxgi_factory *factory = impl_from_IDXGIFactory1(iface);
67 ULONG refcount = InterlockedDecrement(&factory->refcount);
69 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
71 if (!refcount)
73 UINT i;
75 if (factory->device_window)
76 DestroyWindow(factory->device_window);
77 for (i = 0; i < factory->adapter_count; ++i)
79 IDXGIAdapter1_Release(factory->adapters[i]);
81 HeapFree(GetProcessHeap(), 0, factory->adapters);
83 EnterCriticalSection(&dxgi_cs);
84 wined3d_decref(factory->wined3d);
85 LeaveCriticalSection(&dxgi_cs);
86 wined3d_private_store_cleanup(&factory->private_store);
87 HeapFree(GetProcessHeap(), 0, factory);
90 return refcount;
93 static HRESULT STDMETHODCALLTYPE dxgi_factory_SetPrivateData(IDXGIFactory1 *iface,
94 REFGUID guid, UINT data_size, const void *data)
96 struct dxgi_factory *factory = impl_from_IDXGIFactory1(iface);
98 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
100 return dxgi_set_private_data(&factory->private_store, guid, data_size, data);
103 static HRESULT STDMETHODCALLTYPE dxgi_factory_SetPrivateDataInterface(IDXGIFactory1 *iface,
104 REFGUID guid, const IUnknown *object)
106 struct dxgi_factory *factory = impl_from_IDXGIFactory1(iface);
108 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
110 return dxgi_set_private_data_interface(&factory->private_store, guid, object);
113 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetPrivateData(IDXGIFactory1 *iface,
114 REFGUID guid, UINT *data_size, void *data)
116 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
118 return E_NOTIMPL;
121 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetParent(IDXGIFactory1 *iface, REFIID iid, void **parent)
123 WARN("iface %p, iid %s, parent %p.\n", iface, debugstr_guid(iid), parent);
125 *parent = NULL;
127 return E_NOINTERFACE;
130 static HRESULT STDMETHODCALLTYPE dxgi_factory_EnumAdapters1(IDXGIFactory1 *iface,
131 UINT adapter_idx, IDXGIAdapter1 **adapter)
133 struct dxgi_factory *factory = impl_from_IDXGIFactory1(iface);
135 TRACE("iface %p, adapter_idx %u, adapter %p.\n", iface, adapter_idx, adapter);
137 if (!adapter)
138 return DXGI_ERROR_INVALID_CALL;
140 if (adapter_idx >= factory->adapter_count)
142 *adapter = NULL;
143 return DXGI_ERROR_NOT_FOUND;
146 *adapter = (IDXGIAdapter1 *)factory->adapters[adapter_idx];
147 IDXGIAdapter1_AddRef(*adapter);
149 TRACE("Returning adapter %p.\n", *adapter);
151 return S_OK;
154 static HRESULT STDMETHODCALLTYPE dxgi_factory_EnumAdapters(IDXGIFactory1 *iface,
155 UINT adapter_idx, IDXGIAdapter **adapter)
157 TRACE("iface %p, adapter_idx %u, adapter %p.\n", iface, adapter_idx, adapter);
159 return dxgi_factory_EnumAdapters1(iface, adapter_idx, (IDXGIAdapter1 **)adapter);
162 static HRESULT STDMETHODCALLTYPE dxgi_factory_MakeWindowAssociation(IDXGIFactory1 *iface, HWND window, UINT flags)
164 FIXME("iface %p, window %p, flags %#x stub!\n", iface, window, flags);
166 return E_NOTIMPL;
169 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetWindowAssociation(IDXGIFactory1 *iface, HWND *window)
171 FIXME("iface %p, window %p stub!\n", iface, window);
173 return E_NOTIMPL;
176 static UINT dxgi_rational_to_uint(const DXGI_RATIONAL *rational)
178 if (rational->Denominator)
179 return rational->Numerator / rational->Denominator;
180 else
181 return rational->Numerator;
184 static HRESULT STDMETHODCALLTYPE dxgi_factory_CreateSwapChain(IDXGIFactory1 *iface,
185 IUnknown *device, DXGI_SWAP_CHAIN_DESC *desc, IDXGISwapChain **swapchain)
187 struct wined3d_swapchain *wined3d_swapchain;
188 struct wined3d_swapchain_desc wined3d_desc;
189 IWineDXGIDevice *dxgi_device;
190 HRESULT hr;
192 FIXME("iface %p, device %p, desc %p, swapchain %p partial stub!\n", iface, device, desc, swapchain);
194 hr = IUnknown_QueryInterface(device, &IID_IWineDXGIDevice, (void **)&dxgi_device);
195 if (FAILED(hr))
197 ERR("This is not the device we're looking for\n");
198 return hr;
201 if (!desc->OutputWindow)
203 FIXME("No output window, should use factory output window\n");
206 FIXME("Ignoring SwapEffect and Flags\n");
208 wined3d_desc.backbuffer_width = desc->BufferDesc.Width;
209 wined3d_desc.backbuffer_height = desc->BufferDesc.Height;
210 wined3d_desc.backbuffer_format = wined3dformat_from_dxgi_format(desc->BufferDesc.Format);
211 wined3d_desc.backbuffer_count = desc->BufferCount;
212 if (desc->SampleDesc.Count > 1)
214 wined3d_desc.multisample_type = desc->SampleDesc.Count;
215 wined3d_desc.multisample_quality = desc->SampleDesc.Quality;
217 else
219 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
220 wined3d_desc.multisample_quality = 0;
222 wined3d_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
223 wined3d_desc.device_window = desc->OutputWindow;
224 wined3d_desc.windowed = desc->Windowed;
225 wined3d_desc.enable_auto_depth_stencil = FALSE;
226 wined3d_desc.auto_depth_stencil_format = 0;
227 wined3d_desc.flags = 0; /* WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL? */
228 wined3d_desc.refresh_rate = dxgi_rational_to_uint(&desc->BufferDesc.RefreshRate);
229 wined3d_desc.swap_interval = WINED3DPRESENT_INTERVAL_DEFAULT;
231 hr = IWineDXGIDevice_create_swapchain(dxgi_device, &wined3d_desc, &wined3d_swapchain);
232 IWineDXGIDevice_Release(dxgi_device);
233 if (FAILED(hr))
235 WARN("Failed to create swapchain, hr %#x.\n", hr);
236 return hr;
239 *swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
241 return S_OK;
244 static HRESULT STDMETHODCALLTYPE dxgi_factory_CreateSoftwareAdapter(IDXGIFactory1 *iface,
245 HMODULE swrast, IDXGIAdapter **adapter)
247 FIXME("iface %p, swrast %p, adapter %p stub!\n", iface, swrast, adapter);
249 return E_NOTIMPL;
252 static BOOL STDMETHODCALLTYPE dxgi_factory_IsCurrent(IDXGIFactory1 *iface)
254 FIXME("iface %p stub!\n", iface);
256 return TRUE;
259 static const struct IDXGIFactory1Vtbl dxgi_factory_vtbl =
261 dxgi_factory_QueryInterface,
262 dxgi_factory_AddRef,
263 dxgi_factory_Release,
264 dxgi_factory_SetPrivateData,
265 dxgi_factory_SetPrivateDataInterface,
266 dxgi_factory_GetPrivateData,
267 dxgi_factory_GetParent,
268 dxgi_factory_EnumAdapters,
269 dxgi_factory_MakeWindowAssociation,
270 dxgi_factory_GetWindowAssociation,
271 dxgi_factory_CreateSwapChain,
272 dxgi_factory_CreateSoftwareAdapter,
273 dxgi_factory_EnumAdapters1,
274 dxgi_factory_IsCurrent,
277 struct dxgi_factory *unsafe_impl_from_IDXGIFactory1(IDXGIFactory1 *iface)
279 if (!iface)
280 return NULL;
281 assert(iface->lpVtbl == &dxgi_factory_vtbl);
282 return CONTAINING_RECORD(iface, struct dxgi_factory, IDXGIFactory1_iface);
285 static HRESULT dxgi_factory_init(struct dxgi_factory *factory, BOOL extended)
287 HRESULT hr;
288 UINT i;
290 factory->IDXGIFactory1_iface.lpVtbl = &dxgi_factory_vtbl;
291 factory->refcount = 1;
292 wined3d_private_store_init(&factory->private_store);
294 EnterCriticalSection(&dxgi_cs);
295 factory->wined3d = wined3d_create(0);
296 if (!factory->wined3d)
298 LeaveCriticalSection(&dxgi_cs);
299 wined3d_private_store_cleanup(&factory->private_store);
300 return DXGI_ERROR_UNSUPPORTED;
303 factory->adapter_count = wined3d_get_adapter_count(factory->wined3d);
304 LeaveCriticalSection(&dxgi_cs);
305 factory->adapters = HeapAlloc(GetProcessHeap(), 0, factory->adapter_count * sizeof(*factory->adapters));
306 if (!factory->adapters)
308 ERR("Failed to allocate DXGI adapter array memory.\n");
309 hr = E_OUTOFMEMORY;
310 goto fail;
313 for (i = 0; i < factory->adapter_count; ++i)
315 struct dxgi_adapter *adapter = HeapAlloc(GetProcessHeap(), 0, sizeof(*adapter));
316 if (!adapter)
318 UINT j;
320 ERR("Failed to allocate DXGI adapter memory.\n");
322 for (j = 0; j < i; ++j)
324 IDXGIAdapter1_Release(factory->adapters[j]);
326 hr = E_OUTOFMEMORY;
327 goto fail;
330 if (FAILED(hr = dxgi_adapter_init(adapter, factory, i)))
332 UINT j;
334 ERR("Failed to initialize adapter, hr %#x.\n", hr);
336 HeapFree(GetProcessHeap(), 0, adapter);
337 for (j = 0; j < i; ++j)
339 IDXGIAdapter1_Release(factory->adapters[j]);
341 goto fail;
344 factory->adapters[i] = &adapter->IDXGIAdapter1_iface;
347 factory->extended = extended;
349 return S_OK;
351 fail:
352 HeapFree(GetProcessHeap(), 0, factory->adapters);
353 EnterCriticalSection(&dxgi_cs);
354 wined3d_decref(factory->wined3d);
355 LeaveCriticalSection(&dxgi_cs);
356 wined3d_private_store_cleanup(&factory->private_store);
357 return hr;
360 HRESULT dxgi_factory_create(REFIID riid, void **factory, BOOL extended)
362 struct dxgi_factory *object;
363 HRESULT hr;
365 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
366 return E_OUTOFMEMORY;
368 if (FAILED(hr = dxgi_factory_init(object, extended)))
370 WARN("Failed to initialize factory, hr %#x.\n", hr);
371 HeapFree(GetProcessHeap(), 0, object);
372 return hr;
375 TRACE("Created factory %p.\n", object);
377 hr = IDXGIFactory1_QueryInterface(&object->IDXGIFactory1_iface, riid, factory);
378 IDXGIFactory1_Release(&object->IDXGIFactory1_iface);
380 return hr;
383 HWND dxgi_factory_get_device_window(struct dxgi_factory *factory)
385 EnterCriticalSection(&dxgi_cs);
387 if (!factory->device_window)
389 if (!(factory->device_window = CreateWindowA("static", "DXGI device window",
390 WS_DISABLED, 0, 0, 0, 0, NULL, NULL, NULL, NULL)))
392 LeaveCriticalSection(&dxgi_cs);
393 ERR("Failed to create a window.\n");
394 return NULL;
396 TRACE("Created device window %p for factory %p.\n", factory->device_window, factory);
399 LeaveCriticalSection(&dxgi_cs);
401 return factory->device_window;