Skip to content

Commit b556619

Browse files
adding defensive check to ensure device context is valid before D2D operations (#15620)
* adding defensive check to ensure device contetxt is valid before performing D2D operation * Change files
1 parent 25db080 commit b556619

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "adding defensive check to ensure device contetxt is valid before performing D2D operation",
4+
"packageName": "react-native-windows",
5+
"email": "protikbiswas@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative.Cxx/AutoDraw.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class AutoDrawDrawingSurface {
1616
POINT *offset) noexcept {
1717
drawingSurface.as(m_drawingSurfaceInterop);
1818
auto dpi = scaleFactor * 96.0f;
19-
m_drawingSurfaceInterop->BeginDraw(m_d2dDeviceContext.put(), dpi, dpi, offset);
19+
auto hr = m_drawingSurfaceInterop->BeginDraw(m_d2dDeviceContext.put(), dpi, dpi, offset);
20+
if (FAILED(hr)) {
21+
m_d2dDeviceContext = nullptr;
22+
}
2023
}
2124

2225
~AutoDrawDrawingSurface() noexcept {
@@ -34,6 +37,11 @@ class AutoDrawDrawingSurface {
3437
return m_d2dDeviceContext.get();
3538
}
3639

40+
// Returns true if the device context is valid and safe to use for drawing operations
41+
bool IsValid() const noexcept {
42+
return m_d2dDeviceContext != nullptr;
43+
}
44+
3745
private:
3846
winrt::com_ptr<Experimental::ICompositionDrawingSurfaceInterop> m_drawingSurfaceInterop;
3947
winrt::com_ptr<ID2D1DeviceContext> m_d2dDeviceContext;

vnext/Microsoft.ReactNative/Fabric/Composition/UriImageManager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ struct SvgDataImageHandler
104104
::Microsoft::ReactNative::Composition::AutoDrawDrawingSurface autoDraw(drawingBrush, 1.0, &pt);
105105
auto renderTarget = autoDraw.GetRenderTarget();
106106

107+
// Defensive check: ensure device context is valid before D2D operations
108+
if (!renderTarget) {
109+
return nullptr;
110+
}
111+
107112
winrt::com_ptr<ID2D1DeviceContext5> deviceContext5;
108113
winrt::check_hresult(renderTarget->QueryInterface(IID_ID2D1DeviceContext5, deviceContext5.put_void()));
109114

0 commit comments

Comments
 (0)