Synchronize with trunk.
[reactos.git] / dll / directx / dxgi / factory.c
1 /*
2 * Copyright 2008 Henri Verbeet for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include "dxgi_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
26
27 static inline struct dxgi_factory *impl_from_IWineDXGIFactory(IWineDXGIFactory *iface)
28 {
29 return CONTAINING_RECORD(iface, struct dxgi_factory, IWineDXGIFactory_iface);
30 }
31
32 /* IUnknown methods */
33
34 static HRESULT STDMETHODCALLTYPE dxgi_factory_QueryInterface(IWineDXGIFactory *iface, REFIID riid, void **object)
35 {
36 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
37
38 if (IsEqualGUID(riid, &IID_IUnknown)
39 || IsEqualGUID(riid, &IID_IDXGIObject)
40 || IsEqualGUID(riid, &IID_IDXGIFactory)
41 || IsEqualGUID(riid, &IID_IWineDXGIFactory))
42 {
43 IUnknown_AddRef(iface);
44 *object = iface;
45 return S_OK;
46 }
47
48 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
49
50 *object = NULL;
51 return E_NOINTERFACE;
52 }
53
54 static ULONG STDMETHODCALLTYPE dxgi_factory_AddRef(IWineDXGIFactory *iface)
55 {
56 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
57 ULONG refcount = InterlockedIncrement(&This->refcount);
58
59 TRACE("%p increasing refcount to %u\n", This, refcount);
60
61 return refcount;
62 }
63
64 static ULONG STDMETHODCALLTYPE dxgi_factory_Release(IWineDXGIFactory *iface)
65 {
66 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
67 ULONG refcount = InterlockedDecrement(&This->refcount);
68
69 TRACE("%p decreasing refcount to %u\n", This, refcount);
70
71 if (!refcount)
72 {
73 UINT i;
74
75 for (i = 0; i < This->adapter_count; ++i)
76 {
77 IDXGIAdapter_Release(This->adapters[i]);
78 }
79 HeapFree(GetProcessHeap(), 0, This->adapters);
80
81 EnterCriticalSection(&dxgi_cs);
82 wined3d_decref(This->wined3d);
83 LeaveCriticalSection(&dxgi_cs);
84 HeapFree(GetProcessHeap(), 0, This);
85 }
86
87 return refcount;
88 }
89
90 /* IDXGIObject methods */
91
92 static HRESULT STDMETHODCALLTYPE dxgi_factory_SetPrivateData(IWineDXGIFactory *iface,
93 REFGUID guid, UINT data_size, const void *data)
94 {
95 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
96
97 return E_NOTIMPL;
98 }
99
100 static HRESULT STDMETHODCALLTYPE dxgi_factory_SetPrivateDataInterface(IWineDXGIFactory *iface,
101 REFGUID guid, const IUnknown *object)
102 {
103 FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
104
105 return E_NOTIMPL;
106 }
107
108 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetPrivateData(IWineDXGIFactory *iface,
109 REFGUID guid, UINT *data_size, void *data)
110 {
111 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
112
113 return E_NOTIMPL;
114 }
115
116 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetParent(IWineDXGIFactory *iface, REFIID riid, void **parent)
117 {
118 WARN("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
119
120 *parent = NULL;
121
122 return E_NOINTERFACE;
123 }
124
125 /* IDXGIFactory methods */
126
127 static HRESULT STDMETHODCALLTYPE dxgi_factory_EnumAdapters(IWineDXGIFactory *iface,
128 UINT adapter_idx, IDXGIAdapter **adapter)
129 {
130 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
131
132 TRACE("iface %p, adapter_idx %u, adapter %p\n", iface, adapter_idx, adapter);
133
134 if (!adapter) return DXGI_ERROR_INVALID_CALL;
135
136 if (adapter_idx >= This->adapter_count)
137 {
138 *adapter = NULL;
139 return DXGI_ERROR_NOT_FOUND;
140 }
141
142 *adapter = This->adapters[adapter_idx];
143 IDXGIAdapter_AddRef(*adapter);
144
145 TRACE("Returning adapter %p\n", *adapter);
146
147 return S_OK;
148 }
149
150 static HRESULT STDMETHODCALLTYPE dxgi_factory_MakeWindowAssociation(IWineDXGIFactory *iface, HWND window, UINT flags)
151 {
152 FIXME("iface %p, window %p, flags %#x stub!\n\n", iface, window, flags);
153
154 return E_NOTIMPL;
155 }
156
157 static HRESULT STDMETHODCALLTYPE dxgi_factory_GetWindowAssociation(IWineDXGIFactory *iface, HWND *window)
158 {
159 FIXME("iface %p, window %p stub!\n", iface, window);
160
161 return E_NOTIMPL;
162 }
163
164 static UINT dxgi_rational_to_uint(const DXGI_RATIONAL *rational)
165 {
166 if (rational->Denominator)
167 return rational->Numerator / rational->Denominator;
168 else
169 return rational->Numerator;
170 }
171
172 static HRESULT STDMETHODCALLTYPE dxgi_factory_CreateSwapChain(IWineDXGIFactory *iface,
173 IUnknown *device, DXGI_SWAP_CHAIN_DESC *desc, IDXGISwapChain **swapchain)
174 {
175 struct wined3d_swapchain *wined3d_swapchain;
176 struct wined3d_swapchain_desc wined3d_desc;
177 struct wined3d_device *wined3d_device;
178 IWineDXGIDevice *dxgi_device;
179 UINT count;
180 HRESULT hr;
181
182 FIXME("iface %p, device %p, desc %p, swapchain %p partial stub!\n", iface, device, desc, swapchain);
183
184 hr = IUnknown_QueryInterface(device, &IID_IWineDXGIDevice, (void **)&dxgi_device);
185 if (FAILED(hr))
186 {
187 ERR("This is not the device we're looking for\n");
188 return hr;
189 }
190
191 wined3d_device = IWineDXGIDevice_get_wined3d_device(dxgi_device);
192 IWineDXGIDevice_Release(dxgi_device);
193
194 count = wined3d_device_get_swapchain_count(wined3d_device);
195 if (count)
196 {
197 FIXME("Only a single swapchain supported.\n");
198 wined3d_device_decref(wined3d_device);
199 return E_FAIL;
200 }
201
202 if (!desc->OutputWindow)
203 {
204 FIXME("No output window, should use factory output window\n");
205 }
206
207 FIXME("Ignoring SwapEffect and Flags\n");
208
209 wined3d_desc.backbuffer_width = desc->BufferDesc.Width;
210 wined3d_desc.backbuffer_height = desc->BufferDesc.Height;
211 wined3d_desc.backbuffer_format = wined3dformat_from_dxgi_format(desc->BufferDesc.Format);
212 wined3d_desc.backbuffer_count = desc->BufferCount;
213 if (desc->SampleDesc.Count > 1)
214 {
215 wined3d_desc.multisample_type = desc->SampleDesc.Count;
216 wined3d_desc.multisample_quality = desc->SampleDesc.Quality;
217 }
218 else
219 {
220 wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
221 wined3d_desc.multisample_quality = 0;
222 }
223 wined3d_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
224 wined3d_desc.device_window = desc->OutputWindow;
225 wined3d_desc.windowed = desc->Windowed;
226 wined3d_desc.enable_auto_depth_stencil = FALSE;
227 wined3d_desc.auto_depth_stencil_format = 0;
228 wined3d_desc.flags = 0; /* WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL? */
229 wined3d_desc.refresh_rate = dxgi_rational_to_uint(&desc->BufferDesc.RefreshRate);
230 wined3d_desc.swap_interval = WINED3DPRESENT_INTERVAL_DEFAULT;
231
232 hr = wined3d_device_init_3d(wined3d_device, &wined3d_desc);
233 if (FAILED(hr))
234 {
235 WARN("Failed to initialize 3D, returning %#x\n", hr);
236 wined3d_device_decref(wined3d_device);
237 return hr;
238 }
239
240 wined3d_swapchain = wined3d_device_get_swapchain(wined3d_device, 0);
241 wined3d_device_decref(wined3d_device);
242 if (!wined3d_swapchain)
243 {
244 WARN("Failed to get swapchain.\n");
245 return E_FAIL;
246 }
247
248 *swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
249
250 /* FIXME? The swapchain is created with refcount 1 by the wined3d device,
251 * but the wined3d device can't hold a real reference. */
252
253 TRACE("Created IDXGISwapChain %p\n", *swapchain);
254
255 return S_OK;
256 }
257
258 static HRESULT STDMETHODCALLTYPE dxgi_factory_CreateSoftwareAdapter(IWineDXGIFactory *iface,
259 HMODULE swrast, IDXGIAdapter **adapter)
260 {
261 FIXME("iface %p, swrast %p, adapter %p stub!\n", iface, swrast, adapter);
262
263 return E_NOTIMPL;
264 }
265
266 /* IWineDXGIFactory methods */
267
268 static struct wined3d * STDMETHODCALLTYPE dxgi_factory_get_wined3d(IWineDXGIFactory *iface)
269 {
270 struct dxgi_factory *This = impl_from_IWineDXGIFactory(iface);
271
272 TRACE("iface %p\n", iface);
273
274 EnterCriticalSection(&dxgi_cs);
275 wined3d_incref(This->wined3d);
276 LeaveCriticalSection(&dxgi_cs);
277 return This->wined3d;
278 }
279
280 static const struct IWineDXGIFactoryVtbl dxgi_factory_vtbl =
281 {
282 /* IUnknown methods */
283 dxgi_factory_QueryInterface,
284 dxgi_factory_AddRef,
285 dxgi_factory_Release,
286 /* IDXGIObject methods */
287 dxgi_factory_SetPrivateData,
288 dxgi_factory_SetPrivateDataInterface,
289 dxgi_factory_GetPrivateData,
290 dxgi_factory_GetParent,
291 /* IDXGIFactory methods */
292 dxgi_factory_EnumAdapters,
293 dxgi_factory_MakeWindowAssociation,
294 dxgi_factory_GetWindowAssociation,
295 dxgi_factory_CreateSwapChain,
296 dxgi_factory_CreateSoftwareAdapter,
297 /* IWineDXGIFactory methods */
298 dxgi_factory_get_wined3d,
299 };
300
301 HRESULT dxgi_factory_init(struct dxgi_factory *factory)
302 {
303 HRESULT hr;
304 UINT i;
305
306 factory->IWineDXGIFactory_iface.lpVtbl = &dxgi_factory_vtbl;
307 factory->refcount = 1;
308
309 EnterCriticalSection(&dxgi_cs);
310 factory->wined3d = wined3d_create(10, 0);
311 if (!factory->wined3d)
312 {
313 LeaveCriticalSection(&dxgi_cs);
314 return DXGI_ERROR_UNSUPPORTED;
315 }
316
317 factory->adapter_count = wined3d_get_adapter_count(factory->wined3d);
318 LeaveCriticalSection(&dxgi_cs);
319 factory->adapters = HeapAlloc(GetProcessHeap(), 0, factory->adapter_count * sizeof(*factory->adapters));
320 if (!factory->adapters)
321 {
322 ERR("Failed to allocate DXGI adapter array memory.\n");
323 hr = E_OUTOFMEMORY;
324 goto fail;
325 }
326
327 for (i = 0; i < factory->adapter_count; ++i)
328 {
329 struct dxgi_adapter *adapter = HeapAlloc(GetProcessHeap(), 0, sizeof(*adapter));
330 if (!adapter)
331 {
332 UINT j;
333
334 ERR("Failed to allocate DXGI adapter memory.\n");
335
336 for (j = 0; j < i; ++j)
337 {
338 IDXGIAdapter_Release(factory->adapters[j]);
339 }
340 hr = E_OUTOFMEMORY;
341 goto fail;
342 }
343
344 hr = dxgi_adapter_init(adapter, &factory->IWineDXGIFactory_iface, i);
345 if (FAILED(hr))
346 {
347 UINT j;
348
349 ERR("Failed to initialize adapter, hr %#x.\n", hr);
350
351 HeapFree(GetProcessHeap(), 0, adapter);
352 for (j = 0; j < i; ++j)
353 {
354 IDXGIAdapter_Release(factory->adapters[j]);
355 }
356 goto fail;
357 }
358
359 factory->adapters[i] = (IDXGIAdapter *)adapter;
360 }
361
362 return S_OK;
363
364 fail:
365 HeapFree(GetProcessHeap(), 0, factory->adapters);
366 EnterCriticalSection(&dxgi_cs);
367 wined3d_decref(factory->wined3d);
368 LeaveCriticalSection(&dxgi_cs);
369 return hr;
370 }