ddrawex: Directly return the ddraw surface in IDirectDraw4Impl_GetSurfaceFromDC().
[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(LPDIRECT3DSWAPCHAIN9 iface, UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9** ppBackBuffer) {
110     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
111     HRESULT hrc = D3D_OK;
112     IWineD3DSurface *mySurface = NULL;
113
114     TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
115             iface, iBackBuffer, Type, ppBackBuffer);
116
117     wined3d_mutex_lock();
118     hrc = IWineD3DSwapChain_GetBackBuffer(This->wineD3DSwapChain, iBackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &mySurface);
119     if (hrc == D3D_OK && NULL != mySurface) {
120        IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppBackBuffer);
121        IWineD3DSurface_Release(mySurface);
122     }
123     wined3d_mutex_unlock();
124
125     /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
126     return hrc;
127 }
128
129 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
130     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
131     HRESULT hr;
132
133     TRACE("iface %p, raster_status %p.\n", iface, pRasterStatus);
134
135     wined3d_mutex_lock();
136     hr = IWineD3DSwapChain_GetRasterStatus(This->wineD3DSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
137     wined3d_mutex_unlock();
138
139     return hr;
140 }
141
142 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
143     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
144     HRESULT hr;
145
146     TRACE("iface %p, mode %p.\n", iface, pMode);
147
148     wined3d_mutex_lock();
149     hr = IWineD3DSwapChain_GetDisplayMode(This->wineD3DSwapChain, (WINED3DDISPLAYMODE *) pMode);
150     wined3d_mutex_unlock();
151
152     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
153
154     return hr;
155 }
156
157 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(IDirect3DSwapChain9 *iface, IDirect3DDevice9 **device)
158 {
159     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
160
161     TRACE("iface %p, device %p.\n", iface, device);
162
163     *device = (IDirect3DDevice9 *)This->parentDevice;
164     IDirect3DDevice9_AddRef(*device);
165
166     TRACE("Returning device %p.\n", *device);
167
168     return D3D_OK;
169 }
170
171 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
172     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
173     WINED3DPRESENT_PARAMETERS winePresentParameters;
174     HRESULT hr;
175
176     TRACE("iface %p, parameters %p.\n", iface, pPresentationParameters);
177
178     wined3d_mutex_lock();
179     hr = IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
180     wined3d_mutex_unlock();
181
182     pPresentationParameters->BackBufferWidth            = winePresentParameters.BackBufferWidth;
183     pPresentationParameters->BackBufferHeight           = winePresentParameters.BackBufferHeight;
184     pPresentationParameters->BackBufferFormat           = d3dformat_from_wined3dformat(winePresentParameters.BackBufferFormat);
185     pPresentationParameters->BackBufferCount            = winePresentParameters.BackBufferCount;
186     pPresentationParameters->MultiSampleType            = winePresentParameters.MultiSampleType;
187     pPresentationParameters->MultiSampleQuality         = winePresentParameters.MultiSampleQuality;
188     pPresentationParameters->SwapEffect                 = winePresentParameters.SwapEffect;
189     pPresentationParameters->hDeviceWindow              = winePresentParameters.hDeviceWindow;
190     pPresentationParameters->Windowed                   = winePresentParameters.Windowed;
191     pPresentationParameters->EnableAutoDepthStencil     = winePresentParameters.EnableAutoDepthStencil;
192     pPresentationParameters->AutoDepthStencilFormat     = d3dformat_from_wined3dformat(winePresentParameters.AutoDepthStencilFormat);
193     pPresentationParameters->Flags                      = winePresentParameters.Flags;
194     pPresentationParameters->FullScreen_RefreshRateInHz = winePresentParameters.FullScreen_RefreshRateInHz;
195     pPresentationParameters->PresentationInterval       = winePresentParameters.PresentationInterval;
196
197     return hr;
198 }
199
200
201 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
202 {
203     IDirect3DSwapChain9Impl_QueryInterface,
204     IDirect3DSwapChain9Impl_AddRef,
205     IDirect3DSwapChain9Impl_Release,
206     IDirect3DSwapChain9Impl_Present,
207     IDirect3DSwapChain9Impl_GetFrontBufferData,
208     IDirect3DSwapChain9Impl_GetBackBuffer,
209     IDirect3DSwapChain9Impl_GetRasterStatus,
210     IDirect3DSwapChain9Impl_GetDisplayMode,
211     IDirect3DSwapChain9Impl_GetDevice,
212     IDirect3DSwapChain9Impl_GetPresentParameters
213 };
214
215 HRESULT swapchain_init(IDirect3DSwapChain9Impl *swapchain, IDirect3DDevice9Impl *device,
216         D3DPRESENT_PARAMETERS *present_parameters)
217 {
218     WINED3DPRESENT_PARAMETERS wined3d_parameters;
219     HRESULT hr;
220
221     swapchain->ref = 1;
222     swapchain->lpVtbl = &Direct3DSwapChain9_Vtbl;
223
224     wined3d_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
225     wined3d_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
226     wined3d_parameters.BackBufferFormat = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
227     wined3d_parameters.BackBufferCount = max(1, present_parameters->BackBufferCount);
228     wined3d_parameters.MultiSampleType = present_parameters->MultiSampleType;
229     wined3d_parameters.MultiSampleQuality = present_parameters->MultiSampleQuality;
230     wined3d_parameters.SwapEffect = present_parameters->SwapEffect;
231     wined3d_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
232     wined3d_parameters.Windowed = present_parameters->Windowed;
233     wined3d_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
234     wined3d_parameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
235     wined3d_parameters.Flags = present_parameters->Flags;
236     wined3d_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
237     wined3d_parameters.PresentationInterval = present_parameters->PresentationInterval;
238     wined3d_parameters.AutoRestoreDisplayMode = TRUE;
239
240     wined3d_mutex_lock();
241     hr = IWineD3DDevice_CreateSwapChain(device->WineD3DDevice, &wined3d_parameters,
242             &swapchain->wineD3DSwapChain, (IUnknown *)swapchain, SURFACE_OPENGL);
243     wined3d_mutex_unlock();
244
245     present_parameters->BackBufferWidth = wined3d_parameters.BackBufferWidth;
246     present_parameters->BackBufferHeight = wined3d_parameters.BackBufferHeight;
247     present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters.BackBufferFormat);
248     present_parameters->BackBufferCount = wined3d_parameters.BackBufferCount;
249     present_parameters->MultiSampleType = wined3d_parameters.MultiSampleType;
250     present_parameters->MultiSampleQuality = wined3d_parameters.MultiSampleQuality;
251     present_parameters->SwapEffect = wined3d_parameters.SwapEffect;
252     present_parameters->hDeviceWindow = wined3d_parameters.hDeviceWindow;
253     present_parameters->Windowed = wined3d_parameters.Windowed;
254     present_parameters->EnableAutoDepthStencil = wined3d_parameters.EnableAutoDepthStencil;
255     present_parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(wined3d_parameters.AutoDepthStencilFormat);
256     present_parameters->Flags = wined3d_parameters.Flags;
257     present_parameters->FullScreen_RefreshRateInHz = wined3d_parameters.FullScreen_RefreshRateInHz;
258     present_parameters->PresentationInterval = wined3d_parameters.PresentationInterval;
259
260     if (FAILED(hr))
261     {
262         WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
263         return hr;
264     }
265
266     swapchain->parentDevice = (IDirect3DDevice9Ex *)device;
267     IDirect3DDevice9Ex_AddRef(swapchain->parentDevice);
268
269     return D3D_OK;
270 }
271
272 HRESULT  WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_GetSwapChain(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, IDirect3DSwapChain9** pSwapChain) {
273     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
274     HRESULT hrc = D3D_OK;
275     IWineD3DSwapChain *swapchain = NULL;
276
277     TRACE("iface %p, swapchain_idx %u, swapchain %p.\n",
278             iface, iSwapChain, pSwapChain);
279
280     wined3d_mutex_lock();
281     hrc = IWineD3DDevice_GetSwapChain(This->WineD3DDevice, iSwapChain, &swapchain);
282     if (hrc == D3D_OK && NULL != swapchain) {
283        IWineD3DSwapChain_GetParent(swapchain, (IUnknown **)pSwapChain);
284        IWineD3DSwapChain_Release(swapchain);
285     } else {
286         *pSwapChain = NULL;
287     }
288     wined3d_mutex_unlock();
289
290     return hrc;
291 }
292
293 UINT     WINAPI  IDirect3DDevice9Impl_GetNumberOfSwapChains(LPDIRECT3DDEVICE9EX iface) {
294     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
295     UINT ret;
296
297     TRACE("iface %p.\n", iface);
298
299     wined3d_mutex_lock();
300     ret = IWineD3DDevice_GetNumberOfSwapChains(This->WineD3DDevice);
301     wined3d_mutex_unlock();
302
303     return ret;
304 }