-
Notifications
You must be signed in to change notification settings - Fork 49
/
PixelBuffer.cs
218 lines (174 loc) · 5.62 KB
/
PixelBuffer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright © Clinton Ingram and Contributors. Licensed under the MIT License.
using System;
using System.Runtime.CompilerServices;
namespace PhotoSauce.MagicScaler;
internal sealed class PixelBuffer<T>(int minLines, int stride, T param = default) : IDisposable where T : struct, BufferType
{
private readonly T tval = param;
private int capacity = minLines;
private int read = typeof(T) == typeof(BufferType.Windowed) ? minLines : 0;
private int start;
private int loaded;
private byte[]? buffArray;
private byte buffOffset;
public readonly int Stride = stride;
private int buffLength
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => window + capacity * Stride;
}
private int window
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (typeof(T) == typeof(BufferType.Windowed))
{
var wval = (BufferType.Windowed)(object)tval;
return wval.Window;
}
return 0;
}
}
private int consumed
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => typeof(T) == typeof(BufferType.Windowed) ? loaded : read;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
if (typeof(T) != typeof(BufferType.Windowed))
read = value;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private (int, int) getValidRange() => typeof(T) == typeof(BufferType.Windowed) && loaded > read
? (start + loaded - read, read)
: (start, loaded);
private Span<byte> init(int first, int lines)
{
if (buffArray is null)
{
if (capacity == 0)
ThrowHelper.ThrowObjectDisposed(nameof(PixelBuffer<T>));
var buff = BufferPool.RentRawAligned(checked(window + Math.Max(capacity, lines) * Stride));
buffArray = buff.Array!;
buffOffset = (byte)buff.Offset;
capacity = (buffArray.Length - buffOffset - window) / Stride;
if (typeof(T) == typeof(BufferType.Sliding) || typeof(T) == typeof(BufferType.Windowed))
Unsafe.InitBlockUnaligned(ref buffArray[(nuint)buffOffset], 0, (uint)buffLength);
}
else if (lines > capacity)
{
grow(0, 0);
}
start = first;
loaded = lines;
consumed = 0;
return new Span<byte>(buffArray, buffOffset, window != 0 ? window : lines * Stride);
}
private void grow(int cbKeep, int cbKill)
{
var tbuff = BufferPool.RentRawAligned(checked(window + capacity * Stride * 2));
if (cbKeep > 0)
Unsafe.CopyBlockUnaligned(ref tbuff.Array![tbuff.Offset], ref buffArray![buffOffset + cbKill], (uint)cbKeep);
BufferPool.ReturnRaw(new ArraySegment<byte>(buffArray!, buffOffset, buffLength));
buffArray = tbuff.Array!;
buffOffset = (byte)tbuff.Offset;
capacity = (buffArray.Length - buffOffset - window) / Stride;
if (typeof(T) == typeof(BufferType.Sliding) || typeof(T) == typeof(BufferType.Windowed))
Unsafe.InitBlockUnaligned(ref buffArray[buffOffset + cbKeep], 0, (uint)(buffLength - cbKeep));
}
private unsafe void slide(int cbKeep, int cbKill)
{
fixed (byte* pb = &buffArray![(nuint)buffOffset])
Buffer.MemoryCopy(pb + cbKill, pb, buffLength, cbKeep);
}
public Span<byte> PrepareLoad(scoped ref int first, scoped ref int lines)
{
var (firstValid, linesValid) = getValidRange();
if (buffArray is null || first < firstValid || first > (firstValid + linesValid))
return init(first, lines);
int toLoad, toKeep;
int next = first;
if (first + lines <= start + capacity)
{
toKeep = loaded;
toLoad = (first + lines) - (start + loaded);
next = start + loaded;
}
else
{
int newStart = Math.Min(start + consumed, first);
toKeep = Math.Max(start + loaded - newStart, 0);
toLoad = (first + lines) - (newStart + toKeep);
next += lines - toLoad;
int toKill = loaded - toKeep;
if (toKeep != 0)
{
int cbKeep = window == 0 ? toKeep * Stride : window - (Math.Min(lines, loaded) - toKeep) * Stride;
int cbKill = toKill * Stride;
if (capacity - toKeep < toLoad)
grow(cbKeep, cbKill);
else
slide(cbKeep, cbKill);
}
start = newStart;
consumed -= toKill;
}
loaded = toKeep + toLoad;
if (typeof(T) == typeof(BufferType.Caching))
return default;
int reqlines = lines;
lines = toLoad;
first = next;
return new Span<byte>(buffArray, buffOffset + (next - start) * Stride, window == 0 ? toLoad * Stride : window - (reqlines - toLoad) * Stride);
}
public Span<byte> PrepareLoad(int first, int lines)
{
if (typeof(T) == typeof(BufferType.Caching))
{
PrepareLoad(ref first, ref lines);
int offset = first - start;
return new Span<byte>(buffArray, buffOffset + offset * Stride, lines * Stride);
}
if (typeof(T) == typeof(BufferType.Sliding))
{
return PrepareLoad(ref first, ref lines);
}
throw new NotSupportedException();
}
public ReadOnlySpan<byte> PrepareRead(int first, int lines)
{
int offset = first - start;
consumed = Math.Max(consumed, offset + lines);
return new ReadOnlySpan<byte>(buffArray, buffOffset + offset * Stride, window != 0 ? window : lines * Stride);
}
public bool ContainsLine(int line) => ContainsRange(line, 1);
public bool ContainsRange(int first, int lines)
{
var (firstValid, linesValid) = getValidRange();
return first >= firstValid && first + lines <= firstValid + linesValid;
}
public void Reset()
{
start = loaded = 0;
if (typeof(T) != typeof(BufferType.Windowed))
read = 0;
}
public void Dispose()
{
if (buffArray is null)
return;
Reset();
BufferPool.ReturnRaw(new ArraySegment<byte>(buffArray, buffOffset, buffLength));
buffArray = null;
capacity = 0;
}
}
internal interface BufferType
{
public readonly struct Caching : BufferType { }
public readonly struct Sliding : BufferType { }
public readonly struct Windowed(int w) : BufferType { public readonly int Window = w; }
}