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