msxml3: Fix indexed access to schema collection namespace uris.
[wine] / dlls / dxgi / device.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 #include "dxgi_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
26
27 static inline struct dxgi_device *impl_from_IWineDXGIDevice(IWineDXGIDevice *iface)
28 {
29     return CONTAINING_RECORD(iface, struct dxgi_device, IWineDXGIDevice_iface);
30 }
31
32 /* IUnknown methods */
33
34 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
35 {
36     struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
37
38     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
39
40     if (IsEqualGUID(riid, &IID_IUnknown)
41             || IsEqualGUID(riid, &IID_IDXGIObject)
42             || IsEqualGUID(riid, &IID_IDXGIDevice)
43             || IsEqualGUID(riid, &IID_IWineDXGIDevice))
44     {
45         IUnknown_AddRef(iface);
46         *object = iface;
47         return S_OK;
48     }
49
50     if (This->child_layer)
51     {
52         TRACE("forwarding to child layer %p\n", This->child_layer);
53         return IUnknown_QueryInterface(This->child_layer, riid, object);
54     }
55
56     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
57
58     *object = NULL;
59     return E_NOINTERFACE;
60 }
61
62 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
63 {
64     struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
65     ULONG refcount = InterlockedIncrement(&This->refcount);
66
67     TRACE("%p increasing refcount to %u\n", This, refcount);
68
69     return refcount;
70 }
71
72 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
73 {
74     struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
75     ULONG refcount = InterlockedDecrement(&This->refcount);
76
77     TRACE("%p decreasing refcount to %u\n", This, refcount);
78
79     if (!refcount)
80     {
81         if (This->child_layer) IUnknown_Release(This->child_layer);
82         EnterCriticalSection(&dxgi_cs);
83         wined3d_device_decref(This->wined3d_device);
84         LeaveCriticalSection(&dxgi_cs);
85         IWineDXGIFactory_Release(This->factory);
86         HeapFree(GetProcessHeap(), 0, This);
87     }
88
89     return refcount;
90 }
91
92 /* IDXGIObject methods */
93
94 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
95         REFGUID guid, UINT data_size, const void *data)
96 {
97     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
98
99     return E_NOTIMPL;
100 }
101
102 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
103         REFGUID guid, const IUnknown *object)
104 {
105     FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
106
107     return E_NOTIMPL;
108 }
109
110 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
111         REFGUID guid, UINT *data_size, void *data)
112 {
113     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
114
115     return E_NOTIMPL;
116 }
117
118 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
119 {
120     IDXGIAdapter *adapter;
121     HRESULT hr;
122
123     TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
124
125     hr = IWineDXGIDevice_GetAdapter(iface, &adapter);
126     if (FAILED(hr))
127     {
128         ERR("Failed to get adapter, hr %#x.\n", hr);
129         return hr;
130     }
131
132     hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
133     IDXGIAdapter_Release(adapter);
134
135     return hr;
136 }
137
138 /* IDXGIDevice methods */
139
140 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
141 {
142     struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
143     struct wined3d_device_creation_parameters create_parameters;
144
145     TRACE("iface %p, adapter %p\n", iface, adapter);
146
147     EnterCriticalSection(&dxgi_cs);
148     wined3d_device_get_creation_parameters(This->wined3d_device, &create_parameters);
149     LeaveCriticalSection(&dxgi_cs);
150
151     return IWineDXGIFactory_EnumAdapters(This->factory, create_parameters.adapter_idx, adapter);
152 }
153
154 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
155         const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
156         const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
157 {
158     struct wined3d_device_parent *device_parent;
159     IWineDXGIDeviceParent *dxgi_device_parent;
160     HRESULT hr;
161     UINT i;
162     UINT j;
163
164     TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
165             iface, desc, surface_count, usage, shared_resource, surface);
166
167     hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
168     if (FAILED(hr))
169     {
170         ERR("Device should implement IWineD3DDeviceParent\n");
171         return E_FAIL;
172     }
173
174     device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
175
176     FIXME("Implement DXGI<->wined3d usage conversion\n");
177
178     memset(surface, 0, surface_count * sizeof(*surface));
179     for (i = 0; i < surface_count; ++i)
180     {
181         struct wined3d_surface *wined3d_surface;
182         IUnknown *parent;
183
184         hr = device_parent->ops->create_swapchain_surface(device_parent, NULL,
185                 desc->Width, desc->Height, wined3dformat_from_dxgi_format(desc->Format), usage,
186                 desc->SampleDesc.Count > 1 ? desc->SampleDesc.Count : WINED3D_MULTISAMPLE_NONE,
187                 desc->SampleDesc.Quality, &wined3d_surface);
188         if (FAILED(hr))
189         {
190             ERR("CreateSurface failed, returning %#x\n", hr);
191             goto fail;
192         }
193
194         parent = wined3d_surface_get_parent(wined3d_surface);
195         hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
196         wined3d_surface_decref(wined3d_surface);
197         if (FAILED(hr))
198         {
199             ERR("Surface should implement IDXGISurface\n");
200             goto fail;
201         }
202
203         TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
204     }
205     IWineDXGIDeviceParent_Release(dxgi_device_parent);
206
207     return S_OK;
208
209 fail:
210     for (j = 0; j < i; ++j)
211     {
212         IDXGISurface_Release(surface[i]);
213     }
214     IWineDXGIDeviceParent_Release(dxgi_device_parent);
215     return hr;
216 }
217
218 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
219         IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
220 {
221     FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
222             iface, resources, residency, resource_count);
223
224     return E_NOTIMPL;
225 }
226
227 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
228 {
229     FIXME("iface %p, priority %d stub!\n", iface, priority);
230
231     return E_NOTIMPL;
232 }
233
234 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
235 {
236     FIXME("iface %p, priority %p stub!\n", iface, priority);
237
238     return E_NOTIMPL;
239 }
240
241 /* IWineDXGIDevice methods */
242
243 static struct wined3d_device * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
244 {
245     struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
246
247     TRACE("iface %p\n", iface);
248
249     EnterCriticalSection(&dxgi_cs);
250     wined3d_device_incref(This->wined3d_device);
251     LeaveCriticalSection(&dxgi_cs);
252     return This->wined3d_device;
253 }
254
255 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
256         DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
257 {
258     struct dxgi_surface *object;
259     HRESULT hr;
260
261     FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
262             iface, desc, usage, shared_resource, outer, surface);
263
264     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
265     if (!object)
266     {
267         ERR("Failed to allocate DXGI surface object memory\n");
268         return E_OUTOFMEMORY;
269     }
270
271     hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer);
272     if (FAILED(hr))
273     {
274         WARN("Failed to initialize surface, hr %#x.\n", hr);
275         HeapFree(GetProcessHeap(), 0, object);
276         return hr;
277     }
278
279     TRACE("Created IDXGISurface %p\n", object);
280     *surface = outer ? &object->IUnknown_iface : (IUnknown *)&object->IDXGISurface_iface;
281
282     return S_OK;
283 }
284
285 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
286         struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **wined3d_swapchain)
287 {
288     struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
289     struct dxgi_swapchain *object;
290     HRESULT hr;
291
292     TRACE("iface %p, desc %p, wined3d_swapchain %p.\n",
293             iface, desc, wined3d_swapchain);
294
295     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
296     if (!object)
297     {
298         ERR("Failed to allocate DXGI swapchain object memory\n");
299         return E_OUTOFMEMORY;
300     }
301
302     hr = dxgi_swapchain_init(object, This, desc);
303     if (FAILED(hr))
304     {
305         WARN("Failed to initialize swapchain, hr %#x.\n", hr);
306         HeapFree(GetProcessHeap(), 0, object);
307         return hr;
308     }
309
310     TRACE("Created IDXGISwapChain %p\n", object);
311     *wined3d_swapchain = object->wined3d_swapchain;
312
313     return S_OK;
314 }
315
316 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
317 {
318     /* IUnknown methods */
319     dxgi_device_QueryInterface,
320     dxgi_device_AddRef,
321     dxgi_device_Release,
322     /* IDXGIObject methods */
323     dxgi_device_SetPrivateData,
324     dxgi_device_SetPrivateDataInterface,
325     dxgi_device_GetPrivateData,
326     dxgi_device_GetParent,
327     /* IDXGIDevice methods */
328     dxgi_device_GetAdapter,
329     dxgi_device_CreateSurface,
330     dxgi_device_QueryResourceResidency,
331     dxgi_device_SetGPUThreadPriority,
332     dxgi_device_GetGPUThreadPriority,
333     /* IWineDXGIAdapter methods */
334     dxgi_device_get_wined3d_device,
335     dxgi_device_create_surface,
336     dxgi_device_create_swapchain,
337 };
338
339 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
340         IDXGIFactory *factory, IDXGIAdapter *adapter)
341 {
342     struct wined3d_device_parent *wined3d_device_parent;
343     IWineDXGIDeviceParent *dxgi_device_parent;
344     IWineDXGIAdapter *wine_adapter;
345     UINT adapter_ordinal;
346     struct wined3d *wined3d;
347     void *layer_base;
348     HRESULT hr;
349     WINED3DCAPS caps;
350
351     device->IWineDXGIDevice_iface.lpVtbl = &dxgi_device_vtbl;
352     device->refcount = 1;
353
354     layer_base = device + 1;
355
356     hr = layer->create(layer->id, &layer_base, 0,
357             device, &IID_IUnknown, (void **)&device->child_layer);
358     if (FAILED(hr))
359     {
360         WARN("Failed to create device, returning %#x.\n", hr);
361         goto fail;
362     }
363
364     hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&device->factory);
365     if (FAILED(hr))
366     {
367         WARN("This is not the factory we're looking for, returning %#x.\n", hr);
368         goto fail;
369     }
370     wined3d = IWineDXGIFactory_get_wined3d(device->factory);
371
372     hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
373     if (FAILED(hr))
374     {
375         WARN("This is not the adapter we're looking for, returning %#x.\n", hr);
376         EnterCriticalSection(&dxgi_cs);
377         wined3d_decref(wined3d);
378         LeaveCriticalSection(&dxgi_cs);
379         goto fail;
380     }
381     adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
382     IWineDXGIAdapter_Release(wine_adapter);
383
384     hr = IWineDXGIDevice_QueryInterface(&device->IWineDXGIDevice_iface, &IID_IWineDXGIDeviceParent,
385             (void **)&dxgi_device_parent);
386     if (FAILED(hr))
387     {
388         ERR("DXGI device should implement IWineD3DDeviceParent.\n");
389         goto fail;
390     }
391
392     wined3d_device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
393
394     FIXME("Ignoring adapter type.\n");
395
396     hr = wined3d_get_device_caps(wined3d, adapter_ordinal, WINED3D_DEVICE_TYPE_HAL, &caps);
397     if (FAILED(hr) || caps.VertexShaderVersion < 4 || caps.PixelShaderVersion < 4)
398     {
399         WARN("Direct3D 10 is not supported on this GPU with the current shader backend.\n");
400         if (SUCCEEDED(hr))
401             hr = E_FAIL;
402         goto fail;
403     }
404
405     EnterCriticalSection(&dxgi_cs);
406     hr = wined3d_device_create(wined3d, adapter_ordinal, WINED3D_DEVICE_TYPE_HAL, NULL, 0, 4,
407             wined3d_device_parent, &device->wined3d_device);
408     IWineDXGIDeviceParent_Release(dxgi_device_parent);
409     wined3d_decref(wined3d);
410     LeaveCriticalSection(&dxgi_cs);
411     if (FAILED(hr))
412     {
413         WARN("Failed to create a wined3d device, returning %#x.\n", hr);
414         goto fail;
415     }
416
417     return S_OK;
418
419 fail:
420     if (device->wined3d_device)
421     {
422         EnterCriticalSection(&dxgi_cs);
423         wined3d_device_decref(device->wined3d_device);
424         LeaveCriticalSection(&dxgi_cs);
425     }
426     if (device->factory) IWineDXGIFactory_Release(device->factory);
427     if (device->child_layer) IUnknown_Release(device->child_layer);
428     return hr;
429 }