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_IDXGIFactory1
)
42 || IsEqualGUID(riid
, &IID_IWineDXGIFactory
))
44 IUnknown_AddRef(iface
);
49 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid
));
55 static ULONG STDMETHODCALLTYPE
dxgi_factory_AddRef(IWineDXGIFactory
*iface
)
57 struct dxgi_factory
*This
= impl_from_IWineDXGIFactory(iface
);
58 ULONG refcount
= InterlockedIncrement(&This
->refcount
);
60 TRACE("%p increasing refcount to %u\n", This
, refcount
);
65 static ULONG STDMETHODCALLTYPE
dxgi_factory_Release(IWineDXGIFactory
*iface
)
67 struct dxgi_factory
*This
= impl_from_IWineDXGIFactory(iface
);
68 ULONG refcount
= InterlockedDecrement(&This
->refcount
);
70 TRACE("%p decreasing refcount to %u\n", This
, refcount
);
76 for (i
= 0; i
< This
->adapter_count
; ++i
)
78 IWineDXGIAdapter_Release(This
->adapters
[i
]);
80 HeapFree(GetProcessHeap(), 0, This
->adapters
);
82 EnterCriticalSection(&dxgi_cs
);
83 wined3d_decref(This
->wined3d
);
84 LeaveCriticalSection(&dxgi_cs
);
85 HeapFree(GetProcessHeap(), 0, This
);
91 /* IDXGIObject methods */
93 static HRESULT STDMETHODCALLTYPE
dxgi_factory_SetPrivateData(IWineDXGIFactory
*iface
,
94 REFGUID guid
, UINT data_size
, const void *data
)
96 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface
, debugstr_guid(guid
), data_size
, data
);
101 static HRESULT STDMETHODCALLTYPE
dxgi_factory_SetPrivateDataInterface(IWineDXGIFactory
*iface
,
102 REFGUID guid
, const IUnknown
*object
)
104 FIXME("iface %p, guid %s, object %p stub!\n", iface
, debugstr_guid(guid
), object
);
109 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetPrivateData(IWineDXGIFactory
*iface
,
110 REFGUID guid
, UINT
*data_size
, void *data
)
112 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface
, debugstr_guid(guid
), data_size
, data
);
117 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetParent(IWineDXGIFactory
*iface
, REFIID riid
, void **parent
)
119 WARN("iface %p, riid %s, parent %p.\n", iface
, debugstr_guid(riid
), parent
);
123 return E_NOINTERFACE
;
126 /* IDXGIFactory methods */
128 static HRESULT STDMETHODCALLTYPE
dxgi_factory_EnumAdapters1(IWineDXGIFactory
*iface
,
129 UINT adapter_idx
, IDXGIAdapter1
**adapter
)
131 struct dxgi_factory
*factory
= impl_from_IWineDXGIFactory(iface
);
133 TRACE("iface %p, adapter_idx %u, adapter %p\n", iface
, adapter_idx
, adapter
);
136 return DXGI_ERROR_INVALID_CALL
;
138 if (adapter_idx
>= factory
->adapter_count
)
141 return DXGI_ERROR_NOT_FOUND
;
144 *adapter
= (IDXGIAdapter1
*)factory
->adapters
[adapter_idx
];
145 IDXGIAdapter1_AddRef(*adapter
);
147 TRACE("Returning adapter %p\n", *adapter
);
152 static HRESULT STDMETHODCALLTYPE
dxgi_factory_EnumAdapters(IWineDXGIFactory
*iface
,
153 UINT adapter_idx
, IDXGIAdapter
**adapter
)
155 TRACE("iface %p, adapter_idx %u, adapter %p\n", iface
, adapter_idx
, adapter
);
157 return dxgi_factory_EnumAdapters1(iface
, adapter_idx
, (IDXGIAdapter1
**)adapter
);
160 static HRESULT STDMETHODCALLTYPE
dxgi_factory_MakeWindowAssociation(IWineDXGIFactory
*iface
, HWND window
, UINT flags
)
162 FIXME("iface %p, window %p, flags %#x stub!\n\n", iface
, window
, flags
);
167 static HRESULT STDMETHODCALLTYPE
dxgi_factory_GetWindowAssociation(IWineDXGIFactory
*iface
, HWND
*window
)
169 FIXME("iface %p, window %p stub!\n", iface
, window
);
174 static UINT
dxgi_rational_to_uint(const DXGI_RATIONAL
*rational
)
176 if (rational
->Denominator
)
177 return rational
->Numerator
/ rational
->Denominator
;
179 return rational
->Numerator
;
182 static HRESULT STDMETHODCALLTYPE
dxgi_factory_CreateSwapChain(IWineDXGIFactory
*iface
,
183 IUnknown
*device
, DXGI_SWAP_CHAIN_DESC
*desc
, IDXGISwapChain
**swapchain
)
185 struct wined3d_swapchain
*wined3d_swapchain
;
186 struct wined3d_swapchain_desc wined3d_desc
;
187 struct wined3d_device
*wined3d_device
;
188 IWineDXGIDevice
*dxgi_device
;
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
);
197 ERR("This is not the device we're looking for\n");
201 wined3d_device
= IWineDXGIDevice_get_wined3d_device(dxgi_device
);
202 IWineDXGIDevice_Release(dxgi_device
);
204 count
= wined3d_device_get_swapchain_count(wined3d_device
);
207 FIXME("Only a single swapchain supported.\n");
208 wined3d_device_decref(wined3d_device
);
212 if (!desc
->OutputWindow
)
214 FIXME("No output window, should use factory output window\n");
217 FIXME("Ignoring SwapEffect and Flags\n");
219 wined3d_desc
.backbuffer_width
= desc
->BufferDesc
.Width
;
220 wined3d_desc
.backbuffer_height
= desc
->BufferDesc
.Height
;
221 wined3d_desc
.backbuffer_format
= wined3dformat_from_dxgi_format(desc
->BufferDesc
.Format
);
222 wined3d_desc
.backbuffer_count
= desc
->BufferCount
;
223 if (desc
->SampleDesc
.Count
> 1)
225 wined3d_desc
.multisample_type
= desc
->SampleDesc
.Count
;
226 wined3d_desc
.multisample_quality
= desc
->SampleDesc
.Quality
;
230 wined3d_desc
.multisample_type
= WINED3D_MULTISAMPLE_NONE
;
231 wined3d_desc
.multisample_quality
= 0;
233 wined3d_desc
.swap_effect
= WINED3D_SWAP_EFFECT_DISCARD
;
234 wined3d_desc
.device_window
= desc
->OutputWindow
;
235 wined3d_desc
.windowed
= desc
->Windowed
;
236 wined3d_desc
.enable_auto_depth_stencil
= FALSE
;
237 wined3d_desc
.auto_depth_stencil_format
= 0;
238 wined3d_desc
.flags
= 0; /* WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL? */
239 wined3d_desc
.refresh_rate
= dxgi_rational_to_uint(&desc
->BufferDesc
.RefreshRate
);
240 wined3d_desc
.swap_interval
= WINED3DPRESENT_INTERVAL_DEFAULT
;
242 hr
= wined3d_device_init_3d(wined3d_device
, &wined3d_desc
);
245 WARN("Failed to initialize 3D, returning %#x\n", hr
);
246 wined3d_device_decref(wined3d_device
);
250 wined3d_swapchain
= wined3d_device_get_swapchain(wined3d_device
, 0);
251 wined3d_device_decref(wined3d_device
);
252 if (!wined3d_swapchain
)
254 WARN("Failed to get swapchain.\n");
258 *swapchain
= wined3d_swapchain_get_parent(wined3d_swapchain
);
260 /* FIXME? The swapchain is created with refcount 1 by the wined3d device,
261 * but the wined3d device can't hold a real reference. */
263 TRACE("Created IDXGISwapChain %p\n", *swapchain
);
268 static HRESULT STDMETHODCALLTYPE
dxgi_factory_CreateSoftwareAdapter(IWineDXGIFactory
*iface
,
269 HMODULE swrast
, IDXGIAdapter
**adapter
)
271 FIXME("iface %p, swrast %p, adapter %p stub!\n", iface
, swrast
, adapter
);
276 static BOOL STDMETHODCALLTYPE
dxgi_factory_IsCurrent(IWineDXGIFactory
*iface
)
278 FIXME("iface %p stub!\n", iface
);
283 /* IWineDXGIFactory methods */
285 static struct wined3d
* STDMETHODCALLTYPE
dxgi_factory_get_wined3d(IWineDXGIFactory
*iface
)
287 struct dxgi_factory
*This
= impl_from_IWineDXGIFactory(iface
);
289 TRACE("iface %p\n", iface
);
291 EnterCriticalSection(&dxgi_cs
);
292 wined3d_incref(This
->wined3d
);
293 LeaveCriticalSection(&dxgi_cs
);
294 return This
->wined3d
;
297 static const struct IWineDXGIFactoryVtbl dxgi_factory_vtbl
=
299 /* IUnknown methods */
300 dxgi_factory_QueryInterface
,
302 dxgi_factory_Release
,
303 /* IDXGIObject methods */
304 dxgi_factory_SetPrivateData
,
305 dxgi_factory_SetPrivateDataInterface
,
306 dxgi_factory_GetPrivateData
,
307 dxgi_factory_GetParent
,
308 /* IDXGIFactory methods */
309 dxgi_factory_EnumAdapters
,
310 dxgi_factory_MakeWindowAssociation
,
311 dxgi_factory_GetWindowAssociation
,
312 dxgi_factory_CreateSwapChain
,
313 dxgi_factory_CreateSoftwareAdapter
,
314 /* IDXGIFactory1 methods */
315 dxgi_factory_EnumAdapters1
,
316 dxgi_factory_IsCurrent
,
317 /* IWineDXGIFactory methods */
318 dxgi_factory_get_wined3d
,
321 HRESULT
dxgi_factory_init(struct dxgi_factory
*factory
)
326 factory
->IWineDXGIFactory_iface
.lpVtbl
= &dxgi_factory_vtbl
;
327 factory
->refcount
= 1;
329 EnterCriticalSection(&dxgi_cs
);
330 factory
->wined3d
= wined3d_create(10, 0);
331 if (!factory
->wined3d
)
333 LeaveCriticalSection(&dxgi_cs
);
334 return DXGI_ERROR_UNSUPPORTED
;
337 factory
->adapter_count
= wined3d_get_adapter_count(factory
->wined3d
);
338 LeaveCriticalSection(&dxgi_cs
);
339 factory
->adapters
= HeapAlloc(GetProcessHeap(), 0, factory
->adapter_count
* sizeof(*factory
->adapters
));
340 if (!factory
->adapters
)
342 ERR("Failed to allocate DXGI adapter array memory.\n");
347 for (i
= 0; i
< factory
->adapter_count
; ++i
)
349 struct dxgi_adapter
*adapter
= HeapAlloc(GetProcessHeap(), 0, sizeof(*adapter
));
354 ERR("Failed to allocate DXGI adapter memory.\n");
356 for (j
= 0; j
< i
; ++j
)
358 IWineDXGIAdapter_Release(factory
->adapters
[j
]);
364 hr
= dxgi_adapter_init(adapter
, &factory
->IWineDXGIFactory_iface
, i
);
369 ERR("Failed to initialize adapter, hr %#x.\n", hr
);
371 HeapFree(GetProcessHeap(), 0, adapter
);
372 for (j
= 0; j
< i
; ++j
)
374 IWineDXGIAdapter_Release(factory
->adapters
[j
]);
379 factory
->adapters
[i
] = &adapter
->IWineDXGIAdapter_iface
;
385 HeapFree(GetProcessHeap(), 0, factory
->adapters
);
386 EnterCriticalSection(&dxgi_cs
);
387 wined3d_decref(factory
->wined3d
);
388 LeaveCriticalSection(&dxgi_cs
);