performance_overlay.dart 6.73 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'box.dart';
6
import 'layer.dart';
7
import 'object.dart';
8

9 10 11 12 13 14
/// The options that control whether the performance overlay displays certain
/// aspects of the compositor.
enum PerformanceOverlayOption {
  // these must be in the order needed for their index values to match the
  // constants in //engine/src/sky/compositor/performance_overlay_layer.h

15 16 17 18 19 20 21 22
  /// Display the frame time and FPS of the last frame rendered. This field is
  /// updated every frame.
  ///
  /// This is the time spent by the rasterizer as it tries
  /// to convert the layer tree obtained from the widgets into OpenGL commands
  /// and tries to flush them onto the screen. When the total time taken by this
  /// step exceeds the frame slice, a frame is lost.
  displayRasterizerStatistics,
23

24 25 26 27 28
  /// Display the rasterizer frame times as they change over a set period of
  /// time in the form of a graph. The y axis of the graph denotes the total
  /// time spent by the rasterizer as a fraction of the total frame slice. When
  /// the bar turns red, a frame is lost.
  visualizeRasterizerStatistics,
29

30 31 32 33 34 35 36
  /// Display the frame time and FPS at which the interface can construct a
  /// layer tree for the rasterizer (whose behavior is described above) to
  /// consume.
  ///
  /// This involves all layout, animations, etc. When the total time taken by
  /// this step exceeds the frame slice, a frame is lost.
  displayEngineStatistics,
37

38 39
  /// Display the engine frame times as they change over a set period of time
  /// in the form of a graph. The y axis of the graph denotes the total time
40
  /// spent by the engine as a fraction of the total frame slice. When the bar
41 42 43
  /// turns red, a frame is lost.
  visualizeEngineStatistics,
}
44

45 46
/// Displays performance statistics.
///
Sigurd Meldgaard's avatar
Sigurd Meldgaard committed
47 48
/// The overlay shows two time series. The first shows how much time was
/// required on this thread to produce each frame. The second shows how much
49 50 51 52 53 54 55
/// time was required on the raster thread (formerly known as 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, each
/// thread should ideally spend less than 16ms producing each frame.
/// This ideal condition is indicated by a green vertical line for each thread.
/// Otherwise, the performance overlay shows a red vertical line.
56 57 58
///
/// The simplest way to show the performance overlay is to set
/// [MaterialApp.showPerformanceOverlay] or [WidgetsApp.showPerformanceOverlay]
59
/// to true.
60
class RenderPerformanceOverlay extends RenderBox {
61 62
  /// Creates a performance overlay render object.
  ///
63 64
  /// The [optionsMask], [rasterizerThreshold], [checkerboardRasterCacheImages],
  /// and [checkerboardOffscreenLayers] arguments must not be null.
65
  RenderPerformanceOverlay({
66 67 68 69
    int optionsMask = 0,
    int rasterizerThreshold = 0,
    bool checkerboardRasterCacheImages = false,
    bool checkerboardOffscreenLayers = false,
70 71 72 73 74 75 76 77
  }) : assert(optionsMask != null),
       assert(rasterizerThreshold != null),
       assert(checkerboardRasterCacheImages != null),
       assert(checkerboardOffscreenLayers != null),
       _optionsMask = optionsMask,
       _rasterizerThreshold = rasterizerThreshold,
       _checkerboardRasterCacheImages = checkerboardRasterCacheImages,
       _checkerboardOffscreenLayers = checkerboardOffscreenLayers;
78

79
  /// The mask is created by shifting 1 by the index of the specific
80
  /// [PerformanceOverlayOption] to enable.
81
  int get optionsMask => _optionsMask;
82
  int _optionsMask;
83 84
  set optionsMask(int value) {
    assert(value != null);
85
    if (value == _optionsMask) {
86
      return;
87
    }
88
    _optionsMask = value;
89 90 91
    markNeedsPaint();
  }

92 93 94
  /// The rasterizer threshold is an integer specifying the number of frame
  /// intervals that the rasterizer must miss before it decides that the frame
  /// is suitable for capturing an SkPicture trace for further analysis.
95
  int get rasterizerThreshold => _rasterizerThreshold;
96
  int _rasterizerThreshold;
97
  set rasterizerThreshold(int value) {
98
    assert(value != null);
99
    if (value == _rasterizerThreshold) {
100
      return;
101
    }
102
    _rasterizerThreshold = value;
103 104 105
    markNeedsPaint();
  }

106 107 108
  /// Whether the raster cache should checkerboard cached entries.
  bool get checkerboardRasterCacheImages => _checkerboardRasterCacheImages;
  bool _checkerboardRasterCacheImages;
109
  set checkerboardRasterCacheImages(bool value) {
110
    assert(value != null);
111
    if (value == _checkerboardRasterCacheImages) {
112
      return;
113
    }
114
    _checkerboardRasterCacheImages = value;
115 116 117
    markNeedsPaint();
  }

118 119 120 121 122
  /// Whether the compositor should checkerboard layers rendered to offscreen bitmaps.
  bool get checkerboardOffscreenLayers => _checkerboardOffscreenLayers;
  bool _checkerboardOffscreenLayers;
  set checkerboardOffscreenLayers(bool value) {
    assert(value != null);
123
    if (value == _checkerboardOffscreenLayers) {
124
      return;
125
    }
126 127 128 129
    _checkerboardOffscreenLayers = value;
    markNeedsPaint();
  }

130
  @override
131
  bool get sizedByParent => true;
132 133

  @override
134
  bool get alwaysNeedsCompositing => true;
135

136
  @override
137
  double computeMinIntrinsicWidth(double height) {
138
    return 0.0;
139 140
  }

141
  @override
142
  double computeMaxIntrinsicWidth(double height) {
143
    return 0.0;
144 145
  }

146
  double get _intrinsicHeight {
Hixie's avatar
Hixie committed
147
    const double kDefaultGraphHeight = 80.0;
148
    double result = 0.0;
149
    if ((optionsMask | (1 << PerformanceOverlayOption.displayRasterizerStatistics.index) > 0) ||
150
        (optionsMask | (1 << PerformanceOverlayOption.visualizeRasterizerStatistics.index) > 0)) {
Hixie's avatar
Hixie committed
151
      result += kDefaultGraphHeight;
152
    }
153
    if ((optionsMask | (1 << PerformanceOverlayOption.displayEngineStatistics.index) > 0) ||
154
        (optionsMask | (1 << PerformanceOverlayOption.visualizeEngineStatistics.index) > 0)) {
Hixie's avatar
Hixie committed
155
      result += kDefaultGraphHeight;
156
    }
157
    return result;
158 159
  }

160
  @override
161
  double computeMinIntrinsicHeight(double width) {
162
    return _intrinsicHeight;
163 164
  }

165
  @override
166
  double computeMaxIntrinsicHeight(double width) {
167
    return _intrinsicHeight;
168 169
  }

170
  @override
171 172
  Size computeDryLayout(BoxConstraints constraints) {
    return constraints.constrain(Size(double.infinity, _intrinsicHeight));
173 174
  }

175
  @override
176
  void paint(PaintingContext context, Offset offset) {
177
    assert(needsCompositing);
178 179
    context.addLayer(PerformanceOverlayLayer(
      overlayRect: Rect.fromLTWH(offset.dx, offset.dy, size.width, size.height),
180
      optionsMask: optionsMask,
181 182
      rasterizerThreshold: rasterizerThreshold,
      checkerboardRasterCacheImages: checkerboardRasterCacheImages,
183
      checkerboardOffscreenLayers: checkerboardOffscreenLayers,
184
    ));
185 186
  }
}