-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathaccelerator_test.go
More file actions
360 lines (300 loc) · 8.75 KB
/
Copy pathaccelerator_test.go
File metadata and controls
360 lines (300 loc) · 8.75 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package gg
import (
"errors"
"log/slog"
"sync"
"testing"
)
// mockAccelerator implements GPUAccelerator and loggerSetter for testing.
type mockAccelerator struct {
name string
initErr error
closed bool
canAccel AcceleratedOp
logger *slog.Logger
mu sync.Mutex
}
func (m *mockAccelerator) Name() string { return m.name }
func (m *mockAccelerator) Init() error { return m.initErr }
func (m *mockAccelerator) Close() {
m.mu.Lock()
m.closed = true
m.mu.Unlock()
}
func (m *mockAccelerator) isClosed() bool {
m.mu.Lock()
defer m.mu.Unlock()
return m.closed
}
func (m *mockAccelerator) CanAccelerate(op AcceleratedOp) bool {
return m.canAccel&op != 0
}
func (m *mockAccelerator) FillPath(_ GPURenderTarget, _ *Path, _ *Paint) error {
return ErrFallbackToCPU
}
func (m *mockAccelerator) StrokePath(_ GPURenderTarget, _ *Path, _ *Paint) error {
return ErrFallbackToCPU
}
func (m *mockAccelerator) FillShape(_ GPURenderTarget, _ DetectedShape, _ *Paint) error {
return ErrFallbackToCPU
}
func (m *mockAccelerator) StrokeShape(_ GPURenderTarget, _ DetectedShape, _ *Paint) error {
return ErrFallbackToCPU
}
func (m *mockAccelerator) Flush(_ GPURenderTarget) error { return nil }
func (m *mockAccelerator) SetLogger(l *slog.Logger) { m.logger = l }
// resetAccelerator clears the global accelerator state between tests.
func resetAccelerator() {
accelMu.Lock()
accel = nil
accelMu.Unlock()
}
func TestRegisterAcceleratorNil(t *testing.T) {
resetAccelerator()
err := RegisterAccelerator(nil)
if err == nil {
t.Fatal("expected error when registering nil accelerator")
}
if err.Error() != "gg: accelerator must not be nil" {
t.Errorf("unexpected error message: %s", err.Error())
}
if Accelerator() != nil {
t.Error("accelerator should remain nil after failed registration")
}
}
func TestRegisterAcceleratorInitError(t *testing.T) {
resetAccelerator()
initErr := errors.New("GPU init failed")
mock := &mockAccelerator{name: "failing", initErr: initErr}
err := RegisterAccelerator(mock)
if err == nil {
t.Fatal("expected error when Init fails")
}
if !errors.Is(err, initErr) {
t.Errorf("expected init error, got: %v", err)
}
if Accelerator() != nil {
t.Error("accelerator should remain nil after Init failure")
}
}
func TestRegisterAcceleratorSuccess(t *testing.T) {
resetAccelerator()
mock := &mockAccelerator{name: "test-gpu", canAccel: AccelFill | AccelStroke}
err := RegisterAccelerator(mock)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
a := Accelerator()
if a == nil {
t.Fatal("expected non-nil accelerator after registration")
}
if a.Name() != "test-gpu" {
t.Errorf("expected name %q, got %q", "test-gpu", a.Name())
}
resetAccelerator()
}
func TestRegisterAcceleratorReplacesOld(t *testing.T) {
resetAccelerator()
first := &mockAccelerator{name: "first"}
second := &mockAccelerator{name: "second"}
if err := RegisterAccelerator(first); err != nil {
t.Fatalf("unexpected error registering first: %v", err)
}
if err := RegisterAccelerator(second); err != nil {
t.Fatalf("unexpected error registering second: %v", err)
}
// First accelerator should be closed.
if !first.isClosed() {
t.Error("expected first accelerator to be closed after replacement")
}
// Second should be current.
a := Accelerator()
if a == nil {
t.Fatal("expected non-nil accelerator")
}
if a.Name() != "second" {
t.Errorf("expected name %q, got %q", "second", a.Name())
}
// Second should NOT be closed.
if second.isClosed() {
t.Error("second accelerator should not be closed")
}
resetAccelerator()
}
func TestAcceleratorReturnsNilWhenNoneRegistered(t *testing.T) {
resetAccelerator()
a := Accelerator()
if a != nil {
t.Errorf("expected nil accelerator, got %v", a)
}
}
func TestAcceleratedOpBitfield(t *testing.T) {
tests := []struct {
name string
combined AcceleratedOp
check AcceleratedOp
want bool
}{
{"fill in fill", AccelFill, AccelFill, true},
{"stroke in stroke", AccelStroke, AccelStroke, true},
{"fill in fill|stroke", AccelFill | AccelStroke, AccelFill, true},
{"stroke in fill|stroke", AccelFill | AccelStroke, AccelStroke, true},
{"scene not in fill|stroke", AccelFill | AccelStroke, AccelScene, false},
{"text not in fill", AccelFill, AccelText, false},
{"all ops combined", AccelFill | AccelStroke | AccelScene | AccelText | AccelImage | AccelGradient | AccelCircleSDF | AccelRRectSDF, AccelGradient, true},
{"circle sdf in sdf ops", AccelCircleSDF | AccelRRectSDF, AccelCircleSDF, true},
{"rrect sdf in sdf ops", AccelCircleSDF | AccelRRectSDF, AccelRRectSDF, true},
{"empty has nothing", 0, AccelFill, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.combined&tt.check != 0
if got != tt.want {
t.Errorf("(%b & %b != 0) = %v, want %v", tt.combined, tt.check, got, tt.want)
}
})
}
}
func TestCanAccelerate(t *testing.T) {
resetAccelerator()
mock := &mockAccelerator{
name: "capable",
canAccel: AccelFill | AccelStroke | AccelScene,
}
if err := RegisterAccelerator(mock); err != nil {
t.Fatalf("unexpected error: %v", err)
}
tests := []struct {
name string
op AcceleratedOp
want bool
}{
{"fill supported", AccelFill, true},
{"stroke supported", AccelStroke, true},
{"scene supported", AccelScene, true},
{"text not supported", AccelText, false},
{"image not supported", AccelImage, false},
{"gradient not supported", AccelGradient, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := Accelerator()
got := a.CanAccelerate(tt.op)
if got != tt.want {
t.Errorf("CanAccelerate(%d) = %v, want %v", tt.op, got, tt.want)
}
})
}
resetAccelerator()
}
func TestCloseAcceleratorClosesAndUnregisters(t *testing.T) {
resetAccelerator()
mock := &mockAccelerator{name: "closeable"}
if err := RegisterAccelerator(mock); err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Verify registered.
if Accelerator() == nil {
t.Fatal("expected non-nil accelerator after registration")
}
CloseAccelerator()
// Verify unregistered.
if Accelerator() != nil {
t.Error("expected nil accelerator after CloseAccelerator")
}
// Verify Close was called.
if !mock.isClosed() {
t.Error("expected accelerator to be closed after CloseAccelerator")
}
}
func TestCloseAcceleratorIdempotent(t *testing.T) {
resetAccelerator()
mock := &mockAccelerator{name: "idempotent"}
if err := RegisterAccelerator(mock); err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Multiple calls should not panic.
CloseAccelerator()
CloseAccelerator()
CloseAccelerator()
if Accelerator() != nil {
t.Error("expected nil accelerator after CloseAccelerator")
}
}
func TestCloseAcceleratorNoop(t *testing.T) {
resetAccelerator()
// Should not panic when no accelerator is registered.
CloseAccelerator()
if Accelerator() != nil {
t.Error("expected nil accelerator")
}
}
func TestErrFallbackToCPU(t *testing.T) {
// Verify ErrFallbackToCPU is usable with errors.Is.
wrapped := errors.New("gpu: texture too large")
_ = wrapped // The sentinel itself is the base case.
if !errors.Is(ErrFallbackToCPU, ErrFallbackToCPU) {
t.Error("ErrFallbackToCPU should match itself with errors.Is")
}
// Verify it works when wrapped.
wrappedErr := errors.Join(ErrFallbackToCPU, errors.New("detail"))
if !errors.Is(wrappedErr, ErrFallbackToCPU) {
t.Error("wrapped ErrFallbackToCPU should be detectable with errors.Is")
}
}
func TestAcceleratedOpValues(t *testing.T) {
// Verify each op has a unique power-of-two value.
ops := []AcceleratedOp{AccelFill, AccelStroke, AccelScene, AccelText, AccelImage, AccelGradient, AccelCircleSDF, AccelRRectSDF}
seen := make(map[AcceleratedOp]bool)
for _, op := range ops {
if op == 0 {
t.Errorf("op value should not be zero")
}
// Verify power of two.
if op&(op-1) != 0 {
t.Errorf("op %d is not a power of two", op)
}
if seen[op] {
t.Errorf("duplicate op value: %d", op)
}
seen[op] = true
}
}
func BenchmarkAcceleratorNilCheck(b *testing.B) {
resetAccelerator()
b.ReportAllocs()
for b.Loop() {
a := Accelerator()
if a != nil {
b.Fatal("should be nil")
}
}
}
func BenchmarkAcceleratorRegistered(b *testing.B) {
resetAccelerator()
mock := &mockAccelerator{name: "bench"}
if err := RegisterAccelerator(mock); err != nil {
b.Fatalf("unexpected error: %v", err)
}
defer resetAccelerator()
b.ReportAllocs()
for b.Loop() {
a := Accelerator()
if a == nil {
b.Fatal("should not be nil")
}
}
}
func BenchmarkCanAccelerate(b *testing.B) {
resetAccelerator()
mock := &mockAccelerator{name: "bench", canAccel: AccelFill | AccelStroke}
if err := RegisterAccelerator(mock); err != nil {
b.Fatalf("unexpected error: %v", err)
}
defer resetAccelerator()
a := Accelerator()
b.ReportAllocs()
for b.Loop() {
_ = a.CanAccelerate(AccelFill)
}
}