OLD | NEW |
| (Empty) |
1 // Copyright 2012 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 "CCTextureUpdateController.h" | |
8 | |
9 #include "CCSchedulerTestCommon.h" | |
10 #include "CCSingleThreadProxy.h" // For DebugScopedSetImplThread | |
11 #include "CCTiledLayerTestCommon.h" | |
12 #include "FakeWebCompositorOutputSurface.h" | |
13 #include "FakeWebGraphicsContext3D.h" | |
14 #include "WebCompositorInitializer.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include <public/WebThread.h> | |
17 #include <wtf/RefPtr.h> | |
18 | |
19 using namespace cc; | |
20 using namespace WebKit; | |
21 using namespace WebKitTests; | |
22 using testing::Test; | |
23 | |
24 | |
25 namespace { | |
26 | |
27 const int kFlushPeriodFull = 4; | |
28 const int kFlushPeriodPartial = kFlushPeriodFull; | |
29 | |
30 class CCTextureUpdateControllerTest; | |
31 | |
32 class WebGraphicsContext3DForUploadTest : public FakeWebGraphicsContext3D { | |
33 public: | |
34 WebGraphicsContext3DForUploadTest(CCTextureUpdateControllerTest *test) | |
35 : m_test(test) | |
36 , m_supportShallowFlush(true) | |
37 { } | |
38 | |
39 virtual void flush(void); | |
40 virtual void shallowFlushCHROMIUM(void); | |
41 virtual GrGLInterface* onCreateGrGLInterface() { return 0; } | |
42 | |
43 virtual WebString getString(WGC3Denum name) | |
44 { | |
45 if (m_supportShallowFlush) | |
46 return WebString("GL_CHROMIUM_shallow_flush"); | |
47 return WebString(""); | |
48 } | |
49 | |
50 private: | |
51 CCTextureUpdateControllerTest* m_test; | |
52 bool m_supportShallowFlush; | |
53 }; | |
54 | |
55 | |
56 class TextureUploaderForUploadTest : public FakeTextureUploader { | |
57 public: | |
58 TextureUploaderForUploadTest(CCTextureUpdateControllerTest *test) : m_test(t
est) { } | |
59 | |
60 virtual void uploadTexture(cc::CCResourceProvider*, Parameters) OVERRIDE; | |
61 | |
62 private: | |
63 CCTextureUpdateControllerTest* m_test; | |
64 }; | |
65 | |
66 class TextureForUploadTest : public LayerTextureUpdater::Texture { | |
67 public: | |
68 TextureForUploadTest() | |
69 : LayerTextureUpdater::Texture(scoped_ptr<CCPrioritizedTexture>()) | |
70 , m_evicted(false) | |
71 { | |
72 } | |
73 virtual void updateRect(CCResourceProvider*, const IntRect& sourceRect, cons
t IntSize& destOffset) { } | |
74 virtual bool backingResourceWasEvicted() const { return m_evicted; } | |
75 void evictBackingResource() { m_evicted = true; } | |
76 private: | |
77 bool m_evicted; | |
78 }; | |
79 | |
80 | |
81 class CCTextureUpdateControllerTest : public Test { | |
82 public: | |
83 CCTextureUpdateControllerTest() | |
84 : m_queue(adoptPtr(new CCTextureUpdateQueue)) | |
85 , m_uploader(this) | |
86 , m_compositorInitializer(m_thread.get()) | |
87 , m_fullUploadCountExpected(0) | |
88 , m_partialCountExpected(0) | |
89 , m_totalUploadCountExpected(0) | |
90 , m_maxUploadCountPerUpdate(0) | |
91 , m_numConsecutiveFlushes(0) | |
92 , m_numDanglingUploads(0) | |
93 , m_numTotalUploads(0) | |
94 , m_numTotalFlushes(0) | |
95 { | |
96 } | |
97 | |
98 public: | |
99 void onFlush() | |
100 { | |
101 // Check for back-to-back flushes. | |
102 EXPECT_EQ(0, m_numConsecutiveFlushes) << "Back-to-back flushes detected.
"; | |
103 | |
104 m_numDanglingUploads = 0; | |
105 m_numConsecutiveFlushes++; | |
106 m_numTotalFlushes++; | |
107 } | |
108 | |
109 void onUpload() | |
110 { | |
111 // Check for too many consecutive uploads | |
112 if (m_numTotalUploads < m_fullUploadCountExpected) | |
113 EXPECT_LT(m_numDanglingUploads, kFlushPeriodFull) << "Too many conse
cutive full uploads detected."; | |
114 else | |
115 EXPECT_LT(m_numDanglingUploads, kFlushPeriodPartial) << "Too many co
nsecutive partial uploads detected."; | |
116 | |
117 m_numConsecutiveFlushes = 0; | |
118 m_numDanglingUploads++; | |
119 m_numTotalUploads++; | |
120 } | |
121 | |
122 protected: | |
123 virtual void SetUp() | |
124 { | |
125 m_context = FakeWebCompositorOutputSurface::create(adoptPtr(new WebGraph
icsContext3DForUploadTest(this))); | |
126 DebugScopedSetImplThread implThread; | |
127 m_resourceProvider = CCResourceProvider::create(m_context.get()); | |
128 } | |
129 | |
130 | |
131 void appendFullUploadsOfIndexedTextureToUpdateQueue(int count, int textureIn
dex) | |
132 { | |
133 m_fullUploadCountExpected += count; | |
134 m_totalUploadCountExpected += count; | |
135 | |
136 const IntRect rect(0, 0, 300, 150); | |
137 const TextureUploader::Parameters upload = { &m_textures[textureIndex],
rect, IntSize() }; | |
138 for (int i = 0; i < count; i++) | |
139 m_queue->appendFullUpload(upload); | |
140 } | |
141 | |
142 void appendFullUploadsToUpdateQueue(int count) | |
143 { | |
144 appendFullUploadsOfIndexedTextureToUpdateQueue(count, 0); | |
145 } | |
146 | |
147 void appendPartialUploadsOfIndexedTextureToUpdateQueue(int count, int textur
eIndex) | |
148 { | |
149 m_partialCountExpected += count; | |
150 m_totalUploadCountExpected += count; | |
151 | |
152 const IntRect rect(0, 0, 100, 100); | |
153 const TextureUploader::Parameters upload = { &m_textures[textureIndex],
rect, IntSize() }; | |
154 for (int i = 0; i < count; i++) | |
155 m_queue->appendPartialUpload(upload); | |
156 } | |
157 | |
158 void appendPartialUploadsToUpdateQueue(int count) | |
159 { | |
160 appendPartialUploadsOfIndexedTextureToUpdateQueue(count, 0); | |
161 } | |
162 | |
163 void setMaxUploadCountPerUpdate(int count) | |
164 { | |
165 m_maxUploadCountPerUpdate = count; | |
166 } | |
167 | |
168 void updateTextures() | |
169 { | |
170 OwnPtr<CCTextureUpdateController> updateController = | |
171 CCTextureUpdateController::create( | |
172 NULL, | |
173 CCProxy::implThread(), | |
174 m_queue.release(), | |
175 m_resourceProvider.get(), | |
176 &m_uploader); | |
177 updateController->finalize(); | |
178 } | |
179 | |
180 protected: | |
181 // Classes required to interact and test the CCTextureUpdateController | |
182 scoped_ptr<CCGraphicsContext> m_context; | |
183 OwnPtr<CCResourceProvider> m_resourceProvider; | |
184 OwnPtr<CCTextureUpdateQueue> m_queue; | |
185 TextureForUploadTest m_textures[4]; | |
186 TextureUploaderForUploadTest m_uploader; | |
187 OwnPtr<WebThread> m_thread; | |
188 WebCompositorInitializer m_compositorInitializer; | |
189 | |
190 | |
191 // Properties / expectations of this test | |
192 int m_fullUploadCountExpected; | |
193 int m_partialCountExpected; | |
194 int m_totalUploadCountExpected; | |
195 int m_maxUploadCountPerUpdate; | |
196 | |
197 // Dynamic properties of this test | |
198 int m_numConsecutiveFlushes; | |
199 int m_numDanglingUploads; | |
200 int m_numTotalUploads; | |
201 int m_numTotalFlushes; | |
202 }; | |
203 | |
204 void WebGraphicsContext3DForUploadTest::flush(void) | |
205 { | |
206 m_test->onFlush(); | |
207 } | |
208 | |
209 void WebGraphicsContext3DForUploadTest::shallowFlushCHROMIUM(void) | |
210 { | |
211 m_test->onFlush(); | |
212 } | |
213 | |
214 void TextureUploaderForUploadTest::uploadTexture(cc::CCResourceProvider*, Parame
ters) | |
215 { | |
216 m_test->onUpload(); | |
217 } | |
218 | |
219 // ZERO UPLOADS TESTS | |
220 TEST_F(CCTextureUpdateControllerTest, ZeroUploads) | |
221 { | |
222 appendFullUploadsToUpdateQueue(0); | |
223 appendPartialUploadsToUpdateQueue(0); | |
224 DebugScopedSetImplThread implThread; | |
225 updateTextures(); | |
226 | |
227 EXPECT_EQ(0, m_numTotalFlushes); | |
228 EXPECT_EQ(0, m_numTotalUploads); | |
229 } | |
230 | |
231 | |
232 // ONE UPLOAD TESTS | |
233 TEST_F(CCTextureUpdateControllerTest, OneFullUpload) | |
234 { | |
235 appendFullUploadsToUpdateQueue(1); | |
236 appendPartialUploadsToUpdateQueue(0); | |
237 DebugScopedSetImplThread implThread; | |
238 updateTextures(); | |
239 | |
240 EXPECT_EQ(1, m_numTotalFlushes); | |
241 EXPECT_EQ(1, m_numTotalUploads); | |
242 EXPECT_EQ(0, m_numDanglingUploads) << "Last upload wasn't followed by a flus
h."; | |
243 } | |
244 | |
245 TEST_F(CCTextureUpdateControllerTest, OnePartialUpload) | |
246 { | |
247 appendFullUploadsToUpdateQueue(0); | |
248 appendPartialUploadsToUpdateQueue(1); | |
249 DebugScopedSetImplThread implThread; | |
250 updateTextures(); | |
251 | |
252 EXPECT_EQ(1, m_numTotalFlushes); | |
253 EXPECT_EQ(1, m_numTotalUploads); | |
254 EXPECT_EQ(0, m_numDanglingUploads) << "Last upload wasn't followed by a flus
h."; | |
255 } | |
256 | |
257 TEST_F(CCTextureUpdateControllerTest, OneFullOnePartialUpload) | |
258 { | |
259 appendFullUploadsToUpdateQueue(1); | |
260 appendPartialUploadsToUpdateQueue(1); | |
261 DebugScopedSetImplThread implThread; | |
262 updateTextures(); | |
263 | |
264 EXPECT_EQ(1, m_numTotalFlushes); | |
265 EXPECT_EQ(2, m_numTotalUploads); | |
266 EXPECT_EQ(0, m_numDanglingUploads) << "Last upload wasn't followed by a flus
h."; | |
267 } | |
268 | |
269 | |
270 // This class of tests upload a number of textures that is a multiple of the flu
sh period. | |
271 const int fullUploadFlushMultipler = 7; | |
272 const int fullCount = fullUploadFlushMultipler * kFlushPeriodFull; | |
273 | |
274 const int partialUploadFlushMultipler = 11; | |
275 const int partialCount = partialUploadFlushMultipler * kFlushPeriodPartial; | |
276 | |
277 TEST_F(CCTextureUpdateControllerTest, ManyFullUploads) | |
278 { | |
279 appendFullUploadsToUpdateQueue(fullCount); | |
280 appendPartialUploadsToUpdateQueue(0); | |
281 DebugScopedSetImplThread implThread; | |
282 updateTextures(); | |
283 | |
284 EXPECT_EQ(fullUploadFlushMultipler, m_numTotalFlushes); | |
285 EXPECT_EQ(fullCount, m_numTotalUploads); | |
286 EXPECT_EQ(0, m_numDanglingUploads) << "Last upload wasn't followed by a flus
h."; | |
287 } | |
288 | |
289 TEST_F(CCTextureUpdateControllerTest, ManyPartialUploads) | |
290 { | |
291 appendFullUploadsToUpdateQueue(0); | |
292 appendPartialUploadsToUpdateQueue(partialCount); | |
293 DebugScopedSetImplThread implThread; | |
294 updateTextures(); | |
295 | |
296 EXPECT_EQ(partialUploadFlushMultipler, m_numTotalFlushes); | |
297 EXPECT_EQ(partialCount, m_numTotalUploads); | |
298 EXPECT_EQ(0, m_numDanglingUploads) << "Last upload wasn't followed by a flus
h."; | |
299 } | |
300 | |
301 TEST_F(CCTextureUpdateControllerTest, ManyFullManyPartialUploads) | |
302 { | |
303 appendFullUploadsToUpdateQueue(fullCount); | |
304 appendPartialUploadsToUpdateQueue(partialCount); | |
305 DebugScopedSetImplThread implThread; | |
306 updateTextures(); | |
307 | |
308 EXPECT_EQ(fullUploadFlushMultipler + partialUploadFlushMultipler, m_numTotal
Flushes); | |
309 EXPECT_EQ(fullCount + partialCount, m_numTotalUploads); | |
310 EXPECT_EQ(0, m_numDanglingUploads) << "Last upload wasn't followed by a flus
h."; | |
311 } | |
312 | |
313 class FakeCCTextureUpdateControllerClient : public cc::CCTextureUpdateController
Client { | |
314 public: | |
315 FakeCCTextureUpdateControllerClient() { reset(); } | |
316 void reset() { m_readyToFinalizeCalled = false; } | |
317 bool readyToFinalizeCalled() const { return m_readyToFinalizeCalled; } | |
318 | |
319 virtual void readyToFinalizeTextureUpdates() OVERRIDE { m_readyToFinalizeCal
led = true; } | |
320 | |
321 protected: | |
322 bool m_readyToFinalizeCalled; | |
323 }; | |
324 | |
325 class FakeCCTextureUpdateController : public cc::CCTextureUpdateController { | |
326 public: | |
327 static PassOwnPtr<FakeCCTextureUpdateController> create(cc::CCTextureUpdateC
ontrollerClient* client, cc::CCThread* thread, PassOwnPtr<CCTextureUpdateQueue>
queue, CCResourceProvider* resourceProvider, TextureUploader* uploader) | |
328 { | |
329 return adoptPtr(new FakeCCTextureUpdateController(client, thread, queue,
resourceProvider, uploader)); | |
330 } | |
331 | |
332 void setNow(base::TimeTicks time) { m_now = time; } | |
333 virtual base::TimeTicks now() const OVERRIDE { return m_now; } | |
334 void setUpdateMoreTexturesTime(base::TimeDelta time) { m_updateMoreTexturesT
ime = time; } | |
335 virtual base::TimeDelta updateMoreTexturesTime() const OVERRIDE { return m_u
pdateMoreTexturesTime; } | |
336 void setUpdateMoreTexturesSize(size_t size) { m_updateMoreTexturesSize = siz
e; } | |
337 virtual size_t updateMoreTexturesSize() const OVERRIDE { return m_updateMore
TexturesSize; } | |
338 | |
339 protected: | |
340 FakeCCTextureUpdateController(cc::CCTextureUpdateControllerClient* client, c
c::CCThread* thread, PassOwnPtr<CCTextureUpdateQueue> queue, CCResourceProvider*
resourceProvider, TextureUploader* uploader) | |
341 : cc::CCTextureUpdateController(client, thread, queue, resourceProvider,
uploader) | |
342 , m_updateMoreTexturesSize(0) { } | |
343 | |
344 base::TimeTicks m_now; | |
345 base::TimeDelta m_updateMoreTexturesTime; | |
346 size_t m_updateMoreTexturesSize; | |
347 }; | |
348 | |
349 static void runPendingTask(FakeCCThread* thread, FakeCCTextureUpdateController*
controller) | |
350 { | |
351 EXPECT_TRUE(thread->hasPendingTask()); | |
352 controller->setNow(controller->now() + base::TimeDelta::FromMilliseconds(thr
ead->pendingDelayMs())); | |
353 thread->runPendingTask(); | |
354 } | |
355 | |
356 TEST_F(CCTextureUpdateControllerTest, UpdateMoreTextures) | |
357 { | |
358 FakeCCTextureUpdateControllerClient client; | |
359 FakeCCThread thread; | |
360 | |
361 setMaxUploadCountPerUpdate(1); | |
362 appendFullUploadsToUpdateQueue(3); | |
363 appendPartialUploadsToUpdateQueue(0); | |
364 | |
365 DebugScopedSetImplThread implThread; | |
366 OwnPtr<FakeCCTextureUpdateController> controller(FakeCCTextureUpdateControll
er::create(&client, &thread, m_queue.release(), m_resourceProvider.get(), &m_upl
oader)); | |
367 | |
368 controller->setNow( | |
369 controller->now() + base::TimeDelta::FromMilliseconds(1)); | |
370 controller->setUpdateMoreTexturesTime( | |
371 base::TimeDelta::FromMilliseconds(100)); | |
372 controller->setUpdateMoreTexturesSize(1); | |
373 // Not enough time for any updates. | |
374 controller->performMoreUpdates( | |
375 controller->now() + base::TimeDelta::FromMilliseconds(90)); | |
376 EXPECT_FALSE(thread.hasPendingTask()); | |
377 | |
378 controller->setUpdateMoreTexturesTime( | |
379 base::TimeDelta::FromMilliseconds(100)); | |
380 controller->setUpdateMoreTexturesSize(1); | |
381 // Only enough time for 1 update. | |
382 controller->performMoreUpdates( | |
383 controller->now() + base::TimeDelta::FromMilliseconds(120)); | |
384 runPendingTask(&thread, controller.get()); | |
385 EXPECT_FALSE(thread.hasPendingTask()); | |
386 EXPECT_EQ(1, m_numTotalUploads); | |
387 | |
388 controller->setUpdateMoreTexturesTime( | |
389 base::TimeDelta::FromMilliseconds(100)); | |
390 controller->setUpdateMoreTexturesSize(1); | |
391 // Enough time for 2 updates. | |
392 controller->performMoreUpdates( | |
393 controller->now() + base::TimeDelta::FromMilliseconds(220)); | |
394 runPendingTask(&thread, controller.get()); | |
395 runPendingTask(&thread, controller.get()); | |
396 EXPECT_FALSE(thread.hasPendingTask()); | |
397 EXPECT_TRUE(client.readyToFinalizeCalled()); | |
398 EXPECT_EQ(3, m_numTotalUploads); | |
399 } | |
400 | |
401 TEST_F(CCTextureUpdateControllerTest, NoMoreUpdates) | |
402 { | |
403 FakeCCTextureUpdateControllerClient client; | |
404 FakeCCThread thread; | |
405 | |
406 setMaxUploadCountPerUpdate(1); | |
407 appendFullUploadsToUpdateQueue(2); | |
408 appendPartialUploadsToUpdateQueue(0); | |
409 | |
410 DebugScopedSetImplThread implThread; | |
411 OwnPtr<FakeCCTextureUpdateController> controller(FakeCCTextureUpdateControll
er::create(&client, &thread, m_queue.release(), m_resourceProvider.get(), &m_upl
oader)); | |
412 | |
413 controller->setNow( | |
414 controller->now() + base::TimeDelta::FromMilliseconds(1)); | |
415 controller->setUpdateMoreTexturesTime( | |
416 base::TimeDelta::FromMilliseconds(100)); | |
417 controller->setUpdateMoreTexturesSize(1); | |
418 // Enough time for 3 updates but only 2 necessary. | |
419 controller->performMoreUpdates( | |
420 controller->now() + base::TimeDelta::FromMilliseconds(310)); | |
421 runPendingTask(&thread, controller.get()); | |
422 runPendingTask(&thread, controller.get()); | |
423 EXPECT_FALSE(thread.hasPendingTask()); | |
424 EXPECT_TRUE(client.readyToFinalizeCalled()); | |
425 EXPECT_EQ(2, m_numTotalUploads); | |
426 | |
427 controller->setUpdateMoreTexturesTime( | |
428 base::TimeDelta::FromMilliseconds(100)); | |
429 controller->setUpdateMoreTexturesSize(1); | |
430 // Enough time for updates but no more updates left. | |
431 controller->performMoreUpdates( | |
432 controller->now() + base::TimeDelta::FromMilliseconds(310)); | |
433 // 0-delay task used to call readyToFinalizeTextureUpdates(). | |
434 runPendingTask(&thread, controller.get()); | |
435 EXPECT_FALSE(thread.hasPendingTask()); | |
436 EXPECT_TRUE(client.readyToFinalizeCalled()); | |
437 EXPECT_EQ(2, m_numTotalUploads); | |
438 } | |
439 | |
440 TEST_F(CCTextureUpdateControllerTest, UpdatesCompleteInFiniteTime) | |
441 { | |
442 FakeCCTextureUpdateControllerClient client; | |
443 FakeCCThread thread; | |
444 | |
445 setMaxUploadCountPerUpdate(1); | |
446 appendFullUploadsToUpdateQueue(2); | |
447 appendPartialUploadsToUpdateQueue(0); | |
448 | |
449 DebugScopedSetImplThread implThread; | |
450 OwnPtr<FakeCCTextureUpdateController> controller(FakeCCTextureUpdateControll
er::create(&client, &thread, m_queue.release(), m_resourceProvider.get(), &m_upl
oader)); | |
451 | |
452 controller->setNow( | |
453 controller->now() + base::TimeDelta::FromMilliseconds(1)); | |
454 controller->setUpdateMoreTexturesTime( | |
455 base::TimeDelta::FromMilliseconds(500)); | |
456 controller->setUpdateMoreTexturesSize(1); | |
457 | |
458 for (int i = 0; i < 100; i++) { | |
459 if (client.readyToFinalizeCalled()) | |
460 break; | |
461 | |
462 // Not enough time for any updates. | |
463 controller->performMoreUpdates( | |
464 controller->now() + base::TimeDelta::FromMilliseconds(400)); | |
465 | |
466 if (thread.hasPendingTask()) | |
467 runPendingTask(&thread, controller.get()); | |
468 } | |
469 | |
470 EXPECT_FALSE(thread.hasPendingTask()); | |
471 EXPECT_TRUE(client.readyToFinalizeCalled()); | |
472 EXPECT_EQ(2, m_numTotalUploads); | |
473 } | |
474 | |
475 TEST_F(CCTextureUpdateControllerTest, ClearUploadsToEvictedResources) | |
476 { | |
477 appendFullUploadsOfIndexedTextureToUpdateQueue(1, 0); | |
478 appendPartialUploadsOfIndexedTextureToUpdateQueue(1, 1); | |
479 appendFullUploadsOfIndexedTextureToUpdateQueue(1, 2); | |
480 appendPartialUploadsOfIndexedTextureToUpdateQueue(1, 3); | |
481 DebugScopedSetImplThread implThread; | |
482 | |
483 m_queue->clearUploadsToEvictedResources(); | |
484 EXPECT_EQ(2u, m_queue->fullUploadSize()); | |
485 EXPECT_EQ(2u, m_queue->partialUploadSize()); | |
486 | |
487 m_textures[0].evictBackingResource(); | |
488 m_queue->clearUploadsToEvictedResources(); | |
489 EXPECT_EQ(1u, m_queue->fullUploadSize()); | |
490 EXPECT_EQ(2u, m_queue->partialUploadSize()); | |
491 | |
492 m_textures[3].evictBackingResource(); | |
493 m_queue->clearUploadsToEvictedResources(); | |
494 EXPECT_EQ(1u, m_queue->fullUploadSize()); | |
495 EXPECT_EQ(1u, m_queue->partialUploadSize()); | |
496 | |
497 m_textures[2].evictBackingResource(); | |
498 m_queue->clearUploadsToEvictedResources(); | |
499 EXPECT_EQ(0u, m_queue->fullUploadSize()); | |
500 EXPECT_EQ(1u, m_queue->partialUploadSize()); | |
501 | |
502 m_textures[1].evictBackingResource(); | |
503 m_queue->clearUploadsToEvictedResources(); | |
504 EXPECT_EQ(0u, m_queue->fullUploadSize()); | |
505 EXPECT_EQ(0u, m_queue->partialUploadSize()); | |
506 } | |
507 | |
508 } // namespace | |
OLD | NEW |