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() {} | |
sunnyps
2017/05/30 22:13:17
nit: Can you = default the ctors/dtors instead?
stanisc
2017/05/31 01:17:53
Done.
| |
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{}; | |
sunnyps
2017/05/30 22:13:17
nit: space between override and {} in accordance w
stanisc
2017/05/31 01:17:53
It is interesting that presubmit complains when th
| |
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() {} | |
51 ~GpuVSyncBeginFrameSourceTest() override {} | |
52 | |
53 void SetUp() override {} | |
54 void TearDown() override {} | |
sunnyps
2017/05/30 22:13:17
nit: Can you remove SetUp and TearDown if you're n
stanisc
2017/05/31 01:17:53
Done.
| |
55 | |
56 protected: | |
57 void SetNeedsVSync(bool needs_vsync) override { needs_vsync_ = needs_vsync; } | |
58 | |
59 bool needs_vsync_ = false; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(GpuVSyncBeginFrameSourceTest); | |
62 }; | |
63 | |
64 // Test that an observer can be added to BFS and that it receives OnBeginFrame | |
65 // notification. | |
66 TEST_F(GpuVSyncBeginFrameSourceTest, BasicTest) { | |
67 TestBeginFrameObserver observer; | |
68 TestGpuVSyncBeginFrameSource begin_frame_source(this); | |
69 | |
70 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromHours(2); | |
71 begin_frame_source.SetNow(now); | |
72 | |
73 EXPECT_FALSE(needs_vsync_); | |
74 | |
75 begin_frame_source.AddObserver(&observer); | |
76 | |
77 EXPECT_TRUE(needs_vsync_); | |
78 EXPECT_FALSE(observer.LastUsedBeginFrameArgs().IsValid()); | |
79 | |
80 base::TimeTicks frame_time = now - base::TimeDelta::FromSeconds(1); | |
81 base::TimeDelta interval = base::TimeDelta::FromSeconds(2); | |
82 | |
83 begin_frame_source.OnVSync(frame_time, interval); | |
84 | |
85 EXPECT_EQ(1, observer.begin_frame_count()); | |
86 | |
87 cc::BeginFrameArgs args = observer.LastUsedBeginFrameArgs(); | |
88 EXPECT_TRUE(args.IsValid()); | |
89 EXPECT_EQ(frame_time, args.frame_time); | |
90 EXPECT_EQ(interval, args.interval); | |
91 EXPECT_EQ(frame_time + interval, args.deadline); | |
92 EXPECT_EQ(cc::BeginFrameArgs::kStartingFrameNumber + 1, args.sequence_number); | |
93 EXPECT_EQ(cc::BeginFrameArgs::NORMAL, args.type); | |
94 EXPECT_EQ(begin_frame_source.source_id(), args.source_id); | |
95 | |
96 // Make sure that the deadline time is correctly advanced forward to be after | |
97 // 'now' when frame time is way behind. | |
98 now += base::TimeDelta::FromSeconds(10); | |
99 begin_frame_source.SetNow(now); | |
100 // Frame time is 5 seconds behind but the deadline should be 1 second after | |
101 // 'now' considering the 2 second interval. | |
102 frame_time = now - base::TimeDelta::FromSeconds(5); | |
103 | |
104 begin_frame_source.OnVSync(frame_time, interval); | |
105 | |
106 EXPECT_EQ(2, observer.begin_frame_count()); | |
107 | |
108 args = observer.LastUsedBeginFrameArgs(); | |
109 EXPECT_TRUE(args.IsValid()); | |
110 EXPECT_EQ(frame_time, args.frame_time); | |
sunnyps
2017/05/30 22:13:17
nit: Should the BFS also adjust the frame time lik
stanisc
2017/05/31 01:17:53
frame time is passed to RAF so I think it shouldn'
| |
111 EXPECT_EQ(interval, args.interval); | |
112 EXPECT_EQ(now + base::TimeDelta::FromSeconds(1), args.deadline); | |
113 EXPECT_EQ(cc::BeginFrameArgs::kStartingFrameNumber + 2, args.sequence_number); | |
114 EXPECT_EQ(cc::BeginFrameArgs::NORMAL, args.type); | |
115 EXPECT_EQ(begin_frame_source.source_id(), args.source_id); | |
116 | |
117 begin_frame_source.RemoveObserver(&observer); | |
118 | |
119 EXPECT_EQ(2, observer.begin_frame_count()); | |
120 EXPECT_FALSE(needs_vsync_); | |
121 } | |
122 | |
123 // Test that MISSED OnBeginFrame is produced as expected. | |
124 TEST_F(GpuVSyncBeginFrameSourceTest, MissedBeginFrameArgs) { | |
125 TestBeginFrameObserver observer1; | |
126 TestBeginFrameObserver observer2; | |
127 TestGpuVSyncBeginFrameSource begin_frame_source(this); | |
128 | |
129 base::TimeTicks now = base::TimeTicks() + base::TimeDelta::FromHours(2); | |
130 begin_frame_source.SetNow(now); | |
131 | |
132 begin_frame_source.AddObserver(&observer1); | |
sunnyps
2017/05/30 22:13:17
nit: Can you add an EXPECT that the observer doesn
stanisc
2017/05/31 01:17:53
Done.
| |
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); | |
sunnyps
2017/05/30 22:13:17
nit: Can you add EXPECTs for the observer's begin
stanisc
2017/05/31 01:17:53
This is covered by BasicTest. I've added a simple
| |
138 | |
139 // Remove the first observer and add it back. It should get a more | |
140 // recent missed notification calculated from the projection of the previous | |
141 // notification. | |
142 begin_frame_source.RemoveObserver(&observer1); | |
143 begin_frame_source.AddObserver(&observer1); | |
144 | |
145 EXPECT_EQ(2, observer1.begin_frame_count()); | |
146 | |
147 // The projected MISSED frame_time should be 1 second behind 'now'. | |
148 base::TimeTicks timestamp1 = now - base::TimeDelta::FromSeconds(1); | |
149 cc::BeginFrameArgs args1 = observer1.LastUsedBeginFrameArgs(); | |
150 EXPECT_TRUE(args1.IsValid()); | |
151 EXPECT_EQ(timestamp1, args1.frame_time); | |
sunnyps
2017/05/30 22:13:17
nit: EXPECT for the deadline?
stanisc
2017/05/31 01:17:53
Done.
| |
152 EXPECT_EQ(cc::BeginFrameArgs::kStartingFrameNumber + 2, | |
153 args1.sequence_number); | |
154 EXPECT_EQ(cc::BeginFrameArgs::MISSED, args1.type); | |
155 | |
156 // Add second observer which should receive the same notification. | |
157 begin_frame_source.AddObserver(&observer2); | |
158 | |
159 EXPECT_EQ(1, observer2.begin_frame_count()); | |
160 | |
161 cc::BeginFrameArgs args2 = observer2.LastUsedBeginFrameArgs(); | |
162 EXPECT_TRUE(args2.IsValid()); | |
163 EXPECT_EQ(timestamp1, args2.frame_time); | |
sunnyps
2017/05/30 22:13:17
nit: deadline?
stanisc
2017/05/31 01:17:53
Done.
| |
164 EXPECT_EQ(cc::BeginFrameArgs::kStartingFrameNumber + 2, | |
165 args2.sequence_number); | |
166 EXPECT_EQ(cc::BeginFrameArgs::MISSED, args2.type); | |
167 | |
168 // Adding and removing the second observer shouldn't produce any | |
169 // new notifications. | |
170 begin_frame_source.RemoveObserver(&observer2); | |
171 begin_frame_source.AddObserver(&observer2); | |
172 | |
173 EXPECT_EQ(1, observer2.begin_frame_count()); | |
174 } | |
175 | |
176 } // namespace | |
177 } // namespace content | |
OLD | NEW |