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