imm32: Retrieve the graphics driver module from gdi32.
[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 };
43 static struct dxgi_main dxgi_main;
44
45 static void dxgi_main_cleanup(void)
46 {
47     EnterCriticalSection(&dxgi_cs);
48
49     HeapFree(GetProcessHeap(), 0, dxgi_main.device_layers);
50     dxgi_main.device_layers = NULL;
51     dxgi_main.layer_count = 0;
52
53     FreeLibrary(dxgi_main.d3d10core);
54     dxgi_main.d3d10core = NULL;
55
56     LeaveCriticalSection(&dxgi_cs);
57     DeleteCriticalSection(&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             break;
69
70         case DLL_PROCESS_DETACH:
71             dxgi_main_cleanup();
72             break;
73     }
74
75     return TRUE;
76 }
77
78 HRESULT WINAPI CreateDXGIFactory(REFIID riid, void **factory)
79 {
80     struct dxgi_factory *object;
81     HRESULT hr;
82
83     TRACE("riid %s, factory %p\n", debugstr_guid(riid), factory);
84
85     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
86     if (!object)
87     {
88         ERR("Failed to allocate DXGI factory object memory\n");
89         *factory = NULL;
90         return E_OUTOFMEMORY;
91     }
92
93     hr = dxgi_factory_init(object);
94     if (FAILED(hr))
95     {
96         WARN("Failed to initialize swapchain, hr %#x.\n", hr);
97         HeapFree(GetProcessHeap(), 0, object);
98         *factory = NULL;
99         return hr;
100     }
101
102     TRACE("Created IDXGIFactory %p\n", object);
103
104     hr = IDXGIFactory_QueryInterface((IDXGIFactory *)object, riid, factory);
105     IDXGIFactory_Release((IDXGIFactory *)object);
106
107     return hr;
108 }
109
110 static BOOL get_layer(enum dxgi_device_layer_id id, struct dxgi_device_layer *layer)
111 {
112     UINT i;
113
114     EnterCriticalSection(&dxgi_cs);
115
116     for (i = 0; i < dxgi_main.layer_count; ++i)
117     {
118         if (dxgi_main.device_layers[i].id == id)
119         {
120             *layer = dxgi_main.device_layers[i];
121             LeaveCriticalSection(&dxgi_cs);
122             return TRUE;
123         }
124     }
125
126     LeaveCriticalSection(&dxgi_cs);
127     return FALSE;
128 }
129
130 static HRESULT register_d3d10core_layers(HMODULE d3d10core)
131 {
132     EnterCriticalSection(&dxgi_cs);
133
134     if (!dxgi_main.d3d10core)
135     {
136         HRESULT hr;
137         HRESULT (WINAPI *d3d10core_register_layers)(void);
138         HMODULE mod;
139         BOOL ret;
140
141         ret = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)d3d10core, &mod);
142         if (!ret)
143         {
144             LeaveCriticalSection(&dxgi_cs);
145             return E_FAIL;
146         }
147
148         d3d10core_register_layers = (void *)GetProcAddress(mod, "D3D10CoreRegisterLayers");
149         hr = d3d10core_register_layers();
150         if (FAILED(hr))
151         {
152             ERR("Failed to register d3d10core layers, returning %#x\n", hr);
153             LeaveCriticalSection(&dxgi_cs);
154             return hr;
155         }
156
157         dxgi_main.d3d10core = mod;
158     }
159
160     LeaveCriticalSection(&dxgi_cs);
161
162     return S_OK;
163 }
164
165 HRESULT WINAPI DXGID3D10CreateDevice(HMODULE d3d10core, IDXGIFactory *factory, IDXGIAdapter *adapter,
166         UINT flags, void *unknown0, void **device)
167 {
168     struct layer_get_size_args get_size_args;
169     struct dxgi_device *dxgi_device;
170     struct dxgi_device_layer d3d10_layer;
171     UINT device_size;
172     DWORD count;
173     HRESULT hr;
174
175     TRACE("d3d10core %p, factory %p, adapter %p, flags %#x, unknown0 %p, device %p.\n",
176             d3d10core, factory, adapter, flags, unknown0, device);
177
178     hr = register_d3d10core_layers(d3d10core);
179     if (FAILED(hr))
180     {
181         ERR("Failed to register d3d10core layers, returning %#x\n", hr);
182         return hr;
183     }
184
185     if (!get_layer(DXGI_DEVICE_LAYER_D3D10_DEVICE, &d3d10_layer))
186     {
187         ERR("Failed to get D3D10 device layer\n");
188         return E_FAIL;
189     }
190
191     count = 0;
192     hr = d3d10_layer.init(d3d10_layer.id, &count, NULL);
193     if (FAILED(hr))
194     {
195         WARN("Failed to initialize D3D10 device layer\n");
196         return E_FAIL;
197     }
198
199     get_size_args.unknown0 = 0;
200     get_size_args.unknown1 = 0;
201     get_size_args.unknown2 = NULL;
202     get_size_args.unknown3 = NULL;
203     get_size_args.adapter = adapter;
204     get_size_args.interface_major = 10;
205     get_size_args.interface_minor = 1;
206     get_size_args.version_build = 4;
207     get_size_args.version_revision = 6000;
208
209     device_size = d3d10_layer.get_size(d3d10_layer.id, &get_size_args, 0);
210     device_size += sizeof(*dxgi_device);
211
212     dxgi_device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device_size);
213     if (!dxgi_device)
214     {
215         ERR("Failed to allocate device memory\n");
216         return E_OUTOFMEMORY;
217     }
218
219     hr = dxgi_device_init(dxgi_device, &d3d10_layer, factory, adapter);
220     if (FAILED(hr))
221     {
222         WARN("Failed to initialize device, hr %#x.\n", hr);
223         HeapFree(GetProcessHeap(), 0, dxgi_device);
224         *device = NULL;
225         return hr;
226     }
227
228     TRACE("Created device %p.\n", dxgi_device);
229     *device = dxgi_device;
230
231     return S_OK;
232 }
233
234 HRESULT WINAPI DXGID3D10RegisterLayers(const struct dxgi_device_layer *layers, UINT layer_count)
235 {
236     UINT i;
237     struct dxgi_device_layer *new_layers;
238
239     TRACE("layers %p, layer_count %u\n", layers, layer_count);
240
241     EnterCriticalSection(&dxgi_cs);
242
243     if (!dxgi_main.layer_count)
244         new_layers = HeapAlloc(GetProcessHeap(), 0, layer_count * sizeof(*new_layers));
245     else
246         new_layers = HeapReAlloc(GetProcessHeap(), 0, dxgi_main.device_layers,
247                 (dxgi_main.layer_count + layer_count) * sizeof(*new_layers));
248
249     if (!new_layers)
250     {
251         LeaveCriticalSection(&dxgi_cs);
252         ERR("Failed to allocate layer memory\n");
253         return E_OUTOFMEMORY;
254     }
255
256     for (i = 0; i < layer_count; ++i)
257     {
258         const struct dxgi_device_layer *layer = &layers[i];
259
260         TRACE("layer %d: id %#x, init %p, get_size %p, create %p\n",
261                 i, layer->id, layer->init, layer->get_size, layer->create);
262
263         new_layers[dxgi_main.layer_count + i] = *layer;
264     }
265
266     dxgi_main.device_layers = new_layers;
267     dxgi_main.layer_count += layer_count;
268
269     LeaveCriticalSection(&dxgi_cs);
270
271     return S_OK;
272 }