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