winex11: Add window data structure locking to the GravityNotify event handler.
[wine.git] / dlls / dxgi / device.c
blob7bbc5e0b055f463c7552762860de31fd08bb07ae
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 inline struct dxgi_device *impl_from_IWineDXGIDevice(IWineDXGIDevice *iface)
29 return CONTAINING_RECORD(iface, struct dxgi_device, IWineDXGIDevice_iface);
32 /* IUnknown methods */
34 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
36 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
38 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
40 if (IsEqualGUID(riid, &IID_IUnknown)
41 || IsEqualGUID(riid, &IID_IDXGIObject)
42 || IsEqualGUID(riid, &IID_IDXGIDevice)
43 || IsEqualGUID(riid, &IID_IWineDXGIDevice))
45 IUnknown_AddRef(iface);
46 *object = iface;
47 return S_OK;
50 if (This->child_layer)
52 TRACE("forwarding to child layer %p\n", This->child_layer);
53 return IUnknown_QueryInterface(This->child_layer, riid, object);
56 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
58 *object = NULL;
59 return E_NOINTERFACE;
62 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
64 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
65 ULONG refcount = InterlockedIncrement(&This->refcount);
67 TRACE("%p increasing refcount to %u\n", This, refcount);
69 return refcount;
72 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
74 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
75 ULONG refcount = InterlockedDecrement(&This->refcount);
77 TRACE("%p decreasing refcount to %u\n", This, refcount);
79 if (!refcount)
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);
89 return refcount;
92 /* IDXGIObject methods */
94 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
95 REFGUID guid, UINT data_size, const void *data)
97 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
99 return E_NOTIMPL;
102 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
103 REFGUID guid, const IUnknown *object)
105 FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
107 return E_NOTIMPL;
110 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
111 REFGUID guid, UINT *data_size, void *data)
113 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
115 return E_NOTIMPL;
118 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
120 IDXGIAdapter *adapter;
121 HRESULT hr;
123 TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
125 hr = IWineDXGIDevice_GetAdapter(iface, &adapter);
126 if (FAILED(hr))
128 ERR("Failed to get adapter, hr %#x.\n", hr);
129 return hr;
132 hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
133 IDXGIAdapter_Release(adapter);
135 return hr;
138 /* IDXGIDevice methods */
140 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
142 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
143 struct wined3d_device_creation_parameters create_parameters;
144 HRESULT hr;
146 TRACE("iface %p, adapter %p\n", iface, adapter);
148 EnterCriticalSection(&dxgi_cs);
150 hr = wined3d_device_get_creation_parameters(This->wined3d_device, &create_parameters);
151 if (FAILED(hr))
153 LeaveCriticalSection(&dxgi_cs);
154 return hr;
157 LeaveCriticalSection(&dxgi_cs);
159 return IWineDXGIFactory_EnumAdapters(This->factory, create_parameters.adapter_idx, adapter);
162 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
163 const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
164 const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
166 struct wined3d_device_parent *device_parent;
167 IWineDXGIDeviceParent *dxgi_device_parent;
168 HRESULT hr;
169 UINT i;
170 UINT j;
172 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
173 iface, desc, surface_count, usage, shared_resource, surface);
175 hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
176 if (FAILED(hr))
178 ERR("Device should implement IWineD3DDeviceParent\n");
179 return E_FAIL;
182 device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
184 FIXME("Implement DXGI<->wined3d usage conversion\n");
186 memset(surface, 0, surface_count * sizeof(*surface));
187 for (i = 0; i < surface_count; ++i)
189 struct wined3d_surface *wined3d_surface;
190 IUnknown *parent;
192 hr = device_parent->ops->create_swapchain_surface(device_parent, NULL,
193 desc->Width, desc->Height, wined3dformat_from_dxgi_format(desc->Format), usage,
194 desc->SampleDesc.Count > 1 ? desc->SampleDesc.Count : WINED3D_MULTISAMPLE_NONE,
195 desc->SampleDesc.Quality, &wined3d_surface);
196 if (FAILED(hr))
198 ERR("CreateSurface failed, returning %#x\n", hr);
199 goto fail;
202 parent = wined3d_surface_get_parent(wined3d_surface);
203 hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
204 wined3d_surface_decref(wined3d_surface);
205 if (FAILED(hr))
207 ERR("Surface should implement IDXGISurface\n");
208 goto fail;
211 TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
213 IWineDXGIDeviceParent_Release(dxgi_device_parent);
215 return S_OK;
217 fail:
218 for (j = 0; j < i; ++j)
220 IDXGISurface_Release(surface[i]);
222 IWineDXGIDeviceParent_Release(dxgi_device_parent);
223 return hr;
226 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
227 IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
229 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
230 iface, resources, residency, resource_count);
232 return E_NOTIMPL;
235 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
237 FIXME("iface %p, priority %d stub!\n", iface, priority);
239 return E_NOTIMPL;
242 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
244 FIXME("iface %p, priority %p stub!\n", iface, priority);
246 return E_NOTIMPL;
249 /* IWineDXGIDevice methods */
251 static struct wined3d_device * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
253 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
255 TRACE("iface %p\n", iface);
257 EnterCriticalSection(&dxgi_cs);
258 wined3d_device_incref(This->wined3d_device);
259 LeaveCriticalSection(&dxgi_cs);
260 return This->wined3d_device;
263 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
264 DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
266 struct dxgi_surface *object;
267 HRESULT hr;
269 FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
270 iface, desc, usage, shared_resource, outer, surface);
272 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
273 if (!object)
275 ERR("Failed to allocate DXGI surface object memory\n");
276 return E_OUTOFMEMORY;
279 hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer);
280 if (FAILED(hr))
282 WARN("Failed to initialize surface, hr %#x.\n", hr);
283 HeapFree(GetProcessHeap(), 0, object);
284 return hr;
287 TRACE("Created IDXGISurface %p\n", object);
288 *surface = outer ? &object->IUnknown_iface : (IUnknown *)&object->IDXGISurface_iface;
290 return S_OK;
293 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
294 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **wined3d_swapchain)
296 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
297 struct dxgi_swapchain *object;
298 HRESULT hr;
300 TRACE("iface %p, desc %p, wined3d_swapchain %p.\n",
301 iface, desc, wined3d_swapchain);
303 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
304 if (!object)
306 ERR("Failed to allocate DXGI swapchain object memory\n");
307 return E_OUTOFMEMORY;
310 hr = dxgi_swapchain_init(object, This, desc);
311 if (FAILED(hr))
313 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
314 HeapFree(GetProcessHeap(), 0, object);
315 return hr;
318 TRACE("Created IDXGISwapChain %p\n", object);
319 *wined3d_swapchain = object->wined3d_swapchain;
321 return S_OK;
324 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
326 /* IUnknown methods */
327 dxgi_device_QueryInterface,
328 dxgi_device_AddRef,
329 dxgi_device_Release,
330 /* IDXGIObject methods */
331 dxgi_device_SetPrivateData,
332 dxgi_device_SetPrivateDataInterface,
333 dxgi_device_GetPrivateData,
334 dxgi_device_GetParent,
335 /* IDXGIDevice methods */
336 dxgi_device_GetAdapter,
337 dxgi_device_CreateSurface,
338 dxgi_device_QueryResourceResidency,
339 dxgi_device_SetGPUThreadPriority,
340 dxgi_device_GetGPUThreadPriority,
341 /* IWineDXGIAdapter methods */
342 dxgi_device_get_wined3d_device,
343 dxgi_device_create_surface,
344 dxgi_device_create_swapchain,
347 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
348 IDXGIFactory *factory, IDXGIAdapter *adapter)
350 struct wined3d_device_parent *wined3d_device_parent;
351 IWineDXGIDeviceParent *dxgi_device_parent;
352 IWineDXGIAdapter *wine_adapter;
353 UINT adapter_ordinal;
354 struct wined3d *wined3d;
355 void *layer_base;
356 HRESULT hr;
357 WINED3DCAPS caps;
359 device->IWineDXGIDevice_iface.lpVtbl = &dxgi_device_vtbl;
360 device->refcount = 1;
362 layer_base = device + 1;
364 hr = layer->create(layer->id, &layer_base, 0,
365 device, &IID_IUnknown, (void **)&device->child_layer);
366 if (FAILED(hr))
368 WARN("Failed to create device, returning %#x.\n", hr);
369 goto fail;
372 hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&device->factory);
373 if (FAILED(hr))
375 WARN("This is not the factory we're looking for, returning %#x.\n", hr);
376 goto fail;
378 wined3d = IWineDXGIFactory_get_wined3d(device->factory);
380 hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
381 if (FAILED(hr))
383 WARN("This is not the adapter we're looking for, returning %#x.\n", hr);
384 EnterCriticalSection(&dxgi_cs);
385 wined3d_decref(wined3d);
386 LeaveCriticalSection(&dxgi_cs);
387 goto fail;
389 adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
390 IWineDXGIAdapter_Release(wine_adapter);
392 hr = IWineDXGIDevice_QueryInterface(&device->IWineDXGIDevice_iface, &IID_IWineDXGIDeviceParent,
393 (void **)&dxgi_device_parent);
394 if (FAILED(hr))
396 ERR("DXGI device should implement IWineD3DDeviceParent.\n");
397 goto fail;
400 wined3d_device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
402 FIXME("Ignoring adapter type.\n");
404 hr = wined3d_get_device_caps(wined3d, adapter_ordinal, WINED3D_DEVICE_TYPE_HAL, &caps);
405 if (FAILED(hr) || caps.VertexShaderVersion < 4 || caps.PixelShaderVersion < 4)
407 WARN("Direct3D 10 is not supported on this GPU with the current shader backend.\n");
408 if (SUCCEEDED(hr))
409 hr = E_FAIL;
410 goto fail;
413 EnterCriticalSection(&dxgi_cs);
414 hr = wined3d_device_create(wined3d, adapter_ordinal, WINED3D_DEVICE_TYPE_HAL, NULL, 0, 4,
415 wined3d_device_parent, &device->wined3d_device);
416 IWineDXGIDeviceParent_Release(dxgi_device_parent);
417 wined3d_decref(wined3d);
418 LeaveCriticalSection(&dxgi_cs);
419 if (FAILED(hr))
421 WARN("Failed to create a wined3d device, returning %#x.\n", hr);
422 goto fail;
425 return S_OK;
427 fail:
428 if (device->wined3d_device)
430 EnterCriticalSection(&dxgi_cs);
431 wined3d_device_decref(device->wined3d_device);
432 LeaveCriticalSection(&dxgi_cs);
434 if (device->factory) IWineDXGIFactory_Release(device->factory);
435 if (device->child_layer) IUnknown_Release(device->child_layer);
436 return hr;