dxgi: Replace dxgi_cs with wined3d_mutex.
[wine/multimedia.git] / dlls / dxgi / swapchain.c
blob53d2b7f1a4516061bc393aff0b00b7d04dd81141
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)
62 wined3d_swapchain_incref(This->wined3d_swapchain);
64 return refcount;
67 static ULONG STDMETHODCALLTYPE dxgi_swapchain_Release(IDXGISwapChain *iface)
69 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
70 ULONG refcount = InterlockedDecrement(&This->refcount);
72 TRACE("%p decreasing refcount to %u\n", This, refcount);
74 if (!refcount)
75 wined3d_swapchain_decref(This->wined3d_swapchain);
77 return refcount;
80 /* IDXGIObject methods */
82 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_SetPrivateData(IDXGISwapChain *iface,
83 REFGUID guid, UINT data_size, const void *data)
85 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
87 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
89 return dxgi_set_private_data(&swapchain->private_store, guid, data_size, data);
92 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_SetPrivateDataInterface(IDXGISwapChain *iface,
93 REFGUID guid, const IUnknown *object)
95 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
97 TRACE("iface %p, guid %s, object %p.\n", iface, debugstr_guid(guid), object);
99 return dxgi_set_private_data_interface(&swapchain->private_store, guid, object);
102 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetPrivateData(IDXGISwapChain *iface,
103 REFGUID guid, UINT *data_size, void *data)
105 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
107 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
109 return dxgi_get_private_data(&swapchain->private_store, guid, data_size, data);
112 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetParent(IDXGISwapChain *iface, REFIID riid, void **parent)
114 FIXME("iface %p, riid %s, parent %p stub!\n", iface, debugstr_guid(riid), parent);
116 return E_NOTIMPL;
119 /* IDXGIDeviceSubObject methods */
121 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetDevice(IDXGISwapChain *iface, REFIID riid, void **device)
123 FIXME("iface %p, riid %s, device %p stub!\n", iface, debugstr_guid(riid), device);
125 return E_NOTIMPL;
128 /* IDXGISwapChain methods */
130 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_Present(IDXGISwapChain *iface, UINT sync_interval, UINT flags)
132 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
134 TRACE("iface %p, sync_interval %u, flags %#x\n", iface, sync_interval, flags);
136 if (sync_interval) FIXME("Unimplemented sync interval %u\n", sync_interval);
137 if (flags) FIXME("Unimplemented flags %#x\n", flags);
139 return wined3d_swapchain_present(This->wined3d_swapchain, NULL, NULL, NULL, NULL, 0);
142 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetBuffer(IDXGISwapChain *iface,
143 UINT buffer_idx, REFIID riid, void **surface)
145 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
146 struct wined3d_texture *texture;
147 IUnknown *parent;
148 HRESULT hr;
150 TRACE("iface %p, buffer_idx %u, riid %s, surface %p\n",
151 iface, buffer_idx, debugstr_guid(riid), surface);
153 wined3d_mutex_lock();
155 if (!(texture = wined3d_swapchain_get_back_buffer(This->wined3d_swapchain, buffer_idx)))
157 wined3d_mutex_unlock();
158 return DXGI_ERROR_INVALID_CALL;
161 parent = wined3d_texture_get_parent(texture);
162 hr = IUnknown_QueryInterface(parent, riid, surface);
163 wined3d_mutex_unlock();
165 return hr;
168 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_SetFullscreenState(IDXGISwapChain *iface,
169 BOOL fullscreen, IDXGIOutput *target)
171 FIXME("iface %p, fullscreen %u, target %p stub!\n", iface, fullscreen, target);
173 return E_NOTIMPL;
176 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetFullscreenState(IDXGISwapChain *iface,
177 BOOL *fullscreen, IDXGIOutput **target)
179 FIXME("iface %p, fullscreen %p, target %p stub!\n", iface, fullscreen, target);
181 return E_NOTIMPL;
184 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetDesc(IDXGISwapChain *iface, DXGI_SWAP_CHAIN_DESC *desc)
186 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
187 struct wined3d_swapchain_desc wined3d_desc;
189 FIXME("iface %p, desc %p partial stub!\n", iface, desc);
191 if (desc == NULL)
192 return E_INVALIDARG;
194 wined3d_mutex_lock();
195 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
196 wined3d_mutex_unlock();
198 FIXME("Ignoring ScanlineOrdering, Scaling, SwapEffect and Flags\n");
200 desc->BufferDesc.Width = wined3d_desc.backbuffer_width;
201 desc->BufferDesc.Height = wined3d_desc.backbuffer_height;
202 desc->BufferDesc.RefreshRate.Numerator = wined3d_desc.refresh_rate;
203 desc->BufferDesc.RefreshRate.Denominator = 1;
204 desc->BufferDesc.Format = dxgi_format_from_wined3dformat(wined3d_desc.backbuffer_format);
205 desc->BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
206 desc->BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
207 dxgi_sample_desc_from_wined3d(&desc->SampleDesc, wined3d_desc.multisample_type, wined3d_desc.multisample_quality);
208 desc->BufferCount = wined3d_desc.backbuffer_count;
209 desc->OutputWindow = wined3d_desc.device_window;
210 desc->Windowed = wined3d_desc.windowed;
211 desc->SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
212 desc->Flags = 0;
214 return S_OK;
217 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_ResizeBuffers(IDXGISwapChain *iface,
218 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags)
220 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
221 struct wined3d_swapchain_desc wined3d_desc;
222 struct wined3d_texture *texture;
223 IUnknown *parent;
224 unsigned int i;
225 HRESULT hr;
227 TRACE("iface %p, buffer_count %u, width %u, height %u, format %s, flags %#x.\n",
228 iface, buffer_count, width, height, debug_dxgi_format(format), flags);
230 if (flags)
231 FIXME("Ignoring flags %#x.\n", flags);
233 wined3d_mutex_lock();
234 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
235 for (i = 0; i < wined3d_desc.backbuffer_count; ++i)
237 texture = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain, i);
238 parent = wined3d_texture_get_parent(texture);
239 IUnknown_AddRef(parent);
240 if (IUnknown_Release(parent))
242 wined3d_mutex_unlock();
243 return DXGI_ERROR_INVALID_CALL;
246 if (format != DXGI_FORMAT_UNKNOWN)
247 wined3d_desc.backbuffer_format = wined3dformat_from_dxgi_format(format);
248 hr = wined3d_swapchain_resize_buffers(swapchain->wined3d_swapchain, buffer_count, width, height,
249 wined3d_desc.backbuffer_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality);
250 wined3d_mutex_unlock();
252 return hr;
255 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_ResizeTarget(IDXGISwapChain *iface,
256 const DXGI_MODE_DESC *target_mode_desc)
258 FIXME("iface %p, target_mode_desc %p stub!\n", iface, target_mode_desc);
260 return E_NOTIMPL;
263 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetContainingOutput(IDXGISwapChain *iface, IDXGIOutput **output)
265 FIXME("iface %p, output %p stub!\n", iface, output);
267 return E_NOTIMPL;
270 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetFrameStatistics(IDXGISwapChain *iface, DXGI_FRAME_STATISTICS *stats)
272 FIXME("iface %p, stats %p stub!\n", iface, stats);
274 return E_NOTIMPL;
277 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetLastPresentCount(IDXGISwapChain *iface, UINT *last_present_count)
279 FIXME("iface %p, last_present_count %p stub!\n", iface, last_present_count);
281 return E_NOTIMPL;
284 static const struct IDXGISwapChainVtbl dxgi_swapchain_vtbl =
286 /* IUnknown methods */
287 dxgi_swapchain_QueryInterface,
288 dxgi_swapchain_AddRef,
289 dxgi_swapchain_Release,
290 /* IDXGIObject methods */
291 dxgi_swapchain_SetPrivateData,
292 dxgi_swapchain_SetPrivateDataInterface,
293 dxgi_swapchain_GetPrivateData,
294 dxgi_swapchain_GetParent,
295 /* IDXGIDeviceSubObject methods */
296 dxgi_swapchain_GetDevice,
297 /* IDXGISwapChain methods */
298 dxgi_swapchain_Present,
299 dxgi_swapchain_GetBuffer,
300 dxgi_swapchain_SetFullscreenState,
301 dxgi_swapchain_GetFullscreenState,
302 dxgi_swapchain_GetDesc,
303 dxgi_swapchain_ResizeBuffers,
304 dxgi_swapchain_ResizeTarget,
305 dxgi_swapchain_GetContainingOutput,
306 dxgi_swapchain_GetFrameStatistics,
307 dxgi_swapchain_GetLastPresentCount,
310 static void STDMETHODCALLTYPE dxgi_swapchain_wined3d_object_released(void *parent)
312 struct dxgi_swapchain *swapchain = parent;
314 wined3d_private_store_cleanup(&swapchain->private_store);
315 HeapFree(GetProcessHeap(), 0, parent);
318 static const struct wined3d_parent_ops dxgi_swapchain_wined3d_parent_ops =
320 dxgi_swapchain_wined3d_object_released,
323 HRESULT dxgi_swapchain_init(struct dxgi_swapchain *swapchain, struct dxgi_device *device,
324 struct wined3d_swapchain_desc *desc)
326 HRESULT hr;
328 swapchain->IDXGISwapChain_iface.lpVtbl = &dxgi_swapchain_vtbl;
329 swapchain->refcount = 1;
330 wined3d_private_store_init(&swapchain->private_store);
332 if (FAILED(hr = wined3d_swapchain_create(device->wined3d_device, desc, swapchain,
333 &dxgi_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain)))
335 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
336 wined3d_private_store_cleanup(&swapchain->private_store);
337 return hr;
340 return S_OK;