comctl32/datetime: Use SDK defined class name instead of another local constant.
[wine] / dlls / dxgi / dxgi_main.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 #define DXGI_INIT_GUID
24 #include "dxgi_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
27
28 static CRITICAL_SECTION_DEBUG dxgi_cs_debug =
29 {
30     0, 0, &dxgi_cs,
31     {&dxgi_cs_debug.ProcessLocksList,
32     &dxgi_cs_debug.ProcessLocksList},
33     0, 0, {(DWORD_PTR)(__FILE__ ": dxgi_cs")}
34 };
35 CRITICAL_SECTION dxgi_cs = {&dxgi_cs_debug, -1, 0, 0, 0, 0};
36
37 struct dxgi_main
38 {
39     HMODULE d3d10core;
40     struct dxgi_device_layer *device_layers;
41     UINT layer_count;
42     LONG refcount;
43 };
44 static struct dxgi_main dxgi_main;
45
46 static void dxgi_main_cleanup(void)
47 {
48     EnterCriticalSection(&dxgi_cs);
49
50     HeapFree(GetProcessHeap(), 0, dxgi_main.device_layers);
51     dxgi_main.device_layers = NULL;
52     dxgi_main.layer_count = 0;
53
54     FreeLibrary(dxgi_main.d3d10core);
55     dxgi_main.d3d10core = NULL;
56
57     LeaveCriticalSection(&dxgi_cs);
58 }
59
60 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
61 {
62     TRACE("fdwReason %u\n", fdwReason);
63
64     switch(fdwReason)
65     {
66         case DLL_PROCESS_ATTACH:
67             DisableThreadLibraryCalls(hInstDLL);
68             ++dxgi_main.refcount;
69             break;
70
71         case DLL_PROCESS_DETACH:
72             if (!--dxgi_main.refcount) dxgi_main_cleanup();
73             break;
74     }
75
76     return TRUE;
77 }
78
79 HRESULT WINAPI CreateDXGIFactory(REFIID riid, void **factory)
80 {
81     struct dxgi_factory *object;
82     HRESULT hr;
83     UINT i;
84
85     TRACE("riid %s, factory %p\n", debugstr_guid(riid), factory);
86
87     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
88     if (!object)
89     {
90         ERR("Failed to allocate DXGI factory object memory\n");
91         *factory = NULL;
92         return E_OUTOFMEMORY;
93     }
94
95     object->vtbl = &dxgi_factory_vtbl;
96     object->refcount = 1;
97
98     EnterCriticalSection(&dxgi_cs);
99     object->wined3d = WineDirect3DCreate(10, (IUnknown *)object);
100     if(!object->wined3d)
101     {
102         hr = DXGI_ERROR_UNSUPPORTED;
103         LeaveCriticalSection(&dxgi_cs);
104         goto fail;
105     }
106
107     object->adapter_count = IWineD3D_GetAdapterCount(object->wined3d);
108     LeaveCriticalSection(&dxgi_cs);
109     object->adapters = HeapAlloc(GetProcessHeap(), 0, object->adapter_count * sizeof(*object->adapters));
110     if (!object->adapters)
111     {
112         ERR("Failed to allocate DXGI adapter array memory\n");
113         hr = E_OUTOFMEMORY;
114         goto fail;
115     }
116
117     for (i = 0; i < object->adapter_count; ++i)
118     {
119         struct dxgi_adapter *adapter = HeapAlloc(GetProcessHeap(), 0, sizeof(*adapter));
120         if (!adapter)
121         {
122             UINT j;
123             ERR("Failed to allocate DXGI adapter memory\n");
124             for (j = 0; j < i; ++j)
125             {
126                 HeapFree(GetProcessHeap(), 0, object->adapters[j]);
127             }
128             hr = E_OUTOFMEMORY;
129             goto fail;
130         }
131
132         hr = dxgi_adapter_init(adapter, (IDXGIFactory *)object, i);
133         if (FAILED(hr))
134         {
135             UINT j;
136
137             ERR("Failed to initialize adapter, hr %#x.\n", hr);
138
139             HeapFree(GetProcessHeap(), 0, adapter);
140             for (j = 0; j < i; ++j)
141             {
142                 IDXGIAdapter_Release(object->adapters[j]);
143             }
144             goto fail;
145         }
146
147         object->adapters[i] = (IDXGIAdapter *)adapter;
148     }
149
150     TRACE("Created IDXGIFactory %p\n", object);
151
152     hr = IDXGIFactory_QueryInterface((IDXGIFactory *)object, riid, factory);
153     IDXGIFactory_Release((IDXGIFactory *)object);
154
155     return hr;
156
157 fail:
158     HeapFree(GetProcessHeap(), 0, object->adapters);
159     if (object->wined3d)
160     {
161         EnterCriticalSection(&dxgi_cs);
162         IWineD3D_Release(object->wined3d);
163         LeaveCriticalSection(&dxgi_cs);
164     }
165     HeapFree(GetProcessHeap(), 0, object);
166     *factory = NULL;
167     return hr;
168
169 }
170
171 static BOOL get_layer(enum dxgi_device_layer_id id, struct dxgi_device_layer *layer)
172 {
173     UINT i;
174
175     EnterCriticalSection(&dxgi_cs);
176
177     for (i = 0; i < dxgi_main.layer_count; ++i)
178     {
179         if (dxgi_main.device_layers[i].id == id)
180         {
181             *layer = dxgi_main.device_layers[i];
182             LeaveCriticalSection(&dxgi_cs);
183             return TRUE;
184         }
185     }
186
187     LeaveCriticalSection(&dxgi_cs);
188     return FALSE;
189 }
190
191 static HRESULT register_d3d10core_layers(HMODULE d3d10core)
192 {
193     EnterCriticalSection(&dxgi_cs);
194
195     if (!dxgi_main.d3d10core)
196     {
197         HRESULT hr;
198         HRESULT (WINAPI *d3d10core_register_layers)(void);
199         HMODULE mod;
200         BOOL ret;
201
202         ret = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)d3d10core, &mod);
203         if (!ret)
204         {
205             LeaveCriticalSection(&dxgi_cs);
206             return E_FAIL;
207         }
208
209         d3d10core_register_layers = (HRESULT (WINAPI *)(void))GetProcAddress(mod, "D3D10CoreRegisterLayers");
210         hr = d3d10core_register_layers();
211         if (FAILED(hr))
212         {
213             ERR("Failed to register d3d10core layers, returning %#x\n", hr);
214             LeaveCriticalSection(&dxgi_cs);
215             return hr;
216         }
217
218         dxgi_main.d3d10core = mod;
219     }
220
221     LeaveCriticalSection(&dxgi_cs);
222
223     return S_OK;
224 }
225
226 HRESULT WINAPI DXGID3D10CreateDevice(HMODULE d3d10core, IDXGIFactory *factory, IDXGIAdapter *adapter,
227         UINT flags, DWORD unknown0, void **device)
228 {
229     IWineD3DDeviceParent *wined3d_device_parent;
230     struct layer_get_size_args get_size_args;
231     struct dxgi_device *dxgi_device;
232     struct dxgi_device_layer d3d10_layer;
233     IWineDXGIAdapter *wine_adapter;
234     UINT adapter_ordinal;
235     IWineD3D *wined3d;
236     void *layer_base;
237     UINT device_size;
238     DWORD count;
239     HRESULT hr;
240
241     TRACE("d3d10core %p, factory %p, adapter %p, flags %#x, unknown0 %#x, device %p\n",
242             d3d10core, factory, adapter, flags, unknown0, device);
243
244     hr = register_d3d10core_layers(d3d10core);
245     if (FAILED(hr))
246     {
247         ERR("Failed to register d3d10core layers, returning %#x\n", hr);
248         return hr;
249     }
250
251     if (!get_layer(DXGI_DEVICE_LAYER_D3D10_DEVICE, &d3d10_layer))
252     {
253         ERR("Failed to get D3D10 device layer\n");
254         return E_FAIL;
255     }
256
257     count = 0;
258     hr = d3d10_layer.init(d3d10_layer.id, &count, NULL);
259     if (FAILED(hr))
260     {
261         WARN("Failed to initialize D3D10 device layer\n");
262         return E_FAIL;
263     }
264
265     get_size_args.unknown0 = 0;
266     get_size_args.unknown1 = 0;
267     get_size_args.unknown2 = NULL;
268     get_size_args.unknown3 = NULL;
269     get_size_args.adapter = adapter;
270     get_size_args.interface_major = 10;
271     get_size_args.interface_minor = 1;
272     get_size_args.version_build = 4;
273     get_size_args.version_revision = 6000;
274
275     device_size = d3d10_layer.get_size(d3d10_layer.id, &get_size_args, 0);
276     device_size += sizeof(*dxgi_device);
277
278     dxgi_device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device_size);
279     if (!dxgi_device)
280     {
281         ERR("Failed to allocate device memory\n");
282         return E_OUTOFMEMORY;
283     }
284
285     dxgi_device->vtbl = &dxgi_device_vtbl;
286     dxgi_device->refcount = 1;
287
288     layer_base = dxgi_device + 1;
289
290     hr = d3d10_layer.create(d3d10_layer.id, &layer_base, 0,
291             dxgi_device, &IID_IUnknown, (void **)&dxgi_device->child_layer);
292     if (FAILED(hr))
293     {
294         WARN("Failed to create device, returning %#x\n", hr);
295         goto fail;
296     }
297
298     hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&dxgi_device->factory);
299     if (FAILED(hr))
300     {
301         WARN("This is not the factory we're looking for, returning %#x\n", hr);
302         goto fail;
303     }
304     wined3d = IWineDXGIFactory_get_wined3d(dxgi_device->factory);
305
306     hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
307     if (FAILED(hr))
308     {
309         WARN("This is not the adapter we're looking for, returning %#x\n", hr);
310         EnterCriticalSection(&dxgi_cs);
311         IWineD3D_Release(wined3d);
312         LeaveCriticalSection(&dxgi_cs);
313         goto fail;
314     }
315     adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
316     IWineDXGIAdapter_Release(wine_adapter);
317
318     hr = IUnknown_QueryInterface((IUnknown *)dxgi_device, &IID_IWineD3DDeviceParent, (void **)&wined3d_device_parent);
319     if (FAILED(hr))
320     {
321         ERR("DXGI device should implement IWineD3DDeviceParent\n");
322         goto fail;
323     }
324
325     FIXME("Ignoring adapter type\n");
326     EnterCriticalSection(&dxgi_cs);
327     hr = IWineD3D_CreateDevice(wined3d, adapter_ordinal, WINED3DDEVTYPE_HAL, NULL, 0,
328             (IUnknown *)dxgi_device, wined3d_device_parent, &dxgi_device->wined3d_device);
329     IWineD3DDeviceParent_Release(wined3d_device_parent);
330     IWineD3D_Release(wined3d);
331     LeaveCriticalSection(&dxgi_cs);
332     if (FAILED(hr))
333     {
334         WARN("Failed to create a WineD3D device, returning %#x\n", hr);
335         goto fail;
336     }
337
338     *device = dxgi_device;
339
340     return hr;
341
342 fail:
343     if (dxgi_device->wined3d_device)
344     {
345         EnterCriticalSection(&dxgi_cs);
346         IWineD3DDevice_Release(dxgi_device->wined3d_device);
347         LeaveCriticalSection(&dxgi_cs);
348     }
349     if (dxgi_device->factory) IWineDXGIFactory_Release(dxgi_device->factory);
350     if (dxgi_device->child_layer) IUnknown_Release(dxgi_device->child_layer);
351     HeapFree(GetProcessHeap(), 0, dxgi_device);
352     *device = NULL;
353     return hr;
354 }
355
356 HRESULT WINAPI DXGID3D10RegisterLayers(const struct dxgi_device_layer *layers, UINT layer_count)
357 {
358     UINT i;
359     struct dxgi_device_layer *new_layers;
360
361     TRACE("layers %p, layer_count %u\n", layers, layer_count);
362
363     EnterCriticalSection(&dxgi_cs);
364
365     if (!dxgi_main.layer_count)
366         new_layers = HeapAlloc(GetProcessHeap(), 0, layer_count * sizeof(*new_layers));
367     else
368         new_layers = HeapReAlloc(GetProcessHeap(), 0, dxgi_main.device_layers,
369                 (dxgi_main.layer_count + layer_count) * sizeof(*new_layers));
370
371     if (!new_layers)
372     {
373         LeaveCriticalSection(&dxgi_cs);
374         ERR("Failed to allocate layer memory\n");
375         return E_OUTOFMEMORY;
376     }
377
378     for (i = 0; i < layer_count; ++i)
379     {
380         const struct dxgi_device_layer *layer = &layers[i];
381
382         TRACE("layer %d: id %#x, init %p, get_size %p, create %p\n",
383                 i, layer->id, layer->init, layer->get_size, layer->create);
384
385         new_layers[dxgi_main.layer_count + i] = *layer;
386     }
387
388     dxgi_main.device_layers = new_layers;
389     dxgi_main.layer_count += layer_count;
390
391     LeaveCriticalSection(&dxgi_cs);
392
393     return S_OK;
394 }