Skip to content

Commit cb76f44

Browse files
authored
Merge pull request SixLabors#607 from SixLabors/af/memory-bridge
Introduce basic Memory<T> API-s
2 parents 7e4e765 + 4d7bd2c commit cb76f44

File tree

14 files changed

+205
-167
lines changed

14 files changed

+205
-167
lines changed

src/ImageSharp.Drawing/Primitives/Region.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using SixLabors.Primitives;
56

67
namespace SixLabors.ImageSharp.Primitives
@@ -19,7 +20,7 @@ public abstract class Region
1920
/// Gets the bounding box that entirely surrounds this region.
2021
/// </summary>
2122
/// <remarks>
22-
/// This should always contains all possible points returned from <see cref="Scan(float, float[], int)"/>.
23+
/// This should always contains all possible points returned from <see cref="Scan"/>.
2324
/// </remarks>
2425
public abstract Rectangle Bounds { get; }
2526

@@ -28,8 +29,8 @@ public abstract class Region
2829
/// </summary>
2930
/// <param name="y">The position along the y axis to find intersections.</param>
3031
/// <param name="buffer">The buffer.</param>
31-
/// <param name="offset">The point in the buffer to start setting offset.</param>
32+
/// <param name="configuration">A <see cref="Configuration"/> instance in the context of the caller.</param>
3233
/// <returns>The number of intersections found.</returns>
33-
public abstract int Scan(float y, float[] buffer, int offset);
34+
public abstract int Scan(float y, Span<float> buffer, Configuration configuration);
3435
}
3536
}

src/ImageSharp.Drawing/Primitives/ShapeRegion.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using SixLabors.Memory;
56
using SixLabors.Primitives;
67
using SixLabors.Shapes;
78

@@ -39,21 +40,23 @@ public ShapeRegion(IPath shape)
3940
public override Rectangle Bounds { get; }
4041

4142
/// <inheritdoc/>
42-
public override int Scan(float y, float[] buffer, int offset)
43+
public override int Scan(float y, Span<float> buffer, Configuration configuration)
4344
{
4445
var start = new PointF(this.Bounds.Left - 1, y);
4546
var end = new PointF(this.Bounds.Right + 1, y);
4647

47-
// TODO: This is a temporary workaround because of the lack of Span<T> API-s on IPath. We should use MemoryManager.Allocate() here!
48-
var innerBuffer = new PointF[buffer.Length];
49-
int count = this.Shape.FindIntersections(start, end, innerBuffer, 0);
50-
51-
for (int i = 0; i < count; i++)
48+
using (IBuffer<PointF> tempBuffer = configuration.MemoryAllocator.Allocate<PointF>(buffer.Length))
5249
{
53-
buffer[i + offset] = innerBuffer[i].X;
54-
}
50+
Span<PointF> innerBuffer = tempBuffer.GetSpan();
51+
int count = this.Shape.FindIntersections(start, end, innerBuffer);
5552

56-
return count;
53+
for (int i = 0; i < count; i++)
54+
{
55+
buffer[i] = innerBuffer[i].X;
56+
}
57+
58+
return count;
59+
}
5760
}
5861
}
5962
}

src/ImageSharp.Drawing/Processing/Drawing/Brushes/BrushApplicator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
using System;
55
using SixLabors.ImageSharp.Advanced;
6-
using SixLabors.ImageSharp.Memory;
76
using SixLabors.ImageSharp.PixelFormats;
7+
using SixLabors.Memory;
88

99
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
1010
{
@@ -65,13 +65,13 @@ internal BrushApplicator(ImageFrame<TPixel> target, GraphicsOptions options)
6565
/// <remarks>scanlineBuffer will be > scanlineWidth but provide and offset in case we want to share a larger buffer across runs.</remarks>
6666
internal virtual void Apply(Span<float> scanline, int x, int y)
6767
{
68-
MemoryManager memoryManager = this.Target.MemoryManager;
68+
MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
6969

70-
using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length))
71-
using (IBuffer<TPixel> overlay = memoryManager.Allocate<TPixel>(scanline.Length))
70+
using (IBuffer<float> amountBuffer = memoryAllocator.Allocate<float>(scanline.Length))
71+
using (IBuffer<TPixel> overlay = memoryAllocator.Allocate<TPixel>(scanline.Length))
7272
{
73-
Span<float> amountSpan = amountBuffer.Span;
74-
Span<TPixel> overlaySpan = overlay.Span;
73+
Span<float> amountSpan = amountBuffer.GetSpan();
74+
Span<TPixel> overlaySpan = overlay.GetSpan();
7575

7676
for (int i = 0; i < scanline.Length; i++)
7777
{
@@ -88,7 +88,7 @@ internal virtual void Apply(Span<float> scanline, int x, int y)
8888
}
8989

9090
Span<TPixel> destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
91-
this.Blender.Blend(memoryManager, destinationRow, destinationRow, overlaySpan, amountSpan);
91+
this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, overlaySpan, amountSpan);
9292
}
9393
}
9494
}

src/ImageSharp.Drawing/Processing/Drawing/Brushes/ImageBrush{TPixel}.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
using System;
55
using SixLabors.ImageSharp.Advanced;
6-
using SixLabors.ImageSharp.Memory;
76
using SixLabors.ImageSharp.PixelFormats;
7+
using SixLabors.Memory;
88
using SixLabors.Primitives;
99

1010
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
@@ -118,11 +118,11 @@ public override void Dispose()
118118
internal override void Apply(Span<float> scanline, int x, int y)
119119
{
120120
// Create a span for colors
121-
using (IBuffer<float> amountBuffer = this.Target.MemoryManager.Allocate<float>(scanline.Length))
122-
using (IBuffer<TPixel> overlay = this.Target.MemoryManager.Allocate<TPixel>(scanline.Length))
121+
using (IBuffer<float> amountBuffer = this.Target.MemoryAllocator.Allocate<float>(scanline.Length))
122+
using (IBuffer<TPixel> overlay = this.Target.MemoryAllocator.Allocate<TPixel>(scanline.Length))
123123
{
124-
Span<float> amountSpan = amountBuffer.Span;
125-
Span<TPixel> overlaySpan = overlay.Span;
124+
Span<float> amountSpan = amountBuffer.GetSpan();
125+
Span<TPixel> overlaySpan = overlay.GetSpan();
126126

127127
int sourceY = (y - this.offsetY) % this.yLength;
128128
int offsetX = x - this.offsetX;
@@ -138,7 +138,7 @@ internal override void Apply(Span<float> scanline, int x, int y)
138138
}
139139

140140
Span<TPixel> destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
141-
this.Blender.Blend(this.source.MemoryManager, destinationRow, destinationRow, overlaySpan, amountSpan);
141+
this.Blender.Blend(this.source.MemoryAllocator, destinationRow, destinationRow, overlaySpan, amountSpan);
142142
}
143143
}
144144
}

src/ImageSharp.Drawing/Processing/Drawing/Brushes/PatternBrush{TPixel}.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System;
55
using System.Numerics;
66
using SixLabors.ImageSharp.Advanced;
7-
using SixLabors.ImageSharp.Memory;
87
using SixLabors.ImageSharp.PixelFormats;
98
using SixLabors.ImageSharp.Primitives;
9+
using SixLabors.Memory;
1010
using SixLabors.Primitives;
1111

1212
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
@@ -151,13 +151,13 @@ public override void Dispose()
151151
internal override void Apply(Span<float> scanline, int x, int y)
152152
{
153153
int patternY = y % this.pattern.Rows;
154-
MemoryManager memoryManager = this.Target.MemoryManager;
154+
MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
155155

156-
using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length))
157-
using (IBuffer<TPixel> overlay = memoryManager.Allocate<TPixel>(scanline.Length))
156+
using (IBuffer<float> amountBuffer = memoryAllocator.Allocate<float>(scanline.Length))
157+
using (IBuffer<TPixel> overlay = memoryAllocator.Allocate<TPixel>(scanline.Length))
158158
{
159-
Span<float> amountSpan = amountBuffer.Span;
160-
Span<TPixel> overlaySpan = overlay.Span;
159+
Span<float> amountSpan = amountBuffer.GetSpan();
160+
Span<TPixel> overlaySpan = overlay.GetSpan();
161161

162162
for (int i = 0; i < scanline.Length; i++)
163163
{
@@ -168,7 +168,7 @@ internal override void Apply(Span<float> scanline, int x, int y)
168168
}
169169

170170
Span<TPixel> destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
171-
this.Blender.Blend(memoryManager, destinationRow, destinationRow, overlaySpan, amountSpan);
171+
this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, overlaySpan, amountSpan);
172172
}
173173
}
174174
}

src/ImageSharp.Drawing/Processing/Drawing/Brushes/RecolorBrush{TPixel}.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System;
55
using System.Numerics;
66
using SixLabors.ImageSharp.Advanced;
7-
using SixLabors.ImageSharp.Memory;
87
using SixLabors.ImageSharp.PixelFormats;
8+
using SixLabors.Memory;
99
using SixLabors.Primitives;
1010

1111
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
@@ -136,13 +136,13 @@ public override void Dispose()
136136
/// <inheritdoc />
137137
internal override void Apply(Span<float> scanline, int x, int y)
138138
{
139-
MemoryManager memoryManager = this.Target.MemoryManager;
139+
MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
140140

141-
using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length))
142-
using (IBuffer<TPixel> overlay = memoryManager.Allocate<TPixel>(scanline.Length))
141+
using (IBuffer<float> amountBuffer = memoryAllocator.Allocate<float>(scanline.Length))
142+
using (IBuffer<TPixel> overlay = memoryAllocator.Allocate<TPixel>(scanline.Length))
143143
{
144-
Span<float> amountSpan = amountBuffer.Span;
145-
Span<TPixel> overlaySpan = overlay.Span;
144+
Span<float> amountSpan = amountBuffer.GetSpan();
145+
Span<TPixel> overlaySpan = overlay.GetSpan();
146146

147147
for (int i = 0; i < scanline.Length; i++)
148148
{
@@ -156,7 +156,7 @@ internal override void Apply(Span<float> scanline, int x, int y)
156156
}
157157

158158
Span<TPixel> destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
159-
this.Blender.Blend(memoryManager, destinationRow, destinationRow, overlaySpan, amountSpan);
159+
this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, overlaySpan, amountSpan);
160160
}
161161
}
162162
}

src/ImageSharp.Drawing/Processing/Drawing/Brushes/SolidBrush{TPixel}.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
using System;
55
using SixLabors.ImageSharp.Advanced;
6-
using SixLabors.ImageSharp.Memory;
76
using SixLabors.ImageSharp.PixelFormats;
7+
using SixLabors.Memory;
88
using SixLabors.Primitives;
99

1010
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
@@ -58,8 +58,8 @@ private class SolidBrushApplicator : BrushApplicator<TPixel>
5858
public SolidBrushApplicator(ImageFrame<TPixel> source, TPixel color, GraphicsOptions options)
5959
: base(source, options)
6060
{
61-
this.Colors = source.MemoryManager.Allocate<TPixel>(source.Width);
62-
this.Colors.Span.Fill(color);
61+
this.Colors = source.MemoryAllocator.Allocate<TPixel>(source.Width);
62+
this.Colors.GetSpan().Fill(color);
6363
}
6464

6565
/// <summary>
@@ -75,7 +75,7 @@ public SolidBrushApplicator(ImageFrame<TPixel> source, TPixel color, GraphicsOpt
7575
/// <returns>
7676
/// The color
7777
/// </returns>
78-
internal override TPixel this[int x, int y] => this.Colors.Span[x];
78+
internal override TPixel this[int x, int y] => this.Colors.GetSpan()[x];
7979

8080
/// <inheritdoc />
8181
public override void Dispose()
@@ -88,24 +88,24 @@ internal override void Apply(Span<float> scanline, int x, int y)
8888
{
8989
Span<TPixel> destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
9090

91-
MemoryManager memoryManager = this.Target.MemoryManager;
91+
MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
9292

9393
if (this.Options.BlendPercentage == 1f)
9494
{
95-
this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, scanline);
95+
this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, this.Colors.GetSpan(), scanline);
9696
}
9797
else
9898
{
99-
using (IBuffer<float> amountBuffer = memoryManager.Allocate<float>(scanline.Length))
99+
using (IBuffer<float> amountBuffer = memoryAllocator.Allocate<float>(scanline.Length))
100100
{
101-
Span<float> amountSpan = amountBuffer.Span;
101+
Span<float> amountSpan = amountBuffer.GetSpan();
102102

103103
for (int i = 0; i < scanline.Length; i++)
104104
{
105105
amountSpan[i] = scanline[i] * this.Options.BlendPercentage;
106106
}
107107

108-
this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, amountSpan);
108+
this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, this.Colors.GetSpan(), amountSpan);
109109
}
110110
}
111111
}

src/ImageSharp.Drawing/Processing/Drawing/Processors/DrawImageProcessor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System;
55
using System.Threading.Tasks;
66
using SixLabors.ImageSharp.Advanced;
7-
using SixLabors.ImageSharp.Memory;
87
using SixLabors.ImageSharp.PixelFormats;
98
using SixLabors.ImageSharp.Processing.Processors;
9+
using SixLabors.Memory;
1010
using SixLabors.Primitives;
1111

1212
namespace SixLabors.ImageSharp.Processing.Drawing.Processors
@@ -133,11 +133,11 @@ protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle source
133133

134134
int width = maxX - minX;
135135

136-
MemoryManager memoryManager = this.Image.GetConfiguration().MemoryManager;
136+
MemoryAllocator memoryAllocator = this.Image.GetConfiguration().MemoryAllocator;
137137

138-
using (IBuffer<float> amount = memoryManager.Allocate<float>(width))
138+
using (IBuffer<float> amount = memoryAllocator.Allocate<float>(width))
139139
{
140-
amount.Span.Fill(this.Opacity);
140+
amount.GetSpan().Fill(this.Opacity);
141141

142142
Parallel.For(
143143
minY,
@@ -147,7 +147,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle source
147147
{
148148
Span<TPixel> background = source.GetPixelRowSpan(y).Slice(minX, width);
149149
Span<TPixel> foreground = targetImage.GetPixelRowSpan(y - locationY).Slice(targetX, width);
150-
blender.Blend(memoryManager, background, background, foreground, amount.Span);
150+
blender.Blend(memoryAllocator, background, background, foreground, amount.GetSpan());
151151
});
152152
}
153153
}

src/ImageSharp.Drawing/Processing/Drawing/Processors/FillProcessor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
using System;
55
using System.Threading.Tasks;
66
using SixLabors.ImageSharp.Advanced;
7-
using SixLabors.ImageSharp.Memory;
87
using SixLabors.ImageSharp.PixelFormats;
98
using SixLabors.ImageSharp.Processing.Drawing.Brushes;
109
using SixLabors.ImageSharp.Processing.Processors;
10+
using SixLabors.Memory;
1111
using SixLabors.Primitives;
1212

1313
namespace SixLabors.ImageSharp.Processing.Drawing.Processors
@@ -77,13 +77,13 @@ protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle source
7777
startY = 0;
7878
}
7979

80-
using (IBuffer<float> amount = source.MemoryManager.Allocate<float>(width))
80+
using (IBuffer<float> amount = source.MemoryAllocator.Allocate<float>(width))
8181
using (BrushApplicator<TPixel> applicator = this.brush.CreateApplicator(
8282
source,
8383
sourceRectangle,
8484
this.options))
8585
{
86-
amount.Span.Fill(1f);
86+
amount.GetSpan().Fill(1f);
8787

8888
Parallel.For(
8989
minY,
@@ -94,7 +94,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle source
9494
int offsetY = y - startY;
9595
int offsetX = minX - startX;
9696

97-
applicator.Apply(amount.Span, offsetX, offsetY);
97+
applicator.Apply(amount.GetSpan(), offsetX, offsetY);
9898
});
9999
}
100100
}

0 commit comments

Comments
 (0)