Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: cc/test/layer_tree_test_common.cc

Issue 11571068: Use WeakPtr for posting cc tasks to main thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use WeakPtr for tasks posted to main thread in ThreadedTests Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « cc/test/layer_tree_test_common.h ('k') | cc/thread_proxy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/test/layer_tree_test_common.h" 5 #include "cc/test/layer_tree_test_common.h"
6 6
7 #include "cc/active_animation.h" 7 #include "cc/active_animation.h"
8 #include "cc/content_layer.h" 8 #include "cc/content_layer.h"
9 #include "cc/font_atlas.h" 9 #include "cc/font_atlas.h"
10 #include "cc/input_handler.h" 10 #include "cc/input_handler.h"
11 #include "cc/layer.h" 11 #include "cc/layer.h"
12 #include "cc/layer_animation_controller.h" 12 #include "cc/layer_animation_controller.h"
13 #include "cc/layer_impl.h" 13 #include "cc/layer_impl.h"
14 #include "cc/layer_tree_host_impl.h" 14 #include "cc/layer_tree_host_impl.h"
15 #include "cc/scoped_thread_proxy.h"
16 #include "cc/single_thread_proxy.h" 15 #include "cc/single_thread_proxy.h"
17 #include "cc/thread_impl.h" 16 #include "cc/thread_impl.h"
18 #include "cc/test/animation_test_common.h" 17 #include "cc/test/animation_test_common.h"
19 #include "cc/test/fake_output_surface.h" 18 #include "cc/test/fake_output_surface.h"
20 #include "cc/test/occlusion_tracker_test_common.h" 19 #include "cc/test/occlusion_tracker_test_common.h"
21 #include "cc/test/tiled_layer_test_common.h" 20 #include "cc/test/tiled_layer_test_common.h"
22 #include "cc/timing_function.h" 21 #include "cc/timing_function.h"
23 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
24 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperation. h" 23 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperation. h"
25 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations .h" 24 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations .h"
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 TestHooks* m_testHooks; 214 TestHooks* m_testHooks;
216 }; 215 };
217 216
218 ThreadedTest::ThreadedTest() 217 ThreadedTest::ThreadedTest()
219 : m_beginning(false) 218 : m_beginning(false)
220 , m_endWhenBeginReturns(false) 219 , m_endWhenBeginReturns(false)
221 , m_timedOut(false) 220 , m_timedOut(false)
222 , m_scheduled(false) 221 , m_scheduled(false)
223 , m_started(false) 222 , m_started(false)
224 , m_implThread(0) 223 , m_implThread(0)
224 , m_weakFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this))
225 { 225 {
226 m_mainThreadWeakPtr = m_weakFactory.GetWeakPtr();
226 } 227 }
227 228
228 ThreadedTest::~ThreadedTest() 229 ThreadedTest::~ThreadedTest()
229 { 230 {
230 } 231 }
231 232
232 void ThreadedTest::endTest() 233 void ThreadedTest::endTest()
233 { 234 {
234 // For the case where we endTest during beginTest(), set a flag to indicate that 235 // For the case where we endTest during beginTest(), set a flag to indicate that
235 // the test should end the second beginTest regains control. 236 // the test should end the second beginTest regains control.
236 if (m_beginning) 237 if (m_beginning)
237 m_endWhenBeginReturns = true; 238 m_endWhenBeginReturns = true;
238 else 239 else
239 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::realEnd Test, base::Unretained(this))); 240 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::realEndTest, m _mainThreadWeakPtr));
240 } 241 }
241 242
242 void ThreadedTest::endTestAfterDelay(int delayMilliseconds) 243 void ThreadedTest::endTestAfterDelay(int delayMilliseconds)
243 { 244 {
244 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::endTest, ba se::Unretained(this))); 245 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::endTest, m_mainThr eadWeakPtr));
245 }
246
247 void ThreadedTest::postSetNeedsAnimateToMainThread()
248 {
249 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchSet NeedsAnimate, base::Unretained(this)));
250 } 246 }
251 247
252 void ThreadedTest::postAddAnimationToMainThread(Layer* layerToReceiveAnimation) 248 void ThreadedTest::postAddAnimationToMainThread(Layer* layerToReceiveAnimation)
253 { 249 {
254 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchAdd Animation, base::Unretained(this), base::Unretained(layerToReceiveAnimation))); 250 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchAddAnimati on, m_mainThreadWeakPtr, base::Unretained(layerToReceiveAnimation)));
255 } 251 }
256 252
257 void ThreadedTest::postAddInstantAnimationToMainThread() 253 void ThreadedTest::postAddInstantAnimationToMainThread()
258 { 254 {
259 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchAdd InstantAnimation, base::Unretained(this))); 255 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchAddInstant Animation, m_mainThreadWeakPtr));
260 } 256 }
261 257
262 void ThreadedTest::postSetNeedsCommitToMainThread() 258 void ThreadedTest::postSetNeedsCommitToMainThread()
263 { 259 {
264 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchSet NeedsCommit, base::Unretained(this))); 260 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchSetNeedsCo mmit, m_mainThreadWeakPtr));
265 } 261 }
266 262
267 void ThreadedTest::postAcquireLayerTextures() 263 void ThreadedTest::postAcquireLayerTextures()
268 { 264 {
269 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchAcq uireLayerTextures, base::Unretained(this))); 265 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchAcquireLay erTextures, m_mainThreadWeakPtr));
270 } 266 }
271 267
272 void ThreadedTest::postSetNeedsRedrawToMainThread() 268 void ThreadedTest::postSetNeedsRedrawToMainThread()
273 { 269 {
274 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchSet NeedsRedraw, base::Unretained(this))); 270 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchSetNeedsRe draw, m_mainThreadWeakPtr));
275 } 271 }
276 272
277 void ThreadedTest::postSetVisibleToMainThread(bool visible) 273 void ThreadedTest::postSetVisibleToMainThread(bool visible)
278 { 274 {
279 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchSet Visible, base::Unretained(this), visible)); 275 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchSetVisible , m_mainThreadWeakPtr, visible));
280 } 276 }
281 277
282 void ThreadedTest::postDidAddAnimationToMainThread() 278 void ThreadedTest::postDidAddAnimationToMainThread()
283 { 279 {
284 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchDid AddAnimation, base::Unretained(this))); 280 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchDidAddAnim ation, m_mainThreadWeakPtr));
285 } 281 }
286 282
287 void ThreadedTest::doBeginTest() 283 void ThreadedTest::doBeginTest()
288 { 284 {
289 m_client = ThreadedMockLayerTreeHostClient::create(this); 285 m_client = ThreadedMockLayerTreeHostClient::create(this);
290 286
291 scoped_ptr<cc::Thread> implCCThread(NULL); 287 scoped_ptr<cc::Thread> implCCThread(NULL);
292 if (m_implThread) 288 if (m_implThread)
293 implCCThread = cc::ThreadImpl::createForDifferentThread(m_implThread->me ssage_loop_proxy()); 289 implCCThread = cc::ThreadImpl::createForDifferentThread(m_implThread->me ssage_loop_proxy());
294 m_layerTreeHost = MockLayerTreeHost::create(this, m_client.get(), m_settings , implCCThread.Pass()); 290 m_layerTreeHost = MockLayerTreeHost::create(this, m_client.get(), m_settings , implCCThread.Pass());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 { 324 {
329 m_timedOut = true; 325 m_timedOut = true;
330 endTest(); 326 endTest();
331 } 327 }
332 328
333 void ThreadedTest::scheduleComposite() 329 void ThreadedTest::scheduleComposite()
334 { 330 {
335 if (!m_started || m_scheduled) 331 if (!m_started || m_scheduled)
336 return; 332 return;
337 m_scheduled = true; 333 m_scheduled = true;
338 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::dispatchCom posite, base::Unretained(this))); 334 proxy()->mainThread()->postTask(base::Bind(&ThreadedTest::dispatchComposite, m_mainThreadWeakPtr));
339 } 335 }
340 336
341 void ThreadedTest::realEndTest() 337 void ThreadedTest::realEndTest()
342 { 338 {
343 if (m_layerTreeHost && m_layerTreeHost->proxy()->commitPendingForTesting()) { 339 if (m_layerTreeHost && m_layerTreeHost->proxy()->commitPendingForTesting()) {
344 m_mainThreadProxy->postTask(FROM_HERE, base::Bind(&ThreadedTest::realEnd Test, base::Unretained(this))); 340 m_layerTreeHost->proxy()->mainThread()->postTask(base::Bind(&ThreadedTes t::realEndTest, m_mainThreadWeakPtr));
danakj 2012/12/20 02:35:32 nit: proxy() directly like the others?
345 return; 341 return;
346 } 342 }
347 343
348 MessageLoop::current()->Quit(); 344 MessageLoop::current()->Quit();
349 } 345 }
350 346
351 void ThreadedTest::dispatchSetNeedsAnimate()
352 {
353 DCHECK(!proxy() || proxy()->isMainThread());
354
355 if (m_layerTreeHost.get())
356 m_layerTreeHost->setNeedsAnimate();
357 }
358
359 void ThreadedTest::dispatchAddInstantAnimation() 347 void ThreadedTest::dispatchAddInstantAnimation()
360 { 348 {
361 DCHECK(!proxy() || proxy()->isMainThread()); 349 DCHECK(!proxy() || proxy()->isMainThread());
362 350
363 if (m_layerTreeHost.get() && m_layerTreeHost->rootLayer()) 351 if (m_layerTreeHost.get() && m_layerTreeHost->rootLayer())
364 addOpacityTransitionToLayer(*m_layerTreeHost->rootLayer(), 0, 0, 0.5, fa lse); 352 addOpacityTransitionToLayer(*m_layerTreeHost->rootLayer(), 0, 0, 0.5, fa lse);
365 } 353 }
366 354
367 void ThreadedTest::dispatchAddAnimation(Layer* layerToReceiveAnimation) 355 void ThreadedTest::dispatchAddAnimation(Layer* layerToReceiveAnimation)
368 { 356 {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 } 408 }
421 409
422 void ThreadedTest::runTest(bool threaded) 410 void ThreadedTest::runTest(bool threaded)
423 { 411 {
424 if (threaded) { 412 if (threaded) {
425 m_implThread.reset(new base::Thread("ThreadedTest")); 413 m_implThread.reset(new base::Thread("ThreadedTest"));
426 ASSERT_TRUE(m_implThread->Start()); 414 ASSERT_TRUE(m_implThread->Start());
427 } 415 }
428 416
429 m_mainCCThread = cc::ThreadImpl::createForCurrentThread(); 417 m_mainCCThread = cc::ThreadImpl::createForCurrentThread();
430 m_mainThreadProxy = ScopedThreadProxy::create(m_mainCCThread.get());
431 418
432 initializeSettings(m_settings); 419 initializeSettings(m_settings);
433 420
434 m_mainCCThread->postTask(base::Bind(&ThreadedTest::doBeginTest, base::Unreta ined(this))); 421 m_mainCCThread->postTask(base::Bind(&ThreadedTest::doBeginTest, base::Unreta ined(this)));
435 m_timeout.Reset(base::Bind(&ThreadedTest::timeout, base::Unretained(this))); 422 m_timeout.Reset(base::Bind(&ThreadedTest::timeout, base::Unretained(this)));
436 m_mainCCThread->postDelayedTask(m_timeout.callback(), 5000); 423 m_mainCCThread->postDelayedTask(m_timeout.callback(), 5000);
437 MessageLoop::current()->Run(); 424 MessageLoop::current()->Run();
438 if (m_layerTreeHost.get() && m_layerTreeHost->rootLayer()) 425 if (m_layerTreeHost.get() && m_layerTreeHost->rootLayer())
439 m_layerTreeHost->rootLayer()->setLayerTreeHost(0); 426 m_layerTreeHost->rootLayer()->setLayerTreeHost(0);
440 m_layerTreeHost.reset(); 427 m_layerTreeHost.reset();
441 428
442 m_timeout.Cancel(); 429 m_timeout.Cancel();
443 430
444 ASSERT_FALSE(m_layerTreeHost.get()); 431 ASSERT_FALSE(m_layerTreeHost.get());
445 m_client.reset(); 432 m_client.reset();
446 if (m_timedOut) { 433 if (m_timedOut) {
447 FAIL() << "Test timed out"; 434 FAIL() << "Test timed out";
448 return; 435 return;
449 } 436 }
450 afterTest(); 437 afterTest();
451 } 438 }
452 439
453 } // namespace cc 440 } // namespace cc
OLDNEW
« no previous file with comments | « cc/test/layer_tree_test_common.h ('k') | cc/thread_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698