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