wined3d: Don't use TMP_COLOR unless srgb is enabled.
[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     HRESULT hr;
78
79     TRACE("(%p) Relay\n", This);
80
81     EnterCriticalSection(&d3d9_cs);
82     hr = IWineD3DSwapChain_Present(This->wineD3DSwapChain, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
83     LeaveCriticalSection(&d3d9_cs);
84
85     return hr;
86 }
87
88 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetFrontBufferData(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DSurface9* pDestSurface) {
89     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
90     HRESULT hr;
91     TRACE("(%p) Relay\n", This);
92
93     EnterCriticalSection(&d3d9_cs);
94     hr = IWineD3DSwapChain_GetFrontBufferData(This->wineD3DSwapChain,  ((IDirect3DSurface9Impl *)pDestSurface)->wineD3DSurface);
95     LeaveCriticalSection(&d3d9_cs);
96     return hr;
97 }
98
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;
103
104     TRACE("(%p) Relay\n", This);
105
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);
111     }
112     LeaveCriticalSection(&d3d9_cs);
113     /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
114     return hrc;
115 }
116
117 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
118     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
119     HRESULT hr;
120     TRACE("(%p) Relay\n", This);
121
122     EnterCriticalSection(&d3d9_cs);
123     hr = IWineD3DSwapChain_GetRasterStatus(This->wineD3DSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
124     LeaveCriticalSection(&d3d9_cs);
125     return hr;
126 }
127
128 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
129     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
130     HRESULT hr;
131     TRACE("(%p) Relay\n", This);
132
133     EnterCriticalSection(&d3d9_cs);
134     hr = IWineD3DSwapChain_GetDisplayMode(This->wineD3DSwapChain, (WINED3DDISPLAYMODE *) pMode);
135     LeaveCriticalSection(&d3d9_cs);
136
137     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
138
139     return hr;
140 }
141
142 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(LPDIRECT3DSWAPCHAIN9 iface, IDirect3DDevice9** ppDevice) {
143     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
144     HRESULT hrc = D3D_OK;
145     IWineD3DDevice *device = NULL;
146
147     TRACE("(%p) Relay\n", This);
148
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);
154     }
155     LeaveCriticalSection(&d3d9_cs);
156     return hrc;
157 }
158
159 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(LPDIRECT3DSWAPCHAIN9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
160     IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
161     WINED3DPRESENT_PARAMETERS winePresentParameters;
162     HRESULT hr;
163
164     TRACE("(%p)->(%p): Relay\n", This, pPresentationParameters);
165
166     EnterCriticalSection(&d3d9_cs);
167     hr = IWineD3DSwapChain_GetPresentParameters(This->wineD3DSwapChain, &winePresentParameters);
168     LeaveCriticalSection(&d3d9_cs);
169
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;
184
185     return hr;
186 }
187
188
189 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
190 {
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
201 };
202
203
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;
210
211     TRACE("(%p) Relay\n", This);
212
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;
217     }
218     object->ref = 1;
219     object->lpVtbl = &Direct3DSwapChain9_Vtbl;
220
221     /* The back buffer count is set to one if it's 0 */
222     if(pPresentationParameters->BackBufferCount == 0) {
223         pPresentationParameters->BackBufferCount = 1;
224     }
225
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;
242
243     EnterCriticalSection(&d3d9_cs);
244     hrc = IWineD3DDevice_CreateSwapChain(This->WineD3DDevice, &localParameters,
245             &object->wineD3DSwapChain, (IUnknown*)object, SURFACE_OPENGL);
246     LeaveCriticalSection(&d3d9_cs);
247
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;
262
263     if (hrc != D3D_OK) {
264         FIXME("(%p) call to IWineD3DDevice_CreateSwapChain failed\n", This);
265         HeapFree(GetProcessHeap(), 0 , object);
266     } else {
267         IDirect3DDevice9Ex_AddRef(iface);
268         object->parentDevice = iface;
269         *pSwapChain = (IDirect3DSwapChain9 *)object;
270         TRACE("(%p) : Created swapchain %p\n", This, *pSwapChain);
271     }
272     TRACE("(%p) returning %p\n", This, *pSwapChain);
273     return hrc;
274 }
275
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;
280
281     TRACE("(%p) Relay\n", This);
282
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);
288     } else {
289         *pSwapChain = NULL;
290     }
291     LeaveCriticalSection(&d3d9_cs);
292     return hrc;
293 }
294
295 UINT     WINAPI  IDirect3DDevice9Impl_GetNumberOfSwapChains(LPDIRECT3DDEVICE9EX iface) {
296     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
297     UINT ret;
298     TRACE("(%p) Relay\n", This);
299
300     EnterCriticalSection(&d3d9_cs);
301     ret = IWineD3DDevice_GetNumberOfSwapChains(This->WineD3DDevice);
302     LeaveCriticalSection(&d3d9_cs);
303     return ret;
304 }