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

Side by Side Diff: third_party/WebKit/Source/platform/graphics/Canvas2DLayerBridgeTest.cpp

Issue 1520123002: Fix state restoration logic in canvas 2d wakeup from hibernation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */ 23 */
24 24
25 #include "config.h" 25 #include "config.h"
26 #include "platform/graphics/Canvas2DLayerBridge.h" 26 #include "platform/graphics/Canvas2DLayerBridge.h"
27 27
28 #include "SkSurface.h" 28 #include "SkSurface.h"
29 #include "platform/graphics/ImageBuffer.h" 29 #include "platform/graphics/ImageBuffer.h"
30 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
30 #include "platform/graphics/test/MockWebGraphicsContext3D.h" 31 #include "platform/graphics/test/MockWebGraphicsContext3D.h"
31 #include "public/platform/Platform.h" 32 #include "public/platform/Platform.h"
32 #include "public/platform/WebExternalBitmap.h" 33 #include "public/platform/WebExternalBitmap.h"
33 #include "public/platform/WebGraphicsContext3DProvider.h" 34 #include "public/platform/WebGraphicsContext3DProvider.h"
34 #include "public/platform/WebScheduler.h" 35 #include "public/platform/WebScheduler.h"
35 #include "public/platform/WebTaskRunner.h" 36 #include "public/platform/WebTaskRunner.h"
36 #include "public/platform/WebThread.h" 37 #include "public/platform/WebThread.h"
37 #include "public/platform/WebTraceLocation.h" 38 #include "public/platform/WebTraceLocation.h"
38 #include "testing/gmock/include/gmock/gmock.h" 39 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h" 40 #include "testing/gtest/include/gtest/gtest.h"
40 #include "third_party/skia/include/core/SkCanvas.h" 41 #include "third_party/skia/include/core/SkCanvas.h"
41 #include "third_party/skia/include/gpu/GrContext.h" 42 #include "third_party/skia/include/gpu/GrContext.h"
42 #include "third_party/skia/include/gpu/gl/SkNullGLContext.h" 43 #include "third_party/skia/include/gpu/gl/SkNullGLContext.h"
43 #include "wtf/RefPtr.h" 44 #include "wtf/RefPtr.h"
44 45
46 using testing::AnyNumber;
47 using testing::AtLeast;
45 using testing::InSequence; 48 using testing::InSequence;
46 using testing::Return; 49 using testing::Return;
47 using testing::Test; 50 using testing::Test;
51 using testing::_;
48 52
49 namespace blink { 53 namespace blink {
50 54
51 namespace { 55 namespace {
52 56
53 class MockCanvasContext : public MockWebGraphicsContext3D { 57 class MockCanvasContext : public MockWebGraphicsContext3D {
54 public: 58 public:
55 MOCK_METHOD0(flush, void(void)); 59 MOCK_METHOD0(flush, void(void));
56 MOCK_METHOD0(createTexture, unsigned(void)); 60 MOCK_METHOD0(createTexture, unsigned(void));
57 MOCK_METHOD1(deleteTexture, void(unsigned)); 61 MOCK_METHOD1(deleteTexture, void(unsigned));
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 if (m_doneEvent) 438 if (m_doneEvent)
435 m_doneEvent->signal(); 439 m_doneEvent->signal();
436 } 440 }
437 441
438 private: 442 private:
439 Canvas2DLayerBridge* m_bridge; 443 Canvas2DLayerBridge* m_bridge;
440 bool m_value; 444 bool m_value;
441 WebWaitableEvent* m_doneEvent; 445 WebWaitableEvent* m_doneEvent;
442 }; 446 };
443 447
448 class MockImageBuffer : public ImageBuffer {
449 public:
450 MockImageBuffer()
451 : ImageBuffer(adoptPtr(new UnacceleratedImageBufferSurface(IntSize(1, 1) ))) { }
452
453 MOCK_CONST_METHOD1(resetCanvas, void(SkCanvas*));
454
455 virtual ~MockImageBuffer() { }
456 };
457
444 TEST_F(Canvas2DLayerBridgeTest, HibernationLifeCycle) 458 TEST_F(Canvas2DLayerBridgeTest, HibernationLifeCycle)
445 { 459 {
446 MockCanvasContext mainMock; 460 MockCanvasContext mainMock;
447 OwnPtr<WebThread> testThread = adoptPtr(Platform::current()->createThread("T estThread")); 461 OwnPtr<WebThread> testThread = adoptPtr(Platform::current()->createThread("T estThread"));
448 462
449 // The Canvas2DLayerBridge has to be created on the thread that will use it 463 // The Canvas2DLayerBridge has to be created on the thread that will use it
450 // to avoid WeakPtr thread check issues. 464 // to avoid WeakPtr thread check issues.
451 Canvas2DLayerBridgePtr bridge; 465 Canvas2DLayerBridgePtr bridge;
452 OwnPtr<WebWaitableEvent> bridgeCreatedEvent = adoptPtr(Platform::current()-> createWaitableEvent()); 466 OwnPtr<WebWaitableEvent> bridgeCreatedEvent = adoptPtr(Platform::current()-> createWaitableEvent());
453 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new CreateBridgeTask(&br idge, &mainMock, this, bridgeCreatedEvent.get())); 467 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new CreateBridgeTask(&br idge, &mainMock, this, bridgeCreatedEvent.get()));
(...skipping 26 matching lines...) Expand all
480 494
481 // Tear down the bridge on the thread so that 'bridge' can go out of scope 495 // Tear down the bridge on the thread so that 'bridge' can go out of scope
482 // without crashing due to thread checks 496 // without crashing due to thread checks
483 OwnPtr<WebWaitableEvent> bridgeDestroyedEvent = adoptPtr(Platform::current() ->createWaitableEvent()); 497 OwnPtr<WebWaitableEvent> bridgeDestroyedEvent = adoptPtr(Platform::current() ->createWaitableEvent());
484 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new DestroyBridgeTask(&b ridge, bridgeDestroyedEvent.get())); 498 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new DestroyBridgeTask(&b ridge, bridgeDestroyedEvent.get()));
485 bridgeDestroyedEvent->wait(); 499 bridgeDestroyedEvent->wait();
486 500
487 ::testing::Mock::VerifyAndClearExpectations(&mainMock); 501 ::testing::Mock::VerifyAndClearExpectations(&mainMock);
488 } 502 }
489 503
504 TEST_F(Canvas2DLayerBridgeTest, HibernationLifeCycleWithDeferredRenderingDisable d)
505 {
506 MockCanvasContext mainMock;
507 OwnPtr<WebThread> testThread = adoptPtr(Platform::current()->createThread("T estThread"));
508
509 // The Canvas2DLayerBridge has to be created on the thread that will use it
510 // to avoid WeakPtr thread check issues.
511 Canvas2DLayerBridgePtr bridge;
512 OwnPtr<WebWaitableEvent> bridgeCreatedEvent = adoptPtr(Platform::current()-> createWaitableEvent());
513 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new CreateBridgeTask(&br idge, &mainMock, this, bridgeCreatedEvent.get()));
514 bridgeCreatedEvent->wait();
515 bridge->disableDeferral();
516 MockImageBuffer mockImageBuffer;
517 EXPECT_CALL(mockImageBuffer, resetCanvas(_)).Times(AnyNumber());
518 bridge->setImageBuffer(&mockImageBuffer);
519
520 // Register an alternate Logger for tracking hibernation events
521 OwnPtr<MockLogger> mockLogger = adoptPtr(new MockLogger);
522 MockLogger* mockLoggerPtr = mockLogger.get();
523 bridge->setLoggerForTesting(mockLogger.release());
524
525 // Test entering hibernation
526 OwnPtr<WebWaitableEvent> hibernationStartedEvent = adoptPtr(Platform::curren t()->createWaitableEvent());
527 EXPECT_CALL(*mockLoggerPtr, reportHibernationEvent(Canvas2DLayerBridge::Hibe rnationScheduled));
528 EXPECT_CALL(*mockLoggerPtr, didStartHibernating())
529 .WillOnce(testing::Invoke(hibernationStartedEvent.get(), &WebWaitableEve nt::signal));
530 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new SetIsHiddenTask(brid ge.get(), true));
531 hibernationStartedEvent->wait();
532 ::testing::Mock::VerifyAndClearExpectations(mockLoggerPtr);
533 ::testing::Mock::VerifyAndClearExpectations(&mockImageBuffer);
534 EXPECT_FALSE(bridge->isAccelerated());
535 EXPECT_TRUE(bridge->isHibernating());
536
537 // Test exiting hibernation
538 OwnPtr<WebWaitableEvent> hibernationEndedEvent = adoptPtr(Platform::current( )->createWaitableEvent());
539 EXPECT_CALL(*mockLoggerPtr, reportHibernationEvent(Canvas2DLayerBridge::Hibe rnationEndedNormally));
540 EXPECT_CALL(mockImageBuffer, resetCanvas(_)).Times(AtLeast(1)); // Because d eferred rendering is disabled
541 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new SetIsHiddenTask(brid ge.get(), false, hibernationEndedEvent.get()));
542 hibernationEndedEvent->wait();
543 ::testing::Mock::VerifyAndClearExpectations(mockLoggerPtr);
544 ::testing::Mock::VerifyAndClearExpectations(&mockImageBuffer);
545 EXPECT_TRUE(bridge->isAccelerated());
546 EXPECT_FALSE(bridge->isHibernating());
547
548 // Tear down the bridge on the thread so that 'bridge' can go out of scope
549 // without crashing due to thread checks
550 OwnPtr<WebWaitableEvent> bridgeDestroyedEvent = adoptPtr(Platform::current() ->createWaitableEvent());
551 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new DestroyBridgeTask(&b ridge, bridgeDestroyedEvent.get()));
552 bridgeDestroyedEvent->wait();
553
554 ::testing::Mock::VerifyAndClearExpectations(&mainMock);
555 }
556
490 class RenderingTask : public WebTaskRunner::Task { 557 class RenderingTask : public WebTaskRunner::Task {
491 public: 558 public:
492 RenderingTask(Canvas2DLayerBridge* bridge, WebWaitableEvent* doneEvent) 559 RenderingTask(Canvas2DLayerBridge* bridge, WebWaitableEvent* doneEvent)
493 : m_bridge(bridge) 560 : m_bridge(bridge)
494 , m_doneEvent(doneEvent) 561 , m_doneEvent(doneEvent)
495 { } 562 { }
496 563
497 virtual ~RenderingTask() { } 564 virtual ~RenderingTask() { }
498 565
499 void run() override 566 void run() override
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 623
557 // Tear down the bridge on the thread so that 'bridge' can go out of scope 624 // Tear down the bridge on the thread so that 'bridge' can go out of scope
558 // without crashing due to thread checks 625 // without crashing due to thread checks
559 OwnPtr<WebWaitableEvent> bridgeDestroyedEvent = adoptPtr(Platform::current() ->createWaitableEvent()); 626 OwnPtr<WebWaitableEvent> bridgeDestroyedEvent = adoptPtr(Platform::current() ->createWaitableEvent());
560 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new DestroyBridgeTask(&b ridge, bridgeDestroyedEvent.get())); 627 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new DestroyBridgeTask(&b ridge, bridgeDestroyedEvent.get()));
561 bridgeDestroyedEvent->wait(); 628 bridgeDestroyedEvent->wait();
562 629
563 ::testing::Mock::VerifyAndClearExpectations(&mainMock); 630 ::testing::Mock::VerifyAndClearExpectations(&mainMock);
564 } 631 }
565 632
633 TEST_F(Canvas2DLayerBridgeTest, BackgroundRenderingWhileHibernatingWithDeferredR enderingDisabled)
634 {
635 MockCanvasContext mainMock;
636 OwnPtr<WebThread> testThread = adoptPtr(Platform::current()->createThread("T estThread"));
637
638 // The Canvas2DLayerBridge has to be created on the thread that will use it
639 // to avoid WeakPtr thread check issues.
640 Canvas2DLayerBridgePtr bridge;
641 OwnPtr<WebWaitableEvent> bridgeCreatedEvent = adoptPtr(Platform::current()-> createWaitableEvent());
642 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new CreateBridgeTask(&br idge, &mainMock, this, bridgeCreatedEvent.get()));
643 bridgeCreatedEvent->wait();
644 MockImageBuffer mockImageBuffer;
645 EXPECT_CALL(mockImageBuffer, resetCanvas(_)).Times(AnyNumber());
646 bridge->setImageBuffer(&mockImageBuffer);
647 bridge->disableDeferral();
648
649
650 // Register an alternate Logger for tracking hibernation events
651 OwnPtr<MockLogger> mockLogger = adoptPtr(new MockLogger);
652 MockLogger* mockLoggerPtr = mockLogger.get();
653 bridge->setLoggerForTesting(mockLogger.release());
654
655 // Test entering hibernation
656 OwnPtr<WebWaitableEvent> hibernationStartedEvent = adoptPtr(Platform::curren t()->createWaitableEvent());
657 EXPECT_CALL(*mockLoggerPtr, reportHibernationEvent(Canvas2DLayerBridge::Hibe rnationScheduled));
658 EXPECT_CALL(*mockLoggerPtr, didStartHibernating())
659 .WillOnce(testing::Invoke(hibernationStartedEvent.get(), &WebWaitableEve nt::signal));
660 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new SetIsHiddenTask(brid ge.get(), true));
661 hibernationStartedEvent->wait();
662 ::testing::Mock::VerifyAndClearExpectations(mockLoggerPtr);
663 ::testing::Mock::VerifyAndClearExpectations(&mockImageBuffer);
664 EXPECT_FALSE(bridge->isAccelerated());
665 EXPECT_TRUE(bridge->isHibernating());
666
667 // Rendering in the background -> temp switch to SW
668 EXPECT_CALL(*mockLoggerPtr, reportHibernationEvent(Canvas2DLayerBridge::Hibe rnationEndedWithSwitchToBackgroundRendering));
669 EXPECT_CALL(mockImageBuffer, resetCanvas(_)).Times(AtLeast(1));
670 OwnPtr<WebWaitableEvent> switchEvent = adoptPtr(Platform::current()->createW aitableEvent());
671 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new RenderingTask(bridge .get(), switchEvent.get()));
672 switchEvent->wait();
673 ::testing::Mock::VerifyAndClearExpectations(mockLoggerPtr);
674 ::testing::Mock::VerifyAndClearExpectations(&mockImageBuffer);
675 EXPECT_FALSE(bridge->isAccelerated());
676 EXPECT_FALSE(bridge->isHibernating());
677
678 // Unhide
679 EXPECT_CALL(mockImageBuffer, resetCanvas(_)).Times(AtLeast(1));
680 OwnPtr<WebWaitableEvent> unhideEvent = adoptPtr(Platform::current()->createW aitableEvent());
681 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new SetIsHiddenTask(brid ge.get(), false, unhideEvent.get()));
682 unhideEvent->wait();
683 ::testing::Mock::VerifyAndClearExpectations(mockLoggerPtr);
684 ::testing::Mock::VerifyAndClearExpectations(&mockImageBuffer);
685 EXPECT_TRUE(bridge->isAccelerated()); // Becoming visible causes switch back to GPU
686 EXPECT_FALSE(bridge->isHibernating());
687
688 // Tear down the bridge on the thread so that 'bridge' can go out of scope
689 // without crashing due to thread checks
690 EXPECT_CALL(mockImageBuffer, resetCanvas(_)).Times(AnyNumber());
691 OwnPtr<WebWaitableEvent> bridgeDestroyedEvent = adoptPtr(Platform::current() ->createWaitableEvent());
692 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new DestroyBridgeTask(&b ridge, bridgeDestroyedEvent.get()));
693 bridgeDestroyedEvent->wait();
694 }
695
566 TEST_F(Canvas2DLayerBridgeTest, TeardownWhileHibernating) 696 TEST_F(Canvas2DLayerBridgeTest, TeardownWhileHibernating)
567 { 697 {
568 MockCanvasContext mainMock; 698 MockCanvasContext mainMock;
569 OwnPtr<WebThread> testThread = adoptPtr(Platform::current()->createThread("T estThread")); 699 OwnPtr<WebThread> testThread = adoptPtr(Platform::current()->createThread("T estThread"));
570 700
571 // The Canvas2DLayerBridge has to be created on the thread that will use it 701 // The Canvas2DLayerBridge has to be created on the thread that will use it
572 // to avoid WeakPtr thread check issues. 702 // to avoid WeakPtr thread check issues.
573 Canvas2DLayerBridgePtr bridge; 703 Canvas2DLayerBridgePtr bridge;
574 OwnPtr<WebWaitableEvent> bridgeCreatedEvent = adoptPtr(Platform::current()-> createWaitableEvent()); 704 OwnPtr<WebWaitableEvent> bridgeCreatedEvent = adoptPtr(Platform::current()-> createWaitableEvent());
575 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new CreateBridgeTask(&br idge, &mainMock, this, bridgeCreatedEvent.get())); 705 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new CreateBridgeTask(&br idge, &mainMock, this, bridgeCreatedEvent.get()));
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 // Tear down the bridge on the thread so that 'bridge' can go out of scope 909 // Tear down the bridge on the thread so that 'bridge' can go out of scope
780 // without crashing due to thread checks 910 // without crashing due to thread checks
781 OwnPtr<WebWaitableEvent> bridgeDestroyedEvent = adoptPtr(Platform::current() ->createWaitableEvent()); 911 OwnPtr<WebWaitableEvent> bridgeDestroyedEvent = adoptPtr(Platform::current() ->createWaitableEvent());
782 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new DestroyBridgeTask(&b ridge, bridgeDestroyedEvent.get())); 912 testThread->taskRunner()->postTask(BLINK_FROM_HERE, new DestroyBridgeTask(&b ridge, bridgeDestroyedEvent.get()));
783 bridgeDestroyedEvent->wait(); 913 bridgeDestroyedEvent->wait();
784 914
785 ::testing::Mock::VerifyAndClearExpectations(&mainMock); 915 ::testing::Mock::VerifyAndClearExpectations(&mainMock);
786 } 916 }
787 917
788 } // namespace blink 918 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698