8000 Fix get TransformToDevice in Stylus Input thread will throw the InvalidOperationException by lindexi · Pull Request #6840 · dotnet/wpf · GitHub
[go: up one dir, main page]

Skip to content

Fix get TransformToDevice in Stylus Input thread will throw the InvalidOperationException #6840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,38 @@ protected Matrix GetAndCacheTransformToDeviceMatrix(PresentationSource source)
var hwndSource = source as HwndSource;
Matrix toDevice = Matrix.Identity;

if (hwndSource?.CompositionTarget != null)
if (hwndSource?.CompositionTarget is { } compositionTarget)
{
// If we have not yet seen this DPI, store the matrix for it.
if (!_transformToDeviceMatrices.ContainsKey(hwndSource.CompositionTarget.CurrentDpiScale))
// We can calculate the Matrix fast without any cache and avoid any thread issues.
// If others inherit HwndTarget, the following rules may not apply. Because they can override the TransformToDevice property.
// Fix https://github.com/dotnet/wpf/issues/6829
Type compositionTargetType = compositionTarget.GetType();
if (compositionTargetType == typeof(HwndTarget))
{
_transformToDeviceMatrices[hwndSource.CompositionTarget.CurrentDpiScale] = hwndSource.CompositionTarget.TransformToDevice;
Debug.Assert(_transformToDeviceMatrices[hwndSource.CompositionTarget.CurrentDpiScale].HasInverse);
DpiScale2 currentDpiScale = compositionTarget.CurrentDpiScale;
toDevice.Scale(currentDpiScale.DpiScaleX, currentDpiScale.DpiScaleY);
Copy link
Contributor
@miloush miloush Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the transform not cached?

Copy link
Member Author
@lindexi lindexi Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miloush Do you think cache is fast than calculation? Thank you. I means that we should handle the cross thread issues when we cache it.

}
else
{
// If we have not yet seen this DPI, store the matrix for it.
if (!_transformToDeviceMatrices.TryGetValue(compositionTarget.CurrentDpiScale, out toDevice))
{
// The Stylus Input thread will enter this case.
if (compositionTarget.Dispatcher.CheckAccess())
{
toDevice = compositionTarget.TransformToDevice;
}
else
{
// We have to run it in UI Thread, see https://github.com/dotnet/wpf/issues/6829
compositionTarget.Dispatcher.Invoke(() => toDevice = compositionTarget.TransformToDevice);
}

Debug.Assert(toDevice.HasInverse);

toDevice = _transformToDeviceMatrices[hwndSource.CompositionTarget.CurrentDpiScale];
_transformToDeviceMatrices[compositionTarget.CurrentDpiScale] = toDevice;
}
}
}

return toDevice;
Expand Down
0