Skip to content

Commit 7e38261

Browse files
authored
Record path memory usage in SkPictures (flutter#18827)
1 parent 5efd2e8 commit 7e38261

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

lib/ui/painting/canvas.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ void Canvas::clipPath(const CanvasPath* path, bool doAntiAlias) {
181181
if (!path)
182182
Dart_ThrowException(
183183
ToDart("Canvas.clipPath called with non-genuine Path."));
184+
external_allocation_size_ += path->path().approximateBytesUsed();
184185
canvas_->clipPath(path->path(), doAntiAlias);
185186
}
186187

@@ -280,6 +281,7 @@ void Canvas::drawPath(const CanvasPath* path,
280281
if (!path)
281282
Dart_ThrowException(
282283
ToDart("Canvas.drawPath called with non-genuine Path."));
284+
external_allocation_size_ += path->path().approximateBytesUsed();
283285
canvas_->drawPath(path->path(), *paint.paint());
284286
}
285287

@@ -425,6 +427,7 @@ void Canvas::drawShadow(const CanvasPath* path,
425427
ToDart("Canvas.drawShader called with non-genuine Path."));
426428
SkScalar dpr =
427429
UIDartState::Current()->window()->viewport_metrics().device_pixel_ratio;
430+
external_allocation_size_ += path->path().approximateBytesUsed();
428431
flutter::PhysicalShapeLayer::DrawShadow(canvas_, path->path(), color,
429432
elevation, transparentOccluder, dpr);
430433
}

lib/ui/painting/path.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,11 @@ void CanvasPath::clone(Dart_Handle path_handle) {
297297
path->path_ = path_;
298298
}
299299

300+
// This is doomed to be called too early, since Paths are mutable.
301+
// However, it can help for some of the clone/shift/transform type methods
302+
// where the resultant path will initially have a meaningful size.
303+
size_t CanvasPath::GetAllocationSize() const {
304+
return sizeof(CanvasPath) + path_.approximateBytesUsed();
305+
}
306+
300307
} // namespace flutter

lib/ui/painting/path.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class CanvasPath : public RefCountedDartWrappable<CanvasPath> {
110110

111111
const SkPath& path() const { return path_; }
112112

113+
size_t GetAllocationSize() const override;
114+
113115
static void RegisterNatives(tonic::DartLibraryNatives* natives);
114116

115117
private:

testing/dart/canvas_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,23 @@ void main() {
286286

287287
expect(picture2.approximateBytesUsed, greaterThan(minimumExpected));
288288
});
289+
290+
test('Path reflected in picture size for drawPath, clipPath, and drawShadow', () async {
291+
final PictureRecorder recorder = PictureRecorder();
292+
final Canvas canvas = Canvas(recorder);
293+
final Path path = Path();
294+
for (int i = 0; i < 10000; i++) {
295+
path.lineTo(5, 9);
296+
path.lineTo(i.toDouble(), i.toDouble());
297+
}
298+
path.close();
299+
canvas.drawPath(path, Paint());
300+
canvas.drawShadow(path, const Color(0xFF000000), 5.0, false);
301+
canvas.clipPath(path);
302+
final Picture picture = recorder.endRecording();
303+
304+
// Slightly fuzzy here to allow for platform specific differences
305+
// Measurement on macOS: 541078
306+
expect(picture.approximateBytesUsed, greaterThan(530000));
307+
});
289308
}

0 commit comments

Comments
 (0)