| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium 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 "content/browser/compositor/gpu_vsync_begin_frame_source.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace content { | |
| 11 namespace { | |
| 12 | |
| 13 class TestBeginFrameObserver : public cc::BeginFrameObserverBase { | |
| 14 public: | |
| 15 TestBeginFrameObserver() = default; | |
| 16 | |
| 17 int begin_frame_count() { return begin_frame_count_; } | |
| 18 | |
| 19 private: | |
| 20 bool OnBeginFrameDerivedImpl(const cc::BeginFrameArgs& args) override { | |
| 21 begin_frame_count_++; | |
| 22 return true; | |
| 23 } | |
| 24 | |
| 25 void OnBeginFrameSourcePausedChanged(bool paused) override {} | |
| 26 | |
| 27 int begin_frame_count_ = 0; | |
| 28 | |
| 29 DISALLOW_COPY_AND_ASSIGN(TestBeginFrameObserver); | |
| 30 }; | |
| 31 | |
| 32 class TestGpuVSyncBeginFrameSource : public GpuVSyncBeginFrameSource { | |
| 33 public: | |
| 34 explicit TestGpuVSyncBeginFrameSource(GpuVSyncControl* vsync_control) | |
| 35 : GpuVSyncBeginFrameSource(vsync_control) {} | |
| 36 | |
| 37 void SetNow(base::TimeTicks now) { now_ = now; } | |
| 38 | |
| 39 private: | |
| 40 base::TimeTicks Now() const override { return now_; } | |
| 41 | |
| 42 base::TimeTicks now_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(TestGpuVSyncBeginFrameSource); | |
| 45 }; | |
| 46 | |
| 47 class GpuVSyncBeginFrameSourceTest : public testing::Test, | |
| 48 public GpuVSyncControl { | |
| 49 public: | |
| 50 GpuVSyncBeginFrameSourceTest() = default; | |
| 51 ~GpuVSyncBeginFrameSourceTest() override = default; | |
| 52 | |
| 53 protected: | |
| 54 void SetNeedsVSync(bool needs_vsync) override { needs_vsync_ = needs_vsync; } | |
| 55 | |
| 56 bool needs_vsync_ = false; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(GpuVSyncBeginFrameSourceTest); | |
| 59 }; | |
| 60 | |
| 61 // Test that an observer can be added to BFS and that it receives OnBeginFrame | |
| 62 // notification. | |
| 63 TEST_F(GpuVSyncBeginFrameSourceTest, BasicTest) { | |
| 64 TestBeginFrameObserver observer; | |
| 65 TestGpuVSyncBeginFrameSource begin_frame_source(this); | |
| 66 | |
| 67 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromHours(2); | |
| 68 begin_frame_source.SetNow(now); | |
| 69 | |
| 70 EXPECT_FALSE(needs_vsync_); | |
| 71 | |
| 72 begin_frame_source.AddObserver(&observer); | |
| 73 | |
| 74 EXPECT_TRUE(needs_vsync_); | |
| 75 EXPECT_FALSE(observer.LastUsedBeginFrameArgs().IsValid()); | |
| 76 | |
| 77 base::TimeTicks frame_time = now - base::TimeDelta::FromSeconds(1); | |
| 78 base::TimeDelta interval = base::TimeDelta::FromSeconds(2); | |
| 79 | |
| 80 begin_frame_source.OnVSync(frame_time, interval); | |
| 81 | |
| 82 EXPECT_EQ(1, observer.begin_frame_count()); | |
| 83 | |
| 84 cc::BeginFrameArgs args = observer.LastUsedBeginFrameArgs(); | |
| 85 EXPECT_TRUE(args.IsValid()); | |
| 86 EXPECT_EQ(frame_time, args.frame_time); | |
| 87 EXPECT_EQ(interval, args.interval); | |
| 88 EXPECT_EQ(frame_time + interval, args.deadline); | |
| 89 EXPECT_EQ(cc::BeginFrameArgs::kStartingFrameNumber + 1, args.sequence_number); | |
| 90 EXPECT_EQ(cc::BeginFrameArgs::NORMAL, args.type); | |
| 91 EXPECT_EQ(begin_frame_source.source_id(), args.source_id); | |
| 92 | |
| 93 // Make sure that the deadline time is correctly advanced forward to be after | |
| 94 // 'now' when frame time is way behind. | |
| 95 now += base::TimeDelta::FromSeconds(10); | |
| 96 begin_frame_source.SetNow(now); | |
| 97 // Frame time is 5 seconds behind but the deadline should be 1 second after | |
| 98 // 'now' considering the 2 second interval. | |
| 99 frame_time = now - base::TimeDelta::FromSeconds(5); | |
| 100 | |
| 101 begin_frame_source.OnVSync(frame_time, interval); | |
| 102 | |
| 103 EXPECT_EQ(2, observer.begin_frame_count()); | |
| 104 | |
| 105 args = observer.LastUsedBeginFrameArgs(); | |
| 106 EXPECT_TRUE(args.IsValid()); | |
| 107 EXPECT_EQ(frame_time, args.frame_time); | |
| 108 EXPECT_EQ(interval, args.interval); | |
| 109 EXPECT_EQ(now + base::TimeDelta::FromSeconds(1), args.deadline); | |
| 110 EXPECT_EQ(cc::BeginFrameArgs::kStartingFrameNumber + 2, args.sequence_number); | |
| 111 EXPECT_EQ(cc::BeginFrameArgs::NORMAL, args.type); | |
| 112 EXPECT_EQ(begin_frame_source.source_id(), args.source_id); | |
| 113 | |
| 114 begin_frame_source.RemoveObserver(&observer); | |
| 115 | |
| 116 EXPECT_EQ(2, observer.begin_frame_count()); | |
| 117 EXPECT_FALSE(needs_vsync_); | |
| 118 } | |
| 119 | |
| 120 // Test that MISSED OnBeginFrame is produced as expected. | |
| 121 TEST_F(GpuVSyncBeginFrameSourceTest, MissedBeginFrameArgs) { | |
| 122 TestBeginFrameObserver observer1; | |
| 123 TestBeginFrameObserver observer2; | |
| 124 TestGpuVSyncBeginFrameSource begin_frame_source(this); | |
| 125 | |
| 126 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromHours(2); | |
| 127 begin_frame_source.SetNow(now); | |
| 128 | |
| 129 begin_frame_source.AddObserver(&observer1); | |
| 130 | |
| 131 // The observer shouldn't be getting any BeginFrame at this point. | |
| 132 EXPECT_EQ(0, observer1.begin_frame_count()); | |
| 133 | |
| 134 base::TimeDelta interval = base::TimeDelta::FromSeconds(2); | |
| 135 | |
| 136 // Trigger first OnBeginFrame 5 seconds behind now. | |
| 137 begin_frame_source.OnVSync(now - base::TimeDelta::FromSeconds(5), interval); | |
| 138 | |
| 139 // The observer should get a NORMAL BeginFrame notification which is covered | |
| 140 // by BasicTest above. | |
| 141 EXPECT_EQ(1, observer1.begin_frame_count()); | |
| 142 EXPECT_EQ(cc::BeginFrameArgs::NORMAL, | |
| 143 observer1.LastUsedBeginFrameArgs().type); | |
| 144 | |
| 145 // Remove the first observer and add it back. It should get a more | |
| 146 // recent missed notification calculated from the projection of the previous | |
| 147 // notification. | |
| 148 begin_frame_source.RemoveObserver(&observer1); | |
| 149 begin_frame_source.AddObserver(&observer1); | |
| 150 | |
| 151 EXPECT_EQ(2, observer1.begin_frame_count()); | |
| 152 | |
| 153 // The projected MISSED frame_time should be 1 second behind 'now'. | |
| 154 base::TimeTicks timestamp1 = now - base::TimeDelta::FromSeconds(1); | |
| 155 cc::BeginFrameArgs args1 = observer1.LastUsedBeginFrameArgs(); | |
| 156 EXPECT_TRUE(args1.IsValid()); | |
| 157 EXPECT_EQ(timestamp1, args1.frame_time); | |
| 158 EXPECT_EQ(timestamp1 + interval, args1.deadline); | |
| 159 EXPECT_EQ(cc::BeginFrameArgs::kStartingFrameNumber + 2, | |
| 160 args1.sequence_number); | |
| 161 EXPECT_EQ(cc::BeginFrameArgs::MISSED, args1.type); | |
| 162 | |
| 163 // Add second observer which should receive the same notification. | |
| 164 begin_frame_source.AddObserver(&observer2); | |
| 165 | |
| 166 EXPECT_EQ(1, observer2.begin_frame_count()); | |
| 167 | |
| 168 cc::BeginFrameArgs args2 = observer2.LastUsedBeginFrameArgs(); | |
| 169 EXPECT_TRUE(args2.IsValid()); | |
| 170 EXPECT_EQ(timestamp1, args2.frame_time); | |
| 171 EXPECT_EQ(timestamp1 + interval, args2.deadline); | |
| 172 EXPECT_EQ(cc::BeginFrameArgs::kStartingFrameNumber + 2, | |
| 173 args2.sequence_number); | |
| 174 EXPECT_EQ(cc::BeginFrameArgs::MISSED, args2.type); | |
| 175 | |
| 176 // Adding and removing the second observer shouldn't produce any | |
| 177 // new notifications. | |
| 178 begin_frame_source.RemoveObserver(&observer2); | |
| 179 begin_frame_source.AddObserver(&observer2); | |
| 180 | |
| 181 EXPECT_EQ(1, observer2.begin_frame_count()); | |
| 182 } | |
| 183 | |
| 184 } // namespace | |
| 185 } // namespace content | |
| OLD | NEW |