Skip to content

Commit db9d7e6

Browse files
committed
wip
1 parent fe60bf5 commit db9d7e6

19 files changed

+502
-46
lines changed

osu-framework.sln.DotSettings

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@
342342
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=API/@EntryIndexedValue">API</s:String>
343343
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ARGB/@EntryIndexedValue">ARGB</s:String>
344344
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BPM/@EntryIndexedValue">BPM</s:String>
345+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CG/@EntryIndexedValue">CG</s:String>
345346
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FBO/@EntryIndexedValue">FBO</s:String>
346347
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CCL/@EntryIndexedValue">CCL</s:String>
347348
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GC/@EntryIndexedValue">GC</s:String>

osu.Framework.iOS/Graphics/Textures/IOSTextureLoaderStore.cs

+17-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
using ObjCRuntime;
1212
using osu.Framework.Graphics.Textures;
1313
using osu.Framework.IO.Stores;
14+
using osu.Framework.Logging;
1415
using SixLabors.ImageSharp;
16+
using SixLabors.ImageSharp.Advanced;
1517
using SixLabors.ImageSharp.Processing;
1618
using UIKit;
1719

@@ -22,10 +24,16 @@ public class IOSTextureLoaderStore : TextureLoaderStore
2224
public IOSTextureLoaderStore(IResourceStore<byte[]> store)
2325
: base(store)
2426
{
27+
if (!stopwatch.IsRunning)
28+
stopwatch.Start();
2529
}
2630

31+
private static readonly Stopwatch stopwatch = new Stopwatch();
32+
2733
protected override unsafe Image<TPixel> ImageFromStream<TPixel>(Stream stream)
2834
{
35+
double time = stopwatch.ElapsedMilliseconds;
36+
2937
using (var nativeData = NSData.FromStream(stream))
3038
{
3139
if (nativeData == null)
@@ -57,19 +65,23 @@ protected override unsafe Image<TPixel> ImageFromStream<TPixel>(Stream stream)
5765
Debug.Assert(alignment > 0);
5866

5967
// allocate aligned memory region to contain image pixel data.
60-
int bytesCount = accelerateImage.BytesPerRow * accelerateImage.Height;
6168
accelerateImage.Data = (IntPtr)NativeMemory.AlignedAlloc((nuint)(accelerateImage.BytesPerRow * accelerateImage.Height), (nuint)alignment);
6269

6370
var result = vImageBuffer_InitWithCGImage(&accelerateImage, &format, null, uiImage.CGImage!.Handle, vImageFlags.NoAllocate);
6471
Debug.Assert(result == vImageError.NoError);
6572

66-
var dataSpan = new ReadOnlySpan<byte>(accelerateImage.Data.ToPointer(), bytesCount);
73+
var image = new Image<TPixel>(width, height);
6774

68-
int stride = accelerateImage.BytesPerRow / 4;
69-
var image = Image.LoadPixelData<TPixel>(dataSpan, stride, height);
70-
image.Mutate(i => i.Crop(width, height));
75+
for (int i = 0; i < height; i++)
76+
{
77+
var imageRow = image.DangerousGetPixelRowMemory(i);
78+
var dataRow = new ReadOnlySpan<TPixel>((byte*)accelerateImage.Data + i * accelerateImage.BytesPerRow, width);
79+
dataRow.CopyTo(imageRow.Span);
80+
}
7181

7282
NativeMemory.AlignedFree(accelerateImage.Data.ToPointer());
83+
84+
Logger.Log($"IOSTextureLoaderStore: texture loading spent {stopwatch.ElapsedMilliseconds - time:0.00}ms");
7385
return image;
7486
}
7587
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
// ReSharper disable InconsistentNaming
8+
9+
namespace osu.Framework.Platform.Apple.Native
10+
{
11+
internal partial class Accelerate
12+
{
13+
private const string library = "/System/Library/Frameworks/Accelerate.framework/Accelerate";
14+
15+
[LibraryImport(library)]
16+
internal static unsafe partial vImageError vImageBuffer_Init(vImageBuffer* buf, uint height, uint width, uint pixelBits, vImageFlags flags);
17+
18+
[LibraryImport(library)]
19+
internal static unsafe partial vImageError vImageBuffer_InitWithCGImage(vImageBuffer* buf, vImage_CGImageFormat* format, double* backgroundColour, IntPtr image, vImageFlags flags);
20+
21+
public enum vImageError : long
22+
{
23+
OutOfPlaceOperationRequired = -21780, // 0xFFFFFFFFFFFFAAEC
24+
ColorSyncIsAbsent = -21779, // 0xFFFFFFFFFFFFAAED
25+
InvalidImageFormat = -21778, // 0xFFFFFFFFFFFFAAEE
26+
InvalidRowBytes = -21777, // 0xFFFFFFFFFFFFAAEF
27+
InternalError = -21776, // 0xFFFFFFFFFFFFAAF0
28+
UnknownFlagsBit = -21775, // 0xFFFFFFFFFFFFAAF1
29+
BufferSizeMismatch = -21774, // 0xFFFFFFFFFFFFAAF2
30+
InvalidParameter = -21773, // 0xFFFFFFFFFFFFAAF3
31+
NullPointerArgument = -21772, // 0xFFFFFFFFFFFFAAF4
32+
MemoryAllocationError = -21771, // 0xFFFFFFFFFFFFAAF5
33+
InvalidOffsetY = -21770, // 0xFFFFFFFFFFFFAAF6
34+
InvalidOffsetX = -21769, // 0xFFFFFFFFFFFFAAF7
35+
InvalidEdgeStyle = -21768, // 0xFFFFFFFFFFFFAAF8
36+
InvalidKernelSize = -21767, // 0xFFFFFFFFFFFFAAF9
37+
RoiLargerThanInputBuffer = -21766, // 0xFFFFFFFFFFFFAAFA
38+
NoError = 0,
39+
}
40+
41+
public enum vImageFlags : uint
42+
{
43+
NoFlags = 0,
44+
LeaveAlphaUnchanged = 1,
45+
CopyInPlace = 2,
46+
BackgroundColorFill = 4,
47+
EdgeExtend = 8,
48+
DoNotTile = 16, // 0x00000010
49+
HighQualityResampling = 32, // 0x00000020
50+
TruncateKernel = 64, // 0x00000040
51+
GetTempBufferSize = 128, // 0x00000080
52+
PrintDiagnosticsToConsole = 256, // 0x00000100
53+
NoAllocate = 512, // 0x00000200
54+
}
55+
56+
public unsafe struct vImageBuffer
57+
{
58+
public byte* Data;
59+
public nuint Height;
60+
public nuint Width;
61+
public nuint BytesPerRow;
62+
}
63+
64+
public unsafe struct vImage_CGImageFormat
65+
{
66+
public uint BitsPerComponent;
67+
public uint BitsPerPixel;
68+
public CGColorSpace ColorSpace;
69+
public CGBitmapFlags BitmapInfo;
70+
public uint Version;
71+
public double* Decode;
72+
public CGColorRenderingIntent RenderingIntent;
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
namespace osu.Framework.Platform.Apple.Native
8+
{
9+
internal readonly partial struct CGBitmapContext
10+
{
11+
internal IntPtr Handle { get; }
12+
13+
internal CGBitmapContext(IntPtr handle)
14+
{
15+
Handle = handle;
16+
}
17+
18+
[LibraryImport(CGColorSpace.LIB_CORE_GRAPHICS, EntryPoint = "CGBitmapContextCreate")]
19+
public static partial CGBitmapContext Create(IntPtr data, nuint width, nuint height, nuint bitsPerComponent, nuint bytesPerRow, CGColorSpace colorSpace, CGBitmapFlags bitmapInfo);
20+
21+
[LibraryImport(CGColorSpace.LIB_CORE_GRAPHICS, EntryPoint = "CGContextDrawImage")]
22+
public static partial void DrawImage(CGBitmapContext context, CGRect rect, CGImage image);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
namespace osu.Framework.Platform.Apple.Native
5+
{
6+
public enum CGBitmapFlags : uint
7+
{
8+
None = 0,
9+
PremultipliedLast = 1,
10+
PremultipliedFirst = 2,
11+
Last = PremultipliedFirst | PremultipliedLast, // 0x00000003
12+
First = 4,
13+
NoneSkipLast = First | PremultipliedLast, // 0x00000005
14+
NoneSkipFirst = First | PremultipliedFirst, // 0x00000006
15+
Only = NoneSkipFirst | PremultipliedLast, // 0x00000007
16+
AlphaInfoMask = 31, // 0x0000001F
17+
FloatInfoMask = 3840, // 0x00000F00
18+
FloatComponents = 256, // 0x00000100
19+
ByteOrderMask = 28672, // 0x00007000
20+
ByteOrderDefault = 0,
21+
ByteOrder16Little = 4096, // 0x00001000
22+
ByteOrder32Little = 8192, // 0x00002000
23+
ByteOrder16Big = ByteOrder32Little | ByteOrder16Little, // 0x00003000
24+
ByteOrder32Big = 16384, // 0x00004000
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
namespace osu.Framework.Platform.Apple.Native
5+
{
6+
public enum CGColorRenderingIntent
7+
{
8+
Default,
9+
AbsoluteColorimetric,
10+
RelativeColorimetric,
11+
Perceptual,
12+
Saturation,
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
namespace osu.Framework.Platform.Apple.Native
8+
{
9+
public readonly partial struct CGColorSpace
10+
{
11+
internal const string LIB_CORE_GRAPHICS = "/System/Library/Frameworks/CoreGraphics.framework/Versions/Current/CoreGraphics";
12+
13+
internal IntPtr Handle { get; }
14+
15+
internal CGColorSpace(IntPtr handle)
16+
{
17+
Handle = handle;
18+
}
19+
20+
[LibraryImport(LIB_CORE_GRAPHICS, EntryPoint = "CGColorSpaceCreateDeviceRGB")]
21+
internal static partial CGColorSpace CreateDeviceRGB();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
namespace osu.Framework.Platform.Apple.Native
8+
{
9+
internal readonly partial struct CGImage
10+
{
11+
internal IntPtr Handle { get; }
12+
13+
public CGImage(IntPtr handle)
14+
{
15+
Handle = handle;
16+
}
17+
18+
[LibraryImport(CGColorSpace.LIB_CORE_GRAPHICS, EntryPoint = "CGImageRelease")]
19+
public static partial void Release(CGImage image);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
namespace osu.Framework.Platform.Apple.Native
5+
{
6+
internal enum CGImageAlphaInfo : uint
7+
{
8+
None,
9+
PremultipliedLast,
10+
PremultipliedFirst,
11+
Last,
12+
First,
13+
NoneSkipLast,
14+
NoneSkipFirst,
15+
Only,
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
namespace osu.Framework.Platform.Apple.Native
5+
{
6+
internal struct CGPoint
7+
{
8+
internal double X;
9+
internal double Y;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
namespace osu.Framework.Platform.Apple.Native
5+
{
6+
internal struct CGRect
7+
{
8+
internal CGPoint Origin;
9+
internal CGSize Size;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
namespace osu.Framework.Platform.Apple.Native
5+
{
6+
internal struct CGSize
7+
{
8+
internal double Width;
9+
internal double Height;
10+
}
11+
}

osu.Framework/Platform/MacOS/MacOSGameHost.cs

+5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using System.Collections.Generic;
88
using System.IO;
99
using System.Linq;
10+
using osu.Framework.Graphics.Textures;
1011
using osu.Framework.Input;
1112
using osu.Framework.Input.Bindings;
1213
using osu.Framework.Input.Handlers;
1314
using osu.Framework.Input.Handlers.Mouse;
15+
using osu.Framework.IO.Stores;
1416
using osu.Framework.Logging;
1517
using osu.Framework.Platform.MacOS.Native;
1618

@@ -44,6 +46,9 @@ public override IEnumerable<string> UserStoragePaths
4446

4547
protected override ReadableKeyCombinationProvider CreateReadableKeyCombinationProvider() => new MacOSReadableKeyCombinationProvider();
4648

49+
public override IResourceStore<TextureUpload> CreateTextureLoaderStore(IResourceStore<byte[]> underlyingStore)
50+
=> new MacOSTextureLoaderStore(underlyingStore);
51+
4752
protected override void Swap()
4853
{
4954
base.Swap();

0 commit comments

Comments
 (0)