Revert "d3d: Remove AddRef from IWineD3DDevice_GetSwapChain.".
[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         IUnknown_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     return ref;
52 }
53
54 static ULONG WINAPI IDirect3DSwapChain9Impl_Release(LPDIRECT3DSWAPCHAIN9 iface) {
55     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
56     ULONG ref = InterlockedDecrement(&This->ref);
57
58     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
59
60     if (ref == 0) {
61         IWineD3DSwapChain_Release(This->wineD3DSwapChain);
62         if (This->parentDevice) IUnknown_Release(This->parentDevice);
63         HeapFree(GetProcessHeap(), 0, This);
64     }
65     return ref;
66 }
67
68 /* IDirect3DSwapChain9 parts follow: */
69 static HRESULT WINAPI IDirect3DSwapChain9Impl_Present(LPDIRECT3DSWAPCHAIN9 iface, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags) {
70     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
71     TRACE("(%p) Relay\n", This);
72     return IWineD3DSwapChain_Present(This->wineD3DSwapChain, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
73 }
74
75 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetFrontBufferData(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DSurface9* pDestSurface) {
76     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
77     TRACE("(%p) Relay\n", This);
78     return IWineD3DSwapChain_GetFrontBufferData(This->wineD3DSwapChain,  ((IDirect3DSurface9Impl *)pDestSurface)->wineD3DSurface);
79 }
80
81 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetBackBuffer(LPDIRECT3DSWAPCHAIN9 iface, UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9** ppBackBuffer) {
82     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
83     HRESULT hrc = D3D_OK;
84     IWineD3DSurface *mySurface = NULL;
85
86     TRACE("(%p) Relay\n", This);
87
88     hrc = IWineD3DSwapChain_GetBackBuffer(This->wineD3DSwapChain, iBackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &mySurface);
89     if (hrc == D3D_OK && NULL != mySurface) {
90        IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppBackBuffer);
91     }
92     /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
93     return hrc;
94 }
95
96 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
97     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
98     TRACE("(%p) Relay\n", This);
99     return IWineD3DSwapChain_GetRasterStatus(This->wineD3DSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
100 }
101
102 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
103     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
104     TRACE("(%p) Relay\n", This);
105     return IWineD3DSwapChain_GetDisplayMode(This->wineD3DSwapChain, (WINED3DDISPLAYMODE *) pMode);
106 }
107
108 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DDevice9** ppDevice) {
109     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
110     HRESULT hrc = D3D_OK;
111     IWineD3DDevice *device = NULL;
112
113     TRACE("(%p) Relay\n", This);
114
115     hrc = IWineD3DSwapChain_GetDevice(This->wineD3DSwapChain, &device);
116     if (hrc == D3D_OK && NULL != device) {
117        IWineD3DDevice_GetParent(device, (IUnknown **)ppDevice);
118        IWineD3DDevice_Release(device);
119     }
120     return hrc;
121 }
122
123 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
124     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
125     WINED3DPRESENT_PARAMETERS winePresentParameters;
126     TRACE("(%p)->(%p): Relay\n", This, pPresentationParameters);
127     winePresentParameters.BackBufferWidth = &pPresentationParameters->BackBufferWidth;
128     winePresentParameters.BackBufferHeight = &pPresentationParameters->BackBufferHeight;
129     winePresentParameters.BackBufferFormat = (WINED3DFORMAT *) &pPresentationParameters->BackBufferFormat;
130     winePresentParameters.BackBufferCount = &pPresentationParameters->BackBufferCount;
131     winePresentParameters.MultiSampleType = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
132     winePresentParameters.MultiSampleQuality = &pPresentationParameters->MultiSampleQuality;
133     winePresentParameters.SwapEffect = (WINED3DSWAPEFFECT *)&pPresentationParameters->SwapEffect;
134     winePresentParameters.hDeviceWindow = &pPresentationParameters->hDeviceWindow;
135     winePresentParameters.Windowed = &pPresentationParameters->Windowed;
136     winePresentParameters.EnableAutoDepthStencil = &pPresentationParameters->EnableAutoDepthStencil;
137     winePresentParameters.Flags = &pPresentationParameters->Flags;
138     winePresentParameters.FullScreen_RefreshRateInHz = &pPresentationParameters->FullScreen_RefreshRateInHz;
139     winePresentParameters.PresentationInterval = &pPresentationParameters->PresentationInterval;
140     return IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
141 }
142
143
144 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
145 {
146     IDirect3DSwapChain9Impl_QueryInterface,
147     IDirect3DSwapChain9Impl_AddRef,
148     IDirect3DSwapChain9Impl_Release,
149     IDirect3DSwapChain9Impl_Present,
150     IDirect3DSwapChain9Impl_GetFrontBufferData,
151     IDirect3DSwapChain9Impl_GetBackBuffer,
152     IDirect3DSwapChain9Impl_GetRasterStatus,
153     IDirect3DSwapChain9Impl_GetDisplayMode,
154     IDirect3DSwapChain9Impl_GetDevice,
155     IDirect3DSwapChain9Impl_GetPresentParameters
156 };
157
158
159 /* IDirect3DDevice9 IDirect3DSwapChain9 Methods follow: */
160 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain9** pSwapChain) {
161     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
162     IDirect3DSwapChain9Impl* object;
163     HRESULT hrc = D3D_OK;
164     WINED3DPRESENT_PARAMETERS localParameters;
165
166     TRACE("(%p) Relay\n", This);
167
168     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
169     if (NULL == object) {
170         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
171         return D3DERR_OUTOFVIDEOMEMORY;
172     }
173     object->ref = 1;
174     object->lpVtbl = &Direct3DSwapChain9_Vtbl;
175
176     /* The back buffer count is set to one if it's 0 */
177     if(pPresentationParameters->BackBufferCount == 0) {
178         pPresentationParameters->BackBufferCount = 1;
179     }
180
181     /* Allocate an associated WineD3DDevice object */
182     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;
183     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
184     localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
185     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
186     localParameters.MultiSampleType                = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
187     localParameters.MultiSampleQuality             = &pPresentationParameters->MultiSampleQuality;
188     localParameters.SwapEffect                     = (WINED3DSWAPEFFECT *)&pPresentationParameters->SwapEffect;
189     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
190     localParameters.Windowed                       = &pPresentationParameters->Windowed;
191     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
192     localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
193     localParameters.Flags                          = &pPresentationParameters->Flags;
194     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz;
195     localParameters.PresentationInterval           = &pPresentationParameters->PresentationInterval;
196
197
198     hrc = IWineD3DDevice_CreateAdditionalSwapChain(This->WineD3DDevice, &localParameters, &object->wineD3DSwapChain, (IUnknown*)object, D3D9CB_CreateRenderTarget, D3D9CB_CreateDepthStencilSurface);
199     if (hrc != D3D_OK) {
200         FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
201         HeapFree(GetProcessHeap(), 0 , object);
202     } else {
203         IUnknown_AddRef(iface);
204         object->parentDevice = iface;
205         *pSwapChain = (IDirect3DSwapChain9 *)object;
206         TRACE("(%p) : Created swapchain %p\n", This, *pSwapChain);
207     }
208     TRACE("(%p) returning %p\n", This, *pSwapChain);
209     return hrc;
210 }
211
212 HRESULT  WINAPI  IDirect3DDevice9Impl_GetSwapChain(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, IDirect3DSwapChain9** pSwapChain) {
213     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
214     HRESULT hrc = D3D_OK;
215     IWineD3DSwapChain *swapchain = NULL;
216
217     TRACE("(%p) Relay\n", This);
218
219     hrc = IWineD3DDevice_GetSwapChain(This->WineD3DDevice, iSwapChain, &swapchain);
220     if (hrc == D3D_OK && NULL != swapchain) {
221        IWineD3DSwapChain_GetParent(swapchain, (IUnknown **)pSwapChain);
222        IWineD3DSwapChain_Release(swapchain);
223     } else {
224         *pSwapChain = NULL;
225     }
226     return hrc;
227 }
228
229 UINT     WINAPI  IDirect3DDevice9Impl_GetNumberOfSwapChains(LPDIRECT3DDEVICE9 iface) {
230     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
231     TRACE("(%p) Relay\n", This);
232     return IWineD3DDevice_GetNumberOfSwapChains(This->WineD3DDevice);
233 }