Skip to content

Commit c4073f9

Browse files
authored
Merge pull request #6471 from frenzibyte/ios-avoid-crop
Shave allocation overheads in recent iOS texture loading code
2 parents fe60bf5 + 26f10d0 commit c4073f9

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using osu.Framework.Graphics.Textures;
1313
using osu.Framework.IO.Stores;
1414
using SixLabors.ImageSharp;
15-
using SixLabors.ImageSharp.Processing;
15+
using SixLabors.ImageSharp.Advanced;
1616
using UIKit;
1717

1818
namespace osu.Framework.iOS.Graphics.Textures
@@ -53,21 +53,26 @@ protected override unsafe Image<TPixel> ImageFromStream<TPixel>(Stream stream)
5353
vImageBuffer accelerateImage = default;
5454

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

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

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

66-
var dataSpan = new ReadOnlySpan<byte>(accelerateImage.Data.ToPointer(), bytesCount);
67+
var image = new Image<TPixel>(width, height);
68+
byte* data = (byte*)accelerateImage.Data;
6769

68-
int stride = accelerateImage.BytesPerRow / 4;
69-
var image = Image.LoadPixelData<TPixel>(dataSpan, stride, height);
70-
image.Mutate(i => i.Crop(width, height));
70+
for (int i = 0; i < height; i++)
71+
{
72+
var imageRow = image.DangerousGetPixelRowMemory(i);
73+
var dataRow = new ReadOnlySpan<TPixel>(&data[bytesPerRow * i], width);
74+
dataRow.CopyTo(imageRow.Span);
75+
}
7176

7277
NativeMemory.AlignedFree(accelerateImage.Data.ToPointer());
7378
return image;

0 commit comments

Comments
 (0)