Sync to trunk revision 61757.
[reactos.git] / dll / directx / wine / dxgi / swapchain.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 "dxgi_private.h"
21
22 static inline struct dxgi_swapchain *impl_from_IDXGISwapChain(IDXGISwapChain *iface)
23 {
24 return CONTAINING_RECORD(iface, struct dxgi_swapchain, IDXGISwapChain_iface);
25 }
26
27 /* IUnknown methods */
28
29 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_QueryInterface(IDXGISwapChain *iface, REFIID riid, void **object)
30 {
31 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
32
33 if (IsEqualGUID(riid, &IID_IUnknown)
34 || IsEqualGUID(riid, &IID_IDXGIObject)
35 || IsEqualGUID(riid, &IID_IDXGIDeviceSubObject)
36 || IsEqualGUID(riid, &IID_IDXGISwapChain))
37 {
38 IUnknown_AddRef(iface);
39 *object = iface;
40 return S_OK;
41 }
42
43 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
44
45 *object = NULL;
46 return E_NOINTERFACE;
47 }
48
49 static ULONG STDMETHODCALLTYPE dxgi_swapchain_AddRef(IDXGISwapChain *iface)
50 {
51 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
52 ULONG refcount = InterlockedIncrement(&This->refcount);
53
54 TRACE("%p increasing refcount to %u\n", This, refcount);
55
56 if (refcount == 1)
57 wined3d_swapchain_incref(This->wined3d_swapchain);
58
59 return refcount;
60 }
61
62 static ULONG STDMETHODCALLTYPE dxgi_swapchain_Release(IDXGISwapChain *iface)
63 {
64 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
65 ULONG refcount = InterlockedDecrement(&This->refcount);
66
67 TRACE("%p decreasing refcount to %u\n", This, refcount);
68
69 if (!refcount)
70 {
71 struct wined3d_device *wined3d_device;
72 HRESULT hr;
73
74 FIXME("Only a single swapchain is supported\n");
75
76 wined3d_device = wined3d_swapchain_get_device(This->wined3d_swapchain);
77 hr = wined3d_device_uninit_3d(wined3d_device);
78 if (FAILED(hr))
79 {
80 ERR("Uninit3D failed, hr %#x\n", hr);
81 }
82 }
83
84 return refcount;
85 }
86
87 /* IDXGIObject methods */
88
89 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_SetPrivateData(IDXGISwapChain *iface,
90 REFGUID guid, UINT data_size, const void *data)
91 {
92 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
93
94 return E_NOTIMPL;
95 }
96
97 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_SetPrivateDataInterface(IDXGISwapChain *iface,
98 REFGUID guid, const IUnknown *object)
99 {
100 FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
101
102 return E_NOTIMPL;
103 }
104
105 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetPrivateData(IDXGISwapChain *iface,
106 REFGUID guid, UINT *data_size, void *data)
107 {
108 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
109
110 return E_NOTIMPL;
111 }
112
113 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetParent(IDXGISwapChain *iface, REFIID riid, void **parent)
114 {
115 FIXME("iface %p, riid %s, parent %p stub!\n", iface, debugstr_guid(riid), parent);
116
117 return E_NOTIMPL;
118 }
119
120 /* IDXGIDeviceSubObject methods */
121
122 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetDevice(IDXGISwapChain *iface, REFIID riid, void **device)
123 {
124 FIXME("iface %p, riid %s, device %p stub!\n", iface, debugstr_guid(riid), device);
125
126 return E_NOTIMPL;
127 }
128
129 /* IDXGISwapChain methods */
130
131 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_Present(IDXGISwapChain *iface, UINT sync_interval, UINT flags)
132 {
133 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
134
135 TRACE("iface %p, sync_interval %u, flags %#x\n", iface, sync_interval, flags);
136
137 if (sync_interval) FIXME("Unimplemented sync interval %u\n", sync_interval);
138 if (flags) FIXME("Unimplemented flags %#x\n", flags);
139
140 return wined3d_swapchain_present(This->wined3d_swapchain, NULL, NULL, NULL, NULL, 0);
141 }
142
143 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetBuffer(IDXGISwapChain *iface,
144 UINT buffer_idx, REFIID riid, void **surface)
145 {
146 struct dxgi_swapchain *This = impl_from_IDXGISwapChain(iface);
147 struct wined3d_surface *backbuffer;
148 IUnknown *parent;
149 HRESULT hr;
150
151 TRACE("iface %p, buffer_idx %u, riid %s, surface %p\n",
152 iface, buffer_idx, debugstr_guid(riid), surface);
153
154 EnterCriticalSection(&dxgi_cs);
155
156 if (!(backbuffer = wined3d_swapchain_get_back_buffer(This->wined3d_swapchain,
157 buffer_idx, WINED3D_BACKBUFFER_TYPE_MONO)))
158 {
159 LeaveCriticalSection(&dxgi_cs);
160 return DXGI_ERROR_INVALID_CALL;
161 }
162
163 parent = wined3d_surface_get_parent(backbuffer);
164 hr = IUnknown_QueryInterface(parent, riid, surface);
165 LeaveCriticalSection(&dxgi_cs);
166
167 return hr;
168 }
169
170 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_SetFullscreenState(IDXGISwapChain *iface,
171 BOOL fullscreen, IDXGIOutput *target)
172 {
173 FIXME("iface %p, fullscreen %u, target %p stub!\n", iface, fullscreen, target);
174
175 return E_NOTIMPL;
176 }
177
178 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetFullscreenState(IDXGISwapChain *iface,
179 BOOL *fullscreen, IDXGIOutput **target)
180 {
181 FIXME("iface %p, fullscreen %p, target %p stub!\n", iface, fullscreen, target);
182
183 return E_NOTIMPL;
184 }
185
186 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetDesc(IDXGISwapChain *iface, DXGI_SWAP_CHAIN_DESC *desc)
187 {
188 struct dxgi_swapchain *swapchain = impl_from_IDXGISwapChain(iface);
189 struct wined3d_swapchain_desc wined3d_desc;
190
191 FIXME("iface %p, desc %p partial stub!\n", iface, desc);
192
193 if (desc == NULL)
194 return E_INVALIDARG;
195
196 EnterCriticalSection(&dxgi_cs);
197 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &wined3d_desc);
198 LeaveCriticalSection(&dxgi_cs);
199
200 FIXME("Ignoring ScanlineOrdering, Scaling, SwapEffect and Flags\n");
201
202 desc->BufferDesc.Width = wined3d_desc.backbuffer_width;
203 desc->BufferDesc.Height = wined3d_desc.backbuffer_height;
204 desc->BufferDesc.RefreshRate.Numerator = wined3d_desc.refresh_rate;
205 desc->BufferDesc.RefreshRate.Denominator = 1;
206 desc->BufferDesc.Format = dxgi_format_from_wined3dformat(wined3d_desc.backbuffer_format);
207 desc->BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
208 desc->BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
209 desc->SampleDesc.Count = wined3d_desc.multisample_type;
210 desc->SampleDesc.Quality = wined3d_desc.multisample_quality;
211 desc->BufferCount = wined3d_desc.backbuffer_count;
212 desc->OutputWindow = wined3d_desc.device_window;
213 desc->Windowed = wined3d_desc.windowed;
214 desc->SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
215 desc->Flags = 0;
216
217 return S_OK;
218 }
219
220 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_ResizeBuffers(IDXGISwapChain *iface,
221 UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags)
222 {
223 FIXME("iface %p, buffer_count %u, width %u, height %u, format %s, flags %#x stub!\n",
224 iface, buffer_count, width, height, debug_dxgi_format(format), flags);
225
226 return E_NOTIMPL;
227 }
228
229 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_ResizeTarget(IDXGISwapChain *iface,
230 const DXGI_MODE_DESC *target_mode_desc)
231 {
232 FIXME("iface %p, target_mode_desc %p stub!\n", iface, target_mode_desc);
233
234 return E_NOTIMPL;
235 }
236
237 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetContainingOutput(IDXGISwapChain *iface, IDXGIOutput **output)
238 {
239 FIXME("iface %p, output %p stub!\n", iface, output);
240
241 return E_NOTIMPL;
242 }
243
244 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetFrameStatistics(IDXGISwapChain *iface, DXGI_FRAME_STATISTICS *stats)
245 {
246 FIXME("iface %p, stats %p stub!\n", iface, stats);
247
248 return E_NOTIMPL;
249 }
250
251 static HRESULT STDMETHODCALLTYPE dxgi_swapchain_GetLastPresentCount(IDXGISwapChain *iface, UINT *last_present_count)
252 {
253 FIXME("iface %p, last_present_count %p stub!\n", iface, last_present_count);
254
255 return E_NOTIMPL;
256 }
257
258 static const struct IDXGISwapChainVtbl dxgi_swapchain_vtbl =
259 {
260 /* IUnknown methods */
261 dxgi_swapchain_QueryInterface,
262 dxgi_swapchain_AddRef,
263 dxgi_swapchain_Release,
264 /* IDXGIObject methods */
265 dxgi_swapchain_SetPrivateData,
266 dxgi_swapchain_SetPrivateDataInterface,
267 dxgi_swapchain_GetPrivateData,
268 dxgi_swapchain_GetParent,
269 /* IDXGIDeviceSubObject methods */
270 dxgi_swapchain_GetDevice,
271 /* IDXGISwapChain methods */
272 dxgi_swapchain_Present,
273 dxgi_swapchain_GetBuffer,
274 dxgi_swapchain_SetFullscreenState,
275 dxgi_swapchain_GetFullscreenState,
276 dxgi_swapchain_GetDesc,
277 dxgi_swapchain_ResizeBuffers,
278 dxgi_swapchain_ResizeTarget,
279 dxgi_swapchain_GetContainingOutput,
280 dxgi_swapchain_GetFrameStatistics,
281 dxgi_swapchain_GetLastPresentCount,
282 };
283
284 static void STDMETHODCALLTYPE dxgi_swapchain_wined3d_object_released(void *parent)
285 {
286 HeapFree(GetProcessHeap(), 0, parent);
287 }
288
289 static const struct wined3d_parent_ops dxgi_swapchain_wined3d_parent_ops =
290 {
291 dxgi_swapchain_wined3d_object_released,
292 };
293
294 HRESULT dxgi_swapchain_init(struct dxgi_swapchain *swapchain, struct dxgi_device *device,
295 struct wined3d_swapchain_desc *desc)
296 {
297 HRESULT hr;
298
299 swapchain->IDXGISwapChain_iface.lpVtbl = &dxgi_swapchain_vtbl;
300 swapchain->refcount = 1;
301
302 if (FAILED(hr = wined3d_swapchain_create(device->wined3d_device, desc, swapchain,
303 &dxgi_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain)))
304 {
305 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
306 return hr;
307 }
308
309 return S_OK;
310 }