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

Add native macOS image loading support #6475

Merged
merged 7 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
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 osu-framework.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=API/@EntryIndexedValue">API</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ARGB/@EntryIndexedValue">ARGB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BPM/@EntryIndexedValue">BPM</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CG/@EntryIndexedValue">CG</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=FBO/@EntryIndexedValue">FBO</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CCL/@EntryIndexedValue">CCL</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GC/@EntryIndexedValue">GC</s:String>
Expand Down
91 changes: 11 additions & 80 deletions osu.Framework.iOS/Graphics/Textures/IOSTextureLoaderStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Accelerate;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Framework.Platform.Apple;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using UIKit;

namespace osu.Framework.iOS.Graphics.Textures
{
public class IOSTextureLoaderStore : TextureLoaderStore
internal class IOSTextureLoaderStore : AppleTextureLoaderStore
{
public IOSTextureLoaderStore(IResourceStore<byte[]> store)
: base(store)
Expand All @@ -26,81 +20,18 @@ public IOSTextureLoaderStore(IResourceStore<byte[]> store)

protected override unsafe Image<TPixel> ImageFromStream<TPixel>(Stream stream)
{
using (var nativeData = NSData.FromStream(stream))
{
if (nativeData == null)
throw new ArgumentException($"{nameof(Image)} could not be created from {nameof(stream)}.");
int length = (int)(stream.Length - stream.Position);
using var nativeData = NSMutableData.FromLength(length);

using (var uiImage = UIImage.LoadFromData(nativeData))
{
if (uiImage == null) throw new ArgumentException($"{nameof(Image)} could not be created from {nameof(stream)}.");
var bytesSpan = new Span<byte>(nativeData.MutableBytes.ToPointer(), length);
stream.ReadExactly(bytesSpan);

int width = (int)uiImage.Size.Width;
int height = (int)uiImage.Size.Height;
using var uiImage = UIImage.LoadFromData(nativeData);
if (uiImage == null)
throw new ArgumentException($"{nameof(Image)} could not be created from {nameof(stream)}.");

var format = new vImage_CGImageFormat
{
BitsPerComponent = 8,
BitsPerPixel = 32,
ColorSpace = CGColorSpace.CreateDeviceRGB().Handle,
// notably, iOS generally uses premultiplied alpha when rendering image to pixels via CGBitmapContext or otherwise,
// but vImage offers using straight alpha directly without any conversion from our side (by specifying Last instead of PremultipliedLast).
BitmapInfo = (CGBitmapFlags)CGImageAlphaInfo.Last,
Decode = null,
RenderingIntent = CGColorRenderingIntent.Default,
};

vImageBuffer accelerateImage = default;

// perform initial call to retrieve preferred alignment and bytes-per-row values for the given image dimensions.
nuint alignment = (nuint)vImageBuffer_Init(&accelerateImage, (uint)height, (uint)width, 32, vImageFlags.NoAllocate);
Debug.Assert(alignment > 0);

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

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

var image = new Image<TPixel>(width, height);
byte* data = (byte*)accelerateImage.Data;

for (int i = 0; i < height; i++)
{
var imageRow = image.DangerousGetPixelRowMemory(i);
var dataRow = new ReadOnlySpan<TPixel>(&data[bytesPerRow * i], width);
dataRow.CopyTo(imageRow.Span);
}

NativeMemory.AlignedFree(accelerateImage.Data.ToPointer());
return image;
}
}
var cgImage = new Platform.Apple.Native.CGImage(uiImage.CGImage!.Handle);
return ImageFromCGImage<TPixel>(cgImage);
}

#region Accelerate API

[DllImport(Constants.AccelerateLibrary)]
private static extern unsafe vImageError vImageBuffer_Init(vImageBuffer* buf, uint height, uint width, uint pixelBits, vImageFlags flags);

[DllImport(Constants.AccelerateLibrary)]
private static extern unsafe vImageError vImageBuffer_InitWithCGImage(vImageBuffer* buf, vImage_CGImageFormat* format, nfloat* backgroundColour, NativeHandle image, vImageFlags flags);

// ReSharper disable once InconsistentNaming
[StructLayout(LayoutKind.Sequential)]
public unsafe struct vImage_CGImageFormat
{
public uint BitsPerComponent;
public uint BitsPerPixel;
public NativeHandle ColorSpace;
public CGBitmapFlags BitmapInfo;
public uint Version;
public nfloat* Decode;
public CGColorRenderingIntent RenderingIntent;
}

#endregion
}
}
69 changes: 69 additions & 0 deletions osu.Framework/Platform/Apple/AppleTextureLoaderStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Framework.Platform.Apple.Native;
using osu.Framework.Platform.Apple.Native.Accelerate;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;

namespace osu.Framework.Platform.Apple
{
internal abstract class AppleTextureLoaderStore : TextureLoaderStore
{
protected AppleTextureLoaderStore(IResourceStore<byte[]> store)
: base(store)
{
}

protected unsafe Image<TPixel> ImageFromCGImage<TPixel>(CGImage cgImage)
where TPixel : unmanaged, IPixel<TPixel>
{
int width = (int)cgImage.Width;
int height = (int)cgImage.Height;

var format = new vImage_CGImageFormat
{
BitsPerComponent = 8,
BitsPerPixel = 32,
ColorSpace = CGColorSpace.CreateDeviceRGB(),
// notably, macOS & iOS generally use premultiplied alpha when rendering image to pixels via CGBitmapContext or otherwise,
// but vImage offers rendering as straight alpha by specifying Last instead of PremultipliedLast.
BitmapInfo = (CGBitmapInfo)CGImageAlphaInfo.Last,
Decode = null,
RenderingIntent = CGColorRenderingIntent.Default,
};

vImage_Buffer accImage = default;

// perform initial call to retrieve preferred alignment and bytes-per-row values for the given image dimensions.
nuint alignment = (nuint)vImage.Init(&accImage, (uint)height, (uint)width, 32, vImage_Flags.NoAllocate);
Debug.Assert(alignment > 0);

// allocate aligned memory region to contain image pixel data.
nuint bytesPerRow = accImage.BytesPerRow;
nuint bytesCount = bytesPerRow * accImage.Height;
accImage.Data = (byte*)NativeMemory.AlignedAlloc(bytesCount, alignment);

var result = vImage.InitWithCGImage(&accImage, &format, null, cgImage.Handle, vImage_Flags.NoAllocate);
Debug.Assert(result == vImage_Error.NoError);

var image = new Image<TPixel>(width, height);

for (int i = 0; i < height; i++)
{
var imageRow = image.DangerousGetPixelRowMemory(i);
var dataRow = new ReadOnlySpan<TPixel>(&accImage.Data[(int)bytesPerRow * i], width);
dataRow.CopyTo(imageRow.Span);
}

NativeMemory.AlignedFree(accImage.Data);
return image;
}
}
}
19 changes: 19 additions & 0 deletions osu.Framework/Platform/Apple/Native/Accelerate/vImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Runtime.InteropServices;

// ReSharper disable InconsistentNaming

namespace osu.Framework.Platform.Apple.Native.Accelerate
{
internal static unsafe partial class vImage
{
[LibraryImport(Interop.LIB_ACCELERATE, EntryPoint = "vImageBuffer_Init")]
internal static partial vImage_Error Init(vImage_Buffer* buf, uint height, uint width, uint pixelBits, vImage_Flags flags);

[LibraryImport(Interop.LIB_ACCELERATE, EntryPoint = "vImageBuffer_InitWithCGImage")]
internal static partial vImage_Error InitWithCGImage(vImage_Buffer* buf, vImage_CGImageFormat* format, double* backgroundColour, IntPtr image, vImage_Flags flags);
}
}
18 changes: 18 additions & 0 deletions osu.Framework/Platform/Apple/Native/Accelerate/vImage_Buffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

// ReSharper disable InconsistentNaming

using System.Runtime.InteropServices;

namespace osu.Framework.Platform.Apple.Native.Accelerate
{
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct vImage_Buffer
{
public byte* Data;
public nuint Height;
public nuint Width;
public nuint BytesPerRow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

// ReSharper disable InconsistentNaming

using System.Runtime.InteropServices;

namespace osu.Framework.Platform.Apple.Native.Accelerate
{
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct vImage_CGImageFormat
{
public uint BitsPerComponent;
public uint BitsPerPixel;
public CGColorSpace ColorSpace;
public CGBitmapInfo BitmapInfo;
public uint Version;
public double* Decode;
public CGColorRenderingIntent RenderingIntent;
}
}
27 changes: 27 additions & 0 deletions osu.Framework/Platform/Apple/Native/Accelerate/vImage_Error.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

// ReSharper disable InconsistentNaming

namespace osu.Framework.Platform.Apple.Native.Accelerate
{
internal enum vImage_Error : long
{
OutOfPlaceOperationRequired = -21780,
ColorSyncIsAbsent = -21779,
InvalidImageFormat = -21778,
InvalidRowBytes = -21777,
InternalError = -21776,
UnknownFlagsBit = -21775,
BufferSizeMismatch = -21774,
InvalidParameter = -21773,
NullPointerArgument = -21772,
MemoryAllocationError = -21771,
InvalidOffsetY = -21770,
InvalidOffsetX = -21769,
InvalidEdgeStyle = -21768,
InvalidKernelSize = -21767,
RoiLargerThanInputBuffer = -21766,
NoError = 0,
}
}
22 changes: 22 additions & 0 deletions osu.Framework/Platform/Apple/Native/Accelerate/vImage_Flags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

// ReSharper disable InconsistentNaming

namespace osu.Framework.Platform.Apple.Native.Accelerate
{
internal enum vImage_Flags : uint
{
NoFlags = 0,
LeaveAlphaUnchanged = 1,
CopyInPlace = 2,
BackgroundColorFill = 4,
EdgeExtend = 8,
DoNotTile = 16,
HighQualityResampling = 32,
TruncateKernel = 64,
GetTempBufferSize = 128,
PrintDiagnosticsToConsole = 256,
NoAllocate = 512,
}
}
24 changes: 24 additions & 0 deletions osu.Framework/Platform/Apple/Native/CGBitmapContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Runtime.InteropServices;

namespace osu.Framework.Platform.Apple.Native
{
internal readonly partial struct CGBitmapContext
{
internal IntPtr Handle { get; }

internal CGBitmapContext(IntPtr handle)
{
Handle = handle;
}

[LibraryImport(Interop.LIB_CORE_GRAPHICS, EntryPoint = "CGBitmapContextCreate")]
public static partial CGBitmapContext Create(IntPtr data, nuint width, nuint height, nuint bitsPerComponent, nuint bytesPerRow, CGColorSpace colorSpace, CGBitmapInfo bitmapInfo);

[LibraryImport(Interop.LIB_CORE_GRAPHICS, EntryPoint = "CGContextDrawImage")]
public static partial void DrawImage(CGBitmapContext context, CGRect rect, CGImage image);
}
}
26 changes: 26 additions & 0 deletions osu.Framework/Platform/Apple/Native/CGBitmapInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace osu.Framework.Platform.Apple.Native
{
public enum CGBitmapInfo : uint
{
None,
PremultipliedLast,
PremultipliedFirst,
Last,
First,
NoneSkipLast,
NoneSkipFirst,
Only,
AlphaInfoMask = 31,
FloatInfoMask = 3840,
FloatComponents = 256,
ByteOrderMask = 28672,
ByteOrderDefault = 0,
ByteOrder16Little = 4096,
ByteOrder32Little = 8192,
ByteOrder16Big = 12288,
ByteOrder32Big = 16384,
}
}
14 changes: 14 additions & 0 deletions osu.Framework/Platform/Apple/Native/CGColorRenderingIntent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace osu.Framework.Platform.Apple.Native
{
public enum CGColorRenderingIntent
{
Default,
AbsoluteColorimetric,
RelativeColorimetric,
Perceptual,
Saturation,
}
}
21 changes: 21 additions & 0 deletions osu.Framework/Platform/Apple/Native/CGColorSpace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Runtime.InteropServices;

namespace osu.Framework.Platform.Apple.Native
{
public readonly partial struct CGColorSpace
{
internal IntPtr Handle { get; }

internal CGColorSpace(IntPtr handle)
{
Handle = handle;
}

[LibraryImport(Interop.LIB_CORE_GRAPHICS, EntryPoint = "CGColorSpaceCreateDeviceRGB")]
internal static partial CGColorSpace CreateDeviceRGB();
}
}
Loading
Loading