comctl32/tests: Use BOOL type where appropriate.
[wine.git] / dlls / dxgi / dxgi_main.c
blob8249b88284b754839f4344c478f7ff4446ba8ab2
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;
43 static struct dxgi_main dxgi_main;
45 static void dxgi_main_cleanup(void)
47 HeapFree(GetProcessHeap(), 0, dxgi_main.device_layers);
48 FreeLibrary(dxgi_main.d3d10core);
49 DeleteCriticalSection(&dxgi_cs);
52 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
54 switch (reason)
56 case DLL_PROCESS_ATTACH:
57 DisableThreadLibraryCalls(inst);
58 break;
60 case DLL_PROCESS_DETACH:
61 if (!reserved)
62 dxgi_main_cleanup();
63 break;
66 return TRUE;
69 HRESULT WINAPI CreateDXGIFactory(REFIID riid, void **factory)
71 struct dxgi_factory *object;
72 HRESULT hr;
74 TRACE("riid %s, factory %p\n", debugstr_guid(riid), factory);
76 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
77 if (!object)
79 ERR("Failed to allocate DXGI factory object memory\n");
80 *factory = NULL;
81 return E_OUTOFMEMORY;
84 hr = dxgi_factory_init(object);
85 if (FAILED(hr))
87 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
88 HeapFree(GetProcessHeap(), 0, object);
89 *factory = NULL;
90 return hr;
93 TRACE("Created IDXGIFactory %p\n", object);
95 hr = IDXGIFactory_QueryInterface((IDXGIFactory *)object, riid, factory);
96 IDXGIFactory_Release((IDXGIFactory *)object);
98 return hr;
101 static BOOL get_layer(enum dxgi_device_layer_id id, struct dxgi_device_layer *layer)
103 UINT i;
105 EnterCriticalSection(&dxgi_cs);
107 for (i = 0; i < dxgi_main.layer_count; ++i)
109 if (dxgi_main.device_layers[i].id == id)
111 *layer = dxgi_main.device_layers[i];
112 LeaveCriticalSection(&dxgi_cs);
113 return TRUE;
117 LeaveCriticalSection(&dxgi_cs);
118 return FALSE;
121 static HRESULT register_d3d10core_layers(HMODULE d3d10core)
123 EnterCriticalSection(&dxgi_cs);
125 if (!dxgi_main.d3d10core)
127 HRESULT hr;
128 HRESULT (WINAPI *d3d10core_register_layers)(void);
129 HMODULE mod;
130 BOOL ret;
132 if (!(ret = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const char *)d3d10core, &mod)))
134 LeaveCriticalSection(&dxgi_cs);
135 return E_FAIL;
138 d3d10core_register_layers = (void *)GetProcAddress(mod, "D3D10CoreRegisterLayers");
139 hr = d3d10core_register_layers();
140 if (FAILED(hr))
142 ERR("Failed to register d3d10core layers, returning %#x\n", hr);
143 LeaveCriticalSection(&dxgi_cs);
144 return hr;
147 dxgi_main.d3d10core = mod;
150 LeaveCriticalSection(&dxgi_cs);
152 return S_OK;
155 HRESULT WINAPI DXGID3D10CreateDevice(HMODULE d3d10core, IDXGIFactory *factory, IDXGIAdapter *adapter,
156 UINT flags, void *unknown0, void **device)
158 struct layer_get_size_args get_size_args;
159 struct dxgi_device *dxgi_device;
160 struct dxgi_device_layer d3d10_layer;
161 UINT device_size;
162 DWORD count;
163 HRESULT hr;
165 TRACE("d3d10core %p, factory %p, adapter %p, flags %#x, unknown0 %p, device %p.\n",
166 d3d10core, factory, adapter, flags, unknown0, device);
168 hr = register_d3d10core_layers(d3d10core);
169 if (FAILED(hr))
171 ERR("Failed to register d3d10core layers, returning %#x\n", hr);
172 return hr;
175 if (!get_layer(DXGI_DEVICE_LAYER_D3D10_DEVICE, &d3d10_layer))
177 ERR("Failed to get D3D10 device layer\n");
178 return E_FAIL;
181 count = 0;
182 hr = d3d10_layer.init(d3d10_layer.id, &count, NULL);
183 if (FAILED(hr))
185 WARN("Failed to initialize D3D10 device layer\n");
186 return E_FAIL;
189 get_size_args.unknown0 = 0;
190 get_size_args.unknown1 = 0;
191 get_size_args.unknown2 = NULL;
192 get_size_args.unknown3 = NULL;
193 get_size_args.adapter = adapter;
194 get_size_args.interface_major = 10;
195 get_size_args.interface_minor = 1;
196 get_size_args.version_build = 4;
197 get_size_args.version_revision = 6000;
199 device_size = d3d10_layer.get_size(d3d10_layer.id, &get_size_args, 0);
200 device_size += sizeof(*dxgi_device);
202 dxgi_device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device_size);
203 if (!dxgi_device)
205 ERR("Failed to allocate device memory\n");
206 return E_OUTOFMEMORY;
209 hr = dxgi_device_init(dxgi_device, &d3d10_layer, factory, adapter);
210 if (FAILED(hr))
212 WARN("Failed to initialize device, hr %#x.\n", hr);
213 HeapFree(GetProcessHeap(), 0, dxgi_device);
214 *device = NULL;
215 return hr;
218 TRACE("Created device %p.\n", dxgi_device);
219 *device = dxgi_device;
221 return S_OK;
224 HRESULT WINAPI DXGID3D10RegisterLayers(const struct dxgi_device_layer *layers, UINT layer_count)
226 UINT i;
227 struct dxgi_device_layer *new_layers;
229 TRACE("layers %p, layer_count %u\n", layers, layer_count);
231 EnterCriticalSection(&dxgi_cs);
233 if (!dxgi_main.layer_count)
234 new_layers = HeapAlloc(GetProcessHeap(), 0, layer_count * sizeof(*new_layers));
235 else
236 new_layers = HeapReAlloc(GetProcessHeap(), 0, dxgi_main.device_layers,
237 (dxgi_main.layer_count + layer_count) * sizeof(*new_layers));
239 if (!new_layers)
241 LeaveCriticalSection(&dxgi_cs);
242 ERR("Failed to allocate layer memory\n");
243 return E_OUTOFMEMORY;
246 for (i = 0; i < layer_count; ++i)
248 const struct dxgi_device_layer *layer = &layers[i];
250 TRACE("layer %d: id %#x, init %p, get_size %p, create %p\n",
251 i, layer->id, layer->init, layer->get_size, layer->create);
253 new_layers[dxgi_main.layer_count + i] = *layer;
256 dxgi_main.device_layers = new_layers;
257 dxgi_main.layer_count += layer_count;
259 LeaveCriticalSection(&dxgi_cs);
261 return S_OK;