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
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211

cc / slim / delayer_scheduler_unittest.cc [blame]

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

#include <optional>

#include "cc/slim/delayed_scheduler.h"
#include "cc/slim/scheduler.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc::slim {

namespace {

class TestSchedulerClient : public SchedulerClient {
 public:
  explicit TestSchedulerClient(Scheduler* scheduler) : scheduler_(scheduler) {}

  bool DoBeginFrame(const viz::BeginFrameArgs& begin_frame_args) override {
    last_do_begin_frame_args_ = begin_frame_args;
    if (do_begin_frame_result_) {
      scheduler_->SetIsSwapThrottled(true);
    }
    return do_begin_frame_result_;
  }
  void SendDidNotProduceFrame(
      const viz::BeginFrameArgs& begin_frame_args) override {
    last_did_not_produce_frame_args_ = begin_frame_args;
  }

  void SetDoBeginFrameResult(bool result) { do_begin_frame_result_ = result; }
  std::optional<viz::BeginFrameArgs> TakeLastDoBeginFrameArgs() {
    auto rv = last_do_begin_frame_args_;
    last_do_begin_frame_args_.reset();
    return rv;
  }
  std::optional<viz::BeginFrameArgs> TakeLastDidNotProduceFrameArgs() {
    auto rv = last_did_not_produce_frame_args_;
    last_did_not_produce_frame_args_.reset();
    return rv;
  }

 private:
  const raw_ptr<Scheduler> scheduler_;
  std::optional<viz::BeginFrameArgs> last_do_begin_frame_args_;
  std::optional<viz::BeginFrameArgs> last_did_not_produce_frame_args_;
  bool do_begin_frame_result_ = false;
};

class SlimDelayedSchedulerTest : public testing::Test {
 public:
  void SetUp() override { scheduler_.Initialize(&client_); }

  viz::BeginFrameArgs GenBeginFrameArgs() {
    base::TimeTicks frame_time = base::TimeTicks::Now();
    base::TimeDelta interval = viz::BeginFrameArgs::DefaultInterval();
    return viz::BeginFrameArgs::Create(BEGINFRAME_FROM_HERE,
                                       /*source_id=*/1, ++sequence_id_,
                                       frame_time, frame_time + interval,
                                       interval, viz::BeginFrameArgs::NORMAL);
  }

  void CheckDoBeginFrameCalledWith(const viz::BeginFrameArgs& args) {
    auto args_opt = client_.TakeLastDoBeginFrameArgs();
    ASSERT_TRUE(args_opt);
    EXPECT_EQ(args_opt->frame_id, args.frame_id);
  }

  void CheckDidNotProcueFrameCalledWith(const viz::BeginFrameArgs& args) {
    auto args_opt = client_.TakeLastDidNotProduceFrameArgs();
    ASSERT_TRUE(args_opt);
    EXPECT_EQ(args_opt->frame_id, args.frame_id);
  }

  void CheckDoBeginFrameNotCalled() {
    EXPECT_FALSE(client_.TakeLastDoBeginFrameArgs());
  }

  void CheckDidNotProcueFrameNotCalled() {
    EXPECT_FALSE(client_.TakeLastDidNotProduceFrameArgs());
  }

 protected:
  uint64_t sequence_id_ = 0;
  DelayedScheduler scheduler_;
  TestSchedulerClient client_{&scheduler_};
};

TEST_F(SlimDelayedSchedulerTest, DelayedBeginFrame) {
  scheduler_.SetNeedsBeginFrame(true);

  client_.SetDoBeginFrameResult(false);
  viz::BeginFrameArgs args1 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args1);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();

  viz::BeginFrameArgs args2 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args2);
  CheckDoBeginFrameCalledWith(args1);
  CheckDidNotProcueFrameCalledWith(args1);

  viz::BeginFrameArgs args3 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args3);
  CheckDoBeginFrameCalledWith(args2);
  CheckDidNotProcueFrameCalledWith(args2);

  client_.SetDoBeginFrameResult(true);
  viz::BeginFrameArgs args4 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args4);
  CheckDoBeginFrameCalledWith(args3);
  CheckDidNotProcueFrameNotCalled();
  scheduler_.SetIsSwapThrottled(false);

  viz::BeginFrameArgs args5 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args5);
  CheckDoBeginFrameCalledWith(args4);
  CheckDidNotProcueFrameNotCalled();
  scheduler_.SetIsSwapThrottled(false);

  scheduler_.SetNeedsBeginFrame(false);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameCalledWith(args5);
}

TEST_F(SlimDelayedSchedulerTest, MaybeCompositeNow) {
  scheduler_.SetNeedsBeginFrame(true);
  client_.SetDoBeginFrameResult(false);

  viz::BeginFrameArgs args1 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args1);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();

  scheduler_.MaybeCompositeNow();
  CheckDoBeginFrameCalledWith(args1);
  CheckDidNotProcueFrameCalledWith(args1);

  viz::BeginFrameArgs args2 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args2);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();

  scheduler_.MaybeCompositeNow();
  CheckDoBeginFrameCalledWith(args2);
  CheckDidNotProcueFrameCalledWith(args2);

  scheduler_.MaybeCompositeNow();
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();

  viz::BeginFrameArgs args3 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args3);
  // Missed MaybeCompositeNow so begin frame immediately.
  CheckDoBeginFrameCalledWith(args3);
  CheckDidNotProcueFrameCalledWith(args3);

  scheduler_.SetNeedsBeginFrame(false);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();
}

TEST_F(SlimDelayedSchedulerTest, SwapThrottled) {
  scheduler_.SetNeedsBeginFrame(true);
  client_.SetDoBeginFrameResult(true);

  viz::BeginFrameArgs args1 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args1);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();

  scheduler_.MaybeCompositeNow();
  CheckDoBeginFrameCalledWith(args1);
  CheckDidNotProcueFrameNotCalled();
  // Swap throttled at this point.

  viz::BeginFrameArgs args2 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args1);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();

  scheduler_.MaybeCompositeNow();
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();

  scheduler_.SetIsSwapThrottled(false);
  CheckDoBeginFrameCalledWith(args1);
  CheckDidNotProcueFrameNotCalled();

  scheduler_.SetNeedsBeginFrame(false);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();
}

TEST_F(SlimDelayedSchedulerTest, BeginFramePaused) {
  scheduler_.SetNeedsBeginFrame(true);
  client_.SetDoBeginFrameResult(true);

  viz::BeginFrameArgs args1 = GenBeginFrameArgs();
  scheduler_.OnBeginFrameFromViz(args1);
  CheckDoBeginFrameNotCalled();
  CheckDidNotProcueFrameNotCalled();

  scheduler_.OnBeginFramePausedChanged(true);
  CheckDoBeginFrameCalledWith(args1);
  CheckDidNotProcueFrameNotCalled();
}

}  // namespace

}  // namespace cc::slim