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