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 struct dxgi_factory
*factory
= impl_from_IWineDXGIFactory(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_IDXGIFactory
)
43 || (factory
->extended
&& IsEqualGUID(riid
, &IID_IDXGIFactory1
))
44 || IsEqualGUID(riid
, &IID_IWineDXGIFactory
))
46 IUnknown_AddRef(iface
);
51 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid
));
57 static ULONG STDMETHODCALLTYPE
dxgi_factory_AddRef(IWineDXGIFactory
*iface
)
59 struct dxgi_factory
*This
= impl_from_IWineDXGIFactory(iface
);
60 ULONG refcount
= InterlockedIncrement(&This
->refcount
);
62 TRACE("%p increasing refcount to %u\n", This
, refcount
);
67 static ULONG STDMETHODCALLTYPE
dxgi_factory_Release(IWineDXGIFactory
*iface
)
69 struct dxgi_factory
*This
= impl_from_IWineDXGIFactory(iface
);
70 ULONG refcount
= InterlockedDecrement(&This
->refcount
);
72 TRACE("%p decreasing refcount to %u\n", This
, refcount
);
78 for (i
= 0; i
< This
->adapter_count
; ++i
)
80 IWineDXGIAdapter_Release(This
->adapters
[i
]);
82 HeapFree(GetProcessHeap(), 0, This
->adapters
);
84 EnterCriticalSection(&dxgi_cs
);
85 wined3d_decref(This
->wined3d
);
86 LeaveCriticalSection(&dxgi_cs
);
87 HeapFree(GetProcessHeap(), 0, This
);
93 /* IDXGIObject methods */
95 static HRESULT STDMETHODCALLTYPE
dxgi_factory_SetPrivateData(IWineDXGIFactory
*iface
,
96 REFGUID guid
, UINT data_size
, const void *data
)
98 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface
, debugstr_guid(guid
), data_size
, data
);
103 static HRESULT STDMETHODCALLTYPE
dxgi_factory_SetPrivateDataInterface(IWineDXGIFactory
*iface
,
104 REFGUID guid
, const IUnknown
*object
)
106 FIXME("iface %p, guid %s, object %p stub!\n", iface
, debugstr_guid(guid
), object
);
111 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetPrivateData(IWineDXGIFactory
*iface
,
112 REFGUID guid
, UINT
*data_size
, void *data
)
114 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface
, debugstr_guid(guid
), data_size
, data
);
119 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetParent(IWineDXGIFactory
*iface
, REFIID riid
, void **parent
)
121 WARN("iface %p, riid %s, parent %p.\n", iface
, debugstr_guid(riid
), parent
);
125 return E_NOINTERFACE
;
128 /* IDXGIFactory methods */
130 static HRESULT STDMETHODCALLTYPE
dxgi_factory_EnumAdapters1(IWineDXGIFactory
*iface
,
131 UINT adapter_idx
, IDXGIAdapter1
**adapter
)
133 struct dxgi_factory
*factory
= impl_from_IWineDXGIFactory(iface
);
135 TRACE("iface %p, adapter_idx %u, adapter %p\n", iface
, adapter_idx
, adapter
);
138 return DXGI_ERROR_INVALID_CALL
;
140 if (adapter_idx
>= factory
->adapter_count
)
143 return DXGI_ERROR_NOT_FOUND
;
146 *adapter
= (IDXGIAdapter1
*)factory
->adapters
[adapter_idx
];
147 IDXGIAdapter1_AddRef(*adapter
);
149 TRACE("Returning adapter %p\n", *adapter
);
154 static HRESULT STDMETHODCALLTYPE
dxgi_factory_EnumAdapters(IWineDXGIFactory
*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(IWineDXGIFactory
*iface
, HWND window
, UINT flags
)
164 FIXME("iface %p, window %p, flags %#x stub!\n", iface
, window
, flags
);
169 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetWindowAssociation(IWineDXGIFactory
*iface
, HWND
*window
)
171 FIXME("iface %p, window %p stub!\n", iface
, window
);
176 static UINT
dxgi_rational_to_uint(const DXGI_RATIONAL
*rational
)
178 if (rational
->Denominator
)
179 return rational
->Numerator
/ rational
->Denominator
;
181 return rational
->Numerator
;
184 static HRESULT STDMETHODCALLTYPE
dxgi_factory_CreateSwapChain(IWineDXGIFactory
*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 struct wined3d_device
*wined3d_device
;
190 IWineDXGIDevice
*dxgi_device
;
194 FIXME("iface %p, device %p, desc %p, swapchain %p partial stub!\n", iface
, device
, desc
, swapchain
);
196 hr
= IUnknown_QueryInterface(device
, &IID_IWineDXGIDevice
, (void **)&dxgi_device
);
199 ERR("This is not the device we're looking for\n");
203 wined3d_device
= IWineDXGIDevice_get_wined3d_device(dxgi_device
);
204 IWineDXGIDevice_Release(dxgi_device
);
206 count
= wined3d_device_get_swapchain_count(wined3d_device
);
209 FIXME("Only a single swapchain supported.\n");
210 wined3d_device_decref(wined3d_device
);
214 if (!desc
->OutputWindow
)
216 FIXME("No output window, should use factory output window\n");
219 FIXME("Ignoring SwapEffect and Flags\n");
221 wined3d_desc
.backbuffer_width
= desc
->BufferDesc
.Width
;
222 wined3d_desc
.backbuffer_height
= desc
->BufferDesc
.Height
;
223 wined3d_desc
.backbuffer_format
= wined3dformat_from_dxgi_format(desc
->BufferDesc
.Format
);
224 wined3d_desc
.backbuffer_count
= desc
->BufferCount
;
225 if (desc
->SampleDesc
.Count
> 1)
227 wined3d_desc
.multisample_type
= desc
->SampleDesc
.Count
;
228 wined3d_desc
.multisample_quality
= desc
->SampleDesc
.Quality
;
232 wined3d_desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
233 wined3d_desc
.multisample_quality
= 0;
235 wined3d_desc
.swap_effect
= WINED3D_SWAP_EFFECT_DISCARD
;
236 wined3d_desc
.device_window
= desc
->OutputWindow
;
237 wined3d_desc
.windowed
= desc
->Windowed
;
238 wined3d_desc
.enable_auto_depth_stencil
= FALSE
;
239 wined3d_desc
.auto_depth_stencil_format
= 0;
240 wined3d_desc
.flags
= 0; /* WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL? */
241 wined3d_desc
.refresh_rate
= dxgi_rational_to_uint(&desc
->BufferDesc
.RefreshRate
);
242 wined3d_desc
.swap_interval
= WINED3DPRESENT_INTERVAL_DEFAULT
;
244 hr
= wined3d_device_init_3d(wined3d_device
, &wined3d_desc
);
247 WARN("Failed to initialize 3D, returning %#x\n", hr
);
248 wined3d_device_decref(wined3d_device
);
252 wined3d_swapchain
= wined3d_device_get_swapchain(wined3d_device
, 0);
253 wined3d_device_decref(wined3d_device
);
254 if (!wined3d_swapchain
)
256 WARN("Failed to get swapchain.\n");
260 *swapchain
= wined3d_swapchain_get_parent(wined3d_swapchain
);
262 /* FIXME? The swapchain is created with refcount 1 by the wined3d device,
263 * but the wined3d device can't hold a real reference. */
265 TRACE("Created IDXGISwapChain %p\n", *swapchain
);
270 static HRESULT STDMETHODCALLTYPE
dxgi_factory_CreateSoftwareAdapter(IWineDXGIFactory
*iface
,
271 HMODULE swrast
, IDXGIAdapter
**adapter
)
273 FIXME("iface %p, swrast %p, adapter %p stub!\n", iface
, swrast
, adapter
);
278 static BOOL STDMETHODCALLTYPE
dxgi_factory_IsCurrent(IWineDXGIFactory
*iface
)
280 FIXME("iface %p stub!\n", iface
);
285 /* IWineDXGIFactory methods */
287 static struct wined3d
* STDMETHODCALLTYPE
dxgi_factory_get_wined3d(IWineDXGIFactory
*iface
)
289 struct dxgi_factory
*This
= impl_from_IWineDXGIFactory(iface
);
291 TRACE("iface %p\n", iface
);
293 EnterCriticalSection(&dxgi_cs
);
294 wined3d_incref(This
->wined3d
);
295 LeaveCriticalSection(&dxgi_cs
);
296 return This
->wined3d
;
299 static const struct IWineDXGIFactoryVtbl dxgi_factory_vtbl
=
301 /* IUnknown methods */
302 dxgi_factory_QueryInterface
,
304 dxgi_factory_Release
,
305 /* IDXGIObject methods */
306 dxgi_factory_SetPrivateData
,
307 dxgi_factory_SetPrivateDataInterface
,
308 dxgi_factory_GetPrivateData
,
309 dxgi_factory_GetParent
,
310 /* IDXGIFactory methods */
311 dxgi_factory_EnumAdapters
,
312 dxgi_factory_MakeWindowAssociation
,
313 dxgi_factory_GetWindowAssociation
,
314 dxgi_factory_CreateSwapChain
,
315 dxgi_factory_CreateSoftwareAdapter
,
316 /* IDXGIFactory1 methods */
317 dxgi_factory_EnumAdapters1
,
318 dxgi_factory_IsCurrent
,
319 /* IWineDXGIFactory methods */
320 dxgi_factory_get_wined3d
,
323 static HRESULT
dxgi_factory_init(struct dxgi_factory
*factory
, BOOL extended
)
328 factory
->IWineDXGIFactory_iface
.lpVtbl
= &dxgi_factory_vtbl
;
329 factory
->refcount
= 1;
331 EnterCriticalSection(&dxgi_cs
);
332 factory
->wined3d
= wined3d_create(0);
333 if (!factory
->wined3d
)
335 LeaveCriticalSection(&dxgi_cs
);
336 return DXGI_ERROR_UNSUPPORTED
;
339 factory
->adapter_count
= wined3d_get_adapter_count(factory
->wined3d
);
340 LeaveCriticalSection(&dxgi_cs
);
341 factory
->adapters
= HeapAlloc(GetProcessHeap(), 0, factory
->adapter_count
* sizeof(*factory
->adapters
));
342 if (!factory
->adapters
)
344 ERR("Failed to allocate DXGI adapter array memory.\n");
349 for (i
= 0; i
< factory
->adapter_count
; ++i
)
351 struct dxgi_adapter
*adapter
= HeapAlloc(GetProcessHeap(), 0, sizeof(*adapter
));
356 ERR("Failed to allocate DXGI adapter memory.\n");
358 for (j
= 0; j
< i
; ++j
)
360 IWineDXGIAdapter_Release(factory
->adapters
[j
]);
366 hr
= dxgi_adapter_init(adapter
, &factory
->IWineDXGIFactory_iface
, i
);
371 ERR("Failed to initialize adapter, hr %#x.\n", hr
);
373 HeapFree(GetProcessHeap(), 0, adapter
);
374 for (j
= 0; j
< i
; ++j
)
376 IWineDXGIAdapter_Release(factory
->adapters
[j
]);
381 factory
->adapters
[i
] = &adapter
->IWineDXGIAdapter_iface
;
384 factory
->extended
= extended
;
389 HeapFree(GetProcessHeap(), 0, factory
->adapters
);
390 EnterCriticalSection(&dxgi_cs
);
391 wined3d_decref(factory
->wined3d
);
392 LeaveCriticalSection(&dxgi_cs
);
396 HRESULT
dxgi_factory_create(REFIID riid
, void **factory
, BOOL extended
)
398 struct dxgi_factory
*object
;
401 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
402 return E_OUTOFMEMORY
;
404 if (FAILED(hr
= dxgi_factory_init(object
, extended
)))
406 WARN("Failed to initialize factory, hr %#x.\n", hr
);
407 HeapFree(GetProcessHeap(), 0, object
);
411 TRACE("Created factory %p.\n", object
);
413 hr
= IWineDXGIFactory_QueryInterface(&object
->IWineDXGIFactory_iface
, riid
, factory
);
414 IWineDXGIFactory_Release(&object
->IWineDXGIFactory_iface
);