Skip to content

Commit ffeffa6

Browse files
author
fbchen
committed
add performance overlay
1 parent d1aeaf8 commit ffeffa6

File tree

1 file changed

+67
-9
lines changed

1 file changed

+67
-9
lines changed

lib/src/constraint_layout.dart

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@ class _ConstraintRenderBox extends RenderBox
10121012
final Map<ConstraintId, _ConstrainedNode> _tempConstrainedNodes = HashMap();
10131013
late List<_ConstrainedNode> _paintingOrderList;
10141014

1015+
static const int maxTimeUsage = 20;
1016+
Queue<int> layoutTimeUsage = Queue();
1017+
Queue<int> paintTimeUsage = Queue();
1018+
10151019
set childConstraints(List<Constraint> value) {
10161020
bool isSameList = true;
10171021
if (_childConstraints.length != value.length) {
@@ -1349,13 +1353,15 @@ class _ConstraintRenderBox extends RenderBox
13491353
_layoutByConstrainedNodeTrees(constrainedNodeTrees);
13501354

13511355
if (_releasePrintLayoutTime && kReleaseMode) {
1352-
print(
1353-
'ConstraintLayout@${_debugName ?? hashCode} layout time = ${DateTime.now().millisecondsSinceEpoch - startTime} ms(current is release mode).');
1356+
layoutTimeUsage.add(DateTime.now().millisecondsSinceEpoch - startTime);
1357+
if (layoutTimeUsage.length > maxTimeUsage) {
1358+
layoutTimeUsage.removeFirst();
1359+
}
13541360
}
13551361
assert(() {
1356-
if (_debugPrintLayoutTime) {
1357-
debugPrint(
1358-
'ConstraintLayout@${_debugName ?? hashCode} layout time = ${DateTime.now().millisecondsSinceEpoch - startTime} ms(current is debug mode, release mode may take less time).');
1362+
layoutTimeUsage.add(DateTime.now().millisecondsSinceEpoch - startTime);
1363+
if (layoutTimeUsage.length > maxTimeUsage) {
1364+
layoutTimeUsage.removeFirst();
13591365
}
13601366
return true;
13611367
}());
@@ -2000,17 +2006,69 @@ class _ConstraintRenderBox extends RenderBox
20002006
}());
20012007

20022008
if (_releasePrintLayoutTime && kReleaseMode) {
2003-
print(
2004-
'ConstraintLayout@${_debugName ?? hashCode} paint time = ${DateTime.now().millisecondsSinceEpoch - startTime} ms(current is release mode).');
2009+
paintTimeUsage.add(DateTime.now().millisecondsSinceEpoch - startTime);
2010+
if (paintTimeUsage.length > maxTimeUsage) {
2011+
paintTimeUsage.removeFirst();
2012+
}
2013+
_debugShowPerformance(context, offset);
20052014
}
20062015
assert(() {
20072016
if (_debugPrintLayoutTime) {
2008-
debugPrint(
2009-
'ConstraintLayout@${_debugName ?? hashCode} paint time = ${DateTime.now().millisecondsSinceEpoch - startTime} ms(current is debug mode, release mode may take less time).');
2017+
paintTimeUsage.add(DateTime.now().millisecondsSinceEpoch - startTime);
2018+
if (paintTimeUsage.length > maxTimeUsage) {
2019+
paintTimeUsage.removeFirst();
2020+
}
2021+
_debugShowPerformance(context, offset);
20102022
}
20112023
return true;
20122024
}());
20132025
}
2026+
2027+
void _debugShowPerformance(
2028+
PaintingContext context,
2029+
Offset offset,
2030+
) {
2031+
Iterator<int> layoutIterator = layoutTimeUsage.iterator;
2032+
Iterator<int> paintIterator = paintTimeUsage.iterator;
2033+
double heightOffset = 0;
2034+
while (layoutIterator.moveNext() && paintIterator.moveNext()) {
2035+
int layoutTime = layoutIterator.current;
2036+
int paintTime = paintIterator.current;
2037+
ui.ParagraphBuilder paragraphBuilder =
2038+
ui.ParagraphBuilder(ui.ParagraphStyle(
2039+
textAlign: TextAlign.center,
2040+
fontSize: 8,
2041+
));
2042+
if (layoutTime > 5 || paintTime > 5) {
2043+
paragraphBuilder.pushStyle(ui.TextStyle(
2044+
color: Colors.red,
2045+
));
2046+
} else {
2047+
paragraphBuilder.pushStyle(ui.TextStyle(
2048+
color: Colors.green,
2049+
));
2050+
}
2051+
paragraphBuilder.addText("layout $layoutTime ms, draw $paintTime ms");
2052+
ui.Paragraph paragraph = paragraphBuilder.build();
2053+
paragraph.layout(const ui.ParagraphConstraints(
2054+
width: 200,
2055+
));
2056+
context.canvas.drawParagraph(paragraph, Offset(0, heightOffset) + offset);
2057+
heightOffset += 10;
2058+
}
2059+
2060+
ui.ParagraphBuilder paragraphBuilder =
2061+
ui.ParagraphBuilder(ui.ParagraphStyle(
2062+
textAlign: TextAlign.center,
2063+
fontSize: 8,
2064+
));
2065+
paragraphBuilder.addText('The bottom one is the latest');
2066+
ui.Paragraph paragraph = paragraphBuilder.build();
2067+
paragraph.layout(const ui.ParagraphConstraints(
2068+
width: 200,
2069+
));
2070+
context.canvas.drawParagraph(paragraph, Offset(0, heightOffset) + offset);
2071+
}
20142072
}
20152073

20162074
class _ConstrainedNode {

0 commit comments

Comments
 (0)