Skip to content

Commit

Permalink
Add ResourceProvider and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Sep 22, 2023
1 parent 86965b7 commit 57f2998
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("SkiaSharp.Resources")]
[assembly: AssemblyDescription("This package adds lottie support to SkiaSharp via skottie.")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyProduct("SkiaSharp")]
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
[assembly: NeutralResourcesLanguage("en")]

[assembly: InternalsVisibleTo("SkiaSharp.Tests, PublicKey=" +
"002400000480000094000000060200000024000052534131000400000100010079159977d2d03a" +
"8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c" +
"3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fd" +
"dafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef00" +
"65d016df")]

[assembly: InternalsVisibleTo("SkiaSharp.Benchmarks, PublicKey=" +
"002400000480000094000000060200000024000052534131000400000100010079159977d2d03a" +
"8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c" +
"3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fd" +
"dafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef00" +
"65d016df")]

[assembly: AssemblyMetadata("IsTrimmable", "True")]

#if __IOS__ || __TVOS__ || __MACOS__
// This attribute allows you to mark your assemblies as “safe to link”.
// When the attribute is present, the linker—if enabled—will process the assembly
// even if you’re using the “Link SDK assemblies only” option, which is the default for device builds.
#pragma warning disable CS0618 // Type or member is obsolete
[assembly: Foundation.LinkerSafe]
#pragma warning restore CS0618 // Type or member is obsolete
#endif
43 changes: 43 additions & 0 deletions binding/SkiaSharp.Resources/ResourceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.IO;

namespace SkiaSharp.Resources
{
public unsafe class ResourceProvider : SKObject, ISKSkipObjectRegistration
{
internal ResourceProvider (IntPtr handle, bool owns)
: base (handle, owns)
{
}

protected override void DisposeNative ()
=> ResourcesApi.skresources_resource_provider_delete (Handle);

public SKData? Load (string resourceName) =>
Load ("", resourceName);

public SKData? Load (string resourcePath, string resourceName) =>
SKData.GetObject (ResourcesApi.skresources_resource_provider_load (Handle, resourcePath, resourceName));

public static ResourceProvider CreateCaching (ResourceProvider resourceProvider)
{
_ = resourceProvider ?? throw new ArgumentNullException (nameof (resourceProvider));
return GetObject (ResourcesApi.skresources_caching_resource_provider_proxy_make (resourceProvider.Handle))!;
}

public static ResourceProvider CreateDataUri (ResourceProvider fallbackProvider, bool preDecode = false)
{
_ = fallbackProvider ?? throw new ArgumentNullException (nameof (fallbackProvider));
return GetObject (ResourcesApi.skresources_data_uri_resource_provider_proxy_make (fallbackProvider.Handle, preDecode))!;
}

public static ResourceProvider CreateFile (string baseDirectory, bool preDecode = false)
{
using var baseDir = new SKString(baseDirectory ?? throw new ArgumentNullException (nameof (baseDirectory)));
return GetObject (ResourcesApi.skresources_file_resource_provider_make (baseDir.Handle, preDecode))!;
}

internal static ResourceProvider? GetObject (IntPtr handle) =>
handle == IntPtr.Zero ? null : new ResourceProvider (handle, true);
}
}
8 changes: 4 additions & 4 deletions binding/SkiaSharp.Resources/SkiaSharp.Resources.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(AllTargetFrameworks)</TargetFrameworks>
<RootNamespace>SkiaSharp.SkResources</RootNamespace>
<AssemblyName>SkiaSharp.SkResources</AssemblyName>
<PackagingGroup>SkiaSharp.SkResources</PackagingGroup>
<Title>SkiaSharp SkResources</Title>
<RootNamespace>SkiaSharp.Resources</RootNamespace>
<AssemblyName>SkiaSharp.Resources</AssemblyName>
<PackagingGroup>SkiaSharp.Resources</PackagingGroup>
<Title>SkiaSharp Resource Providers</Title>
<PackageDescription>SkiaSharp Skottie provides a Lottie implementation using the SkiaSharp library.</PackageDescription>
<PackageTags>skottie;lottie</PackageTags>
<Nullable>enable</Nullable>
Expand Down
1 change: 1 addition & 0 deletions binding/SkiaSharp.Skottie/SkiaSharp.Skottie.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<ItemGroup>
<ProjectReference Include="..\SkiaSharp\SkiaSharp.csproj" />
<ProjectReference Include="..\SkiaSharp.SceneGraph\SkiaSharp.SceneGraph.csproj" />
<ProjectReference Include="..\SkiaSharp.Resources\SkiaSharp.Resources.csproj" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions binding/SkiaSharp/Properties/SkiaSharpAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
"dafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef00" +
"65d016df")]

[assembly: InternalsVisibleTo("SkiaSharp.Resources, PublicKey=" +
"002400000480000094000000060200000024000052534131000400000100010079159977d2d03a" +
"8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c" +
"3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fd" +
"dafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef00" +
"65d016df")]

[assembly: AssemblyMetadata("IsTrimmable", "True")]

#if __IOS__ || __TVOS__ || __MACOS__
Expand Down
15 changes: 15 additions & 0 deletions tests/SkiaSharp.Tests.Console.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Vulkan.SharpVk",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.SceneGraph", "..\binding\SkiaSharp.SceneGraph\SkiaSharp.SceneGraph.csproj", "{978AD2C6-E592-4F5E-B565-26C357877B2C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Resources", "..\binding\SkiaSharp.Resources\SkiaSharp.Resources.csproj", "{AD2C6978-4F5E-E592-B565-26C357877B2C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Skottie", "..\binding\SkiaSharp.Skottie\SkiaSharp.Skottie.csproj", "{D35696BA-3038-41F2-9BE9-181439E6EF9E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "source", "source", "{9FAA974B-A671-40BC-82EA-8EF77F463ECB}"
Expand Down Expand Up @@ -115,6 +117,18 @@ Global
{978AD2C6-E592-4F5E-B565-26C357877B2C}.Release|x64.Build.0 = Release|Any CPU
{978AD2C6-E592-4F5E-B565-26C357877B2C}.Release|x86.ActiveCfg = Release|Any CPU
{978AD2C6-E592-4F5E-B565-26C357877B2C}.Release|x86.Build.0 = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|x64.ActiveCfg = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|x64.Build.0 = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|x86.ActiveCfg = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|x86.Build.0 = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|Any CPU.Build.0 = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|x64.ActiveCfg = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|x64.Build.0 = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|x86.ActiveCfg = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|x86.Build.0 = Release|Any CPU
{D35696BA-3038-41F2-9BE9-181439E6EF9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D35696BA-3038-41F2-9BE9-181439E6EF9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D35696BA-3038-41F2-9BE9-181439E6EF9E}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -137,6 +151,7 @@ Global
{0DE402FA-A101-438E-8528-6EA82E0FF803} = {9FAA974B-A671-40BC-82EA-8EF77F463ECB}
{A53DD2E5-55C4-4F7C-9316-D7FAD9A6F19F} = {9FAA974B-A671-40BC-82EA-8EF77F463ECB}
{978AD2C6-E592-4F5E-B565-26C357877B2C} = {9FAA974B-A671-40BC-82EA-8EF77F463ECB}
{AD2C6978-4F5E-E592-B565-26C357877B2C} = {9FAA974B-A671-40BC-82EA-8EF77F463ECB}
{D35696BA-3038-41F2-9BE9-181439E6EF9E} = {9FAA974B-A671-40BC-82EA-8EF77F463ECB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ItemGroup>
<ProjectReference Include="..\..\binding\HarfBuzzSharp\HarfBuzzSharp.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.SceneGraph\SkiaSharp.SceneGraph.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.Resources\SkiaSharp.Resources.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.Skottie\SkiaSharp.Skottie.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp\SkiaSharp.csproj" />
<ProjectReference Include="..\..\source\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz.csproj" />
Expand Down
7 changes: 7 additions & 0 deletions tests/SkiaSharp.Tests.Devices.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HarfBuzzSharp", "..\binding
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.SceneGraph", "..\binding\SkiaSharp.SceneGraph\SkiaSharp.SceneGraph.csproj", "{8CD906F8-B3E4-48E6-8B16-EAFC0C34EAE1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Resources", "..\binding\SkiaSharp.Resources\SkiaSharp.Resources.csproj", "{AD2C6978-4F5E-E592-B565-26C357877B2C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Skottie", "..\binding\SkiaSharp.Skottie\SkiaSharp.Skottie.csproj", "{915D1D57-B059-4301-9A35-2E5EB68DED99}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.HarfBuzz", "..\source\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz.csproj", "{6F999CA5-B67F-46A3-9A94-9E99527060F6}"
Expand Down Expand Up @@ -55,6 +57,10 @@ Global
{8CD906F8-B3E4-48E6-8B16-EAFC0C34EAE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CD906F8-B3E4-48E6-8B16-EAFC0C34EAE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CD906F8-B3E4-48E6-8B16-EAFC0C34EAE1}.Release|Any CPU.Build.0 = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|Any CPU.Build.0 = Release|Any CPU
{915D1D57-B059-4301-9A35-2E5EB68DED99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{915D1D57-B059-4301-9A35-2E5EB68DED99}.Debug|Any CPU.Build.0 = Debug|Any CPU
{915D1D57-B059-4301-9A35-2E5EB68DED99}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -87,6 +93,7 @@ Global
{9D753C4C-D7FC-4D1B-ABF0-BF1C089B987A} = {6779122B-72B0-42ED-A1E7-5029C1C0A78D}
{D48557C5-795D-4948-84EE-A7531DDD91DC} = {6779122B-72B0-42ED-A1E7-5029C1C0A78D}
{8CD906F8-B3E4-48E6-8B16-EAFC0C34EAE1} = {6779122B-72B0-42ED-A1E7-5029C1C0A78D}
{AD2C6978-4F5E-E592-B565-26C357877B2C} = {6779122B-72B0-42ED-A1E7-5029C1C0A78D}
{915D1D57-B059-4301-9A35-2E5EB68DED99} = {6779122B-72B0-42ED-A1E7-5029C1C0A78D}
{6F999CA5-B67F-46A3-9A94-9E99527060F6} = {6779122B-72B0-42ED-A1E7-5029C1C0A78D}
{398936B0-1B68-4F2D-B91C-6880CAC9F168} = {6779122B-72B0-42ED-A1E7-5029C1C0A78D}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<ProjectReference Include="..\..\binding\SkiaSharp\SkiaSharp.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.SceneGraph\SkiaSharp.SceneGraph.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.Resources\SkiaSharp.Resources.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.Skottie\SkiaSharp.Skottie.csproj" />
<ProjectReference Include="..\..\binding\HarfBuzzSharp\HarfBuzzSharp.csproj" />
<ProjectReference Include="..\..\source\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz.csproj" />
Expand Down
7 changes: 7 additions & 0 deletions tests/SkiaSharp.Tests.Wasm.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.HarfBuzz", "..\so
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.SceneGraph", "..\binding\SkiaSharp.SceneGraph\SkiaSharp.SceneGraph.csproj", "{2A777BD2-9108-4747-BBD2-0240B8D7CCC3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Resources", "..\binding\SkiaSharp.Resources\SkiaSharp.Resources.csproj", "{AD2C6978-4F5E-E592-B565-26C357877B2C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkiaSharp.Skottie", "..\binding\SkiaSharp.Skottie\SkiaSharp.Skottie.csproj", "{3FCCE93A-727E-4891-8449-468F13E8052A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "source", "source", "{5C42DB9F-1771-4F1D-8247-E58AEB9D9094}"
Expand Down Expand Up @@ -43,6 +45,10 @@ Global
{2A777BD2-9108-4747-BBD2-0240B8D7CCC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A777BD2-9108-4747-BBD2-0240B8D7CCC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A777BD2-9108-4747-BBD2-0240B8D7CCC3}.Release|Any CPU.Build.0 = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD2C6978-4F5E-E592-B565-26C357877B2C}.Release|Any CPU.Build.0 = Release|Any CPU
{3FCCE93A-727E-4891-8449-468F13E8052A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FCCE93A-727E-4891-8449-468F13E8052A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FCCE93A-727E-4891-8449-468F13E8052A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -56,6 +62,7 @@ Global
{B020BCC5-FC01-4ADC-839E-18E63BC6D4C5} = {5C42DB9F-1771-4F1D-8247-E58AEB9D9094}
{6E6DF00F-30EF-479B-92FB-B07DDE6B1259} = {5C42DB9F-1771-4F1D-8247-E58AEB9D9094}
{2A777BD2-9108-4747-BBD2-0240B8D7CCC3} = {5C42DB9F-1771-4F1D-8247-E58AEB9D9094}
{AD2C6978-4F5E-E592-B565-26C357877B2C} = {5C42DB9F-1771-4F1D-8247-E58AEB9D9094}
{3FCCE93A-727E-4891-8449-468F13E8052A} = {5C42DB9F-1771-4F1D-8247-E58AEB9D9094}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
1 change: 1 addition & 0 deletions tests/SkiaSharp.Tests.Wasm/SkiaSharp.Tests.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<ItemGroup>
<ProjectReference Include="..\..\binding\SkiaSharp\SkiaSharp.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.SceneGraph\SkiaSharp.SceneGraph.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.Resources\SkiaSharp.Resources.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.Skottie\SkiaSharp.Skottie.csproj" />
<ProjectReference Include="..\..\binding\HarfBuzzSharp\HarfBuzzSharp.csproj" />
<ProjectReference Include="..\..\source\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz.csproj" />
Expand Down
1 change: 1 addition & 0 deletions tests/SkiaSharp.Tests/SkiaSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ItemGroup>
<ProjectReference Include="..\..\binding\HarfBuzzSharp\HarfBuzzSharp.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.SceneGraph\SkiaSharp.SceneGraph.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.Resources\SkiaSharp.Resources.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp.Skottie\SkiaSharp.Skottie.csproj" />
<ProjectReference Include="..\..\binding\SkiaSharp\SkiaSharp.csproj" />
<ProjectReference Include="..\..\source\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz\SkiaSharp.HarfBuzz.csproj" />
Expand Down
38 changes: 38 additions & 0 deletions tests/Tests/Resources/ResourceProviderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.IO;
using SkiaSharp.Resources;
using Xunit;

namespace SkiaSharp.Tests
{
public class ResourceProviderTest : SKTest
{
[SkippableFact]
public void FileResourceProviderCanReadFiles()
{
var fullPath = Path.Combine(PathToImages, "baboon.png");
var expectedData = SKData.Create(fullPath);

using var rp = ResourceProvider.CreateFile(PathToImages);

using var data = rp.Load("baboon.png");

Assert.Equal(expectedData.ToArray(), data.ToArray());
}

[SkippableFact]
public void ProxyProviderCanReadFiles()
{
var fullPath = Path.Combine(PathToImages, "baboon.png");
var expectedData = SKData.Create(fullPath);

using var files = ResourceProvider.CreateFile(PathToImages);
using var datauri = ResourceProvider.CreateDataUri(files);
using var caching = ResourceProvider.CreateCaching(datauri);

using var data = caching.Load("baboon.png");

Assert.Equal(expectedData.ToArray(), data.ToArray());
}
}
}

0 comments on commit 57f2998

Please sign in to comment.