2 *IDirect3DSwapChain9 implementation
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
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.
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.
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
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
28 WINE_DECLARE_DEBUG_CHANNEL(fps
);
30 static void WINAPI
IWineGDISwapChainImpl_Destroy(IWineD3DSwapChain
*iface
)
32 IWineD3DSwapChainImpl
*This
= (IWineD3DSwapChainImpl
*)iface
;
33 WINED3DDISPLAYMODE mode
;
35 TRACE("Destroying swapchain %p\n", iface
);
37 IWineD3DSwapChain_SetGammaRamp(iface
, 0, &This
->orig_gamma
);
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)
44 WARN("(%p) Something's still holding the front buffer\n",This
);
48 if(This
->backBuffer
) {
50 for(i
= 0; i
< This
->presentParms
.BackBufferCount
; i
++) {
51 IWineD3DSurface_SetContainer(This
->backBuffer
[i
], 0);
52 if (IWineD3DSurface_Release(This
->backBuffer
[i
]) > 0)
54 WARN("(%p) Something's still holding the back buffer\n",This
);
57 HeapFree(GetProcessHeap(), 0, This
->backBuffer
);
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
65 if(This
->presentParms
.Windowed
== FALSE
&& This
->presentParms
.AutoRestoreDisplayMode
) {
66 mode
.Width
= This
->orig_width
;
67 mode
.Height
= This
->orig_height
;
69 mode
.Format
= This
->orig_fmt
;
70 IWineD3DDevice_SetDisplayMode((IWineD3DDevice
*)This
->device
, 0, &mode
);
73 wined3d_unregister_window(This
->win_handle
);
75 HeapFree(GetProcessHeap(), 0, This
->context
);
76 HeapFree(GetProcessHeap(), 0, This
);
79 /*****************************************************************************
82 * Helper function that blts the front buffer contents to the target window
85 * This: Surface to copy from
86 * rc: Rectangle to copy
88 *****************************************************************************/
89 void x11_copy_to_screen(IWineD3DSwapChainImpl
*This
, const RECT
*rc
)
91 IWineD3DSurfaceImpl
*front
= (IWineD3DSurfaceImpl
*) This
->frontBuffer
;
93 if(front
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
) {
99 TRACE("(%p)->(%p): Copying to screen\n", front
, rc
);
101 hSurfaceDC
= front
->hDC
;
103 hDisplayWnd
= This
->win_handle
;
104 hDisplayDC
= GetDCEx(hDisplayWnd
, 0, DCX_CLIPSIBLINGS
|DCX_CACHE
);
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
);
110 /* Front buffer coordinates are screen coordinates. Map them to the destination
111 * window if not fullscreened
113 if(This
->presentParms
.Windowed
) {
114 ClientToScreen(hDisplayWnd
, &offset
);
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 */
127 drawrect
.right
= front
->currentDesc
.Width
;
129 drawrect
.bottom
= front
->currentDesc
.Height
;
132 /* TODO: Support clippers */
136 HWND hwnd
= ((IWineD3DClipperImpl
*) front
->clipper
)->hWnd
;
137 if (hwnd
&& GetClientRect(hwnd
,&xrc
))
139 OffsetRect(&xrc
,offset
.x
,offset
.y
);
140 IntersectRect(&drawrect
,&drawrect
,&xrc
);
145 IntersectRect(&drawrect
,&drawrect
,rc
);
148 /* Only use this if the caller did not pass a rectangle, since
149 * due to double locking this could be the wrong one ...
151 if (front
->lockedRect
.left
!= front
->lockedRect
.right
) {
152 IntersectRect(&drawrect
,&drawrect
,&front
->lockedRect
);
157 drawrect
.left
-offset
.x
, drawrect
.top
-offset
.y
,
158 drawrect
.right
-drawrect
.left
, drawrect
.bottom
-drawrect
.top
,
160 drawrect
.left
, drawrect
.top
,
162 ReleaseDC(hDisplayWnd
, hDisplayDC
);
166 static HRESULT WINAPI
IWineGDISwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain
*iface
, HWND window
) {
167 IWineD3DSwapChainImpl
*This
= (IWineD3DSwapChainImpl
*)iface
;
169 This
->win_handle
= window
;
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
;
177 if(!This
->backBuffer
) {
178 WARN("Swapchain doesn't have a backbuffer, returning WINED3DERR_INVALIDCALL\n");
179 return WINED3DERR_INVALIDCALL
;
181 front
= (IWineD3DSurfaceImpl
*) This
->frontBuffer
;
182 back
= (IWineD3DSurfaceImpl
*) This
->backBuffer
[0];
188 front
->hDC
= back
->hDC
;
192 /* Flip the DIBsection */
195 tmp
= front
->dib
.DIBsection
;
196 front
->dib
.DIBsection
= back
->dib
.DIBsection
;
197 back
->dib
.DIBsection
= tmp
;
200 /* Flip the surface data */
204 tmp
= front
->dib
.bitmap_data
;
205 front
->dib
.bitmap_data
= back
->dib
.bitmap_data
;
206 back
->dib
.bitmap_data
= tmp
;
208 tmp
= front
->resource
.allocatedMemory
;
209 front
->resource
.allocatedMemory
= back
->resource
.allocatedMemory
;
210 back
->resource
.allocatedMemory
= tmp
;
212 if(front
->resource
.heapMemory
) {
213 ERR("GDI Surface %p has heap memory allocated\n", front
);
215 if(back
->resource
.heapMemory
) {
216 ERR("GDI Surface %p has heap memory allocated\n", back
);
220 /* client_memory should not be different, but just in case */
223 tmp
= front
->dib
.client_memory
;
224 front
->dib
.client_memory
= back
->dib
.client_memory
;
225 back
->dib
.client_memory
= tmp
;
231 static long prev_time
, frames
;
233 DWORD time
= GetTickCount();
235 /* every 1.5 seconds */
236 if (time
- prev_time
> 1500) {
237 TRACE_(fps
)("@ approx %.2ffps\n", 1000.0*frames
/(time
- prev_time
));
243 x11_copy_to_screen(This
, NULL
);
248 const IWineD3DSwapChainVtbl IWineGDISwapChain_Vtbl
=
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