Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6d0ff37

Browse files
authored
Documentation and other cleanup in dart:ui, plus a small performance improvement. (#38047)
* Remove nonsensical (and I think unnecessary?) comment. * Document willChangeHint, isComplexHint * Fix grammer in Rect docs * Remove runtimeType.toString twice * Add detail to ImageShader constructor docs. * Document Vertices and drawVertices! * Update checks in Vertices constructors (and add tests). * Fix some typos. * Minor other doc improvements. * Fold in @jonahwilliams' performance improvement from #38041
1 parent f7df812 commit 6d0ff37

File tree

10 files changed

+308
-89
lines changed

10 files changed

+308
-89
lines changed

flow/layers/display_list_raster_cache_item.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ void DisplayListRasterCacheItem::PrerollFinalize(PrerollContext* context,
104104
}
105105
auto* raster_cache = context->raster_cache;
106106
SkRect bounds = display_list_->bounds().makeOffset(offset_.x(), offset_.y());
107-
// We must to create an entry whenever if the react is intersect.
108-
// if the rect is intersect we will get the entry access_count to confirm if
109-
// it great than the threshold. Otherwise we only increase the entry
110-
// access_count.
111107
bool visible = !context->state_stack.content_culled(bounds);
112108
int accesses = raster_cache->MarkSeen(key_id_, matrix, visible);
113109
if (!visible || accesses <= raster_cache->access_threshold()) {

lib/ui/compositing.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,25 @@ class SceneBuilder extends NativeFieldWrapperClass1 {
748748

749749
/// Adds a [Picture] to the scene.
750750
///
751-
/// The picture is rasterized at the given offset.
751+
/// The picture is rasterized at the given `offset`.
752+
///
753+
/// The rendering _may_ be cached to reduce the cost of painting the picture
754+
/// if it is reused in subsequent frames. Whether a picture is cached or not
755+
/// depends on the backend implementation. When caching is considered, the
756+
/// choice to cache or not cache is a heuristic based on how often the picture
757+
/// is being painted and the cost of painting the picture. To disable this
758+
/// caching, set `willChangeHint` to true. To force the caching to happen (in
759+
/// backends that do caching), set `isComplexHint` to true. When both are set,
760+
/// `willChangeHint` prevails.
761+
///
762+
/// In general, setting these hints is not very useful. Backends that cache
763+
/// pictures only do so for pictures that have been rendered three times
764+
/// already; setting `willChangeHint` to true to avoid caching an animating
765+
/// picture that changes every frame is therefore redundant, the picture
766+
/// wouldn't have been cached anyway. Similarly, backends that cache pictures
767+
/// are relatively aggressive about doing so, such that any image complicated
768+
/// enough to warrant caching is probably already being cached even without
769+
/// `isComplexHint` being set to true.
752770
void addPicture(
753771
Offset offset,
754772
Picture picture, {

lib/ui/geometry.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ class Size extends OffsetBase {
625625
/// An immutable, 2D, axis-aligned, floating-point rectangle whose coordinates
626626
/// are relative to a given origin.
627627
///
628-
/// A Rect can be created with one its constructors or from an [Offset] and a
628+
/// A Rect can be created with one of its constructors or from an [Offset] and a
629629
/// [Size] using the `&` operator:
630630
///
631631
/// ```dart

lib/ui/painting.dart

Lines changed: 205 additions & 79 deletions
Large diffs are not rendered by default.

lib/web_ui/lib/canvas.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ abstract class Vertices {
2525
factory Vertices(
2626
VertexMode mode,
2727
List<Offset> positions, {
28-
List<Offset>? textureCoordinates,
2928
List<Color>? colors,
29+
List<Offset>? textureCoordinates,
3030
List<int>? indices,
3131
}) {
3232
return engine.renderer.createVertices(mode,
@@ -38,8 +38,8 @@ abstract class Vertices {
3838
factory Vertices.raw(
3939
VertexMode mode,
4040
Float32List positions, {
41-
Float32List? textureCoordinates,
4241
Int32List? colors,
42+
Float32List? textureCoordinates,
4343
Uint16List? indices,
4444
}) {
4545
return engine.renderer.createVerticesRaw(mode,

lib/web_ui/lib/src/engine/html/render_vertices.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class SurfaceVertices implements ui.Vertices {
7070
if (assertionsEnabled) {
7171
return _disposed;
7272
}
73-
throw StateError('Vertices.debugDisposed is only avialalbe when asserts are enabled.');
73+
throw StateError('Vertices.debugDisposed is only available when asserts are enabled.');
7474
}
7575
}
7676

testing/dart/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tests = [
3232
"lerp_test.dart",
3333
"locale_test.dart",
3434
"mask_filter_test.dart",
35+
"painting_test.dart",
3536
"paragraph_builder_test.dart",
3637
"paragraph_test.dart",
3738
"path_test.dart",

testing/dart/compositing_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:typed_data' show ByteData, Float64List;
5+
import 'dart:typed_data';
66
import 'dart:ui';
77

88
import 'package:litetest/litetest.dart';

testing/dart/painting_test.dart

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:typed_data';
6+
import 'dart:ui';
7+
8+
import 'package:litetest/litetest.dart';
9+
10+
void main() {
11+
test('Vertices checks', () {
12+
try {
13+
Vertices(
14+
VertexMode.triangles,
15+
const <Offset>[Offset.zero, Offset.zero, Offset.zero],
16+
indices: Uint16List.fromList(const <int>[0, 2, 5]),
17+
);
18+
throw 'Vertices did not throw the expected error.';
19+
} on ArgumentError catch (e) {
20+
expect('$e', 'Invalid argument(s): "indices" values must be valid indices in the positions list (i.e. numbers in the range 0..2), but indices[2] is 5, which is too big.');
21+
}
22+
Vertices( // This one does not throw.
23+
VertexMode.triangles,
24+
const <Offset>[Offset.zero],
25+
).dispose();
26+
Vertices( // This one should not throw.
27+
VertexMode.triangles,
28+
const <Offset>[Offset.zero, Offset.zero, Offset.zero],
29+
indices: Uint16List.fromList(const <int>[0, 2, 1, 2, 0, 1, 2, 0]), // Uint16List implements List<int> so this is ok.
30+
).dispose();
31+
});
32+
33+
test('Vertices.raw checks', () {
34+
try {
35+
Vertices.raw(
36+
VertexMode.triangles,
37+
Float32List.fromList(const <double>[0.0]),
38+
);
39+
throw 'Vertices.raw did not throw the expected error.';
40+
} on ArgumentError catch (e) {
41+
expect('$e', 'Invalid argument(s): "positions" must have an even number of entries (each coordinate is an x,y pair).');
42+
}
43+
try {
44+
Vertices.raw(
45+
VertexMode.triangles,
46+
Float32List.fromList(const <double>[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
47+
indices: Uint16List.fromList(const <int>[0, 2, 5]),
48+
);
49+
throw 'Vertices.raw did not throw the expected error.';
50+
} on ArgumentError catch (e) {
51+
expect('$e', 'Invalid argument(s): "indices" values must be valid indices in the positions list (i.e. numbers in the range 0..2), but indices[2] is 5, which is too big.');
52+
}
53+
Vertices.raw( // This one does not throw.
54+
VertexMode.triangles,
55+
Float32List.fromList(const <double>[0.0, 0.0]),
56+
).dispose();
57+
Vertices.raw( // This one should not throw.
58+
VertexMode.triangles,
59+
Float32List.fromList(const <double>[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
60+
indices: Uint16List.fromList(const <int>[0, 2, 1, 2, 0, 1, 2, 0]),
61+
).dispose();
62+
});
63+
}

testing/dart/path_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,19 @@ void main() {
225225
expect(newFirstMetric.getTangentForOffset(4.0)!.vector, const Offset(0.0, 1.0));
226226
expect(newFirstMetric.extractPath(4.0, 10.0).computeMetrics().first.length, 6.0);
227227
});
228+
229+
test('PathMetrics on a mutated path', () {
230+
final Path path = Path()
231+
..lineTo(0, 30)
232+
..lineTo(40, 30)
233+
..moveTo(100, 0)
234+
..lineTo(100, 30)
235+
..lineTo(140, 30)
236+
..close();
237+
final PathMetrics metrics = path.computeMetrics();
238+
expect(metrics.toString(),
239+
'(PathMetric(length: 70.0, isClosed: false, contourIndex: 0), '
240+
'PathMetric(length: 120.0, isClosed: true, contourIndex: 1))',
241+
);
242+
});
228243
}

0 commit comments

Comments
 (0)