2 *IDirect3DSwapChain9 implementation
4 *Copyright 2002-2003 Jason Edmeades
5 *Copyright 2002-2003 Raphael Junqueira
6 *Copyright 2005 Oliver Stieber
8 *This library is free software; you can redistribute it and/or
9 *modify it under the terms of the GNU Lesser General Public
10 *License as published by the Free Software Foundation; either
11 *version 2.1 of the License, or (at your option) any later version.
13 *This library is distributed in the hope that it will be useful,
14 *but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 *Lesser General Public License for more details.
18 *You should have received a copy of the GNU Lesser General Public
19 *License along with this library; if not, write to the Free Software
20 *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
27 /* TODO: move to shared header (or context manager )*/
28 /* x11drv GDI escapes */
29 #define X11DRV_ESCAPE 6789
30 enum x11drv_escape_codes
32 X11DRV_GET_DISPLAY, /* get X11 display for a DC */
33 X11DRV_GET_DRAWABLE, /* get current drawable for a DC */
34 X11DRV_GET_FONT, /* get current X font for a DC */
37 /* retrieve the X display to use on a given DC */
38 inline static Display *get_display( HDC hdc )
41 enum x11drv_escape_codes escape = X11DRV_GET_DISPLAY;
43 if (!ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape,
44 sizeof(display), (LPSTR)&display )) display = NULL;
48 /*TODO: some of the additional parameters may be required to
49 set the gamma ramp (for some weird reason microsoft have left swap gammaramp in device
50 but it operates on a swapchain, it may be a good idea to move it to IWineD3DSwapChain for IWineD3D)*/
53 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
54 WINE_DECLARE_DEBUG_CHANNEL(fps);
57 /* IDirect3DSwapChain IUnknown parts follow: */
58 static ULONG WINAPI IWineD3DSwapChainImpl_AddRef(IWineD3DSwapChain *iface) {
59 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
60 DWORD refCount = InterlockedIncrement(&This->ref);
61 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
65 static HRESULT WINAPI IWineD3DSwapChainImpl_QueryInterface(IWineD3DSwapChain *iface, REFIID riid, LPVOID *ppobj)
67 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
68 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
69 if (IsEqualGUID(riid, &IID_IUnknown)
70 || IsEqualGUID(riid, &IID_IWineD3DBase)
71 || IsEqualGUID(riid, &IID_IWineD3DSwapChain)){
72 IWineD3DSwapChainImpl_AddRef(iface);
74 ERR("Query interface called but now data allocated\n");
85 static ULONG WINAPI IWineD3DSwapChainImpl_Release(IWineD3DSwapChain *iface) {
86 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
88 refCount = InterlockedDecrement(&This->ref);
89 TRACE("(%p) : ReleaseRef to %d\n", This, refCount);
91 IWineD3DSwapChain_Destroy(iface, D3DCB_DefaultDestroySurface);
96 static HRESULT WINAPI IWineD3DSwapChainImpl_GetParent(IWineD3DSwapChain *iface, IUnknown ** ppParent){
97 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
98 *ppParent = This->parent;
99 IUnknown_AddRef(*ppParent);
100 TRACE("(%p) returning %p\n", This , *ppParent);
104 /*IWineD3DSwapChain parts follow: */
105 static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroyRenderTarget) {
106 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
107 WINED3DDISPLAYMODE mode;
109 /* release the ref to the front and back buffer parents */
110 if(This->frontBuffer) {
111 IWineD3DSurface_SetContainer(This->frontBuffer, 0);
112 if(D3DCB_DestroyRenderTarget(This->frontBuffer) > 0) {
113 FIXME("(%p) Something's still holding the front buffer\n",This);
117 if(This->backBuffer) {
119 for(i = 0; i < This->presentParms.BackBufferCount; i++) {
120 IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
121 if(D3DCB_DestroyRenderTarget(This->backBuffer[i]) > 0) {
122 FIXME("(%p) Something's still holding the back buffer\n",This);
127 /* Restore the screen resolution if we rendered in fullscreen
128 * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
129 * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
130 * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
132 if(This->presentParms.Windowed == FALSE) {
133 mode.Width = This->orig_width;
134 mode.Height = This->orig_height;
135 mode.RefreshRate = 0;
136 mode.Format = This->orig_fmt;
137 IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
139 DestroyContext(This->wineD3DDevice, This->context);
141 HeapFree(GetProcessHeap(), 0, This);
144 static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
145 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
149 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
150 if(This->wineD3DDevice->bCursorVisible && This->wineD3DDevice->cursorTexture) {
151 IWineD3DSurfaceImpl cursor;
152 RECT destRect = {This->wineD3DDevice->xScreenSpace - This->wineD3DDevice->xHotSpot,
153 This->wineD3DDevice->yScreenSpace - This->wineD3DDevice->yHotSpot,
154 This->wineD3DDevice->xScreenSpace + This->wineD3DDevice->cursorWidth - This->wineD3DDevice->xHotSpot,
155 This->wineD3DDevice->yScreenSpace + This->wineD3DDevice->cursorHeight - This->wineD3DDevice->yHotSpot};
156 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
157 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
158 * the application because we are only supposed to copy the information out. Using a fake surface
159 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
161 memset(&cursor, 0, sizeof(cursor));
162 cursor.lpVtbl = &IWineD3DSurface_Vtbl;
163 cursor.resource.ref = 1;
164 cursor.resource.wineD3DDevice = This->wineD3DDevice;
165 cursor.resource.pool = WINED3DPOOL_SCRATCH;
166 cursor.resource.format = WINED3DFMT_A8R8G8B8;
167 cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
168 cursor.glDescription.textureName = This->wineD3DDevice->cursorTexture;
169 cursor.glDescription.target = GL_TEXTURE_2D;
170 cursor.glDescription.level = 0;
171 cursor.currentDesc.Width = This->wineD3DDevice->cursorWidth;
172 cursor.currentDesc.Height = This->wineD3DDevice->cursorHeight;
173 cursor.glRect.left = 0;
174 cursor.glRect.top = 0;
175 cursor.glRect.right = cursor.currentDesc.Width;
176 cursor.glRect.bottom = cursor.currentDesc.Height;
177 /* The cursor must have pow2 sizes */
178 cursor.pow2Width = cursor.currentDesc.Width;
179 cursor.pow2Height = cursor.currentDesc.Height;
180 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
181 * which is exactly what we want :-)
183 if (This->presentParms.Windowed) {
184 MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
186 IWineD3DSurface_Blt(This->backBuffer[0], &destRect, (IWineD3DSurface *) &cursor, NULL, DDBLT_KEYSRC, NULL);
189 if (pSourceRect || pDestRect) FIXME("Unhandled present options %p/%p\n", pSourceRect, pDestRect);
190 /* TODO: If only source rect or dest rect are supplied then clip the window to match */
191 TRACE("preseting display %p, drawable %ld\n", This->context->display, This->context->drawable);
193 /* Don't call checkGLcall, as glGetError is not applicable here */
194 if (hDestWindowOverride && This->win_handle != hDestWindowOverride) {
196 WINED3DLOCKED_RECT r;
200 TRACE("Performing dest override of swapchain %p from window %p to %p\n", This, This->win_handle, hDestWindowOverride);
201 if(This->context == This->wineD3DDevice->contexts[0]) {
202 /* The primary context 'owns' all the opengl resources. Destroying and recreating that context would require downloading
203 * all opengl resources, deleting the gl resources, destroying all other contexts, then recreating all other contexts
204 * and reload the resources
206 ERR("Cannot change the destination window of the owner of the primary context\n");
208 hDc = GetDC(hDestWindowOverride);
209 This->win_handle = hDestWindowOverride;
210 This->win = (Window)GetPropA( hDestWindowOverride, "__wine_x11_whole_window" );
211 display = get_display(hDc);
212 ReleaseDC(hDestWindowOverride, hDc);
214 /* The old back buffer has to be copied over to the new back buffer. A lockrect - switchcontext - unlockrect
215 * would suffice in theory, but it is rather nasty and may cause troubles with future changes of the locking code
216 * So lock read only, copy the surface out, then lock with the discard flag and write back
218 IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_READONLY);
219 mem = HeapAlloc(GetProcessHeap(), 0, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
220 memcpy(mem, r.pBits, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
221 IWineD3DSurface_UnlockRect(This->backBuffer[0]);
223 DestroyContext(This->wineD3DDevice, This->context);
224 This->context = CreateContext(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer, display, This->win);
226 IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_DISCARD);
227 memcpy(r.pBits, mem, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
228 HeapFree(GetProcessHeap(), 0, mem);
229 IWineD3DSurface_UnlockRect(This->backBuffer[0]);
233 glXSwapBuffers(This->context->display, This->context->drawable); /* TODO: cycle through the swapchain buffers */
235 TRACE("glXSwapBuffers called, Starting new frame\n");
239 DWORD time = GetTickCount();
241 /* every 1.5 seconds */
242 if (time - This->prev_time > 1500) {
243 TRACE_(fps)("%p @ approx %.2ffps\n", This, 1000.0*This->frames/(time - This->prev_time));
244 This->prev_time = time;
249 #if defined(FRAME_DEBUGGING)
251 if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES) {
254 FIXME("Enabling D3D Trace\n");
255 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 1);
256 #if defined(SHOW_FRAME_MAKEUP)
257 FIXME("Singe Frame snapshots Starting\n");
258 isDumpingFrames = TRUE;
259 glClear(GL_COLOR_BUFFER_BIT);
262 #if defined(SINGLE_FRAME_DEBUGGING)
264 #if defined(SHOW_FRAME_MAKEUP)
265 FIXME("Singe Frame snapshots Finishing\n");
266 isDumpingFrames = FALSE;
268 FIXME("Singe Frame trace complete\n");
269 DeleteFileA("C:\\D3DTRACE");
270 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
276 #if defined(SHOW_FRAME_MAKEUP)
277 FIXME("Single Frame snapshots Finishing\n");
278 isDumpingFrames = FALSE;
280 FIXME("Disabling D3D Trace\n");
281 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
288 /* Although this is not strictly required, a simple demo showed this does occur
289 on (at least non-debug) d3d */
290 if (This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
294 IWineD3DDevice_Clear((IWineD3DDevice*)This->wineD3DDevice, 0, NULL, WINED3DCLEAR_STENCIL|WINED3DCLEAR_ZBUFFER|WINED3DCLEAR_TARGET, 0x00, 1.0, 0);
297 TRACE("Clearing z/stencil buffer\n");
299 IWineD3DDevice_Clear((IWineD3DDevice*)This->wineD3DDevice, 0, NULL, WINED3DCLEAR_STENCIL|WINED3DCLEAR_ZBUFFER, 0x00, 1.0, 0);
302 if(!(((IWineD3DSurfaceImpl *) This->frontBuffer)->Flags & SFLAG_GLDIRTY) ||
303 !(((IWineD3DSurfaceImpl *) This->backBuffer[0])->Flags & SFLAG_GLDIRTY) ) {
304 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying */
305 IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
306 IWineD3DSurfaceImpl *back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
307 BOOL frontdirty = front->Flags & SFLAG_GLDIRTY;
308 BOOL backdirty = back->Flags & SFLAG_GLDIRTY;
314 front->hDC = back->hDC;
318 /* Flip the DIBsection */
321 BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
322 tmp = front->dib.DIBsection;
323 front->dib.DIBsection = back->dib.DIBsection;
324 back->dib.DIBsection = tmp;
326 if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
327 else front->Flags &= ~SFLAG_DIBSECTION;
328 if(hasDib) back->Flags |= SFLAG_DIBSECTION;
329 else back->Flags &= ~SFLAG_DIBSECTION;
332 /* Flip the surface data */
336 tmp = front->dib.bitmap_data;
337 front->dib.bitmap_data = back->dib.bitmap_data;
338 back->dib.bitmap_data = tmp;
340 tmp = front->resource.allocatedMemory;
341 front->resource.allocatedMemory = back->resource.allocatedMemory;
342 back->resource.allocatedMemory = tmp;
345 /* client_memory should not be different, but just in case */
348 tmp = front->dib.client_memory;
349 front->dib.client_memory = back->dib.client_memory;
350 back->dib.client_memory = tmp;
352 if(frontdirty) back->Flags |= SFLAG_GLDIRTY;
353 else back->Flags &= ~SFLAG_GLDIRTY;
354 if(backdirty) front->Flags |= SFLAG_GLDIRTY;
355 else front->Flags &= ~SFLAG_GLDIRTY;
358 TRACE("returning\n");
362 static HRESULT WINAPI IWineD3DSwapChainImpl_GetFrontBufferData(IWineD3DSwapChain *iface, IWineD3DSurface *pDestSurface) {
363 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
366 TRACE("(%p) : iface(%p) pDestSurface(%p)\n", This, iface, pDestSurface);
371 if (This->presentParms.Windowed) {
372 MapWindowPoints(This->win_handle, NULL, &start, 1);
375 IWineD3DSurface_BltFast(pDestSurface, start.x, start.y, This->frontBuffer, NULL, 0);
379 static HRESULT WINAPI IWineD3DSwapChainImpl_GetBackBuffer(IWineD3DSwapChain *iface, UINT iBackBuffer, WINED3DBACKBUFFER_TYPE Type, IWineD3DSurface **ppBackBuffer) {
381 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
383 if (iBackBuffer > This->presentParms.BackBufferCount - 1) {
384 TRACE("Back buffer count out of range\n");
385 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it here
386 * in wined3d to avoid problems in other libs
388 *ppBackBuffer = NULL;
389 return WINED3DERR_INVALIDCALL;
392 *ppBackBuffer = This->backBuffer[iBackBuffer];
393 TRACE("(%p) : BackBuf %d Type %d returning %p\n", This, iBackBuffer, Type, *ppBackBuffer);
395 /* Note inc ref on returned surface */
396 if(*ppBackBuffer) IWineD3DSurface_AddRef(*ppBackBuffer);
401 static HRESULT WINAPI IWineD3DSwapChainImpl_GetRasterStatus(IWineD3DSwapChain *iface, WINED3DRASTER_STATUS *pRasterStatus) {
402 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
403 static BOOL showFixmes = TRUE;
404 pRasterStatus->InVBlank = TRUE;
405 pRasterStatus->ScanLine = 0;
406 /* No openGL equivalent */
408 FIXME("(%p) : stub (once)\n", This);
414 static HRESULT WINAPI IWineD3DSwapChainImpl_GetDisplayMode(IWineD3DSwapChain *iface, WINED3DDISPLAYMODE*pMode) {
415 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
419 pMode->Width = GetSystemMetrics(SM_CXSCREEN);
420 pMode->Height = GetSystemMetrics(SM_CYSCREEN);
421 pMode->RefreshRate = 85; /* FIXME: How to identify? */
424 bpp = GetDeviceCaps(hdc, BITSPIXEL);
428 case 8: pMode->Format = WINED3DFMT_R8G8B8; break;
429 case 16: pMode->Format = WINED3DFMT_R5G6B5; break;
430 case 24: /*pMode->Format = WINED3DFMT_R8G8B8; break; */ /* 32bpp and 24bpp can be aliased for X */
431 case 32: pMode->Format = WINED3DFMT_A8R8G8B8; break;
433 FIXME("Unrecognized display mode format\n");
434 pMode->Format = WINED3DFMT_UNKNOWN;
437 TRACE("(%p) : returning w(%d) h(%d) rr(%d) fmt(%u,%s)\n", This, pMode->Width, pMode->Height, pMode->RefreshRate,
438 pMode->Format, debug_d3dformat(pMode->Format));
442 static HRESULT WINAPI IWineD3DSwapChainImpl_GetDevice(IWineD3DSwapChain *iface, IWineD3DDevice**ppDevice) {
443 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
445 *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
447 /* Note Calling this method will increase the internal reference count
448 on the IDirect3DDevice9 interface. */
449 IWineD3DDevice_AddRef(*ppDevice);
450 TRACE("(%p) : returning %p\n", This, *ppDevice);
454 static HRESULT WINAPI IWineD3DSwapChainImpl_GetPresentParameters(IWineD3DSwapChain *iface, WINED3DPRESENT_PARAMETERS *pPresentationParameters) {
455 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
456 TRACE("(%p)\n", This);
458 *pPresentationParameters = This->presentParms;
463 static HRESULT WINAPI IWineD3DSwapChainImpl_SetGammaRamp(IWineD3DSwapChain *iface, DWORD Flags, CONST WINED3DGAMMARAMP *pRamp){
465 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
467 TRACE("(%p) : pRamp@%p flags(%d)\n", This, pRamp, Flags);
468 hDC = GetDC(This->win_handle);
469 SetDeviceGammaRamp(hDC, (LPVOID)pRamp);
470 ReleaseDC(This->win_handle, hDC);
475 static HRESULT WINAPI IWineD3DSwapChainImpl_GetGammaRamp(IWineD3DSwapChain *iface, WINED3DGAMMARAMP *pRamp){
477 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
479 TRACE("(%p) : pRamp@%p\n", This, pRamp);
480 hDC = GetDC(This->win_handle);
481 GetDeviceGammaRamp(hDC, pRamp);
482 ReleaseDC(This->win_handle, hDC);
488 const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
491 IWineD3DSwapChainImpl_QueryInterface,
492 IWineD3DSwapChainImpl_AddRef,
493 IWineD3DSwapChainImpl_Release,
494 /* IWineD3DSwapChain */
495 IWineD3DSwapChainImpl_GetParent,
496 IWineD3DSwapChainImpl_Destroy,
497 IWineD3DSwapChainImpl_GetDevice,
498 IWineD3DSwapChainImpl_Present,
499 IWineD3DSwapChainImpl_GetFrontBufferData,
500 IWineD3DSwapChainImpl_GetBackBuffer,
501 IWineD3DSwapChainImpl_GetRasterStatus,
502 IWineD3DSwapChainImpl_GetDisplayMode,
503 IWineD3DSwapChainImpl_GetPresentParameters,
504 IWineD3DSwapChainImpl_SetGammaRamp,
505 IWineD3DSwapChainImpl_GetGammaRamp