-
Notifications
You must be signed in to change notification settings - Fork 945
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove WindowsGraphics and DeviceContext (#3553)
This change removes all of the misc\GDI classes and replaces them with new isolated caches for Font-to-HFONT and "measurement" screen DCs. This reduces significant allocations and complexity and improves performance with these legacy code paths. Fundamental components: -Fully eliminates all existing finalizable GDI wrappers and caches (from GDI\misc) -Replaces DeviceContext and WindowsGraphics with DeviceContextHdcScope -Font to HFONT caching is contained in FontCache -Screen HDC's are now managed by ScreenDcCache -GdiCache is the entry point for GDI object caches -Starts implementing IDeviceContext on event args as a method for smuggling through existing IDeviceContext APIs to DeviceContextHdcScope, which looks for the internal IGraphicsHdcProvider that the event args also implement -HDC/Graphics optimization is wrapped in DrawingEventArgs struct that events can contain to manage consistently -Hacky extension TryGetGraphics() allows us to refactor APIs to use IDeviceContext when there is still a dependency on Graphics for some code paths (e.g. transparent color drawing) -Add a few new drawing helpers in DeviceContextExtensions -Some initial fiddling with GDI+ direct usage- will iterate on where that lands in upcoming PRs -Some APIs no longer throw ArgumentNullException, updated a number of tests -Various style cleanups as I was going through investigating call chains -Eliminate unnecessary state saving or Graphics wrapping on a number of call sites
- Loading branch information
1 parent
dec24b8
commit a6a308f
Showing
121 changed files
with
5,567 additions
and
6,084 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.GetClipBox.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Gdi32 | ||
{ | ||
[DllImport(Libraries.Gdi32, ExactSpelling = true)] | ||
public static extern RegionType GetClipBox(HDC hdc, ref RECT lprect); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.SetBackgroundColorScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Drawing; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Gdi32 | ||
{ | ||
/// <summary> | ||
/// Helper to scope selecting a given background color into a HDC. Restores the original background color into | ||
/// the HDC when disposed. | ||
/// </summary> | ||
/// <remarks> | ||
/// Use in a <see langword="using" /> statement. If you must pass this around, always pass by | ||
/// <see langword="ref" /> to avoid duplicating the handle and resetting multiple times. | ||
/// </remarks> | ||
internal readonly ref struct SetBackgroundColorScope | ||
{ | ||
private readonly int _previousColor; | ||
private readonly HDC _hdc; | ||
|
||
/// <summary> | ||
/// Sets text color <paramref name="color"/> in the given <paramref name="hdc"/> using | ||
/// <see cref="SetBkColor(HDC, int)"/>. | ||
/// </summary> | ||
public SetBackgroundColorScope(HDC hdc, Color color) | ||
{ | ||
int colorref = ColorTranslator.ToWin32(color); | ||
_previousColor = SetBkColor(hdc, colorref); | ||
|
||
// If we didn't actually change the color, don't keep the HDC so we skip putting back the same state. | ||
_hdc = colorref == _previousColor ? default : hdc; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_hdc.IsNull) | ||
{ | ||
SetBkColor(_hdc, _previousColor); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.SetMapModeScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Drawing; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Gdi32 | ||
{ | ||
/// <summary> | ||
/// Helper to scope selecting a given mapping mode into a HDC. Restores the original mapping mode into the HDC | ||
/// when disposed. | ||
/// </summary> | ||
/// <remarks> | ||
/// Use in a <see langword="using" /> statement. If you must pass this around, always pass by | ||
/// <see langword="ref" /> to avoid duplicating the handle and resetting multiple times. | ||
/// </remarks> | ||
internal readonly ref struct SetMapModeScope | ||
{ | ||
private readonly MM _previousMapMode; | ||
private readonly HDC _hdc; | ||
|
||
/// <summary> | ||
/// Sets the <paramref name="mapMode"/> in the given <paramref name="hdc"/> using | ||
/// <see cref="SetMapMode(HDC, MM)"/>. | ||
/// </summary> | ||
public SetMapModeScope(HDC hdc, MM mapMode) | ||
{ | ||
_previousMapMode = SetMapMode(hdc, mapMode); | ||
|
||
// If we didn't actually change the map mode, don't keep the HDC so we skip putting back the same state. | ||
_hdc = mapMode == _previousMapMode ? default : hdc; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_hdc.IsNull) | ||
{ | ||
SetMapMode(_hdc, _previousMapMode); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.SetTextAlignmentScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Gdi32 | ||
{ | ||
/// <summary> | ||
/// Helper to scope selecting a given text alignment mode into a HDC. Restores the original text alignment | ||
/// mode into the HDC when disposed. | ||
/// </summary> | ||
/// <remarks> | ||
/// Use in a <see langword="using" /> statement. If you must pass this around, always pass by | ||
/// <see langword="ref" /> to avoid duplicating the handle and resetting multiple times. | ||
/// </remarks> | ||
internal readonly ref struct SetTextAlignmentScope | ||
{ | ||
private readonly TA _previousTa; | ||
private readonly HDC _hdc; | ||
|
||
/// <summary> | ||
/// Sets <paramref name="ta"/> in the given <paramref name="hdc"/> using <see cref="SetTextAlign(HDC, TA)"/>. | ||
/// </summary> | ||
public SetTextAlignmentScope(HDC hdc, TA ta) | ||
{ | ||
_previousTa = SetTextAlign(hdc, ta); | ||
|
||
// If we didn't actually change the TA, don't keep the HDC so we skip putting back the same state. | ||
_hdc = _previousTa == ta ? default : hdc; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_hdc.IsNull) | ||
{ | ||
SetTextAlign(_hdc, _previousTa); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.