1759class CCLayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers : public CCLayerTreeHostTest {
1760public:
1761
1762 CCLayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers()
1763 : m_rootLayer(ContentLayerChromium::create(&m_delegate))
1764 , m_childLayer(ContentLayerChromium::create(&m_delegate))
1765 {
1766 }
1767
1768 virtual void beginTest()
1769 {
1770 // The device viewport should be scaled by the device scale factor.
1771
1772 // Set viewport then scale factor.
1773 m_layerTreeHost->setViewportSize(IntSize(30, 30));
1774 m_layerTreeHost->setDeviceScaleFactor(2);
1775 EXPECT_EQ(IntSize(30, 30), m_layerTreeHost->viewportSize());
1776 EXPECT_EQ(IntSize(60, 60), m_layerTreeHost->deviceViewportSize());
1777
1778 // Set viewport after scale factor.
1779 m_layerTreeHost->setViewportSize(IntSize(40, 40));
1780 EXPECT_EQ(IntSize(40, 40), m_layerTreeHost->viewportSize());
1781 EXPECT_EQ(IntSize(80, 80), m_layerTreeHost->deviceViewportSize());
1782
1783 m_rootLayer->addChild(m_childLayer);
1784
1785 m_rootLayer->setIsDrawable(true);
1786 m_rootLayer->setBounds(IntSize(30, 30));
1787 m_rootLayer->setAnchorPoint(FloatPoint(0, 0));
1788 EXPECT_EQ(1, m_rootLayer->contentsScale());
1789 EXPECT_EQ(IntSize(30, 30), m_rootLayer->contentBounds());
1790
1791 m_childLayer->setIsDrawable(true);
1792 m_childLayer->setPosition(IntPoint(2, 2));
1793 m_childLayer->setBounds(IntSize(10, 10));
1794 m_childLayer->setAnchorPoint(FloatPoint(0, 0));
1795 EXPECT_EQ(1, m_childLayer->contentsScale());
1796 EXPECT_EQ(IntSize(10, 10), m_childLayer->contentBounds());
1797
1798 m_layerTreeHost->setRootLayer(m_rootLayer);
1799
1800 // Each layer's contents scale reflects the device scale factor.
1801 EXPECT_EQ(2, m_rootLayer->contentsScale());
1802 EXPECT_EQ(IntSize(60, 60), m_rootLayer->contentBounds());
1803 EXPECT_EQ(2, m_childLayer->contentsScale());
1804 EXPECT_EQ(IntSize(20, 20), m_childLayer->contentBounds());
1805 }
1806
1807 virtual void commitCompleteOnCCThread(CCLayerTreeHostImpl* impl)
1808 {
1809 // Get access to protected methods.
1810 MockLayerTreeHostImpl* mockImpl = static_cast<MockLayerTreeHostImpl*>(impl);
1811
1812 // Should only do one commit.
1813 EXPECT_EQ(0, impl->sourceFrameNumber());
1814 // Device scale factor should come over to impl.
1815 EXPECT_EQ(2, impl->deviceScaleFactor());
1816
1817 // Both layers are on impl.
1818 ASSERT_EQ(1u, impl->rootLayer()->children().size());
1819
1820 // Device viewport is scaled.
1821 EXPECT_EQ(IntSize(40, 40), impl->viewportSize());
1822 EXPECT_EQ(IntSize(80, 80), impl->deviceViewportSize());
1823
1824 CCLayerImpl* root = impl->rootLayer();
1825 CCLayerImpl* child = impl->rootLayer()->children()[0].get();
1826
1827 // Content sizes are scaled.
1828 EXPECT_EQ(IntSize(60, 60), root->contentBounds());
1829 EXPECT_EQ(IntSize(20, 20), child->contentBounds());
1830
1831 // Positions remain in DIP.
1832 EXPECT_EQ(IntPoint(0, 0), root->position());
1833 EXPECT_EQ(IntPoint(2, 2), child->position());
1834
1835 // Compute all the layer transforms for the frame.
1836 MockLayerTreeHostImpl::CCLayerList renderSurfaceLayerList;
1837 mockImpl->calculateRenderSurfaceLayerList(renderSurfaceLayerList);
1838
1839 // Both layers should be drawing into the root render surface.
1840 ASSERT_EQ(1u, renderSurfaceLayerList.size());
1841 ASSERT_EQ(root->renderSurface(), renderSurfaceLayerList[0]->renderSurface());
1842 ASSERT_EQ(2u, root->renderSurface()->layerList().size());
1843
1844 // The root render surface is the size of the viewport.
1845 EXPECT_EQ_RECT(IntRect(0, 0, 80, 80), root->renderSurface()->contentRect());
1846
1847 TransformationMatrix scaleTransform;
1848 scaleTransform.scale(impl->deviceScaleFactor());
1849
1850 // The root layer is scaled by 2x.
1851 TransformationMatrix rootScreenSpaceTransform = scaleTransform;
1852 TransformationMatrix rootDrawTransform = scaleTransform;
1853 rootDrawTransform.translate(root->bounds().width() * 0.5, root->bounds().height() * 0.5);
1854
1855 EXPECT_EQ(rootDrawTransform, root->drawTransform());
1856 EXPECT_EQ(rootScreenSpaceTransform, root->screenSpaceTransform());
1857
1858 // The child is at position 2,2, so translate by 2,2 before applying the scale by 2x.
1859 TransformationMatrix childScreenSpaceTransform = scaleTransform;
1860 childScreenSpaceTransform.translate(2, 2);
1861 TransformationMatrix childDrawTransform = scaleTransform;
1862 childDrawTransform.translate(2, 2);
1863 childDrawTransform.translate(child->bounds().width() * 0.5, child->bounds().height() * 0.5);
1864
1865 EXPECT_EQ(childDrawTransform, child->drawTransform());
1866 EXPECT_EQ(childScreenSpaceTransform, child->screenSpaceTransform());
1867
1868 endTest();
1869 }
1870
1871 virtual void afterTest()
1872 {
1873 m_rootLayer.clear();
1874 m_childLayer.clear();
1875 }
1876
1877private:
1878 MockContentLayerDelegate m_delegate;
1879 RefPtr<ContentLayerChromium> m_rootLayer;
1880 RefPtr<ContentLayerChromium> m_childLayer;
1881};
1882
1883TEST_F(CCLayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers, runMultiThread)
1884{
1885 runTest(true);
1886}
1887