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