Skip to content
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

[Breaking change]: Graphics.GetContextInfo() is obsolete and marked as Windows only. #26785

Closed
1 of 2 tasks
safern opened this issue Nov 2, 2021 · 2 comments
Closed
1 of 2 tasks
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3 source incompatible Source code may encounter a breaking change in behavior when targeting the new version.

Comments

@safern
Copy link
Member

safern commented Nov 2, 2021

Description

Graphics.GetContextInfo() is a very costly API, so instead we added 2 GetContextInfo() overrides:

https://docs.microsoft.com/dotnet/api/system.drawing.graphics.getcontextinfo?view=dotnet-plat-ext-6.0#System_Drawing_Graphics_GetContextInfo_System_Drawing_PointF__
https://docs.microsoft.com/dotnet/api/system.drawing.graphics.getcontextinfo?view=dotnet-plat-ext-6.0#System_Drawing_Graphics_GetContextInfo_System_Drawing_PointF__System_Drawing_Region__

These new overrides calculate just the information needed based on what was requested by the caller depending on the override used, making it lower in allocations and time. Also, instead of returning an object (that represents an array of object containing the data) we are returning the structs needed to represent this data, making it easier for the consumer to access that data.

Version

6.0.0-preview4

Version

Other (please put exact version in description textbox)

Previous behavior

No warnings when compiling.

New behavior

When compiling on Windows:

warning SYSLIB0016: 'Graphics.GetContextInfo()' is obsolete: 'Use the Graphics.GetContextInfo overloads that accept arguments for better performance and fewer allocations.'

When compiling on non-Windows:

warning CA1416: This call site is reachable on all platforms. 'Graphics.GetContextInfo()' is only supported on: 'windows'.

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
  • Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.

Reason for change

Graphics.GetContextInfo exists to allow WinForms to apply cumulative Graphics transform translation and clipping to the underlying HDC so that rendering via GDI has similar output. This method currently returns an object array of [ Region, Matrix ]. (Note that the Matrix is the current Transform with only the cumulative offset applied.)

This API is called many times during rendering of WinForms applications. Avoiding the array allocation would make a significant difference in rendering allocations. A much greater allocation reduction would be obtained if Graphics tracked if the .Clip and .Transform setters are ever accessed to allow an early out in the method logic.

WinForms also don't always need both values. System.Drawing can't calculate the clipping region without the offset, but it can calculate the offset without the Region. Making the Region optional will save significant overhead.

Recommended action

Use the overload that is most appropriate for the scenario from the new overloads replacing the obsolete API.

Feature area

Extensions

Affected APIs

@safern safern added doc-idea Indicates issues that are suggestions for new topics [org][type][category] breaking-change Indicates a .NET Core breaking change Pri1 High priority, do before Pri2 and Pri3 labels Nov 2, 2021
@dotnet-bot dotnet-bot added ⌚ Not Triaged Not triaged source incompatible Source code may encounter a breaking change in behavior when targeting the new version. labels Nov 2, 2021
@gewarren gewarren added 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release and removed ⌚ Not Triaged Not triaged labels Nov 4, 2021
@gewarren
Copy link
Contributor

gewarren commented Nov 9, 2021

@gewarren gewarren closed this as completed Nov 9, 2021
@safern
Copy link
Member Author

safern commented Nov 9, 2021

Thanks @gewarren didn't know we had an entry for the obsoleted type 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 6 Issues and PRs for the .NET 6 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3 source incompatible Source code may encounter a breaking change in behavior when targeting the new version.
Projects
None yet
Development

No branches or pull requests

3 participants