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 static inline 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;
110 /* release the ref to the front and back buffer parents */
111 if(This->frontBuffer) {
112 IWineD3DSurface_SetContainer(This->frontBuffer, 0);
113 if(D3DCB_DestroyRenderTarget(This->frontBuffer) > 0) {
114 FIXME("(%p) Something's still holding the front buffer\n",This);
118 if(This->backBuffer) {
120 for(i = 0; i < This->presentParms.BackBufferCount; i++) {
121 IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
122 if(D3DCB_DestroyRenderTarget(This->backBuffer[i]) > 0) {
123 FIXME("(%p) Something's still holding the back buffer\n",This);
128 /* Restore the screen resolution if we rendered in fullscreen
129 * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
130 * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
131 * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
133 if(This->presentParms.Windowed == FALSE) {
134 mode.Width = This->orig_width;
135 mode.Height = This->orig_height;
136 mode.RefreshRate = 0;
137 mode.Format = This->orig_fmt;
138 IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
140 for(i = 0; i < This->num_contexts; i++) {
141 DestroyContext(This->wineD3DDevice, This->context[i]);
143 HeapFree(GetProcessHeap(), 0, This->context);
145 HeapFree(GetProcessHeap(), 0, This);
148 static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
149 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
150 DWORD clear_flags = 0;
154 /* Does glXSwapBuffers need a glx context? I don't think so. Blt will activate its own context if needed */
156 /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
157 if(This->wineD3DDevice->bCursorVisible && This->wineD3DDevice->cursorTexture) {
158 IWineD3DSurfaceImpl cursor;
159 RECT destRect = {This->wineD3DDevice->xScreenSpace - This->wineD3DDevice->xHotSpot,
160 This->wineD3DDevice->yScreenSpace - This->wineD3DDevice->yHotSpot,
161 This->wineD3DDevice->xScreenSpace + This->wineD3DDevice->cursorWidth - This->wineD3DDevice->xHotSpot,
162 This->wineD3DDevice->yScreenSpace + This->wineD3DDevice->cursorHeight - This->wineD3DDevice->yHotSpot};
163 TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
164 /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
165 * the application because we are only supposed to copy the information out. Using a fake surface
166 * allows to use the Blitting engine and avoid copying the whole texture -> render target blitting code.
168 memset(&cursor, 0, sizeof(cursor));
169 cursor.lpVtbl = &IWineD3DSurface_Vtbl;
170 cursor.resource.ref = 1;
171 cursor.resource.wineD3DDevice = This->wineD3DDevice;
172 cursor.resource.pool = WINED3DPOOL_SCRATCH;
173 cursor.resource.format = WINED3DFMT_A8R8G8B8;
174 cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
175 cursor.glDescription.textureName = This->wineD3DDevice->cursorTexture;
176 cursor.glDescription.target = GL_TEXTURE_2D;
177 cursor.glDescription.level = 0;
178 cursor.currentDesc.Width = This->wineD3DDevice->cursorWidth;
179 cursor.currentDesc.Height = This->wineD3DDevice->cursorHeight;
180 cursor.glRect.left = 0;
181 cursor.glRect.top = 0;
182 cursor.glRect.right = cursor.currentDesc.Width;
183 cursor.glRect.bottom = cursor.currentDesc.Height;
184 /* The cursor must have pow2 sizes */
185 cursor.pow2Width = cursor.currentDesc.Width;
186 cursor.pow2Height = cursor.currentDesc.Height;
187 /* The surface is in the texture */
188 cursor.Flags |= SFLAG_INTEXTURE;
189 /* DDBLT_KEYSRC will cause BltOverride to enable the alpha test with GL_NOTEQUAL, 0.0,
190 * which is exactly what we want :-)
192 if (This->presentParms.Windowed) {
193 MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
195 IWineD3DSurface_Blt(This->backBuffer[0], &destRect, (IWineD3DSurface *) &cursor, NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_NONE);
198 if (pSourceRect || pDestRect) FIXME("Unhandled present options %p/%p\n", pSourceRect, pDestRect);
199 /* TODO: If only source rect or dest rect are supplied then clip the window to match */
200 TRACE("preseting display %p, drawable %ld\n", This->context[0]->display, This->context[0]->drawable);
202 /* Don't call checkGLcall, as glGetError is not applicable here */
203 if (hDestWindowOverride && This->win_handle != hDestWindowOverride) {
205 WINED3DLOCKED_RECT r;
209 TRACE("Performing dest override of swapchain %p from window %p to %p\n", This, This->win_handle, hDestWindowOverride);
210 if(This->context[0] == This->wineD3DDevice->contexts[0]) {
211 /* The primary context 'owns' all the opengl resources. Destroying and recreating that context would require downloading
212 * all opengl resources, deleting the gl resources, destroying all other contexts, then recreating all other contexts
213 * and reload the resources
215 ERR("Cannot change the destination window of the owner of the primary context\n");
217 hDc = GetDC(hDestWindowOverride);
218 This->win_handle = hDestWindowOverride;
219 This->win = (Window)GetPropA( hDestWindowOverride, "__wine_x11_whole_window" );
220 display = get_display(hDc);
221 ReleaseDC(hDestWindowOverride, hDc);
223 /* The old back buffer has to be copied over to the new back buffer. A lockrect - switchcontext - unlockrect
224 * would suffice in theory, but it is rather nasty and may cause troubles with future changes of the locking code
225 * So lock read only, copy the surface out, then lock with the discard flag and write back
227 IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_READONLY);
228 mem = HeapAlloc(GetProcessHeap(), 0, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
229 memcpy(mem, r.pBits, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
230 IWineD3DSurface_UnlockRect(This->backBuffer[0]);
232 DestroyContext(This->wineD3DDevice, This->context[0]);
233 This->context[0] = CreateContext(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer, display, This->win);
235 IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_DISCARD);
236 memcpy(r.pBits, mem, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
237 HeapFree(GetProcessHeap(), 0, mem);
238 IWineD3DSurface_UnlockRect(This->backBuffer[0]);
242 glXSwapBuffers(This->context[0]->display, This->context[0]->drawable); /* TODO: cycle through the swapchain buffers */
244 TRACE("glXSwapBuffers called, Starting new frame\n");
248 DWORD time = GetTickCount();
250 /* every 1.5 seconds */
251 if (time - This->prev_time > 1500) {
252 TRACE_(fps)("%p @ approx %.2ffps\n", This, 1000.0*This->frames/(time - This->prev_time));
253 This->prev_time = time;
258 #if defined(FRAME_DEBUGGING)
260 if (GetFileAttributesA("C:\\D3DTRACE") != INVALID_FILE_ATTRIBUTES) {
263 FIXME("Enabling D3D Trace\n");
264 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 1);
265 #if defined(SHOW_FRAME_MAKEUP)
266 FIXME("Singe Frame snapshots Starting\n");
267 isDumpingFrames = TRUE;
268 glClear(GL_COLOR_BUFFER_BIT);
271 #if defined(SINGLE_FRAME_DEBUGGING)
273 #if defined(SHOW_FRAME_MAKEUP)
274 FIXME("Singe Frame snapshots Finishing\n");
275 isDumpingFrames = FALSE;
277 FIXME("Singe Frame trace complete\n");
278 DeleteFileA("C:\\D3DTRACE");
279 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
285 #if defined(SHOW_FRAME_MAKEUP)
286 FIXME("Single Frame snapshots Finishing\n");
287 isDumpingFrames = FALSE;
289 FIXME("Disabling D3D Trace\n");
290 __WINE_SET_DEBUGGING(__WINE_DBCL_TRACE, __wine_dbch_d3d, 0);
298 if (This->wineD3DDevice->stencilBufferTarget) {
299 clear_flags |= WINED3DCLEAR_STENCIL|WINED3DCLEAR_ZBUFFER;
302 /* Although this is not strictly required, a simple demo showed this does occur
303 on (at least non-debug) d3d */
304 if (This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
308 IWineD3DDevice_Clear((IWineD3DDevice*)This->wineD3DDevice, 0, NULL, clear_flags|WINED3DCLEAR_TARGET, 0x00, 1.0, 0);
310 } else if (clear_flags) {
311 TRACE("Clearing z/stencil buffer\n");
313 IWineD3DDevice_Clear((IWineD3DDevice*)This->wineD3DDevice, 0, NULL, clear_flags, 0x00, 1.0, 0);
316 if(((IWineD3DSurfaceImpl *) This->frontBuffer)->Flags & SFLAG_INSYSMEM ||
317 ((IWineD3DSurfaceImpl *) This->backBuffer[0])->Flags & SFLAG_INSYSMEM ) {
318 /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying */
319 IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
320 IWineD3DSurfaceImpl *back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
321 BOOL frontuptodate = front->Flags & SFLAG_INSYSMEM;
322 BOOL backuptodate = back->Flags & SFLAG_INSYSMEM;
328 front->hDC = back->hDC;
332 /* Flip the DIBsection */
335 BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
336 tmp = front->dib.DIBsection;
337 front->dib.DIBsection = back->dib.DIBsection;
338 back->dib.DIBsection = tmp;
340 if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
341 else front->Flags &= ~SFLAG_DIBSECTION;
342 if(hasDib) back->Flags |= SFLAG_DIBSECTION;
343 else back->Flags &= ~SFLAG_DIBSECTION;
346 /* Flip the surface data */
350 tmp = front->dib.bitmap_data;
351 front->dib.bitmap_data = back->dib.bitmap_data;
352 back->dib.bitmap_data = tmp;
354 tmp = front->resource.allocatedMemory;
355 front->resource.allocatedMemory = back->resource.allocatedMemory;
356 back->resource.allocatedMemory = tmp;
359 /* client_memory should not be different, but just in case */
362 tmp = front->dib.client_memory;
363 front->dib.client_memory = back->dib.client_memory;
364 back->dib.client_memory = tmp;
366 if(frontuptodate) back->Flags |= SFLAG_INSYSMEM;
367 else back->Flags &= ~SFLAG_INSYSMEM;
368 if(backuptodate) front->Flags |= SFLAG_INSYSMEM;
369 else front->Flags &= ~SFLAG_INSYSMEM;
372 TRACE("returning\n");
376 static HRESULT WINAPI IWineD3DSwapChainImpl_GetFrontBufferData(IWineD3DSwapChain *iface, IWineD3DSurface *pDestSurface) {
377 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
380 TRACE("(%p) : iface(%p) pDestSurface(%p)\n", This, iface, pDestSurface);
385 if (This->presentParms.Windowed) {
386 MapWindowPoints(This->win_handle, NULL, &start, 1);
389 IWineD3DSurface_BltFast(pDestSurface, start.x, start.y, This->frontBuffer, NULL, 0);
393 static HRESULT WINAPI IWineD3DSwapChainImpl_GetBackBuffer(IWineD3DSwapChain *iface, UINT iBackBuffer, WINED3DBACKBUFFER_TYPE Type, IWineD3DSurface **ppBackBuffer) {
395 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
397 if (iBackBuffer > This->presentParms.BackBufferCount - 1) {
398 TRACE("Back buffer count out of range\n");
399 /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it here
400 * in wined3d to avoid problems in other libs
402 *ppBackBuffer = NULL;
403 return WINED3DERR_INVALIDCALL;
406 *ppBackBuffer = This->backBuffer[iBackBuffer];
407 TRACE("(%p) : BackBuf %d Type %d returning %p\n", This, iBackBuffer, Type, *ppBackBuffer);
409 /* Note inc ref on returned surface */
410 if(*ppBackBuffer) IWineD3DSurface_AddRef(*ppBackBuffer);
415 static HRESULT WINAPI IWineD3DSwapChainImpl_GetRasterStatus(IWineD3DSwapChain *iface, WINED3DRASTER_STATUS *pRasterStatus) {
416 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
417 static BOOL showFixmes = TRUE;
418 pRasterStatus->InVBlank = TRUE;
419 pRasterStatus->ScanLine = 0;
420 /* No openGL equivalent */
422 FIXME("(%p) : stub (once)\n", This);
428 static HRESULT WINAPI IWineD3DSwapChainImpl_GetDisplayMode(IWineD3DSwapChain *iface, WINED3DDISPLAYMODE*pMode) {
429 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
433 pMode->Width = GetSystemMetrics(SM_CXSCREEN);
434 pMode->Height = GetSystemMetrics(SM_CYSCREEN);
435 pMode->RefreshRate = 85; /* FIXME: How to identify? */
438 bpp = GetDeviceCaps(hdc, BITSPIXEL);
442 case 8: pMode->Format = WINED3DFMT_R8G8B8; break;
443 case 16: pMode->Format = WINED3DFMT_R5G6B5; break;
444 case 24: /*pMode->Format = WINED3DFMT_R8G8B8; break; */ /* 32bpp and 24bpp can be aliased for X */
445 case 32: pMode->Format = WINED3DFMT_A8R8G8B8; break;
447 FIXME("Unrecognized display mode format\n");
448 pMode->Format = WINED3DFMT_UNKNOWN;
451 TRACE("(%p) : returning w(%d) h(%d) rr(%d) fmt(%u,%s)\n", This, pMode->Width, pMode->Height, pMode->RefreshRate,
452 pMode->Format, debug_d3dformat(pMode->Format));
456 static HRESULT WINAPI IWineD3DSwapChainImpl_GetDevice(IWineD3DSwapChain *iface, IWineD3DDevice**ppDevice) {
457 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
459 *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
461 /* Note Calling this method will increase the internal reference count
462 on the IDirect3DDevice9 interface. */
463 IWineD3DDevice_AddRef(*ppDevice);
464 TRACE("(%p) : returning %p\n", This, *ppDevice);
468 static HRESULT WINAPI IWineD3DSwapChainImpl_GetPresentParameters(IWineD3DSwapChain *iface, WINED3DPRESENT_PARAMETERS *pPresentationParameters) {
469 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
470 TRACE("(%p)\n", This);
472 *pPresentationParameters = This->presentParms;
477 static HRESULT WINAPI IWineD3DSwapChainImpl_SetGammaRamp(IWineD3DSwapChain *iface, DWORD Flags, CONST WINED3DGAMMARAMP *pRamp){
479 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
481 TRACE("(%p) : pRamp@%p flags(%d)\n", This, pRamp, Flags);
482 hDC = GetDC(This->win_handle);
483 SetDeviceGammaRamp(hDC, (LPVOID)pRamp);
484 ReleaseDC(This->win_handle, hDC);
489 static HRESULT WINAPI IWineD3DSwapChainImpl_GetGammaRamp(IWineD3DSwapChain *iface, WINED3DGAMMARAMP *pRamp){
491 IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
493 TRACE("(%p) : pRamp@%p\n", This, pRamp);
494 hDC = GetDC(This->win_handle);
495 GetDeviceGammaRamp(hDC, pRamp);
496 ReleaseDC(This->win_handle, hDC);
502 const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
505 IWineD3DSwapChainImpl_QueryInterface,
506 IWineD3DSwapChainImpl_AddRef,
507 IWineD3DSwapChainImpl_Release,
508 /* IWineD3DSwapChain */
509 IWineD3DSwapChainImpl_GetParent,
510 IWineD3DSwapChainImpl_Destroy,
511 IWineD3DSwapChainImpl_GetDevice,
512 IWineD3DSwapChainImpl_Present,
513 IWineD3DSwapChainImpl_GetFrontBufferData,
514 IWineD3DSwapChainImpl_GetBackBuffer,
515 IWineD3DSwapChainImpl_GetRasterStatus,
516 IWineD3DSwapChainImpl_GetDisplayMode,
517 IWineD3DSwapChainImpl_GetPresentParameters,
518 IWineD3DSwapChainImpl_SetGammaRamp,
519 IWineD3DSwapChainImpl_GetGammaRamp