openal32: Remove the ALCcontext wrapper.
[wine.git] / dlls / d3d9 / swapchain.c
blobc692371a25893c243cd0b2f6b7829be9a67dcdff
1 /*
2 * IDirect3DSwapChain9 implementation
4 * Copyright 2002-2003 Jason Edmeades
5 * Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
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.
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.
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
23 #include "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 /* IDirect3DSwapChain IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DSwapChain9Impl_QueryInterface(LPDIRECT3DSWAPCHAIN9 iface, REFIID riid, LPVOID* ppobj)
31 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
33 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
35 if (IsEqualGUID(riid, &IID_IUnknown)
36 || IsEqualGUID(riid, &IID_IDirect3DSwapChain9)) {
37 IDirect3DSwapChain9_AddRef(iface);
38 *ppobj = This;
39 return S_OK;
42 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
43 *ppobj = NULL;
44 return E_NOINTERFACE;
47 static ULONG WINAPI IDirect3DSwapChain9Impl_AddRef(LPDIRECT3DSWAPCHAIN9 iface) {
48 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
49 ULONG ref = InterlockedIncrement(&This->ref);
51 TRACE("%p increasing refcount to %u.\n", iface, ref);
53 if(ref == 1 && This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
55 return ref;
58 static ULONG WINAPI IDirect3DSwapChain9Impl_Release(LPDIRECT3DSWAPCHAIN9 iface) {
59 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
60 ULONG ref = InterlockedDecrement(&This->ref);
62 TRACE("%p decreasing refcount to %u.\n", iface, ref);
64 if (ref == 0) {
65 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
67 if (!This->isImplicit) {
68 wined3d_mutex_lock();
69 IWineD3DSwapChain_Destroy(This->wineD3DSwapChain);
70 wined3d_mutex_unlock();
72 HeapFree(GetProcessHeap(), 0, This);
75 /* Release the device last, as it may cause the device to be destroyed. */
76 if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
78 return ref;
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;
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);
89 wined3d_mutex_lock();
90 hr = IWineD3DSwapChain_Present(This->wineD3DSwapChain, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
91 wined3d_mutex_unlock();
93 return hr;
96 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetFrontBufferData(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DSurface9* pDestSurface) {
97 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
98 HRESULT hr;
100 TRACE("iface %p, surface %p.\n", iface, pDestSurface);
102 wined3d_mutex_lock();
103 hr = IWineD3DSwapChain_GetFrontBufferData(This->wineD3DSwapChain, ((IDirect3DSurface9Impl *)pDestSurface)->wineD3DSurface);
104 wined3d_mutex_unlock();
106 return hr;
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;
114 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
115 iface, iBackBuffer, Type, ppBackBuffer);
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);
123 wined3d_mutex_unlock();
125 /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
126 return hrc;
129 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
130 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
131 HRESULT hr;
133 TRACE("iface %p, raster_status %p.\n", iface, pRasterStatus);
135 wined3d_mutex_lock();
136 hr = IWineD3DSwapChain_GetRasterStatus(This->wineD3DSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
137 wined3d_mutex_unlock();
139 return hr;
142 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
143 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
144 HRESULT hr;
146 TRACE("iface %p, mode %p.\n", iface, pMode);
148 wined3d_mutex_lock();
149 hr = IWineD3DSwapChain_GetDisplayMode(This->wineD3DSwapChain, (WINED3DDISPLAYMODE *) pMode);
150 wined3d_mutex_unlock();
152 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
154 return hr;
157 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(IDirect3DSwapChain9 *iface, IDirect3DDevice9 **device)
159 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
161 TRACE("iface %p, device %p.\n", iface, device);
163 *device = (IDirect3DDevice9 *)This->parentDevice;
164 IDirect3DDevice9_AddRef(*device);
166 TRACE("Returning device %p.\n", *device);
168 return D3D_OK;
171 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
172 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
173 WINED3DPRESENT_PARAMETERS winePresentParameters;
174 HRESULT hr;
176 TRACE("iface %p, parameters %p.\n", iface, pPresentationParameters);
178 wined3d_mutex_lock();
179 hr = IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
180 wined3d_mutex_unlock();
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;
197 return hr;
201 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
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
215 HRESULT swapchain_init(IDirect3DSwapChain9Impl *swapchain, IDirect3DDevice9Impl *device,
216 D3DPRESENT_PARAMETERS *present_parameters)
218 WINED3DPRESENT_PARAMETERS wined3d_parameters;
219 HRESULT hr;
221 swapchain->ref = 1;
222 swapchain->lpVtbl = &Direct3DSwapChain9_Vtbl;
224 wined3d_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
225 wined3d_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
226 wined3d_parameters.BackBufferFormat = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
227 wined3d_parameters.BackBufferCount = max(1, present_parameters->BackBufferCount);
228 wined3d_parameters.MultiSampleType = present_parameters->MultiSampleType;
229 wined3d_parameters.MultiSampleQuality = present_parameters->MultiSampleQuality;
230 wined3d_parameters.SwapEffect = present_parameters->SwapEffect;
231 wined3d_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
232 wined3d_parameters.Windowed = present_parameters->Windowed;
233 wined3d_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
234 wined3d_parameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
235 wined3d_parameters.Flags = present_parameters->Flags;
236 wined3d_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
237 wined3d_parameters.PresentationInterval = present_parameters->PresentationInterval;
238 wined3d_parameters.AutoRestoreDisplayMode = TRUE;
240 wined3d_mutex_lock();
241 hr = IWineD3DDevice_CreateSwapChain(device->WineD3DDevice, &wined3d_parameters,
242 &swapchain->wineD3DSwapChain, (IUnknown *)swapchain, SURFACE_OPENGL);
243 wined3d_mutex_unlock();
245 present_parameters->BackBufferWidth = wined3d_parameters.BackBufferWidth;
246 present_parameters->BackBufferHeight = wined3d_parameters.BackBufferHeight;
247 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters.BackBufferFormat);
248 present_parameters->BackBufferCount = wined3d_parameters.BackBufferCount;
249 present_parameters->MultiSampleType = wined3d_parameters.MultiSampleType;
250 present_parameters->MultiSampleQuality = wined3d_parameters.MultiSampleQuality;
251 present_parameters->SwapEffect = wined3d_parameters.SwapEffect;
252 present_parameters->hDeviceWindow = wined3d_parameters.hDeviceWindow;
253 present_parameters->Windowed = wined3d_parameters.Windowed;
254 present_parameters->EnableAutoDepthStencil = wined3d_parameters.EnableAutoDepthStencil;
255 present_parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(wined3d_parameters.AutoDepthStencilFormat);
256 present_parameters->Flags = wined3d_parameters.Flags;
257 present_parameters->FullScreen_RefreshRateInHz = wined3d_parameters.FullScreen_RefreshRateInHz;
258 present_parameters->PresentationInterval = wined3d_parameters.PresentationInterval;
260 if (FAILED(hr))
262 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
263 return hr;
266 swapchain->parentDevice = (IDirect3DDevice9Ex *)device;
267 IDirect3DDevice9Ex_AddRef(swapchain->parentDevice);
269 return D3D_OK;
272 HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_GetSwapChain(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, IDirect3DSwapChain9** pSwapChain) {
273 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
274 HRESULT hrc = D3D_OK;
275 IWineD3DSwapChain *swapchain = NULL;
277 TRACE("iface %p, swapchain_idx %u, swapchain %p.\n",
278 iface, iSwapChain, pSwapChain);
280 wined3d_mutex_lock();
281 hrc = IWineD3DDevice_GetSwapChain(This->WineD3DDevice, iSwapChain, &swapchain);
282 if (hrc == D3D_OK && NULL != swapchain) {
283 IWineD3DSwapChain_GetParent(swapchain, (IUnknown **)pSwapChain);
284 IWineD3DSwapChain_Release(swapchain);
285 } else {
286 *pSwapChain = NULL;
288 wined3d_mutex_unlock();
290 return hrc;
293 UINT WINAPI IDirect3DDevice9Impl_GetNumberOfSwapChains(LPDIRECT3DDEVICE9EX iface) {
294 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
295 UINT ret;
297 TRACE("iface %p.\n", iface);
299 wined3d_mutex_lock();
300 ret = IWineD3DDevice_GetNumberOfSwapChains(This->WineD3DDevice);
301 wined3d_mutex_unlock();
303 return ret;