push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / dxgi / dxgi_main.c
blobd2de584aa665c83a15f31e06e8b4de56f94e3b3e
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 #include "dxgi_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
27 static CRITICAL_SECTION dxgi_cs;
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 static 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;
84 FIXME("riid %s, factory %p partial stub!\n", debugstr_guid(riid), factory);
86 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
87 if (!object)
89 ERR("Failed to allocate DXGI factory object memory\n");
90 return E_OUTOFMEMORY;
93 object->vtbl = &dxgi_factory_vtbl;
94 object->refcount = 1;
96 TRACE("Created IDXGIFactory %p\n", object);
98 hr = IDXGIFactory_QueryInterface((IDXGIFactory *)object, riid, factory);
99 IDXGIFactory_Release((IDXGIFactory *)object);
101 return hr;
104 static BOOL get_layer(enum dxgi_device_layer_id id, struct dxgi_device_layer *layer)
106 UINT i;
108 EnterCriticalSection(&dxgi_cs);
110 for (i = 0; i < dxgi_main.layer_count; ++i)
112 if (dxgi_main.device_layers[i].id == id)
114 *layer = dxgi_main.device_layers[i];
115 LeaveCriticalSection(&dxgi_cs);
116 return TRUE;
120 LeaveCriticalSection(&dxgi_cs);
121 return FALSE;
124 static HRESULT register_d3d10core_layers(HMODULE d3d10core)
126 EnterCriticalSection(&dxgi_cs);
128 if (!dxgi_main.d3d10core)
130 HRESULT hr;
131 HRESULT (WINAPI *d3d10core_register_layers)(void);
132 HMODULE mod;
133 BOOL ret;
135 ret = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)d3d10core, &mod);
136 if (!ret)
138 LeaveCriticalSection(&dxgi_cs);
139 return E_FAIL;
142 d3d10core_register_layers = (HRESULT (WINAPI *)(void))GetProcAddress(mod, "D3D10CoreRegisterLayers");
143 hr = d3d10core_register_layers();
144 if (FAILED(hr))
146 ERR("Failed to register d3d10core layers, returning %#x\n", hr);
147 LeaveCriticalSection(&dxgi_cs);
148 return hr;
151 dxgi_main.d3d10core = mod;
154 LeaveCriticalSection(&dxgi_cs);
156 return S_OK;
159 HRESULT WINAPI DXGID3D10CreateDevice(HMODULE d3d10core, IDXGIFactory *factory, IDXGIAdapter *adapter,
160 UINT flags, DWORD unknown0, void **device)
162 struct layer_get_size_args get_size_args;
163 struct dxgi_device *dxgi_device;
164 struct dxgi_device_layer d3d10_layer;
165 void *layer_base;
166 UINT device_size;
167 DWORD count;
168 HRESULT hr;
170 TRACE("d3d10core %p, factory %p, adapter %p, flags %#x, unknown0 %#x, device %p\n",
171 d3d10core, factory, adapter, flags, unknown0, device);
173 hr = register_d3d10core_layers(d3d10core);
174 if (FAILED(hr))
176 ERR("Failed to register d3d10core layers, returning %#x\n", hr);
177 return hr;
180 if (!get_layer(DXGI_DEVICE_LAYER_D3D10_DEVICE, &d3d10_layer))
182 ERR("Failed to get D3D10 device layer\n");
183 return E_FAIL;
186 count = 0;
187 hr = d3d10_layer.init(d3d10_layer.id, &count, NULL);
188 if (FAILED(hr))
190 WARN("Failed to initialize D3D10 device layer\n");
191 return E_FAIL;
194 get_size_args.unknown0 = 0;
195 get_size_args.unknown1 = 0;
196 get_size_args.unknown2 = NULL;
197 get_size_args.unknown3 = NULL;
198 get_size_args.adapter = adapter;
199 get_size_args.interface_major = 10;
200 get_size_args.interface_minor = 1;
201 get_size_args.version_build = 4;
202 get_size_args.version_revision = 6000;
204 device_size = d3d10_layer.get_size(d3d10_layer.id, &get_size_args, 0);
205 device_size += sizeof(*dxgi_device);
207 dxgi_device = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device_size);
208 if (!dxgi_device)
210 ERR("Failed to allocate device memory\n");
211 return E_OUTOFMEMORY;
214 dxgi_device->vtbl = &dxgi_device_vtbl;
215 dxgi_device->refcount = 1;
216 layer_base = dxgi_device + 1;
218 hr = d3d10_layer.create(d3d10_layer.id, &layer_base, 0,
219 dxgi_device, &IID_IUnknown, (void **)&dxgi_device->child_layer);
220 if (FAILED(hr))
222 WARN("Failed to create device, returning %#x\n", hr);
223 HeapFree(GetProcessHeap(), 0, dxgi_device);
224 *device = NULL;
225 return hr;
228 *device = (IUnknown *)dxgi_device;
230 return hr;
233 HRESULT WINAPI DXGID3D10RegisterLayers(const struct dxgi_device_layer *layers, UINT layer_count)
235 UINT i;
236 struct dxgi_device_layer *new_layers;
238 TRACE("layers %p, layer_count %u\n", layers, layer_count);
240 EnterCriticalSection(&dxgi_cs);
242 if (!dxgi_main.layer_count)
243 new_layers = HeapAlloc(GetProcessHeap(), 0, layer_count * sizeof(*new_layers));
244 else
245 new_layers = HeapReAlloc(GetProcessHeap(), 0, dxgi_main.device_layers,
246 (dxgi_main.layer_count + layer_count) * sizeof(*new_layers));
248 if (!new_layers)
250 LeaveCriticalSection(&dxgi_cs);
251 ERR("Failed to allocate layer memory\n");
252 return E_OUTOFMEMORY;
255 for (i = 0; i < layer_count; ++i)
257 const struct dxgi_device_layer *layer = &layers[i];
259 TRACE("layer %d: id %#x, init %p, get_size %p, create %p\n",
260 i, layer->id, layer->init, layer->get_size, layer->create);
262 new_layers[dxgi_main.layer_count + i] = *layer;
265 dxgi_main.device_layers = new_layers;
266 dxgi_main.layer_count += layer_count;
268 LeaveCriticalSection(&dxgi_cs);
270 return S_OK;