OLD | NEW |
| (Empty) |
1 // Copyright 2011 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 "config.h" | |
6 | |
7 #include "CCScheduler.h" | |
8 | |
9 #include "CCSchedulerTestCommon.h" | |
10 #include "testing/gmock/include/gmock/gmock.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 #include <wtf/OwnPtr.h> | |
13 | |
14 using namespace cc; | |
15 using namespace WebKitTests; | |
16 using namespace WTF; | |
17 | |
18 namespace { | |
19 | |
20 class FakeCCSchedulerClient : public CCSchedulerClient { | |
21 public: | |
22 FakeCCSchedulerClient() { reset(); } | |
23 void reset() | |
24 { | |
25 m_actions.clear(); | |
26 m_drawWillHappen = true; | |
27 m_swapWillHappenIfDrawHappens = true; | |
28 m_numDraws = 0; | |
29 } | |
30 | |
31 int numDraws() const { return m_numDraws; } | |
32 int numActions() const { return static_cast<int>(m_actions.size()); } | |
33 const char* action(int i) const { return m_actions[i]; } | |
34 | |
35 bool hasAction(const char* action) const | |
36 { | |
37 for (size_t i = 0; i < m_actions.size(); i++) | |
38 if (!strcmp(m_actions[i], action)) | |
39 return true; | |
40 return false; | |
41 } | |
42 | |
43 virtual void scheduledActionBeginFrame() OVERRIDE { m_actions.push_back("sch
eduledActionBeginFrame"); } | |
44 virtual CCScheduledActionDrawAndSwapResult scheduledActionDrawAndSwapIfPossi
ble() OVERRIDE | |
45 { | |
46 m_actions.push_back("scheduledActionDrawAndSwapIfPossible"); | |
47 m_numDraws++; | |
48 return CCScheduledActionDrawAndSwapResult(m_drawWillHappen, m_drawWillHa
ppen && m_swapWillHappenIfDrawHappens); | |
49 } | |
50 | |
51 virtual CCScheduledActionDrawAndSwapResult scheduledActionDrawAndSwapForced(
) OVERRIDE | |
52 { | |
53 m_actions.push_back("scheduledActionDrawAndSwapForced"); | |
54 return CCScheduledActionDrawAndSwapResult(true, m_swapWillHappenIfDrawHa
ppens); | |
55 } | |
56 | |
57 virtual void scheduledActionCommit() OVERRIDE { m_actions.push_back("schedul
edActionCommit"); } | |
58 virtual void scheduledActionBeginContextRecreation() OVERRIDE { m_actions.pu
sh_back("scheduledActionBeginContextRecreation"); } | |
59 virtual void scheduledActionAcquireLayerTexturesForMainThread() OVERRIDE { m
_actions.push_back("scheduledActionAcquireLayerTexturesForMainThread"); } | |
60 virtual void didAnticipatedDrawTimeChange(base::TimeTicks) OVERRIDE { } | |
61 | |
62 void setDrawWillHappen(bool drawWillHappen) { m_drawWillHappen = drawWillHap
pen; } | |
63 void setSwapWillHappenIfDrawHappens(bool swapWillHappenIfDrawHappens) { m_sw
apWillHappenIfDrawHappens = swapWillHappenIfDrawHappens; } | |
64 | |
65 protected: | |
66 bool m_drawWillHappen; | |
67 bool m_swapWillHappenIfDrawHappens; | |
68 int m_numDraws; | |
69 std::vector<const char*> m_actions; | |
70 }; | |
71 | |
72 TEST(CCSchedulerTest, RequestCommit) | |
73 { | |
74 FakeCCSchedulerClient client; | |
75 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
76 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, make_scoped_ptr
(new CCFrameRateController(timeSource))); | |
77 scheduler->setCanBeginFrame(true); | |
78 scheduler->setVisible(true); | |
79 scheduler->setCanDraw(true); | |
80 | |
81 // SetNeedsCommit should begin the frame. | |
82 scheduler->setNeedsCommit(); | |
83 EXPECT_EQ(1, client.numActions()); | |
84 EXPECT_STREQ("scheduledActionBeginFrame", client.action(0)); | |
85 EXPECT_FALSE(timeSource->active()); | |
86 client.reset(); | |
87 | |
88 // beginFrameComplete should commit | |
89 scheduler->beginFrameComplete(); | |
90 EXPECT_EQ(1, client.numActions()); | |
91 EXPECT_STREQ("scheduledActionCommit", client.action(0)); | |
92 EXPECT_TRUE(timeSource->active()); | |
93 client.reset(); | |
94 | |
95 // Tick should draw. | |
96 timeSource->tick(); | |
97 EXPECT_EQ(1, client.numActions()); | |
98 EXPECT_STREQ("scheduledActionDrawAndSwapIfPossible", client.action(0)); | |
99 EXPECT_FALSE(timeSource->active()); | |
100 client.reset(); | |
101 | |
102 // Timer should be off. | |
103 EXPECT_FALSE(timeSource->active()); | |
104 } | |
105 | |
106 TEST(CCSchedulerTest, RequestCommitAfterBeginFrame) | |
107 { | |
108 FakeCCSchedulerClient client; | |
109 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
110 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, make_scoped_ptr
(new CCFrameRateController(timeSource))); | |
111 scheduler->setCanBeginFrame(true); | |
112 scheduler->setVisible(true); | |
113 scheduler->setCanDraw(true); | |
114 | |
115 // SetNedsCommit should begin the frame. | |
116 scheduler->setNeedsCommit(); | |
117 EXPECT_EQ(1, client.numActions()); | |
118 EXPECT_STREQ("scheduledActionBeginFrame", client.action(0)); | |
119 client.reset(); | |
120 | |
121 // Now setNeedsCommit again. Calling here means we need a second frame. | |
122 scheduler->setNeedsCommit(); | |
123 | |
124 // Since, another commit is needed, beginFrameComplete should commit, | |
125 // then begin another frame. | |
126 scheduler->beginFrameComplete(); | |
127 EXPECT_EQ(1, client.numActions()); | |
128 EXPECT_STREQ("scheduledActionCommit", client.action(0)); | |
129 client.reset(); | |
130 | |
131 // Tick should draw but then begin another frame. | |
132 timeSource->tick(); | |
133 EXPECT_FALSE(timeSource->active()); | |
134 EXPECT_EQ(2, client.numActions()); | |
135 EXPECT_STREQ("scheduledActionDrawAndSwapIfPossible", client.action(0)); | |
136 EXPECT_STREQ("scheduledActionBeginFrame", client.action(1)); | |
137 client.reset(); | |
138 } | |
139 | |
140 TEST(CCSchedulerTest, TextureAcquisitionCollision) | |
141 { | |
142 FakeCCSchedulerClient client; | |
143 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
144 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, make_scoped_ptr
(new CCFrameRateController(timeSource))); | |
145 scheduler->setCanBeginFrame(true); | |
146 scheduler->setVisible(true); | |
147 scheduler->setCanDraw(true); | |
148 | |
149 scheduler->setNeedsCommit(); | |
150 scheduler->setMainThreadNeedsLayerTextures(); | |
151 EXPECT_EQ(2, client.numActions()); | |
152 EXPECT_STREQ("scheduledActionBeginFrame", client.action(0)); | |
153 EXPECT_STREQ("scheduledActionAcquireLayerTexturesForMainThread", client.acti
on(1)); | |
154 client.reset(); | |
155 | |
156 // Compositor not scheduled to draw because textures are locked by main thre
ad | |
157 EXPECT_FALSE(timeSource->active()); | |
158 | |
159 // Trigger the commit | |
160 scheduler->beginFrameComplete(); | |
161 EXPECT_TRUE(timeSource->active()); | |
162 client.reset(); | |
163 | |
164 // Between commit and draw, texture acquisition for main thread delayed, | |
165 // and main thread blocks. | |
166 scheduler->setMainThreadNeedsLayerTextures(); | |
167 EXPECT_EQ(0, client.numActions()); | |
168 client.reset(); | |
169 | |
170 // Once compositor draw complete, the delayed texture acquisition fires. | |
171 timeSource->tick(); | |
172 EXPECT_EQ(3, client.numActions()); | |
173 EXPECT_STREQ("scheduledActionDrawAndSwapIfPossible", client.action(0)); | |
174 EXPECT_STREQ("scheduledActionAcquireLayerTexturesForMainThread", client.acti
on(1)); | |
175 EXPECT_STREQ("scheduledActionBeginFrame", client.action(2)); | |
176 client.reset(); | |
177 } | |
178 | |
179 TEST(CCSchedulerTest, VisibilitySwitchWithTextureAcquisition) | |
180 { | |
181 FakeCCSchedulerClient client; | |
182 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
183 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, make_scoped_ptr
(new CCFrameRateController(timeSource))); | |
184 scheduler->setCanBeginFrame(true); | |
185 scheduler->setVisible(true); | |
186 scheduler->setCanDraw(true); | |
187 | |
188 scheduler->setNeedsCommit(); | |
189 scheduler->beginFrameComplete(); | |
190 scheduler->setMainThreadNeedsLayerTextures(); | |
191 client.reset(); | |
192 // Verify that pending texture acquisition fires when visibility | |
193 // is lost in order to avoid a deadlock. | |
194 scheduler->setVisible(false); | |
195 EXPECT_EQ(1, client.numActions()); | |
196 EXPECT_STREQ("scheduledActionAcquireLayerTexturesForMainThread", client.acti
on(0)); | |
197 client.reset(); | |
198 | |
199 // Regaining visibility with textures acquired by main thread while | |
200 // compositor is waiting for first draw should result in a request | |
201 // for a new frame in order to escape a deadlock. | |
202 scheduler->setVisible(true); | |
203 EXPECT_EQ(1, client.numActions()); | |
204 EXPECT_STREQ("scheduledActionBeginFrame", client.action(0)); | |
205 client.reset(); | |
206 } | |
207 | |
208 class SchedulerClientThatSetNeedsDrawInsideDraw : public FakeCCSchedulerClient { | |
209 public: | |
210 SchedulerClientThatSetNeedsDrawInsideDraw() | |
211 : m_scheduler(0) { } | |
212 | |
213 void setScheduler(CCScheduler* scheduler) { m_scheduler = scheduler; } | |
214 | |
215 virtual void scheduledActionBeginFrame() OVERRIDE { } | |
216 virtual CCScheduledActionDrawAndSwapResult scheduledActionDrawAndSwapIfPossi
ble() OVERRIDE | |
217 { | |
218 // Only setNeedsRedraw the first time this is called | |
219 if (!m_numDraws) | |
220 m_scheduler->setNeedsRedraw(); | |
221 return FakeCCSchedulerClient::scheduledActionDrawAndSwapIfPossible(); | |
222 } | |
223 | |
224 virtual CCScheduledActionDrawAndSwapResult scheduledActionDrawAndSwapForced(
) OVERRIDE | |
225 { | |
226 ASSERT_NOT_REACHED(); | |
227 return CCScheduledActionDrawAndSwapResult(true, true); | |
228 } | |
229 | |
230 virtual void scheduledActionCommit() OVERRIDE { } | |
231 virtual void scheduledActionBeginContextRecreation() OVERRIDE { } | |
232 virtual void didAnticipatedDrawTimeChange(base::TimeTicks) OVERRIDE { } | |
233 | |
234 protected: | |
235 CCScheduler* m_scheduler; | |
236 }; | |
237 | |
238 // Tests for two different situations: | |
239 // 1. the scheduler dropping setNeedsRedraw requests that happen inside | |
240 // a scheduledActionDrawAndSwap | |
241 // 2. the scheduler drawing twice inside a single tick | |
242 TEST(CCSchedulerTest, RequestRedrawInsideDraw) | |
243 { | |
244 SchedulerClientThatSetNeedsDrawInsideDraw client; | |
245 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
246 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, make_scoped_ptr
(new CCFrameRateController(timeSource))); | |
247 client.setScheduler(scheduler.get()); | |
248 scheduler->setCanBeginFrame(true); | |
249 scheduler->setVisible(true); | |
250 scheduler->setCanDraw(true); | |
251 | |
252 scheduler->setNeedsRedraw(); | |
253 EXPECT_TRUE(scheduler->redrawPending()); | |
254 EXPECT_TRUE(timeSource->active()); | |
255 EXPECT_EQ(0, client.numDraws()); | |
256 | |
257 timeSource->tick(); | |
258 EXPECT_EQ(1, client.numDraws()); | |
259 EXPECT_TRUE(scheduler->redrawPending()); | |
260 EXPECT_TRUE(timeSource->active()); | |
261 | |
262 timeSource->tick(); | |
263 EXPECT_EQ(2, client.numDraws()); | |
264 EXPECT_FALSE(scheduler->redrawPending()); | |
265 EXPECT_FALSE(timeSource->active()); | |
266 } | |
267 | |
268 // Test that requesting redraw inside a failed draw doesn't lose the request. | |
269 TEST(CCSchedulerTest, RequestRedrawInsideFailedDraw) | |
270 { | |
271 SchedulerClientThatSetNeedsDrawInsideDraw client; | |
272 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
273 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, make_scoped_ptr
(new CCFrameRateController(timeSource))); | |
274 client.setScheduler(scheduler.get()); | |
275 scheduler->setCanBeginFrame(true); | |
276 scheduler->setVisible(true); | |
277 scheduler->setCanDraw(true); | |
278 client.setDrawWillHappen(false); | |
279 | |
280 scheduler->setNeedsRedraw(); | |
281 EXPECT_TRUE(scheduler->redrawPending()); | |
282 EXPECT_TRUE(timeSource->active()); | |
283 EXPECT_EQ(0, client.numDraws()); | |
284 | |
285 // Fail the draw. | |
286 timeSource->tick(); | |
287 EXPECT_EQ(1, client.numDraws()); | |
288 | |
289 // We have a commit pending and the draw failed, and we didn't lose the redr
aw request. | |
290 EXPECT_TRUE(scheduler->commitPending()); | |
291 EXPECT_TRUE(scheduler->redrawPending()); | |
292 EXPECT_TRUE(timeSource->active()); | |
293 | |
294 // Fail the draw again. | |
295 timeSource->tick(); | |
296 EXPECT_EQ(2, client.numDraws()); | |
297 EXPECT_TRUE(scheduler->commitPending()); | |
298 EXPECT_TRUE(scheduler->redrawPending()); | |
299 EXPECT_TRUE(timeSource->active()); | |
300 | |
301 // Draw successfully. | |
302 client.setDrawWillHappen(true); | |
303 timeSource->tick(); | |
304 EXPECT_EQ(3, client.numDraws()); | |
305 EXPECT_TRUE(scheduler->commitPending()); | |
306 EXPECT_FALSE(scheduler->redrawPending()); | |
307 EXPECT_FALSE(timeSource->active()); | |
308 } | |
309 | |
310 class SchedulerClientThatSetNeedsCommitInsideDraw : public FakeCCSchedulerClient
{ | |
311 public: | |
312 SchedulerClientThatSetNeedsCommitInsideDraw() | |
313 : m_scheduler(0) { } | |
314 | |
315 void setScheduler(CCScheduler* scheduler) { m_scheduler = scheduler; } | |
316 | |
317 virtual void scheduledActionBeginFrame() OVERRIDE { } | |
318 virtual CCScheduledActionDrawAndSwapResult scheduledActionDrawAndSwapIfPossi
ble() OVERRIDE | |
319 { | |
320 // Only setNeedsCommit the first time this is called | |
321 if (!m_numDraws) | |
322 m_scheduler->setNeedsCommit(); | |
323 return FakeCCSchedulerClient::scheduledActionDrawAndSwapIfPossible(); | |
324 } | |
325 | |
326 virtual CCScheduledActionDrawAndSwapResult scheduledActionDrawAndSwapForced(
) OVERRIDE | |
327 { | |
328 ASSERT_NOT_REACHED(); | |
329 return CCScheduledActionDrawAndSwapResult(true, true); | |
330 } | |
331 | |
332 virtual void scheduledActionCommit() OVERRIDE { } | |
333 virtual void scheduledActionBeginContextRecreation() OVERRIDE { } | |
334 virtual void didAnticipatedDrawTimeChange(base::TimeTicks) OVERRIDE { } | |
335 | |
336 protected: | |
337 CCScheduler* m_scheduler; | |
338 }; | |
339 | |
340 // Tests for the scheduler infinite-looping on setNeedsCommit requests that | |
341 // happen inside a scheduledActionDrawAndSwap | |
342 TEST(CCSchedulerTest, RequestCommitInsideDraw) | |
343 { | |
344 SchedulerClientThatSetNeedsCommitInsideDraw client; | |
345 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
346 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, make_scoped_ptr
(new CCFrameRateController(timeSource))); | |
347 client.setScheduler(scheduler.get()); | |
348 scheduler->setCanBeginFrame(true); | |
349 scheduler->setVisible(true); | |
350 scheduler->setCanDraw(true); | |
351 | |
352 scheduler->setNeedsRedraw(); | |
353 EXPECT_TRUE(scheduler->redrawPending()); | |
354 EXPECT_EQ(0, client.numDraws()); | |
355 EXPECT_TRUE(timeSource->active()); | |
356 | |
357 timeSource->tick(); | |
358 EXPECT_FALSE(timeSource->active()); | |
359 EXPECT_EQ(1, client.numDraws()); | |
360 EXPECT_TRUE(scheduler->commitPending()); | |
361 scheduler->beginFrameComplete(); | |
362 | |
363 timeSource->tick(); | |
364 EXPECT_EQ(2, client.numDraws()); | |
365 EXPECT_FALSE(timeSource->active()); | |
366 EXPECT_FALSE(scheduler->redrawPending()); | |
367 } | |
368 | |
369 // Tests that when a draw fails then the pending commit should not be dropped. | |
370 TEST(CCSchedulerTest, RequestCommitInsideFailedDraw) | |
371 { | |
372 SchedulerClientThatSetNeedsDrawInsideDraw client; | |
373 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
374 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, make_scoped_ptr
(new CCFrameRateController(timeSource))); | |
375 client.setScheduler(scheduler.get()); | |
376 scheduler->setCanBeginFrame(true); | |
377 scheduler->setVisible(true); | |
378 scheduler->setCanDraw(true); | |
379 client.setDrawWillHappen(false); | |
380 | |
381 scheduler->setNeedsRedraw(); | |
382 EXPECT_TRUE(scheduler->redrawPending()); | |
383 EXPECT_TRUE(timeSource->active()); | |
384 EXPECT_EQ(0, client.numDraws()); | |
385 | |
386 // Fail the draw. | |
387 timeSource->tick(); | |
388 EXPECT_EQ(1, client.numDraws()); | |
389 | |
390 // We have a commit pending and the draw failed, and we didn't lose the comm
it request. | |
391 EXPECT_TRUE(scheduler->commitPending()); | |
392 EXPECT_TRUE(scheduler->redrawPending()); | |
393 EXPECT_TRUE(timeSource->active()); | |
394 | |
395 // Fail the draw again. | |
396 timeSource->tick(); | |
397 EXPECT_EQ(2, client.numDraws()); | |
398 EXPECT_TRUE(scheduler->commitPending()); | |
399 EXPECT_TRUE(scheduler->redrawPending()); | |
400 EXPECT_TRUE(timeSource->active()); | |
401 | |
402 // Draw successfully. | |
403 client.setDrawWillHappen(true); | |
404 timeSource->tick(); | |
405 EXPECT_EQ(3, client.numDraws()); | |
406 EXPECT_TRUE(scheduler->commitPending()); | |
407 EXPECT_FALSE(scheduler->redrawPending()); | |
408 EXPECT_FALSE(timeSource->active()); | |
409 } | |
410 | |
411 TEST(CCSchedulerTest, NoBeginFrameWhenDrawFails) | |
412 { | |
413 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
414 SchedulerClientThatSetNeedsCommitInsideDraw client; | |
415 scoped_ptr<FakeCCFrameRateController> controller(new FakeCCFrameRateControll
er(timeSource)); | |
416 FakeCCFrameRateController* controllerPtr = controller.get(); | |
417 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, controller.Pass
As<CCFrameRateController>()); | |
418 client.setScheduler(scheduler.get()); | |
419 scheduler->setCanBeginFrame(true); | |
420 scheduler->setVisible(true); | |
421 scheduler->setCanDraw(true); | |
422 | |
423 EXPECT_EQ(0, controllerPtr->numFramesPending()); | |
424 | |
425 scheduler->setNeedsRedraw(); | |
426 EXPECT_TRUE(scheduler->redrawPending()); | |
427 EXPECT_TRUE(timeSource->active()); | |
428 EXPECT_EQ(0, client.numDraws()); | |
429 | |
430 // Draw successfully, this starts a new frame. | |
431 timeSource->tick(); | |
432 EXPECT_EQ(1, client.numDraws()); | |
433 EXPECT_EQ(1, controllerPtr->numFramesPending()); | |
434 scheduler->didSwapBuffersComplete(); | |
435 EXPECT_EQ(0, controllerPtr->numFramesPending()); | |
436 | |
437 scheduler->setNeedsRedraw(); | |
438 EXPECT_TRUE(scheduler->redrawPending()); | |
439 EXPECT_TRUE(timeSource->active()); | |
440 | |
441 // Fail to draw, this should not start a frame. | |
442 client.setDrawWillHappen(false); | |
443 timeSource->tick(); | |
444 EXPECT_EQ(2, client.numDraws()); | |
445 EXPECT_EQ(0, controllerPtr->numFramesPending()); | |
446 } | |
447 | |
448 TEST(CCSchedulerTest, NoBeginFrameWhenSwapFailsDuringForcedCommit) | |
449 { | |
450 RefPtr<FakeCCTimeSource> timeSource = adoptRef(new FakeCCTimeSource()); | |
451 FakeCCSchedulerClient client; | |
452 scoped_ptr<FakeCCFrameRateController> controller(new FakeCCFrameRateControll
er(timeSource)); | |
453 FakeCCFrameRateController* controllerPtr = controller.get(); | |
454 OwnPtr<CCScheduler> scheduler = CCScheduler::create(&client, controller.Pass
As<CCFrameRateController>()); | |
455 | |
456 EXPECT_EQ(0, controllerPtr->numFramesPending()); | |
457 | |
458 // Tell the client that it will fail to swap. | |
459 client.setDrawWillHappen(true); | |
460 client.setSwapWillHappenIfDrawHappens(false); | |
461 | |
462 // Get the compositor to do a scheduledActionDrawAndSwapForced. | |
463 scheduler->setNeedsRedraw(); | |
464 scheduler->setNeedsForcedRedraw(); | |
465 EXPECT_TRUE(client.hasAction("scheduledActionDrawAndSwapForced")); | |
466 | |
467 // We should not have told the frame rate controller that we began a frame. | |
468 EXPECT_EQ(0, controllerPtr->numFramesPending()); | |
469 } | |
470 | |
471 } | |
OLD | NEW |