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