wined3d: Capture some more renderstates in vertex and pixel stateblocks.
[wine] / dlls / wined3d / swapchain_gdi.c
1 /*
2  *IDirect3DSwapChain9 implementation
3  *
4  *Copyright 2002-2003 Jason Edmeades
5  *Copyright 2002-2003 Raphael Junqueira
6  *Copyright 2005 Oliver Stieber
7  *Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8  *
9  *This library is free software; you can redistribute it and/or
10  *modify it under the terms of the GNU Lesser General Public
11  *License as published by the Free Software Foundation; either
12  *version 2.1 of the License, or (at your option) any later version.
13  *
14  *This library is distributed in the hope that it will be useful,
15  *but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *Lesser General Public License for more details.
18  *
19  *You should have received a copy of the GNU Lesser General Public
20  *License along with this library; if not, write to the Free Software
21  *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(fps);
29
30 static void WINAPI IWineGDISwapChainImpl_Destroy(IWineD3DSwapChain *iface)
31 {
32     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
33     WINED3DDISPLAYMODE mode;
34
35     TRACE("Destroying swapchain %p\n", iface);
36
37     IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
38
39     /* release the ref to the front and back buffer parents */
40     if(This->frontBuffer) {
41         IWineD3DSurface_SetContainer(This->frontBuffer, 0);
42         if (IWineD3DSurface_Release(This->frontBuffer) > 0)
43         {
44             WARN("(%p) Something's still holding the front buffer\n",This);
45         }
46     }
47
48     if(This->backBuffer) {
49         UINT i;
50         for(i = 0; i < This->presentParms.BackBufferCount; i++) {
51             IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
52             if (IWineD3DSurface_Release(This->backBuffer[i]) > 0)
53             {
54                 WARN("(%p) Something's still holding the back buffer\n",This);
55             }
56         }
57         HeapFree(GetProcessHeap(), 0, This->backBuffer);
58     }
59
60     /* Restore the screen resolution if we rendered in fullscreen
61      * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
62      * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
63      * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
64      */
65     if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
66         mode.Width = This->orig_width;
67         mode.Height = This->orig_height;
68         mode.RefreshRate = 0;
69         mode.Format = This->orig_fmt;
70         IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
71     }
72
73     HeapFree(GetProcessHeap(), 0, This);
74 }
75
76 /*****************************************************************************
77  * x11_copy_to_screen
78  *
79  * Helper function that blts the front buffer contents to the target window
80  *
81  * Params:
82  *  This: Surface to copy from
83  *  rc: Rectangle to copy
84  *
85  *****************************************************************************/
86 void x11_copy_to_screen(IWineD3DSwapChainImpl *This, const RECT *rc)
87 {
88     IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
89
90     if(front->resource.usage & WINED3DUSAGE_RENDERTARGET) {
91         POINT offset = {0,0};
92         HWND hDisplayWnd;
93         HDC hDisplayDC;
94         HDC hSurfaceDC = 0;
95         RECT drawrect;
96         TRACE("(%p)->(%p): Copying to screen\n", front, rc);
97
98         hSurfaceDC = front->hDC;
99
100         hDisplayWnd = This->win_handle;
101         hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
102         if(rc) {
103             TRACE(" copying rect (%d,%d)->(%d,%d), offset (%d,%d)\n",
104                   rc->left, rc->top, rc->right, rc->bottom, offset.x, offset.y);
105         }
106
107         /* Front buffer coordinates are screen coordinates. Map them to the destination
108          * window if not fullscreened
109          */
110         if(This->presentParms.Windowed) {
111             ClientToScreen(hDisplayWnd, &offset);
112         }
113 #if 0
114         /* FIXME: This doesn't work... if users really want to run
115         * X in 8bpp, then we need to call directly into display.drv
116         * (or Wine's equivalent), and force a private colormap
117         * without default entries. */
118         if (front->palette) {
119         SelectPalette(hDisplayDC, front->palette->hpal, FALSE);
120         RealizePalette(hDisplayDC); /* sends messages => deadlocks */
121     }
122 #endif
123         drawrect.left   = 0;
124         drawrect.right  = front->currentDesc.Width;
125         drawrect.top    = 0;
126         drawrect.bottom = front->currentDesc.Height;
127
128 #if 0
129         /* TODO: Support clippers */
130         if (front->clipper)
131         {
132         RECT xrc;
133         HWND hwnd = ((IWineD3DClipperImpl *) front->clipper)->hWnd;
134         if (hwnd && GetClientRect(hwnd,&xrc))
135         {
136         OffsetRect(&xrc,offset.x,offset.y);
137         IntersectRect(&drawrect,&drawrect,&xrc);
138     }
139     }
140 #endif
141         if (rc) {
142             IntersectRect(&drawrect,&drawrect,rc);
143         }
144         else {
145             /* Only use this if the caller did not pass a rectangle, since
146             * due to double locking this could be the wrong one ...
147             */
148             if (front->lockedRect.left != front->lockedRect.right) {
149                 IntersectRect(&drawrect,&drawrect,&front->lockedRect);
150             }
151         }
152
153         BitBlt(hDisplayDC,
154                drawrect.left-offset.x, drawrect.top-offset.y,
155                drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
156                hSurfaceDC,
157                drawrect.left, drawrect.top,
158                SRCCOPY);
159         ReleaseDC(hDisplayWnd, hDisplayDC);
160     }
161 }
162
163 static HRESULT WINAPI IWineGDISwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window) {
164     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
165
166     This->win_handle = window;
167     return WINED3D_OK;
168 }
169
170 static HRESULT WINAPI IWineGDISwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
171     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
172     IWineD3DSurfaceImpl *front, *back;
173
174     if(!This->backBuffer) {
175         WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
176         return WINED3DERR_INVALIDCALL;
177     }
178     front = (IWineD3DSurfaceImpl *) This->frontBuffer;
179     back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
180
181     /* Flip the DC */
182     {
183         HDC tmp;
184         tmp = front->hDC;
185         front->hDC = back->hDC;
186         back->hDC = tmp;
187     }
188
189     /* Flip the DIBsection */
190     {
191         HBITMAP tmp;
192         tmp = front->dib.DIBsection;
193         front->dib.DIBsection = back->dib.DIBsection;
194         back->dib.DIBsection = tmp;
195     }
196
197     /* Flip the surface data */
198     {
199         void* tmp;
200
201         tmp = front->dib.bitmap_data;
202         front->dib.bitmap_data = back->dib.bitmap_data;
203         back->dib.bitmap_data = tmp;
204
205         tmp = front->resource.allocatedMemory;
206         front->resource.allocatedMemory = back->resource.allocatedMemory;
207         back->resource.allocatedMemory = tmp;
208
209         if(front->resource.heapMemory) {
210             ERR("GDI Surface %p has heap memory allocated\n", front);
211         }
212         if(back->resource.heapMemory) {
213             ERR("GDI Surface %p has heap memory allocated\n", back);
214         }
215     }
216
217     /* client_memory should not be different, but just in case */
218     {
219         BOOL tmp;
220         tmp = front->dib.client_memory;
221         front->dib.client_memory = back->dib.client_memory;
222         back->dib.client_memory = tmp;
223     }
224
225     /* FPS support */
226     if (TRACE_ON(fps))
227     {
228         static long prev_time, frames;
229
230         DWORD time = GetTickCount();
231         frames++;
232         /* every 1.5 seconds */
233         if (time - prev_time > 1500) {
234             TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
235             prev_time = time;
236             frames = 0;
237         }
238     }
239
240     x11_copy_to_screen(This, NULL);
241
242     return WINED3D_OK;
243 }
244
245 const IWineD3DSwapChainVtbl IWineGDISwapChain_Vtbl =
246 {
247     /* IUnknown */
248     IWineD3DBaseSwapChainImpl_QueryInterface,
249     IWineD3DBaseSwapChainImpl_AddRef,
250     IWineD3DBaseSwapChainImpl_Release,
251     /* IWineD3DSwapChain */
252     IWineD3DBaseSwapChainImpl_GetParent,
253     IWineGDISwapChainImpl_Destroy,
254     IWineD3DBaseSwapChainImpl_GetDevice,
255     IWineGDISwapChainImpl_Present,
256     IWineGDISwapChainImpl_SetDestWindowOverride,
257     IWineD3DBaseSwapChainImpl_GetFrontBufferData,
258     IWineD3DBaseSwapChainImpl_GetBackBuffer,
259     IWineD3DBaseSwapChainImpl_GetRasterStatus,
260     IWineD3DBaseSwapChainImpl_GetDisplayMode,
261     IWineD3DBaseSwapChainImpl_GetPresentParameters,
262     IWineD3DBaseSwapChainImpl_SetGammaRamp,
263     IWineD3DBaseSwapChainImpl_GetGammaRamp
264 };