performance_overlay.dart 5.05 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/rendering.dart';
6

Adam Barth's avatar
Adam Barth committed
7
import 'framework.dart';
8

9
/// Displays performance statistics.
10 11 12 13 14 15
///
/// The overlay show two time series. The first shows how much time was required
/// on this thread to produce each frame. The second shows how much time was
/// required on the GPU thread to produce each frame. Ideally, both these values
/// would be less than the total frame budget for the hardware on which the app
/// is running. For example, if the hardware has a screen that updates at 60 Hz,
16
/// each thread should ideally spend less than 16ms producing each frame. This
17
/// ideal condition is indicated by a green vertical line for each thread.
18
/// Otherwise, the performance overlay shows a red vertical line.
19 20 21 22
///
/// The simplest way to show the performance overlay is to set
/// [MaterialApp.showPerformanceOverlay] or [WidgetsApp.showPerformanceOverlay]
/// to `true`.
23
class PerformanceOverlay extends LeafRenderObjectWidget {
24 25
  // TODO(abarth): We should have a page on the web site with a screenshot and
  // an explanation of all the various readouts.
26

27
  /// Create a performance overlay that only displays specific statistics. The
28 29
  /// mask is created by shifting 1 by the index of the specific
  /// [StatisticOption] to enable.
30 31
  PerformanceOverlay({
    Key key,
32 33 34
    this.optionsMask: 0,
    this.rasterizerThreshold: 0,
    this.checkerboardRasterCacheImages: false
35
  }) : super(key: key);
36

37
  /// Create a performance overlay that displays all available statistics
38 39 40
  PerformanceOverlay.allEnabled({ Key key,
                                  this.rasterizerThreshold: 0,
                                  this.checkerboardRasterCacheImages: false })
Hixie's avatar
Hixie committed
41
    : optionsMask = (
42 43 44 45
        1 << PerformanceOverlayOption.displayRasterizerStatistics.index |
        1 << PerformanceOverlayOption.visualizeRasterizerStatistics.index |
        1 << PerformanceOverlayOption.displayEngineStatistics.index |
        1 << PerformanceOverlayOption.visualizeEngineStatistics.index
Hixie's avatar
Hixie committed
46 47
      ),
      super(key: key);
48

49 50
  /// The mask is created by shifting 1 by the index of the specific
  /// [PerformanceOverlayOption] to enable.
51
  final int optionsMask;
52 53 54

  /// The rasterizer threshold is an integer specifying the number of frame
  /// intervals that the rasterizer must miss before it decides that the frame
Devon Carew's avatar
Devon Carew committed
55
  /// is suitable for capturing an SkPicture trace for further analysis.
56 57 58 59 60 61 62 63 64
  ///
  /// For example, if you want a trace of all pictures that could not be
  /// renderered by the rasterizer within the frame boundary (and hence caused
  /// jank), specify 1. Specifying 2 will trace all pictures that took more
  /// more than 2 frame intervals to render. Adjust this value to only capture
  /// the particularly expensive pictures while skipping the others. Specifying
  /// 0 disables all capture.
  ///
  /// Captured traces are placed on your device in the application documents
65
  /// directory in this form "trace_<collection_time>.skp". These can
66 67 68 69 70 71 72 73 74 75
  /// be viewed in the Skia debugger.
  ///
  /// Notes:
  /// The rasterizer only takes into account the time it took to render
  /// the already constructed picture. This include the Skia calls (which is
  /// also why an SkPicture trace is generated) but not any of the time spent in
  /// dart to construct that picture. To profile that part of your code, use
  /// the instrumentation available in observatory.
  ///
  /// To decide what threshold interval to use, count the number of horizontal
76
  /// lines displayed in the performance overlay for the rasterizer (not the
77 78
  /// engine). That should give an idea of how often frames are skipped (and by
  /// how many frame intervals).
79
  final int rasterizerThreshold;
80

81 82 83 84 85 86 87 88 89 90 91 92 93
  /// Whether the raster cache should checkerboard cached entries.
  ///
  /// The compositor can sometimes decide to cache certain portions of the
  /// widget hierarchy. Such portions typically don't change often from frame to
  /// frame and are expensive to render. This can speed up overall rendering. However,
  /// there is certain upfront cost to constructing these cache entries. And, if
  /// the cache entries are not used very often, this cost may not be worth the
  /// speedup in rendering of subsequent frames. If the developer wants to be certain
  /// that populating the raster cache is not causing stutters, this option can be
  /// set. Depending on the observations made, hints can be provided to the compositor
  /// that aid it in making better decisions about caching.
  final bool checkerboardRasterCacheImages;

94
  @override
95
  RenderPerformanceOverlay createRenderObject(BuildContext context) => new RenderPerformanceOverlay(
96
    optionsMask: optionsMask,
97 98
    rasterizerThreshold: rasterizerThreshold,
    checkerboardRasterCacheImages: checkerboardRasterCacheImages
99
  );
100

101
  @override
102
  void updateRenderObject(BuildContext context, RenderPerformanceOverlay renderObject) {
103 104 105
    renderObject
      ..optionsMask = optionsMask
      ..rasterizerThreshold = rasterizerThreshold;
106 107
  }
}