wineoss.drv: Add period to latency calculation.
[wine.git] / dlls / dxgi / factory.c
blobc7a11cf1cb6ecbe26c05102e98a16c2bbbccdfe7
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_IWineDXGIFactory(IWineDXGIFactory *iface)
29 return CONTAINING_RECORD(iface, struct dxgi_factory, IWineDXGIFactory_iface);
32 /* IUnknown methods */
34 static HRESULT STDMETHODCALLTYPE dxgi_factory_QueryInterface(IWineDXGIFactory *iface, REFIID riid, void **object)
36 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
38 if (IsEqualGUID(riid, &IID_IUnknown)
39 || IsEqualGUID(riid, &IID_IDXGIObject)
40 || IsEqualGUID(riid, &IID_IDXGIFactory)
41 || IsEqualGUID(riid, &IID_IWineDXGIFactory))
43 IUnknown_AddRef(iface);
44 *object = iface;
45 return S_OK;
48 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
50 *object = NULL;
51 return E_NOINTERFACE;
54 static ULONG STDMETHODCALLTYPE dxgi_factory_AddRef(IWineDXGIFactory *iface)
56 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
57 ULONG refcount = InterlockedIncrement(&This->refcount);
59 TRACE("%p increasing refcount to %u\n", This, refcount);
61 return refcount;
64 static ULONG STDMETHODCALLTYPE dxgi_factory_Release(IWineDXGIFactory *iface)
66 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
67 ULONG refcount = InterlockedDecrement(&This->refcount);
69 TRACE("%p decreasing refcount to %u\n", This, refcount);
71 if (!refcount)
73 UINT i;
75 for (i = 0; i < This->adapter_count; ++i)
77 IDXGIAdapter_Release(This->adapters[i]);
79 HeapFree(GetProcessHeap(), 0, This->adapters);
81 EnterCriticalSection(&dxgi_cs);
82 wined3d_decref(This->wined3d);
83 LeaveCriticalSection(&dxgi_cs);
84 HeapFree(GetProcessHeap(), 0, This);
87 return refcount;
90 /* IDXGIObject methods */
92 static HRESULT STDMETHODCALLTYPE dxgi_factory_SetPrivateData(IWineDXGIFactory *iface,
93 REFGUID guid, UINT data_size, const void *data)
95 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
97 return E_NOTIMPL;
100 static HRESULT STDMETHODCALLTYPE dxgi_factory_SetPrivateDataInterface(IWineDXGIFactory *iface,
101 REFGUID guid, const IUnknown *object)
103 FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
105 return E_NOTIMPL;
108 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetPrivateData(IWineDXGIFactory *iface,
109 REFGUID guid, UINT *data_size, void *data)
111 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
113 return E_NOTIMPL;
116 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetParent(IWineDXGIFactory *iface, REFIID riid, void **parent)
118 WARN("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
120 *parent = NULL;
122 return E_NOINTERFACE;
125 /* IDXGIFactory methods */
127 static HRESULT STDMETHODCALLTYPE dxgi_factory_EnumAdapters(IWineDXGIFactory *iface,
128 UINT adapter_idx, IDXGIAdapter **adapter)
130 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
132 TRACE("iface %p, adapter_idx %u, adapter %p\n", iface, adapter_idx, adapter);
134 if (!adapter) return DXGI_ERROR_INVALID_CALL;
136 if (adapter_idx >= This->adapter_count)
138 *adapter = NULL;
139 return DXGI_ERROR_NOT_FOUND;
142 *adapter = This->adapters[adapter_idx];
143 IDXGIAdapter_AddRef(*adapter);
145 TRACE("Returning adapter %p\n", *adapter);
147 return S_OK;
150 static HRESULT STDMETHODCALLTYPE dxgi_factory_MakeWindowAssociation(IWineDXGIFactory *iface, HWND window, UINT flags)
152 FIXME("iface %p, window %p, flags %#x stub!\n\n", iface, window, flags);
154 return E_NOTIMPL;
157 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetWindowAssociation(IWineDXGIFactory *iface, HWND *window)
159 FIXME("iface %p, window %p stub!\n", iface, window);
161 return E_NOTIMPL;
164 /* TODO: The DXGI swapchain desc is a bit nicer than WINED3DPRESENT_PARAMETERS,
165 * change wined3d to use a structure more similar to DXGI. */
166 static HRESULT STDMETHODCALLTYPE dxgi_factory_CreateSwapChain(IWineDXGIFactory *iface,
167 IUnknown *device, DXGI_SWAP_CHAIN_DESC *desc, IDXGISwapChain **swapchain)
169 WINED3DPRESENT_PARAMETERS present_parameters;
170 struct wined3d_swapchain *wined3d_swapchain;
171 struct wined3d_device *wined3d_device;
172 IWineDXGIDevice *dxgi_device;
173 UINT count;
174 HRESULT hr;
176 FIXME("iface %p, device %p, desc %p, swapchain %p partial stub!\n", iface, device, desc, swapchain);
178 hr = IUnknown_QueryInterface(device, &IID_IWineDXGIDevice, (void **)&dxgi_device);
179 if (FAILED(hr))
181 ERR("This is not the device we're looking for\n");
182 return hr;
185 wined3d_device = IWineDXGIDevice_get_wined3d_device(dxgi_device);
186 IWineDXGIDevice_Release(dxgi_device);
188 count = wined3d_device_get_swapchain_count(wined3d_device);
189 if (count)
191 FIXME("Only a single swapchain supported.\n");
192 wined3d_device_decref(wined3d_device);
193 return E_FAIL;
196 if (!desc->OutputWindow)
198 FIXME("No output window, should use factory output window\n");
201 FIXME("Ignoring SwapEffect and Flags\n");
203 present_parameters.BackBufferWidth = desc->BufferDesc.Width;
204 present_parameters.BackBufferHeight = desc->BufferDesc.Height;
205 present_parameters.BackBufferFormat = wined3dformat_from_dxgi_format(desc->BufferDesc.Format);
206 present_parameters.BackBufferCount = desc->BufferCount;
207 if (desc->SampleDesc.Count > 1)
209 present_parameters.MultiSampleType = desc->SampleDesc.Count;
210 present_parameters.MultiSampleQuality = desc->SampleDesc.Quality;
212 else
214 present_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
215 present_parameters.MultiSampleQuality = 0;
217 present_parameters.SwapEffect = WINED3DSWAPEFFECT_DISCARD;
218 present_parameters.hDeviceWindow = desc->OutputWindow;
219 present_parameters.Windowed = desc->Windowed;
220 present_parameters.EnableAutoDepthStencil = FALSE;
221 present_parameters.AutoDepthStencilFormat = 0;
222 present_parameters.Flags = 0; /* WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL? */
223 present_parameters.FullScreen_RefreshRateInHz =
224 desc->BufferDesc.RefreshRate.Numerator / desc->BufferDesc.RefreshRate.Denominator;
225 present_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
227 hr = wined3d_device_init_3d(wined3d_device, &present_parameters);
228 if (FAILED(hr))
230 WARN("Failed to initialize 3D, returning %#x\n", hr);
231 wined3d_device_decref(wined3d_device);
232 return hr;
235 hr = wined3d_device_get_swapchain(wined3d_device, 0, &wined3d_swapchain);
236 wined3d_device_decref(wined3d_device);
237 if (FAILED(hr))
239 WARN("Failed to get swapchain, returning %#x\n", hr);
240 return hr;
243 *swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
244 wined3d_swapchain_decref(wined3d_swapchain);
246 /* FIXME? The swapchain is created with refcount 1 by the wined3d device,
247 * but the wined3d device can't hold a real reference. */
249 TRACE("Created IDXGISwapChain %p\n", *swapchain);
251 return S_OK;
254 static HRESULT STDMETHODCALLTYPE dxgi_factory_CreateSoftwareAdapter(IWineDXGIFactory *iface,
255 HMODULE swrast, IDXGIAdapter **adapter)
257 FIXME("iface %p, swrast %p, adapter %p stub!\n", iface, swrast, adapter);
259 return E_NOTIMPL;
262 /* IWineDXGIFactory methods */
264 static struct wined3d * STDMETHODCALLTYPE dxgi_factory_get_wined3d(IWineDXGIFactory *iface)
266 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
268 TRACE("iface %p\n", iface);
270 EnterCriticalSection(&dxgi_cs);
271 wined3d_incref(This->wined3d);
272 LeaveCriticalSection(&dxgi_cs);
273 return This->wined3d;
276 static const struct IWineDXGIFactoryVtbl dxgi_factory_vtbl =
278 /* IUnknown methods */
279 dxgi_factory_QueryInterface,
280 dxgi_factory_AddRef,
281 dxgi_factory_Release,
282 /* IDXGIObject methods */
283 dxgi_factory_SetPrivateData,
284 dxgi_factory_SetPrivateDataInterface,
285 dxgi_factory_GetPrivateData,
286 dxgi_factory_GetParent,
287 /* IDXGIFactory methods */
288 dxgi_factory_EnumAdapters,
289 dxgi_factory_MakeWindowAssociation,
290 dxgi_factory_GetWindowAssociation,
291 dxgi_factory_CreateSwapChain,
292 dxgi_factory_CreateSoftwareAdapter,
293 /* IWineDXGIFactory methods */
294 dxgi_factory_get_wined3d,
297 HRESULT dxgi_factory_init(struct dxgi_factory *factory)
299 HRESULT hr;
300 UINT i;
302 factory->IWineDXGIFactory_iface.lpVtbl = &dxgi_factory_vtbl;
303 factory->refcount = 1;
305 EnterCriticalSection(&dxgi_cs);
306 factory->wined3d = wined3d_create(10, 0, factory);
307 if (!factory->wined3d)
309 LeaveCriticalSection(&dxgi_cs);
310 return DXGI_ERROR_UNSUPPORTED;
313 factory->adapter_count = wined3d_get_adapter_count(factory->wined3d);
314 LeaveCriticalSection(&dxgi_cs);
315 factory->adapters = HeapAlloc(GetProcessHeap(), 0, factory->adapter_count * sizeof(*factory->adapters));
316 if (!factory->adapters)
318 ERR("Failed to allocate DXGI adapter array memory.\n");
319 hr = E_OUTOFMEMORY;
320 goto fail;
323 for (i = 0; i < factory->adapter_count; ++i)
325 struct dxgi_adapter *adapter = HeapAlloc(GetProcessHeap(), 0, sizeof(*adapter));
326 if (!adapter)
328 UINT j;
330 ERR("Failed to allocate DXGI adapter memory.\n");
332 for (j = 0; j < i; ++j)
334 IDXGIAdapter_Release(factory->adapters[j]);
336 hr = E_OUTOFMEMORY;
337 goto fail;
340 hr = dxgi_adapter_init(adapter, &factory->IWineDXGIFactory_iface, i);
341 if (FAILED(hr))
343 UINT j;
345 ERR("Failed to initialize adapter, hr %#x.\n", hr);
347 HeapFree(GetProcessHeap(), 0, adapter);
348 for (j = 0; j < i; ++j)
350 IDXGIAdapter_Release(factory->adapters[j]);
352 goto fail;
355 factory->adapters[i] = (IDXGIAdapter *)adapter;
358 return S_OK;
360 fail:
361 HeapFree(GetProcessHeap(), 0, factory->adapters);
362 EnterCriticalSection(&dxgi_cs);
363 wined3d_decref(factory->wined3d);
364 LeaveCriticalSection(&dxgi_cs);
365 return hr;