d3d9: Fix some sign compare warnings.
[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             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
130     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
131
132     return hr;
133 }
134
135 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DDevice9** ppDevice) {
136     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
137     HRESULT hrc = D3D_OK;
138     IWineD3DDevice *device = NULL;
139
140     TRACE("(%p) Relay\n", This);
141
142     EnterCriticalSection(&d3d9_cs);
143     hrc = IWineD3DSwapChain_GetDevice(This->wineD3DSwapChain, &device);
144     if (hrc == D3D_OK && NULL != device) {
145        IWineD3DDevice_GetParent(device, (IUnknown **)ppDevice);
146        IWineD3DDevice_Release(device);
147     }
148     LeaveCriticalSection(&d3d9_cs);
149     return hrc;
150 }
151
152 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
153     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
154     WINED3DPRESENT_PARAMETERS winePresentParameters;
155     HRESULT hr;
156
157     TRACE("(%p)->(%p): Relay\n", This, pPresentationParameters);
158
159     EnterCriticalSection(&d3d9_cs);
160     hr = IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
161     LeaveCriticalSection(&d3d9_cs);
162
163     pPresentationParameters->BackBufferWidth            = winePresentParameters.BackBufferWidth;
164     pPresentationParameters->BackBufferHeight           = winePresentParameters.BackBufferHeight;
165     pPresentationParameters->BackBufferFormat           = d3dformat_from_wined3dformat(winePresentParameters.BackBufferFormat);
166     pPresentationParameters->BackBufferCount            = winePresentParameters.BackBufferCount;
167     pPresentationParameters->MultiSampleType            = winePresentParameters.MultiSampleType;
168     pPresentationParameters->MultiSampleQuality         = winePresentParameters.MultiSampleQuality;
169     pPresentationParameters->SwapEffect                 = winePresentParameters.SwapEffect;
170     pPresentationParameters->hDeviceWindow              = winePresentParameters.hDeviceWindow;
171     pPresentationParameters->Windowed                   = winePresentParameters.Windowed;
172     pPresentationParameters->EnableAutoDepthStencil     = winePresentParameters.EnableAutoDepthStencil;
173     pPresentationParameters->AutoDepthStencilFormat     = d3dformat_from_wined3dformat(winePresentParameters.AutoDepthStencilFormat);
174     pPresentationParameters->Flags                      = winePresentParameters.Flags;
175     pPresentationParameters->FullScreen_RefreshRateInHz = winePresentParameters.FullScreen_RefreshRateInHz;
176     pPresentationParameters->PresentationInterval       = winePresentParameters.PresentationInterval;
177
178     return hr;
179 }
180
181
182 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
183 {
184     IDirect3DSwapChain9Impl_QueryInterface,
185     IDirect3DSwapChain9Impl_AddRef,
186     IDirect3DSwapChain9Impl_Release,
187     IDirect3DSwapChain9Impl_Present,
188     IDirect3DSwapChain9Impl_GetFrontBufferData,
189     IDirect3DSwapChain9Impl_GetBackBuffer,
190     IDirect3DSwapChain9Impl_GetRasterStatus,
191     IDirect3DSwapChain9Impl_GetDisplayMode,
192     IDirect3DSwapChain9Impl_GetDevice,
193     IDirect3DSwapChain9Impl_GetPresentParameters
194 };
195
196
197 /* IDirect3DDevice9 IDirect3DSwapChain9 Methods follow: */
198 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE9EX iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain9** pSwapChain) {
199     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
200     IDirect3DSwapChain9Impl* object;
201     HRESULT hrc = D3D_OK;
202     WINED3DPRESENT_PARAMETERS localParameters;
203
204     TRACE("(%p) Relay\n", This);
205
206     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
207     if (NULL == object) {
208         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
209         return D3DERR_OUTOFVIDEOMEMORY;
210     }
211     object->ref = 1;
212     object->lpVtbl = &Direct3DSwapChain9_Vtbl;
213
214     /* The back buffer count is set to one if it's 0 */
215     if(pPresentationParameters->BackBufferCount == 0) {
216         pPresentationParameters->BackBufferCount = 1;
217     }
218
219     /* Allocate an associated WineD3DDevice object */
220     localParameters.BackBufferWidth                     = pPresentationParameters->BackBufferWidth;
221     localParameters.BackBufferHeight                    = pPresentationParameters->BackBufferHeight;
222     localParameters.BackBufferFormat                    = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
223     localParameters.BackBufferCount                     = pPresentationParameters->BackBufferCount;
224     localParameters.MultiSampleType                     = pPresentationParameters->MultiSampleType;
225     localParameters.MultiSampleQuality                  = pPresentationParameters->MultiSampleQuality;
226     localParameters.SwapEffect                          = pPresentationParameters->SwapEffect;
227     localParameters.hDeviceWindow                       = pPresentationParameters->hDeviceWindow;
228     localParameters.Windowed                            = pPresentationParameters->Windowed;
229     localParameters.EnableAutoDepthStencil              = pPresentationParameters->EnableAutoDepthStencil;
230     localParameters.AutoDepthStencilFormat              = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
231     localParameters.Flags                               = pPresentationParameters->Flags;
232     localParameters.FullScreen_RefreshRateInHz          = pPresentationParameters->FullScreen_RefreshRateInHz;
233     localParameters.PresentationInterval                = pPresentationParameters->PresentationInterval;
234     localParameters.AutoRestoreDisplayMode              = TRUE;
235
236     EnterCriticalSection(&d3d9_cs);
237     hrc = IWineD3DDevice_CreateSwapChain(This->WineD3DDevice, &localParameters,
238             &object->wineD3DSwapChain, (IUnknown*)object, SURFACE_OPENGL);
239     LeaveCriticalSection(&d3d9_cs);
240
241     pPresentationParameters->BackBufferWidth            = localParameters.BackBufferWidth;
242     pPresentationParameters->BackBufferHeight           = localParameters.BackBufferHeight;
243     pPresentationParameters->BackBufferFormat           = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
244     pPresentationParameters->BackBufferCount            = localParameters.BackBufferCount;
245     pPresentationParameters->MultiSampleType            = localParameters.MultiSampleType;
246     pPresentationParameters->MultiSampleQuality         = localParameters.MultiSampleQuality;
247     pPresentationParameters->SwapEffect                 = localParameters.SwapEffect;
248     pPresentationParameters->hDeviceWindow              = localParameters.hDeviceWindow;
249     pPresentationParameters->Windowed                   = localParameters.Windowed;
250     pPresentationParameters->EnableAutoDepthStencil     = localParameters.EnableAutoDepthStencil;
251     pPresentationParameters->AutoDepthStencilFormat     = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
252     pPresentationParameters->Flags                      = localParameters.Flags;
253     pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
254     pPresentationParameters->PresentationInterval       = localParameters.PresentationInterval;
255
256     if (hrc != D3D_OK) {
257         FIXME("(%p) call to IWineD3DDevice_CreateSwapChain failed\n", This);
258         HeapFree(GetProcessHeap(), 0 , object);
259     } else {
260         IDirect3DDevice9Ex_AddRef(iface);
261         object->parentDevice = iface;
262         *pSwapChain = (IDirect3DSwapChain9 *)object;
263         TRACE("(%p) : Created swapchain %p\n", This, *pSwapChain);
264     }
265     TRACE("(%p) returning %p\n", This, *pSwapChain);
266     return hrc;
267 }
268
269 HRESULT  WINAPI  IDirect3DDevice9Impl_GetSwapChain(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, IDirect3DSwapChain9** pSwapChain) {
270     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
271     HRESULT hrc = D3D_OK;
272     IWineD3DSwapChain *swapchain = NULL;
273
274     TRACE("(%p) Relay\n", This);
275
276     EnterCriticalSection(&d3d9_cs);
277     hrc = IWineD3DDevice_GetSwapChain(This->WineD3DDevice, iSwapChain, &swapchain);
278     if (hrc == D3D_OK && NULL != swapchain) {
279        IWineD3DSwapChain_GetParent(swapchain, (IUnknown **)pSwapChain);
280        IWineD3DSwapChain_Release(swapchain);
281     } else {
282         *pSwapChain = NULL;
283     }
284     LeaveCriticalSection(&d3d9_cs);
285     return hrc;
286 }
287
288 UINT     WINAPI  IDirect3DDevice9Impl_GetNumberOfSwapChains(LPDIRECT3DDEVICE9EX iface) {
289     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
290     UINT ret;
291     TRACE("(%p) Relay\n", This);
292
293     EnterCriticalSection(&d3d9_cs);
294     ret = IWineD3DDevice_GetNumberOfSwapChains(This->WineD3DDevice);
295     LeaveCriticalSection(&d3d9_cs);
296     return ret;
297 }