wineqtdecoder: Always pass non-NULL preferred allocator to IAsyncReader::RequestAlloc...
[wine.git] / dlls / dxgi / dxgi_main.c
blob8a39d931a75a34f39fb5e31302bb06a72c8b0e5f
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 struct dxgi_main
30 HMODULE d3d10core;
31 struct dxgi_device_layer *device_layers;
32 UINT layer_count;
34 static struct dxgi_main dxgi_main;
36 static void dxgi_main_cleanup(void)
38 HeapFree(GetProcessHeap(), 0, dxgi_main.device_layers);
39 FreeLibrary(dxgi_main.d3d10core);
42 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
44 switch (reason)
46 case DLL_PROCESS_ATTACH:
47 DisableThreadLibraryCalls(inst);
48 break;
50 case DLL_PROCESS_DETACH:
51 if (!reserved)
52 dxgi_main_cleanup();
53 break;
56 return TRUE;
59 HRESULT WINAPI CreateDXGIFactory1(REFIID riid, void **factory)
61 TRACE("riid %s, factory %p\n", debugstr_guid(riid), factory);
63 return dxgi_factory_create(riid, factory, TRUE);
66 HRESULT WINAPI CreateDXGIFactory(REFIID riid, void **factory)
68 TRACE("riid %s, factory %p\n", debugstr_guid(riid), factory);
70 return dxgi_factory_create(riid, factory, FALSE);
73 static BOOL get_layer(enum dxgi_device_layer_id id, struct dxgi_device_layer *layer)
75 UINT i;
77 wined3d_mutex_lock();
79 for (i = 0; i < dxgi_main.layer_count; ++i)
81 if (dxgi_main.device_layers[i].id == id)
83 *layer = dxgi_main.device_layers[i];
84 wined3d_mutex_unlock();
85 return TRUE;
89 wined3d_mutex_unlock();
90 return FALSE;
93 static HRESULT register_d3d10core_layers(HMODULE d3d10core)
95 wined3d_mutex_lock();
97 if (!dxgi_main.d3d10core)
99 HRESULT hr;
100 HRESULT (WINAPI *d3d11core_register_layers)(void);
101 HMODULE mod;
102 BOOL ret;
104 if (!(ret = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const char *)d3d10core, &mod)))
106 wined3d_mutex_unlock();
107 return E_FAIL;
110 d3d11core_register_layers = (void *)GetProcAddress(mod, "D3D11CoreRegisterLayers");
111 hr = d3d11core_register_layers();
112 if (FAILED(hr))
114 ERR("Failed to register d3d11 layers, returning %#x\n", hr);
115 wined3d_mutex_unlock();
116 return hr;
119 dxgi_main.d3d10core = mod;
122 wined3d_mutex_unlock();
124 return S_OK;
127 HRESULT WINAPI DXGID3D10CreateDevice(HMODULE d3d10core, IDXGIFactory *factory, IDXGIAdapter *adapter,
128 unsigned int flags, const D3D_FEATURE_LEVEL *feature_levels, unsigned int level_count, void **device)
130 struct layer_get_size_args get_size_args;
131 struct dxgi_device_layer d3d10_layer;
132 struct dxgi_device *dxgi_device;
133 UINT device_size;
134 DWORD count;
135 HRESULT hr;
137 TRACE("d3d10core %p, factory %p, adapter %p, flags %#x, feature_levels %p, level_count %u, device %p.\n",
138 d3d10core, factory, adapter, flags, feature_levels, level_count, device);
140 if (flags)
141 FIXME("Ignoring flags %#x.\n", flags);
143 if (TRACE_ON(dxgi))
144 dump_feature_levels(feature_levels, level_count);
146 hr = register_d3d10core_layers(d3d10core);
147 if (FAILED(hr))
149 ERR("Failed to register d3d10core layers, returning %#x.\n", hr);
150 return hr;
153 if (!get_layer(DXGI_DEVICE_LAYER_D3D10_DEVICE, &d3d10_layer))
155 ERR("Failed to get D3D10 device layer.\n");
156 return E_FAIL;
159 count = 0;
160 hr = d3d10_layer.init(d3d10_layer.id, &count, NULL);
161 if (FAILED(hr))
163 WARN("Failed to initialize D3D10 device layer.\n");
164 return E_FAIL;
167 get_size_args.unknown0 = 0;
168 get_size_args.unknown1 = 0;
169 get_size_args.unknown2 = NULL;
170 get_size_args.unknown3 = NULL;
171 get_size_args.adapter = adapter;
172 get_size_args.interface_major = 10;
173 get_size_args.interface_minor = 1;
174 get_size_args.version_build = 4;
175 get_size_args.version_revision = 6000;
177 device_size = d3d10_layer.get_size(d3d10_layer.id, &get_size_args, 0);
178 device_size += sizeof(*dxgi_device);
180 dxgi_device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device_size);
181 if (!dxgi_device)
183 ERR("Failed to allocate device memory.\n");
184 return E_OUTOFMEMORY;
187 hr = dxgi_device_init(dxgi_device, &d3d10_layer, factory, adapter, feature_levels, level_count);
188 if (FAILED(hr))
190 WARN("Failed to initialize device, hr %#x.\n", hr);
191 HeapFree(GetProcessHeap(), 0, dxgi_device);
192 *device = NULL;
193 return hr;
196 TRACE("Created device %p.\n", dxgi_device);
197 *device = dxgi_device;
199 return S_OK;
202 HRESULT WINAPI DXGID3D10RegisterLayers(const struct dxgi_device_layer *layers, UINT layer_count)
204 UINT i;
205 struct dxgi_device_layer *new_layers;
207 TRACE("layers %p, layer_count %u\n", layers, layer_count);
209 wined3d_mutex_lock();
211 if (!dxgi_main.layer_count)
212 new_layers = HeapAlloc(GetProcessHeap(), 0, layer_count * sizeof(*new_layers));
213 else
214 new_layers = HeapReAlloc(GetProcessHeap(), 0, dxgi_main.device_layers,
215 (dxgi_main.layer_count + layer_count) * sizeof(*new_layers));
217 if (!new_layers)
219 wined3d_mutex_unlock();
220 ERR("Failed to allocate layer memory\n");
221 return E_OUTOFMEMORY;
224 for (i = 0; i < layer_count; ++i)
226 const struct dxgi_device_layer *layer = &layers[i];
228 TRACE("layer %d: id %#x, init %p, get_size %p, create %p\n",
229 i, layer->id, layer->init, layer->get_size, layer->create);
231 new_layers[dxgi_main.layer_count + i] = *layer;
234 dxgi_main.device_layers = new_layers;
235 dxgi_main.layer_count += layer_count;
237 wined3d_mutex_unlock();
239 return S_OK;