| 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/lib/ui/volatile_path_tracker.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | namespace flutter { |
| 10 | |
| 11 | VolatilePathTracker::VolatilePathTracker( |
| 12 | fml::RefPtr<fml::TaskRunner> ui_task_runner, |
| 13 | bool enabled) |
| 14 | : ui_task_runner_(std::move(ui_task_runner)), enabled_(enabled) {} |
| 15 | |
| 16 | void VolatilePathTracker::Track(const std::shared_ptr<TrackedPath>& path) { |
| 17 | FML_DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 18 | FML_DCHECK(path); |
| 19 | FML_DCHECK(path->path.isVolatile()); |
| 20 | if (!enabled_) { |
| 21 | path->path.setIsVolatile(false); |
| 22 | return; |
| 23 | } |
| 24 | paths_.push_back(x: path); |
| 25 | } |
| 26 | |
| 27 | void VolatilePathTracker::OnFrame() { |
| 28 | FML_DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 29 | if (!enabled_) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | paths_.erase(first: std::remove_if(first: paths_.begin(), last: paths_.end(), |
| 34 | pred: [](const std::weak_ptr<TrackedPath>& weak_path) { |
| 35 | auto path = weak_path.lock(); |
| 36 | if (!path) { |
| 37 | return true; |
| 38 | } |
| 39 | path->frame_count++; |
| 40 | if (path->frame_count >= kFramesOfVolatility) { |
| 41 | path->path.setIsVolatile(false); |
| 42 | path->tracking_volatility = false; |
| 43 | return true; |
| 44 | } |
| 45 | return false; |
| 46 | }), |
| 47 | last: paths_.end()); |
| 48 | } |
| 49 | |
| 50 | } // namespace flutter |
| 51 |
