2 * IDirect3DSwapChain9 implementation
4 * Copyright 2002-2003 Jason Edmeades
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
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 if (IsEqualGUID(riid, &IID_IUnknown)
34 || IsEqualGUID(riid, &IID_IDirect3DSwapChain9)) {
35 IDirect3DSwapChain9_AddRef(iface);
40 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
45 static ULONG WINAPI IDirect3DSwapChain9Impl_AddRef(LPDIRECT3DSWAPCHAIN9 iface) {
46 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
47 ULONG ref = InterlockedIncrement(&This->ref);
49 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
51 if(ref == 1 && This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
56 static ULONG WINAPI IDirect3DSwapChain9Impl_Release(LPDIRECT3DSWAPCHAIN9 iface) {
57 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
58 ULONG ref = InterlockedDecrement(&This->ref);
60 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
63 if (This->parentDevice) IDirect3DDevice9Ex_Release(This->parentDevice);
64 if (!This->isImplicit) {
65 EnterCriticalSection(&d3d9_cs);
66 IWineD3DSwapChain_Destroy(This->wineD3DSwapChain, D3D9CB_DestroyRenderTarget);
67 LeaveCriticalSection(&d3d9_cs);
68 HeapFree(GetProcessHeap(), 0, This);
74 /* IDirect3DSwapChain9 parts follow: */
75 static HRESULT WINAPI IDirect3DSwapChain9Impl_Present(LPDIRECT3DSWAPCHAIN9 iface, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags) {
76 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
79 TRACE("(%p) Relay\n", This);
81 EnterCriticalSection(&d3d9_cs);
82 hr = IWineD3DSwapChain_Present(This->wineD3DSwapChain, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
83 LeaveCriticalSection(&d3d9_cs);
88 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetFrontBufferData(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DSurface9* pDestSurface) {
89 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
91 TRACE("(%p) Relay\n", This);
93 EnterCriticalSection(&d3d9_cs);
94 hr = IWineD3DSwapChain_GetFrontBufferData(This->wineD3DSwapChain, ((IDirect3DSurface9Impl *)pDestSurface)->wineD3DSurface);
95 LeaveCriticalSection(&d3d9_cs);
99 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetBackBuffer(LPDIRECT3DSWAPCHAIN9 iface, UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9** ppBackBuffer) {
100 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
101 HRESULT hrc = D3D_OK;
102 IWineD3DSurface *mySurface = NULL;
104 TRACE("(%p) Relay\n", This);
106 EnterCriticalSection(&d3d9_cs);
107 hrc = IWineD3DSwapChain_GetBackBuffer(This->wineD3DSwapChain, iBackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &mySurface);
108 if (hrc == D3D_OK && NULL != mySurface) {
109 IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppBackBuffer);
110 IWineD3DSurface_Release(mySurface);
112 LeaveCriticalSection(&d3d9_cs);
113 /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
117 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
118 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
120 TRACE("(%p) Relay\n", This);
122 EnterCriticalSection(&d3d9_cs);
123 hr = IWineD3DSwapChain_GetRasterStatus(This->wineD3DSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
124 LeaveCriticalSection(&d3d9_cs);
128 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
129 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
131 TRACE("(%p) Relay\n", This);
133 EnterCriticalSection(&d3d9_cs);
134 hr = IWineD3DSwapChain_GetDisplayMode(This->wineD3DSwapChain, (WINED3DDISPLAYMODE *) pMode);
135 LeaveCriticalSection(&d3d9_cs);
137 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
142 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DDevice9** ppDevice) {
143 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
144 HRESULT hrc = D3D_OK;
145 IWineD3DDevice *device = NULL;
147 TRACE("(%p) Relay\n", This);
149 EnterCriticalSection(&d3d9_cs);
150 hrc = IWineD3DSwapChain_GetDevice(This->wineD3DSwapChain, &device);
151 if (hrc == D3D_OK && NULL != device) {
152 IWineD3DDevice_GetParent(device, (IUnknown **)ppDevice);
153 IWineD3DDevice_Release(device);
155 LeaveCriticalSection(&d3d9_cs);
159 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
160 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
161 WINED3DPRESENT_PARAMETERS winePresentParameters;
164 TRACE("(%p)->(%p): Relay\n", This, pPresentationParameters);
166 EnterCriticalSection(&d3d9_cs);
167 hr = IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
168 LeaveCriticalSection(&d3d9_cs);
170 pPresentationParameters->BackBufferWidth = winePresentParameters.BackBufferWidth;
171 pPresentationParameters->BackBufferHeight = winePresentParameters.BackBufferHeight;
172 pPresentationParameters->BackBufferFormat = d3dformat_from_wined3dformat(winePresentParameters.BackBufferFormat);
173 pPresentationParameters->BackBufferCount = winePresentParameters.BackBufferCount;
174 pPresentationParameters->MultiSampleType = winePresentParameters.MultiSampleType;
175 pPresentationParameters->MultiSampleQuality = winePresentParameters.MultiSampleQuality;
176 pPresentationParameters->SwapEffect = winePresentParameters.SwapEffect;
177 pPresentationParameters->hDeviceWindow = winePresentParameters.hDeviceWindow;
178 pPresentationParameters->Windowed = winePresentParameters.Windowed;
179 pPresentationParameters->EnableAutoDepthStencil = winePresentParameters.EnableAutoDepthStencil;
180 pPresentationParameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(winePresentParameters.AutoDepthStencilFormat);
181 pPresentationParameters->Flags = winePresentParameters.Flags;
182 pPresentationParameters->FullScreen_RefreshRateInHz = winePresentParameters.FullScreen_RefreshRateInHz;
183 pPresentationParameters->PresentationInterval = winePresentParameters.PresentationInterval;
189 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
191 IDirect3DSwapChain9Impl_QueryInterface,
192 IDirect3DSwapChain9Impl_AddRef,
193 IDirect3DSwapChain9Impl_Release,
194 IDirect3DSwapChain9Impl_Present,
195 IDirect3DSwapChain9Impl_GetFrontBufferData,
196 IDirect3DSwapChain9Impl_GetBackBuffer,
197 IDirect3DSwapChain9Impl_GetRasterStatus,
198 IDirect3DSwapChain9Impl_GetDisplayMode,
199 IDirect3DSwapChain9Impl_GetDevice,
200 IDirect3DSwapChain9Impl_GetPresentParameters
204 /* IDirect3DDevice9 IDirect3DSwapChain9 Methods follow: */
205 HRESULT WINAPI IDirect3DDevice9Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE9EX iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain9** pSwapChain) {
206 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
207 IDirect3DSwapChain9Impl* object;
208 HRESULT hrc = D3D_OK;
209 WINED3DPRESENT_PARAMETERS localParameters;
211 TRACE("(%p) Relay\n", This);
213 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
214 if (NULL == object) {
215 FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
216 return D3DERR_OUTOFVIDEOMEMORY;
219 object->lpVtbl = &Direct3DSwapChain9_Vtbl;
221 /* The back buffer count is set to one if it's 0 */
222 if(pPresentationParameters->BackBufferCount == 0) {
223 pPresentationParameters->BackBufferCount = 1;
226 /* Allocate an associated WineD3DDevice object */
227 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
228 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
229 localParameters.BackBufferFormat = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
230 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
231 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
232 localParameters.MultiSampleQuality = pPresentationParameters->MultiSampleQuality;
233 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
234 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
235 localParameters.Windowed = pPresentationParameters->Windowed;
236 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
237 localParameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
238 localParameters.Flags = pPresentationParameters->Flags;
239 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
240 localParameters.PresentationInterval = pPresentationParameters->PresentationInterval;
241 localParameters.AutoRestoreDisplayMode = TRUE;
243 EnterCriticalSection(&d3d9_cs);
244 hrc = IWineD3DDevice_CreateSwapChain(This->WineD3DDevice, &localParameters,
245 &object->wineD3DSwapChain, (IUnknown*)object, SURFACE_OPENGL);
246 LeaveCriticalSection(&d3d9_cs);
248 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
249 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
250 pPresentationParameters->BackBufferFormat = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
251 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
252 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
253 pPresentationParameters->MultiSampleQuality = localParameters.MultiSampleQuality;
254 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
255 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
256 pPresentationParameters->Windowed = localParameters.Windowed;
257 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
258 pPresentationParameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
259 pPresentationParameters->Flags = localParameters.Flags;
260 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
261 pPresentationParameters->PresentationInterval = localParameters.PresentationInterval;
264 FIXME("(%p) call to IWineD3DDevice_CreateSwapChain failed\n", This);
265 HeapFree(GetProcessHeap(), 0 , object);
267 IDirect3DDevice9Ex_AddRef(iface);
268 object->parentDevice = iface;
269 *pSwapChain = (IDirect3DSwapChain9 *)object;
270 TRACE("(%p) : Created swapchain %p\n", This, *pSwapChain);
272 TRACE("(%p) returning %p\n", This, *pSwapChain);
276 HRESULT WINAPI IDirect3DDevice9Impl_GetSwapChain(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, IDirect3DSwapChain9** pSwapChain) {
277 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
278 HRESULT hrc = D3D_OK;
279 IWineD3DSwapChain *swapchain = NULL;
281 TRACE("(%p) Relay\n", This);
283 EnterCriticalSection(&d3d9_cs);
284 hrc = IWineD3DDevice_GetSwapChain(This->WineD3DDevice, iSwapChain, &swapchain);
285 if (hrc == D3D_OK && NULL != swapchain) {
286 IWineD3DSwapChain_GetParent(swapchain, (IUnknown **)pSwapChain);
287 IWineD3DSwapChain_Release(swapchain);
291 LeaveCriticalSection(&d3d9_cs);
295 UINT WINAPI IDirect3DDevice9Impl_GetNumberOfSwapChains(LPDIRECT3DDEVICE9EX iface) {
296 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
298 TRACE("(%p) Relay\n", This);
300 EnterCriticalSection(&d3d9_cs);
301 ret = IWineD3DDevice_GetNumberOfSwapChains(This->WineD3DDevice);
302 LeaveCriticalSection(&d3d9_cs);