inetcomm: Support LPSTR to LPWSTR conversion in GetProp.
[wine.git] / dlls / dxgi / swapchain.c
blob921fcdfeb2a28957a402f20aaef77a17bc44a107
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_swapchain *impl_from_IDXGISwapChain(IDXGISwapChain *iface)
29 return CONTAINING_RECORD(iface, struct dxgi_swapchain, IDXGISwapChain_iface);
32 /* IUnknown methods */
34 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_QueryInterface(IDXGISwapChain *iface, REFIID riid, void **object)
36 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
38 if (IsEqualGUID(riid, &IID_IUnknown)
39 || IsEqualGUID(riid, &IID_IDXGIObject)
40 || IsEqualGUID(riid, &IID_IDXGIDeviceSubObject)
41 || IsEqualGUID(riid, &IID_IDXGISwapChain))
43 IUnknown_AddRef(iface);
44 *object = iface;
45 return S_OK;
48 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
50 *object = NULL;
51 return E_NOINTERFACE;
54 static ULONG STDMETHODCALLTYPE dxgi_swapchain_AddRef(IDXGISwapChain *iface)
56 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
57 ULONG refcount = InterlockedIncrement(&This->refcount);
59 TRACE("%p increasing refcount to %u\n", This, refcount);
61 if (refcount == 1)
63 wined3d_mutex_lock();
64 wined3d_swapchain_incref(This->wined3d_swapchain);
65 wined3d_mutex_unlock();
68 return refcount;
71 static ULONG STDMETHODCALLTYPE dxgi_swapchain_Release(IDXGISwapChain *iface)
73 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
74 ULONG refcount = InterlockedDecrement(&swapchain->refcount);
76 TRACE("%p decreasing refcount to %u.\n", swapchain, refcount);
78 if (!refcount)
80 IWineDXGIDevice *device = swapchain->device;
81 if (swapchain->factory)
82 IDXGIFactory_Release(swapchain->factory);
83 wined3d_mutex_lock();
84 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
85 wined3d_mutex_unlock();
86 if (device)
87 IWineDXGIDevice_Release(device);
90 return refcount;
93 /* IDXGIObject methods */
95 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_SetPrivateData(IDXGISwapChain *iface,
96 REFGUID guid, UINT data_size, const void *data)
98 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
100 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
102 return dxgi_set_private_data(&swapchain->private_store, guid, data_size, data);
105 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_SetPrivateDataInterface(IDXGISwapChain *iface,
106 REFGUID guid, const IUnknown *object)
108 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
110 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
112 return dxgi_set_private_data_interface(&swapchain->private_store, guid, object);
115 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetPrivateData(IDXGISwapChain *iface,
116 REFGUID guid, UINT *data_size, void *data)
118 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
120 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
122 return dxgi_get_private_data(&swapchain->private_store, guid, data_size, data);
125 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetParent(IDXGISwapChain *iface, REFIID riid, void **parent)
127 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
129 TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
131 if (!swapchain->factory)
133 ERR("Implicit swapchain does not store reference to parent.\n");
134 *parent = NULL;
135 return E_NOINTERFACE;
138 return IDXGIFactory_QueryInterface(swapchain->factory, riid, parent);
141 /* IDXGIDeviceSubObject methods */
143 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetDevice(IDXGISwapChain *iface, REFIID riid, void **device)
145 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
147 TRACE("iface %p, riid %s, device %p.\n", iface, debugstr_guid(riid), device);
149 if (!swapchain->device)
151 ERR("Implicit swapchain does not store reference to device.\n");
152 *device = NULL;
153 return E_NOINTERFACE;
156 return IWineDXGIDevice_QueryInterface(swapchain->device, riid, device);
159 /* IDXGISwapChain methods */
161 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_Present(IDXGISwapChain *iface, UINT sync_interval, UINT flags)
163 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
164 HRESULT hr;
166 TRACE("iface %p, sync_interval %u, flags %#x\n", iface, sync_interval, flags);
168 if (sync_interval) FIXME("Unimplemented sync interval %u\n", sync_interval);
169 if (flags) FIXME("Unimplemented flags %#x\n", flags);
171 wined3d_mutex_lock();
172 hr = wined3d_swapchain_present(This->wined3d_swapchain, NULL, NULL, NULL, 0);
173 wined3d_mutex_unlock();
175 return hr;
178 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetBuffer(IDXGISwapChain *iface,
179 UINT buffer_idx, REFIID riid, void **surface)
181 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
182 struct wined3d_texture *texture;
183 IUnknown *parent;
184 HRESULT hr;
186 TRACE("iface %p, buffer_idx %u, riid %s, surface %p\n",
187 iface, buffer_idx, debugstr_guid(riid), surface);
189 wined3d_mutex_lock();
191 if (!(texture = wined3d_swapchain_get_back_buffer(This->wined3d_swapchain, buffer_idx)))
193 wined3d_mutex_unlock();
194 return DXGI_ERROR_INVALID_CALL;
197 parent = wined3d_texture_get_parent(texture);
198 hr = IUnknown_QueryInterface(parent, riid, surface);
199 wined3d_mutex_unlock();
201 return hr;
204 static HRESULT STDMETHODCALLTYPE DECLSPEC_HOTPATCH dxgi_swapchain_SetFullscreenState(IDXGISwapChain *iface,
205 BOOL fullscreen, IDXGIOutput *target)
207 FIXME("iface %p, fullscreen %#x, target %p stub!\n", iface, fullscreen, target);
209 return E_NOTIMPL;
212 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetFullscreenState(IDXGISwapChain *iface,
213 BOOL *fullscreen, IDXGIOutput **target)
215 FIXME("iface %p, fullscreen %p, target %p stub!\n", iface, fullscreen, target);
217 return E_NOTIMPL;
220 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetDesc(IDXGISwapChain *iface, DXGI_SWAP_CHAIN_DESC *desc)
222 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
223 struct wined3d_swapchain_desc wined3d_desc;
225 FIXME("iface %p, desc %p partial stub!\n", iface, desc);
227 if (desc == NULL)
228 return E_INVALIDARG;
230 wined3d_mutex_lock();
231 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
232 wined3d_mutex_unlock();
234 FIXME("Ignoring ScanlineOrdering, Scaling, SwapEffect and Flags\n");
236 desc->BufferDesc.Width = wined3d_desc.backbuffer_width;
237 desc->BufferDesc.Height = wined3d_desc.backbuffer_height;
238 desc->BufferDesc.RefreshRate.Numerator = wined3d_desc.refresh_rate;
239 desc->BufferDesc.RefreshRate.Denominator = 1;
240 desc->BufferDesc.Format = dxgi_format_from_wined3dformat(wined3d_desc.backbuffer_format);
241 desc->BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
242 desc->BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
243 dxgi_sample_desc_from_wined3d(&desc->SampleDesc, wined3d_desc.multisample_type, wined3d_desc.multisample_quality);
244 desc->BufferCount = wined3d_desc.backbuffer_count;
245 desc->OutputWindow = wined3d_desc.device_window;
246 desc->Windowed = wined3d_desc.windowed;
247 desc->SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
248 desc->Flags = 0;
250 return S_OK;
253 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_ResizeBuffers(IDXGISwapChain *iface,
254 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags)
256 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
257 struct wined3d_swapchain_desc wined3d_desc;
258 struct wined3d_texture *texture;
259 IUnknown *parent;
260 unsigned int i;
261 HRESULT hr;
263 TRACE("iface %p, buffer_count %u, width %u, height %u, format %s, flags %#x.\n",
264 iface, buffer_count, width, height, debug_dxgi_format(format), flags);
266 if (flags)
267 FIXME("Ignoring flags %#x.\n", flags);
269 wined3d_mutex_lock();
270 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
271 for (i = 0; i < wined3d_desc.backbuffer_count; ++i)
273 texture = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain, i);
274 parent = wined3d_texture_get_parent(texture);
275 IUnknown_AddRef(parent);
276 if (IUnknown_Release(parent))
278 wined3d_mutex_unlock();
279 return DXGI_ERROR_INVALID_CALL;
282 if (format != DXGI_FORMAT_UNKNOWN)
283 wined3d_desc.backbuffer_format = wined3dformat_from_dxgi_format(format);
284 hr = wined3d_swapchain_resize_buffers(swapchain->wined3d_swapchain, buffer_count, width, height,
285 wined3d_desc.backbuffer_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality);
286 wined3d_mutex_unlock();
288 return hr;
291 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_ResizeTarget(IDXGISwapChain *iface,
292 const DXGI_MODE_DESC *target_mode_desc)
294 FIXME("iface %p, target_mode_desc %p stub!\n", iface, target_mode_desc);
296 return E_NOTIMPL;
299 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetContainingOutput(IDXGISwapChain *iface, IDXGIOutput **output)
301 IDXGIAdapter *adapter;
302 IDXGIDevice *device;
303 HRESULT hr;
305 TRACE("iface %p, output %p.\n", iface, output);
307 if (FAILED(hr = dxgi_swapchain_GetDevice(iface, &IID_IDXGIDevice, (void **)&device)))
308 return hr;
310 hr = IDXGIDevice_GetAdapter(device, &adapter);
311 IDXGIDevice_Release(device);
312 if (FAILED(hr))
314 WARN("GetAdapter failed, hr %#x.\n", hr);
315 return hr;
318 if (SUCCEEDED(IDXGIAdapter_EnumOutputs(adapter, 1, output)))
320 FIXME("Adapter has got multiple outputs, returning the first one.\n");
321 IDXGIOutput_Release(*output);
324 hr = IDXGIAdapter_EnumOutputs(adapter, 0, output);
325 IDXGIAdapter_Release(adapter);
326 return hr;
329 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetFrameStatistics(IDXGISwapChain *iface, DXGI_FRAME_STATISTICS *stats)
331 FIXME("iface %p, stats %p stub!\n", iface, stats);
333 return E_NOTIMPL;
336 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetLastPresentCount(IDXGISwapChain *iface, UINT *last_present_count)
338 FIXME("iface %p, last_present_count %p stub!\n", iface, last_present_count);
340 return E_NOTIMPL;
343 static const struct IDXGISwapChainVtbl dxgi_swapchain_vtbl =
345 /* IUnknown methods */
346 dxgi_swapchain_QueryInterface,
347 dxgi_swapchain_AddRef,
348 dxgi_swapchain_Release,
349 /* IDXGIObject methods */
350 dxgi_swapchain_SetPrivateData,
351 dxgi_swapchain_SetPrivateDataInterface,
352 dxgi_swapchain_GetPrivateData,
353 dxgi_swapchain_GetParent,
354 /* IDXGIDeviceSubObject methods */
355 dxgi_swapchain_GetDevice,
356 /* IDXGISwapChain methods */
357 dxgi_swapchain_Present,
358 dxgi_swapchain_GetBuffer,
359 dxgi_swapchain_SetFullscreenState,
360 dxgi_swapchain_GetFullscreenState,
361 dxgi_swapchain_GetDesc,
362 dxgi_swapchain_ResizeBuffers,
363 dxgi_swapchain_ResizeTarget,
364 dxgi_swapchain_GetContainingOutput,
365 dxgi_swapchain_GetFrameStatistics,
366 dxgi_swapchain_GetLastPresentCount,
369 static void STDMETHODCALLTYPE dxgi_swapchain_wined3d_object_released(void *parent)
371 struct dxgi_swapchain *swapchain = parent;
373 wined3d_private_store_cleanup(&swapchain->private_store);
374 HeapFree(GetProcessHeap(), 0, parent);
377 static const struct wined3d_parent_ops dxgi_swapchain_wined3d_parent_ops =
379 dxgi_swapchain_wined3d_object_released,
382 HRESULT dxgi_swapchain_init(struct dxgi_swapchain *swapchain, struct dxgi_device *device,
383 struct wined3d_swapchain_desc *desc, BOOL implicit)
385 HRESULT hr;
388 * A reference to the implicit swapchain is held by the wined3d device.
389 * In order to avoid circular references we do not keep a reference
390 * to the device in the implicit swapchain.
392 if (!implicit)
394 if (FAILED(hr = IDXGIAdapter1_GetParent(device->adapter, &IID_IDXGIFactory,
395 (void **)&swapchain->factory)))
397 WARN("Failed to get adapter parent, hr %#x.\n", hr);
398 return hr;
400 swapchain->device = &device->IWineDXGIDevice_iface;
401 IWineDXGIDevice_AddRef(swapchain->device);
403 else
405 swapchain->device = NULL;
406 swapchain->factory = NULL;
409 swapchain->IDXGISwapChain_iface.lpVtbl = &dxgi_swapchain_vtbl;
410 swapchain->refcount = 1;
411 wined3d_mutex_lock();
412 wined3d_private_store_init(&swapchain->private_store);
414 if (FAILED(hr = wined3d_swapchain_create(device->wined3d_device, desc, swapchain,
415 &dxgi_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain)))
417 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
418 wined3d_private_store_cleanup(&swapchain->private_store);
419 wined3d_mutex_unlock();
420 if (swapchain->factory)
421 IDXGIFactory_Release(swapchain->factory);
422 if (swapchain->device)
423 IWineDXGIDevice_Release(swapchain->device);
424 return hr;
426 wined3d_mutex_unlock();
428 return S_OK;