1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122

cc / trees / frame_rate_estimator.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/trees/frame_rate_estimator.h"

#include "base/feature_list.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "cc/base/features.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"

namespace cc {
namespace {

constexpr auto kInputPriorityDelay = base::Milliseconds(250);

bool IsThrottleFrameRateOnManyDidNotProduceFrameEnabled() {
  static const bool feature_allowed = base::FeatureList::IsEnabled(
      features::kThrottleFrameRateOnManyDidNotProduceFrame);
  return feature_allowed;
}

}  // namespace

FrameRateEstimator::FrameRateEstimator(base::SequencedTaskRunner* task_runner)
    : notifier_(
          task_runner,
          base::BindRepeating(&FrameRateEstimator::OnExitInputPriorityMode,
                              base::Unretained(this)),
          kInputPriorityDelay) {}

FrameRateEstimator::~FrameRateEstimator() = default;

void FrameRateEstimator::SetVideoConferenceMode(bool enabled) {
  if (enabled == in_video_conference_mode_) {
    return;
  }

  in_video_conference_mode_ = enabled;
  last_draw_time_in_video_conference_mode_ = base::TimeTicks();
  num_of_consecutive_frames_with_min_delta_ = 0u;
}

void FrameRateEstimator::WillDraw(base::TimeTicks now) {
  TRACE_EVENT1(
      "cc,benchmark", "FrameRateEstimator::WillDraw", "Info",
      base::StringPrintf("num_did_not_produce_frame_since_last_draw: %" PRIu64
                         "\nin_video_conference_mode: %d\ninput_priority_mode: "
                         "%d\nkNumDidNotProduceFrameBeforeThrottle: %d",
                         num_did_not_produce_frame_since_last_draw_,
                         in_video_conference_mode_, input_priority_mode_,
                         features::kNumDidNotProduceFrameBeforeThrottle.Get()));

  num_did_not_produce_frame_since_last_draw_ = 0u;
  if (!in_video_conference_mode_ || input_priority_mode_) {
    return;
  }

  if (last_draw_time_in_video_conference_mode_ == base::TimeTicks()) {
    last_draw_time_in_video_conference_mode_ = now;
    return;
  }

  auto draw_delta = now - last_draw_time_in_video_conference_mode_;
  last_draw_time_in_video_conference_mode_ = now;

  // If we see that the page is animating consistently at 30 fps or more, then
  // we assume that BeginFrames can not be throttled. But if the animation
  // frequency is lower than that, then using a lower frame rate is permitted.
  // The delta below is to account for minor offsets in frame times.
  constexpr auto kFudgeDelta = base::Milliseconds(1);
  constexpr auto kMinDelta =
      (viz::BeginFrameArgs::DefaultInterval() * 2) - kFudgeDelta;
  if (draw_delta < kMinDelta)
    num_of_consecutive_frames_with_min_delta_++;
  else
    num_of_consecutive_frames_with_min_delta_ = 0u;
}

base::TimeDelta FrameRateEstimator::GetPreferredInterval() const {
  if (input_priority_mode_) {
    return viz::BeginFrameArgs::MinInterval();
  }

  constexpr size_t kMinNumOfFramesWithMinDelta = 4u;
  if (in_video_conference_mode_ &&
      num_of_consecutive_frames_with_min_delta_ < kMinNumOfFramesWithMinDelta) {
    return viz::BeginFrameArgs::DefaultInterval() * 2;
  }

  static const uint64_t num_did_not_produce_frame_before_throttle =
      static_cast<uint64_t>(
          features::kNumDidNotProduceFrameBeforeThrottle.Get());
  if (IsThrottleFrameRateOnManyDidNotProduceFrameEnabled() &&
      num_did_not_produce_frame_since_last_draw_ >
          num_did_not_produce_frame_before_throttle) {
    return viz::BeginFrameArgs::DefaultInterval() * 2;
  }

  return viz::BeginFrameArgs::MinInterval();
}

void FrameRateEstimator::NotifyInputEvent() {
  input_priority_mode_ = true;
  notifier_.Schedule();
}

void FrameRateEstimator::OnExitInputPriorityMode() {
  input_priority_mode_ = false;
  last_draw_time_in_video_conference_mode_ = base::TimeTicks();
  num_of_consecutive_frames_with_min_delta_ = 0u;
}

void FrameRateEstimator::DidNotProduceFrame() {
  num_of_consecutive_frames_with_min_delta_ = 0u;
  ++num_did_not_produce_frame_since_last_draw_;
}

}  // namespace cc