dinput: Avoid size_t in traces.
[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, D3DCB_DESTROYSURFACEFN D3DCB_DestroyRenderback) {
31     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
32     WINED3DDISPLAYMODE mode;
33
34     TRACE("Destroying swapchain %p\n", iface);
35
36     IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
37
38     /* release the ref to the front and back buffer parents */
39     if(This->frontBuffer) {
40         IWineD3DSurface_SetContainer(This->frontBuffer, 0);
41         if(D3DCB_DestroyRenderback(This->frontBuffer) > 0) {
42             FIXME("(%p) Something's still holding the front buffer\n",This);
43         }
44     }
45
46     if(This->backBuffer) {
47         UINT i;
48         for(i = 0; i < This->presentParms.BackBufferCount; i++) {
49             IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
50             if(D3DCB_DestroyRenderback(This->backBuffer[i]) > 0) {
51                 FIXME("(%p) Something's still holding the back buffer\n",This);
52             }
53         }
54         HeapFree(GetProcessHeap(), 0, This->backBuffer);
55     }
56
57     /* Restore the screen resolution if we rendered in fullscreen
58      * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
59      * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
60      * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
61      */
62     if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
63         mode.Width = This->orig_width;
64         mode.Height = This->orig_height;
65         mode.RefreshRate = 0;
66         mode.Format = This->orig_fmt;
67         IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
68     }
69
70     HeapFree(GetProcessHeap(), 0, This);
71 }
72
73 /*****************************************************************************
74  * x11_copy_to_screen
75  *
76  * Helper function that blts the front buffer contents to the target window
77  *
78  * Params:
79  *  This: Surface to copy from
80  *  rc: Rectangle to copy
81  *
82  *****************************************************************************/
83 void x11_copy_to_screen(IWineD3DSwapChainImpl *This, const RECT *rc)
84 {
85     IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
86
87     if(front->resource.usage & WINED3DUSAGE_RENDERTARGET) {
88         POINT offset = {0,0};
89         HWND hDisplayWnd;
90         HDC hDisplayDC;
91         HDC hSurfaceDC = 0;
92         RECT drawrect;
93         TRACE("(%p)->(%p): Copying to screen\n", front, rc);
94
95         hSurfaceDC = front->hDC;
96
97         hDisplayWnd = This->win_handle;
98         hDisplayDC = GetDCEx(hDisplayWnd, 0, DCX_CLIPSIBLINGS|DCX_CACHE);
99         if(rc) {
100             TRACE(" copying rect (%d,%d)->(%d,%d), offset (%d,%d)\n",
101                   rc->left, rc->top, rc->right, rc->bottom, offset.x, offset.y);
102         }
103
104         /* Front buffer coordinates are screen coordinates. Map them to the destination
105          * window if not fullscreened
106          */
107         if(This->presentParms.Windowed) {
108             ClientToScreen(hDisplayWnd, &offset);
109         }
110 #if 0
111         /* FIXME: This doesn't work... if users really want to run
112         * X in 8bpp, then we need to call directly into display.drv
113         * (or Wine's equivalent), and force a private colormap
114         * without default entries. */
115         if (front->palette) {
116         SelectPalette(hDisplayDC, front->palette->hpal, FALSE);
117         RealizePalette(hDisplayDC); /* sends messages => deadlocks */
118     }
119 #endif
120         drawrect.left   = 0;
121         drawrect.right  = front->currentDesc.Width;
122         drawrect.top    = 0;
123         drawrect.bottom = front->currentDesc.Height;
124
125 #if 0
126         /* TODO: Support clippers */
127         if (front->clipper)
128         {
129         RECT xrc;
130         HWND hwnd = ((IWineD3DClipperImpl *) front->clipper)->hWnd;
131         if (hwnd && GetClientRect(hwnd,&xrc))
132         {
133         OffsetRect(&xrc,offset.x,offset.y);
134         IntersectRect(&drawrect,&drawrect,&xrc);
135     }
136     }
137 #endif
138         if (rc) {
139             IntersectRect(&drawrect,&drawrect,rc);
140         }
141         else {
142             /* Only use this if the caller did not pass a rectangle, since
143             * due to double locking this could be the wrong one ...
144             */
145             if (front->lockedRect.left != front->lockedRect.right) {
146                 IntersectRect(&drawrect,&drawrect,&front->lockedRect);
147             }
148         }
149
150         BitBlt(hDisplayDC,
151                drawrect.left-offset.x, drawrect.top-offset.y,
152                drawrect.right-drawrect.left, drawrect.bottom-drawrect.top,
153                hSurfaceDC,
154                drawrect.left, drawrect.top,
155                SRCCOPY);
156         ReleaseDC(hDisplayWnd, hDisplayDC);
157     }
158 }
159
160 static HRESULT WINAPI IWineGDISwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window) {
161     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
162
163     This->win_handle = window;
164     return WINED3D_OK;
165 }
166
167 static HRESULT WINAPI IWineGDISwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
168     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
169     IWineD3DSurfaceImpl *front, *back;
170
171     if(!This->backBuffer) {
172         WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
173         return WINED3DERR_INVALIDCALL;
174     }
175     front = (IWineD3DSurfaceImpl *) This->frontBuffer;
176     back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
177
178     /* Flip the DC */
179     {
180         HDC tmp;
181         tmp = front->hDC;
182         front->hDC = back->hDC;
183         back->hDC = tmp;
184     }
185
186     /* Flip the DIBsection */
187     {
188         HBITMAP tmp;
189         tmp = front->dib.DIBsection;
190         front->dib.DIBsection = back->dib.DIBsection;
191         back->dib.DIBsection = tmp;
192     }
193
194     /* Flip the surface data */
195     {
196         void* tmp;
197
198         tmp = front->dib.bitmap_data;
199         front->dib.bitmap_data = back->dib.bitmap_data;
200         back->dib.bitmap_data = tmp;
201
202         tmp = front->resource.allocatedMemory;
203         front->resource.allocatedMemory = back->resource.allocatedMemory;
204         back->resource.allocatedMemory = tmp;
205
206         if(front->resource.heapMemory) {
207             ERR("GDI Surface %p has heap memory allocated\n", front);
208         }
209         if(back->resource.heapMemory) {
210             ERR("GDI Surface %p has heap memory allocated\n", back);
211         }
212     }
213
214     /* client_memory should not be different, but just in case */
215     {
216         BOOL tmp;
217         tmp = front->dib.client_memory;
218         front->dib.client_memory = back->dib.client_memory;
219         back->dib.client_memory = tmp;
220     }
221
222     /* FPS support */
223     if (TRACE_ON(fps))
224     {
225         static long prev_time, frames;
226
227         DWORD time = GetTickCount();
228         frames++;
229         /* every 1.5 seconds */
230         if (time - prev_time > 1500) {
231             TRACE_(fps)("@ approx %.2ffps\n", 1000.0*frames/(time - prev_time));
232             prev_time = time;
233             frames = 0;
234         }
235     }
236
237     x11_copy_to_screen(This, NULL);
238
239     return WINED3D_OK;
240 }
241
242 const IWineD3DSwapChainVtbl IWineGDISwapChain_Vtbl =
243 {
244     /* IUnknown */
245     IWineD3DBaseSwapChainImpl_QueryInterface,
246     IWineD3DBaseSwapChainImpl_AddRef,
247     IWineD3DBaseSwapChainImpl_Release,
248     /* IWineD3DSwapChain */
249     IWineD3DBaseSwapChainImpl_GetParent,
250     IWineGDISwapChainImpl_Destroy,
251     IWineD3DBaseSwapChainImpl_GetDevice,
252     IWineGDISwapChainImpl_Present,
253     IWineGDISwapChainImpl_SetDestWindowOverride,
254     IWineD3DBaseSwapChainImpl_GetFrontBufferData,
255     IWineD3DBaseSwapChainImpl_GetBackBuffer,
256     IWineD3DBaseSwapChainImpl_GetRasterStatus,
257     IWineD3DBaseSwapChainImpl_GetDisplayMode,
258     IWineD3DBaseSwapChainImpl_GetPresentParameters,
259     IWineD3DBaseSwapChainImpl_SetGammaRamp,
260     IWineD3DBaseSwapChainImpl_GetGammaRamp
261 };