diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index d988fc923e..c6fc50a84e 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -397,8 +397,16 @@ class PaintingContext extends ClipContext { /// If this hint is not set, the compositor will apply its own heuristics to /// decide whether the current layer is complex enough to benefit from /// caching. + /// + /// Calling this ensures a [Canvas] is available. Only draw calls on the + /// current canvas will be hinted; the hint is not propagated to new canvases + /// created after a new layer is added to the painting context (e.g. with + /// [addLayer] or [pushLayer]). void setIsComplexHint() { - _currentLayer?.isComplexHint = true; + if (_currentLayer == null) { + _startRecording(); + } + _currentLayer!.isComplexHint = true; } /// Hints that the painting in the current layer is likely to change next frame. @@ -407,8 +415,16 @@ class PaintingContext extends ClipContext { /// cache will not be used in the future. If this hint is not set, the /// compositor will apply its own heuristics to decide whether the current /// layer is likely to be reused in the future. + /// + /// Calling this ensures a [Canvas] is available. Only draw calls on the + /// current canvas will be hinted; the hint is not propagated to new canvases + /// created after a new layer is added to the painting context (e.g. with + /// [addLayer] or [pushLayer]). void setWillChangeHint() { - _currentLayer?.willChangeHint = true; + if (_currentLayer == null) { + _startRecording(); + } + _currentLayer!.willChangeHint = true; } /// Adds a composited leaf layer to the recording. diff --git a/packages/flutter/test/rendering/painting_context_test.dart b/packages/flutter/test/rendering/painting_context_test.dart new file mode 100644 index 0000000000..0b97e7db34 --- /dev/null +++ b/packages/flutter/test/rendering/painting_context_test.dart @@ -0,0 +1,32 @@ +// Copyright 2014 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. + +import 'package:flutter/rendering.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'rendering_tester.dart'; + +void main() { + TestRenderingFlutterBinding.ensureInitialized(); + + test('PaintingContext.setIsComplexHint', () { + final ContainerLayer layer = ContainerLayer(); + final PaintingContext context = PaintingContext(layer, Rect.zero); + expect(layer.hasChildren, isFalse); + context.setIsComplexHint(); + expect(layer.hasChildren, isTrue); + expect(layer.firstChild, isA()); + expect((layer.firstChild! as PictureLayer).isComplexHint, isTrue); + }); + + test('PaintingContext.setWillChangeHint', () { + final ContainerLayer layer = ContainerLayer(); + final PaintingContext context = PaintingContext(layer, Rect.zero); + expect(layer.hasChildren, isFalse); + context.setWillChangeHint(); + expect(layer.hasChildren, isTrue); + expect(layer.firstChild, isA()); + expect((layer.firstChild! as PictureLayer).willChangeHint, isTrue); + }); +}