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