Skip to content

Commit 2b56f0a

Browse files
committed
Recreate ID2D1HwndRenderTarget if needed.
1 parent 021cd17 commit 2b56f0a

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

Sources/WindowsAppSupport/WAS_Direct2DContext.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static float GetPenThickness (const NUIE::Pen& pen)
9696

9797
Direct2DContext::Direct2DContext () :
9898
NUIE::NativeDrawingContext (),
99+
hwnd (NULL),
99100
width (0),
100101
height (0),
101102
renderTarget (nullptr)
@@ -110,20 +111,8 @@ Direct2DContext::~Direct2DContext ()
110111

111112
void Direct2DContext::Init (void* nativeHandle)
112113
{
113-
HWND hwnd = (HWND) nativeHandle;
114-
115-
RECT clientRect;
116-
GetClientRect (hwnd, &clientRect);
117-
width = clientRect.right - clientRect.left;
118-
height = clientRect.bottom - clientRect.top;
119-
120-
D2D1_SIZE_U size = D2D1::SizeU (width, height);
121-
D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties ();
122-
D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderTargetProperties = D2D1::HwndRenderTargetProperties (hwnd, size);
123-
124-
direct2DHandler.direct2DFactory->CreateHwndRenderTarget (renderTargetProperties, hwndRenderTargetProperties, &renderTarget);
125-
renderTarget->SetDpi (96.0f, 96.0f);
126-
DBGASSERT (renderTarget != nullptr);
114+
hwnd = (HWND) nativeHandle;
115+
CreateRenderTarget ();
127116
}
128117

129118
void Direct2DContext::BlitToWindow (void*)
@@ -171,9 +160,10 @@ void Direct2DContext::BeginDraw (Phase phase)
171160
void Direct2DContext::EndDraw (Phase phase)
172161
{
173162
if (phase == Phase::Draw) {
174-
// TODO: recreate in case of error
175-
DBGONLY (HRESULT success = ) renderTarget->EndDraw ();
176-
DBGASSERT (SUCCEEDED (success));
163+
HRESULT result = renderTarget->EndDraw ();
164+
if (result == D2DERR_RECREATE_TARGET) {
165+
CreateRenderTarget ();
166+
}
177167
} else if (phase == Phase::DrawConnections) {
178168
renderTarget->SetAntialiasMode (D2D1_ANTIALIAS_MODE_ALIASED);
179169
}
@@ -290,4 +280,21 @@ NUIE::Size Direct2DContext::MeasureText (const NUIE::Font& font, const std::wstr
290280
return NUIE::Size (metrics.width * SafetyTextRatio, metrics.height * SafetyTextRatio);
291281
}
292282

283+
void Direct2DContext::CreateRenderTarget ()
284+
{
285+
RECT clientRect;
286+
GetClientRect (hwnd, &clientRect);
287+
width = clientRect.right - clientRect.left;
288+
height = clientRect.bottom - clientRect.top;
289+
290+
D2D1_SIZE_U size = D2D1::SizeU (width, height);
291+
D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties ();
292+
D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderTargetProperties = D2D1::HwndRenderTargetProperties (hwnd, size);
293+
294+
SafeRelease (&renderTarget);
295+
direct2DHandler.direct2DFactory->CreateHwndRenderTarget (renderTargetProperties, hwndRenderTargetProperties, &renderTarget);
296+
renderTarget->SetDpi (96.0f, 96.0f);
297+
DBGASSERT (renderTarget != nullptr);
298+
}
299+
293300
}

Sources/WindowsAppSupport/WAS_Direct2DContext.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class Direct2DContext : public NUIE::NativeDrawingContext
4242
virtual NUIE::Size MeasureText (const NUIE::Font& font, const std::wstring& text) override;
4343

4444
private:
45+
void CreateRenderTarget ();
46+
47+
HWND hwnd;
4548
int width;
4649
int height;
4750
ID2D1HwndRenderTarget* renderTarget;

0 commit comments

Comments
 (0)