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