d3d9: Don't release the parent device before destroying its children.
[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     if (IsEqualGUID(riid, &IID_IUnknown)
34         || IsEqualGUID(riid, &IID_IDirect3DSwapChain9)) {
35         IDirect3DSwapChain9_AddRef(iface);
36         *ppobj = This;
37         return S_OK;
38     }
39
40     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41     *ppobj = NULL;
42     return E_NOINTERFACE;
43 }
44
45 static ULONG WINAPI IDirect3DSwapChain9Impl_AddRef(LPDIRECT3DSWAPCHAIN9 iface) {
46     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
47     ULONG ref = InterlockedIncrement(&This->ref);
48
49     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
50
51     if(ref == 1 && This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
52
53     return ref;
54 }
55
56 static ULONG WINAPI IDirect3DSwapChain9Impl_Release(LPDIRECT3DSWAPCHAIN9 iface) {
57     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
58     ULONG ref = InterlockedDecrement(&This->ref);
59
60     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
61
62     if (ref == 0) {
63         IDirect3DDevice9Ex *parentDevice = This->parentDevice;
64
65         if (!This->isImplicit) {
66             wined3d_mutex_lock();
67             IWineD3DSwapChain_Destroy(This->wineD3DSwapChain);
68             wined3d_mutex_unlock();
69
70             HeapFree(GetProcessHeap(), 0, This);
71         }
72
73         /* Release the device last, as it may cause the device to be destroyed. */
74         if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
75     }
76     return ref;
77 }
78
79 /* IDirect3DSwapChain9 parts follow: */
80 static HRESULT WINAPI IDirect3DSwapChain9Impl_Present(LPDIRECT3DSWAPCHAIN9 iface, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags) {
81     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
82     HRESULT hr;
83
84     TRACE("(%p) Relay\n", This);
85
86     wined3d_mutex_lock();
87     hr = IWineD3DSwapChain_Present(This->wineD3DSwapChain, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
88     wined3d_mutex_unlock();
89
90     return hr;
91 }
92
93 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetFrontBufferData(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DSurface9* pDestSurface) {
94     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
95     HRESULT hr;
96     TRACE("(%p) Relay\n", This);
97
98     wined3d_mutex_lock();
99     hr = IWineD3DSwapChain_GetFrontBufferData(This->wineD3DSwapChain,  ((IDirect3DSurface9Impl *)pDestSurface)->wineD3DSurface);
100     wined3d_mutex_unlock();
101
102     return hr;
103 }
104
105 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetBackBuffer(LPDIRECT3DSWAPCHAIN9 iface, UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9** ppBackBuffer) {
106     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
107     HRESULT hrc = D3D_OK;
108     IWineD3DSurface *mySurface = NULL;
109
110     TRACE("(%p) Relay\n", This);
111
112     wined3d_mutex_lock();
113     hrc = IWineD3DSwapChain_GetBackBuffer(This->wineD3DSwapChain, iBackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &mySurface);
114     if (hrc == D3D_OK && NULL != mySurface) {
115        IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppBackBuffer);
116        IWineD3DSurface_Release(mySurface);
117     }
118     wined3d_mutex_unlock();
119
120     /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
121     return hrc;
122 }
123
124 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
125     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
126     HRESULT hr;
127     TRACE("(%p) Relay\n", This);
128
129     wined3d_mutex_lock();
130     hr = IWineD3DSwapChain_GetRasterStatus(This->wineD3DSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
131     wined3d_mutex_unlock();
132
133     return hr;
134 }
135
136 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
137     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
138     HRESULT hr;
139     TRACE("(%p) Relay\n", This);
140
141     wined3d_mutex_lock();
142     hr = IWineD3DSwapChain_GetDisplayMode(This->wineD3DSwapChain, (WINED3DDISPLAYMODE *) pMode);
143     wined3d_mutex_unlock();
144
145     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
146
147     return hr;
148 }
149
150 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DDevice9** ppDevice) {
151     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
152     HRESULT hrc = D3D_OK;
153     IWineD3DDevice *device = NULL;
154
155     TRACE("(%p) Relay\n", This);
156
157     wined3d_mutex_lock();
158     hrc = IWineD3DSwapChain_GetDevice(This->wineD3DSwapChain, &device);
159     if (hrc == D3D_OK && NULL != device) {
160        IWineD3DDevice_GetParent(device, (IUnknown **)ppDevice);
161        IWineD3DDevice_Release(device);
162     }
163     wined3d_mutex_unlock();
164
165     return hrc;
166 }
167
168 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
169     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
170     WINED3DPRESENT_PARAMETERS winePresentParameters;
171     HRESULT hr;
172
173     TRACE("(%p)->(%p): Relay\n", This, pPresentationParameters);
174
175     wined3d_mutex_lock();
176     hr = IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
177     wined3d_mutex_unlock();
178
179     pPresentationParameters->BackBufferWidth            = winePresentParameters.BackBufferWidth;
180     pPresentationParameters->BackBufferHeight           = winePresentParameters.BackBufferHeight;
181     pPresentationParameters->BackBufferFormat           = d3dformat_from_wined3dformat(winePresentParameters.BackBufferFormat);
182     pPresentationParameters->BackBufferCount            = winePresentParameters.BackBufferCount;
183     pPresentationParameters->MultiSampleType            = winePresentParameters.MultiSampleType;
184     pPresentationParameters->MultiSampleQuality         = winePresentParameters.MultiSampleQuality;
185     pPresentationParameters->SwapEffect                 = winePresentParameters.SwapEffect;
186     pPresentationParameters->hDeviceWindow              = winePresentParameters.hDeviceWindow;
187     pPresentationParameters->Windowed                   = winePresentParameters.Windowed;
188     pPresentationParameters->EnableAutoDepthStencil     = winePresentParameters.EnableAutoDepthStencil;
189     pPresentationParameters->AutoDepthStencilFormat     = d3dformat_from_wined3dformat(winePresentParameters.AutoDepthStencilFormat);
190     pPresentationParameters->Flags                      = winePresentParameters.Flags;
191     pPresentationParameters->FullScreen_RefreshRateInHz = winePresentParameters.FullScreen_RefreshRateInHz;
192     pPresentationParameters->PresentationInterval       = winePresentParameters.PresentationInterval;
193
194     return hr;
195 }
196
197
198 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
199 {
200     IDirect3DSwapChain9Impl_QueryInterface,
201     IDirect3DSwapChain9Impl_AddRef,
202     IDirect3DSwapChain9Impl_Release,
203     IDirect3DSwapChain9Impl_Present,
204     IDirect3DSwapChain9Impl_GetFrontBufferData,
205     IDirect3DSwapChain9Impl_GetBackBuffer,
206     IDirect3DSwapChain9Impl_GetRasterStatus,
207     IDirect3DSwapChain9Impl_GetDisplayMode,
208     IDirect3DSwapChain9Impl_GetDevice,
209     IDirect3DSwapChain9Impl_GetPresentParameters
210 };
211
212
213 /* IDirect3DDevice9 IDirect3DSwapChain9 Methods follow: */
214 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE9EX iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain9** pSwapChain) {
215     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
216     IDirect3DSwapChain9Impl* object;
217     HRESULT hrc = D3D_OK;
218     WINED3DPRESENT_PARAMETERS localParameters;
219
220     TRACE("(%p) Relay\n", This);
221
222     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
223     if (NULL == object) {
224         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
225         return D3DERR_OUTOFVIDEOMEMORY;
226     }
227     object->ref = 1;
228     object->lpVtbl = &Direct3DSwapChain9_Vtbl;
229
230     /* The back buffer count is set to one if it's 0 */
231     if(pPresentationParameters->BackBufferCount == 0) {
232         pPresentationParameters->BackBufferCount = 1;
233     }
234
235     /* Allocate an associated WineD3DDevice object */
236     localParameters.BackBufferWidth                     = pPresentationParameters->BackBufferWidth;
237     localParameters.BackBufferHeight                    = pPresentationParameters->BackBufferHeight;
238     localParameters.BackBufferFormat                    = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
239     localParameters.BackBufferCount                     = pPresentationParameters->BackBufferCount;
240     localParameters.MultiSampleType                     = pPresentationParameters->MultiSampleType;
241     localParameters.MultiSampleQuality                  = pPresentationParameters->MultiSampleQuality;
242     localParameters.SwapEffect                          = pPresentationParameters->SwapEffect;
243     localParameters.hDeviceWindow                       = pPresentationParameters->hDeviceWindow;
244     localParameters.Windowed                            = pPresentationParameters->Windowed;
245     localParameters.EnableAutoDepthStencil              = pPresentationParameters->EnableAutoDepthStencil;
246     localParameters.AutoDepthStencilFormat              = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
247     localParameters.Flags                               = pPresentationParameters->Flags;
248     localParameters.FullScreen_RefreshRateInHz          = pPresentationParameters->FullScreen_RefreshRateInHz;
249     localParameters.PresentationInterval                = pPresentationParameters->PresentationInterval;
250     localParameters.AutoRestoreDisplayMode              = TRUE;
251
252     wined3d_mutex_lock();
253     hrc = IWineD3DDevice_CreateSwapChain(This->WineD3DDevice, &localParameters,
254             &object->wineD3DSwapChain, (IUnknown*)object, SURFACE_OPENGL);
255     wined3d_mutex_unlock();
256
257     pPresentationParameters->BackBufferWidth            = localParameters.BackBufferWidth;
258     pPresentationParameters->BackBufferHeight           = localParameters.BackBufferHeight;
259     pPresentationParameters->BackBufferFormat           = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
260     pPresentationParameters->BackBufferCount            = localParameters.BackBufferCount;
261     pPresentationParameters->MultiSampleType            = localParameters.MultiSampleType;
262     pPresentationParameters->MultiSampleQuality         = localParameters.MultiSampleQuality;
263     pPresentationParameters->SwapEffect                 = localParameters.SwapEffect;
264     pPresentationParameters->hDeviceWindow              = localParameters.hDeviceWindow;
265     pPresentationParameters->Windowed                   = localParameters.Windowed;
266     pPresentationParameters->EnableAutoDepthStencil     = localParameters.EnableAutoDepthStencil;
267     pPresentationParameters->AutoDepthStencilFormat     = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
268     pPresentationParameters->Flags                      = localParameters.Flags;
269     pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
270     pPresentationParameters->PresentationInterval       = localParameters.PresentationInterval;
271
272     if (hrc != D3D_OK) {
273         FIXME("(%p) call to IWineD3DDevice_CreateSwapChain failed\n", This);
274         HeapFree(GetProcessHeap(), 0 , object);
275     } else {
276         IDirect3DDevice9Ex_AddRef(iface);
277         object->parentDevice = iface;
278         *pSwapChain = (IDirect3DSwapChain9 *)object;
279         TRACE("(%p) : Created swapchain %p\n", This, *pSwapChain);
280     }
281     TRACE("(%p) returning %p\n", This, *pSwapChain);
282     return hrc;
283 }
284
285 HRESULT  WINAPI  IDirect3DDevice9Impl_GetSwapChain(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, IDirect3DSwapChain9** pSwapChain) {
286     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
287     HRESULT hrc = D3D_OK;
288     IWineD3DSwapChain *swapchain = NULL;
289
290     TRACE("(%p) Relay\n", This);
291
292     wined3d_mutex_lock();
293     hrc = IWineD3DDevice_GetSwapChain(This->WineD3DDevice, iSwapChain, &swapchain);
294     if (hrc == D3D_OK && NULL != swapchain) {
295        IWineD3DSwapChain_GetParent(swapchain, (IUnknown **)pSwapChain);
296        IWineD3DSwapChain_Release(swapchain);
297     } else {
298         *pSwapChain = NULL;
299     }
300     wined3d_mutex_unlock();
301
302     return hrc;
303 }
304
305 UINT     WINAPI  IDirect3DDevice9Impl_GetNumberOfSwapChains(LPDIRECT3DDEVICE9EX iface) {
306     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
307     UINT ret;
308     TRACE("(%p) Relay\n", This);
309
310     wined3d_mutex_lock();
311     ret = IWineD3DDevice_GetNumberOfSwapChains(This->WineD3DDevice);
312     wined3d_mutex_unlock();
313
314     return ret;
315 }