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