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