Skip to content

Commit 2270ed0

Browse files
committed
Viewer: reuse back buffer during repaint
1 parent a34324a commit 2270ed0

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

Viewer/ViewerView.cpp

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ CViewerView::CViewerView()
191191
, mNewRgbBufferInfoQ(new SSafeCQ<BufferInfo>(1))
192192
, mBufferPool(NULL)
193193
, mKeyProcessing(false)
194+
, mPrevBackBitmap(NULL)
195+
, mBackW(0)
196+
, mBackH(0)
194197
, mXCursor(-1)
195198
, mYCursor(-1)
196199
, mSelMode(false)
@@ -268,6 +271,7 @@ CViewerView::~CViewerView()
268271
delete mNewRgbBufferInfoQ;
269272

270273
mMouseMenu.DestroyMenu();
274+
ReleaseBackBuffer();
271275

272276
if (mRgbBuf)
273277
_mm_free(mRgbBuf);
@@ -293,6 +297,48 @@ BOOL CViewerView::PreCreateWindow(CREATESTRUCT& cs)
293297
return CView::PreCreateWindow(cs);
294298
}
295299

300+
bool CViewerView::EnsureBackBuffer(CDC *pDC)
301+
{
302+
if (mWClient <= 0 || mHClient <= 0)
303+
return false;
304+
305+
if (mBackDC.GetSafeHdc() && mBackW == mWClient && mBackH == mHClient)
306+
return true;
307+
308+
ReleaseBackBuffer();
309+
310+
if (!mBackDC.CreateCompatibleDC(pDC))
311+
return false;
312+
313+
if (!mBackBitmap.CreateCompatibleBitmap(pDC, mWClient, mHClient)) {
314+
mBackDC.DeleteDC();
315+
return false;
316+
}
317+
318+
mPrevBackBitmap = mBackDC.SelectObject(&mBackBitmap);
319+
mBackW = mWClient;
320+
mBackH = mHClient;
321+
322+
return true;
323+
}
324+
325+
void CViewerView::ReleaseBackBuffer()
326+
{
327+
if (mBackDC.GetSafeHdc()) {
328+
if (mPrevBackBitmap) {
329+
mBackDC.SelectObject(mPrevBackBitmap);
330+
mPrevBackBitmap = NULL;
331+
}
332+
mBackDC.DeleteDC();
333+
}
334+
335+
if (mBackBitmap.GetSafeHandle())
336+
mBackBitmap.DeleteObject();
337+
338+
mBackW = 0;
339+
mBackH = 0;
340+
}
341+
296342
void CViewerView::AdjustWindowSize()
297343
{
298344
CViewerDoc* pDoc = GetDocument();
@@ -1133,13 +1179,10 @@ void CViewerView::OnDraw(CDC *pDC)
11331179
if (!pDoc)
11341180
return;
11351181

1136-
CDC memDC;
1137-
memDC.CreateCompatibleDC(pDC);
1138-
1139-
CBitmap bitmap;
1140-
bitmap.CreateCompatibleBitmap(pDC, mWClient, mHClient);
1182+
if (!EnsureBackBuffer(pDC))
1183+
return;
11411184

1142-
memDC.SelectObject(bitmap);
1185+
CDC &memDC = mBackDC;
11431186
memDC.SetStretchBltMode(COLORONCOLOR);
11441187
memDC.FillSolidRect(CRect(0, 0, mWClient, mHClient), Q1UI_COLOR_CANVAS_BG);
11451188

Viewer/ViewerView.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ class CViewerView : public CView
211211

212212
// Helper
213213
private:
214+
bool EnsureBackBuffer(CDC *pDC);
215+
void ReleaseBackBuffer();
214216
CRect CvtCoord2Show(const CRect &rt);
215217
bool FindFile(CViewerDoc* pDoc, UINT nChar);
216218
bool HandleNavigationKey(UINT nChar);
@@ -225,6 +227,12 @@ class CViewerView : public CView
225227
void _ScaleRgb(BYTE *src, BYTE *dst, int sDst, q1::GridInfo &gi);
226228
int DrawBoxInfoText(CDC *pDC, CRect &rect, COLORREF color, int hAccumGap);
227229

230+
CDC mBackDC;
231+
CBitmap mBackBitmap;
232+
CBitmap *mPrevBackBitmap;
233+
int mBackW;
234+
int mBackH;
235+
228236
// Overrides
229237
public:
230238
virtual void OnDraw(CDC* pDC); // overridden to draw this view

0 commit comments

Comments
 (0)