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
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);
48 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
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);
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);
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);
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);
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);
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);
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);
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)
139 return DXGI_ERROR_NOT_FOUND;
142 *adapter = This->adapters[adapter_idx];
143 IDXGIAdapter_AddRef(*adapter);
145 TRACE("Returning adapter %p\n", *adapter);
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);
157 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetWindowAssociation(IWineDXGIFactory *iface, HWND *window)
159 FIXME("iface %p, window %p stub!\n", iface, window);
164 static HRESULT STDMETHODCALLTYPE dxgi_factory_CreateSwapChain(IWineDXGIFactory *iface,
165 IUnknown *device, DXGI_SWAP_CHAIN_DESC *desc, IDXGISwapChain **swapchain)
167 struct wined3d_swapchain *wined3d_swapchain;
168 struct wined3d_swapchain_desc wined3d_desc;
169 struct wined3d_device *wined3d_device;
170 IWineDXGIDevice *dxgi_device;
174 FIXME("iface %p, device %p, desc %p, swapchain %p partial stub!\n", iface, device, desc, swapchain);
176 hr = IUnknown_QueryInterface(device, &IID_IWineDXGIDevice, (void **)&dxgi_device);
179 ERR("This is not the device we're looking for\n");
183 wined3d_device = IWineDXGIDevice_get_wined3d_device(dxgi_device);
184 IWineDXGIDevice_Release(dxgi_device);
186 count = wined3d_device_get_swapchain_count(wined3d_device);
189 FIXME("Only a single swapchain supported.\n");
190 wined3d_device_decref(wined3d_device);
194 if (!desc->OutputWindow)
196 FIXME("No output window, should use factory output window\n");
199 FIXME("Ignoring SwapEffect and Flags\n");
201 wined3d_desc.backbuffer_width = desc->BufferDesc.Width;
202 wined3d_desc.backbuffer_height = desc->BufferDesc.Height;
203 wined3d_desc.backbuffer_format = wined3dformat_from_dxgi_format(desc->BufferDesc.Format);
204 wined3d_desc.backbuffer_count = desc->BufferCount;
205 if (desc->SampleDesc.Count > 1)
207 wined3d_desc.multisample_type = desc->SampleDesc.Count;
208 wined3d_desc.multisample_quality = desc->SampleDesc.Quality;
212 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
213 wined3d_desc.multisample_quality = 0;
215 wined3d_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
216 wined3d_desc.device_window = desc->OutputWindow;
217 wined3d_desc.windowed = desc->Windowed;
218 wined3d_desc.enable_auto_depth_stencil = FALSE;
219 wined3d_desc.auto_depth_stencil_format = 0;
220 wined3d_desc.flags = 0; /* WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL? */
221 wined3d_desc.refresh_rate = desc->BufferDesc.RefreshRate.Numerator / desc->BufferDesc.RefreshRate.Denominator;
222 wined3d_desc.swap_interval = WINED3DPRESENT_INTERVAL_DEFAULT;
224 hr = wined3d_device_init_3d(wined3d_device, &wined3d_desc);
227 WARN("Failed to initialize 3D, returning %#x\n", hr);
228 wined3d_device_decref(wined3d_device);
232 wined3d_swapchain = wined3d_device_get_swapchain(wined3d_device, 0);
233 wined3d_device_decref(wined3d_device);
234 if (!wined3d_swapchain)
236 WARN("Failed to get swapchain.\n");
240 *swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
242 /* FIXME? The swapchain is created with refcount 1 by the wined3d device,
243 * but the wined3d device can't hold a real reference. */
245 TRACE("Created IDXGISwapChain %p\n", *swapchain);
250 static HRESULT STDMETHODCALLTYPE dxgi_factory_CreateSoftwareAdapter(IWineDXGIFactory *iface,
251 HMODULE swrast, IDXGIAdapter **adapter)
253 FIXME("iface %p, swrast %p, adapter %p stub!\n", iface, swrast, adapter);
258 /* IWineDXGIFactory methods */
260 static struct wined3d * STDMETHODCALLTYPE dxgi_factory_get_wined3d(IWineDXGIFactory *iface)
262 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
264 TRACE("iface %p\n", iface);
266 EnterCriticalSection(&dxgi_cs);
267 wined3d_incref(This->wined3d);
268 LeaveCriticalSection(&dxgi_cs);
269 return This->wined3d;
272 static const struct IWineDXGIFactoryVtbl dxgi_factory_vtbl =
274 /* IUnknown methods */
275 dxgi_factory_QueryInterface,
277 dxgi_factory_Release,
278 /* IDXGIObject methods */
279 dxgi_factory_SetPrivateData,
280 dxgi_factory_SetPrivateDataInterface,
281 dxgi_factory_GetPrivateData,
282 dxgi_factory_GetParent,
283 /* IDXGIFactory methods */
284 dxgi_factory_EnumAdapters,
285 dxgi_factory_MakeWindowAssociation,
286 dxgi_factory_GetWindowAssociation,
287 dxgi_factory_CreateSwapChain,
288 dxgi_factory_CreateSoftwareAdapter,
289 /* IWineDXGIFactory methods */
290 dxgi_factory_get_wined3d,
293 HRESULT dxgi_factory_init(struct dxgi_factory *factory)
298 factory->IWineDXGIFactory_iface.lpVtbl = &dxgi_factory_vtbl;
299 factory->refcount = 1;
301 EnterCriticalSection(&dxgi_cs);
302 factory->wined3d = wined3d_create(10, 0);
303 if (!factory->wined3d)
305 LeaveCriticalSection(&dxgi_cs);
306 return DXGI_ERROR_UNSUPPORTED;
309 factory->adapter_count = wined3d_get_adapter_count(factory->wined3d);
310 LeaveCriticalSection(&dxgi_cs);
311 factory->adapters = HeapAlloc(GetProcessHeap(), 0, factory->adapter_count * sizeof(*factory->adapters));
312 if (!factory->adapters)
314 ERR("Failed to allocate DXGI adapter array memory.\n");
319 for (i = 0; i < factory->adapter_count; ++i)
321 struct dxgi_adapter *adapter = HeapAlloc(GetProcessHeap(), 0, sizeof(*adapter));
326 ERR("Failed to allocate DXGI adapter memory.\n");
328 for (j = 0; j < i; ++j)
330 IDXGIAdapter_Release(factory->adapters[j]);
336 hr = dxgi_adapter_init(adapter, &factory->IWineDXGIFactory_iface, i);
341 ERR("Failed to initialize adapter, hr %#x.\n", hr);
343 HeapFree(GetProcessHeap(), 0, adapter);
344 for (j = 0; j < i; ++j)
346 IDXGIAdapter_Release(factory->adapters[j]);
351 factory->adapters[i] = (IDXGIAdapter *)adapter;
357 HeapFree(GetProcessHeap(), 0, factory->adapters);
358 EnterCriticalSection(&dxgi_cs);
359 wined3d_decref(factory->wined3d);
360 LeaveCriticalSection(&dxgi_cs);