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