Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
performance_overlay_layer.cc
Go to the documentation of this file.
1// Copyright 2013 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#include "flutter/flow/layers/performance_overlay_layer.h"
6
7#include <iomanip>
8#include <iostream>
9#include <memory>
10#include <string>
11
12#include "flow/stopwatch.h"
13#include "flow/stopwatch_dl.h"
14#include "flow/stopwatch_sk.h"
19#include "txt/platform.h"
20#ifdef IMPELLER_SUPPORTS_RENDERING
22#endif // IMPELLER_SUPPORTS_RENDERING
23
24namespace flutter {
25namespace {
26
27void VisualizeStopWatch(DlCanvas* canvas,
28 const bool impeller_enabled,
29 const Stopwatch& stopwatch,
30 SkScalar x,
31 SkScalar y,
34 bool show_graph,
35 bool show_labels,
36 const std::string& label_prefix,
37 const std::string& font_path) {
38 const int label_x = 8; // distance from x
39 const int label_y = -10; // distance from y+height
40
41 if (show_graph) {
42 SkRect visualization_rect = SkRect::MakeXYWH(x, y, width, height);
43 std::unique_ptr<StopwatchVisualizer> visualizer;
44
45 if (impeller_enabled) {
46 visualizer = std::make_unique<DlStopwatchVisualizer>(stopwatch);
47 } else {
48 visualizer = std::make_unique<SkStopwatchVisualizer>(stopwatch);
49 }
50
51 visualizer->Visualize(canvas, visualization_rect);
52 }
53
54 if (show_labels) {
56 stopwatch, label_prefix, font_path);
57 // Historically SK_ColorGRAY (== 0xFF888888) was used here
58 DlPaint paint(DlColor(0xFF888888));
59#ifdef IMPELLER_SUPPORTS_RENDERING
60 if (impeller_enabled) {
61 canvas->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(text),
62 x + label_x, y + height + label_y, paint);
63 return;
64 }
65#endif // IMPELLER_SUPPORTS_RENDERING
66 canvas->DrawTextBlob(text, x + label_x, y + height + label_y, paint);
67 }
68}
69
70} // namespace
71
73 const Stopwatch& stopwatch,
74 const std::string& label_prefix,
75 const std::string& font_path) {
76 SkFont font;
78 if (font_path == "") {
79 if (sk_sp<SkTypeface> face = font_mgr->matchFamilyStyle(nullptr, {})) {
80 font = SkFont(face, 15);
81 } else {
82 // In Skia's Android fontmgr, matchFamilyStyle can return null instead
83 // of falling back to a default typeface. If that's the case, we can use
84 // legacyMakeTypeface, which *does* use that default typeface.
85 font = SkFont(font_mgr->legacyMakeTypeface(nullptr, {}), 15);
86 }
87 } else {
88 font = SkFont(font_mgr->makeFromFile(font_path.c_str()), 15);
89 }
90 // Make sure there's not an empty typeface returned, or we won't see any text.
91 FML_DCHECK(font.getTypeface()->countGlyphs() > 0);
92
93 double max_ms_per_frame = stopwatch.MaxDelta().ToMillisecondsF();
94 double average_ms_per_frame = stopwatch.AverageDelta().ToMillisecondsF();
95 std::stringstream stream;
96 stream.setf(std::ios::fixed | std::ios::showpoint);
97 stream << std::setprecision(1);
98 stream << label_prefix << " " << "max " << max_ms_per_frame << " ms/frame, "
99 << "avg " << average_ms_per_frame << " ms/frame";
100 auto text = stream.str();
101 return SkTextBlob::MakeFromText(text.c_str(), text.size(), font,
103}
104
106 const char* font_path)
107 : options_(options) {
108 if (font_path != nullptr) {
109 font_path_ = font_path;
110 }
111}
112
114 const Layer* old_layer) {
115 DiffContext::AutoSubtreeRestore subtree(context);
116 if (!context->IsSubtreeDirty()) {
117 FML_DCHECK(old_layer);
118 auto prev = old_layer->as_performance_overlay_layer();
119 context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(prev));
120 }
121 context->AddLayerBounds(paint_bounds());
122 context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
123}
124
126 const int padding = 8;
127
128 if (!options_) {
129 return;
130 }
131
132 SkScalar x = paint_bounds().x() + padding;
133 SkScalar y = paint_bounds().y() + padding;
134 SkScalar width = paint_bounds().width() - (padding * 2);
136 auto mutator = context.state_stack.save();
137
138 VisualizeStopWatch(
139 context.canvas, context.impeller_enabled, context.raster_time, x, y,
140 width, height - padding, options_ & kVisualizeRasterizerStatistics,
141 options_ & kDisplayRasterizerStatistics, "Raster", font_path_);
142
143 VisualizeStopWatch(context.canvas, context.impeller_enabled, context.ui_time,
144 x, y + height, width, height - padding,
146 options_ & kDisplayEngineStatistics, "UI", font_path_);
147}
148
149} // namespace flutter
const char * options
static float prev(float f)
@ kUTF8
uses bytes to represent UTF-8 or ASCII
static sk_sp< SkTextBlob > MakeFromText(const void *text, size_t byteLength, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
void AddLayerBounds(const SkRect &rect)
void SetLayerPaintRegion(const Layer *layer, const PaintRegion &region)
void MarkSubtreeDirty(const PaintRegion &previous_paint_region=PaintRegion())
PaintRegion CurrentSubtreeRegion() const
PaintRegion GetOldLayerPaintRegion(const Layer *layer) const
bool IsSubtreeDirty() const
const SkRect & paint_bounds() const
Definition layer.h:209
virtual const PerformanceOverlayLayer * as_performance_overlay_layer() const
Definition layer.h:260
void Paint(PaintContext &context) const override
void Diff(DiffContext *context, const Layer *old_layer) override
PerformanceOverlayLayer(uint64_t options, const char *font_path=nullptr)
static sk_sp< SkTextBlob > MakeStatisticsText(const Stopwatch &stopwatch, const std::string &label_prefix, const std::string &font_path)
fml::TimeDelta MaxDelta() const
Definition stopwatch.cc:70
fml::TimeDelta AverageDelta() const
Definition stopwatch.cc:80
constexpr double ToMillisecondsF() const
Definition time_delta.h:68
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define FML_DCHECK(condition)
Definition logging.h:103
std::u16string text
double y
double x
const int kVisualizeEngineStatistics
const int kDisplayEngineStatistics
const int kVisualizeRasterizerStatistics
const int kDisplayRasterizerStatistics
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
sk_sp< SkFontMgr > GetDefaultFontManager(uint32_t font_initialization_data)
Definition platform.cc:17
int32_t height
int32_t width
constexpr float x() const
Definition SkRect.h:720
constexpr float y() const
Definition SkRect.h:727
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
const Stopwatch & raster_time
Definition layer.h:111
const Stopwatch & ui_time
Definition layer.h:112
DlCanvas * canvas
Definition layer.h:101
LayerStateStack & state_stack
Definition layer.h:100