DXGL r446 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r445‎ | r446 | r447 >
Date:18:17, 25 May 2014
Author:admin
Status:new
Tags:
Comment:
Use temporary buffer when blitting with same source and destination surface.
Modified paths:
  • /ddraw/glDirectDraw.cpp (modified) (history)
  • /ddraw/glDirectDraw.h (modified) (history)
  • /ddraw/glDirectDrawSurface.cpp (modified) (history)

Diff [purge]

Index: ddraw/glDirectDraw.cpp
@@ -835,13 +835,14 @@
836836 if(!lpDDSurfaceDesc2) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
837837 if(pUnkOuter) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
838838 if(lpDDSurfaceDesc2->dwSize < sizeof(DDSURFACEDESC2)) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
839 - TRACE_RET(HRESULT,23,CreateSurface2(lpDDSurfaceDesc2,lplpDDSurface,pUnkOuter));
 839+ TRACE_RET(HRESULT,23,CreateSurface2(lpDDSurfaceDesc2,lplpDDSurface,pUnkOuter,TRUE));
840840 }
841841
842842
843 -HRESULT glDirectDraw7::CreateSurface2(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRECTDRAWSURFACE7 FAR *lplpDDSurface, IUnknown FAR *pUnkOuter)
 843+HRESULT glDirectDraw7::CreateSurface2(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRECTDRAWSURFACE7 FAR *lplpDDSurface, IUnknown FAR *pUnkOuter, BOOL RecordSurface)
844844 {
845 - TRACE_ENTER(4,14,this,14,lpDDSurfaceDesc2,14,lplpDDSurface,14,pUnkOuter);
 845+ HRESULT error;
 846+ TRACE_ENTER(5, 14, this, 14, lpDDSurfaceDesc2, 14, lplpDDSurface, 14, pUnkOuter, 22, RecordSurface);
846847 if(!this) TRACE_RET(HRESULT,23,DDERR_INVALIDOBJECT);
847848 if(!lpDDSurfaceDesc2) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
848849 if(pUnkOuter) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
@@ -857,24 +858,31 @@
858859 }
859860 else TRACE_RET(HRESULT,23,DDERR_PRIMARYSURFACEALREADYEXISTS);
860861 }
861 - surfacecount++;
862 - if(surfacecount > surfacecountmax)
 862+ if (RecordSurface)
863863 {
864 - glDirectDrawSurface7 **surfaces2;
865 - surfaces2 = (glDirectDrawSurface7 **)realloc(surfaces,(surfacecountmax+1024)*sizeof(glDirectDrawSurface7 *));
866 - if(!surfaces2) TRACE_RET(HRESULT,23,DDERR_OUTOFMEMORY);
867 - surfaces = surfaces2;
868 - ZeroMemory(&surfaces[surfacecountmax],1024*sizeof(glDirectDrawSurface7 *));
869 - surfacecountmax += 1024;
 864+ surfacecount++;
 865+ if (surfacecount > surfacecountmax)
 866+ {
 867+ glDirectDrawSurface7 **surfaces2;
 868+ surfaces2 = (glDirectDrawSurface7 **)realloc(surfaces, (surfacecountmax + 1024)*sizeof(glDirectDrawSurface7 *));
 869+ if (!surfaces2) TRACE_RET(HRESULT, 23, DDERR_OUTOFMEMORY);
 870+ surfaces = surfaces2;
 871+ ZeroMemory(&surfaces[surfacecountmax], 1024 * sizeof(glDirectDrawSurface7 *));
 872+ surfacecountmax += 1024;
 873+ }
 874+ surfaces[surfacecount - 1] = new glDirectDrawSurface7(this, lpDDSurfaceDesc2, &error, false, NULL);
 875+ if (lpDDSurfaceDesc2->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
 876+ {
 877+ primary = surfaces[surfacecount - 1];
 878+ primarylost = false;
 879+ }
 880+ *lplpDDSurface = surfaces[surfacecount - 1];
870881 }
871 - HRESULT error;
872 - surfaces[surfacecount-1] = new glDirectDrawSurface7(this,lpDDSurfaceDesc2,&error,false,NULL);
873 - if(lpDDSurfaceDesc2->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)
 882+ else
874883 {
875 - primary = surfaces[surfacecount-1];
876 - primarylost = false;
 884+ if (lpDDSurfaceDesc2->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) TRACE_RET(23, HRESULT, DDERR_INVALIDPARAMS);
 885+ *lplpDDSurface = new glDirectDrawSurface7(this, lpDDSurfaceDesc2, &error, false, NULL);
877886 }
878 - *lplpDDSurface = surfaces[surfacecount-1];
879887 TRACE_VAR("*lplpDDSurface",14,*lplpDDSurface);
880888 TRACE_EXIT(23,error);
881889 return error;
@@ -1152,6 +1160,7 @@
11531161 ZeroMemory(surfaces,1024*sizeof(glDirectDrawSurface7 *));
11541162 surfacecount = 0;
11551163 surfacecountmax = 1024;
 1164+ tmpsurface = NULL;
11561165 clippers = (glDirectDrawClipper **)malloc(1024*sizeof(glDirectDrawClipper *));
11571166 if(!clippers) TRACE_RET(HRESULT,23,DDERR_OUTOFMEMORY);
11581167 ZeroMemory(clippers,1024*sizeof(glDirectDrawClipper *));
@@ -1679,7 +1688,11 @@
16801689 TRACE_ENTER(2,14,this,14,surface);
16811690 for(int i = 0; i < surfacecount; i++)
16821691 if(surfaces[i] == surface) surfaces[i] = NULL;
1683 - if(surface == primary) primary = NULL;
 1692+ if (surface == primary)
 1693+ {
 1694+ primary = NULL;
 1695+ DeleteTempSurface();
 1696+ }
16841697 TRACE_EXIT(0,0);
16851698 }
16861699
@@ -1691,6 +1704,60 @@
16921705 TRACE_EXIT(0,0);
16931706 }
16941707
 1708+HRESULT glDirectDraw7::SetupTempSurface(DWORD width, DWORD height)
 1709+{
 1710+ DDSURFACEDESC2 ddsd;
 1711+ HRESULT error;
 1712+ if (!width || !height) return DDERR_INVALIDPARAMS;
 1713+ if (!tmpsurface)
 1714+ {
 1715+ ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
 1716+ ddsd.dwSize = sizeof(DDSURFACEDESC2);
 1717+ ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
 1718+ ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
 1719+ ddsd.dwWidth = width;
 1720+ ddsd.dwHeight = height;
 1721+ error = CreateSurface2(&ddsd, (LPDIRECTDRAWSURFACE7*)&tmpsurface, NULL, FALSE);
 1722+ if (error == DDERR_OUTOFVIDEOMEMORY)
 1723+ {
 1724+ ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
 1725+ error = CreateSurface2(&ddsd, (LPDIRECTDRAWSURFACE7*)&tmpsurface, NULL, FALSE);
 1726+ }
 1727+ if (error != DD_OK) return error;
 1728+ }
 1729+ else
 1730+ {
 1731+ if ((tmpsurface->ddsd.dwWidth >= width) && (tmpsurface->ddsd.dwHeight >= height)) return DD_OK;
 1732+ else
 1733+ {
 1734+ ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
 1735+ ddsd.dwSize = sizeof(DDSURFACEDESC2);
 1736+ ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
 1737+ ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
 1738+ ddsd.dwWidth = width > tmpsurface->ddsd.dwWidth ? width : tmpsurface->ddsd.dwWidth;
 1739+ ddsd.dwHeight = height > tmpsurface->ddsd.dwHeight ? height : tmpsurface->ddsd.dwHeight;
 1740+ tmpsurface->Release();
 1741+ tmpsurface = NULL;
 1742+ error = CreateSurface(&ddsd, (LPDIRECTDRAWSURFACE7*)&tmpsurface, NULL);
 1743+ if (error == DDERR_OUTOFVIDEOMEMORY)
 1744+ {
 1745+ ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
 1746+ error = CreateSurface(&ddsd, (LPDIRECTDRAWSURFACE7*)&tmpsurface, NULL);
 1747+ }
 1748+ if (error != DD_OK) return error;
 1749+ }
 1750+ }
 1751+ return DD_OK;
 1752+}
 1753+
 1754+void glDirectDraw7::DeleteTempSurface()
 1755+{
 1756+ if (tmpsurface)
 1757+ {
 1758+ tmpsurface->Release();
 1759+ tmpsurface = 0;
 1760+ }
 1761+}
16951762 // DDRAW1 wrapper
16961763 glDirectDraw1::glDirectDraw1(glDirectDraw7 *gl_DD7)
16971764 {
@@ -1766,7 +1833,7 @@
17671834 if(!lpDDSurfaceDesc) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
17681835 if(lpDDSurfaceDesc->dwSize < sizeof(DDSURFACEDESC)) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
17691836 LPDIRECTDRAWSURFACE7 lpDDS7;
1770 - HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
 1837+ HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter,TRUE);
17711838 if(err == DD_OK)
17721839 {
17731840 lpDDS7->QueryInterface(IID_IDirectDrawSurface,(LPVOID*) lplpDDSurface);
@@ -1954,7 +2021,7 @@
19552022 if(!lpDDSurfaceDesc) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
19562023 if(lpDDSurfaceDesc->dwSize < sizeof(DDSURFACEDESC)) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
19572024 LPDIRECTDRAWSURFACE7 lpDDS7;
1958 - HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
 2025+ HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter,TRUE);
19592026 if(err == DD_OK)
19602027 {
19612028 lpDDS7->QueryInterface(IID_IDirectDrawSurface,(LPVOID*) lplpDDSurface);
@@ -2165,7 +2232,7 @@
21662233 if(!lpDDSurfaceDesc) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
21672234 if(lpDDSurfaceDesc->dwSize < sizeof(DDSURFACEDESC2)) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
21682235 LPDIRECTDRAWSURFACE7 lpDDS7;
2169 - HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter);
 2236+ HRESULT err = glDD7->CreateSurface2((LPDDSURFACEDESC2)lpDDSurfaceDesc,&lpDDS7,pUnkOuter,TRUE);
21702237 if(err == DD_OK)
21712238 {
21722239 lpDDS7->QueryInterface(IID_IDirectDrawSurface4,(LPVOID*) lplpDDSurface);
Index: ddraw/glDirectDraw.h
@@ -19,6 +19,8 @@
2020 #ifndef _GLDIRECTDRAW_H
2121 #define _GLDIRECTDRAW_H
2222
 23+#define DXGLBLT_NOPALSHADER 0x80000000
 24+
2325 class glDirectDrawSurface7;
2426 class glDirectDrawClipper;
2527 class glDirect3D7;
@@ -70,7 +72,7 @@
7173 HRESULT WINAPI EvaluateMode(DWORD dwFlags, DWORD *pSecondsUntilTimeout);
7274
7375 // internal functions
74 - HRESULT CreateSurface2(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRECTDRAWSURFACE7 FAR *lplpDDSurface, IUnknown FAR *pUnkOuter);
 76+ HRESULT CreateSurface2(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRECTDRAWSURFACE7 FAR *lplpDDSurface, IUnknown FAR *pUnkOuter, BOOL RecordSurface);
7577 HRESULT err() {return error;}
7678 void RemoveSurface(glDirectDrawSurface7 *surface);
7779 void GetSizes(LONG *sizes);
@@ -82,6 +84,8 @@
8385 bool GetFullscreen(){return fullscreen;};
8486 void DeleteSurface(glDirectDrawSurface7 *surface);
8587 void DeleteClipper(glDirectDrawClipper *clipper);
 88+ HRESULT SetupTempSurface(DWORD width, DWORD height);
 89+ void DeleteTempSurface();
8690 glDirectDrawSurface7 *primary;
8791 bool primarylost;
8892 bool lastsync;
@@ -90,6 +94,7 @@
9195 glDirectDraw4 *glDD4;
9296 DDDEVICEIDENTIFIER2 devid;
9397 glRenderer *renderer;
 98+ glDirectDrawSurface7 *tmpsurface;
9499 private:
95100 HRESULT error;
96101 ULONG refcount;
Index: ddraw/glDirectDrawSurface.cpp
@@ -607,6 +607,8 @@
608608 }
609609 HRESULT WINAPI glDirectDrawSurface7::Blt(LPRECT lpDestRect, LPDIRECTDRAWSURFACE7 lpDDSrcSurface, LPRECT lpSrcRect, DWORD dwFlags, LPDDBLTFX lpDDBltFx)
610610 {
 611+ HRESULT error;
 612+ RECT tmprect;
611613 TRACE_ENTER(6,14,this,26,lpDestRect,14,lpDDSrcSurface,26,lpSrcRect,9,dwFlags,14,lpDDBltFx);
612614 if(!this) TRACE_RET(HRESULT,23,DDERR_INVALIDOBJECT);
613615 if((dwFlags & DDBLT_COLORFILL) && !lpDDBltFx) TRACE_RET(HRESULT,23,DDERR_INVALIDPARAMS);
@@ -626,7 +628,18 @@
627629 src->ddsd.ddpfPixelFormat.dwRGBBitCount);
628630 src->dirty &= ~1;
629631 }
630 - TRACE_RET(HRESULT,23,ddInterface->renderer->Blt(lpDestRect,src,this,lpSrcRect,dwFlags,lpDDBltFx));
 632+ if (this == src)
 633+ {
 634+ tmprect.left = tmprect.top = 0;
 635+ tmprect.right = lpSrcRect->right - lpSrcRect->left;
 636+ tmprect.bottom = lpSrcRect->bottom - lpSrcRect->top;
 637+ error = ddInterface->SetupTempSurface(tmprect.right, tmprect.bottom);
 638+ if (error) TRACE_RET(HRESULT, 23, error);
 639+ error = ddInterface->tmpsurface->Blt(&tmprect, lpDDSrcSurface, lpSrcRect, 0, NULL);
 640+ if (error) TRACE_RET(HRESULT, 23, error);
 641+ TRACE_RET(HRESULT,23,this->Blt(lpDestRect, ddInterface->tmpsurface, &tmprect, dwFlags, lpDDBltFx));
 642+ }
 643+ else TRACE_RET(HRESULT,23,ddInterface->renderer->Blt(lpDestRect,src,this,lpSrcRect,dwFlags,lpDDBltFx));
631644 }
632645 HRESULT WINAPI glDirectDrawSurface7::BltBatch(LPDDBLTBATCH lpDDBltBatch, DWORD dwCount, DWORD dwFlags)
633646 {