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_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
);
48 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid
));
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
);
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
);
73 if (factory
->device_window
)
74 DestroyWindow(factory
->device_window
);
77 wined3d_decref(factory
->wined3d
);
78 wined3d_mutex_unlock();
79 wined3d_private_store_cleanup(&factory
->private_store
);
80 HeapFree(GetProcessHeap(), 0, factory
);
86 static HRESULT STDMETHODCALLTYPE
dxgi_factory_SetPrivateData(IDXGIFactory1
*iface
,
87 REFGUID guid
, UINT data_size
, const void *data
)
89 struct dxgi_factory
*factory
= impl_from_IDXGIFactory1(iface
);
91 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
93 return dxgi_set_private_data(&factory
->private_store
, guid
, data_size
, data
);
96 static HRESULT STDMETHODCALLTYPE
dxgi_factory_SetPrivateDataInterface(IDXGIFactory1
*iface
,
97 REFGUID guid
, const IUnknown
*object
)
99 struct dxgi_factory
*factory
= impl_from_IDXGIFactory1(iface
);
101 TRACE("iface %p, guid %s, object %p.\n", iface
, debugstr_guid(guid
), object
);
103 return dxgi_set_private_data_interface(&factory
->private_store
, guid
, object
);
106 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetPrivateData(IDXGIFactory1
*iface
,
107 REFGUID guid
, UINT
*data_size
, void *data
)
109 struct dxgi_factory
*factory
= impl_from_IDXGIFactory1(iface
);
111 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
113 return dxgi_get_private_data(&factory
->private_store
, guid
, data_size
, data
);
116 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetParent(IDXGIFactory1
*iface
, REFIID iid
, void **parent
)
118 WARN("iface %p, iid %s, parent %p.\n", iface
, debugstr_guid(iid
), parent
);
122 return E_NOINTERFACE
;
125 static HRESULT STDMETHODCALLTYPE
dxgi_factory_EnumAdapters1(IDXGIFactory1
*iface
,
126 UINT adapter_idx
, IDXGIAdapter1
**adapter
)
128 struct dxgi_factory
*factory
= impl_from_IDXGIFactory1(iface
);
129 struct dxgi_adapter
*adapter_object
;
133 TRACE("iface %p, adapter_idx %u, adapter %p.\n", iface
, adapter_idx
, adapter
);
136 return DXGI_ERROR_INVALID_CALL
;
138 wined3d_mutex_lock();
139 adapter_count
= wined3d_get_adapter_count(factory
->wined3d
);
140 wined3d_mutex_unlock();
142 if (adapter_idx
>= adapter_count
)
145 return DXGI_ERROR_NOT_FOUND
;
148 if (FAILED(hr
= dxgi_adapter_create(factory
, adapter_idx
, &adapter_object
)))
154 *adapter
= &adapter_object
->IDXGIAdapter1_iface
;
156 TRACE("Returning adapter %p.\n", *adapter
);
161 static HRESULT STDMETHODCALLTYPE
dxgi_factory_EnumAdapters(IDXGIFactory1
*iface
,
162 UINT adapter_idx
, IDXGIAdapter
**adapter
)
164 TRACE("iface %p, adapter_idx %u, adapter %p.\n", iface
, adapter_idx
, adapter
);
166 return dxgi_factory_EnumAdapters1(iface
, adapter_idx
, (IDXGIAdapter1
**)adapter
);
169 static HRESULT STDMETHODCALLTYPE
dxgi_factory_MakeWindowAssociation(IDXGIFactory1
*iface
, HWND window
, UINT flags
)
171 FIXME("iface %p, window %p, flags %#x stub!\n", iface
, window
, flags
);
176 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetWindowAssociation(IDXGIFactory1
*iface
, HWND
*window
)
178 FIXME("iface %p, window %p stub!\n", iface
, window
);
183 static UINT
dxgi_rational_to_uint(const DXGI_RATIONAL
*rational
)
185 if (rational
->Denominator
)
186 return rational
->Numerator
/ rational
->Denominator
;
188 return rational
->Numerator
;
191 static HRESULT STDMETHODCALLTYPE
dxgi_factory_CreateSwapChain(IDXGIFactory1
*iface
,
192 IUnknown
*device
, DXGI_SWAP_CHAIN_DESC
*desc
, IDXGISwapChain
**swapchain
)
194 struct wined3d_swapchain
*wined3d_swapchain
;
195 struct wined3d_swapchain_desc wined3d_desc
;
196 unsigned int min_buffer_count
;
197 IWineDXGIDevice
*dxgi_device
;
200 FIXME("iface %p, device %p, desc %p, swapchain %p partial stub!\n", iface
, device
, desc
, swapchain
);
202 switch (desc
->SwapEffect
)
204 case DXGI_SWAP_EFFECT_DISCARD
:
205 case DXGI_SWAP_EFFECT_SEQUENTIAL
:
206 min_buffer_count
= 1;
209 case DXGI_SWAP_EFFECT_FLIP_DISCARD
:
210 case DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL
:
211 min_buffer_count
= 2;
215 WARN("Invalid swap effect %u used, returning DXGI_ERROR_INVALID_CALL.\n", desc
->SwapEffect
);
216 return DXGI_ERROR_INVALID_CALL
;
219 if (desc
->BufferCount
< min_buffer_count
|| desc
->BufferCount
> 16)
221 WARN("BufferCount is %u, returning DXGI_ERROR_INVALID_CALL.\n", desc
->BufferCount
);
222 return DXGI_ERROR_INVALID_CALL
;
224 if (!desc
->OutputWindow
)
226 FIXME("No output window, should use factory output window.\n");
229 hr
= IUnknown_QueryInterface(device
, &IID_IWineDXGIDevice
, (void **)&dxgi_device
);
232 ERR("This is not the device we're looking for\n");
236 FIXME("Ignoring SwapEffect %#x.\n", desc
->SwapEffect
);
238 wined3d_desc
.backbuffer_width
= desc
->BufferDesc
.Width
;
239 wined3d_desc
.backbuffer_height
= desc
->BufferDesc
.Height
;
240 wined3d_desc
.backbuffer_format
= wined3dformat_from_dxgi_format(desc
->BufferDesc
.Format
);
241 wined3d_desc
.backbuffer_count
= desc
->BufferCount
;
242 wined3d_sample_desc_from_dxgi(&wined3d_desc
.multisample_type
,
243 &wined3d_desc
.multisample_quality
, &desc
->SampleDesc
);
244 wined3d_desc
.swap_effect
= WINED3D_SWAP_EFFECT_DISCARD
;
245 wined3d_desc
.device_window
= desc
->OutputWindow
;
246 wined3d_desc
.windowed
= desc
->Windowed
;
247 wined3d_desc
.enable_auto_depth_stencil
= FALSE
;
248 wined3d_desc
.auto_depth_stencil_format
= 0;
249 wined3d_desc
.flags
= wined3d_swapchain_flags_from_dxgi(desc
->Flags
);
250 wined3d_desc
.refresh_rate
= dxgi_rational_to_uint(&desc
->BufferDesc
.RefreshRate
);
251 wined3d_desc
.swap_interval
= WINED3DPRESENT_INTERVAL_DEFAULT
;
252 wined3d_desc
.auto_restore_display_mode
= TRUE
;
254 hr
= IWineDXGIDevice_create_swapchain(dxgi_device
, &wined3d_desc
, FALSE
, &wined3d_swapchain
);
255 IWineDXGIDevice_Release(dxgi_device
);
258 WARN("Failed to create swapchain, hr %#x.\n", hr
);
262 wined3d_mutex_lock();
263 *swapchain
= wined3d_swapchain_get_parent(wined3d_swapchain
);
264 wined3d_mutex_unlock();
269 static HRESULT STDMETHODCALLTYPE
dxgi_factory_CreateSoftwareAdapter(IDXGIFactory1
*iface
,
270 HMODULE swrast
, IDXGIAdapter
**adapter
)
272 FIXME("iface %p, swrast %p, adapter %p stub!\n", iface
, swrast
, adapter
);
277 static BOOL STDMETHODCALLTYPE
dxgi_factory_IsCurrent(IDXGIFactory1
*iface
)
279 FIXME("iface %p stub!\n", iface
);
284 static const struct IDXGIFactory1Vtbl dxgi_factory_vtbl
=
286 dxgi_factory_QueryInterface
,
288 dxgi_factory_Release
,
289 dxgi_factory_SetPrivateData
,
290 dxgi_factory_SetPrivateDataInterface
,
291 dxgi_factory_GetPrivateData
,
292 dxgi_factory_GetParent
,
293 dxgi_factory_EnumAdapters
,
294 dxgi_factory_MakeWindowAssociation
,
295 dxgi_factory_GetWindowAssociation
,
296 dxgi_factory_CreateSwapChain
,
297 dxgi_factory_CreateSoftwareAdapter
,
298 dxgi_factory_EnumAdapters1
,
299 dxgi_factory_IsCurrent
,
302 struct dxgi_factory
*unsafe_impl_from_IDXGIFactory1(IDXGIFactory1
*iface
)
306 assert(iface
->lpVtbl
== &dxgi_factory_vtbl
);
307 return CONTAINING_RECORD(iface
, struct dxgi_factory
, IDXGIFactory1_iface
);
310 static HRESULT
dxgi_factory_init(struct dxgi_factory
*factory
, BOOL extended
)
312 factory
->IDXGIFactory1_iface
.lpVtbl
= &dxgi_factory_vtbl
;
313 factory
->refcount
= 1;
314 wined3d_private_store_init(&factory
->private_store
);
316 wined3d_mutex_lock();
317 factory
->wined3d
= wined3d_create(0);
318 wined3d_mutex_unlock();
319 if (!factory
->wined3d
)
321 wined3d_private_store_cleanup(&factory
->private_store
);
322 return DXGI_ERROR_UNSUPPORTED
;
325 factory
->extended
= extended
;
330 HRESULT
dxgi_factory_create(REFIID riid
, void **factory
, BOOL extended
)
332 struct dxgi_factory
*object
;
335 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
336 return E_OUTOFMEMORY
;
338 if (FAILED(hr
= dxgi_factory_init(object
, extended
)))
340 WARN("Failed to initialize factory, hr %#x.\n", hr
);
341 HeapFree(GetProcessHeap(), 0, object
);
345 TRACE("Created factory %p.\n", object
);
347 hr
= IDXGIFactory1_QueryInterface(&object
->IDXGIFactory1_iface
, riid
, factory
);
348 IDXGIFactory1_Release(&object
->IDXGIFactory1_iface
);
353 HWND
dxgi_factory_get_device_window(struct dxgi_factory
*factory
)
355 wined3d_mutex_lock();
357 if (!factory
->device_window
)
359 if (!(factory
->device_window
= CreateWindowA("static", "DXGI device window",
360 WS_DISABLED
, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
)))
362 wined3d_mutex_unlock();
363 ERR("Failed to create a window.\n");
366 TRACE("Created device window %p for factory %p.\n", factory
->device_window
, factory
);
369 wined3d_mutex_unlock();
371 return factory
->device_window
;