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#ifndef FLUTTER_LIB_VOLATILE_PATH_TRACKER_H_
6#define FLUTTER_LIB_VOLATILE_PATH_TRACKER_H_
7
8#include <deque>
9#include <memory>
10#include <mutex>
11#include <vector>
12
13#include "flutter/fml/macros.h"
14#include "flutter/fml/task_runner.h"
15#include "flutter/fml/trace_event.h"
16#include "third_party/skia/include/core/SkPath.h"
17
18namespace flutter {
19
20namespace testing {
21class ShellTest;
22} // namespace testing
23
24/// A cache for paths drawn from dart:ui.
25///
26/// Whenever a flutter::CanvasPath is created, it must Insert an entry into
27/// this cache. Whenever a frame is drawn, the shell must call OnFrame. The
28/// cache will flip the volatility bit on the SkPath and remove it from the
29/// cache. If the Dart object is released, Erase must be called to avoid
30/// tracking a path that is no longer referenced in Dart code.
31///
32/// Enabling this cache may cause difficult to predict minor pixel differences
33/// when paths are rendered. If deterministic rendering is needed, e.g. for a
34/// screen diffing test, this class will not cache any paths and will
35/// automatically set the volatility of the path to false.
36class VolatilePathTracker {
37 public:
38 /// The fields of this struct must only accessed on the UI task runner.
39 struct TrackedPath {
40 bool tracking_volatility = false;
41 int frame_count = 0;
42 SkPath path;
43 };
44
45 VolatilePathTracker(fml::RefPtr<fml::TaskRunner> ui_task_runner,
46 bool enabled);
47
48 static constexpr int kFramesOfVolatility = 2;
49
50 // Starts tracking a path.
51 // Must be called from the UI task runner.
52 //
53 // Callers should only insert paths that are currently volatile.
54 void Track(const std::shared_ptr<TrackedPath>& path);
55
56 // Called by the shell at the end of a frame after notifying Dart about idle
57 // time.
58 //
59 // This method will flip the volatility bit to false for any paths that have
60 // survived the |kFramesOfVolatility|.
61 //
62 // Must be called from the UI task runner.
63 void OnFrame();
64
65 bool enabled() const { return enabled_; }
66
67 private:
68 fml::RefPtr<fml::TaskRunner> ui_task_runner_;
69 std::vector<std::weak_ptr<TrackedPath>> paths_;
70 bool enabled_ = true;
71
72 friend class testing::ShellTest;
73
74 FML_DISALLOW_COPY_AND_ASSIGN(VolatilePathTracker);
75};
76
77} // namespace flutter
78
79#endif // FLUTTER_LIB_VOLATILE_PATH_TRACKER_H_
80

source code of flutter_engine/flutter/lib/ui/volatile_path_tracker.h