-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathaccelerator_integration_test.go
More file actions
326 lines (262 loc) · 7.7 KB
/
Copy pathaccelerator_integration_test.go
File metadata and controls
326 lines (262 loc) · 7.7 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
package gg
import (
"sync"
"testing"
)
// trackingAccelerator is a mock that tracks which methods were called.
type trackingAccelerator struct {
mu sync.Mutex
fillShapeCt int
strokeShapeCt int
fillPathCt int
strokePathCt int
lastShape DetectedShape
}
func (a *trackingAccelerator) Name() string { return "tracking" }
func (a *trackingAccelerator) Init() error { return nil }
func (a *trackingAccelerator) Close() {}
func (a *trackingAccelerator) CanAccelerate(op AcceleratedOp) bool {
return op&(AccelCircleSDF|AccelRRectSDF) != 0
}
func (a *trackingAccelerator) FillPath(_ GPURenderTarget, _ *Path, _ *Paint) error {
a.mu.Lock()
a.fillPathCt++
a.mu.Unlock()
return ErrFallbackToCPU
}
func (a *trackingAccelerator) StrokePath(_ GPURenderTarget, _ *Path, _ *Paint) error {
a.mu.Lock()
a.strokePathCt++
a.mu.Unlock()
return ErrFallbackToCPU
}
func (a *trackingAccelerator) FillShape(target GPURenderTarget, shape DetectedShape, paint *Paint) error {
a.mu.Lock()
a.fillShapeCt++
a.lastShape = shape
a.mu.Unlock()
// Actually render using the SDF accelerator for verification.
sdf := &SDFAccelerator{}
return sdf.FillShape(target, shape, paint)
}
func (a *trackingAccelerator) StrokeShape(target GPURenderTarget, shape DetectedShape, paint *Paint) error {
a.mu.Lock()
a.strokeShapeCt++
a.lastShape = shape
a.mu.Unlock()
sdf := &SDFAccelerator{}
return sdf.StrokeShape(target, shape, paint)
}
func (a *trackingAccelerator) Flush(_ GPURenderTarget) error { return nil }
// flushTrackingAccelerator tracks Flush calls for testing Context.Close() behavior.
type flushTrackingAccelerator struct {
trackingAccelerator
flushCt int
}
func (a *flushTrackingAccelerator) Flush(_ GPURenderTarget) error {
a.mu.Lock()
a.flushCt++
a.mu.Unlock()
return nil
}
func TestContextCloseFlushesGPU(t *testing.T) {
resetAccelerator()
defer resetAccelerator()
tracker := &flushTrackingAccelerator{}
if err := RegisterAccelerator(tracker); err != nil {
t.Fatalf("RegisterAccelerator: %v", err)
}
dc := NewContext(200, 200)
// Draw a circle so there might be pending GPU state.
dc.SetColor(Red.Color())
dc.DrawCircle(100, 100, 30)
if err := dc.Fill(); err != nil {
t.Fatalf("Fill() = %v", err)
}
tracker.mu.Lock()
flushBefore := tracker.flushCt
tracker.mu.Unlock()
// Close should flush the accelerator.
if err := dc.Close(); err != nil {
t.Fatalf("Close() = %v", err)
}
tracker.mu.Lock()
flushAfter := tracker.flushCt
tracker.mu.Unlock()
if flushAfter <= flushBefore {
t.Error("expected Close to flush GPU accelerator, but Flush was not called")
}
}
func TestContextCloseWithoutAccelerator(t *testing.T) {
resetAccelerator()
// Verify Close works without panic when no accelerator is registered.
dc := NewContext(100, 100)
dc.DrawCircle(50, 50, 20)
_ = dc.Fill()
if err := dc.Close(); err != nil {
t.Fatalf("Close() = %v", err)
}
}
func TestContextWithSDFAcceleratorFillCircle(t *testing.T) {
resetAccelerator()
defer resetAccelerator()
tracker := &trackingAccelerator{}
if err := RegisterAccelerator(tracker); err != nil {
t.Fatalf("RegisterAccelerator: %v", err)
}
dc := NewContext(200, 200)
defer func() { _ = dc.Close() }()
dc.SetColor(Red.Color())
dc.DrawCircle(100, 100, 30)
if err := dc.Fill(); err != nil {
t.Fatalf("Fill() = %v", err)
}
tracker.mu.Lock()
fc := tracker.fillShapeCt
lastKind := tracker.lastShape.Kind
tracker.mu.Unlock()
if fc == 0 {
t.Error("expected FillShape to be called for circle, but it was not")
}
if lastKind != ShapeCircle {
t.Errorf("expected shape kind ShapeCircle, got %d", lastKind)
}
// Verify pixels were actually drawn.
px := dc.pixmap.GetPixel(100, 100)
if px.A < 0.5 {
t.Errorf("center pixel alpha = %f, want >= 0.5", px.A)
}
}
func TestContextWithSDFAcceleratorStrokeCircle(t *testing.T) {
resetAccelerator()
defer resetAccelerator()
tracker := &trackingAccelerator{}
if err := RegisterAccelerator(tracker); err != nil {
t.Fatalf("RegisterAccelerator: %v", err)
}
dc := NewContext(200, 200)
defer func() { _ = dc.Close() }()
dc.SetColor(Blue.Color())
dc.SetLineWidth(2.0)
dc.DrawCircle(100, 100, 30)
if err := dc.Stroke(); err != nil {
t.Fatalf("Stroke() = %v", err)
}
tracker.mu.Lock()
sc := tracker.strokeShapeCt
lastKind := tracker.lastShape.Kind
tracker.mu.Unlock()
if sc == 0 {
t.Error("expected StrokeShape to be called for circle, but it was not")
}
if lastKind != ShapeCircle {
t.Errorf("expected shape kind ShapeCircle, got %d", lastKind)
}
}
func TestContextFallbackToSoftware(t *testing.T) {
resetAccelerator()
defer resetAccelerator()
tracker := &trackingAccelerator{}
if err := RegisterAccelerator(tracker); err != nil {
t.Fatalf("RegisterAccelerator: %v", err)
}
dc := NewContext(200, 200)
defer func() { _ = dc.Close() }()
// Draw an arbitrary path that is NOT a recognized shape.
dc.SetColor(Green.Color())
dc.MoveTo(10, 10)
dc.LineTo(100, 10)
dc.QuadraticTo(100, 100, 10, 100)
dc.ClosePath()
if err := dc.Fill(); err != nil {
t.Fatalf("Fill() = %v", err)
}
// The accelerator does not support AccelFill, so it should fall back.
tracker.mu.Lock()
fc := tracker.fillShapeCt
fpc := tracker.fillPathCt
tracker.mu.Unlock()
if fc != 0 {
t.Errorf("expected FillShape not to be called for arbitrary path, got %d calls", fc)
}
// fillPathCt should be 0 because CanAccelerate(AccelFill) returns false.
if fpc != 0 {
t.Errorf("expected FillPath not to be called (unsupported op), got %d calls", fpc)
}
// Verify pixels were drawn by software renderer.
px := dc.pixmap.GetPixel(50, 50)
if px.A < 0.5 {
t.Errorf("center pixel alpha = %f, want >= 0.5 (software fallback)", px.A)
}
}
func TestContextNoAcceleratorSoftwarePath(t *testing.T) {
resetAccelerator()
dc := NewContext(100, 100)
defer func() { _ = dc.Close() }()
dc.SetColor(Red.Color())
dc.DrawCircle(50, 50, 20)
if err := dc.Fill(); err != nil {
t.Fatalf("Fill() = %v", err)
}
// Verify software rendering works when no accelerator is registered.
px := dc.pixmap.GetPixel(50, 50)
if px.A < 0.9 {
t.Errorf("center pixel alpha = %f, want >= 0.9", px.A)
}
if px.R < 0.9 {
t.Errorf("center pixel red = %f, want >= 0.9", px.R)
}
}
func TestContextFillPreserveWithAccelerator(t *testing.T) {
resetAccelerator()
defer resetAccelerator()
tracker := &trackingAccelerator{}
if err := RegisterAccelerator(tracker); err != nil {
t.Fatalf("RegisterAccelerator: %v", err)
}
dc := NewContext(200, 200)
defer func() { _ = dc.Close() }()
dc.SetColor(Red.Color())
dc.DrawCircle(100, 100, 30)
// FillPreserve should use accelerator but NOT clear the path.
if err := dc.FillPreserve(); err != nil {
t.Fatalf("FillPreserve() = %v", err)
}
tracker.mu.Lock()
fc := tracker.fillShapeCt
tracker.mu.Unlock()
if fc == 0 {
t.Error("expected FillShape to be called for FillPreserve")
}
// Path should still have elements.
if dc.path.isEmpty() {
t.Error("path should not be cleared after FillPreserve")
}
}
func TestContextStrokePreserveWithAccelerator(t *testing.T) {
resetAccelerator()
defer resetAccelerator()
tracker := &trackingAccelerator{}
if err := RegisterAccelerator(tracker); err != nil {
t.Fatalf("RegisterAccelerator: %v", err)
}
dc := NewContext(200, 200)
defer func() { _ = dc.Close() }()
dc.SetColor(Blue.Color())
dc.SetLineWidth(2.0)
dc.DrawCircle(100, 100, 30)
// StrokePreserve should use accelerator but NOT clear the path.
if err := dc.StrokePreserve(); err != nil {
t.Fatalf("StrokePreserve() = %v", err)
}
tracker.mu.Lock()
sc := tracker.strokeShapeCt
tracker.mu.Unlock()
if sc == 0 {
t.Error("expected StrokeShape to be called for StrokePreserve")
}
// Path should still have elements.
if dc.path.isEmpty() {
t.Error("path should not be cleared after StrokePreserve")
}
}