push 149f0a5527ac85057a8ef03858d34d91c36f97e8
[wine/hacks.git] / dlls / dxgi / device.c
blob8562d67f7826770f14b0959712b02d064ac7efb3
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 /* IUnknown methods */
29 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
31 struct dxgi_device *This = (struct dxgi_device *)iface;
33 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
35 if (IsEqualGUID(riid, &IID_IUnknown)
36 || IsEqualGUID(riid, &IID_IDXGIObject)
37 || IsEqualGUID(riid, &IID_IDXGIDevice)
38 || IsEqualGUID(riid, &IID_IWineDXGIDevice))
40 IUnknown_AddRef(iface);
41 *object = iface;
42 return S_OK;
45 if (This->child_layer)
47 TRACE("forwarding to child layer %p\n", This->child_layer);
48 return IUnknown_QueryInterface(This->child_layer, riid, object);
51 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
53 *object = NULL;
54 return E_NOINTERFACE;
57 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
59 struct dxgi_device *This = (struct dxgi_device *)iface;
60 ULONG refcount = InterlockedIncrement(&This->refcount);
62 TRACE("%p increasing refcount to %u\n", This, refcount);
64 return refcount;
67 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
69 struct dxgi_device *This = (struct dxgi_device *)iface;
70 ULONG refcount = InterlockedDecrement(&This->refcount);
72 TRACE("%p decreasing refcount to %u\n", This, refcount);
74 if (!refcount)
76 if (This->child_layer) IUnknown_Release(This->child_layer);
77 EnterCriticalSection(&dxgi_cs);
78 IWineD3DDevice_Release(This->wined3d_device);
79 LeaveCriticalSection(&dxgi_cs);
80 IWineDXGIFactory_Release(This->factory);
81 HeapFree(GetProcessHeap(), 0, This);
84 return refcount;
87 /* IDXGIObject methods */
89 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
90 REFGUID guid, UINT data_size, const void *data)
92 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
94 return E_NOTIMPL;
97 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
98 REFGUID guid, const IUnknown *object)
100 FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
102 return E_NOTIMPL;
105 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
106 REFGUID guid, UINT *data_size, void *data)
108 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
110 return E_NOTIMPL;
113 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
115 IDXGIAdapter *adapter;
116 HRESULT hr;
118 TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
120 hr = IDXGIDevice_GetAdapter(iface, &adapter);
121 if (FAILED(hr))
123 ERR("Failed to get adapter, hr %#x.\n", hr);
124 return hr;
127 hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
128 IDXGIAdapter_Release(adapter);
130 return hr;
133 /* IDXGIDevice methods */
135 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
137 struct dxgi_device *This = (struct dxgi_device *)iface;
138 WINED3DDEVICE_CREATION_PARAMETERS create_parameters;
139 HRESULT hr;
141 TRACE("iface %p, adapter %p\n", iface, adapter);
143 EnterCriticalSection(&dxgi_cs);
145 hr = IWineD3DDevice_GetCreationParameters(This->wined3d_device, &create_parameters);
146 if (FAILED(hr))
148 LeaveCriticalSection(&dxgi_cs);
149 return hr;
152 LeaveCriticalSection(&dxgi_cs);
154 return IWineDXGIFactory_EnumAdapters(This->factory, create_parameters.AdapterOrdinal, adapter);
157 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
158 const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
159 const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
161 IWineD3DDeviceParent *device_parent;
162 HRESULT hr;
163 UINT i;
164 UINT j;
166 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
167 iface, desc, surface_count, usage, shared_resource, surface);
169 hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineD3DDeviceParent, (void **)&device_parent);
170 if (FAILED(hr))
172 ERR("Device should implement IWineD3DDeviceParent\n");
173 return E_FAIL;
176 FIXME("Implement DXGI<->wined3d usage conversion\n");
178 memset(surface, 0, surface_count * sizeof(*surface));
179 for (i = 0; i < surface_count; ++i)
181 IWineD3DSurface *wined3d_surface;
182 IUnknown *parent;
184 hr = IWineD3DDeviceParent_CreateSurface(device_parent, NULL, desc->Width, desc->Height,
185 wined3dformat_from_dxgi_format(desc->Format), usage, WINED3DPOOL_DEFAULT, 0,
186 WINED3DCUBEMAP_FACE_POSITIVE_X, &wined3d_surface);
187 if (FAILED(hr))
189 ERR("CreateSurface failed, returning %#x\n", hr);
190 goto fail;
193 hr = IWineD3DSurface_GetParent(wined3d_surface, &parent);
194 IWineD3DSurface_Release(wined3d_surface);
195 if (FAILED(hr))
197 ERR("GetParent failed, returning %#x\n", hr);
198 goto fail;
201 hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
202 IUnknown_Release(parent);
203 if (FAILED(hr))
205 ERR("Surface should implement IDXGISurface\n");
206 goto fail;
209 TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
211 IWineD3DDeviceParent_Release(device_parent);
213 return S_OK;
215 fail:
216 for (j = 0; j < i; ++j)
218 IDXGISurface_Release(surface[i]);
220 IWineD3DDeviceParent_Release(device_parent);
221 return hr;
224 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
225 IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
227 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
228 iface, resources, residency, resource_count);
230 return E_NOTIMPL;
233 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
235 FIXME("iface %p, priority %d stub!\n", iface, priority);
237 return E_NOTIMPL;
240 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
242 FIXME("iface %p, priority %p stub!\n", iface, priority);
244 return E_NOTIMPL;
247 /* IWineDXGIDevice methods */
249 static IWineD3DDevice * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
251 struct dxgi_device *This = (struct dxgi_device *)iface;
253 TRACE("iface %p\n", iface);
255 EnterCriticalSection(&dxgi_cs);
256 IWineD3DDevice_AddRef(This->wined3d_device);
257 LeaveCriticalSection(&dxgi_cs);
258 return This->wined3d_device;
261 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
262 DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
264 struct dxgi_surface *object;
266 FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
267 iface, desc, usage, shared_resource, outer, surface);
269 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
270 if (!object)
272 ERR("Failed to allocate DXGI surface object memory\n");
273 return E_OUTOFMEMORY;
276 object->vtbl = &dxgi_surface_vtbl;
277 object->inner_unknown_vtbl = &dxgi_surface_inner_unknown_vtbl;
278 object->refcount = 1;
280 if (outer)
282 object->outer_unknown = outer;
283 *surface = &object->inner_unknown_vtbl;
285 else
287 object->outer_unknown = (IUnknown *)&object->inner_unknown_vtbl;
288 *surface = object;
291 TRACE("Created IDXGISurface %p\n", object);
293 return S_OK;
296 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
297 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **wined3d_swapchain)
299 struct dxgi_device *This = (struct dxgi_device *)iface;
300 struct dxgi_swapchain *object;
301 HRESULT hr;
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 object->vtbl = &dxgi_swapchain_vtbl;
311 object->refcount = 1;
313 hr = IWineD3DDevice_CreateSwapChain(This->wined3d_device, present_parameters,
314 &object->wined3d_swapchain, (IUnknown *)object, SURFACE_OPENGL);
315 if (FAILED(hr))
317 WARN("Failed to create a swapchain, returning %#x\n", hr);
318 HeapFree(GetProcessHeap(), 0, object);
319 return hr;
321 *wined3d_swapchain = object->wined3d_swapchain;
323 TRACE("Created IDXGISwapChain %p\n", object);
325 return S_OK;
328 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
330 /* IUnknown methods */
331 dxgi_device_QueryInterface,
332 dxgi_device_AddRef,
333 dxgi_device_Release,
334 /* IDXGIObject methods */
335 dxgi_device_SetPrivateData,
336 dxgi_device_SetPrivateDataInterface,
337 dxgi_device_GetPrivateData,
338 dxgi_device_GetParent,
339 /* IDXGIDevice methods */
340 dxgi_device_GetAdapter,
341 dxgi_device_CreateSurface,
342 dxgi_device_QueryResourceResidency,
343 dxgi_device_SetGPUThreadPriority,
344 dxgi_device_GetGPUThreadPriority,
345 /* IWineDXGIAdapter methods */
346 dxgi_device_get_wined3d_device,
347 dxgi_device_create_surface,
348 dxgi_device_create_swapchain,
351 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
352 IDXGIFactory *factory, IDXGIAdapter *adapter)
354 IWineD3DDeviceParent *wined3d_device_parent;
355 IWineDXGIAdapter *wine_adapter;
356 UINT adapter_ordinal;
357 IWineD3D *wined3d;
358 void *layer_base;
359 HRESULT hr;
361 device->vtbl = &dxgi_device_vtbl;
362 device->refcount = 1;
364 layer_base = device + 1;
366 hr = layer->create(layer->id, &layer_base, 0,
367 device, &IID_IUnknown, (void **)&device->child_layer);
368 if (FAILED(hr))
370 WARN("Failed to create device, returning %#x.\n", hr);
371 goto fail;
374 hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&device->factory);
375 if (FAILED(hr))
377 WARN("This is not the factory we're looking for, returning %#x.\n", hr);
378 goto fail;
380 wined3d = IWineDXGIFactory_get_wined3d(device->factory);
382 hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
383 if (FAILED(hr))
385 WARN("This is not the adapter we're looking for, returning %#x.\n", hr);
386 EnterCriticalSection(&dxgi_cs);
387 IWineD3D_Release(wined3d);
388 LeaveCriticalSection(&dxgi_cs);
389 goto fail;
391 adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
392 IWineDXGIAdapter_Release(wine_adapter);
394 hr = IUnknown_QueryInterface((IUnknown *)device, &IID_IWineD3DDeviceParent, (void **)&wined3d_device_parent);
395 if (FAILED(hr))
397 ERR("DXGI device should implement IWineD3DDeviceParent.\n");
398 goto fail;
401 FIXME("Ignoring adapter type.\n");
402 EnterCriticalSection(&dxgi_cs);
403 hr = IWineD3D_CreateDevice(wined3d, adapter_ordinal, WINED3DDEVTYPE_HAL, NULL, 0,
404 (IUnknown *)device, wined3d_device_parent, &device->wined3d_device);
405 IWineD3DDeviceParent_Release(wined3d_device_parent);
406 IWineD3D_Release(wined3d);
407 LeaveCriticalSection(&dxgi_cs);
408 if (FAILED(hr))
410 WARN("Failed to create a wined3d device, returning %#x.\n", hr);
411 goto fail;
414 return S_OK;
416 fail:
417 if (device->wined3d_device)
419 EnterCriticalSection(&dxgi_cs);
420 IWineD3DDevice_Release(device->wined3d_device);
421 LeaveCriticalSection(&dxgi_cs);
423 if (device->factory) IWineDXGIFactory_Release(device->factory);
424 if (device->child_layer) IUnknown_Release(device->child_layer);
425 return hr;