forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompositing_test.dart
383 lines (354 loc) · 12.9 KB
/
compositing_test.dart
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.6
import 'dart:typed_data' show Float64List;
import 'dart:ui';
import 'package:test/test.dart';
void main() {
test('pushTransform validates the matrix', () {
final SceneBuilder builder = SceneBuilder();
final Float64List matrix4 = Float64List.fromList(<double>[
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]);
expect(builder.pushTransform(matrix4), isNotNull);
final Float64List matrix4WrongLength = Float64List.fromList(<double>[
1, 0, 0, 0,
0, 1, 0,
0, 0, 1, 0,
0, 0, 0,
]);
assert(() {
expect(
() => builder.pushTransform(matrix4WrongLength),
throwsA(const TypeMatcher<AssertionError>()),
);
return true;
}());
final Float64List matrix4NaN = Float64List.fromList(<double>[
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, double.nan,
]);
assert(() {
expect(
() => builder.pushTransform(matrix4NaN),
throwsA(const TypeMatcher<AssertionError>()),
);
return true;
}());
final Float64List matrix4Infinity = Float64List.fromList(<double>[
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, double.infinity,
]);
assert(() {
expect(
() => builder.pushTransform(matrix4Infinity),
throwsA(const TypeMatcher<AssertionError>()),
);
return true;
}());
});
test('SceneBuilder accepts typed layers', () {
final SceneBuilder builder1 = SceneBuilder();
final OpacityEngineLayer opacity1 = builder1.pushOpacity(100);
expect(opacity1, isNotNull);
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
final OpacityEngineLayer opacity2 = builder2.pushOpacity(200, oldLayer: opacity1);
expect(opacity2, isNotNull);
builder2.pop();
builder2.build();
});
// Attempts to use the same layer first as `oldLayer` then in `addRetained`.
void testPushThenIllegalRetain(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer layer = pushFunction(builder1, null);
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
pushFunction(builder2, layer);
builder2.pop();
assert(() {
try {
builder2.addRetained(layer);
fail('Expected addRetained to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('The layer is already being used'));
}
return true;
}());
builder2.build();
}
// Attempts to use the same layer first in `addRetained` then as `oldLayer`.
void testAddRetainedThenIllegalPush(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer layer = pushFunction(builder1, null);
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
builder2.addRetained(layer);
assert(() {
try {
pushFunction(builder2, layer);
fail('Expected push to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('The layer is already being used'));
}
return true;
}());
builder2.build();
}
// Attempts to retain the same layer twice in the same scene.
void testDoubleAddRetained(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer layer = pushFunction(builder1, null);
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
builder2.addRetained(layer);
assert(() {
try {
builder2.addRetained(layer);
fail('Expected second addRetained to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('The layer is already being used'));
}
return true;
}());
builder2.build();
}
// Attempts to use the same layer as `oldLayer` twice in the same scene.
void testPushOldLayerTwice(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer layer = pushFunction(builder1, null);
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
pushFunction(builder2, layer);
assert(() {
try {
pushFunction(builder2, layer);
fail('Expected push to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('was previously used as oldLayer'));
}
return true;
}());
builder2.build();
}
// Attempts to use a child of a retained layer as an `oldLayer`.
void testPushChildLayerOfRetainedLayer(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer layer = pushFunction(builder1, null);
final OpacityEngineLayer childLayer = builder1.pushOpacity(123);
builder1.pop();
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
builder2.addRetained(layer);
assert(() {
try {
builder2.pushOpacity(321, oldLayer: childLayer);
fail('Expected pushOpacity to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('The layer is already being used'));
}
return true;
}());
builder2.build();
}
// Attempts to retain a layer whose child is already used as `oldLayer` elsewhere in the scene.
void testRetainParentLayerOfPushedChild(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer layer = pushFunction(builder1, null);
final OpacityEngineLayer childLayer = builder1.pushOpacity(123);
builder1.pop();
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
builder2.pushOpacity(234, oldLayer: childLayer);
builder2.pop();
assert(() {
try {
builder2.addRetained(layer);
fail('Expected addRetained to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('The layer is already being used'));
}
return true;
}());
builder2.build();
}
// Attempts to retain a layer that has been used as `oldLayer` in a previous frame.
void testRetainOldLayer(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer layer = pushFunction(builder1, null);
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
pushFunction(builder2, layer);
builder2.pop();
assert(() {
try {
final SceneBuilder builder3 = SceneBuilder();
builder3.addRetained(layer);
fail('Expected addRetained to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('was previously used as oldLayer'));
}
return true;
}());
builder2.build();
}
// Attempts to pass layer as `oldLayer` that has been used as `oldLayer` in a previous frame.
void testPushOldLayer(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer layer = pushFunction(builder1, null);
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
pushFunction(builder2, layer);
builder2.pop();
assert(() {
try {
final SceneBuilder builder3 = SceneBuilder();
pushFunction(builder3, layer);
fail('Expected addRetained to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('was previously used as oldLayer'));
}
return true;
}());
builder2.build();
}
// Attempts to retain a parent of a layer used as `oldLayer` in a previous frame.
void testRetainsParentOfOldLayer(_TestNoSharingFunction pushFunction) {
final SceneBuilder builder1 = SceneBuilder();
final EngineLayer parentLayer = pushFunction(builder1, null);
final OpacityEngineLayer childLayer = builder1.pushOpacity(123);
builder1.pop();
builder1.pop();
builder1.build();
final SceneBuilder builder2 = SceneBuilder();
builder2.pushOpacity(321, oldLayer: childLayer);
builder2.pop();
assert(() {
try {
final SceneBuilder builder3 = SceneBuilder();
builder3.addRetained(parentLayer);
fail('Expected addRetained to throw AssertionError but it returned successully');
} on AssertionError catch (error) {
expect(error.toString(), contains('was previously used as oldLayer'));
}
return true;
}());
builder2.build();
}
void testNoSharing(_TestNoSharingFunction pushFunction) {
testPushThenIllegalRetain(pushFunction);
testAddRetainedThenIllegalPush(pushFunction);
testDoubleAddRetained(pushFunction);
testPushOldLayerTwice(pushFunction);
testPushChildLayerOfRetainedLayer(pushFunction);
testRetainParentLayerOfPushedChild(pushFunction);
testRetainOldLayer(pushFunction);
testPushOldLayer(pushFunction);
testRetainsParentOfOldLayer(pushFunction);
}
test('SceneBuilder does not share a layer between addRetained and push*', () {
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushOffset(0, 0, oldLayer: oldLayer as OffsetEngineLayer);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushTransform(Float64List(16), oldLayer: oldLayer as TransformEngineLayer);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushClipRect(Rect.zero, oldLayer: oldLayer as ClipRectEngineLayer);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushClipRRect(RRect.zero, oldLayer: oldLayer as ClipRRectEngineLayer);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushClipPath(Path(), oldLayer: oldLayer as ClipPathEngineLayer);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushOpacity(100, oldLayer: oldLayer as OpacityEngineLayer);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushBackdropFilter(ImageFilter.blur(), oldLayer: oldLayer as BackdropFilterEngineLayer);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushShaderMask(
Gradient.radial(
const Offset(0, 0),
10,
const <Color>[Color.fromARGB(0, 0, 0, 0), Color.fromARGB(0, 255, 255, 255)],
),
Rect.zero,
BlendMode.color,
oldLayer: oldLayer as ShaderMaskEngineLayer,
);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushPhysicalShape(path: Path(), color: const Color.fromARGB(0, 0, 0, 0), oldLayer: oldLayer as PhysicalShapeEngineLayer, elevation: 0.0);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushColorFilter(
const ColorFilter.mode(
Color.fromARGB(0, 0, 0, 0),
BlendMode.color,
),
oldLayer: oldLayer as ColorFilterEngineLayer,
);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushColorFilter(
const ColorFilter.matrix(<double>[
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
]),
oldLayer: oldLayer as ColorFilterEngineLayer,
);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushColorFilter(
const ColorFilter.linearToSrgbGamma(),
oldLayer: oldLayer as ColorFilterEngineLayer,
);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushColorFilter(
const ColorFilter.srgbToLinearGamma(),
oldLayer: oldLayer as ColorFilterEngineLayer,
);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushImageFilter(
ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
oldLayer: oldLayer as ImageFilterEngineLayer,
);
});
testNoSharing((SceneBuilder builder, EngineLayer oldLayer) {
return builder.pushImageFilter(
ImageFilter.matrix(Float64List.fromList(<double>[
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
])),
oldLayer: oldLayer as ImageFilterEngineLayer,
);
});
});
}
typedef _TestNoSharingFunction = EngineLayer Function(SceneBuilder builder, EngineLayer oldLayer);