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