d3dx8: Implement D3DXMatrixStack_Scale.
[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
101     object->adapter_count = IWineD3D_GetAdapterCount(object->wined3d);
102     LeaveCriticalSection(&dxgi_cs);
103     object->adapters = HeapAlloc(GetProcessHeap(), 0, object->adapter_count * sizeof(*object->adapters));
104     if (!object->adapters)
105     {
106         ERR("Failed to allocate DXGI adapter array memory\n");
107         hr = E_OUTOFMEMORY;
108         goto fail;
109     }
110
111     for (i = 0; i < object->adapter_count; ++i)
112     {
113         struct dxgi_adapter *adapter = HeapAlloc(GetProcessHeap(), 0, sizeof(**object->adapters));
114         if (!adapter)
115         {
116             UINT j;
117             ERR("Failed to allocate DXGI adapter memory\n");
118             for (j = 0; j < i; ++j)
119             {
120                 HeapFree(GetProcessHeap(), 0, object->adapters[j]);
121             }
122             hr = E_OUTOFMEMORY;
123             goto fail;
124         }
125
126         adapter->vtbl = &dxgi_adapter_vtbl;
127         adapter->refcount = 1;
128         adapter->ordinal = i;
129         adapter->parent = (IDXGIFactory *)object;
130         object->adapters[i] = (IDXGIAdapter *)adapter;
131     }
132
133     TRACE("Created IDXGIFactory %p\n", object);
134
135     hr = IDXGIFactory_QueryInterface((IDXGIFactory *)object, riid, factory);
136     IDXGIFactory_Release((IDXGIFactory *)object);
137
138     return hr;
139
140 fail:
141     HeapFree(GetProcessHeap(), 0, object->adapters);
142     if (object->wined3d)
143     {
144         EnterCriticalSection(&dxgi_cs);
145         IWineD3D_Release(object->wined3d);
146         LeaveCriticalSection(&dxgi_cs);
147     }
148     HeapFree(GetProcessHeap(), 0, object);
149     *factory = NULL;
150     return hr;
151
152 }
153
154 static BOOL get_layer(enum dxgi_device_layer_id id, struct dxgi_device_layer *layer)
155 {
156     UINT i;
157
158     EnterCriticalSection(&dxgi_cs);
159
160     for (i = 0; i < dxgi_main.layer_count; ++i)
161     {
162         if (dxgi_main.device_layers[i].id == id)
163         {
164             *layer = dxgi_main.device_layers[i];
165             LeaveCriticalSection(&dxgi_cs);
166             return TRUE;
167         }
168     }
169
170     LeaveCriticalSection(&dxgi_cs);
171     return FALSE;
172 }
173
174 static HRESULT register_d3d10core_layers(HMODULE d3d10core)
175 {
176     EnterCriticalSection(&dxgi_cs);
177
178     if (!dxgi_main.d3d10core)
179     {
180         HRESULT hr;
181         HRESULT (WINAPI *d3d10core_register_layers)(void);
182         HMODULE mod;
183         BOOL ret;
184
185         ret = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)d3d10core, &mod);
186         if (!ret)
187         {
188             LeaveCriticalSection(&dxgi_cs);
189             return E_FAIL;
190         }
191
192         d3d10core_register_layers = (HRESULT (WINAPI *)(void))GetProcAddress(mod, "D3D10CoreRegisterLayers");
193         hr = d3d10core_register_layers();
194         if (FAILED(hr))
195         {
196             ERR("Failed to register d3d10core layers, returning %#x\n", hr);
197             LeaveCriticalSection(&dxgi_cs);
198             return hr;
199         }
200
201         dxgi_main.d3d10core = mod;
202     }
203
204     LeaveCriticalSection(&dxgi_cs);
205
206     return S_OK;
207 }
208
209 HRESULT WINAPI DXGID3D10CreateDevice(HMODULE d3d10core, IDXGIFactory *factory, IDXGIAdapter *adapter,
210         UINT flags, DWORD unknown0, void **device)
211 {
212     struct layer_get_size_args get_size_args;
213     struct dxgi_device *dxgi_device;
214     struct dxgi_device_layer d3d10_layer;
215     IWineDXGIAdapter *wine_adapter;
216     UINT adapter_ordinal;
217     IWineD3D *wined3d;
218     void *layer_base;
219     UINT device_size;
220     DWORD count;
221     HRESULT hr;
222
223     TRACE("d3d10core %p, factory %p, adapter %p, flags %#x, unknown0 %#x, device %p\n",
224             d3d10core, factory, adapter, flags, unknown0, device);
225
226     hr = register_d3d10core_layers(d3d10core);
227     if (FAILED(hr))
228     {
229         ERR("Failed to register d3d10core layers, returning %#x\n", hr);
230         return hr;
231     }
232
233     if (!get_layer(DXGI_DEVICE_LAYER_D3D10_DEVICE, &d3d10_layer))
234     {
235         ERR("Failed to get D3D10 device layer\n");
236         return E_FAIL;
237     }
238
239     count = 0;
240     hr = d3d10_layer.init(d3d10_layer.id, &count, NULL);
241     if (FAILED(hr))
242     {
243         WARN("Failed to initialize D3D10 device layer\n");
244         return E_FAIL;
245     }
246
247     get_size_args.unknown0 = 0;
248     get_size_args.unknown1 = 0;
249     get_size_args.unknown2 = NULL;
250     get_size_args.unknown3 = NULL;
251     get_size_args.adapter = adapter;
252     get_size_args.interface_major = 10;
253     get_size_args.interface_minor = 1;
254     get_size_args.version_build = 4;
255     get_size_args.version_revision = 6000;
256
257     device_size = d3d10_layer.get_size(d3d10_layer.id, &get_size_args, 0);
258     device_size += sizeof(*dxgi_device);
259
260     dxgi_device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device_size);
261     if (!dxgi_device)
262     {
263         ERR("Failed to allocate device memory\n");
264         return E_OUTOFMEMORY;
265     }
266
267     dxgi_device->vtbl = &dxgi_device_vtbl;
268     dxgi_device->refcount = 1;
269
270     hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&dxgi_device->factory);
271     if (FAILED(hr))
272     {
273         WARN("This is not the factory we're looking for, returning %#x\n", hr);
274         goto fail;
275     }
276     wined3d = IWineDXGIFactory_get_wined3d(dxgi_device->factory);
277
278     hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
279     if (FAILED(hr))
280     {
281         WARN("This is not the adapter we're looking for, returning %#x\n", hr);
282         EnterCriticalSection(&dxgi_cs);
283         IWineD3D_Release(wined3d);
284         LeaveCriticalSection(&dxgi_cs);
285         goto fail;
286     }
287     adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
288     IWineDXGIAdapter_Release(wine_adapter);
289
290     FIXME("Ignoring adapter type\n");
291     EnterCriticalSection(&dxgi_cs);
292     hr = IWineD3D_CreateDevice(wined3d, adapter_ordinal, WINED3DDEVTYPE_HAL, NULL,
293             0, &dxgi_device->wined3d_device, (IUnknown *)dxgi_device);
294     IWineD3D_Release(wined3d);
295     LeaveCriticalSection(&dxgi_cs);
296     if (FAILED(hr))
297     {
298         WARN("Failed to create a WineD3D device, returning %#x\n", hr);
299         goto fail;
300     }
301
302     layer_base = dxgi_device + 1;
303
304     hr = d3d10_layer.create(d3d10_layer.id, &layer_base, 0,
305             dxgi_device, &IID_IUnknown, (void **)&dxgi_device->child_layer);
306     if (FAILED(hr))
307     {
308         WARN("Failed to create device, returning %#x\n", hr);
309         goto fail;
310     }
311
312     *device = (IUnknown *)dxgi_device;
313
314     return hr;
315
316 fail:
317     if (dxgi_device->wined3d_device)
318     {
319         EnterCriticalSection(&dxgi_cs);
320         IWineD3DDevice_Release(dxgi_device->wined3d_device);
321         LeaveCriticalSection(&dxgi_cs);
322     }
323     if (dxgi_device->factory) IWineDXGIFactory_Release(dxgi_device->factory);
324     HeapFree(GetProcessHeap(), 0, dxgi_device);
325     *device = NULL;
326     return hr;
327 }
328
329 HRESULT WINAPI DXGID3D10RegisterLayers(const struct dxgi_device_layer *layers, UINT layer_count)
330 {
331     UINT i;
332     struct dxgi_device_layer *new_layers;
333
334     TRACE("layers %p, layer_count %u\n", layers, layer_count);
335
336     EnterCriticalSection(&dxgi_cs);
337
338     if (!dxgi_main.layer_count)
339         new_layers = HeapAlloc(GetProcessHeap(), 0, layer_count * sizeof(*new_layers));
340     else
341         new_layers = HeapReAlloc(GetProcessHeap(), 0, dxgi_main.device_layers,
342                 (dxgi_main.layer_count + layer_count) * sizeof(*new_layers));
343
344     if (!new_layers)
345     {
346         LeaveCriticalSection(&dxgi_cs);
347         ERR("Failed to allocate layer memory\n");
348         return E_OUTOFMEMORY;
349     }
350
351     for (i = 0; i < layer_count; ++i)
352     {
353         const struct dxgi_device_layer *layer = &layers[i];
354
355         TRACE("layer %d: id %#x, init %p, get_size %p, create %p\n",
356                 i, layer->id, layer->init, layer->get_size, layer->create);
357
358         new_layers[dxgi_main.layer_count + i] = *layer;
359     }
360
361     dxgi_main.device_layers = new_layers;
362     dxgi_main.layer_count += layer_count;
363
364     LeaveCriticalSection(&dxgi_cs);
365
366     return S_OK;
367 }