|
| 1 | +// Copyright 2014 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 'package:flutter/rendering.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | + |
| 8 | +import 'rendering_tester.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + TestRenderingFlutterBinding.ensureInitialized(); |
| 12 | + |
| 13 | + test('paint RenderObjects from top to bottom to avoid duplicated layer tree walks', () { |
| 14 | + // Historic Background: The order in which dirty nodes are processed for paint |
| 15 | + // in [PipelineOwner.flushPaint] was changed from shallowest first to |
| 16 | + // deepest first in August 2015, see https://github.com/flutter/flutter/commit/654fc7346eb79780aadeb9c2883ea9938d5f0bd3#diff-d06e00032e4d722205a2189ffbab26c1d8f5e13652efebc849583d0a1359fec9R612. |
| 17 | + // The reasons for this change are lost in history. In February 2022 it |
| 18 | + // was determined that deepest first actually caused additional unnecessary |
| 19 | + // walks of the layer tree. To avoid those, the processing order was changed |
| 20 | + // back to deepest first (which is also the order used by all other flush-methods on |
| 21 | + // PipelineOwner). The test below encodes that the framework is not doing |
| 22 | + // these unnecessary layer tree walks during paint that would occur during |
| 23 | + // deepest first processing. |
| 24 | + |
| 25 | + late RenderPositionedBox outer, inner; |
| 26 | + late TestRenderBox testBox; |
| 27 | + |
| 28 | + outer = RenderPositionedBox( |
| 29 | + child: RenderRepaintBoundary( |
| 30 | + child: inner = RenderPositionedBox( |
| 31 | + child: testBox = TestRenderBox(), |
| 32 | + ), |
| 33 | + ), |
| 34 | + ); |
| 35 | + |
| 36 | + // Paint the tree for the first time; Our TestLayer is attached exactly once. |
| 37 | + expect((testBox.debugLayer! as TestLayer).attachCount, 0); |
| 38 | + expect((testBox.debugLayer! as TestLayer).detachCount, 0); |
| 39 | + layout(outer, phase: EnginePhase.paint); |
| 40 | + expect((testBox.debugLayer! as TestLayer).attachCount, 1); |
| 41 | + expect((testBox.debugLayer! as TestLayer).detachCount, 0); |
| 42 | + |
| 43 | + // Repaint RenderObjects outside and inside the RepaintBoundary. |
| 44 | + outer.markNeedsPaint(); |
| 45 | + inner.markNeedsPaint(); |
| 46 | + expect((testBox.debugLayer! as TestLayer).attachCount, 1); |
| 47 | + expect((testBox.debugLayer! as TestLayer).detachCount, 0); |
| 48 | + pumpFrame(phase: EnginePhase.paint); |
| 49 | + |
| 50 | + // The TestLayer should be detached and reattached exactly once during the |
| 51 | + // paint process. More attach/detach would indicate unnecessary |
| 52 | + // additional walks of the layer tree. |
| 53 | + expect((testBox.debugLayer! as TestLayer).attachCount, 2); |
| 54 | + expect((testBox.debugLayer! as TestLayer).detachCount, 1); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +class TestRenderBox extends RenderProxyBoxWithHitTestBehavior { |
| 59 | + TestRenderBox() { |
| 60 | + layer = TestLayer(); |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + void paint(PaintingContext context, Offset offset) { |
| 65 | + context.addLayer(layer!); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class TestLayer extends OffsetLayer { |
| 70 | + int attachCount = 0; |
| 71 | + int detachCount = 0; |
| 72 | + |
| 73 | + @override |
| 74 | + void attach(Object owner) { |
| 75 | + super.attach(owner); |
| 76 | + attachCount++; |
| 77 | + } |
| 78 | + |
| 79 | + @override |
| 80 | + void detach() { |
| 81 | + super.detach(); |
| 82 | + detachCount++; |
| 83 | + } |
| 84 | +} |
0 commit comments