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