ws2_32/tests: Add regression tests for WSARecvMsg and IP_PKTINFO.
[wine] / dlls / d3d9 / swapchain.c
1 /*
2  * IDirect3DSwapChain9 implementation
3  *
4  * Copyright 2002-2003 Jason Edmeades
5  *                     Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 /* IDirect3DSwapChain IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DSwapChain9Impl_QueryInterface(LPDIRECT3DSWAPCHAIN9 iface, REFIID riid, LPVOID* ppobj)
30 {
31     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
32
33     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
34
35     if (IsEqualGUID(riid, &IID_IUnknown)
36         || IsEqualGUID(riid, &IID_IDirect3DSwapChain9)) {
37         IDirect3DSwapChain9_AddRef(iface);
38         *ppobj = This;
39         return S_OK;
40     }
41
42     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
43     *ppobj = NULL;
44     return E_NOINTERFACE;
45 }
46
47 static ULONG WINAPI IDirect3DSwapChain9Impl_AddRef(LPDIRECT3DSWAPCHAIN9 iface) {
48     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
49     ULONG ref = InterlockedIncrement(&This->ref);
50
51     TRACE("%p increasing refcount to %u.\n", iface, ref);
52
53     if(ref == 1 && This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
54
55     return ref;
56 }
57
58 static ULONG WINAPI IDirect3DSwapChain9Impl_Release(LPDIRECT3DSWAPCHAIN9 iface) {
59     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
60     ULONG ref = InterlockedDecrement(&This->ref);
61
62     TRACE("%p decreasing refcount to %u.\n", iface, ref);
63
64     if (ref == 0) {
65         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
66
67         if (!This->isImplicit) {
68             wined3d_mutex_lock();
69             IWineD3DSwapChain_Destroy(This->wineD3DSwapChain);
70             wined3d_mutex_unlock();
71
72             HeapFree(GetProcessHeap(), 0, This);
73         }
74
75         /* Release the device last, as it may cause the device to be destroyed. */
76         if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
77     }
78     return ref;
79 }
80
81 /* IDirect3DSwapChain9 parts follow: */
82 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DSwapChain9Impl_Present(LPDIRECT3DSWAPCHAIN9 iface, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags) {
83     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
84     HRESULT hr;
85
86     TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x.\n",
87             iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
88
89     wined3d_mutex_lock();
90     hr = IWineD3DSwapChain_Present(This->wineD3DSwapChain, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
91     wined3d_mutex_unlock();
92
93     return hr;
94 }
95
96 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetFrontBufferData(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DSurface9* pDestSurface) {
97     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
98     HRESULT hr;
99
100     TRACE("iface %p, surface %p.\n", iface, pDestSurface);
101
102     wined3d_mutex_lock();
103     hr = IWineD3DSwapChain_GetFrontBufferData(This->wineD3DSwapChain,  ((IDirect3DSurface9Impl *)pDestSurface)->wineD3DSurface);
104     wined3d_mutex_unlock();
105
106     return hr;
107 }
108
109 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetBackBuffer(IDirect3DSwapChain9 *iface,
110         UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 **ppBackBuffer)
111 {
112     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
113     IWineD3DSurface *mySurface = NULL;
114     HRESULT hr;
115
116     TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
117             iface, iBackBuffer, Type, ppBackBuffer);
118
119     wined3d_mutex_lock();
120     hr = IWineD3DSwapChain_GetBackBuffer(This->wineD3DSwapChain, iBackBuffer,
121             (WINED3DBACKBUFFER_TYPE)Type, &mySurface);
122     if (SUCCEEDED(hr) && mySurface)
123     {
124        *ppBackBuffer = IWineD3DSurface_GetParent(mySurface);
125        IDirect3DSurface9_AddRef(*ppBackBuffer);
126        IWineD3DSurface_Release(mySurface);
127     }
128     wined3d_mutex_unlock();
129
130     /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
131     return hr;
132 }
133
134 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
135     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
136     HRESULT hr;
137
138     TRACE("iface %p, raster_status %p.\n", iface, pRasterStatus);
139
140     wined3d_mutex_lock();
141     hr = IWineD3DSwapChain_GetRasterStatus(This->wineD3DSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
142     wined3d_mutex_unlock();
143
144     return hr;
145 }
146
147 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
148     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
149     HRESULT hr;
150
151     TRACE("iface %p, mode %p.\n", iface, pMode);
152
153     wined3d_mutex_lock();
154     hr = IWineD3DSwapChain_GetDisplayMode(This->wineD3DSwapChain, (WINED3DDISPLAYMODE *) pMode);
155     wined3d_mutex_unlock();
156
157     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
158
159     return hr;
160 }
161
162 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(IDirect3DSwapChain9 *iface, IDirect3DDevice9 **device)
163 {
164     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
165
166     TRACE("iface %p, device %p.\n", iface, device);
167
168     *device = (IDirect3DDevice9 *)This->parentDevice;
169     IDirect3DDevice9_AddRef(*device);
170
171     TRACE("Returning device %p.\n", *device);
172
173     return D3D_OK;
174 }
175
176 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
177     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
178     WINED3DPRESENT_PARAMETERS winePresentParameters;
179     HRESULT hr;
180
181     TRACE("iface %p, parameters %p.\n", iface, pPresentationParameters);
182
183     wined3d_mutex_lock();
184     hr = IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
185     wined3d_mutex_unlock();
186
187     pPresentationParameters->BackBufferWidth            = winePresentParameters.BackBufferWidth;
188     pPresentationParameters->BackBufferHeight           = winePresentParameters.BackBufferHeight;
189     pPresentationParameters->BackBufferFormat           = d3dformat_from_wined3dformat(winePresentParameters.BackBufferFormat);
190     pPresentationParameters->BackBufferCount            = winePresentParameters.BackBufferCount;
191     pPresentationParameters->MultiSampleType            = winePresentParameters.MultiSampleType;
192     pPresentationParameters->MultiSampleQuality         = winePresentParameters.MultiSampleQuality;
193     pPresentationParameters->SwapEffect                 = winePresentParameters.SwapEffect;
194     pPresentationParameters->hDeviceWindow              = winePresentParameters.hDeviceWindow;
195     pPresentationParameters->Windowed                   = winePresentParameters.Windowed;
196     pPresentationParameters->EnableAutoDepthStencil     = winePresentParameters.EnableAutoDepthStencil;
197     pPresentationParameters->AutoDepthStencilFormat     = d3dformat_from_wined3dformat(winePresentParameters.AutoDepthStencilFormat);
198     pPresentationParameters->Flags                      = winePresentParameters.Flags;
199     pPresentationParameters->FullScreen_RefreshRateInHz = winePresentParameters.FullScreen_RefreshRateInHz;
200     pPresentationParameters->PresentationInterval       = winePresentParameters.PresentationInterval;
201
202     return hr;
203 }
204
205
206 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
207 {
208     IDirect3DSwapChain9Impl_QueryInterface,
209     IDirect3DSwapChain9Impl_AddRef,
210     IDirect3DSwapChain9Impl_Release,
211     IDirect3DSwapChain9Impl_Present,
212     IDirect3DSwapChain9Impl_GetFrontBufferData,
213     IDirect3DSwapChain9Impl_GetBackBuffer,
214     IDirect3DSwapChain9Impl_GetRasterStatus,
215     IDirect3DSwapChain9Impl_GetDisplayMode,
216     IDirect3DSwapChain9Impl_GetDevice,
217     IDirect3DSwapChain9Impl_GetPresentParameters
218 };
219
220 HRESULT swapchain_init(IDirect3DSwapChain9Impl *swapchain, IDirect3DDevice9Impl *device,
221         D3DPRESENT_PARAMETERS *present_parameters)
222 {
223     WINED3DPRESENT_PARAMETERS wined3d_parameters;
224     HRESULT hr;
225
226     swapchain->ref = 1;
227     swapchain->lpVtbl = &Direct3DSwapChain9_Vtbl;
228
229     wined3d_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
230     wined3d_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
231     wined3d_parameters.BackBufferFormat = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
232     wined3d_parameters.BackBufferCount = max(1, present_parameters->BackBufferCount);
233     wined3d_parameters.MultiSampleType = present_parameters->MultiSampleType;
234     wined3d_parameters.MultiSampleQuality = present_parameters->MultiSampleQuality;
235     wined3d_parameters.SwapEffect = present_parameters->SwapEffect;
236     wined3d_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
237     wined3d_parameters.Windowed = present_parameters->Windowed;
238     wined3d_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
239     wined3d_parameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
240     wined3d_parameters.Flags = present_parameters->Flags;
241     wined3d_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
242     wined3d_parameters.PresentationInterval = present_parameters->PresentationInterval;
243     wined3d_parameters.AutoRestoreDisplayMode = TRUE;
244
245     wined3d_mutex_lock();
246     hr = IWineD3DDevice_CreateSwapChain(device->WineD3DDevice, &wined3d_parameters,
247             SURFACE_OPENGL, swapchain, &swapchain->wineD3DSwapChain);
248     wined3d_mutex_unlock();
249
250     present_parameters->BackBufferWidth = wined3d_parameters.BackBufferWidth;
251     present_parameters->BackBufferHeight = wined3d_parameters.BackBufferHeight;
252     present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters.BackBufferFormat);
253     present_parameters->BackBufferCount = wined3d_parameters.BackBufferCount;
254     present_parameters->MultiSampleType = wined3d_parameters.MultiSampleType;
255     present_parameters->MultiSampleQuality = wined3d_parameters.MultiSampleQuality;
256     present_parameters->SwapEffect = wined3d_parameters.SwapEffect;
257     present_parameters->hDeviceWindow = wined3d_parameters.hDeviceWindow;
258     present_parameters->Windowed = wined3d_parameters.Windowed;
259     present_parameters->EnableAutoDepthStencil = wined3d_parameters.EnableAutoDepthStencil;
260     present_parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(wined3d_parameters.AutoDepthStencilFormat);
261     present_parameters->Flags = wined3d_parameters.Flags;
262     present_parameters->FullScreen_RefreshRateInHz = wined3d_parameters.FullScreen_RefreshRateInHz;
263     present_parameters->PresentationInterval = wined3d_parameters.PresentationInterval;
264
265     if (FAILED(hr))
266     {
267         WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
268         return hr;
269     }
270
271     swapchain->parentDevice = (IDirect3DDevice9Ex *)device;
272     IDirect3DDevice9Ex_AddRef(swapchain->parentDevice);
273
274     return D3D_OK;
275 }