Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Change System.Drawing.Common's target to netcoreapp2.0 #22833

Merged
merged 2 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions external/dir.proj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<!-- Build for all configurations -->
<ItemGroup>
<Project Include="netcoreapp/netcoreapp.depproj" />
<Project Include="netstandard/netstandard.depproj" />
<Project Include="netfx/netfx.depproj" />
<Project Include="runtime/runtime.depproj" />
Expand Down
8 changes: 8 additions & 0 deletions external/netcoreapp/Configurations.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netcoreapp2.0
</BuildConfigurations>
</PropertyGroup>
</Project>
29 changes: 29 additions & 0 deletions external/netcoreapp/netcoreapp.depproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment to this project describing its purpose: to download a targeting pack only for a specific NETCoreApp version.

<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<BinPlaceRef>true</BinPlaceRef>
<NuGetDeploySourceItem>Reference</NuGetDeploySourceItem>
<NETCoreAppPackageVersion>2.0.0-preview2-25407-01</NETCoreAppPackageVersion>
Copy link
Member

Choose a reason for hiding this comment

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

Don't we have a stable version?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we do. I don't know why I only looked on nuget.org for this version.

Will fix.

<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>$(NETCoreAppPackageVersion)</Version>
</PackageReference>
</ItemGroup>

<ItemGroup>
<!-- for all configurations this project provides refs for that configuration -->
<BinPlaceConfiguration Include="$(Configuration)">
<RefPath>$(RefPath)</RefPath>
</BinPlaceConfiguration>
<BinPlaceConfiguration Include="$(Configuration)">
<!-- copy shims to the testhost as well in order to be able to run the netfx tests -->
Copy link
Member

Choose a reason for hiding this comment

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

Why? This seems broken. TestHostRootPath is set to a BuildConfiguration-specific path, not a configuration specific path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it seems wrong. It was copy-pasted from another project file (along with the $(RefPath) setting above). The shim part can probably be deleted.

<RuntimePath>$(TestHostRootPath)</RuntimePath>
</BinPlaceConfiguration>
</ItemGroup>

<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
98 changes: 98 additions & 0 deletions src/Common/src/System/Drawing/ColorUtil.netcoreapp20.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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.Diagnostics;
using System.Reflection;

namespace System.Drawing
{
// System.Drawing.Common uses several members on System.Drawing.Common which are implemented in .NET Core 2.0, but
// not exposed. This is a helper class which allows System.Drawing.Common to access those members through reflection.
internal static class ColorUtil
{
private const short StateKnownColorValid = 0x0001;
private const short StateNameValid = 0x0008;
private const long NotDefinedValue = 0;

private static readonly ConstructorInfo s_ctorKnownColor; // internal Color(KnownColor knownColor)
Copy link
Member

Choose a reason for hiding this comment

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

I can't recall the specific differences but I know in the past we avoid caching reflection objects and instead get a delegate (either bound or unbound) so that we can have minimal overhead for the lightup after initial setup. Have a look at CreateDelegate as a potential better optimization here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. I believe that also lets us avoid all of the temporary array allocations associated with invoking these reflection objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I'm not sure it works in this case. I am storing ConstructorInfo and FieldInfo objects, and you can't create Delegates out of those. Is that right?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, looking back at this code we never did it for fields. For constructors we created a func to Activator.CreateInstance. FWIW, this was the code I was remembering: https://github.com/Microsoft/referencesource/blob/master/Microsoft.Bcl/System.Threading.Tasks.v1.5/System/Lightup/Lightup.cs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I don't really see much value in that, TBH. Is there some large benefit to calling Activator.CreateInstance(Type, object[]) rather than just invoking a cached ConstructorInfo? We don't save out on the cost of the array allocation. My intuition says the ConstructorInfo would be faster, but I can't say that I've ever tested the difference between the two 😄 .

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I don't expect CreateInstance is any faster. Perhaps is has to do with what remains rooted when you keep these objects around? Honestly I can't remember and its been years since I touched that code. It could have been due to silverlight specifics as well since that was trying to work with SL. I think what you have is fine.

private static readonly ConstructorInfo s_ctorAllValues; // private Color(long value, short state, string name, KnownColor knownColor)
private static readonly FieldInfo s_fieldKnownColor; // private readonly short knownColor
private static readonly FieldInfo s_fieldState; // private readonly short state

static ColorUtil()
Copy link
Member

Choose a reason for hiding this comment

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

Do we think reflection is the best option here? Why not just source sharing and changing the visibility?

{
Type colorType = typeof(Color);
Type knownColorType = colorType.Assembly.GetType("System.Drawing.KnownColor", true);
Debug.Assert(knownColorType != null);
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
s_ctorKnownColor = colorType.GetConstructor(bindingFlags, null, new Type[] { knownColorType }, null);
Debug.Assert(s_ctorKnownColor != null);

s_ctorAllValues = colorType.GetConstructor(bindingFlags, null, new Type[] { typeof(long), typeof(short), typeof(string), knownColorType }, null);
Debug.Assert(s_ctorAllValues != null);

s_fieldKnownColor = colorType.GetField("knownColor", bindingFlags);
Debug.Assert(s_fieldKnownColor != null);

s_fieldState = colorType.GetField("state", bindingFlags);
Debug.Assert(s_fieldState != null);
}

public static Color FromKnownColor(KnownColor color)
{
var value = (int)color;
if (value < (int)KnownColor.ActiveBorder || value > (int)KnownColor.MenuHighlight)
{
return FromName(color.ToString());
}

return (Color)s_ctorKnownColor.Invoke(new object[] { value });
}

public static Color FromName(string name)
{
// try to get a known color first
Color color;
if (ColorTable.TryGetNamedColor(name, out color))
{
return color;
}
// otherwise treat it as a named color
return (Color)s_ctorAllValues.Invoke(new object[] { NotDefinedValue, StateNameValid, name, 0 });
}

public static bool IsSystemColor(this Color color)
{
short knownColor = GetKnownColor(color);
return GetIsKnownColor(color) && ((((KnownColor)knownColor) <= KnownColor.WindowText) || (((KnownColor)knownColor) > KnownColor.YellowGreen));
}

public static short GetKnownColor(this Color color)
{
return (short)s_fieldKnownColor.GetValue(color);
}

public static short GetState(this Color color)
{
return (short)s_fieldState.GetValue(color);
}

public static bool GetIsSystemColor(this Color color)
{
short knownColor = color.GetKnownColor();
return GetIsKnownColor(color) && ((((KnownColor)knownColor) <= KnownColor.WindowText) || (((KnownColor)knownColor) > KnownColor.YellowGreen));
}

public static bool GetIsKnownColor(this Color color)
{
short state = GetState(color);
return ((state & StateKnownColorValid) != 0);
}

public static KnownColor ToKnownColor(this Color c)
{
return (KnownColor)GetKnownColor(c);
}
}
}
15 changes: 15 additions & 0 deletions src/Common/src/System/Drawing/ColorUtil.netcoreapp21.cs
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.Diagnostics;
using System.Reflection;

namespace System.Drawing
{
internal static class ColorUtil
{
public static Color FromKnownColor(KnownColor color) => Color.FromKnownColor(color);
public static bool IsSystemColor(this Color color) => color.IsSystemColor;
}
}
4 changes: 2 additions & 2 deletions src/Common/src/System/Drawing/KnownColorTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static Color ArgbToKnownColor(int targetARGB)
int argb = s_colorTable[index];
if (argb == targetARGB)
{
Color color = Color.FromKnownColor((KnownColor)index);
if (!color.IsSystemColor)
Color color = ColorUtil.FromKnownColor((KnownColor)index);
if (!ColorUtil.IsSystemColor(color))
return color;
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/System.Drawing.Common/System.Drawing.Common.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ Global
{4B93E684-0630-45F4-8F63-6C7788C9892F}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU
{4B93E684-0630-45F4-8F63-6C7788C9892F}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
{4B93E684-0630-45F4-8F63-6C7788C9892F}.Release|Any CPU.Build.0 = netcoreapp-Windows_NT-Release|Any CPU
{191B3618-FECD-4ABD-9D6B-5AC90DC33621}.Debug|Any CPU.ActiveCfg = netcoreapp-Unix-Debug|Any CPU
{191B3618-FECD-4ABD-9D6B-5AC90DC33621}.Debug|Any CPU.Build.0 = netcoreapp-Unix-Debug|Any CPU
{191B3618-FECD-4ABD-9D6B-5AC90DC33621}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
{191B3618-FECD-4ABD-9D6B-5AC90DC33621}.Release|Any CPU.Build.0 = netcoreapp-Windows_NT-Release|Any CPU
{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}.Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}.Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
{191B3618-FECD-4ABD-9D6B-5AC90DC33621}.Debug|Any CPU.ActiveCfg = netcoreapp2.0-Windows_NT-Debug|Any CPU
{191B3618-FECD-4ABD-9D6B-5AC90DC33621}.Debug|Any CPU.Build.0 = netcoreapp2.0-Windows_NT-Debug|Any CPU
{191B3618-FECD-4ABD-9D6B-5AC90DC33621}.Release|Any CPU.ActiveCfg = netcoreapp2.0-Windows_NT-Release|Any CPU
{191B3618-FECD-4ABD-9D6B-5AC90DC33621}.Release|Any CPU.Build.0 = netcoreapp2.0-Windows_NT-Release|Any CPU
{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}.Debug|Any CPU.ActiveCfg = netcoreapp2.0-Debug|Any CPU
{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}.Debug|Any CPU.Build.0 = netcoreapp2.0-Debug|Any CPU
{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}.Release|Any CPU.ActiveCfg = netcoreapp2.0-Release|Any CPU
{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}.Release|Any CPU.Build.0 = netcoreapp2.0-Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion src/System.Drawing.Common/ref/Configurations.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netcoreapp;
netcoreapp2.0;
</BuildConfigurations>
</PropertyGroup>
</Project>
20 changes: 10 additions & 10 deletions src/System.Drawing.Common/ref/System.Drawing.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
<PropertyGroup>
<ProjectGuid>{D7AEA698-275D-441F-B7A7-8491D1F0EFF0}</ProjectGuid>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp2.0-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp2.0-Release|AnyCPU'" />
<ItemGroup>
<Compile Include="System.Drawing.Common.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.Extensions\ref\System.Runtime.Extensions.csproj" />
<ProjectReference Include="..\..\System.Runtime.InteropServices\ref\System.Runtime.InteropServices.csproj" />
<ProjectReference Include="..\..\System.Collections.NonGeneric\ref\System.Collections.NonGeneric.csproj" />
<ProjectReference Include="..\..\System.ComponentModel\ref\System.ComponentModel.csproj" />
<ProjectReference Include="..\..\System.ComponentModel.Primitives\ref\System.ComponentModel.Primitives.csproj" />
<ProjectReference Include="..\..\System.Diagnostics.Debug\ref\System.Diagnostics.Debug.csproj" />
<ProjectReference Include="..\..\System.Drawing.Primitives\ref\System.Drawing.Primitives.csproj" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Collections.NonGeneric" />
<Reference Include="System.ComponentModel" />
<Reference Include="System.ComponentModel.Primitives" />
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.Drawing.Primitives" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
4 changes: 2 additions & 2 deletions src/System.Drawing.Common/src/Configurations.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netcoreapp-Windows_NT;
netcoreapp-Unix;
netcoreapp2.0-Windows_NT;
netcoreapp2.0-Unix;
</BuildConfigurations>
</PropertyGroup>
</Project>
23 changes: 17 additions & 6 deletions src/System.Drawing.Common/src/System.Drawing.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
<DefineConstants Condition="'$(TargetsWindows)' == 'true'">$(DefineConstants);FEATURE_WINDOWS_SYSTEM_COLORS</DefineConstants>
<DefineConstants Condition="'$(TargetsUnix)' == 'true'">$(DefineConstants);CORECLR;NETCORE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Unix-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Unix-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp2.0-Unix-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp2.0-Unix-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp2.0-Windows_NT-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp2.0-Windows_NT-Release|AnyCPU'" />
<ItemGroup>
<Reference Include="Microsoft.Win32.Primitives" />
<Reference Include="System.Collections" />
<Reference Include="System.Collections.NonGeneric" />
<Reference Include="System.Collections.Specialized" />
<Reference Include="System.Configuration.ConfigurationManager" />
<Reference Include="System.Diagnostics.Contracts" />
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.Diagnostics.StackTrace" />
Expand All @@ -41,6 +40,7 @@
</ItemGroup>
<ItemGroup>
<!-- Shared source code, all configurations -->
<Compile Include="misc\InvalidEnumArgumentException.cs" />
<Compile Include="System\Drawing\BitmapSuffixInSameAssemblyAttribute.cs" />
<Compile Include="System\Drawing\BitmapSuffixInSatelliteAssemblyAttribute.cs" />
<Compile Include="System\Drawing\IDeviceContext.cs" />
Expand Down Expand Up @@ -166,6 +166,12 @@
<Compile Include="$(CommonPath)\System\Drawing\ColorTable.cs">
<Link>System\Drawing\ColorTable.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\System\Drawing\ColorUtil.netcoreapp20.cs">
<Link>System\Drawing\ColorUtil.netcoreapp20.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\System\Drawing\KnownColor.cs">
<Link>System\Drawing\KnownColor.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\System\Drawing\KnownColorTable.cs">
<Link>System\Drawing\KnownColorTable.cs</Link>
</Compile>
Expand Down Expand Up @@ -212,7 +218,9 @@
<Compile Include="System\Drawing\Printing\PageSettings.cs" />
<Compile Include="System\Drawing\Printing\PreviewPrintController.cs" />
<Compile Include="System\Drawing\Printing\PrintController.cs" />
<Compile Include="System\Drawing\Printing\PrintDocument.cs" />
<Compile Include="System\Drawing\Printing\PrintDocument.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="System\Drawing\Printing\PrinterSettings.cs" />
<Compile Include="System\Drawing\Printing\PrintEvent.cs" />
<Compile Include="System\Drawing\Printing\PrintPageEvent.cs" />
Expand Down Expand Up @@ -241,6 +249,9 @@
<Compile Include="misc\GDI\WindowsRegion.cs" />
<Compile Include="misc\GDI\WindowsRegionCombineMode.cs" />
<Compile Include="$(CommonPath)\System\LocalAppContext.cs" />
<Compile Include="$(CommonPath)\System\Numerics\Hashing\HashHelpers.cs">
<Link>Common\System\Numerics\Hashing\HashHelpers.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
<Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
</Compile>
Expand Down
18 changes: 3 additions & 15 deletions src/System.Drawing.Common/src/System/Drawing/BitmapSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace System.Drawing
{
using System.Configuration;
using System.Drawing.Configuration;
using System.IO;
using System.Reflection;

Expand All @@ -27,19 +25,9 @@ internal static string Suffix
{
get
{
if (s_suffix == null)
{
s_suffix = string.Empty;
var section = ConfigurationManager.GetSection("system.drawing") as SystemDrawingSection;
if (section != null)
{
var value = section.BitmapSuffix;
if (value != null && value is string)
{
s_suffix = (string)value;
}
}
}
// NOTE: This value is read from the "SystemDrawingSection" of the ConfigurationManager on
// the .NET Framework. To avoid pulling in a direct dependency to that assembly, we are not
// reading the value in this implementation.
return s_suffix;
}
set
Expand Down
Loading