kernel32: Immediately return on failing to start wineboot
[wine/wine64.git] / dlls / dxgi / dxgi_main.c
blobb72c04a16fa0697b6c83994332fd866b5b3db9c8
1 /*
2 * Copyright 2008 Henri Verbeet for CodeWeavers
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.
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.
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
20 #include "config.h"
21 #include "wine/port.h"
23 #define DXGI_INIT_GUID
24 #include "dxgi_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
28 static CRITICAL_SECTION_DEBUG dxgi_cs_debug =
30 0, 0, &dxgi_cs,
31 {&dxgi_cs_debug.ProcessLocksList,
32 &dxgi_cs_debug.ProcessLocksList},
33 0, 0, {(DWORD_PTR)(__FILE__ ": dxgi_cs")}
35 CRITICAL_SECTION dxgi_cs = {&dxgi_cs_debug, -1, 0, 0, 0, 0};
37 struct dxgi_main
39 HMODULE d3d10core;
40 struct dxgi_device_layer *device_layers;
41 UINT layer_count;
42 LONG refcount;
44 static struct dxgi_main dxgi_main;
46 static void dxgi_main_cleanup(void)
48 EnterCriticalSection(&dxgi_cs);
50 HeapFree(GetProcessHeap(), 0, dxgi_main.device_layers);
51 dxgi_main.device_layers = NULL;
52 dxgi_main.layer_count = 0;
54 FreeLibrary(dxgi_main.d3d10core);
55 dxgi_main.d3d10core = NULL;
57 LeaveCriticalSection(&dxgi_cs);
60 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
62 TRACE("fdwReason %u\n", fdwReason);
64 switch(fdwReason)
66 case DLL_PROCESS_ATTACH:
67 DisableThreadLibraryCalls(hInstDLL);
68 ++dxgi_main.refcount;
69 break;
71 case DLL_PROCESS_DETACH:
72 if (!--dxgi_main.refcount) dxgi_main_cleanup();
73 break;
76 return TRUE;
79 HRESULT WINAPI CreateDXGIFactory(REFIID riid, void **factory)
81 struct dxgi_factory *object;
82 HRESULT hr;
83 UINT i;
85 TRACE("riid %s, factory %p\n", debugstr_guid(riid), factory);
87 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
88 if (!object)
90 ERR("Failed to allocate DXGI factory object memory\n");
91 *factory = NULL;
92 return E_OUTOFMEMORY;
95 object->vtbl = &dxgi_factory_vtbl;
96 object->refcount = 1;
98 EnterCriticalSection(&dxgi_cs);
99 object->wined3d = WineDirect3DCreate(10, (IUnknown *)object);
100 if(!object->wined3d)
102 hr = DXGI_ERROR_UNSUPPORTED;
103 LeaveCriticalSection(&dxgi_cs);
104 goto fail;
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)
112 ERR("Failed to allocate DXGI adapter array memory\n");
113 hr = E_OUTOFMEMORY;
114 goto fail;
117 for (i = 0; i < object->adapter_count; ++i)
119 struct dxgi_adapter *adapter = HeapAlloc(GetProcessHeap(), 0, sizeof(*adapter));
120 if (!adapter)
122 UINT j;
123 ERR("Failed to allocate DXGI adapter memory\n");
124 for (j = 0; j < i; ++j)
126 HeapFree(GetProcessHeap(), 0, object->adapters[j]);
128 hr = E_OUTOFMEMORY;
129 goto fail;
132 adapter->vtbl = &dxgi_adapter_vtbl;
133 adapter->refcount = 1;
134 adapter->ordinal = i;
135 adapter->parent = (IDXGIFactory *)object;
136 object->adapters[i] = (IDXGIAdapter *)adapter;
139 TRACE("Created IDXGIFactory %p\n", object);
141 hr = IDXGIFactory_QueryInterface((IDXGIFactory *)object, riid, factory);
142 IDXGIFactory_Release((IDXGIFactory *)object);
144 return hr;
146 fail:
147 HeapFree(GetProcessHeap(), 0, object->adapters);
148 if (object->wined3d)
150 EnterCriticalSection(&dxgi_cs);
151 IWineD3D_Release(object->wined3d);
152 LeaveCriticalSection(&dxgi_cs);
154 HeapFree(GetProcessHeap(), 0, object);
155 *factory = NULL;
156 return hr;
160 static BOOL get_layer(enum dxgi_device_layer_id id, struct dxgi_device_layer *layer)
162 UINT i;
164 EnterCriticalSection(&dxgi_cs);
166 for (i = 0; i < dxgi_main.layer_count; ++i)
168 if (dxgi_main.device_layers[i].id == id)
170 *layer = dxgi_main.device_layers[i];
171 LeaveCriticalSection(&dxgi_cs);
172 return TRUE;
176 LeaveCriticalSection(&dxgi_cs);
177 return FALSE;
180 static HRESULT register_d3d10core_layers(HMODULE d3d10core)
182 EnterCriticalSection(&dxgi_cs);
184 if (!dxgi_main.d3d10core)
186 HRESULT hr;
187 HRESULT (WINAPI *d3d10core_register_layers)(void);
188 HMODULE mod;
189 BOOL ret;
191 ret = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)d3d10core, &mod);
192 if (!ret)
194 LeaveCriticalSection(&dxgi_cs);
195 return E_FAIL;
198 d3d10core_register_layers = (HRESULT (WINAPI *)(void))GetProcAddress(mod, "D3D10CoreRegisterLayers");
199 hr = d3d10core_register_layers();
200 if (FAILED(hr))
202 ERR("Failed to register d3d10core layers, returning %#x\n", hr);
203 LeaveCriticalSection(&dxgi_cs);
204 return hr;
207 dxgi_main.d3d10core = mod;
210 LeaveCriticalSection(&dxgi_cs);
212 return S_OK;
215 HRESULT WINAPI DXGID3D10CreateDevice(HMODULE d3d10core, IDXGIFactory *factory, IDXGIAdapter *adapter,
216 UINT flags, DWORD unknown0, void **device)
218 struct layer_get_size_args get_size_args;
219 struct dxgi_device *dxgi_device;
220 struct dxgi_device_layer d3d10_layer;
221 IWineDXGIAdapter *wine_adapter;
222 UINT adapter_ordinal;
223 IWineD3D *wined3d;
224 void *layer_base;
225 UINT device_size;
226 DWORD count;
227 HRESULT hr;
229 TRACE("d3d10core %p, factory %p, adapter %p, flags %#x, unknown0 %#x, device %p\n",
230 d3d10core, factory, adapter, flags, unknown0, device);
232 hr = register_d3d10core_layers(d3d10core);
233 if (FAILED(hr))
235 ERR("Failed to register d3d10core layers, returning %#x\n", hr);
236 return hr;
239 if (!get_layer(DXGI_DEVICE_LAYER_D3D10_DEVICE, &d3d10_layer))
241 ERR("Failed to get D3D10 device layer\n");
242 return E_FAIL;
245 count = 0;
246 hr = d3d10_layer.init(d3d10_layer.id, &count, NULL);
247 if (FAILED(hr))
249 WARN("Failed to initialize D3D10 device layer\n");
250 return E_FAIL;
253 get_size_args.unknown0 = 0;
254 get_size_args.unknown1 = 0;
255 get_size_args.unknown2 = NULL;
256 get_size_args.unknown3 = NULL;
257 get_size_args.adapter = adapter;
258 get_size_args.interface_major = 10;
259 get_size_args.interface_minor = 1;
260 get_size_args.version_build = 4;
261 get_size_args.version_revision = 6000;
263 device_size = d3d10_layer.get_size(d3d10_layer.id, &get_size_args, 0);
264 device_size += sizeof(*dxgi_device);
266 dxgi_device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device_size);
267 if (!dxgi_device)
269 ERR("Failed to allocate device memory\n");
270 return E_OUTOFMEMORY;
273 dxgi_device->vtbl = &dxgi_device_vtbl;
274 dxgi_device->refcount = 1;
276 hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&dxgi_device->factory);
277 if (FAILED(hr))
279 WARN("This is not the factory we're looking for, returning %#x\n", hr);
280 goto fail;
282 wined3d = IWineDXGIFactory_get_wined3d(dxgi_device->factory);
284 hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
285 if (FAILED(hr))
287 WARN("This is not the adapter we're looking for, returning %#x\n", hr);
288 EnterCriticalSection(&dxgi_cs);
289 IWineD3D_Release(wined3d);
290 LeaveCriticalSection(&dxgi_cs);
291 goto fail;
293 adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
294 IWineDXGIAdapter_Release(wine_adapter);
296 FIXME("Ignoring adapter type\n");
297 EnterCriticalSection(&dxgi_cs);
298 hr = IWineD3D_CreateDevice(wined3d, adapter_ordinal, WINED3DDEVTYPE_HAL, NULL,
299 0, &dxgi_device->wined3d_device, (IUnknown *)dxgi_device);
300 IWineD3D_Release(wined3d);
301 LeaveCriticalSection(&dxgi_cs);
302 if (FAILED(hr))
304 WARN("Failed to create a WineD3D device, returning %#x\n", hr);
305 goto fail;
308 layer_base = dxgi_device + 1;
310 hr = d3d10_layer.create(d3d10_layer.id, &layer_base, 0,
311 dxgi_device, &IID_IUnknown, (void **)&dxgi_device->child_layer);
312 if (FAILED(hr))
314 WARN("Failed to create device, returning %#x\n", hr);
315 goto fail;
318 *device = (IUnknown *)dxgi_device;
320 return hr;
322 fail:
323 if (dxgi_device->wined3d_device)
325 EnterCriticalSection(&dxgi_cs);
326 IWineD3DDevice_Release(dxgi_device->wined3d_device);
327 LeaveCriticalSection(&dxgi_cs);
329 if (dxgi_device->factory) IWineDXGIFactory_Release(dxgi_device->factory);
330 HeapFree(GetProcessHeap(), 0, dxgi_device);
331 *device = NULL;
332 return hr;
335 HRESULT WINAPI DXGID3D10RegisterLayers(const struct dxgi_device_layer *layers, UINT layer_count)
337 UINT i;
338 struct dxgi_device_layer *new_layers;
340 TRACE("layers %p, layer_count %u\n", layers, layer_count);
342 EnterCriticalSection(&dxgi_cs);
344 if (!dxgi_main.layer_count)
345 new_layers = HeapAlloc(GetProcessHeap(), 0, layer_count * sizeof(*new_layers));
346 else
347 new_layers = HeapReAlloc(GetProcessHeap(), 0, dxgi_main.device_layers,
348 (dxgi_main.layer_count + layer_count) * sizeof(*new_layers));
350 if (!new_layers)
352 LeaveCriticalSection(&dxgi_cs);
353 ERR("Failed to allocate layer memory\n");
354 return E_OUTOFMEMORY;
357 for (i = 0; i < layer_count; ++i)
359 const struct dxgi_device_layer *layer = &layers[i];
361 TRACE("layer %d: id %#x, init %p, get_size %p, create %p\n",
362 i, layer->id, layer->init, layer->get_size, layer->create);
364 new_layers[dxgi_main.layer_count + i] = *layer;
367 dxgi_main.device_layers = new_layers;
368 dxgi_main.layer_count += layer_count;
370 LeaveCriticalSection(&dxgi_cs);
372 return S_OK;